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"
34 static struct cpuidle_monitor *monitors[MONITORS_MAX];
35 static unsigned int avail_monitors;
37 static char *progname;
39 enum operation_mode_e { list = 1, show, show_all };
41 static int interval = 1;
42 static char *show_monitors_param;
43 static struct cpupower_topology cpu_top;
44 static unsigned int wake_cpus;
46 /* ToDo: Document this in the manpage */
47 static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };
49 static void print_wrong_arg_exit(void)
51 printf(_("invalid or unknown argument\n"));
55 long long timespec_diff_us(struct timespec start, struct timespec end)
58 if ((end.tv_nsec - start.tv_nsec) < 0) {
59 temp.tv_sec = end.tv_sec - start.tv_sec - 1;
60 temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
62 temp.tv_sec = end.tv_sec - start.tv_sec;
63 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
65 return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
68 void print_n_spaces(int n)
71 for (x = 0; x < n; x++)
75 /*s is filled with left and right spaces
76 *to make its length atleast n+1
78 int fill_string_with_spaces(char *s, int n)
86 temp = malloc(sizeof(char) * (n+1));
87 for (; len < n; len++)
90 snprintf(temp, n+1, " %s", s);
96 #define MAX_COL_WIDTH 6
97 void print_header(int topology_depth)
104 fill_string_with_spaces(buf, topology_depth * 5 - 1);
107 for (mon = 0; mon < avail_monitors; mon++) {
108 need_len = monitors[mon]->hw_states_num * (MAX_COL_WIDTH + 1)
112 sprintf(buf, "%s", monitors[mon]->name);
113 fill_string_with_spaces(buf, need_len);
118 if (topology_depth > 2)
120 if (topology_depth > 1)
122 if (topology_depth > 0)
125 for (mon = 0; mon < avail_monitors; mon++) {
128 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
131 s = monitors[mon]->hw_states[state];
132 sprintf(buf, "%s", s.name);
133 fill_string_with_spaces(buf, MAX_COL_WIDTH);
142 void print_results(int topology_depth, int cpu)
147 unsigned long long result;
150 /* Be careful CPUs may got resorted for pkg value do not just use cpu */
151 if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
153 if (!cpu_top.core_info[cpu].is_online &&
154 cpu_top.core_info[cpu].pkg == -1)
157 if (topology_depth > 2)
158 printf("%4d|", cpu_top.core_info[cpu].pkg);
159 if (topology_depth > 1)
160 printf("%4d|", cpu_top.core_info[cpu].core);
161 if (topology_depth > 0)
162 printf("%4d|", cpu_top.core_info[cpu].cpu);
164 for (mon = 0; mon < avail_monitors; mon++) {
168 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
172 s = monitors[mon]->hw_states[state];
174 if (s.get_count_percent) {
175 ret = s.get_count_percent(s.id, &percent,
176 cpu_top.core_info[cpu].cpu);
179 else if (percent >= 100.0)
180 printf("%6.1f", percent);
182 printf("%6.2f", percent);
183 } else if (s.get_count) {
184 ret = s.get_count(s.id, &result,
185 cpu_top.core_info[cpu].cpu);
189 printf("%6llu", result);
191 printf(_("Monitor %s, Counter %s has no count "
192 "function. Implementation error\n"),
193 monitors[mon]->name, s.name);
199 * The monitor could still provide useful data, for example
200 * AMD HW counters partly sit in PCI config space.
201 * It's up to the monitor plug-in to check .is_online, this one
202 * is just for additional info.
204 if (!cpu_top.core_info[cpu].is_online &&
205 cpu_top.core_info[cpu].pkg != -1) {
206 printf(_(" *is offline\n"));
213 /* param: string passed by -m param (The list of monitors to show)
215 * Monitors must have been registered already, matching monitors
216 * are picked out and available monitors array is overridden
219 * Monitors get sorted in the same order the user passes them
222 static void parse_monitor_param(char *param)
226 char *tmp = param, *token;
227 struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
230 for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
231 token = strtok(tmp, ",");
234 if (strlen(token) >= MONITOR_NAME_LEN) {
235 printf(_("%s: max monitor name length"
236 " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
240 for (num = 0; num < avail_monitors; num++) {
241 if (!strcmp(monitors[num]->name, token)) {
242 dprint("Found requested monitor: %s\n", token);
243 tmp_mons[hits] = monitors[num];
249 printf(_("No matching monitor found in %s, "
250 "try -l option\n"), param);
253 /* Override detected/registerd monitors array with requested one */
254 memcpy(monitors, tmp_mons,
255 sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
256 avail_monitors = hits;
259 void list_monitors(void)
265 for (mon = 0; mon < avail_monitors; mon++) {
266 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
268 monitors[mon]->name, monitors[mon]->hw_states_num,
269 monitors[mon]->overflow_s);
271 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
272 s = monitors[mon]->hw_states[state];
274 * ToDo show more state capabilities:
275 * percent, time (granlarity)
277 printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
283 int fork_it(char **argv)
287 unsigned long long timediff;
289 struct timespec start, end;
292 clock_gettime(CLOCK_REALTIME, &start);
294 for (num = 0; num < avail_monitors; num++)
295 monitors[num]->start();
299 execvp(argv[0], argv);
302 if (child_pid == -1) {
307 signal(SIGINT, SIG_IGN);
308 signal(SIGQUIT, SIG_IGN);
309 if (waitpid(child_pid, &status, 0) == -1) {
314 clock_gettime(CLOCK_REALTIME, &end);
315 for (num = 0; num < avail_monitors; num++)
316 monitors[num]->stop();
318 timediff = timespec_diff_us(start, end);
319 if (WIFEXITED(status))
320 printf(_("%s took %.5f seconds and exited with status %d\n"),
321 argv[0], timediff / (1000.0 * 1000),
322 WEXITSTATUS(status));
326 int do_interval_measure(int i)
332 for (cpu = 0; cpu < cpu_count; cpu++)
335 for (num = 0; num < avail_monitors; num++) {
336 dprint("HW C-state residency monitor: %s - States: %d\n",
337 monitors[num]->name, monitors[num]->hw_states_num);
338 monitors[num]->start();
344 for (cpu = 0; cpu < cpu_count; cpu++)
347 for (num = 0; num < avail_monitors; num++)
348 monitors[num]->stop();
354 static void cmdline(int argc, char *argv[])
357 progname = basename(argv[0]);
359 while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
363 print_wrong_arg_exit();
367 /* only allow -i with -m or no option */
368 if (mode && mode != show)
369 print_wrong_arg_exit();
370 interval = atoi(optarg);
374 print_wrong_arg_exit();
376 show_monitors_param = optarg;
382 print_wrong_arg_exit();
389 int cmd_monitor(int argc, char **argv)
392 struct cpuidle_monitor *test_mon;
396 cpu_count = get_cpu_topology(&cpu_top);
398 printf(_("Cannot read number of available processors\n"));
402 if (!cpu_top.core_info[0].is_online)
403 printf("WARNING: at least one cpu is offline\n");
405 /* Default is: monitor all CPUs */
406 if (bitmask_isallclear(cpus_chosen))
407 bitmask_setall(cpus_chosen);
409 dprint("System has up to %d CPU cores\n", cpu_count);
411 for (num = 0; all_monitors[num]; num++) {
412 dprint("Try to register: %s\n", all_monitors[num]->name);
413 test_mon = all_monitors[num]->do_register();
415 if (test_mon->needs_root && !run_as_root) {
416 fprintf(stderr, _("Available monitor %s needs "
417 "root access\n"), test_mon->name);
420 monitors[avail_monitors] = test_mon;
421 dprint("%s registered\n", all_monitors[num]->name);
426 if (avail_monitors == 0) {
427 printf(_("No HW Cstate monitors found\n"));
437 parse_monitor_param(show_monitors_param);
439 dprint("Packages: %d - Cores: %d - CPUs: %d\n",
440 cpu_top.pkgs, cpu_top.cores, cpu_count);
443 * if any params left, it must be a command to fork
446 fork_it(argv + optind);
448 do_interval_measure(interval);
450 /* ToDo: Topology parsing needs fixing first to do
451 this more generically */
452 if (cpu_top.pkgs > 1)
457 for (cpu = 0; cpu < cpu_count; cpu++) {
458 if (cpu_top.pkgs > 1)
459 print_results(3, cpu);
461 print_results(1, cpu);
464 for (num = 0; num < avail_monitors; num++)
465 monitors[num]->unregister();
467 cpu_topology_release(cpu_top);