Skip to content

lscpu: Read CPU information from sysfs#220

Merged
cakebaker merged 10 commits intouutils:mainfrom
alxndrv:lscpu-read-vulns
Feb 17, 2025
Merged

lscpu: Read CPU information from sysfs#220
cakebaker merged 10 commits intouutils:mainfrom
alxndrv:lscpu-read-vulns

Conversation

@alxndrv
Copy link
Copy Markdown
Contributor

@alxndrv alxndrv commented Feb 13, 2025

Adding a sysfs module to lscpu, to facilitate reading and parsing CPU information out from various files under /sys/.

As part of this, implemented the following sections:

  • Various CPU topology related fields (online cores, core count, threads per core, sockets etc.)
  • CPU cache levels and sizes
  • CPU vulnerability mitigation states

Output on my machine after these changes:

Architecture:             x86_64
  Address sizes:          48 bits physical, 48 bits virtual
  Byte Order:             Little Endian
CPU(s):                   16
  On-line CPU(s) list:    0-15
Vendor ID:                AuthenticAMD
  Model name:             AMD Ryzen 7 7700X 8-Core Processor
    CPU Family:           25
    Model:                97
    Thread(s) per core:   2
    Core(s) per socket:   8
    Socket(s):            1
    Frequency boost:      enabled
Caches (sum of all):      
  L1d:                    256 KiB (8 instances)
  L1i:                    256 KiB (8 instances)
  L2:                     8 MiB (8 instances)
  L3:                     32 MiB (1 instances)
Vulnerabilities:          
  Gather data sampling:   Not affected
  Itlb multihit:          Not affected
  L1tf:                   Not affected
  Mds:                    Not affected
  Meltdown:               Not affected
  Mmio stale data:        Not affected
  Reg file data sampling: Not affected
  Retbleed:               Not affected
  Spec rstack overflow:   Mitigation: Safe RET
  Spec store bypass:      Mitigation: Speculative Store Bypass disabled via prctl
  Spectre v1:             Mitigation: usercopy/swapgs barriers and __user pointer sanitization
  Spectre v2:             Mitigation: Enhanced / Automatic IBRS; IBPB: conditional; STIBP: always-on; RSB filling; PBRSB-eIBRS: Not affected; BHI: Not affected
  Srbds:                  Not affected
  Tsx async abort:        Not affected

@alxndrv
Copy link
Copy Markdown
Contributor Author

alxndrv commented Feb 13, 2025

@cakebaker Since you mentioned in my previous PR that relying on sysinfo might be a good idea long-term, these changes might warrant some additional explanation on my part.

I took a look at sysinfo and it looks like the information it exposes is quite limited in the grand scheme of things. As far as I can tell, things like CPU vulnerability mitigations and topology are completely absent, so we would have to read those from /sys/ in any case. From what I gather, the original lscpu is also tightly coupled to Linux specifically and wouldn't work on anything else, so I'm not sure if we should restrict ourselves to cross-platform support either (at least not yet).

I realize that these changes are pretty much the exact opposite of what you suggested but hopefully the reasoning makes sense 😅 Let me know what you think...

@alxndrv alxndrv force-pushed the lscpu-read-vulns branch 2 times, most recently from 0c0dae4 to 15ec42c Compare February 13, 2025 17:27
@alxndrv alxndrv force-pushed the lscpu-read-vulns branch 2 times, most recently from e820d58 to 1650af0 Compare February 14, 2025 11:32
@cakebaker
Copy link
Copy Markdown
Contributor

@alxndrv Your approach using /proc and /sys is fine :)

I made my comment with regard to the long-term goal of making our version of lscpu work on other platforms. And for that we probably have to rely on external crates.

@alxndrv alxndrv changed the title lscpu: Read data from sysfs lscpu: Read CPU information from sysfs Feb 14, 2025
@alxndrv alxndrv marked this pull request as ready for review February 14, 2025 20:27
Copy link
Copy Markdown
Contributor

@cakebaker cakebaker left a comment

Choose a reason for hiding this comment

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

Well done :)

Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
@cakebaker cakebaker merged commit d9b6f13 into uutils:main Feb 17, 2025
13 checks passed
@cakebaker
Copy link
Copy Markdown
Contributor

Thanks!

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (95d57eb) to head (748bbf8).
Report is 43 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #220   +/-   ##
===========================
===========================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

2 participants