Repository navigation
pgrep: compare --older with the process's age - #821
Conversation
The start time in /proc/<pid>/stat counts clock ticks since boot, so comparing it with the --older seconds matched nearly every process. Compare the time since the process started instead.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #821 +/- ##
==========================================
+ Coverage 19.20% 20.30% +1.10%
==========================================
Files 68 68
Lines 10156 10137 -19
Branches 546 552 +6
==========================================
+ Hits 1950 2058 +108
+ Misses 8206 8079 -127
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| /// time in /proc/<pid>/stat. | ||
| #[cfg(target_os = "linux")] | ||
| fn ticks_since_boot() -> u64 { | ||
| let uptime = std::fs::read_to_string("/proc/uptime") |
There was a problem hiding this comment.
could we use uucore::uptime::get_uptime instead of parsing /proc/uptime again?
w and top already use it
There was a problem hiding this comment.
Done, it uses uucore::uptime::get_uptime now.
| let uptime = std::fs::read_to_string("/proc/uptime") | ||
| .ok() | ||
| .and_then(|s| s.split_whitespace().next()?.parse::<f64>().ok()) | ||
| .unwrap_or(0.0); |
There was a problem hiding this comment.
if /proc/uptime can't be read, --older N silently matches nothing
maybe show an error instead?
There was a problem hiding this comment.
Done, --older now reports the error from get_uptime instead of matching nothing.
| }; | ||
| #[cfg(not(target_os = "linux"))] | ||
| let older_matched = true; | ||
|
|
There was a problem hiding this comment.
why only linux? before this, --older wasn't gated
There was a problem hiding this comment.
No good reason, I removed the Linux gates. Only the tick lookup keeps a cfg(unix), because sysconf is unix-only.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
I noticed a problem might introduced by this pull request, the script below are restructured by LLM. $ sleep 5 & pid=$!; sleep 1.1; /usr/bin/pgrep --older 1 --parent "$$" -x sleep; rc=$?; printf 'expected pid=%s, exit=%s\n' "$pid" "$rc"; kill "$pid"; wait "$pid" 2>/dev/null || true
37409
expected pid=37409, exit=0
$ sleep 5 & pid=$!; sleep 1.1; ./target/debug/pgrep --older 1 --parent "$$" -x sleep; rc=$?; printf 'expected pid=%s, exit=%s\n' "$pid" "$rc"; kill "$pid"; wait "$pid" 2>/dev/null || true
expected pid=37421, exit=1
$ /usr/bin/pgrep --version
pgrep from procps-ng 4.0.4A process that has been running for about 1.1 seconds should match And I think you can make the argument to be the default argument of test you introduced? |
uucore's get_uptime returns whole seconds, so a process could look up to a second younger than it is and a 1.1 second old process did not match --older 1. Read /proc/uptime with its fraction, as procps-ng does, and still report an error when it can't be read.
|
Thanks for catching this.
f5c1865 reads I also changed the test to your case: a |
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
Thanks for your PR |
pgrep --older N(and pkill/pidwait, which share the matcher) should select processes that have been running for at least N seconds. It compared N with the process's start time, which /proc//stat gives in clock ticks since boot, so on any machine up for more than N/100 seconds every process matched:The matcher now reads the time since boot (
/proc/uptime) and the clock tick rate (sysconf(_SC_CLK_TCK)) once, and keeps a process whennow - start_time >= N * ticks. Without--oldernothing changes.Against procps-ng 4.0.4 with a 2.5 s old process:
--older 0/1/2match,--older 3/5/99999999don't, on both.--older 60 -x systemdgives the same processes. A huge N saturates instead of overflowing.Test
test_older_compares_process_age: a just-spawnedsleepis listed with--older 0and not with--older 60(fails on main).cargo test: 225 pass (224 on main),cargo clippy --all-targets -- -D warningsclean.