Iana
Iana

Reputation: 488

linux cpu load over last X min

Hello experts!
Could you please help. I want to calculate CPU load over the last 5 min. And I need to get these values permanently.

I found very useful and detailed article: http://phoxis.org/2013/09/05/finding-overall-and-per-core-cpu-utilization/
But I am not sure what is the best way on retrieving that numbers : run script every second constantly, and calculate the overage number every 300 iteractions (for example)? Thank you in advance!

Upvotes: 1

Views: 2957

Answers (1)

Alan Dyke
Alan Dyke

Reputation: 914

If you write your own code to monitor usage, you are up against a computing equivalent of Heisenberg's uncertainty principle. Alternatively you can use the "uptime" command.

% uptime
  16:00:29 up 3 days, 18:31,  2 users,  load average: 0.04, 0.12, 0.09

The last three numbers are the system load averages over the last 1,5 and 15 minutes. From the man page:

   System load averages is the average number of processes that are
   either in a runnable or uninterruptable state.  A process  in  a
   runnable  state  is  either  using the CPU or waiting to use the
   CPU.  A process in uninterruptable state is waiting for some I/O
   access,  eg  waiting  for disk.  The averages are taken over the
   three time intervals.  Load averages are not normalized for  the
   number  of CPUs in a system, so a load average of 1 means a sin-
   gle CPU system is loaded all the time while on a 4 CPU system it
   means it was idle 75% of the time.

As long as you know how many CPUs are in your system this gives you what you need.

Second iteration:

# cat /proc/uptime
353615.60 344666.42

From the /proc man page:

   /proc/uptime
          This file contains two numbers: the uptime of the sys-
          tem  (seconds),  and  the amount of time spent in idle
          process (seconds).

You can poll this every five minutes and record the change in values.

Upvotes: 2

Related Questions