Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | ============ |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 4 | x86 Topology |
| 5 | ============ |
| 6 | |
| 7 | This documents and clarifies the main aspects of x86 topology modelling and |
| 8 | representation in the kernel. Update/change when doing changes to the |
| 9 | respective code. |
| 10 | |
| 11 | The architecture-agnostic topology definitions are in |
Mauro Carvalho Chehab | 4f4cfa6 | 2019-06-27 14:56:51 -0300 | [diff] [blame] | 12 | Documentation/admin-guide/cputopology.rst. This file holds x86-specific |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 13 | differences/specialities which must not necessarily apply to the generic |
| 14 | definitions. Thus, the way to read up on Linux topology on x86 is to start |
| 15 | with the generic one and look at this one in parallel for the x86 specifics. |
| 16 | |
| 17 | Needless to say, code should use the generic functions - this file is *only* |
| 18 | here to *document* the inner workings of x86 topology. |
| 19 | |
| 20 | Started by Thomas Gleixner <tglx@linutronix.de> and Borislav Petkov <bp@alien8.de>. |
| 21 | |
| 22 | The main aim of the topology facilities is to present adequate interfaces to |
| 23 | code which needs to know/query/use the structure of the running system wrt |
| 24 | threads, cores, packages, etc. |
| 25 | |
| 26 | The kernel does not care about the concept of physical sockets because a |
| 27 | socket has no relevance to software. It's an electromechanical component. In |
| 28 | the past a socket always contained a single package (see below), but with the |
| 29 | advent of Multi Chip Modules (MCM) a socket can hold more than one package. So |
| 30 | there might be still references to sockets in the code, but they are of |
| 31 | historical nature and should be cleaned up. |
| 32 | |
| 33 | The topology of a system is described in the units of: |
| 34 | |
| 35 | - packages |
| 36 | - cores |
| 37 | - threads |
| 38 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 39 | Package |
| 40 | ======= |
| 41 | Packages contain a number of cores plus shared resources, e.g. DRAM |
| 42 | controller, shared caches etc. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 43 | |
Yazen Ghannam | 028c221 | 2020-11-09 21:06:56 +0000 | [diff] [blame] | 44 | Modern systems may also use the term 'Die' for package. |
| 45 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 46 | AMD nomenclature for package is 'Node'. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 47 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 48 | Package-related topology information in the kernel: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 49 | |
| 50 | - cpuinfo_x86.x86_max_cores: |
| 51 | |
| 52 | The number of cores in a package. This information is retrieved via CPUID. |
| 53 | |
Len Brown | 7745f03 | 2019-05-13 13:58:45 -0400 | [diff] [blame] | 54 | - cpuinfo_x86.x86_max_dies: |
| 55 | |
| 56 | The number of dies in a package. This information is retrieved via CPUID. |
| 57 | |
Yazen Ghannam | 028c221 | 2020-11-09 21:06:56 +0000 | [diff] [blame] | 58 | - cpuinfo_x86.cpu_die_id: |
| 59 | |
| 60 | The physical ID of the die. This information is retrieved via CPUID. |
| 61 | |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 62 | - cpuinfo_x86.phys_proc_id: |
| 63 | |
| 64 | The physical ID of the package. This information is retrieved via CPUID |
| 65 | and deduced from the APIC IDs of the cores in the package. |
| 66 | |
Yazen Ghannam | 028c221 | 2020-11-09 21:06:56 +0000 | [diff] [blame] | 67 | Modern systems use this value for the socket. There may be multiple |
| 68 | packages within a socket. This value may differ from cpu_die_id. |
| 69 | |
Len Brown | ef7c772 | 2019-02-26 01:19:59 -0500 | [diff] [blame] | 70 | - cpuinfo_x86.logical_proc_id: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 71 | |
| 72 | The logical ID of the package. As we do not trust BIOSes to enumerate the |
| 73 | packages in a consistent way, we introduced the concept of logical package |
| 74 | ID so we can sanely calculate the number of maximum possible packages in |
| 75 | the system and have the packages enumerated linearly. |
| 76 | |
| 77 | - topology_max_packages(): |
| 78 | |
| 79 | The maximum possible number of packages in the system. Helpful for per |
| 80 | package facilities to preallocate per package information. |
| 81 | |
Borislav Petkov | a268b5f | 2016-11-17 10:45:57 +0100 | [diff] [blame] | 82 | - cpu_llc_id: |
| 83 | |
| 84 | A per-CPU variable containing: |
Borislav Petkov | a268b5f | 2016-11-17 10:45:57 +0100 | [diff] [blame] | 85 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 86 | - On Intel, the first APIC ID of the list of CPUs sharing the Last Level |
| 87 | Cache |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 88 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 89 | - On AMD, the Node ID or Core Complex ID containing the Last Level |
| 90 | Cache. In general, it is a number identifying an LLC uniquely on the |
| 91 | system. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 92 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 93 | Cores |
| 94 | ===== |
| 95 | A core consists of 1 or more threads. It does not matter whether the threads |
| 96 | are SMT- or CMT-type threads. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 97 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 98 | AMDs nomenclature for a CMT core is "Compute Unit". The kernel always uses |
| 99 | "core". |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 100 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 101 | Core-related topology information in the kernel: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 102 | |
| 103 | - smp_num_siblings: |
| 104 | |
| 105 | The number of threads in a core. The number of threads in a package can be |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 106 | calculated by:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 107 | |
| 108 | threads_per_package = cpuinfo_x86.x86_max_cores * smp_num_siblings |
| 109 | |
| 110 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 111 | Threads |
| 112 | ======= |
| 113 | A thread is a single scheduling unit. It's the equivalent to a logical Linux |
| 114 | CPU. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 115 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 116 | AMDs nomenclature for CMT threads is "Compute Unit Core". The kernel always |
| 117 | uses "thread". |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 118 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 119 | Thread-related topology information in the kernel: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 120 | |
| 121 | - topology_core_cpumask(): |
| 122 | |
| 123 | The cpumask contains all online threads in the package to which a thread |
| 124 | belongs. |
| 125 | |
| 126 | The number of online threads is also printed in /proc/cpuinfo "siblings." |
| 127 | |
Dou Liyang | 0c52f7c | 2018-02-22 16:48:12 +0800 | [diff] [blame] | 128 | - topology_sibling_cpumask(): |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 129 | |
| 130 | The cpumask contains all online threads in the core to which a thread |
| 131 | belongs. |
| 132 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 133 | - topology_logical_package_id(): |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 134 | |
| 135 | The logical package ID to which a thread belongs. |
| 136 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 137 | - topology_physical_package_id(): |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 138 | |
| 139 | The physical package ID to which a thread belongs. |
| 140 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 141 | - topology_core_id(); |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 142 | |
| 143 | The ID of the core to which a thread belongs. It is also printed in /proc/cpuinfo |
| 144 | "core_id." |
| 145 | |
| 146 | |
| 147 | |
| 148 | System topology examples |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 149 | ======================== |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 150 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 151 | .. note:: |
| 152 | The alternative Linux CPU enumeration depends on how the BIOS enumerates the |
| 153 | threads. Many BIOSes enumerate all threads 0 first and then all threads 1. |
| 154 | That has the "advantage" that the logical Linux CPU numbers of threads 0 stay |
| 155 | the same whether threads are enabled or not. That's merely an implementation |
| 156 | detail and has no practical impact. |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 157 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 158 | 1) Single Package, Single Core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 159 | |
| 160 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 161 | |
| 162 | 2) Single Package, Dual Core |
| 163 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 164 | a) One thread per core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 165 | |
| 166 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 167 | -> [core 1] -> [thread 0] -> Linux CPU 1 |
| 168 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 169 | b) Two threads per core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 170 | |
| 171 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 172 | -> [thread 1] -> Linux CPU 1 |
| 173 | -> [core 1] -> [thread 0] -> Linux CPU 2 |
| 174 | -> [thread 1] -> Linux CPU 3 |
| 175 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 176 | Alternative enumeration:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 177 | |
| 178 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 179 | -> [thread 1] -> Linux CPU 2 |
| 180 | -> [core 1] -> [thread 0] -> Linux CPU 1 |
| 181 | -> [thread 1] -> Linux CPU 3 |
| 182 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 183 | AMD nomenclature for CMT systems:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 184 | |
| 185 | [node 0] -> [Compute Unit 0] -> [Compute Unit Core 0] -> Linux CPU 0 |
| 186 | -> [Compute Unit Core 1] -> Linux CPU 1 |
| 187 | -> [Compute Unit 1] -> [Compute Unit Core 0] -> Linux CPU 2 |
| 188 | -> [Compute Unit Core 1] -> Linux CPU 3 |
| 189 | |
| 190 | 4) Dual Package, Dual Core |
| 191 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 192 | a) One thread per core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 193 | |
| 194 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 195 | -> [core 1] -> [thread 0] -> Linux CPU 1 |
| 196 | |
| 197 | [package 1] -> [core 0] -> [thread 0] -> Linux CPU 2 |
| 198 | -> [core 1] -> [thread 0] -> Linux CPU 3 |
| 199 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 200 | b) Two threads per core:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 201 | |
| 202 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 203 | -> [thread 1] -> Linux CPU 1 |
| 204 | -> [core 1] -> [thread 0] -> Linux CPU 2 |
| 205 | -> [thread 1] -> Linux CPU 3 |
| 206 | |
| 207 | [package 1] -> [core 0] -> [thread 0] -> Linux CPU 4 |
| 208 | -> [thread 1] -> Linux CPU 5 |
| 209 | -> [core 1] -> [thread 0] -> Linux CPU 6 |
| 210 | -> [thread 1] -> Linux CPU 7 |
| 211 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 212 | Alternative enumeration:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 213 | |
| 214 | [package 0] -> [core 0] -> [thread 0] -> Linux CPU 0 |
| 215 | -> [thread 1] -> Linux CPU 4 |
| 216 | -> [core 1] -> [thread 0] -> Linux CPU 1 |
| 217 | -> [thread 1] -> Linux CPU 5 |
| 218 | |
| 219 | [package 1] -> [core 0] -> [thread 0] -> Linux CPU 2 |
| 220 | -> [thread 1] -> Linux CPU 6 |
| 221 | -> [core 1] -> [thread 0] -> Linux CPU 3 |
| 222 | -> [thread 1] -> Linux CPU 7 |
| 223 | |
Changbin Du | 848942c | 2019-05-08 23:21:17 +0800 | [diff] [blame] | 224 | AMD nomenclature for CMT systems:: |
Borislav Petkov | f7be861 | 2016-03-28 11:56:09 +0200 | [diff] [blame] | 225 | |
| 226 | [node 0] -> [Compute Unit 0] -> [Compute Unit Core 0] -> Linux CPU 0 |
| 227 | -> [Compute Unit Core 1] -> Linux CPU 1 |
| 228 | -> [Compute Unit 1] -> [Compute Unit Core 0] -> Linux CPU 2 |
| 229 | -> [Compute Unit Core 1] -> Linux CPU 3 |
| 230 | |
| 231 | [node 1] -> [Compute Unit 0] -> [Compute Unit Core 0] -> Linux CPU 4 |
| 232 | -> [Compute Unit Core 1] -> Linux CPU 5 |
| 233 | -> [Compute Unit 1] -> [Compute Unit Core 0] -> Linux CPU 6 |
| 234 | -> [Compute Unit Core 1] -> Linux CPU 7 |