// 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 topologyimport ()// CacheType identifies what a cache stores. The values match the type field// of CPUID leaves 0x04 / 0x8000001D.typeCacheTypeuint8const (DataCacheType = iota + 1// Data cacheInstruction// Instruction cacheUnified// Unified cache (holds both))func ( CacheType) () string {switch {caseData:return"Data"caseInstruction:return"Instruction"caseUnified: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.typeCoreTypeuint8const (UnknownCoreType = iota// Type could not be determinedPerformance// Intel P-core (leaf 0x1A == 0x40), AMD classic Zen coreEfficiency// Intel E-core (leaf 0x1A == 0x20)LPEfficiency// Intel low-power E-core on the SoC tileDense// AMD dense core (Zen 4c / 5c))func ( CoreType) () string {switch {casePerformance:return"Performance"caseEfficiency:return"Efficiency"caseLPEfficiency:return"LP-Efficiency"caseDense: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.typeCachestruct { 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.typeThreadstruct { 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.typeCorestruct { 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 { returnlen(.Threads) }// TotalCache returns the size in bytes of the core-private caches of the// given level and type.func ( *Core) ( int, CacheType) int { returnsumCaches(.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.typeGroupstruct { 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 { returnlen(.Cores) }// NumThreads returns the number of logical processors below the group.func ( *Group) () int { returncountThreads(.Cores) }// CoresByType returns a count of cores per CoreType within the group.func ( *Group) () map[CoreType]int { returncoresByType(.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 {returnsumCaches(.Caches, , ) + coresCache(.Cores, , )}// Package is a socket / physical CPU.typePackagestruct { ID int Groups []Group Caches []Cache// Package-wide caches, if any}// NumCores returns the number of cores in the package.func ( *Package) () int { := 0for := range .Groups { += len(.Groups[].Cores) }return}// NumThreads returns the number of logical processors in the package.func ( *Package) () int { := 0for := 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 []Corefor := 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.typeSystemstruct { 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 { returnlen(.Packages) }// NumCores returns the total number of physical cores in the system.func ( *System) () int { := 0for := range .Packages { += .Packages[].NumCores() }return}// NumThreads returns the total number of logical processors in the system.func ( *System) () int { := 0for := 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 []Corefor := 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 { := 0for := range .Packages { += .Packages[].TotalCache(, ) }return}func sumCaches( []Cache, int, CacheType) int { := 0for , := range {if .Level == && .Type == { += .Size } }return}func coresCache( []Core, int, CacheType) int { := 0for := range { += sumCaches([].Caches, , ) }return}func countThreads( []Core) int { := 0for := 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 {varstrings.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 != {returnfalse } }returntrue}func cacheSuffix( []Cache) string {iflen() == 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 {caseData:return"d"caseInstruction:return"i" }return""}func threadList( []Thread) string {varstrings.Builderfor , := range {if > 0 { .WriteByte(',') }fmt.Fprintf(&, " %d", .ID) }return .String()}func humanBytes( int) string {switch {case <= 0:return"0"case %(1<<20) == 0:returnfmt.Sprintf("%dMB", >>20)case %(1<<10) == 0:returnfmt.Sprintf("%dKB", >>10)default:returnfmt.Sprintf("%dB", ) }}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.