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;
43 /* ToDo: Document this in the manpage */
44 static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };
46 long long timespec_diff_us(struct timespec start, struct timespec end)
49 if ((end.tv_nsec - start.tv_nsec) < 0) {
50 temp.tv_sec = end.tv_sec - start.tv_sec - 1;
51 temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
53 temp.tv_sec = end.tv_sec - start.tv_sec;
54 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
56 return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
59 void monitor_help(void)
61 printf(_("cpupower monitor: [-m <mon1>,[<mon2>],.. ] command\n"));
62 printf(_("cpupower monitor: [-m <mon1>,[<mon2>],.. ] [ -i interval_sec ]\n"));
63 printf(_("cpupower monitor: -l\n"));
64 printf(_("\t command: pass an arbitrary command to measure specific workload\n"));
65 printf(_("\t -i: time intervall to measure for in seconds (default 1)\n"));
66 printf(_("\t -l: list available CPU sleep monitors (for use with -m)\n"));
67 printf(_("\t -m: show specific CPU sleep monitors only (in same order)\n"));
68 printf(_("\t -h: print this help\n"));
70 printf(_("only one of: -l, -m are allowed\nIf none of them is passed,"));
71 printf(_(" all supported monitors are shown\n"));
74 void print_n_spaces(int n)
77 for (x = 0; x < n; x++)
81 /* size of s must be at least n + 1 */
82 int fill_string_with_spaces(char *s, int n)
87 for (; len < n; len++)
93 void print_header(int topology_depth)
96 int state, need_len, pr_mon_len;
99 int percent_width = 4;
101 fill_string_with_spaces(buf, topology_depth * 5 - 1);
104 for (mon = 0; mon < avail_monitors; mon++) {
106 need_len = monitors[mon]->hw_states_num * (percent_width + 3)
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++) {
130 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
133 s = monitors[mon]->hw_states[state];
134 sprintf(buf, "%s", s.name);
135 fill_string_with_spaces(buf, percent_width);
144 void print_results(int topology_depth, int cpu)
149 unsigned long long result;
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 if (cpu_top.core_info[cpu].pkg == -1 ||
195 cpu_top.core_info[cpu].core == -1) {
196 printf(_(" *is offline\n"));
203 /* param: string passed by -m param (The list of monitors to show)
205 * Monitors must have been registered already, matching monitors
206 * are picked out and available monitors array is overridden
209 * Monitors get sorted in the same order the user passes them
212 static void parse_monitor_param(char *param)
216 char *tmp = param, *token;
217 struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
220 for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
221 token = strtok(tmp, ",");
224 if (strlen(token) >= MONITOR_NAME_LEN) {
225 printf(_("%s: max monitor name length"
226 " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
230 for (num = 0; num < avail_monitors; num++) {
231 if (!strcmp(monitors[num]->name, token)) {
232 dprint("Found requested monitor: %s\n", token);
233 tmp_mons[hits] = monitors[num];
239 printf(_("No matching monitor found in %s, "
240 "try -l option\n"), param);
244 /* Override detected/registerd monitors array with requested one */
245 memcpy(monitors, tmp_mons,
246 sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
247 avail_monitors = hits;
250 void list_monitors(void)
256 for (mon = 0; mon < avail_monitors; mon++) {
257 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
259 monitors[mon]->name, monitors[mon]->hw_states_num,
260 monitors[mon]->overflow_s);
262 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
263 s = monitors[mon]->hw_states[state];
265 * ToDo show more state capabilities:
266 * percent, time (granlarity)
268 printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
274 int fork_it(char **argv)
278 unsigned long long timediff;
280 struct timespec start, end;
283 clock_gettime(CLOCK_REALTIME, &start);
285 for (num = 0; num < avail_monitors; num++)
286 monitors[num]->start();
290 execvp(argv[0], argv);
293 if (child_pid == -1) {
298 signal(SIGINT, SIG_IGN);
299 signal(SIGQUIT, SIG_IGN);
300 if (waitpid(child_pid, &status, 0) == -1) {
305 clock_gettime(CLOCK_REALTIME, &end);
306 for (num = 0; num < avail_monitors; num++)
307 monitors[num]->stop();
309 timediff = timespec_diff_us(start, end);
310 if (WIFEXITED(status))
311 printf(_("%s took %.5f seconds and exited with status %d\n"),
312 argv[0], timediff / (1000.0 * 1000),
313 WEXITSTATUS(status));
317 int do_interval_measure(int i)
321 for (num = 0; num < avail_monitors; num++) {
322 dprint("HW C-state residency monitor: %s - States: %d\n",
323 monitors[num]->name, monitors[num]->hw_states_num);
324 monitors[num]->start();
327 for (num = 0; num < avail_monitors; num++)
328 monitors[num]->stop();
333 static void cmdline(int argc, char *argv[])
336 progname = basename(argv[0]);
338 while ((opt = getopt(argc, argv, "+hli:m:")) != -1) {
351 /* only allow -i with -m or no option */
352 if (mode && mode != show) {
356 interval = atoi(optarg);
364 show_monitors_param = optarg;
375 int cmd_monitor(int argc, char **argv)
378 struct cpuidle_monitor *test_mon;
382 cpu_count = get_cpu_topology(&cpu_top);
384 printf(_("Cannot read number of available processors\n"));
388 dprint("System has up to %d CPU cores\n", cpu_count);
390 for (num = 0; all_monitors[num]; num++) {
391 dprint("Try to register: %s\n", all_monitors[num]->name);
392 test_mon = all_monitors[num]->do_register();
394 if (test_mon->needs_root && !run_as_root) {
395 fprintf(stderr, _("Available monitor %s needs "
396 "root access\n"), test_mon->name);
399 monitors[avail_monitors] = test_mon;
400 dprint("%s registered\n", all_monitors[num]->name);
405 if (avail_monitors == 0) {
406 printf(_("No HW Cstate monitors found\n"));
416 parse_monitor_param(show_monitors_param);
418 dprint("Packages: %d - Cores: %d - CPUs: %d\n",
419 cpu_top.pkgs, cpu_top.cores, cpu_count);
422 * if any params left, it must be a command to fork
425 fork_it(argv + optind);
427 do_interval_measure(interval);
429 /* ToDo: Topology parsing needs fixing first to do
430 this more generically */
431 if (cpu_top.pkgs > 1)
436 for (cpu = 0; cpu < cpu_count; cpu++) {
437 if (cpu_top.pkgs > 1)
438 print_results(3, cpu);
440 print_results(1, cpu);
443 for (num = 0; num < avail_monitors; num++)
444 monitors[num]->unregister();
446 cpu_topology_release(cpu_top);