From 3150e70fc742e29ec5f3aef2c68c2b7bfcf50d50 Mon Sep 17 00:00:00 2001 From: Chris Gillespie <6572184+gillespiecd@users.noreply.github.com> Date: Sun, 6 Sep 2020 22:02:45 -0700 Subject: [PATCH] Add cpu time to ps -l (#2507) --- Cargo.lock | 1 + crates/nu_plugin_ps/Cargo.toml | 2 ++ crates/nu_plugin_ps/src/ps.rs | 13 ++++++++++++- docs/commands/ps.md | 23 +++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index ac01c984a1..1c6474479e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3379,6 +3379,7 @@ dependencies = [ "nu-plugin", "nu-protocol", "nu-source", + "num-bigint", ] [[package]] diff --git a/crates/nu_plugin_ps/Cargo.toml b/crates/nu_plugin_ps/Cargo.toml index aa23520724..63f2a9ee6b 100644 --- a/crates/nu_plugin_ps/Cargo.toml +++ b/crates/nu_plugin_ps/Cargo.toml @@ -15,6 +15,8 @@ nu-plugin = {path = "../nu-plugin", version = "0.19.0"} nu-protocol = {path = "../nu-protocol", version = "0.19.0"} nu-source = {path = "../nu-source", version = "0.19.0"} +num-bigint = "0.2.6" + futures = {version = "0.3", features = ["compat", "io-compat"]} futures-timer = "3.0.2" diff --git a/crates/nu_plugin_ps/src/ps.rs b/crates/nu_plugin_ps/src/ps.rs index 28be7007a0..352028da87 100644 --- a/crates/nu_plugin_ps/src/ps.rs +++ b/crates/nu_plugin_ps/src/ps.rs @@ -1,12 +1,14 @@ use futures::{StreamExt, TryStreamExt}; use heim::process::{self as process, Process, ProcessResult}; -use heim::units::{information, ratio, Ratio}; +use heim::units::{information, ratio, time, Ratio}; use std::usize; use nu_errors::ShellError; use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value}; use nu_source::Tag; +use num_bigint::BigInt; + use std::time::Duration; #[derive(Default)] @@ -68,6 +70,15 @@ pub async fn ps(tag: Tag, long: bool) -> Result, ShellError> { UntaggedValue::filesize(memory.vms().get::()), ); if long { + if let Ok(cpu_time) = process.cpu_time().await { + let user_time = cpu_time.user().get::().round() as i64; + let system_time = cpu_time.system().get::().round() as i64; + + dict.insert_untagged( + "cpu_time", + UntaggedValue::duration(BigInt::from(user_time + system_time)), + ) + } if let Ok(parent_pid) = process.parent_pid().await { dict.insert_untagged("parent", UntaggedValue::int(parent_pid)) } diff --git a/docs/commands/ps.md b/docs/commands/ps.md index 1127582532..a07422d0cc 100644 --- a/docs/commands/ps.md +++ b/docs/commands/ps.md @@ -22,3 +22,26 @@ Syntax: `ps` 69 │ 8972 │ nu_plugin_ps.exe │ Running │ 58.00986000000000 ━━━━┷━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━ ``` + +Find processes with the highest cpu time +```shell +> ps -l | sort-by cpu_time | last 2 + # │ pid │ name │ status │ cpu │ mem │ virtual │ cpu_time │ parent │ exe │ command +───┼─────┼──────────────────┼─────────┼────────┼──────────┼─────────┼───────────────────┼────────┼──────────────────────┼────────────────────── + 0 │ 396 │ Google Chrome │ Running │ 0.0000 │ 271.6 MB │ 5.8 GB │ 6hr 20min 28sec │ 1 │ /Applications/Google │ /Applications/Google + │ │ │ │ │ │ │ 173ms 641us 315ns │ │ Chrome.app/Contents/ │ Chrome.app/Contents/ + │ │ │ │ │ │ │ │ │ MacOS/Google │ MacOS/Google + │ │ │ │ │ │ │ │ │ Chrome │ Chrome + 1 │ 444 │ Google Chrome He │ Running │ 0.0000 │ 398.9 MB │ 5.3 GB │ 10hr 36min 17sec │ 396 │ /Applications/Google │ /Applications/Google + │ │ │ │ │ │ │ 304ms 66us 889ns │ │ Chrome.app/Contents/ │ Chrome.app/Contents/ + │ │ │ │ │ │ │ │ │ Frameworks/Google │ Frameworks/Google + │ │ │ │ │ │ │ │ │ Chrome │ Chrome + │ │ │ │ │ │ │ │ │ Framework.framework/ │ Framework.framework/ + │ │ │ │ │ │ │ │ │ Versions/84.0.4147.1 │ Versions/84.0.4147.1 + │ │ │ │ │ │ │ │ │ 25/Helpers/Google │ 25/Helpers/Google + │ │ │ │ │ │ │ │ │ Chrome Helper │ Chrome Helper + │ │ │ │ │ │ │ │ │ (GPU).app/Contents/M │ (GPU).app/Contents/M + │ │ │ │ │ │ │ │ │ acOS/Google │ acOS/Google + │ │ │ │ │ │ │ │ │ Chrome Helper (GPU) │ Chrome Helper (GPU) +───┴─────┴──────────────────┴─────────┴────────┴──────────┴─────────┴───────────────────┴────────┴──────────────────────┴────────────────────── +```