In order to measure a user's total CPU time, I'm using the "utime" field out of
utime %lu Amount of time that this process has been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This includes guest time, guest_time (time spent running a virtual CPU, see below), so that applications that are not aware of the guest time field do not lose that time from their calculations.
(from man proc (5))
So, my "user utime" is the sum of the
utime of all PIDs this user is running.
I'm hoping this will give me an accurate value for the number of CPU seconds this user has spent. Am I on the right track?
Some of the things I don't understand or take into account yet:
- Each PID also has a parent PID (or zero). But I'm counting every PID, not just the ones with a ppid of 0. Is this correct?
- There are, in addition to utime, stime, cutime and cstime. Do I need to worry about those? I'm assuming that utime is the total number of cpu seconds for a PID, not counting the parent.
If I calculate the system's total cpu time using
/proc/uptime, this value is fairly close to my sum for all users, but the difference is significant. For instance (in minutes):
system cpu_time: 96.13 sum of users_cputime: 111.45
I get "sensible looking" values for all kinds of things. At the moment I'm using the sum of utime, stime, cutime and cstime. And it reports values that, while I don't understand them, correlate very well with measurements from
If I am completely on the wrong track, there's another question:
- What's the fairest way to monitor total CPU time - per user? (+500 Bounty) on Ask Ubuntu