Skip to content

Commit 541e50a

Browse files
committed
top: implement PR NI
The old PR impl is exactly NI impl
1 parent a28367c commit 541e50a

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

src/uu/top/src/picker.rs

Lines changed: 40 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,42 @@ fn user(pid: u32) -> String {
7677
.to_string()
7778
}
7879

79-
#[cfg(not(target_os = "windows"))]
80+
#[cfg(target_os = "linux")]
81+
fn pr(pid: u32) -> String {
82+
use uucore::libc::*;
83+
let policy = unsafe { sched_getscheduler(pid as i32) };
84+
if policy == -1 {
85+
return "".into();
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 param: *mut sched_param = unsafe { malloc(size_of::<sched_param>()) as *mut sched_param };
95+
unsafe { sched_getparam(pid as c_int, param) };
96+
let priority = unsafe { (*param).sched_priority };
97+
unsafe { free(param as *mut c_void) };
98+
99+
if priority == -1 {
100+
return "".into();
101+
}
102+
priority.to_string()
103+
}
104+
105+
#[cfg(not(target_os = "linux"))]
80106
fn pr(pid: u32) -> String {
107+
todo(pid)
108+
}
109+
110+
#[cfg(not(target_os = "windows"))]
111+
fn get_nice(pid: u32) -> i32 {
81112
use libc::{getpriority, PRIO_PROCESS};
82113
use nix::errno::Errno;
83114

115+
// this is nice value, not priority value
84116
let result = unsafe { getpriority(PRIO_PROCESS, pid) };
85117

86118
let result = if Errno::last() == Errno::UnknownErrno {
@@ -90,12 +122,17 @@ fn pr(pid: u32) -> String {
90122
0
91123
};
92124

93-
format!("{result}")
125+
result as i32
126+
}
127+
128+
#[cfg(not(target_os = "windows"))]
129+
fn ni(pid: u32) -> String {
130+
format!("{}", get_nice(pid))
94131
}
95132

96133
// TODO: Implement this function for Windows
97134
#[cfg(target_os = "windows")]
98-
fn pr(_pid: u32) -> String {
135+
fn ni(_pid: u32) -> String {
99136
"0".into()
100137
}
101138

0 commit comments

Comments
 (0)