Skip to content

Refactor: Replace /proc/cpuinfo parsing with pure sysfs-based CPU topology discovery #71

@AutuSnow

Description

@AutuSnow

Description:
Background

Currently, GetCPUInfos() in pkg/cpuinfo/cpuinfo.go parses /proc/cpuinfo to extract three fields: processor (CpuID), physical id (SocketID),
and core id (CoreID). After that, it immediately turns to sysfs to populate the remaining topology data (NUMA node, L3 cache, siblings, etc.)
— and even overwrites SocketID with the sysfs value in populateTopologyInfo().

This means /proc/cpuinfo parsing is effectively redundant. All three fields are available directly from sysfs:
Proposal
Replace the /proc/cpuinfo text parsing with sysfs-only enumeration

  1. Enumerate online CPUs from /sys/devices/system/cpu/online (or by listing cpu directories).
  2. For each CPU, read topology/physical_package_id and topology/core_id directly from sysfs.
  3. Remove parseCPUInfo() and the /proc/cpuinfo reading logic entirely.
    This also eliminates the trailing-block edge case in the current line-by-line parser (the duplicated parseCPUInfo call after the loop to handle the last CPU block).
    
    Alternative: use an existing Go library
    
    Instead of hand-rolling sysfs reads, we could consider using an existing library such as ghw , which provides a clean topology API (ghw.Topology()) backed by sysfs. This would reduce the amount of low-level file reading code we maintain. However, introducing an external dependency needs to be weighed against the simplicity of a few direct sysfs reads.Benefits
    
  • Single, consistent data source (sysfs) — no more reading /proc/cpuinfo then overwriting values from sysfs
  • Simpler, more maintainable code
  • Removes fragile text parsing logic
  • No change in plugin behavior (same CPUInfo struct, same data)
    
    Verification
  • Unit tests with mock sysfs trees (existing test pattern in the repo)
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions