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

Function CoresForNode

pkg/cpu/cpu_linux.go:191–270  ·  view source on GitHub ↗
(ctx context.Context, nodeID int)

Source from the content-addressed store, hash-verified

189}
190
191func CoresForNode(ctx context.Context, nodeID int) ([]*ProcessorCore, error) {
192 // The /sys/devices/system/node/nodeX directory contains a subdirectory
193 // called 'cpuX' for each logical processor assigned to the node. Each of
194 // those subdirectories contains a topology subdirectory which has a
195 // core_id file that indicates the 0-based identifier of the physical core
196 // the logical processor (hardware thread) is on.
197 paths := linuxpath.New(ctx)
198 path := filepath.Join(
199 paths.SysDevicesSystemNode,
200 fmt.Sprintf("node%d", nodeID),
201 )
202 cores := make([]*ProcessorCore, 0)
203
204 findCoreByID := func(coreID int) *ProcessorCore {
205 for _, c := range cores {
206 if c.ID == coreID {
207 return c
208 }
209 }
210
211 c := &ProcessorCore{
212 ID: coreID,
213 LogicalProcessors: []int{},
214 }
215 cores = append(cores, c)
216 return c
217 }
218
219 files, err := os.ReadDir(path)
220 if err != nil {
221 return nil, err
222 }
223 for _, file := range files {
224 filename := file.Name()
225 if !strings.HasPrefix(filename, "cpu") {
226 continue
227 }
228 if filename == "cpumap" || filename == "cpulist" {
229 // There are two files in the node directory that start with 'cpu'
230 // but are not subdirectories ('cpulist' and 'cpumap'). Ignore
231 // these files.
232 continue
233 }
234 // Grab the logical processor ID by cutting the integer from the
235 // /sys/devices/system/node/nodeX/cpuX filename
236 cpuPath := filepath.Join(path, filename)
237 procID, err := strconv.Atoi(filename[3:])
238 if err != nil {
239 log.Warn(ctx,
240 "failed to determine procID from %s. Expected integer after 3rd char.",
241 filename,
242 )
243 continue
244 }
245 onlineFilePath := filepath.Join(cpuPath, onlineFile)
246 if _, err := os.Stat(onlineFilePath); err == nil {
247 if util.SafeIntFromFile(ctx, onlineFilePath) == 0 {
248 continue

Callers 1

topologyNodesFunction · 0.92

Calls 3

NewFunction · 0.92
WarnFunction · 0.92
SafeIntFromFileFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…