Skip to content

Commit

Permalink
source/cpu: Detect E/P cores and expose IDs through labels
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ozhuraki committed Jun 7, 2024
1 parent 6644b6a commit b8dd0df
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/usage/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ feature.node.kubernetes.io/<feature> = <value>
| ----------------------------------- | ------ | --------------------------------------------------------------------------- |
| **`cpu-cpuid.<cpuid-flag>`** | true | CPU capability is supported. **NOTE:** the capability might be supported but not enabled. |
| **`cpu-hardware_multithreading`** | true | Hardware multithreading, such as Intel HTT, enabled (number of logical CPUs is greater than physical CPUs) |
| **`cpu-e_cores`** | string | List of E core CPU IDs |
| **`cpu-p_cores`** | string | List of P core CPU IDs |
| **`cpu-coprocessor.nx_gzip`** | true | Nest Accelerator for GZIP is supported(Power). |
| **`cpu-power.sst_bf.enabled`** | true | Intel SST-BF ([Intel Speed Select Technology][intel-sst] - Base frequency) enabled |
| **`cpu-pstate.status`** | string | The status of the [Intel pstate][intel-pstate] driver when in use and enabled, either 'active' or 'passive'. |
Expand Down
61 changes: 61 additions & 0 deletions source/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ func (s *cpuSource) GetLabels() (source.FeatureLabels, error) {
labels["coprocessor.nx_gzip"] = v
}

// E cores
if v, ok := features.Attributes[TopologyFeature].Elements["e_cores"]; ok {
labels["e_cores"] = v
}

// P cores
if v, ok := features.Attributes[TopologyFeature].Elements["p_cores"]; ok {
labels["p_cores"] = v
}

return labels, nil
}

Expand Down Expand Up @@ -259,6 +269,27 @@ func getCPUModel() map[string]string {
return cpuModelInfo
}

func getCPUID(name string) int {
id := 0
base := 1
for idx := len(name) - 1; idx > 0; idx-- {
d := name[idx]

if '0' <= d && d <= '9' {
id += base * (int(d) - '0')
base *= 10
} else {
if base > 1 {
return id
}

return -1
}
}

return -1
}

func discoverTopology() map[string]string {
features := make(map[string]string)

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

ht := false
uniquePhysicalIDs := sets.NewString()
coreIDs := sets.NewString()
pCoreIDs := sets.NewString()
eCoreIDs := sets.NewString()

for _, file := range files {
coreIDs.Insert(strconv.Itoa(getCPUID(file.Name())))

// Try to read siblings from topology
siblings, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
if err != nil {
Expand Down Expand Up @@ -299,6 +335,31 @@ func discoverTopology() map[string]string {
features["hardware_multithreading"] = strconv.FormatBool(ht)
features["socket_count"] = strconv.FormatInt(int64(uniquePhysicalIDs.Len()), 10)

for _, entry := range files {

// Try to read core_cpus_list from topology
cpus, err := os.ReadFile(hostpath.SysfsDir.Path("bus/cpu/devices", entry.Name(), "topology/core_cpus_list"))
if err != nil {
klog.ErrorS(err, "error reading core_cpus_list file")
return map[string]string{}
}

if id := strings.Split(string(cpus), "-"); len(id) == 2 {
if _, ok := coreIDs[id[0]]; ok {
pCoreIDs.Insert(id[0])
}
if _, ok := coreIDs[id[1]]; ok {
pCoreIDs.Insert(id[1])
}
} else {
id1 := strings.Split(string(id[0]), "\n")
eCoreIDs.Insert(id1[0])
}
}

features["e_cores"] = strings.Join(eCoreIDs.List(), "_")
features["p_cores"] = strings.Join(pCoreIDs.List(), "_")

return features
}

Expand Down

0 comments on commit b8dd0df

Please sign in to comment.