1 #define _KERNEL_STRUCTURES
3 #include <sys/sysctl.h>
4 #include <sys/vmmeter.h>
19 #define CPU_STARTX (3 + vmm_ncpus)
22 #define DRAW_ROW(n, y, w, fmt, args...) \
24 mvprintw(y, n, fmt, w, args); \
29 static int vmm_fetched;
30 static struct vmmeter *vmm_cur, *vmm_prev;
31 static struct kinfo_cputime *vmm_cptime_cur, *vmm_cptime_prev;
32 static struct save_ctx symctx;
33 static int symbols_read;
41 for (i = 0; i < vmm_ncpus; ++i) {
42 struct vmmeter *vmm = &vmm_cur[i];
46 snprintf(buf, sizeof(buf), "vm.cpu%d.vmmeter", i);
47 if (sysctlbyname(buf, vmm, &sz, NULL, 0))
48 err(1, "sysctlbyname(cpu%d)", i);
50 vmm->v_intr -= (vmm->v_timer + vmm->v_ipi);
53 sz = vmm_ncpus * sizeof(struct kinfo_cputime);
54 if (sysctlbyname("kern.cputime", vmm_cptime_cur, &sz, NULL, 0))
55 err(1, "kern.cputime");
72 for (i = 0; i < vmm_ncpus; ++i) {
73 struct kinfo_cputime d;
74 uint64_t cp_total = 0;
76 n = X_START + CPU_LABEL_W;
78 #define D(idx, field) \
79 (vmm_cur[idx].field - vmm_prev[idx].field) / (u_int)naptime
81 DRAW_ROW(n, CPU_START + i, 6, "%*u", D(i, v_timer));
82 DRAW_ROW(n, CPU_START + i, 8, "%*u", D(i, v_ipi));
83 DRAW_ROW(n, CPU_START + i, 8, "%*u", D(i, v_intr));
87 #define CPUD(dif, idx, field) \
89 dif.cp_##field = vmm_cptime_cur[idx].cp_##field - \
90 vmm_cptime_prev[idx].cp_##field; \
91 cp_total += dif.cp_##field; \
94 #define CPUV(dif, field) \
95 (dif.cp_##field * 100.0) / cp_total
106 DRAW_ROW(n, CPU_START + i, 7, "%*.1f", CPUV(d, user));
107 DRAW_ROW(n, CPU_START + i, 7, "%*.1f", CPUV(d, nice));
108 DRAW_ROW(n, CPU_START + i, 7, "%*.1f", CPUV(d, sys));
109 DRAW_ROW(n, CPU_START + i, 7, "%*.1f", CPUV(d, intr));
110 DRAW_ROW(n, CPU_START + i, 7, "%*.1f", CPUV(d, idle));
114 #define CPUC(idx, field) vmm_cptime_cur[idx].cp_##field
116 n = X_START + CPU_LABEL_W;
118 DRAW_ROW(n, CPU_STARTX + i, 15, "%-*s", CPUC(i, msg));
119 DRAW_ROW(n, CPU_STARTX + i, 35, "%-*s",
120 address_to_symbol((void *)(intptr_t)CPUC(i, stallpc),
131 memcpy(vmm_prev, vmm_cur, sizeof(struct vmmeter) * vmm_ncpus);
132 memcpy(vmm_cptime_prev, vmm_cptime_cur,
133 sizeof(struct kinfo_cputime) * vmm_ncpus);
144 n = X_START + CPU_LABEL_W;
146 DRAW_ROW(n, CPU_START - 1, 6, "%*s", "timer");
147 DRAW_ROW(n, CPU_START - 1, 8, "%*s", "ipi");
148 DRAW_ROW(n, CPU_START - 1, 8, "%*s", "extint");
149 DRAW_ROW(n, CPU_START - 1, 7, "%*s", "user%");
150 DRAW_ROW(n, CPU_START - 1, 7, "%*s", "nice%");
151 DRAW_ROW(n, CPU_START - 1, 7, "%*s", "sys%");
152 DRAW_ROW(n, CPU_START - 1, 7, "%*s", "intr%");
153 DRAW_ROW(n, CPU_START - 1, 7, "%*s", "idle%");
155 for (i = 0; i < vmm_ncpus; ++i)
156 mvprintw(CPU_START + i, X_START, "cpu%d", i);
158 n = X_START + CPU_LABEL_W;
159 DRAW_ROW(n, CPU_STARTX - 1, 15, "%-*s", "contention");
160 DRAW_ROW(n, CPU_STARTX - 1, 35, "%-*s", "function");
162 for (i = 0; i < vmm_ncpus; ++i)
163 mvprintw(CPU_STARTX + i, X_START, "cpu%d", i);
169 if (symbols_read == 0) {
174 if (kinfo_get_cpus(&vmm_ncpus))
175 err(1, "kinfo_get_cpus");
177 vmm_cur = calloc(vmm_ncpus, sizeof(*vmm_cur));
179 err(1, "calloc vmm_cur");
181 vmm_prev = calloc(vmm_ncpus, sizeof(*vmm_prev));
182 if (vmm_prev == NULL)
183 err(1, "calloc vmm_prev");
185 vmm_cptime_cur = calloc(vmm_ncpus, sizeof(*vmm_cptime_cur));
186 if (vmm_cptime_cur == NULL)
187 err(1, "calloc vmm_cptime_cur");
189 vmm_cptime_prev = calloc(vmm_ncpus, sizeof(*vmm_cptime_prev));
190 if (vmm_cptime_prev == NULL)
191 err(1, "calloc vmm_cptime_prev");
203 if (vmm_prev != NULL)
206 if (vmm_cptime_cur != NULL)
207 free(vmm_cptime_cur);
208 if (vmm_cptime_prev != NULL)
209 free(vmm_cptime_prev);