Skip to content

fix(cpu/linux): sort processors by ID - #477

Merged
jaypipes merged 1 commit into
jaypipes:mainfrom
krisiasty:fix/sort-processors-by-id
Sep 26, 2026
Merged

jaypipes merged 1 commit into
jaypipes:mainfrom
krisiasty:fix/sort-processors-by-id

Conversation

@krisiasty

Copy link
Copy Markdown
Contributor

Fixes #476.

processorsGet collects processors in map[int]*Processor keyed by physical_package_id and builds the returned slice by ranging over that map. Go randomises map iteration order, so on a host with more than one package, successive ghw.CPU() calls on unchanged hardware can return Info.Processors in different orders. This sorts the result by ID before returning.

TestProcessorsSortedByID builds a synthetic chroot, in the same way as TestCPUModelFromDeviceTree. It has four logical processors that alternate between packages 1 and 0, so neither directory order nor map order matches ID order. The test calls cpu.New 50 times and checks that the order is [0 1] every time.

The test compiles and passes go vet for linux (GOOS=linux). I have not run it locally, because I have no Linux machine at hand, so CI here is its first run.

processorsGet collects processors in a map keyed by physical package ID
and builds the returned slice by ranging over it. Go randomises map
iteration order, so on a multi-package host successive calls on the same
hardware could return Info.Processors in different orders. Sort the
result by ID.

Fixes jaypipes#476

Signed-off-by: Krzysztof Cieplucha <krisiasty@gmail.com>

@jaypipes jaypipes left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, @krisiasty, thank you for your contribution! :)

@jaypipes
jaypipes merged commit a3cacae into jaypipes:main Sep 26, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cpu: Info.Processors order is nondeterministic on multi-socket hosts

2 participants