4

Is there a way to return the load averages, excluding any load caused by nice'd processes?

We have a load balancing mechanism in place that checks the load of multiple Linux servers, and submits a job to the server having the lowest load. We had a scenario where all servers had too high a load and so no server could be selected in the load balancing. However, I noticed that the servers were handling a bunch of nice'd processes, so although the load averages were high, it was still "safe" to submit another job.

Let me know if clarification is needed. Thanks.

6

You can write up your own script that uses ps to list all processes in the run/runnable state without a nice value greater than 0. The specific syntax you need to use will differ based on your version of ps. Something like this may work:

ps -eo state,nice | awk 'BEGIN {c=0} $2<=0 && $1 ~ /R/ { c++ } END {print c-2}'

It runs ps collecting the state and nice level of all processes and pipes the output to awk which sets a count variable c and increments it whenever the second column (nice) is less than or equal to 0 and the first column includes R (for runnable). Once it's done it prints out the value of c after subtracting 2. I subtract 2 because the ps and the awk commands will almost always be considered runnable for the duration of the command's execution. The end result will be a single number which represents the number of processes that were runnable at the time that the script executed excluding itself and processes run nicely, which is essentially the instantaneous load on the machine. You'd need to run this periodically and average it over 1, 5, and 15 minutes to determine the typical load averages of the machine.

  • Thanks for this answer. I haven't had a chance to test it yet, but I plan to and will get back to you. – Banjer Sep 27 '11 at 17:58
0

I just needed something similar. The answer from mkomitee helped me.

But the command ps -e lists only processes. The load should take into account that a process may have many threads running in parallel. I suggest to add the option -L to the call of ps to list all threads:

ps -eLo state,nice | awk 'BEGIN {c=0} $2<=0 && $1 ~ /R/ { c++ } END {print c-2}'
0

OK, real old thread here but I wanted to put my own 2 cents down here in case someone needs this as I did. In my particular situation I needed the result in percentage and I did not want niced cpu time taken into account.

I found the utility mpstat on my machine (debian) installed by default and I believe its a fairly basic utility that is available for most distributions.

running mpstat 2 1 (one cycle of 2 second - the cpu use is averaged over 2 seconds) give the following output:

Linux 3.16.0-4-amd64 (vm4)  12/14/2017  _x86_64_    (4 CPU)

11:51:51 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11:51:53 PM  all    3.02   96.86    0.12    0.00    0.00    0.00    0.00    0.00    0.00    0.00
Average:     all    3.02   96.86    0.12    0.00    0.00    0.00    0.00    0.00    0.00    0.00

You notice how its all nicely spelled out for you, the nice column and the idle column in the end. You noticed nice is real high in this case.

With a bit of awk magic using the below command (broken down for clarity, hope you can follow along, this is not an awk tutorial):

mpstat 2 1| \
awk '$3 ~ /CPU/ { \
 for(i=1;i<=NF;i++) { \
  if ($i ~ /%nice/) {nice=i} \
  if ($i ~ /%idle/) {idle=i} \
 } \
} \
$3 ~ /all/ { print 100 - $nice - $idle }'

we get the following result:

3.14

which is exactly what we wanted. We assume 100 as the total percentage and subtract from that any idle time (0.00 in this case) and any nice time (96.86 in this case). And voila we have our result.

In my case I use mpstat 2 1 but you could also use mpstat 10 1 and the result will be the load average of the last 10 seconds. Its just that the command then needs 10 seconds to finish as it has to sample 10 seconds. But 2 seconds were sufficient for my needs to get an idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.