1 |
|
CPU Statistics |
2 |
+ |
============== |
3 |
|
|
4 |
< |
cpu_states_t *get_cpu_totals(); |
4 |
< |
cpu_states_t *get_cpu_diff(); |
5 |
< |
cpu_percent_t *cpu_percent_usage(); |
4 |
> |
$Id$ |
5 |
|
|
6 |
+ |
Data Structures |
7 |
+ |
--------------- |
8 |
+ |
|
9 |
+ |
There are two structures returned by the CPU statistics functions. |
10 |
+ |
They look like this: |
11 |
+ |
|
12 |
|
typedef struct{ |
13 |
|
long long user; |
14 |
|
long long kernel; |
30 |
|
time_t time_taken; |
31 |
|
}cpu_percent_t; |
32 |
|
|
33 |
+ |
user, kernel, idle, iowait, swap, and nice are different CPU states. |
34 |
+ |
systime and time_taken are the time in seconds since the last call |
35 |
+ |
of the function. |
36 |
+ |
|
37 |
+ |
Functions |
38 |
+ |
--------- |
39 |
+ |
|
40 |
+ |
cpu_states_t *get_cpu_totals(); |
41 |
+ |
cpu_states_t *get_cpu_diff(); |
42 |
+ |
cpu_percent_t *cpu_percent_usage(); |
43 |
+ |
|
44 |
|
get_cpu_totals() and get_cpu_diff() both return static pointers of |
45 |
< |
type cpu_states_t. get_cpu_totals() returns the total amount of "ticks" |
46 |
< |
the OS has spent in each of the different states. get_cpu_diff() returns |
47 |
< |
the difference in "ticks" for each of the states since last time get_cpu_diff() |
48 |
< |
or get_cpu_totals() was called. If it has never been called, it will return |
49 |
< |
the result of get_cpu_totals() |
45 |
> |
type cpu_states_t. get_cpu_totals() returns the total amount of |
46 |
> |
"ticks" the operating system has spent in each of the different |
47 |
> |
states. get_cpu_diff() returns the difference in "ticks" for each |
48 |
> |
of the states since last time get_cpu_diff() or get_cpu_totals() |
49 |
> |
was called. If it has never been called, it will return the result |
50 |
> |
of get_cpu_totals() |
51 |
|
|
52 |
< |
The vaule stored (the "ticks") will vary from OS to OS. E.g. solaris has a |
53 |
< |
total of 100 per second. Linux has substatially more than that. Also, different |
54 |
< |
OS's store different information. E.g. solaris doesn't do nice. |
52 |
> |
The value stored (the "ticks") will vary between operating systems. |
53 |
> |
For example Solaris has a total of 100 per second, while Linux has |
54 |
> |
substantially more. Also, different operating systems store different |
55 |
> |
information - you won't find nice cpu on Solaris for example. |
56 |
|
|
57 |
< |
Because of this, ideally you will always want to work on a scale against the |
58 |
< |
total, or in percentages. |
57 |
> |
Because of this, you will ideally always want to work on a scale |
58 |
> |
against the total, or in percentages. |
59 |
|
|
60 |
< |
cpu_percent_usage() returns a pointer to a static cpu_percent_t. The function calls |
61 |
< |
get_cpu_diff() and changes the values into percentages. If its never been called |
62 |
< |
before (and nor has get_cpu_totals() and get_cpu_diff() ), the returned percentages |
63 |
< |
will be the systems total ever since its uptime. (Unless the counters of cycled) |
60 |
> |
cpu_percent_usage() returns a pointer to a static cpu_percent_t. |
61 |
> |
The function calls get_cpu_diff() and changes the values into |
62 |
> |
percentages. If it has never been called before (and nor has |
63 |
> |
get_cpu_totals() or get_cpu_diff() ), the returned percentages will |
64 |
> |
be the systems total ever since its uptime. (Unless the counters |
65 |
> |
have cycled) |
66 |
> |
|
67 |
> |
Example |
68 |
> |
------- |
69 |
> |
|
70 |
> |
A basic example can be found in examples/cpu_usage.c |