// Copyright (c) 2026 Klaus Post, released under MIT License. See LICENSE file.

// 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.
package topology import ( ) // CacheType identifies what a cache stores. The values match the type field // of CPUID leaves 0x04 / 0x8000001D. type CacheType uint8 const ( Data CacheType = iota + 1 // Data cache Instruction // Instruction cache Unified // Unified cache (holds both) ) func ( CacheType) () string { switch { case Data: return "Data" case Instruction: return "Instruction" case Unified: return "Unified" } return "Unknown" } // MarshalText renders the cache type as its name, so it serializes as a string // in JSON and other text encodings rather than a number. func ( CacheType) () ([]byte, error) { return []byte(.String()), nil } // CoreType is the microarchitectural class of a core. On heterogeneous // ("hybrid") CPUs this distinguishes performance from efficiency cores. type CoreType uint8 const ( Unknown CoreType = iota // Type could not be determined Performance // Intel P-core (leaf 0x1A == 0x40), AMD classic Zen core Efficiency // Intel E-core (leaf 0x1A == 0x20) LPEfficiency // Intel low-power E-core on the SoC tile Dense // AMD dense core (Zen 4c / 5c) ) func ( CoreType) () string { switch { case Performance: return "Performance" case Efficiency: return "Efficiency" case LPEfficiency: return "LP-Efficiency" case Dense: return "Dense" } return "Unknown" } // MarshalText renders the core type as its name for JSON and other text // encodings. func ( CoreType) () ([]byte, error) { return []byte(.String()), nil } // Cache describes a single cache instance. type Cache struct { Level int // 1, 2, 3, … Type CacheType // Data, Instruction or Unified Size int // Size in bytes of this instance LineSize int // Line size in bytes Ways int // Associativity Sets int // Number of sets SharedThreads int // Logical processors that share this instance Inclusive bool // Inclusive of lower cache levels } // Thread is a single logical processor. type Thread struct { APICID uint32 // (x2)APIC id ID int // OS logical CPU number if known from a scan, else the enumeration index } // Core is a single physical core and the threads it runs. type Core struct { ID int Type CoreType Model string // Microarchitecture label if known, e.g. "Golden Cove" Threads []Thread // More than one indicates SMT / hyper-threading Caches []Cache // Caches private to this core, typically L1 and L2 } // NumThreads returns the number of logical processors on the core. func ( *Core) () int { return len(.Threads) } // TotalCache returns the size in bytes of the core-private caches of the // given level and type. func ( *Core) ( int, CacheType) int { return sumCaches(.Caches, , ) } // 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. type Group struct { ID int NUMANode int // AMD node id (leaf 0x8000001E ECX); 0 when unknown or single-node Cores []Core // Cores in the group Caches []Cache // Caches shared by the whole group, typically L3 } // NumCores returns the number of cores in the group. func ( *Group) () int { return len(.Cores) } // NumThreads returns the number of logical processors below the group. func ( *Group) () int { return countThreads(.Cores) } // CoresByType returns a count of cores per CoreType within the group. func ( *Group) () map[CoreType]int { return coresByType(.Cores) } // TotalCache sums the caches of the given level and type at and below the // group, counting each instance once. func ( *Group) ( int, CacheType) int { return sumCaches(.Caches, , ) + coresCache(.Cores, , ) } // Package is a socket / physical CPU. type Package struct { ID int Groups []Group Caches []Cache // Package-wide caches, if any } // NumCores returns the number of cores in the package. func ( *Package) () int { := 0 for := range .Groups { += len(.Groups[].Cores) } return } // NumThreads returns the number of logical processors in the package. func ( *Package) () int { := 0 for := range .Groups { += .Groups[].NumThreads() } return } // CoresByType returns a count of cores per CoreType within the package. func ( *Package) () map[CoreType]int { := map[CoreType]int{} for := range .Groups { mergeCounts(, .Groups[].Cores) } return } // Cores returns every core in the package. func ( *Package) () []Core { var []Core for := range .Groups { = append(, .Groups[].Cores...) } return } // TotalCache sums the caches of the given level and type at and below the // package, counting each instance once. func ( *Package) ( int, CacheType) int { := sumCaches(.Caches, , ) for := range .Groups { += .Groups[].TotalCache(, ) } return } // System is the root of the topology: the whole machine. type System struct { Vendor string // CPU vendor string Scanned bool // true if built from a per-core scan, false if extrapolated from one core Online int // Logical processors observed Packages []Package // Physical packages / sockets } // NumPackages returns the number of packages (sockets). func ( *System) () int { return len(.Packages) } // NumCores returns the total number of physical cores in the system. func ( *System) () int { := 0 for := range .Packages { += .Packages[].NumCores() } return } // NumThreads returns the total number of logical processors in the system. func ( *System) () int { := 0 for := range .Packages { += .Packages[].NumThreads() } return } // CoresByType returns a count of cores per CoreType across the system. func ( *System) () map[CoreType]int { := map[CoreType]int{} for := range .Packages { for := range .Packages[].Groups { mergeCounts(, .Packages[].Groups[].Cores) } } return } // Cores returns every core in the system. func ( *System) () []Core { var []Core for := range .Packages { = append(, .Packages[].Cores()...) } return } // 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. func ( *System) ( int, CacheType) int { := 0 for := range .Packages { += .Packages[].TotalCache(, ) } return } func sumCaches( []Cache, int, CacheType) int { := 0 for , := range { if .Level == && .Type == { += .Size } } return } func coresCache( []Core, int, CacheType) int { := 0 for := range { += sumCaches([].Caches, , ) } return } func countThreads( []Core) int { := 0 for := range { += len([].Threads) } return } func coresByType( []Core) map[CoreType]int { := map[CoreType]int{} mergeCounts(, ) return } func mergeCounts( map[CoreType]int, []Core) { for := range { [[].Type]++ } } // String renders the topology as an indented tree, followed by a summary. func ( *System) () string { var strings.Builder := "extrapolated from one core" if .Scanned { = "scanned" } fmt.Fprintf(&, "System: %s (%s) — %d package(s), %d core(s), %d thread(s)\n", .Vendor, , .NumPackages(), .NumCores(), .NumThreads()) for := range .Packages { := &.Packages[] fmt.Fprintf(&, " Package #%d%s\n", .ID, cacheSuffix(.Caches)) for := range .Groups { := &.Groups[] := "" if .NUMANode != 0 { = fmt.Sprintf(" NUMA#%d", .NUMANode) } fmt.Fprintf(&, " Group #%d%s%s\n", .ID, , cacheSuffix(.Caches)) for := range .Cores { := &.Cores[] := "" if .Model != "" { = " " + .Model } fmt.Fprintf(&, " Core #%d (%s%s) threads:%s%s\n", .ID, .Type, , threadList(.Threads), cacheSuffix(.Caches)) } } } if := .CoresByType(); len() > 1 || !hasOnly(, Performance) { .WriteString(" Cores by type:") for , := range []CoreType{Performance, Efficiency, LPEfficiency, Dense, Unknown} { if := []; > 0 { fmt.Fprintf(&, " %s=%d", , ) } } .WriteByte('\n') } fmt.Fprintf(&, " Total cache: L1D %s, L1I %s, L2 %s, L3 %s\n", humanBytes(.TotalCache(1, Data)), humanBytes(.TotalCache(1, Instruction)), humanBytes(.TotalCache(2, Unified)), humanBytes(.TotalCache(3, Unified))) return .String() } func hasOnly( map[CoreType]int, CoreType) bool { for := range { if != { return false } } return true } func cacheSuffix( []Cache) string { if len() == 0 { return "" } := make([]string, 0, len()) for , := range { = append(, fmt.Sprintf("L%d%s %s", .Level, cacheShort(.Type), humanBytes(.Size))) } return " [" + strings.Join(, " ") + "]" } func cacheShort( CacheType) string { switch { case Data: return "d" case Instruction: return "i" } return "" } func threadList( []Thread) string { var strings.Builder for , := range { if > 0 { .WriteByte(',') } fmt.Fprintf(&, " %d", .ID) } return .String() } func humanBytes( int) string { switch { case <= 0: return "0" case %(1<<20) == 0: return fmt.Sprintf("%dMB", >>20) case %(1<<10) == 0: return fmt.Sprintf("%dKB", >>10) default: return fmt.Sprintf("%dB", ) } }