2 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
4 * Licensed under the terms of the GNU GPL License version 2.
6 * Output format inspired by Len Brown's <lenb@kernel.org> turbostat tool.
17 #include <sys/types.h>
21 #include "idle_monitor/cpupower-monitor.h"
22 #include "idle_monitor/idle_monitors.h"
23 #include "helpers/helpers.h"
25 /* Define pointers to all monitors. */
26 #define DEF(x) & x ## _monitor ,
27 struct cpuidle_monitor *all_monitors[] = {
28 #include "idle_monitors.def"
32 static struct cpuidle_monitor *monitors[MONITORS_MAX];
33 static unsigned int avail_monitors;
35 static char *progname;
37 enum operation_mode_e { list = 1, show, show_all };
39 static int interval = 1;
40 static char *show_monitors_param;
41 static struct cpupower_topology cpu_top;
42 static unsigned int wake_cpus;
44 /* ToDo: Document this in the manpage */
45 static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };
47 static void print_wrong_arg_exit(void)
49 printf(_("invalid or unknown argument\n"));
53 long long timespec_diff_us(struct timespec start, struct timespec end)
56 if ((end.tv_nsec - start.tv_nsec) < 0) {
57 temp.tv_sec = end.tv_sec - start.tv_sec - 1;
58 temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
60 temp.tv_sec = end.tv_sec - start.tv_sec;
61 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
63 return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
66 void print_n_spaces(int n)
69 for (x = 0; x < n; x++)
73 /* size of s must be at least n + 1 */
74 int fill_string_with_spaces(char *s, int n)
79 for (; len < n; len++)
85 void print_header(int topology_depth)
91 int percent_width = 4;
93 fill_string_with_spaces(buf, topology_depth * 5 - 1);
96 for (mon = 0; mon < avail_monitors; mon++) {
97 need_len = monitors[mon]->hw_states_num * (percent_width + 3)
103 sprintf(buf, "%s", monitors[mon]->name);
104 fill_string_with_spaces(buf, need_len);
109 if (topology_depth > 2)
111 if (topology_depth > 1)
113 if (topology_depth > 0)
116 for (mon = 0; mon < avail_monitors; mon++) {
121 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
124 s = monitors[mon]->hw_states[state];
125 sprintf(buf, "%s", s.name);
126 fill_string_with_spaces(buf, percent_width);
135 void print_results(int topology_depth, int cpu)
140 unsigned long long result;
143 /* Be careful CPUs may got resorted for pkg value do not just use cpu */
144 if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
146 if (!cpu_top.core_info[cpu].is_online &&
147 cpu_top.core_info[cpu].pkg == -1)
150 if (topology_depth > 2)
151 printf("%4d|", cpu_top.core_info[cpu].pkg);
152 if (topology_depth > 1)
153 printf("%4d|", cpu_top.core_info[cpu].core);
154 if (topology_depth > 0)
155 printf("%4d|", cpu_top.core_info[cpu].cpu);
157 for (mon = 0; mon < avail_monitors; mon++) {
161 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
165 s = monitors[mon]->hw_states[state];
167 if (s.get_count_percent) {
168 ret = s.get_count_percent(s.id, &percent,
169 cpu_top.core_info[cpu].cpu);
172 else if (percent >= 100.0)
173 printf("%6.1f", percent);
175 printf("%6.2f", percent);
176 } else if (s.get_count) {
177 ret = s.get_count(s.id, &result,
178 cpu_top.core_info[cpu].cpu);
182 printf("%6llu", result);
184 printf(_("Monitor %s, Counter %s has no count "
185 "function. Implementation error\n"),
186 monitors[mon]->name, s.name);
192 * The monitor could still provide useful data, for example
193 * AMD HW counters partly sit in PCI config space.
194 * It's up to the monitor plug-in to check .is_online, this one
195 * is just for additional info.
197 if (!cpu_top.core_info[cpu].is_online &&
198 cpu_top.core_info[cpu].pkg != -1) {
199 printf(_(" *is offline\n"));
206 /* param: string passed by -m param (The list of monitors to show)
208 * Monitors must have been registered already, matching monitors
209 * are picked out and available monitors array is overridden
212 * Monitors get sorted in the same order the user passes them
215 static void parse_monitor_param(char *param)
219 char *tmp = param, *token;
220 struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
223 for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
224 token = strtok(tmp, ",");
227 if (strlen(token) >= MONITOR_NAME_LEN) {
228 printf(_("%s: max monitor name length"
229 " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
233 for (num = 0; num < avail_monitors; num++) {
234 if (!strcmp(monitors[num]->name, token)) {
235 dprint("Found requested monitor: %s\n", token);
236 tmp_mons[hits] = monitors[num];
242 printf(_("No matching monitor found in %s, "
243 "try -l option\n"), param);
246 /* Override detected/registerd monitors array with requested one */
247 memcpy(monitors, tmp_mons,
248 sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
249 avail_monitors = hits;
252 void list_monitors(void)
258 for (mon = 0; mon < avail_monitors; mon++) {
259 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
261 monitors[mon]->name, monitors[mon]->hw_states_num,
262 monitors[mon]->overflow_s);
264 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
265 s = monitors[mon]->hw_states[state];
267 * ToDo show more state capabilities:
268 * percent, time (granlarity)
270 printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
276 int fork_it(char **argv)
280 unsigned long long timediff;
282 struct timespec start, end;
285 clock_gettime(CLOCK_REALTIME, &start);
287 for (num = 0; num < avail_monitors; num++)
288 monitors[num]->start();
292 execvp(argv[0], argv);
295 if (child_pid == -1) {
300 signal(SIGINT, SIG_IGN);
301 signal(SIGQUIT, SIG_IGN);
302 if (waitpid(child_pid, &status, 0) == -1) {
307 clock_gettime(CLOCK_REALTIME, &end);
308 for (num = 0; num < avail_monitors; num++)
309 monitors[num]->stop();
311 timediff = timespec_diff_us(start, end);
312 if (WIFEXITED(status))
313 printf(_("%s took %.5f seconds and exited with status %d\n"),
314 argv[0], timediff / (1000.0 * 1000),
315 WEXITSTATUS(status));
319 int do_interval_measure(int i)
325 for (cpu = 0; cpu < cpu_count; cpu++)
328 for (num = 0; num < avail_monitors; num++) {
329 dprint("HW C-state residency monitor: %s - States: %d\n",
330 monitors[num]->name, monitors[num]->hw_states_num);
331 monitors[num]->start();
337 for (cpu = 0; cpu < cpu_count; cpu++)
340 for (num = 0; num < avail_monitors; num++)
341 monitors[num]->stop();
347 static void cmdline(int argc, char *argv[])
350 progname = basename(argv[0]);
352 while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
356 print_wrong_arg_exit();
360 /* only allow -i with -m or no option */
361 if (mode && mode != show)
362 print_wrong_arg_exit();
363 interval = atoi(optarg);
367 print_wrong_arg_exit();
369 show_monitors_param = optarg;
375 print_wrong_arg_exit();
382 int cmd_monitor(int argc, char **argv)
385 struct cpuidle_monitor *test_mon;
389 cpu_count = get_cpu_topology(&cpu_top);
391 printf(_("Cannot read number of available processors\n"));
395 if (!cpu_top.core_info[0].is_online)
396 printf("WARNING: at least one cpu is offline\n");
398 /* Default is: monitor all CPUs */
399 if (bitmask_isallclear(cpus_chosen))
400 bitmask_setall(cpus_chosen);
402 dprint("System has up to %d CPU cores\n", cpu_count);
404 for (num = 0; all_monitors[num]; num++) {
405 dprint("Try to register: %s\n", all_monitors[num]->name);
406 test_mon = all_monitors[num]->do_register();
408 if (test_mon->needs_root && !run_as_root) {
409 fprintf(stderr, _("Available monitor %s needs "
410 "root access\n"), test_mon->name);
413 monitors[avail_monitors] = test_mon;
414 dprint("%s registered\n", all_monitors[num]->name);
419 if (avail_monitors == 0) {
420 printf(_("No HW Cstate monitors found\n"));
430 parse_monitor_param(show_monitors_param);
432 dprint("Packages: %d - Cores: %d - CPUs: %d\n",
433 cpu_top.pkgs, cpu_top.cores, cpu_count);
436 * if any params left, it must be a command to fork
439 fork_it(argv + optind);
441 do_interval_measure(interval);
443 /* ToDo: Topology parsing needs fixing first to do
444 this more generically */
445 if (cpu_top.pkgs > 1)
450 for (cpu = 0; cpu < cpu_count; cpu++) {
451 if (cpu_top.pkgs > 1)
452 print_results(3, cpu);
454 print_results(1, cpu);
457 for (num = 0; num < avail_monitors; num++)
458 monitors[num]->unregister();
460 cpu_topology_release(cpu_top);