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 /* size of s must be at least n + 1 */
76 int fill_string_with_spaces(char *s, int n)
81 for (; len < n; len++)
87 void print_header(int topology_depth)
93 int percent_width = 4;
95 fill_string_with_spaces(buf, topology_depth * 5 - 1);
98 for (mon = 0; mon < avail_monitors; mon++) {
99 need_len = monitors[mon]->hw_states_num * (percent_width + 3)
105 sprintf(buf, "%s", monitors[mon]->name);
106 fill_string_with_spaces(buf, need_len);
111 if (topology_depth > 2)
113 if (topology_depth > 1)
115 if (topology_depth > 0)
118 for (mon = 0; mon < avail_monitors; mon++) {
123 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
126 s = monitors[mon]->hw_states[state];
127 sprintf(buf, "%s", s.name);
128 fill_string_with_spaces(buf, percent_width);
137 void print_results(int topology_depth, int cpu)
142 unsigned long long result;
145 /* Be careful CPUs may got resorted for pkg value do not just use cpu */
146 if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
148 if (!cpu_top.core_info[cpu].is_online &&
149 cpu_top.core_info[cpu].pkg == -1)
152 if (topology_depth > 2)
153 printf("%4d|", cpu_top.core_info[cpu].pkg);
154 if (topology_depth > 1)
155 printf("%4d|", cpu_top.core_info[cpu].core);
156 if (topology_depth > 0)
157 printf("%4d|", cpu_top.core_info[cpu].cpu);
159 for (mon = 0; mon < avail_monitors; mon++) {
163 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
167 s = monitors[mon]->hw_states[state];
169 if (s.get_count_percent) {
170 ret = s.get_count_percent(s.id, &percent,
171 cpu_top.core_info[cpu].cpu);
174 else if (percent >= 100.0)
175 printf("%6.1f", percent);
177 printf("%6.2f", percent);
178 } else if (s.get_count) {
179 ret = s.get_count(s.id, &result,
180 cpu_top.core_info[cpu].cpu);
184 printf("%6llu", result);
186 printf(_("Monitor %s, Counter %s has no count "
187 "function. Implementation error\n"),
188 monitors[mon]->name, s.name);
194 * The monitor could still provide useful data, for example
195 * AMD HW counters partly sit in PCI config space.
196 * It's up to the monitor plug-in to check .is_online, this one
197 * is just for additional info.
199 if (!cpu_top.core_info[cpu].is_online &&
200 cpu_top.core_info[cpu].pkg != -1) {
201 printf(_(" *is offline\n"));
208 /* param: string passed by -m param (The list of monitors to show)
210 * Monitors must have been registered already, matching monitors
211 * are picked out and available monitors array is overridden
214 * Monitors get sorted in the same order the user passes them
217 static void parse_monitor_param(char *param)
221 char *tmp = param, *token;
222 struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
225 for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
226 token = strtok(tmp, ",");
229 if (strlen(token) >= MONITOR_NAME_LEN) {
230 printf(_("%s: max monitor name length"
231 " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
235 for (num = 0; num < avail_monitors; num++) {
236 if (!strcmp(monitors[num]->name, token)) {
237 dprint("Found requested monitor: %s\n", token);
238 tmp_mons[hits] = monitors[num];
244 printf(_("No matching monitor found in %s, "
245 "try -l option\n"), param);
248 /* Override detected/registerd monitors array with requested one */
249 memcpy(monitors, tmp_mons,
250 sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
251 avail_monitors = hits;
254 void list_monitors(void)
260 for (mon = 0; mon < avail_monitors; mon++) {
261 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
263 monitors[mon]->name, monitors[mon]->hw_states_num,
264 monitors[mon]->overflow_s);
266 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
267 s = monitors[mon]->hw_states[state];
269 * ToDo show more state capabilities:
270 * percent, time (granlarity)
272 printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
278 int fork_it(char **argv)
282 unsigned long long timediff;
284 struct timespec start, end;
287 clock_gettime(CLOCK_REALTIME, &start);
289 for (num = 0; num < avail_monitors; num++)
290 monitors[num]->start();
294 execvp(argv[0], argv);
297 if (child_pid == -1) {
302 signal(SIGINT, SIG_IGN);
303 signal(SIGQUIT, SIG_IGN);
304 if (waitpid(child_pid, &status, 0) == -1) {
309 clock_gettime(CLOCK_REALTIME, &end);
310 for (num = 0; num < avail_monitors; num++)
311 monitors[num]->stop();
313 timediff = timespec_diff_us(start, end);
314 if (WIFEXITED(status))
315 printf(_("%s took %.5f seconds and exited with status %d\n"),
316 argv[0], timediff / (1000.0 * 1000),
317 WEXITSTATUS(status));
321 int do_interval_measure(int i)
327 for (cpu = 0; cpu < cpu_count; cpu++)
330 for (num = 0; num < avail_monitors; num++) {
331 dprint("HW C-state residency monitor: %s - States: %d\n",
332 monitors[num]->name, monitors[num]->hw_states_num);
333 monitors[num]->start();
339 for (cpu = 0; cpu < cpu_count; cpu++)
342 for (num = 0; num < avail_monitors; num++)
343 monitors[num]->stop();
349 static void cmdline(int argc, char *argv[])
352 progname = basename(argv[0]);
354 while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
358 print_wrong_arg_exit();
362 /* only allow -i with -m or no option */
363 if (mode && mode != show)
364 print_wrong_arg_exit();
365 interval = atoi(optarg);
369 print_wrong_arg_exit();
371 show_monitors_param = optarg;
377 print_wrong_arg_exit();
384 int cmd_monitor(int argc, char **argv)
387 struct cpuidle_monitor *test_mon;
391 cpu_count = get_cpu_topology(&cpu_top);
393 printf(_("Cannot read number of available processors\n"));
397 if (!cpu_top.core_info[0].is_online)
398 printf("WARNING: at least one cpu is offline\n");
400 /* Default is: monitor all CPUs */
401 if (bitmask_isallclear(cpus_chosen))
402 bitmask_setall(cpus_chosen);
404 dprint("System has up to %d CPU cores\n", cpu_count);
406 for (num = 0; all_monitors[num]; num++) {
407 dprint("Try to register: %s\n", all_monitors[num]->name);
408 test_mon = all_monitors[num]->do_register();
410 if (test_mon->needs_root && !run_as_root) {
411 fprintf(stderr, _("Available monitor %s needs "
412 "root access\n"), test_mon->name);
415 monitors[avail_monitors] = test_mon;
416 dprint("%s registered\n", all_monitors[num]->name);
421 if (avail_monitors == 0) {
422 printf(_("No HW Cstate monitors found\n"));
432 parse_monitor_param(show_monitors_param);
434 dprint("Packages: %d - Cores: %d - CPUs: %d\n",
435 cpu_top.pkgs, cpu_top.cores, cpu_count);
438 * if any params left, it must be a command to fork
441 fork_it(argv + optind);
443 do_interval_measure(interval);
445 /* ToDo: Topology parsing needs fixing first to do
446 this more generically */
447 if (cpu_top.pkgs > 1)
452 for (cpu = 0; cpu < cpu_count; cpu++) {
453 if (cpu_top.pkgs > 1)
454 print_results(3, cpu);
456 print_results(1, cpu);
459 for (num = 0; num < avail_monitors; num++)
460 monitors[num]->unregister();
462 cpu_topology_release(cpu_top);