0

There is a procs_running value in /proc/stat on Linux which shows the number of processes (threads) running.

The question is: does this value include the current process which is querying procfs or does not?

On an absolutely idle system, should we expect the value to be 0 or 1 ?

2 Answers2

1

Does procs_running field in /proc/stat include the current process which is querying procfs?

Yes, it does. The /proc/stat pseudofile is generated in fs/proc/stat.c in the Linux kernel, by calling nr_running().

The nr_running() function is defined in kernel/sched/core.c, which simply sums over all per-cpu process counters. The current process is not treated in any special way here, so it is included in the count.

On the absolutely idle system should we expect value to be 0 or 1 ?

Since a process must be reading the pseudo-file for the kernel to generate the contents, and such a process is always counted by the nr_running() kernel function, we should always expect it to be at least 1.

Nominal Animal
  • 3,105
  • 15
  • 13
  • Running 4.7 and 4.8 here, and in all my systems it seems to be affected only by the current command line processes; i.e. if a simple cat, it shows 1, if with pipes, it shows the number of pipes. Odd. – Rui F Ribeiro Nov 27 '16 at 22:53
  • 1
    You are right. It is the same on older kernels such as 3.13 (Ubuntu 14.04 LTS) The question is when how does vmstat always shows 1 even if you run it through pipe – Peter Zaitsev Nov 27 '16 at 23:01
  • @PeterZaitsev: You should have included that in your question, then. In any case, which version; `vmstat --version`? If `procps-ng`, then look at the sources at [`proc/sysinfo.c:getstat()`](https://gitlab.com/procps-ng/procps/blob/master/proc/sysinfo.c); specifically, find the comment `// exclude vmstat itself`. In other words, `vmstat` from `procps-ng` always subtracts one from the `procs_running` field. – Nominal Animal Nov 27 '16 at 23:32
  • I have checked with multiple versions including `vmstat from procps-ng 3.3.9` Subtracting one is easy though @RuiFRibeiro brings interesting point - if you run ` cat /proc/stat | grep procs_r` you get value 2 on the idle box. My real issue is Prometheus Node Exporter seems to report value at least 4 on idle system and I'm trying to figure out a way to normalize it to match vmstat output in a way it will work on all systems – Peter Zaitsev Nov 28 '16 at 00:48
  • No, it is actually not that odd that only one process of `cat /proc/stat | grep -e procs_r` is running at the same time, because `cat`ing the pseudofile does not take much time at all, and the `/proc/stat` contents fit easily in the pipe buffer (4096 bytes by default). The shell creates the pipes first, then forks each piped command sequentially; if the `cat` binary is already in page cache, it is run and finishes faster than the second command in the pipe starts execution. If the second binary is started at roughly the same time, it'll block on standard input (counted in `procs_blocked`). ... – Nominal Animal Nov 28 '16 at 06:26
  • ... I would consider it *rare* for `grep` in that pipe to run at the very same time as `cat` is asking for the statistics. (This ties in to a much larger whole, namely *latencies*, when processing data. Many people, even experienced programmers working on simulation software, focus on *bandwidth*, doing stuff as fast as they can, while completely ignoring *latencies*, things waiting on other things to complete/proceed before they can do meaningful work. Don't get me started on this; I tend to rant long and hard about this when I do. :) – Nominal Animal Nov 28 '16 at 06:30
0

With a little testing it looks like this INCLUDES the process which does the request. As such perhaps vmstat subtracts 1 from the number of active processes to show the real system load