MCPcopy
hub / github.com/jaypipes/ghw / processorsGet

Function processorsGet

pkg/cpu/cpu_linux.go:45–163  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

43}
44
45func processorsGet(ctx context.Context) []*Processor {
46 paths := linuxpath.New(ctx)
47
48 lps := logicalProcessorsFromProcCPUInfo(ctx)
49 // keyed by processor ID (physical_package_id)
50 procs := map[int]*Processor{}
51
52 // /sys/devices/system/cpu pseudodir contains N number of pseudodirs with
53 // information about the logical processors on the host. These logical
54 // processor pseudodirs are of the pattern /sys/devices/system/cpu/cpu{N}
55 fnames, err := os.ReadDir(paths.SysDevicesSystemCPU)
56 if err != nil {
57 log.Warn(ctx, "failed to read /sys/devices/system/cpu: %s", err)
58 return []*Processor{}
59 }
60 for _, fname := range fnames {
61 matches := regexForCpulCore.FindStringSubmatch(fname.Name())
62 if len(matches) < 2 {
63 continue
64 }
65
66 lpID, err := strconv.Atoi(matches[1])
67 if err != nil {
68 log.Warn(ctx, "failed to find numeric logical processor ID: %s", err)
69 continue
70 }
71
72 onlineFilePath := filepath.Join(paths.SysDevicesSystemCPU, fmt.Sprintf("cpu%d", lpID), onlineFile)
73 if _, err := os.Stat(onlineFilePath); err == nil {
74 if util.SafeIntFromFile(ctx, onlineFilePath) == 0 {
75 continue
76 }
77 } else if errors.Is(err, os.ErrNotExist) {
78 // Assume the CPU is online if the online state file doesn't exist
79 // (as is the case with older snapshots)
80 }
81 procID := processorIDFromLogicalProcessorID(ctx, lpID)
82 proc, found := procs[procID]
83 if !found {
84 proc = &Processor{ID: procID}
85 lp, ok := lps[lpID]
86 if !ok {
87 log.Warn(ctx,
88 "failed to find attributes for logical processor %d",
89 lpID,
90 )
91 continue
92 }
93
94 // Assumes /proc/cpuinfo is in order of logical processor id, then
95 // lps[lpID] describes logical processor `lpID`.
96 // Once got a more robust way of fetching the following info,
97 // can we drop /proc/cpuinfo.
98 // 1. Capabilities (Hardware Features)
99 if len(lp.Attrs["flags"]) != 0 { // x86
100 proc.Capabilities = strings.Split(lp.Attrs["flags"], " ")
101 } else if len(lp.Attrs["Features"]) != 0 { // ARM64
102 proc.Capabilities = strings.Split(lp.Attrs["Features"], " ")

Callers 1

loadMethod · 0.70

Calls 7

NewFunction · 0.92
WarnFunction · 0.92
SafeIntFromFileFunction · 0.92
CoreByIDMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…