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"
30 static struct cpuidle_monitor
*monitors
[MONITORS_MAX
];
31 static unsigned int avail_monitors
;
33 static char *progname
;
35 enum operation_mode_e
{ list
= 1, show
, show_all
};
37 static int interval
= 1;
38 static char *show_monitors_param
;
39 static struct cpupower_topology cpu_top
;
40 static unsigned int wake_cpus
;
42 /* ToDo: Document this in the manpage */
43 static char range_abbr
[RANGE_MAX
] = { 'T', 'C', 'P', 'M', };
45 static void print_wrong_arg_exit(void)
47 printf(_("invalid or unknown argument\n"));
51 long long timespec_diff_us(struct timespec start
, struct timespec end
)
54 if ((end
.tv_nsec
- start
.tv_nsec
) < 0) {
55 temp
.tv_sec
= end
.tv_sec
- start
.tv_sec
- 1;
56 temp
.tv_nsec
= 1000000000 + end
.tv_nsec
- start
.tv_nsec
;
58 temp
.tv_sec
= end
.tv_sec
- start
.tv_sec
;
59 temp
.tv_nsec
= end
.tv_nsec
- start
.tv_nsec
;
61 return (temp
.tv_sec
* 1000000) + (temp
.tv_nsec
/ 1000);
64 void print_n_spaces(int n
)
67 for (x
= 0; x
< n
; x
++)
71 /*s is filled with left and right spaces
72 *to make its length atleast n+1
74 int fill_string_with_spaces(char *s
, int n
)
82 temp
= malloc(sizeof(char) * (n
+1));
83 for (; len
< n
; len
++)
86 snprintf(temp
, n
+1, " %s", s
);
92 #define MAX_COL_WIDTH 6
93 void print_header(int topology_depth
)
100 fill_string_with_spaces(buf
, topology_depth
* 5 - 1);
103 for (mon
= 0; mon
< avail_monitors
; mon
++) {
104 need_len
= monitors
[mon
]->hw_states_num
* (MAX_COL_WIDTH
+ 1)
108 sprintf(buf
, "%s", monitors
[mon
]->name
);
109 fill_string_with_spaces(buf
, need_len
);
114 if (topology_depth
> 2)
116 if (topology_depth
> 1)
118 if (topology_depth
> 0)
121 for (mon
= 0; mon
< avail_monitors
; mon
++) {
124 for (state
= 0; state
< monitors
[mon
]->hw_states_num
; state
++) {
127 s
= monitors
[mon
]->hw_states
[state
];
128 sprintf(buf
, "%s", s
.name
);
129 fill_string_with_spaces(buf
, MAX_COL_WIDTH
);
138 void print_results(int topology_depth
, int cpu
)
143 unsigned long long result
;
146 /* Be careful CPUs may got resorted for pkg value do not just use cpu */
147 if (!bitmask_isbitset(cpus_chosen
, cpu_top
.core_info
[cpu
].cpu
))
149 if (!cpu_top
.core_info
[cpu
].is_online
&&
150 cpu_top
.core_info
[cpu
].pkg
== -1)
153 if (topology_depth
> 2)
154 printf("%4d|", cpu_top
.core_info
[cpu
].pkg
);
155 if (topology_depth
> 1)
156 printf("%4d|", cpu_top
.core_info
[cpu
].core
);
157 if (topology_depth
> 0)
158 printf("%4d|", cpu_top
.core_info
[cpu
].cpu
);
160 for (mon
= 0; mon
< avail_monitors
; mon
++) {
164 for (state
= 0; state
< monitors
[mon
]->hw_states_num
; state
++) {
168 s
= monitors
[mon
]->hw_states
[state
];
170 if (s
.get_count_percent
) {
171 ret
= s
.get_count_percent(s
.id
, &percent
,
172 cpu_top
.core_info
[cpu
].cpu
);
175 else if (percent
>= 100.0)
176 printf("%6.1f", percent
);
178 printf("%6.2f", percent
);
179 } else if (s
.get_count
) {
180 ret
= s
.get_count(s
.id
, &result
,
181 cpu_top
.core_info
[cpu
].cpu
);
185 printf("%6llu", result
);
187 printf(_("Monitor %s, Counter %s has no count "
188 "function. Implementation error\n"),
189 monitors
[mon
]->name
, s
.name
);
195 * The monitor could still provide useful data, for example
196 * AMD HW counters partly sit in PCI config space.
197 * It's up to the monitor plug-in to check .is_online, this one
198 * is just for additional info.
200 if (!cpu_top
.core_info
[cpu
].is_online
&&
201 cpu_top
.core_info
[cpu
].pkg
!= -1) {
202 printf(_(" *is offline\n"));
209 /* param: string passed by -m param (The list of monitors to show)
211 * Monitors must have been registered already, matching monitors
212 * are picked out and available monitors array is overridden
215 * Monitors get sorted in the same order the user passes them
218 static void parse_monitor_param(char *param
)
222 char *tmp
= param
, *token
;
223 struct cpuidle_monitor
*tmp_mons
[MONITORS_MAX
];
226 for (mon
= 0; mon
< MONITORS_MAX
; mon
++, tmp
= NULL
) {
227 token
= strtok(tmp
, ",");
230 if (strlen(token
) >= MONITOR_NAME_LEN
) {
231 printf(_("%s: max monitor name length"
232 " (%d) exceeded\n"), token
, MONITOR_NAME_LEN
);
236 for (num
= 0; num
< avail_monitors
; num
++) {
237 if (!strcmp(monitors
[num
]->name
, token
)) {
238 dprint("Found requested monitor: %s\n", token
);
239 tmp_mons
[hits
] = monitors
[num
];
245 printf(_("No matching monitor found in %s, "
246 "try -l option\n"), param
);
249 /* Override detected/registerd monitors array with requested one */
250 memcpy(monitors
, tmp_mons
,
251 sizeof(struct cpuidle_monitor
*) * MONITORS_MAX
);
252 avail_monitors
= hits
;
255 void list_monitors(void)
261 for (mon
= 0; mon
< avail_monitors
; mon
++) {
262 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
264 monitors
[mon
]->name
, monitors
[mon
]->hw_states_num
,
265 monitors
[mon
]->overflow_s
);
267 for (state
= 0; state
< monitors
[mon
]->hw_states_num
; state
++) {
268 s
= monitors
[mon
]->hw_states
[state
];
270 * ToDo show more state capabilities:
271 * percent, time (granlarity)
273 printf("%s\t[%c] -> %s\n", s
.name
, range_abbr
[s
.range
],
279 int fork_it(char **argv
)
283 unsigned long long timediff
;
285 struct timespec start
, end
;
288 clock_gettime(CLOCK_REALTIME
, &start
);
290 for (num
= 0; num
< avail_monitors
; num
++)
291 monitors
[num
]->start();
295 execvp(argv
[0], argv
);
298 if (child_pid
== -1) {
303 signal(SIGINT
, SIG_IGN
);
304 signal(SIGQUIT
, SIG_IGN
);
305 if (waitpid(child_pid
, &status
, 0) == -1) {
310 clock_gettime(CLOCK_REALTIME
, &end
);
311 for (num
= 0; num
< avail_monitors
; num
++)
312 monitors
[num
]->stop();
314 timediff
= timespec_diff_us(start
, end
);
315 if (WIFEXITED(status
))
316 printf(_("%s took %.5f seconds and exited with status %d\n"),
317 argv
[0], timediff
/ (1000.0 * 1000),
318 WEXITSTATUS(status
));
322 int do_interval_measure(int i
)
328 for (cpu
= 0; cpu
< cpu_count
; cpu
++)
331 for (num
= 0; num
< avail_monitors
; num
++) {
332 dprint("HW C-state residency monitor: %s - States: %d\n",
333 monitors
[num
]->name
, monitors
[num
]->hw_states_num
);
334 monitors
[num
]->start();
340 for (cpu
= 0; cpu
< cpu_count
; cpu
++)
343 for (num
= 0; num
< avail_monitors
; num
++)
344 monitors
[num
]->stop();
350 static void cmdline(int argc
, char *argv
[])
353 progname
= basename(argv
[0]);
355 while ((opt
= getopt(argc
, argv
, "+lci:m:")) != -1) {
359 print_wrong_arg_exit();
363 /* only allow -i with -m or no option */
364 if (mode
&& mode
!= show
)
365 print_wrong_arg_exit();
366 interval
= atoi(optarg
);
370 print_wrong_arg_exit();
372 show_monitors_param
= optarg
;
378 print_wrong_arg_exit();
385 int cmd_monitor(int argc
, char **argv
)
388 struct cpuidle_monitor
*test_mon
;
392 cpu_count
= get_cpu_topology(&cpu_top
);
394 printf(_("Cannot read number of available processors\n"));
398 if (!cpu_top
.core_info
[0].is_online
)
399 printf("WARNING: at least one cpu is offline\n");
401 /* Default is: monitor all CPUs */
402 if (bitmask_isallclear(cpus_chosen
))
403 bitmask_setall(cpus_chosen
);
405 dprint("System has up to %d CPU cores\n", cpu_count
);
407 for (num
= 0; all_monitors
[num
]; num
++) {
408 dprint("Try to register: %s\n", all_monitors
[num
]->name
);
409 test_mon
= all_monitors
[num
]->do_register();
411 if (test_mon
->flags
.needs_root
&& !run_as_root
) {
412 fprintf(stderr
, _("Available monitor %s needs "
413 "root access\n"), test_mon
->name
);
416 monitors
[avail_monitors
] = test_mon
;
417 dprint("%s registered\n", all_monitors
[num
]->name
);
422 if (avail_monitors
== 0) {
423 printf(_("No HW Cstate monitors found\n"));
433 parse_monitor_param(show_monitors_param
);
435 dprint("Packages: %d - Cores: %d - CPUs: %d\n",
436 cpu_top
.pkgs
, cpu_top
.cores
, cpu_count
);
439 * if any params left, it must be a command to fork
442 fork_it(argv
+ optind
);
444 do_interval_measure(interval
);
446 /* ToDo: Topology parsing needs fixing first to do
447 this more generically */
448 if (cpu_top
.pkgs
> 1)
453 for (cpu
= 0; cpu
< cpu_count
; cpu
++) {
454 if (cpu_top
.pkgs
> 1)
455 print_results(3, cpu
);
457 print_results(1, cpu
);
460 for (num
= 0; num
< avail_monitors
; num
++)
461 monitors
[num
]->unregister();
463 cpu_topology_release(cpu_top
);