6.1 version numbering.
[dragonfly.git] / tools / tools / mwait_mon / mwait_mon.c
1 #include <sys/types.h>
2 #include <sys/sysctl.h>
3
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <unistd.h>
7
8 #define MWAIT_CX_MAX    8
9
10 struct cx_stat {
11         char            sysctl_name[52];
12         char            state_name[4];
13 };
14
15 static void     getstats(uint64_t[]);
16
17 static struct cx_stat   cx_stats[MWAIT_CX_MAX];
18 static int              cx_stats_cnt;
19
20 int
21 main(void)
22 {
23         uint64_t count[MWAIT_CX_MAX], prev_count[MWAIT_CX_MAX];
24         int i;
25
26         for (i = 0; i < MWAIT_CX_MAX; ++i) {
27                 char name[64];
28                 size_t len;
29                 int subcnt;
30
31                 snprintf(name, sizeof(name), "machdep.mwait.C%d.subcnt", i);
32
33                 len = sizeof(subcnt);
34                 sysctlbyname(name, &subcnt, &len, NULL, 0);
35                 if (subcnt == 0)
36                         continue;
37
38                 snprintf(cx_stats[cx_stats_cnt].state_name,
39                     sizeof(cx_stats[cx_stats_cnt].state_name), "C%d", i);
40                 snprintf(cx_stats[cx_stats_cnt].sysctl_name,
41                     sizeof(cx_stats[cx_stats_cnt].sysctl_name),
42                     "machdep.mwait.C%d.entered", i);
43                 ++cx_stats_cnt;
44         }
45
46         getstats(prev_count);
47
48         for (;;) {
49                 getstats(count);
50
51                 for (i = 0; i < cx_stats_cnt; ++i) {
52                         printf("%s %-5ju ", cx_stats[i].state_name,
53                             (uintmax_t)(count[i] - prev_count[i]));
54                         prev_count[i] = count[i];
55                 }
56                 printf("\n");
57                 fflush(stdout);
58
59                 sleep(1);
60         }
61 }
62
63 static void
64 getstats(uint64_t count[])
65 {
66         int i;
67
68         for (i = 0; i < cx_stats_cnt; ++i) {
69                 size_t len;
70
71                 len = sizeof(uint64_t);
72                 sysctlbyname(cx_stats[i].sysctl_name, &count[i], &len, NULL, 0);
73         }
74 }