1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
5 * Output format inspired by Len Brown's <lenb@kernel.org> turbostat tool.
15 #include <sys/types.h>
19 #include "idle_monitor/cpupower-monitor.h"
20 #include "idle_monitor/idle_monitors.h"
21 #include "helpers/helpers.h"
23 /* Define pointers to all monitors. */
24 #define DEF(x) & x ## _monitor ,
25 struct cpuidle_monitor *all_monitors[] = {
26 #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;
42 static unsigned int wake_cpus;
44 /* ToDo: Document this in the manpage */
45 static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };
47 static void print_wrong_arg_exit(void)
49 printf(_("invalid or unknown argument\n"));
53 long long timespec_diff_us(struct timespec start, struct timespec end)
56 if ((end.tv_nsec - start.tv_nsec) < 0) {
57 temp.tv_sec = end.tv_sec - start.tv_sec - 1;
58 temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
60 temp.tv_sec = end.tv_sec - start.tv_sec;
61 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
63 return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
66 void print_n_spaces(int n)
69 for (x = 0; x < n; x++)
73 /*s is filled with left and right spaces
74 *to make its length atleast n+1
76 int fill_string_with_spaces(char *s, int n)
84 temp = malloc(sizeof(char) * (n+1));
85 for (; len < n; len++)
88 snprintf(temp, n+1, " %s", s);
94 #define MAX_COL_WIDTH 6
95 void print_header(int topology_depth)
102 fill_string_with_spaces(buf, topology_depth * 5 - 1);
105 for (mon = 0; mon < avail_monitors; mon++) {
106 need_len = monitors[mon]->hw_states_num * (MAX_COL_WIDTH + 1)
110 sprintf(buf, "%s", monitors[mon]->name);
111 fill_string_with_spaces(buf, need_len);
116 if (topology_depth > 2)
118 if (topology_depth > 1)
120 if (topology_depth > 0)
123 for (mon = 0; mon < avail_monitors; mon++) {
126 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
129 s = monitors[mon]->hw_states[state];
130 sprintf(buf, "%s", s.name);
131 fill_string_with_spaces(buf, MAX_COL_WIDTH);
140 void print_results(int topology_depth, int cpu)
145 unsigned long long result;
148 /* Be careful CPUs may got resorted for pkg value do not just use cpu */
149 if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
151 if (!cpu_top.core_info[cpu].is_online &&
152 cpu_top.core_info[cpu].pkg == -1)
155 if (topology_depth > 2)
156 printf("%4d|", cpu_top.core_info[cpu].pkg);
157 if (topology_depth > 1)
158 printf("%4d|", cpu_top.core_info[cpu].core);
159 if (topology_depth > 0)
160 printf("%4d|", cpu_top.core_info[cpu].cpu);
162 for (mon = 0; mon < avail_monitors; mon++) {
166 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
170 s = monitors[mon]->hw_states[state];
172 if (s.get_count_percent) {
173 ret = s.get_count_percent(s.id, &percent,
174 cpu_top.core_info[cpu].cpu);
177 else if (percent >= 100.0)
178 printf("%6.1f", percent);
180 printf("%6.2f", percent);
181 } else if (s.get_count) {
182 ret = s.get_count(s.id, &result,
183 cpu_top.core_info[cpu].cpu);
187 printf("%6llu", result);
189 printf(_("Monitor %s, Counter %s has no count "
190 "function. Implementation error\n"),
191 monitors[mon]->name, s.name);
197 * The monitor could still provide useful data, for example
198 * AMD HW counters partly sit in PCI config space.
199 * It's up to the monitor plug-in to check .is_online, this one
200 * is just for additional info.
202 if (!cpu_top.core_info[cpu].is_online &&
203 cpu_top.core_info[cpu].pkg != -1) {
204 printf(_(" *is offline\n"));
211 /* param: string passed by -m param (The list of monitors to show)
213 * Monitors must have been registered already, matching monitors
214 * are picked out and available monitors array is overridden
217 * Monitors get sorted in the same order the user passes them
220 static void parse_monitor_param(char *param)
224 char *tmp = param, *token;
225 struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
228 for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
229 token = strtok(tmp, ",");
232 if (strlen(token) >= MONITOR_NAME_LEN) {
233 printf(_("%s: max monitor name length"
234 " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
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];
247 printf(_("No matching monitor found in %s, "
248 "try -l option\n"), param);
251 /* Override detected/registerd monitors array with requested one */
252 memcpy(monitors, tmp_mons,
253 sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
254 avail_monitors = hits;
257 void list_monitors(void)
263 for (mon = 0; mon < avail_monitors; mon++) {
264 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
266 monitors[mon]->name, monitors[mon]->hw_states_num,
267 monitors[mon]->overflow_s);
269 for (state = 0; state < monitors[mon]->hw_states_num; state++) {
270 s = monitors[mon]->hw_states[state];
272 * ToDo show more state capabilities:
273 * percent, time (granlarity)
275 printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
281 int fork_it(char **argv)
285 unsigned long long timediff;
287 struct timespec start, end;
290 clock_gettime(CLOCK_REALTIME, &start);
292 for (num = 0; num < avail_monitors; num++)
293 monitors[num]->start();
297 execvp(argv[0], argv);
300 if (child_pid == -1) {
305 signal(SIGINT, SIG_IGN);
306 signal(SIGQUIT, SIG_IGN);
307 if (waitpid(child_pid, &status, 0) == -1) {
312 clock_gettime(CLOCK_REALTIME, &end);
313 for (num = 0; num < avail_monitors; num++)
314 monitors[num]->stop();
316 timediff = timespec_diff_us(start, end);
317 if (WIFEXITED(status))
318 printf(_("%s took %.5f seconds and exited with status %d\n"),
319 argv[0], timediff / (1000.0 * 1000),
320 WEXITSTATUS(status));
324 int do_interval_measure(int i)
330 for (cpu = 0; cpu < cpu_count; cpu++)
333 for (num = 0; num < avail_monitors; num++) {
334 dprint("HW C-state residency monitor: %s - States: %d\n",
335 monitors[num]->name, monitors[num]->hw_states_num);
336 monitors[num]->start();
342 for (cpu = 0; cpu < cpu_count; cpu++)
345 for (num = 0; num < avail_monitors; num++)
346 monitors[num]->stop();
352 static void cmdline(int argc, char *argv[])
355 progname = basename(argv[0]);
357 while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
361 print_wrong_arg_exit();
365 /* only allow -i with -m or no option */
366 if (mode && mode != show)
367 print_wrong_arg_exit();
368 interval = atoi(optarg);
372 print_wrong_arg_exit();
374 show_monitors_param = optarg;
380 print_wrong_arg_exit();
387 int cmd_monitor(int argc, char **argv)
390 struct cpuidle_monitor *test_mon;
394 cpu_count = get_cpu_topology(&cpu_top);
396 printf(_("Cannot read number of available processors\n"));
400 if (!cpu_top.core_info[0].is_online)
401 printf("WARNING: at least one cpu is offline\n");
403 /* Default is: monitor all CPUs */
404 if (bitmask_isallclear(cpus_chosen))
405 bitmask_setall(cpus_chosen);
407 dprint("System has up to %d CPU cores\n", cpu_count);
409 for (num = 0; all_monitors[num]; num++) {
410 dprint("Try to register: %s\n", all_monitors[num]->name);
411 test_mon = all_monitors[num]->do_register();
413 if (test_mon->flags.needs_root && !run_as_root) {
414 fprintf(stderr, _("Available monitor %s needs "
415 "root access\n"), test_mon->name);
418 monitors[avail_monitors] = test_mon;
419 dprint("%s registered\n", all_monitors[num]->name);
424 if (avail_monitors == 0) {
425 printf(_("No HW Cstate monitors found\n"));
435 parse_monitor_param(show_monitors_param);
437 dprint("Packages: %d - Cores: %d - CPUs: %d\n",
438 cpu_top.pkgs, cpu_top.cores, cpu_count);
441 * if any params left, it must be a command to fork
444 fork_it(argv + optind);
446 do_interval_measure(interval);
448 /* ToDo: Topology parsing needs fixing first to do
449 this more generically */
450 if (cpu_top.pkgs > 1)
455 for (cpu = 0; cpu < cpu_count; cpu++) {
456 if (cpu_top.pkgs > 1)
457 print_results(3, cpu);
459 print_results(1, cpu);
462 for (num = 0; num < avail_monitors; num++) {
463 if (monitors[num]->unregister)
464 monitors[num]->unregister();
466 cpu_topology_release(cpu_top);