Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes #6829, and another newly discovered bug where CPU usage was always reported as zero on Linux.
Bug 1
Previously,
pswould sometimes return an empty list on Linux. The easiest way to see this is to runpsin a loop:This line was the culprit:
nushell/crates/nu-system/src/linux.rs
Line 121 in 5cc6505
psworks by taking 2 snapshots of process activity, and then comparing them to get an idea of process activity over time. It's a reasonable approach, but we weren't handling the situation where a process dies between the 2 snapshots well.The fix: skip over just the process that died instead of skipping over all processes.
Bug 2
Another problem: I noticed that the
cpufield inpsresults was always returning zero. Even when compiling Rust code on all cores,pswas saying that every process was using zero CPU.When I took a closer look, the problem seemed to be that we were using
procfs::process::Processas if it were a snapshot of process activity, when in reality it's more like a handle to a process:nushell/crates/nu-system/src/linux.rs
Lines 265 to 275 in 5cc6505
The calls to
curr_proc.stat()andprev_proc.stat()are not retrieving previously taken snapshots ofstatdata ; they are taking new snapshots. And so the intended 100ms delay between snapshots was effectively 0ms.To fix this, I tweaked the
pscode to take snapshots ofstatdata like we already do for I/O data.Tests
I did a bunch of manual testing, but this is an area that's somewhat difficult to write automated tests for. If anyone has ideas, I'm all ears.
Make sure you've run and fixed any issues with these commands:
cargo fmt --all -- --checkto check standard code formatting (cargo fmt --allapplies these changes)cargo clippy --workspace --features=extra -- -D warnings -D clippy::unwrap_used -A clippy::needless_collectto check that you're using the standard code stylecargo test --workspace --features=extrato check that all the tests pass