@@ -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" ) ]
8081fn 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