@@ -32,27 +32,33 @@ func TestGetCPU(t *testing.T) {
3232 {
3333 name : "successful host cpu info" ,
3434 fixtures : testfixture {
35+ // Simulate a Linux host: 2 sockets, 2 cores each, 2 threads per core = 8 logical CPUs.
36+ // On Linux, cpu.InfoWithContext returns one InfoStat per logical CPU with Cores=1,
37+ // so PhysicalID and CoreID must be set to identify unique physical cores.
3538 cpuInfo : func (context.Context ) ([]cpu.InfoStat , error ) {
3639 return []cpu.InfoStat {
37- {
38- ModelName : "testmodelname" ,
39- Cores : 4 ,
40- },
41- {
42- ModelName : "testmodelname2" ,
43- Cores : 4 ,
44- },
40+ {ModelName : "testmodelname" , Cores : 1 , PhysicalID : "0" , CoreID : "0" },
41+ { ModelName : "testmodelname" , Cores : 1 , PhysicalID : "0" , CoreID : "0" }, // hyperthreading sibling
42+ { ModelName : "testmodelname" , Cores : 1 , PhysicalID : "0" , CoreID : "1" } ,
43+ { ModelName : "testmodelname" , Cores : 1 , PhysicalID : "0" , CoreID : "1" }, // hyperthreading sibling
44+ {ModelName : "testmodelname2" , Cores : 1 , PhysicalID : "1" , CoreID : "0" },
45+ { ModelName : "testmodelname2" , Cores : 1 , PhysicalID : "1" , CoreID : "0" }, // hyperthreading sibling
46+ { ModelName : "testmodelname2" , Cores : 1 , PhysicalID : "1" , CoreID : "1" } ,
47+ { ModelName : "testmodelname2" , Cores : 1 , PhysicalID : "1" , CoreID : "1" }, // hyperthreading sibling
4548 }, nil
4649 },
47- cpuCounts : func (context.Context , bool ) (int , error ) {
48- return 2 , nil
50+ cpuCounts : func (_ context.Context , logical bool ) (int , error ) {
51+ if logical {
52+ return 8 , nil // 2 sockets * 2 cores * 2 threads
53+ }
54+ return 2 , nil // 2 physical sockets
4955 },
5056 },
5157 wantInfo : map [string ]string {
5258 "host_physical_cpus" : "2" ,
53- "host_cpu_cores" : "8 " ,
59+ "host_cpu_cores" : "4 " ,
5460 "host_cpu_model" : "testmodelname2" ,
55- "host_logical_cpus" : "2 " ,
61+ "host_logical_cpus" : "8 " ,
5662 },
5763 },
5864 {
@@ -90,7 +96,7 @@ func TestGetCPU(t *testing.T) {
9096 },
9197 },
9298 wantInfo : map [string ]string {
93- "host_physical_cpus" : "2" ,
99+ "host_physical_cpus" : "0" , // cpuCounts(false) fails, HostPhysicalCPUs stays at zero
94100 "host_cpu_cores" : "0" ,
95101 "host_cpu_model" : "" ,
96102 "host_logical_cpus" : "0" ,
0 commit comments