8

I see that the htop command reports respective the % of utilisation of each CPU core and also that of the processes.

We know that only 1 process can execute at a time in the CPU. How is it possible for processes to occupy a certain "Percentage" of the CPU ?

Does it have anything to do with the Instruction Pipeline ?

3
  • 2
    it's percentage of CPU time that htop is showing
    – oawuie
    Jul 9, 2014 at 17:48
  • Nothing to do with pipeline. Jul 9, 2014 at 17:49
  • In terms of the CS fundamentals, it has more to do with context switching. Jun 8, 2023 at 7:26

3 Answers 3

4

Top shows usage over some time period - by default, something like 3 seconds. It basically tells you what percentage of CPU time a particular process ID used over that interval. And note that this percentage can be over 100% - if you had one process running two threads and keeping both cores of a dual core system busy, you'd see a number around 195% in the %CPU column for that process.

To add some more detail, as the man page explains:

       k: %CPU  --  CPU usage
          The task's share of the elapsed  CPU  time  since  the  last  screen
          update,  expressed as a percentage of total CPU time.  In a true SMP
          environment, if 'Irix mode' is Off, top  will  operate  in  'Solaris
          mode'  where  a task's cpu usage will be divided by the total number
          of CPUs.  You toggle 'Irix/Solaris' modes with the  'I'  interactive
          command.
...
       -d : Delay time interval as:  -d ss.tt (seconds.tenths)
            Specifies the delay between screen updates, and overrides the cor-
            responding value in  one's  personal  configuration  file  or  the
            startup  default.   Later  this can be changed with the 'd' or 's'
            interactive commands.
...
           Global_defaults
              'A' - Alt display      Off (full-screen)
            * 'd' - Delay time       3.0 seconds
              'I' - Irix mode        On  (no, 'solaris' smp)
            * 'p' - PID monitoring   Off
            * 's' - Secure mode      Off (unsecured)
              'B' - Bold disable     Off

So the %CPU column is the percentage of the machine's total CPU time since the last screen update. You can change the time between screen updates either at the command line or interactively, but it defaults to 3 seconds. By default, Irix mode is enabled and the %CPU can be >100% on a multiprocessor system, but Irix mode can be disabled to cause the percentages to be scaled down based on the number of cores.

3
  • Can you direct me to a reliable and simple article ? ( if you can, include it in the answer) .
    – kesari
    Jul 10, 2014 at 13:37
  • 2
    @sssv96: Nothing's more reliable than the man page - I quoted some sections from it to elaborate on my explanation.
    – godlygeek
    Jul 10, 2014 at 14:17
  • 4
    This is the man page of top. The definition of percentage of CPU of htop is maybe different. Aug 4, 2015 at 20:44
4

Short answer

  • Linux kernel counts usage in jiffies (CPU time unit), /proc/<PID>/stat tells you how many jiffies has a single process used since its start
  • htop compares two snapshots of summed jiffies (divided by number of CPU threads). The interval between these two time points could be configured using -d --delay=DELAY in tenths of seconds. e.g. 1s update interval (default is 1.5s, i.e. -d 15):
htop -d 10
  • 100% means that a single process used 1 CPU thread all the time in observed time window (e.g. 1.5 second). Such value might not be representative in longer period.
  • You can use +/- keys to modify the update interval

Longer explanation

The best source would be the source code :) (for Linux look for LinuxProcessList_scanCPUTime function).

  1. htop reads the PROCSTATFILE which points to the overall system stats stored in /proc/stat, you can check it by yourself:
$ cat /proc/stat

For per process values see:

/proc/<PID>/stat
  1. The first line shows the total CPU usage while in columns we have following values
usertime, nicetime, systemtime, idletime, ioWait, irq, softIrq, steal, guest, guestnice
  1. Following lines show the same values per each CPU core (thread).
  2. Total time is computed as:
totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime

  1. totaltime is divided by number of cpus ($ nproc value on Linux).
double period = (double)this->cpus[0].totalPeriod / cpus;

Finally in LinuxProcessList_recurseProcTree the percentage is computed as usage between two timestamps

percent_cpu = (period < 1e-6) ? 0.0f : ((lp->utime + lp->stime - lasttimes) / period * 100.0);
  • utime CPU time spent in user code, measured in clock ticks
  • stime CPU time spent in kernel code, measured in clock ticks
  • lasttimes is the previous "time" lp->utime + lp->stime (in clock ticks)
  1. Lastly the time units used in this computations relies on jiffies, see man 7 time for more information.
 t * 100 / jiffy

where jiffy is the number of clock ticks per second, instead of normal time.

 jiffy = sysconf(_SC_CLK_TCK);

You can obtain the clock tick value using:

$ getconf CLK_TCK
100

(which means that you can ignore this computation if you're not on special hardware or real-time Linux etc.)

0

It is statistics: 50% means “half of cpu over a time”, if you look deeper you can find what that time is.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .