Skip to content

Commit b8dd0df

Browse files
committed
source/cpu: Detect E/P cores and expose IDs through labels
Detect which CPUs are which types of the cores (P-cores or E-cores) and expose IDs through labels. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
1 parent 6644b6a commit b8dd0df

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

docs/usage/features.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ feature.node.kubernetes.io/<feature> = <value>
4848
| ----------------------------------- | ------ | --------------------------------------------------------------------------- |
4949
| **`cpu-cpuid.<cpuid-flag>`** | true | CPU capability is supported. **NOTE:** the capability might be supported but not enabled. |
5050
| **`cpu-hardware_multithreading`** | true | Hardware multithreading, such as Intel HTT, enabled (number of logical CPUs is greater than physical CPUs) |
51+
| **`cpu-e_cores`** | string | List of E core CPU IDs |
52+
| **`cpu-p_cores`** | string | List of P core CPU IDs |
5153
| **`cpu-coprocessor.nx_gzip`** | true | Nest Accelerator for GZIP is supported(Power). |
5254
| **`cpu-power.sst_bf.enabled`** | true | Intel SST-BF ([Intel Speed Select Technology][intel-sst] - Base frequency) enabled |
5355
| **`cpu-pstate.status`** | string | The status of the [Intel pstate][intel-pstate] driver when in use and enabled, either 'active' or 'passive'. |

source/cpu/cpu.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ func (s *cpuSource) GetLabels() (source.FeatureLabels, error) {
194194
labels["coprocessor.nx_gzip"] = v
195195
}
196196

197+
// E cores
198+
if v, ok := features.Attributes[TopologyFeature].Elements["e_cores"]; ok {
199+
labels["e_cores"] = v
200+
}
201+
202+
// P cores
203+
if v, ok := features.Attributes[TopologyFeature].Elements["p_cores"]; ok {
204+
labels["p_cores"] = v
205+
}
206+
197207
return labels, nil
198208
}
199209

@@ -259,6 +269,27 @@ func getCPUModel() map[string]string {
259269
return cpuModelInfo
260270
}
261271

272+
func getCPUID(name string) int {
273+
id := 0
274+
base := 1
275+
for idx := len(name) - 1; idx > 0; idx-- {
276+
d := name[idx]
277+
278+
if '0' <= d && d <= '9' {
279+
id += base * (int(d) - '0')
280+
base *= 10
281+
} else {
282+
if base > 1 {
283+
return id
284+
}
285+
286+
return -1
287+
}
288+
}
289+
290+
return -1
291+
}
292+
262293
func discoverTopology() map[string]string {
263294
features := make(map[string]string)
264295

@@ -270,8 +301,13 @@ func discoverTopology() map[string]string {
270301

271302
ht := false
272303
uniquePhysicalIDs := sets.NewString()
304+
coreIDs := sets.NewString()
305+
pCoreIDs := sets.NewString()
306+
eCoreIDs := sets.NewString()
273307

274308
for _, file := range files {
309+
coreIDs.Insert(strconv.Itoa(getCPUID(file.Name())))
310+
275311
// Try to read siblings from topology
276312
siblings, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
277313
if err != nil {
@@ -299,6 +335,31 @@ func discoverTopology() map[string]string {
299335
features["hardware_multithreading"] = strconv.FormatBool(ht)
300336
features["socket_count"] = strconv.FormatInt(int64(uniquePhysicalIDs.Len()), 10)
301337

338+
for _, entry := range files {
339+
340+
// Try to read core_cpus_list from topology
341+
cpus, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", entry.Name(), "topology/core_cpus_list"))
342+
if err != nil {
343+
klog.ErrorS(err, "error reading core_cpus_list file")
344+
return map[string]string{}
345+
}
346+
347+
if id := strings.Split(string(cpus), "-"); len(id) == 2 {
348+
if _, ok := coreIDs[id[0]]; ok {
349+
pCoreIDs.Insert(id[0])
350+
}
351+
if _, ok := coreIDs[id[1]]; ok {
352+
pCoreIDs.Insert(id[1])
353+
}
354+
} else {
355+
id1 := strings.Split(string(id[0]), "\n")
356+
eCoreIDs.Insert(id1[0])
357+
}
358+
}
359+
360+
features["e_cores"] = strings.Join(eCoreIDs.List(), "_")
361+
features["p_cores"] = strings.Join(pCoreIDs.List(), "_")
362+
302363
return features
303364
}
304365

0 commit comments

Comments
 (0)