Question

I'm trying to obtain number of runnable processes from linux kernel. sar -q gives this information readily. However I'm trying to get this value from /proc filesystem. There is no file in /proc that gives this value directly, then how is runq-sz computed. The wiki page http://en.wikipedia.org/wiki/Load_(computing) provides some insight into how run queue length is computed based on ldavg values but it is unclear. Can someone provide more pointers on this. Cheers

Was it helpful?

Solution

As gcla said you cat use

cat /proc/loadavg

to read loadavarage from from kernel - but strictly speaking, it is not a queue length.

Take a look at

grep procs_running /proc/stat

and

grep procs_blocked /proc/stat

First is an actual running queue and second is a number of process blocked on disk IO. Load average is a function from sum of both.

OTHER TIPS

here is the function in the sysstat daemon which provides the info sar prints out:

https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L392

if ((fp = fopen(LOADAVG, "r")) == NULL)
        return;

/* Read load averages and queue length */
fscanf(fp, "%d.%d %d.%d %d.%d %ld/%d %*d\n",
       &load_tmp[0], &st_queue->load_avg_1,
       &load_tmp[1], &st_queue->load_avg_5,
       &load_tmp[2], &st_queue->load_avg_15,
       &st_queue->nr_running,
       &st_queue->nr_threads);

It reads from /proc/loadavg, which is populated by this kernel function

http://lxr.free-electrons.com/source/fs/proc/loadavg.c#L13

static int loadavg_proc_show(struct seq_file *m, void *v)
{
        unsigned long avnrun[3];

        get_avenrun(avnrun, FIXED_1/200, 0);

        seq_printf(m, "%lu.%02lu %lu.%02lu %lu.%02lu %ld/%d %d\n",
                LOAD_INT(avnrun[0]), LOAD_FRAC(avnrun[0]),
                LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]),
                LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]),
                nr_running(), nr_threads,
                task_active_pid_ns(current)->last_pid);
        return 0;
}

The nr_running() function provides the total of both currently running tasks and tasks that are ready to run on a CPU; it's an instantaneous measure. I believe this will line up with the sar runq-sz variable.

Graham

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top