In Windows there is a function called GetSystemTimes() that returns the system idle time, the amount of time spent executing kernel code, and the amount of time spent executing user mode code.
Is there an equivalent function(s) in linux?
In Windows there is a function called GetSystemTimes() that returns the system idle time, the amount of time spent executing kernel code, and the amount of time spent executing user mode code.
Is there an equivalent function(s) in linux?
The original answer gave a solution to getting the user and system time of the current running process. However, you want the information on the entire system. As far as I know, the only way to get this information is to parse the contents of /proc/stat
. In particular, the first line, labeled cpu
:
cpu 85806677 11713309 6660413 3490353007 6236822 300919 807875 0
This is followed by per cpu
summaries if you are running an SMP system. The line itself has the following information (in order):
The times are reported in units of USER_HZ
.
There may be other columns after this depending on the version of your kernel.
Original answer:
You want times(2)
:
times()
stores the current process times in thestruct tms
thatbuf
points to. Thestruct tms
is as defined in<sys/times.h>
:
struct tms {
clock_t tms_utime; /* user time */
clock_t tms_stime; /* system time */
clock_t tms_cutime; /* user time of children */
clock_t tms_cstime; /* system time of children */
};
Idle time can be inferred from tracking elapsed wall clock time, and subtracting away the non-idle times reported from the call.