Skip to content

pgrep: compare --older with the process's age - #821

Open
jayhemnani9910 wants to merge 3 commits into
uutils:mainfrom
jayhemnani9910:pgrep-older-uses-age
Open

jayhemnani9910 wants to merge 3 commits into
uutils:mainfrom
jayhemnani9910:pgrep-older-uses-age

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

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:

$ sleep 100 & sleep 2.5
$ pgrep --older 5 -x sleep      # procps-ng 4.0.4: nothing; main: lists the 2.5 s old sleep

The matcher now reads the time since boot (/proc/uptime) and the clock tick rate (sysconf(_SC_CLK_TCK)) once, and keeps a process when now - start_time >= N * ticks. Without --older nothing changes.

Against procps-ng 4.0.4 with a 2.5 s old process: --older 0/1/2 match, --older 3/5/99999999 don't, on both. --older 60 -x systemd gives the same processes. A huge N saturates instead of overflowing.

Test test_older_compares_process_age: a just-spawned sleep is listed with --older 0 and not with --older 60 (fails on main). cargo test: 225 pass (224 on main), cargo clippy --all-targets -- -D warnings clean.

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.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 03:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 41.37931% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 19.91%. Comparing base (67832aa) to head (8e2cda3).
⚠️ Report is 27 commits behind head on main.

Files with missing lines Patch % Lines
src/uu/pgrep/src/process_matcher.rs 0.00% 17 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #821      +/-   ##
==========================================
+ Coverage   19.20%   19.91%   +0.71%     
==========================================
  Files          68       68              
  Lines       10156    10333     +177     
  Branches      546      552       +6     
==========================================
+ Hits         1950     2058     +108     
- Misses       8206     8275      +69     
Flag Coverage Δ
macos_latest 7.00% <0.00%> (+0.25%) ⬆️
ubuntu_latest 20.28% <46.15%> (+0.73%) ⬆️
windows_latest 0.02% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/uu/pgrep/src/process_matcher.rs Outdated
/// time in /proc/<pid>/stat.
#[cfg(target_os = "linux")]
fn ticks_since_boot() -> u64 {
let uptime = std::fs::read_to_string("/proc/uptime")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could we use uucore::uptime::get_uptime instead of parsing /proc/uptime again?
w and top already use it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, it uses uucore::uptime::get_uptime now.

Comment thread src/uu/pgrep/src/process_matcher.rs Outdated
let uptime = std::fs::read_to_string("/proc/uptime")
.ok()
.and_then(|s| s.split_whitespace().next()?.parse::<f64>().ok())
.unwrap_or(0.0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if /proc/uptime can't be read, --older N silently matches nothing
maybe show an error instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, --older now reports the error from get_uptime instead of matching nothing.

};
#[cfg(not(target_os = "linux"))]
let older_matched = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why only linux? before this, --older wasn't gated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No good reason, I removed the Linux gates. Only the tick lookup keeps a cfg(unix), because sysconf is unix-only.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 08:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 09:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

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.

3 participants