Skip to content

Commit e8f7a49

Browse files
Merge pull request #479 from Bluemangoo/feature/top-ni-pr
top: implement `PR` `NI`
2 parents 77bbe0f + b1fbab8 commit e8f7a49

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/uu/top/src/picker.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(crate) fn pickers(fields: &[String]) -> Vec<Box<dyn Fn(u32) -> String>> {
2626
"PID" => helper(pid),
2727
"USER" => helper(user),
2828
"PR" => helper(pr),
29+
"NI" => helper(ni),
2930
"RES" => helper(res),
3031
"SHR" => helper(shr),
3132
"S" => helper(s),
@@ -76,11 +77,39 @@ fn user(pid: u32) -> String {
7677
.to_string()
7778
}
7879

79-
#[cfg(not(target_os = "windows"))]
80+
#[cfg(target_os = "linux")]
8081
fn pr(pid: u32) -> String {
82+
use uucore::libc::*;
83+
let policy = unsafe { sched_getscheduler(pid as i32) };
84+
if policy == -1 {
85+
return String::new();
86+
}
87+
88+
// normal processes
89+
if policy == SCHED_OTHER || policy == SCHED_BATCH || policy == SCHED_IDLE {
90+
return (get_nice(pid) + 20).to_string();
91+
}
92+
93+
// real-time processes
94+
let mut param = sched_param { sched_priority: 0 };
95+
unsafe { sched_getparam(pid as c_int, &mut param) };
96+
if param.sched_priority == -1 {
97+
return String::new();
98+
}
99+
param.sched_priority.to_string()
100+
}
101+
102+
#[cfg(not(target_os = "linux"))]
103+
fn pr(pid: u32) -> String {
104+
todo(pid)
105+
}
106+
107+
#[cfg(not(target_os = "windows"))]
108+
fn get_nice(pid: u32) -> i32 {
81109
use libc::{getpriority, PRIO_PROCESS};
82110
use nix::errno::Errno;
83111

112+
// this is nice value, not priority value
84113
let result = unsafe { getpriority(PRIO_PROCESS, pid) };
85114

86115
let result = if Errno::last() == Errno::UnknownErrno {
@@ -90,12 +119,17 @@ fn pr(pid: u32) -> String {
90119
0
91120
};
92121

93-
format!("{result}")
122+
result as i32
123+
}
124+
125+
#[cfg(not(target_os = "windows"))]
126+
fn ni(pid: u32) -> String {
127+
format!("{}", get_nice(pid))
94128
}
95129

96130
// TODO: Implement this function for Windows
97131
#[cfg(target_os = "windows")]
98-
fn pr(_pid: u32) -> String {
132+
fn ni(_pid: u32) -> String {
99133
"0".into()
100134
}
101135

0 commit comments

Comments
 (0)