cpupower: Make monitor command -c/--cpu aware
[firefly-linux-kernel-4.4.55.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         /* Be careful CPUs may got resorted for pkg value do not just use cpu */
153         if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
154                 return;
155
156         if (topology_depth > 2)
157                 printf("%4d|", cpu_top.core_info[cpu].pkg);
158         if (topology_depth > 1)
159                 printf("%4d|", cpu_top.core_info[cpu].core);
160         if (topology_depth > 0)
161                 printf("%4d|", cpu_top.core_info[cpu].cpu);
162
163         for (mon = 0; mon < avail_monitors; mon++) {
164                 if (mon != 0)
165                         printf("||");
166
167                 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
168                         if (state != 0)
169                                 printf("|");
170
171                         s = monitors[mon]->hw_states[state];
172
173                         if (s.get_count_percent) {
174                                 ret = s.get_count_percent(s.id, &percent,
175                                                   cpu_top.core_info[cpu].cpu);
176                                 if (ret)
177                                         printf("******");
178                                 else if (percent >= 100.0)
179                                         printf("%6.1f", percent);
180                                 else
181                                         printf("%6.2f", percent);
182                         } else if (s.get_count) {
183                                 ret = s.get_count(s.id, &result,
184                                                   cpu_top.core_info[cpu].cpu);
185                                 if (ret)
186                                         printf("******");
187                                 else
188                                         printf("%6llu", result);
189                         } else {
190                                 printf(_("Monitor %s, Counter %s has no count "
191                                          "function. Implementation error\n"),
192                                        monitors[mon]->name, s.name);
193                                 exit(EXIT_FAILURE);
194                         }
195                 }
196         }
197         /*
198          * The monitor could still provide useful data, for example
199          * AMD HW counters partly sit in PCI config space.
200          * It's up to the monitor plug-in to check .is_online, this one
201          * is just for additional info.
202          */
203         if (!cpu_top.core_info[cpu].is_online) {
204                 printf(_(" *is offline\n"));
205                 return;
206         } else
207                 printf("\n");
208 }
209
210
211 /* param: string passed by -m param (The list of monitors to show)
212  *
213  * Monitors must have been registered already, matching monitors
214  * are picked out and available monitors array is overridden
215  * with matching ones
216  *
217  * Monitors get sorted in the same order the user passes them
218 */
219
220 static void parse_monitor_param(char *param)
221 {
222         unsigned int num;
223         int mon, hits = 0;
224         char *tmp = param, *token;
225         struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
226
227
228         for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
229                 token = strtok(tmp, ",");
230                 if (token == NULL)
231                         break;
232                 if (strlen(token) >= MONITOR_NAME_LEN) {
233                         printf(_("%s: max monitor name length"
234                                  " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
235                         continue;
236                 }
237
238                 for (num = 0; num < avail_monitors; num++) {
239                         if (!strcmp(monitors[num]->name, token)) {
240                                 dprint("Found requested monitor: %s\n", token);
241                                 tmp_mons[hits] = monitors[num];
242                                 hits++;
243                         }
244                 }
245         }
246         if (hits == 0) {
247                 printf(_("No matching monitor found in %s, "
248                          "try -l option\n"), param);
249                 monitor_help();
250                 exit(EXIT_FAILURE);
251         }
252         /* Override detected/registerd monitors array with requested one */
253         memcpy(monitors, tmp_mons,
254                 sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
255         avail_monitors = hits;
256 }
257
258 void list_monitors(void)
259 {
260         unsigned int mon;
261         int state;
262         cstate_t s;
263
264         for (mon = 0; mon < avail_monitors; mon++) {
265                 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
266                          "s\n"),
267                         monitors[mon]->name, monitors[mon]->hw_states_num,
268                         monitors[mon]->overflow_s);
269
270                 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
271                         s = monitors[mon]->hw_states[state];
272                         /*
273                          * ToDo show more state capabilities:
274                          * percent, time (granlarity)
275                          */
276                         printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
277                                gettext(s.desc));
278                 }
279         }
280 }
281
282 int fork_it(char **argv)
283 {
284         int status;
285         unsigned int num;
286         unsigned long long timediff;
287         pid_t child_pid;
288         struct timespec start, end;
289
290         child_pid = fork();
291         clock_gettime(CLOCK_REALTIME, &start);
292
293         for (num = 0; num < avail_monitors; num++)
294                 monitors[num]->start();
295
296         if (!child_pid) {
297                 /* child */
298                 execvp(argv[0], argv);
299         } else {
300                 /* parent */
301                 if (child_pid == -1) {
302                         perror("fork");
303                         exit(1);
304                 }
305
306                 signal(SIGINT, SIG_IGN);
307                 signal(SIGQUIT, SIG_IGN);
308                 if (waitpid(child_pid, &status, 0) == -1) {
309                         perror("wait");
310                         exit(1);
311                 }
312         }
313         clock_gettime(CLOCK_REALTIME, &end);
314         for (num = 0; num < avail_monitors; num++)
315                 monitors[num]->stop();
316
317         timediff = timespec_diff_us(start, end);
318         if (WIFEXITED(status))
319                 printf(_("%s took %.5f seconds and exited with status %d\n"),
320                         argv[0], timediff / (1000.0 * 1000),
321                         WEXITSTATUS(status));
322         return 0;
323 }
324
325 int do_interval_measure(int i)
326 {
327         unsigned int num;
328
329         for (num = 0; num < avail_monitors; num++) {
330                 dprint("HW C-state residency monitor: %s - States: %d\n",
331                        monitors[num]->name, monitors[num]->hw_states_num);
332                 monitors[num]->start();
333         }
334         sleep(i);
335         for (num = 0; num < avail_monitors; num++)
336                 monitors[num]->stop();
337
338         return 0;
339 }
340
341 static void cmdline(int argc, char *argv[])
342 {
343         int opt;
344         progname = basename(argv[0]);
345
346         while ((opt = getopt(argc, argv, "+hli:m:")) != -1) {
347                 switch (opt) {
348                 case 'h':
349                         monitor_help();
350                         exit(EXIT_SUCCESS);
351                 case 'l':
352                         if (mode) {
353                                 monitor_help();
354                                 exit(EXIT_FAILURE);
355                         }
356                         mode = list;
357                         break;
358                 case 'i':
359                         /* only allow -i with -m or no option */
360                         if (mode && mode != show) {
361                                 monitor_help();
362                                 exit(EXIT_FAILURE);
363                         }
364                         interval = atoi(optarg);
365                         break;
366                 case 'm':
367                         if (mode) {
368                                 monitor_help();
369                                 exit(EXIT_FAILURE);
370                         }
371                         mode = show;
372                         show_monitors_param = optarg;
373                         break;
374                 default:
375                         monitor_help();
376                         exit(EXIT_FAILURE);
377                 }
378         }
379         if (!mode)
380                 mode = show_all;
381 }
382
383 int cmd_monitor(int argc, char **argv)
384 {
385         unsigned int num;
386         struct cpuidle_monitor *test_mon;
387         int cpu;
388
389         cmdline(argc, argv);
390         cpu_count = get_cpu_topology(&cpu_top);
391         if (cpu_count < 0) {
392                 printf(_("Cannot read number of available processors\n"));
393                 return EXIT_FAILURE;
394         }
395
396         /* Default is: monitor all CPUs */
397         if (bitmask_isallclear(cpus_chosen))
398                 bitmask_setall(cpus_chosen);
399
400         dprint("System has up to %d CPU cores\n", cpu_count);
401
402         for (num = 0; all_monitors[num]; num++) {
403                 dprint("Try to register: %s\n", all_monitors[num]->name);
404                 test_mon = all_monitors[num]->do_register();
405                 if (test_mon) {
406                         if (test_mon->needs_root && !run_as_root) {
407                                 fprintf(stderr, _("Available monitor %s needs "
408                                           "root access\n"), test_mon->name);
409                                 continue;
410                         }
411                         monitors[avail_monitors] = test_mon;
412                         dprint("%s registered\n", all_monitors[num]->name);
413                         avail_monitors++;
414                 }
415         }
416
417         if (avail_monitors == 0) {
418                 printf(_("No HW Cstate monitors found\n"));
419                 return 1;
420         }
421
422         if (mode == list) {
423                 list_monitors();
424                 exit(EXIT_SUCCESS);
425         }
426
427         if (mode == show)
428                 parse_monitor_param(show_monitors_param);
429
430         dprint("Packages: %d - Cores: %d - CPUs: %d\n",
431                cpu_top.pkgs, cpu_top.cores, cpu_count);
432
433         /*
434          * if any params left, it must be a command to fork
435          */
436         if (argc - optind)
437                 fork_it(argv + optind);
438         else
439                 do_interval_measure(interval);
440
441         /* ToDo: Topology parsing needs fixing first to do
442            this more generically */
443         if (cpu_top.pkgs > 1)
444                 print_header(3);
445         else
446                 print_header(1);
447
448         for (cpu = 0; cpu < cpu_count; cpu++) {
449                 if (cpu_top.pkgs > 1)
450                         print_results(3, cpu);
451                 else
452                         print_results(1, cpu);
453         }
454
455         for (num = 0; num < avail_monitors; num++)
456                 monitors[num]->unregister();
457
458         cpu_topology_release(cpu_top);
459         return 0;
460 }