package topology

Import Path
	github.com/klauspost/cpuid/v2/topology (on go.dev)

Dependency Relation
	imports 2 packages, and imported by one package

Involved Source Files Package topology describes the layout of a system's CPUs as a fixed hierarchy: System → Package → Group → Core → Thread. A Package is a socket (a physical "CPU"). A Group is a set of cores that share a last-level cache — an AMD CCX/CCD or an Intel LLC tile; there is always at least one, so a monolithic chip has a single Group holding the whole L3. A Core carries its type (performance/efficiency/…), its shared per-core caches and its logical Threads. Caches are attached to the level that owns them: L1/L2 on a Core, the shared L3 on a Group, a package-wide last-level cache on a Package. Because every instance is stored exactly once, the TotalCache aggregate never double-counts: System.TotalCache(3, Unified) is the combined L3 across all groups (e.g. all CCDs). The package holds types and queries only; it is filled in by the cpuid package and has no dependencies of its own.
Code Examples package main import ( "fmt" "github.com/klauspost/cpuid/v2" "github.com/klauspost/cpuid/v2/topology" ) func main() { // ScanTopology pins the calling goroutine to every online CPU to build an // exact tree. For a cheaper approximation that does no pinning, use // cpuid.CPU.Topology() instead. On unsupported platforms ScanTopology // returns a best-effort snapshot together with a non-nil error. sys, _ := cpuid.ScanTopology() fmt.Printf("%s: %d package(s), %d cores, %d threads\n", sys.Vendor, sys.NumPackages(), sys.NumCores(), sys.NumThreads()) fmt.Printf("performance cores: %d, efficiency cores: %d\n", sys.CoresByType()[topology.Performance], sys.CoresByType()[topology.Efficiency]) fmt.Printf("total L3: %d bytes\n", sys.TotalCache(3, topology.Unified)) // Output varies by machine, so it is not checked here. The method examples // below use a fixed synthetic topology for reproducible output. } package main import ( "fmt" "github.com/klauspost/cpuid/v2/internal/samplecpu" "github.com/klauspost/cpuid/v2/topology" ) func main() { sys := samplecpu.Topology() byType := sys.CoresByType() fmt.Printf("performance: %d\n", byType[topology.Performance]) fmt.Printf("efficiency: %d\n", byType[topology.Efficiency]) fmt.Printf("threads: %d\n", sys.NumThreads()) } package main import ( "fmt" "github.com/klauspost/cpuid/v2/internal/samplecpu" ) func main() { fmt.Print(samplecpu.Topology()) } package main import ( "fmt" "github.com/klauspost/cpuid/v2/internal/samplecpu" "github.com/klauspost/cpuid/v2/topology" ) func main() { sys := samplecpu.Topology() fmt.Printf("L3 total: %d MB\n", sys.TotalCache(3, topology.Unified)>>20) fmt.Printf("L3 in group #0: %d MB\n", sys.Packages[0].Groups[0].TotalCache(3, topology.Unified)>>20) fmt.Printf("L2 total: %d MB\n", sys.TotalCache(2, topology.Unified)>>20) }
Package-Level Type Names (total 8)
/* sort by: | */
Cache describes a single cache instance. // Inclusive of lower cache levels // 1, 2, 3, … // Line size in bytes // Number of sets // Logical processors that share this instance // Size in bytes of this instance // Data, Instruction or Unified // Associativity
CacheType identifies what a cache stores. The values match the type field of CPUID leaves 0x04 / 0x8000001D. MarshalText renders the cache type as its name, so it serializes as a string in JSON and other text encodings rather than a number. ( CacheType) String() string CacheType : github.com/pancsta/asyncmachine-go/internal/nofmt.Stringer CacheType : github.com/pandatix/go-abnf.ElemItf CacheType : encoding.TextMarshaler CacheType : expvar.Var CacheType : fmt.Stringer func (*Core).TotalCache(level int, t CacheType) int func (*Group).TotalCache(level int, t CacheType) int func (*Package).TotalCache(level int, t CacheType) int func (*System).TotalCache(level int, t CacheType) int const Data const Instruction const Unified
Core is a single physical core and the threads it runs. // Caches private to this core, typically L1 and L2 ID int // Microarchitecture label if known, e.g. "Golden Cove" // More than one indicates SMT / hyper-threading Type CoreType NumThreads returns the number of logical processors on the core. TotalCache returns the size in bytes of the core-private caches of the given level and type. func (*Package).Cores() []Core func (*System).Cores() []Core
CoreType is the microarchitectural class of a core. On heterogeneous ("hybrid") CPUs this distinguishes performance from efficiency cores. MarshalText renders the core type as its name for JSON and other text encodings. ( CoreType) String() string CoreType : github.com/pancsta/asyncmachine-go/internal/nofmt.Stringer CoreType : github.com/pandatix/go-abnf.ElemItf CoreType : encoding.TextMarshaler CoreType : expvar.Var CoreType : fmt.Stringer func (*Group).CoresByType() map[CoreType]int func (*Package).CoresByType() map[CoreType]int func (*System).CoresByType() map[CoreType]int const Dense const Efficiency const LPEfficiency const Performance const Unknown
Group is a set of cores sharing a last-level cache — an AMD CCX/CCD or an Intel LLC tile. There is always at least one per package. // Caches shared by the whole group, typically L3 // Cores in the group ID int // AMD node id (leaf 0x8000001E ECX); 0 when unknown or single-node CoresByType returns a count of cores per CoreType within the group. NumCores returns the number of cores in the group. NumThreads returns the number of logical processors below the group. TotalCache sums the caches of the given level and type at and below the group, counting each instance once.
Package is a socket / physical CPU. // Package-wide caches, if any Groups []Group ID int Cores returns every core in the package. CoresByType returns a count of cores per CoreType within the package. NumCores returns the number of cores in the package. NumThreads returns the number of logical processors in the package. TotalCache sums the caches of the given level and type at and below the package, counting each instance once.
System is the root of the topology: the whole machine. // Logical processors observed // Physical packages / sockets // true if built from a per-core scan, false if extrapolated from one core // CPU vendor string Cores returns every core in the system. CoresByType returns a count of cores per CoreType across the system. NumCores returns the total number of physical cores in the system. NumPackages returns the number of packages (sockets). NumThreads returns the total number of logical processors in the system. String renders the topology as an indented tree, followed by a summary. TotalCache sums the caches of the given level and type across the whole system, counting each instance once. For example TotalCache(3, Unified) returns the combined L3 across all groups. *System : github.com/pancsta/asyncmachine-go/internal/nofmt.Stringer *System : github.com/pandatix/go-abnf.ElemItf *System : expvar.Var *System : fmt.Stringer func github.com/klauspost/cpuid/v2.ScanTopology() (*System, error) func github.com/klauspost/cpuid/v2.CPUInfo.Topology() *System
Thread is a single logical processor. // (x2)APIC id // OS logical CPU number if known from a scan, else the enumeration index
Package-Level Constants (total 8)
const Data CacheType = 1 // Data cache
const Dense CoreType = 4 // AMD dense core (Zen 4c / 5c)
const Efficiency CoreType = 2 // Intel E-core (leaf 0x1A == 0x20)
const Instruction CacheType = 2 // Instruction cache
const LPEfficiency CoreType = 3 // Intel low-power E-core on the SoC tile
const Performance CoreType = 1 // Intel P-core (leaf 0x1A == 0x40), AMD classic Zen core
const Unified CacheType = 3 // Unified cache (holds both)
const Unknown CoreType = 0 // Type could not be determined