Skip to contentSkip to conversation

CPU/disk usage

DISCUSSION Adam Dyga 2 participants 2 messages May 21, 2005

lore
Share & tools
lore

Adam Dyga

author
#0 lore raw

Hello,

What's the best way to:
1. Get the percent cpu usage (as in top)
2. Get disk activity (eg. number of bytes read/written from/to disk during some period of time).

Ad1) I know I could find the cpu usage in top sources, but unfortunately I don't have enough time to do it currently - maybe some of you did this already.
There is sysinfo() system call that returns average loads during the last 1, 5, and 15 minutes but average load, AFAIK, is a average number of tasks waiting for cpu, so it's not exactly what I want.

Cheers
AD

#1 lore raw
Quoted 2 lines · Adam Dyga

What's the best way to:
1. Get the percent cpu usage (as in top)

The way that ps does it is to obtain the process start time and amount of user and system time used from /proc/<pid>/stat, and the number of seconds since boot from /proc/uptime, then compute the CPU usage as the ratio of the total time used (user + system) to the time the process has been running (system uptime - process start time).

Note that this gives the average CPU usage over the lifetime of the process. If you want a current figure, measure the change in the total usage over a given interval and divide by the interval.

Signature · 1 line
Glynn Clements <[email protected]>