-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpu.c
65 lines (57 loc) · 2.65 KB
/
cpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libperfstat.h>
int main(int argc, char *argv[])
{
perfstat_partition_total_t part;
perfstat_cpu_t * statp;
perfstat_cpu_t * statq;
int i, retcode, cputotal;
unsigned long long timebase, timebase_saved, hardware_ticks;
/* check how many perfstat_cpu_t structures are available */
cputotal = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
if(cputotal == -1) printf("ret=%d errono=%d perfstat_cpu(NULL)\n",retcode, errno);
/* return code is number of structures returned */
/*printf("number of perfstat_cpu_t available : %d\n", cputotal);*/
/* allocate enough memory for all the structures */
statp = malloc(cputotal * sizeof(perfstat_cpu_t));
statq = malloc(cputotal * sizeof(perfstat_cpu_t));
/* ask to get all the structures available in one call */
retcode = perfstat_cpu((perfstat_id_t *)FIRST_CPU, statq, sizeof(perfstat_cpu_t), cputotal);
if(retcode == -1) printf("ret=%d errono=%d perfstat_cpu(cputotal=%d)\n",retcode, errno, cputotal);
for (; ; ) {
retcode = perfstat_partition_total(NULL, &part, sizeof(perfstat_partition_total_t), 1);
hardware_ticks=part.timebase_last - timebase_saved;
timebase_saved = part.timebase_last;
if(retcode == -1) printf("ret=%d errono=%d timebase=%llu\n",retcode, errno, hardware_ticks);
sleep(1);
retcode = perfstat_cpu((perfstat_id_t *)FIRST_CPU, statp, sizeof(perfstat_cpu_t), cputotal);
if(retcode == -1) printf("ret=%d errono=%d perfstat_cpu(cputotal=%d)\n",retcode, errno, cputotal);
printf(" Logical Physical Stats\n");
printf(" usr:sys:idl:wait usr:sys:idl:wait syscall: read:write: fork: exec: charIO\n");
for (i = 0; i < retcode; i++) {
printf("%3d%3ld:%3ld:%3ld:%3ld",
i,
(long)(statp[i].user - statq[i].user),
(long)(statp[i].sys - statq[i].sys),
(long)(statp[i].idle - statq[i].idle),
(long)(statp[i].wait - statq[i].wait));
printf(" %3ld:%3ld:%3ld:%3ld",
(long)((statp[i].puser - statq[i].puser)*100/hardware_ticks),
(long)((statp[i].psys - statq[i].psys )*100/hardware_ticks),
(long)((statp[i].pidle - statq[i].pidle)*100/hardware_ticks),
(long)((statp[i].pwait - statq[i].pwait)*100/hardware_ticks));
printf(" %7llu:%5llu:%5llu:%5llu:%5llu:%7llu\n",
statp[i].syscall - statq[i].syscall,
statp[i].sysread - statq[i].sysread,
statp[i].syswrite - statq[i].syswrite,
statp[i].sysfork - statq[i].sysfork,
statp[i].sysexec - statq[i].sysexec,
statp[i].readch - statq[i].readch +
statp[i].writech - statq[i].writech );
}
memcpy(statq, statp, sizeof(perfstat_cpu_t) * cputotal);
}
}