Wednesday, 25 February 2015

Difference between PR and NI column values in the output of TOP command

For illustration, I am putting up a snippet of TOP output during one of my sessions --

top - 18:54:23 up 1 day, 23:33,  8 users,  load average: 1.23, 1.08, 1.02
Tasks: 295 total,   3 running, 289 sleeping,   0 stopped,   3 zombie
Cpu(s): 28.4%us,  1.9%sy,  0.0%ni, 67.8%id,  1.8%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   7941828k total,  7741560k used,   200268k free,    90992k buffers
Swap:  8388604k total,   186344k used,  8202260k free,  1479116k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+ COMMAND                                                                                        
   61 root      39  19     0    0    0 S  0.0  0.0   0:07.02 khugepaged                                                                                     
   60 root      25   5     0    0    0 S  0.0  0.0   0:00.00 ksmd                                                                                           
 4358 rtkit     21   1  164m  976  940 S  0.0  0.0   0:02.09 rtkit-daemon                                                                                   
    1 root      20   0 19352 1260 1032 S  0.0  0.0   0:05.40 init                                                                                           
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.40 kthreadd                                                                                       
    4 root      20   0     0    0    0 S  0.0  0.0   0:04.24 ksoftirqd/0                                                                                    
   19 root      20   0     0    0    0 S  0.0  0.0   0:37.61 events/0                                                                                       
   23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cgroup       
         


I wanted to figure out the difference between the values under PR and NI columns, and I found the answer in the man page of C library function getpriority. The following snippet from the man page is the most relevant and explains the difference very well --
     
The  actual  priority range varies between kernel versions.  Linux before 1.3.36 had -infinity..15.  Since kernel 1.3.43 Linux has the range -20..19.  Within the kernel, nice values are actually represented using the corresponding range 40..1  (since  negative  numbers are  error  codes) and these are the values employed by the setpriority() and getpriority() system calls.  The glibc wrapper functions for these system calls handle the translations between the user-land and kernel representations of the nice  value  according  to  the formula unice = 20 - knice.

My Learnings on Linux: A fabulous diagram of the Linux Kernel I/O Stack, ...

My Learnings on Linux: A fabulous diagram of the Linux Kernel I/O Stack, ...: A fabulous diagram of the Linux Kernel I/O Stack, as copied directly from Wikipedia link http://en.wikipedia.org/wiki/Device_mapper ...

Saturday, 7 February 2015

A fabulous diagram of the Linux Kernel I/O Stack, as copied directly from Wikipedia link http://en.wikipedia.org/wiki/Device_mapper