cpupowerutils - cpufrequtils extended with quite some features
[linux-2.6.git] / tools / power / cpupower / utils / idle_monitor / cpupower-monitor.c
1 /*
2  *  (C) 2010,2011       Thomas Renninger <trenn@suse.de>, Novell Inc.
3  *
4  *  Licensed under the terms of the GNU GPL License version 2.
5  *
6  *  Output format inspired by Len Brown's <lenb@kernel.org> turbostat tool.
7  *
8  */
9
10
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>
20
21 #include "idle_monitor/cpupower-monitor.h"
22 #include "idle_monitor/idle_monitors.h"
23 #include "helpers/helpers.h"
24
25 /* Define pointers to all monitors.  */
26 #define DEF(x) & x ## _monitor ,
27 struct cpuidle_monitor * all_monitors[] = {
28 #include "idle_monitors.def"
29 0
30 };
31
32 static struct cpuidle_monitor *monitors[MONITORS_MAX];
33 static unsigned int avail_monitors;
34
35 static char *progname;
36
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;
42
43 /* ToDo: Document this in the manpage */
44 static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };
45
46 long long timespec_diff_us(struct timespec start, struct timespec end)
47 {
48         struct timespec temp;
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;
52         } else {
53                 temp.tv_sec = end.tv_sec - start.tv_sec;
54                 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
55         }
56         return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
57 }
58
59 void monitor_help(void)
60 {
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"));
69         printf("\n");
70         printf(_("only one of: -l, -m are allowed\nIf none of them is passed,"));
71         printf(_(" all supported monitors are shown\n"));
72 }
73
74 void print_n_spaces(int n)
75 {
76         int x;
77         for (x = 0; x < n; x++)
78                 printf(" ");
79 }       
80
81 /* size of s must be at least n + 1 */
82 int fill_string_with_spaces(char *s, int n)
83 {
84         int len = strlen(s);
85         if (len > n)
86                 return -1;
87         for (; len < n; len++)
88                 s[len] = ' ';
89         s[len] = '\0';
90         return 0;
91
92
93 void print_header(int topology_depth)
94 {
95         int unsigned mon;
96         int state, need_len, pr_mon_len;
97         cstate_t s;
98         char buf[128] = "";
99         int percent_width = 4;
100
101         fill_string_with_spaces(buf, topology_depth * 5 - 1);
102         printf("%s|", buf);
103
104         for (mon = 0; mon < avail_monitors; mon++) {
105                 pr_mon_len = 0;
106                 need_len = monitors[mon]->hw_states_num * (percent_width + 3)
107                         - 1;
108                 if (mon != 0) {
109                         printf("|| ");
110                         need_len --;
111                 }
112                 sprintf(buf, "%s", monitors[mon]->name);
113                 fill_string_with_spaces(buf, need_len);
114                 printf("%s", buf);
115         }
116         printf("\n");
117
118         if (topology_depth > 2)
119                 printf("PKG |");
120         if (topology_depth > 1)
121                 printf("CORE|");
122         if (topology_depth > 0)
123                 printf("CPU |");
124
125         for (mon = 0; mon < avail_monitors; mon++) {
126                 if (mon != 0)
127                         printf("|| ");
128                 else
129                         printf(" ");
130                 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
131                         if (state != 0)
132                                 printf(" | ");
133                         s = monitors[mon]->hw_states[state];
134                         sprintf(buf, "%s", s.name);
135                         fill_string_with_spaces(buf, percent_width);
136                         printf("%s", buf);
137                 }
138                 printf(" ");
139         }
140         printf("\n");
141 }
142
143
144 void print_results(int topology_depth, int cpu)
145 {
146         unsigned int mon;
147         int state, ret;
148         double percent;
149         unsigned long long result;
150         cstate_t s;
151
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);
158
159         for (mon = 0; mon < avail_monitors; mon++) {
160                 if (mon != 0)
161                         printf("||");
162
163                 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
164                         if (state != 0)
165                                 printf("|");
166
167                         s = monitors[mon]->hw_states[state];
168
169                         if (s.get_count_percent) {
170                                 ret = s.get_count_percent(s.id, &percent,
171                                                   cpu_top.core_info[cpu].cpu);
172                                 if (ret) {
173                                         printf("******");
174                                 } else if (percent >= 100.0)
175                                         printf("%6.1f", percent);
176                                 else
177                                         printf("%6.2f", percent);
178                         }
179                         else if (s.get_count) {
180                                 ret = s.get_count(s.id, &result,
181                                                   cpu_top.core_info[cpu].cpu);
182                                 if (ret) {
183                                         printf("******");
184                                 } else
185                                         printf("%6llu", result);
186                         }
187                         else {
188                                 printf(_("Monitor %s, Counter %s has no count "
189                                          "function. Implementation error\n"),
190                                        monitors[mon]->name, s.name);
191                                 exit (EXIT_FAILURE);
192                         }
193                 }
194         }
195         /* cpu offline */
196         if (cpu_top.core_info[cpu].pkg == -1 ||
197             cpu_top.core_info[cpu].core == -1) {
198                 printf(_(" *is offline\n"));
199                 return;
200         } else
201                 printf("\n");
202 }
203
204
205 /* param: string passed by -m param (The list of monitors to show)
206  *
207  * Monitors must have been registered already, matching monitors
208  * are picked out and available monitors array is overridden
209  * with matching ones
210  *
211  * Monitors get sorted in the same order the user passes them
212 */
213
214 static void parse_monitor_param(char* param)
215 {
216         unsigned int num;
217         int mon, hits = 0;
218         char *tmp = param, *token;
219         struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
220
221
222         for (mon = 0; mon < MONITORS_MAX;mon++, tmp = NULL) {
223                 token = strtok(tmp, ",");
224                 if (token == NULL)
225                         break;
226                 if (strlen(token) >= MONITOR_NAME_LEN) {
227                         printf(_("%s: max monitor name length"
228                                  " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
229                         continue;
230                 }
231
232                 for (num = 0; num < avail_monitors; num++) {
233                         if (!strcmp(monitors[num]->name, token)) {
234                                 dprint("Found requested monitor: %s\n", token);
235                                 tmp_mons[hits] = monitors[num];
236                                 hits++;
237                         }
238                 }       
239         }
240         if (hits == 0) {
241                 printf(_("No matching monitor found in %s, "
242                          "try -l option\n"), param);
243                 monitor_help();
244                 exit(EXIT_FAILURE);
245         }
246         /* Override detected/registerd monitors array with requested one */
247         memcpy(monitors, tmp_mons, sizeof(struct cpuidle_monitor*) * MONITORS_MAX);
248         avail_monitors = hits;
249 }
250
251 void list_monitors(void) {
252         unsigned int mon;
253         int state;
254         cstate_t s;
255
256         for (mon = 0; mon < avail_monitors; mon++) {
257                 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
258                          "s\n"), monitors[mon]->name, monitors[mon]->hw_states_num,
259                        monitors[mon]->overflow_s);
260                        
261                 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
262                         s = monitors[mon]->hw_states[state];
263                         /*
264                          * ToDo show more state capabilities:
265                          * percent, time (granlarity)
266                          */
267                         printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
268                                gettext(s.desc));
269                 }
270         }
271 }
272
273 int fork_it(char **argv)
274 {
275         int status;
276         unsigned int num;
277         unsigned long long timediff;
278         pid_t child_pid;
279         struct timespec start, end;
280
281         child_pid = fork();
282         clock_gettime(CLOCK_REALTIME, &start);
283
284         for (num = 0; num < avail_monitors; num++)
285                 monitors[num]->start();
286
287         if (!child_pid) {
288                 /* child */
289                 execvp(argv[0], argv);
290         } else {
291                 /* parent */
292                 if (child_pid == -1) {
293                         perror("fork");
294                         exit(1);
295                 }
296
297                 signal(SIGINT, SIG_IGN);
298                 signal(SIGQUIT, SIG_IGN);
299                 if (waitpid(child_pid, &status, 0) == -1) {
300                         perror("wait");
301                         exit(1);
302                 }
303         }
304         clock_gettime(CLOCK_REALTIME, &end);
305         for (num = 0; num < avail_monitors; num++)
306                 monitors[num]->stop();
307
308         timediff = timespec_diff_us(start, end);
309         if (WIFEXITED(status))
310                 printf(_("%s took %.5f seconds and exited with status %d\n"),
311                        argv[0], timediff / (1000.0 * 1000), WEXITSTATUS(status));
312         return 0;
313 }
314
315 int do_interval_measure(int i)
316 {
317         unsigned int num;
318
319         for (num = 0; num < avail_monitors; num++) {
320                 dprint("HW C-state residency monitor: %s - States: %d\n",
321                        monitors[num]->name, monitors[num]->hw_states_num);
322                 monitors[num]->start();
323         }
324         sleep(i);
325         for (num = 0; num < avail_monitors; num++) {
326                 monitors[num]->stop();
327         }
328         return 0;
329 }
330
331 static void cmdline(int argc, char *argv[])
332 {
333         int opt;
334         progname = basename(argv[0]);
335
336         while ((opt = getopt(argc, argv, "+hli:m:")) != -1) {
337                 switch (opt) {
338                 case 'h':
339                         monitor_help();
340                         exit(EXIT_SUCCESS);
341                 case 'l':
342                         if (mode) {
343                                 monitor_help();
344                                 exit(EXIT_FAILURE);
345                         }
346                         mode = list;
347                         break;
348                 case 'i':
349                         /* only allow -i with -m or no option */
350                         if (mode && mode != show) {
351                                 monitor_help();
352                                 exit(EXIT_FAILURE);
353                         }
354                         interval = atoi(optarg);
355                         break;
356                 case 'm':
357                         if (mode) {
358                                 monitor_help();
359                                 exit(EXIT_FAILURE);
360                         }
361                         mode = show;
362                         show_monitors_param = optarg;
363                         break;
364                 default:
365                         monitor_help();
366                         exit(EXIT_FAILURE);
367                 }
368         }
369         if (!mode)
370                 mode = show_all;
371 }
372
373 int cmd_monitor(int argc, char **argv)
374 {
375         unsigned int num;
376         struct cpuidle_monitor *test_mon;
377         int cpu;
378
379         cmdline(argc, argv);
380         cpu_count = get_cpu_topology(&cpu_top);
381         if (cpu_count < 0) {
382                 printf(_("Cannot read number of available processors\n"));
383                 return EXIT_FAILURE;
384         }
385
386         dprint("System has up to %d CPU cores\n", cpu_count);
387         
388         for (num = 0; all_monitors[num]; num++) {
389                 dprint("Try to register: %s\n", all_monitors[num]->name);
390                 test_mon = all_monitors[num]->do_register();
391                 if (test_mon) {
392                         if (test_mon->needs_root && !run_as_root) {
393                                 fprintf(stderr, _("Available monitor %s needs "
394                                           "root access\n"), test_mon->name);
395                                 continue;
396                         }
397                         monitors[avail_monitors] = test_mon;
398                         dprint("%s registered\n", all_monitors[num]->name);
399                         avail_monitors++;
400                 }
401         }
402
403         if (avail_monitors == 0) {
404                 printf(_("No HW Cstate monitors found\n"));
405                 return 1;
406         }
407
408         if (mode == list) {
409                 list_monitors();
410                 exit(EXIT_SUCCESS);
411         }
412
413         if (mode == show)
414                 parse_monitor_param(show_monitors_param);
415
416         dprint("Packages: %d - Cores: %d - CPUs: %d\n",
417                cpu_top.pkgs, cpu_top.cores, cpu_count);
418
419         /*
420          * if any params left, it must be a command to fork
421          */
422         if (argc - optind)
423                 fork_it(argv + optind);
424         else
425                 do_interval_measure(interval);
426
427         /* ToDo: Topology parsing needs fixing first to do
428            this more generically */
429         if (cpu_top.pkgs > 1)
430                 print_header(3);
431         else
432                 print_header(1);
433
434         for (cpu = 0; cpu < cpu_count; cpu++) {
435                 if (cpu_top.pkgs > 1)
436                         print_results(3, cpu);
437                 else
438                         print_results(1, cpu);
439         }
440
441         for (num = 0; num < avail_monitors; num++) {
442                 monitors[num]->unregister();
443         }
444         cpu_topology_release(cpu_top);
445         return 0;
446 }