1 |
pajs |
1.1 |
CPU Statistics |
2 |
|
|
|
3 |
|
|
cpu_states_t *get_cpu_totals(); |
4 |
|
|
cpu_states_t *get_cpu_diff(); |
5 |
|
|
cpu_percent_t *cpu_percent_usage(); |
6 |
|
|
|
7 |
|
|
typedef struct{ |
8 |
|
|
long long user; |
9 |
|
|
long long kernel; |
10 |
|
|
long long idle; |
11 |
|
|
long long iowait; |
12 |
|
|
long long swap; |
13 |
|
|
long long nice; |
14 |
|
|
long long total; |
15 |
|
|
time_t systime; |
16 |
|
|
}cpu_states_t; |
17 |
|
|
|
18 |
|
|
typedef struct{ |
19 |
|
|
float user; |
20 |
|
|
float kernel; |
21 |
|
|
float idle; |
22 |
|
|
float iowait; |
23 |
|
|
float swap; |
24 |
|
|
float nice; |
25 |
|
|
time_t time_taken; |
26 |
|
|
}cpu_percent_t; |
27 |
|
|
|
28 |
|
|
get_cpu_totals() and get_cpu_diff() both return static pointers of |
29 |
|
|
type cpu_states_t. get_cpu_totals() returns the total amount of "ticks" |
30 |
|
|
the OS has spent in each of the different states. get_cpu_diff() returns |
31 |
|
|
the difference in "ticks" for each of the states since last time get_cpu_diff() |
32 |
|
|
or get_cpu_totals() was called. If it has never been called, it will return |
33 |
|
|
the result of get_cpu_totals() |
34 |
|
|
|
35 |
|
|
The vaule stored (the "ticks") will vary from OS to OS. E.g. solaris has a |
36 |
|
|
total of 100 per second. Linux has substatially more than that. Also, different |
37 |
|
|
OS's store different information. E.g. solaris doesn't do nice. |
38 |
|
|
|
39 |
|
|
Because of this, ideally you will always want to work on a scale against the |
40 |
|
|
total, or in percentages. |
41 |
|
|
|
42 |
|
|
cpu_percent_usage() returns a pointer to a static cpu_percent_t. The function calls |
43 |
|
|
get_cpu_diff() and changes the values into percentages. If its never been called |
44 |
|
|
before (and nor has get_cpu_totals() and get_cpu_diff() ), the returned percentages |
45 |
|
|
will be the systems total ever since its uptime. (Unless the counters of cycled) |