Skip to content

Commit

Permalink
Retrieve and print command behind pid
Browse files Browse the repository at this point in the history
  • Loading branch information
sgasse committed Feb 19, 2024
1 parent c83e214 commit 79c6fbd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ loadtimer -s 2 -b 0 -n 3 `pidof logd`
Benchmarking PIDs [229]
3 sample(s) of 60s

| Description | CPU usage % | total mean | utime mean | utime stddev | stime mean | stime stddev |
| 229, 3 samples, 60s | 2.5 | 5.0 | 1.3 | 0.58 | 3.7 | 0.58 |
| Description | CPU usage % | total mean | utime mean | utime stddev | stime mean | stime stddev |
| logd | 2.5 | 5.0 | 1.3 | 0.58 | 3.7 | 0.58 |
```

If you want to build from source, e.g. for Android:
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fs;

use anyhow::Result;
use nix::unistd::SysconfVar;

Expand All @@ -16,3 +18,9 @@ pub fn get_user_hz() -> Result<i64> {
"failed to retreive USER_HZ / SC_CLK_TCK / jiffies"
))
}

/// Get the command behind a process.
pub fn get_process_command(pid: usize) -> Result<String> {
let path = format!("/proc/{pid}/comm");
fs::read_to_string(path).map_err(Into::into)
}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use loadtimer::{
cli::Cli, dump::print_proc_metrics, eval::ProcMetrics, get_user_hz, sample::Sampler,
cli::Cli, dump::print_proc_metrics, eval::ProcMetrics, get_process_command, get_user_hz,
sample::Sampler,
};
use std::time::Duration;

Expand All @@ -27,7 +28,7 @@ fn main() -> Result<()> {
let descriptions = args
.pids
.iter()
.map(|pid| format!("{pid}, {} samples, {}s", args.num_samples, args.sample_secs));
.map(|pid| get_process_command(*pid).unwrap_or_else(|_| format!("PID {pid}")));

println!();
print_proc_metrics(&metrics, descriptions);
Expand Down

0 comments on commit 79c6fbd

Please sign in to comment.