cpupower tools: Fix minor warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / power / cpupower / utils / idle_monitor / cpupower-monitor.c
blob7a657f3da23b73aa4b2ce07373d50efe5f7d1810
1 /*
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.
8 */
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <time.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <libgen.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 };
38 static int mode;
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 static void print_wrong_arg_exit(void)
48 printf(_("invalid or unknown argument\n"));
49 exit(EXIT_FAILURE);
52 long long timespec_diff_us(struct timespec start, struct timespec end)
54 struct timespec temp;
55 if ((end.tv_nsec - start.tv_nsec) < 0) {
56 temp.tv_sec = end.tv_sec - start.tv_sec - 1;
57 temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
58 } else {
59 temp.tv_sec = end.tv_sec - start.tv_sec;
60 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
62 return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
65 void print_n_spaces(int n)
67 int x;
68 for (x = 0; x < n; x++)
69 printf(" ");
72 /* size of s must be at least n + 1 */
73 int fill_string_with_spaces(char *s, int n)
75 int len = strlen(s);
76 if (len > n)
77 return -1;
78 for (; len < n; len++)
79 s[len] = ' ';
80 s[len] = '\0';
81 return 0;
84 void print_header(int topology_depth)
86 int unsigned mon;
87 int state, need_len;
88 cstate_t s;
89 char buf[128] = "";
90 int percent_width = 4;
92 fill_string_with_spaces(buf, topology_depth * 5 - 1);
93 printf("%s|", buf);
95 for (mon = 0; mon < avail_monitors; mon++) {
96 need_len = monitors[mon]->hw_states_num * (percent_width + 3)
97 - 1;
98 if (mon != 0) {
99 printf("|| ");
100 need_len--;
102 sprintf(buf, "%s", monitors[mon]->name);
103 fill_string_with_spaces(buf, need_len);
104 printf("%s", buf);
106 printf("\n");
108 if (topology_depth > 2)
109 printf("PKG |");
110 if (topology_depth > 1)
111 printf("CORE|");
112 if (topology_depth > 0)
113 printf("CPU |");
115 for (mon = 0; mon < avail_monitors; mon++) {
116 if (mon != 0)
117 printf("|| ");
118 else
119 printf(" ");
120 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
121 if (state != 0)
122 printf(" | ");
123 s = monitors[mon]->hw_states[state];
124 sprintf(buf, "%s", s.name);
125 fill_string_with_spaces(buf, percent_width);
126 printf("%s", buf);
128 printf(" ");
130 printf("\n");
134 void print_results(int topology_depth, int cpu)
136 unsigned int mon;
137 int state, ret;
138 double percent;
139 unsigned long long result;
140 cstate_t s;
142 /* Be careful CPUs may got resorted for pkg value do not just use cpu */
143 if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
144 return;
146 if (topology_depth > 2)
147 printf("%4d|", cpu_top.core_info[cpu].pkg);
148 if (topology_depth > 1)
149 printf("%4d|", cpu_top.core_info[cpu].core);
150 if (topology_depth > 0)
151 printf("%4d|", cpu_top.core_info[cpu].cpu);
153 for (mon = 0; mon < avail_monitors; mon++) {
154 if (mon != 0)
155 printf("||");
157 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
158 if (state != 0)
159 printf("|");
161 s = monitors[mon]->hw_states[state];
163 if (s.get_count_percent) {
164 ret = s.get_count_percent(s.id, &percent,
165 cpu_top.core_info[cpu].cpu);
166 if (ret)
167 printf("******");
168 else if (percent >= 100.0)
169 printf("%6.1f", percent);
170 else
171 printf("%6.2f", percent);
172 } else if (s.get_count) {
173 ret = s.get_count(s.id, &result,
174 cpu_top.core_info[cpu].cpu);
175 if (ret)
176 printf("******");
177 else
178 printf("%6llu", result);
179 } else {
180 printf(_("Monitor %s, Counter %s has no count "
181 "function. Implementation error\n"),
182 monitors[mon]->name, s.name);
183 exit(EXIT_FAILURE);
188 * The monitor could still provide useful data, for example
189 * AMD HW counters partly sit in PCI config space.
190 * It's up to the monitor plug-in to check .is_online, this one
191 * is just for additional info.
193 if (!cpu_top.core_info[cpu].is_online) {
194 printf(_(" *is offline\n"));
195 return;
196 } else
197 printf("\n");
201 /* param: string passed by -m param (The list of monitors to show)
203 * Monitors must have been registered already, matching monitors
204 * are picked out and available monitors array is overridden
205 * with matching ones
207 * Monitors get sorted in the same order the user passes them
210 static void parse_monitor_param(char *param)
212 unsigned int num;
213 int mon, hits = 0;
214 char *tmp = param, *token;
215 struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
218 for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
219 token = strtok(tmp, ",");
220 if (token == NULL)
221 break;
222 if (strlen(token) >= MONITOR_NAME_LEN) {
223 printf(_("%s: max monitor name length"
224 " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
225 continue;
228 for (num = 0; num < avail_monitors; num++) {
229 if (!strcmp(monitors[num]->name, token)) {
230 dprint("Found requested monitor: %s\n", token);
231 tmp_mons[hits] = monitors[num];
232 hits++;
236 if (hits == 0) {
237 printf(_("No matching monitor found in %s, "
238 "try -l option\n"), param);
239 exit(EXIT_FAILURE);
241 /* Override detected/registerd monitors array with requested one */
242 memcpy(monitors, tmp_mons,
243 sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
244 avail_monitors = hits;
247 void list_monitors(void)
249 unsigned int mon;
250 int state;
251 cstate_t s;
253 for (mon = 0; mon < avail_monitors; mon++) {
254 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
255 "s\n"),
256 monitors[mon]->name, monitors[mon]->hw_states_num,
257 monitors[mon]->overflow_s);
259 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
260 s = monitors[mon]->hw_states[state];
262 * ToDo show more state capabilities:
263 * percent, time (granlarity)
265 printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
266 gettext(s.desc));
271 int fork_it(char **argv)
273 int status;
274 unsigned int num;
275 unsigned long long timediff;
276 pid_t child_pid;
277 struct timespec start, end;
279 child_pid = fork();
280 clock_gettime(CLOCK_REALTIME, &start);
282 for (num = 0; num < avail_monitors; num++)
283 monitors[num]->start();
285 if (!child_pid) {
286 /* child */
287 execvp(argv[0], argv);
288 } else {
289 /* parent */
290 if (child_pid == -1) {
291 perror("fork");
292 exit(1);
295 signal(SIGINT, SIG_IGN);
296 signal(SIGQUIT, SIG_IGN);
297 if (waitpid(child_pid, &status, 0) == -1) {
298 perror("wait");
299 exit(1);
302 clock_gettime(CLOCK_REALTIME, &end);
303 for (num = 0; num < avail_monitors; num++)
304 monitors[num]->stop();
306 timediff = timespec_diff_us(start, end);
307 if (WIFEXITED(status))
308 printf(_("%s took %.5f seconds and exited with status %d\n"),
309 argv[0], timediff / (1000.0 * 1000),
310 WEXITSTATUS(status));
311 return 0;
314 int do_interval_measure(int i)
316 unsigned int num;
318 for (num = 0; num < avail_monitors; num++) {
319 dprint("HW C-state residency monitor: %s - States: %d\n",
320 monitors[num]->name, monitors[num]->hw_states_num);
321 monitors[num]->start();
323 sleep(i);
324 for (num = 0; num < avail_monitors; num++)
325 monitors[num]->stop();
327 return 0;
330 static void cmdline(int argc, char *argv[])
332 int opt;
333 progname = basename(argv[0]);
335 while ((opt = getopt(argc, argv, "+li:m:")) != -1) {
336 switch (opt) {
337 case 'l':
338 if (mode)
339 print_wrong_arg_exit();
340 mode = list;
341 break;
342 case 'i':
343 /* only allow -i with -m or no option */
344 if (mode && mode != show)
345 print_wrong_arg_exit();
346 interval = atoi(optarg);
347 break;
348 case 'm':
349 if (mode)
350 print_wrong_arg_exit();
351 mode = show;
352 show_monitors_param = optarg;
353 break;
354 default:
355 print_wrong_arg_exit();
358 if (!mode)
359 mode = show_all;
362 int cmd_monitor(int argc, char **argv)
364 unsigned int num;
365 struct cpuidle_monitor *test_mon;
366 int cpu;
368 cmdline(argc, argv);
369 cpu_count = get_cpu_topology(&cpu_top);
370 if (cpu_count < 0) {
371 printf(_("Cannot read number of available processors\n"));
372 return EXIT_FAILURE;
375 /* Default is: monitor all CPUs */
376 if (bitmask_isallclear(cpus_chosen))
377 bitmask_setall(cpus_chosen);
379 dprint("System has up to %d CPU cores\n", cpu_count);
381 for (num = 0; all_monitors[num]; num++) {
382 dprint("Try to register: %s\n", all_monitors[num]->name);
383 test_mon = all_monitors[num]->do_register();
384 if (test_mon) {
385 if (test_mon->needs_root && !run_as_root) {
386 fprintf(stderr, _("Available monitor %s needs "
387 "root access\n"), test_mon->name);
388 continue;
390 monitors[avail_monitors] = test_mon;
391 dprint("%s registered\n", all_monitors[num]->name);
392 avail_monitors++;
396 if (avail_monitors == 0) {
397 printf(_("No HW Cstate monitors found\n"));
398 return 1;
401 if (mode == list) {
402 list_monitors();
403 exit(EXIT_SUCCESS);
406 if (mode == show)
407 parse_monitor_param(show_monitors_param);
409 dprint("Packages: %d - Cores: %d - CPUs: %d\n",
410 cpu_top.pkgs, cpu_top.cores, cpu_count);
413 * if any params left, it must be a command to fork
415 if (argc - optind)
416 fork_it(argv + optind);
417 else
418 do_interval_measure(interval);
420 /* ToDo: Topology parsing needs fixing first to do
421 this more generically */
422 if (cpu_top.pkgs > 1)
423 print_header(3);
424 else
425 print_header(1);
427 for (cpu = 0; cpu < cpu_count; cpu++) {
428 if (cpu_top.pkgs > 1)
429 print_results(3, cpu);
430 else
431 print_results(1, cpu);
434 for (num = 0; num < avail_monitors; num++)
435 monitors[num]->unregister();
437 cpu_topology_release(cpu_top);
438 return 0;