Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lscpu: Support extracting and outputting nested fields #218

Merged
merged 10 commits into from
Feb 13, 2025

Conversation

alxndrv
Copy link
Contributor

@alxndrv alxndrv commented Feb 12, 2025

lscpu should be capable of outputting nested information. When using human-readable output this is achieved simply using indentation, where as in JSON mode nested fields go under the children property.

Core changes:

  • Modified CpuInfo to be a recursive structure, allowing for nesting of entries through the children property.
  • Moved output logic into its own function, adding support for printing out nested values, and ensuring the values on the right are aligned to the same column.
  • Implemented a helper function for extracting arbitrary values from /proc/cpuinfo, using a dynamically constructed Regex.
  • Added support for some basic fields like Vendor ID, CPU Family and Model, to illustrate how the aforementioned changes can be used in action.
  • Added missing -J shorthand counterpart to the --json option.

Output:

CPU(s):          16
Architecture:    x86_64
  Address sizes: 48 bits physical, 48 bits virtual
  Byte Order:    Little Endian
Vendor ID:       AuthenticAMD
  Model name:    AMD Ryzen 7 7700X 8-Core Processor
    CPU Family:  25
    Model:       97

@alxndrv alxndrv marked this pull request as draft February 12, 2025 14:42
@alxndrv alxndrv changed the title lscpu: Add support for nested fields in output lscpu: Support extracting and outputting nested fields Feb 12, 2025
@alxndrv alxndrv marked this pull request as ready for review February 12, 2025 17:07
Copy link
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.

The rest looks good. Some tests would be nice ;-)

@alxndrv
Copy link
Contributor Author

alxndrv commented Feb 13, 2025

The rest looks good. Some tests would be nice ;-)

Thanks for the review. Moved the code around a bit as you suggested, and added a basic test for non-JSON output. One thing to note is that with these changes, lscpu becomes explicitly dependent on Linux, since the tests expect that we can read information from /sys/ and /proc/cpuinfo, which aren't available on other systems.

I've gone ahead and added flags to the tests so they only get executed on Linux builds. Let me know if you have better ideas for a long-term approach 🙂

@alxndrv alxndrv requested a review from cakebaker February 13, 2025 11:46
@cakebaker cakebaker merged commit 01b5f33 into uutils:main Feb 13, 2025
13 checks passed
Copy link

codecov bot commented Feb 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (b6a4bfd) to head (95d57eb).
Report is 12 commits behind head on main.

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

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

@cakebaker
Copy link
Contributor

Thanks!

I've gone ahead and added flags to the tests so they only get executed on Linux builds. Let me know if you have better ideas for a long-term approach

The long-term approach is probably to use a crate like sysinfo that supports multiple platforms.

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