Skip to content

Commit

Permalink
feat: add cpu socket number in cpu.topology
Browse files Browse the repository at this point in the history
Signed-off-by: AhmedGrati <[email protected]>
  • Loading branch information
TessaIO committed Dec 8, 2023
1 parent b087c60 commit eef56a3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
6 changes: 6 additions & 0 deletions deployment/components/common/worker-mounts-combined.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
- op: add
path: /spec/template/spec/volumes
value:
- name: host-proc
hostPath:
path: "/proc"
- name: host-boot
hostPath:
path: "/boot"
Expand Down Expand Up @@ -29,6 +32,9 @@
- op: add
path: /spec/template/spec/containers/1/volumeMounts
value:
- name: host-proc
mountPath: "/host-proc"
readOnly: true
- name: host-boot
mountPath: "/host-boot"
readOnly: true
Expand Down
6 changes: 6 additions & 0 deletions deployment/components/common/worker-mounts.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
- op: add
path: /spec/template/spec/volumes
value:
- name: host-proc
hostPath:
path: "/proc"
- name: host-boot
hostPath:
path: "/boot"
Expand Down Expand Up @@ -29,6 +32,9 @@
- op: add
path: /spec/template/spec/containers/0/volumeMounts
value:
- name: host-proc
mountPath: "/host-proc"
readOnly: true
- name: host-boot
mountPath: "/host-boot"
readOnly: true
Expand Down
8 changes: 7 additions & 1 deletion deployment/helm/node-feature-discovery/templates/worker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ spec:
- name: metrics
containerPort: {{ .Values.worker.metricsPort | default "8081"}}
volumeMounts:
- name: host-proc
mountPath: "/host-proc"
readOnly: true
- name: host-boot
mountPath: "/host-boot"
readOnly: true
Expand Down Expand Up @@ -99,7 +102,10 @@ spec:
readOnly: true
{{- end }}
volumes:
- name: host-boot
- name: host-proc
hostPath:
path: "/proc"
- name: host-proc
hostPath:
path: "/boot"
- name: host-os-release
Expand Down
2 changes: 2 additions & 0 deletions pkg/utils/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
VarDir = HostDir(pathPrefix + "var")
// LibDir is where the /lib directory of the system to be inspected is located
LibDir = HostDir(pathPrefix + "lib")
// ProcDir is where the /proc directory of the system to inspected is located
ProcDir = HostDir(pathPrefix + "proc")
)

// HostDir is a helper for handling host system directories
Expand Down
39 changes: 39 additions & 0 deletions source/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package cpu

import (
"bytes"
"fmt"
"os"
"strconv"
"strings"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -197,6 +199,11 @@ func (s *cpuSource) GetLabels() (source.FeatureLabels, error) {
labels["hardware_multithreading"] = v
}

// CPU Socket Count
if v, ok := features.Attributes[TopologyFeature].Elements["socket_count"]; ok {
labels["socket_count"] = v
}

// NX
if v, ok := features.Attributes[CoprocessorFeature].Elements["nx_gzip"]; ok {
labels["coprocessor.nx_gzip"] = v
Expand Down Expand Up @@ -276,9 +283,41 @@ func discoverTopology() map[string]string {
features["hardware_multithreading"] = strconv.FormatBool(ht)
}

if socketCount, err := getCPUSocketCount(); err != nil {
klog.ErrorS(err, "failed to get sockets count")
} else {
features["socket_count"] = strconv.FormatInt(socketCount, 10)
}

return features
}

func getCPUSocketCount() (int64, error) {
cpuInfo, err := os.ReadFile(hostpath.ProcDir.Path("cpuinfo"))
if err != nil {
return 0, err
}

uniquePhysicalIDs := make(map[int]bool)

lines := bytes.Split(cpuInfo, []byte("\n"))
for _, l := range lines {
line := strings.TrimSpace(string(l))
if strings.HasPrefix(line, "physical id") {
physicalID := strings.TrimSpace(strings.Split(line, ":")[1])

id, err := strconv.Atoi(physicalID)
if err != nil {
return 0, err
}

uniquePhysicalIDs[id] = true
}
}

return int64(len(uniquePhysicalIDs)), nil
}

// Check if any (online) CPUs have thread siblings
func haveThreadSiblings() (bool, error) {

Expand Down

0 comments on commit eef56a3

Please sign in to comment.