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.
17 #include <sys/types.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
};
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"));
52 long long timespec_diff_us(struct timespec start
, struct timespec end
)
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
;
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
)
68 for (x
= 0; x
< n
; x
++)
72 /* size of s must be at least n + 1 */
73 int fill_string_with_spaces(char *s
, int n
)
78 for (; len
< n
; len
++)
84 void print_header(int topology_depth
)
87 int state
, need_len
, pr_mon_len
;
90 int percent_width
= 4;
92 fill_string_with_spaces(buf
, topology_depth
* 5 - 1);
95 for (mon
= 0; mon
< avail_monitors
; mon
++) {
97 need_len
= monitors
[mon
]->hw_states_num
* (percent_width
+ 3)
103 sprintf(buf
, "%s", monitors
[mon
]->name
);
104 fill_string_with_spaces(buf
, need_len
);
109 if (topology_depth
> 2)
111 if (topology_depth
> 1)
113 if (topology_depth
> 0)
116 for (mon
= 0; mon
< avail_monitors
; mon
++) {
121 for (state
= 0; state
< monitors
[mon
]->hw_states_num
; state
++) {
124 s
= monitors
[mon
]->hw_states
[state
];
125 sprintf(buf
, "%s", s
.name
);
126 fill_string_with_spaces(buf
, percent_width
);
135 void print_results(int topology_depth
, int cpu
)
140 unsigned long long result
;
143 /* Be careful CPUs may got resorted for pkg value do not just use cpu */
144 if (!bitmask_isbitset(cpus_chosen
, cpu_top
.core_info
[cpu
].cpu
))
147 if (topology_depth
> 2)
148 printf("%4d|", cpu_top
.core_info
[cpu
].pkg
);
149 if (topology_depth
> 1)
150 printf("%4d|", cpu_top
.core_info
[cpu
].core
);
151 if (topology_depth
> 0)
152 printf("%4d|", cpu_top
.core_info
[cpu
].cpu
);
154 for (mon
= 0; mon
< avail_monitors
; mon
++) {
158 for (state
= 0; state
< monitors
[mon
]->hw_states_num
; state
++) {
162 s
= monitors
[mon
]->hw_states
[state
];
164 if (s
.get_count_percent
) {
165 ret
= s
.get_count_percent(s
.id
, &percent
,
166 cpu_top
.core_info
[cpu
].cpu
);
169 else if (percent
>= 100.0)
170 printf("%6.1f", percent
);
172 printf("%6.2f", percent
);
173 } else if (s
.get_count
) {
174 ret
= s
.get_count(s
.id
, &result
,
175 cpu_top
.core_info
[cpu
].cpu
);
179 printf("%6llu", result
);
181 printf(_("Monitor %s, Counter %s has no count "
182 "function. Implementation error\n"),
183 monitors
[mon
]->name
, s
.name
);
189 * The monitor could still provide useful data, for example
190 * AMD HW counters partly sit in PCI config space.
191 * It's up to the monitor plug-in to check .is_online, this one
192 * is just for additional info.
194 if (!cpu_top
.core_info
[cpu
].is_online
) {
195 printf(_(" *is offline\n"));
202 /* param: string passed by -m param (The list of monitors to show)
204 * Monitors must have been registered already, matching monitors
205 * are picked out and available monitors array is overridden
208 * Monitors get sorted in the same order the user passes them
211 static void parse_monitor_param(char *param
)
215 char *tmp
= param
, *token
;
216 struct cpuidle_monitor
*tmp_mons
[MONITORS_MAX
];
219 for (mon
= 0; mon
< MONITORS_MAX
; mon
++, tmp
= NULL
) {
220 token
= strtok(tmp
, ",");
223 if (strlen(token
) >= MONITOR_NAME_LEN
) {
224 printf(_("%s: max monitor name length"
225 " (%d) exceeded\n"), token
, MONITOR_NAME_LEN
);
229 for (num
= 0; num
< avail_monitors
; num
++) {
230 if (!strcmp(monitors
[num
]->name
, token
)) {
231 dprint("Found requested monitor: %s\n", token
);
232 tmp_mons
[hits
] = monitors
[num
];
238 printf(_("No matching monitor found in %s, "
239 "try -l option\n"), param
);
242 /* Override detected/registerd monitors array with requested one */
243 memcpy(monitors
, tmp_mons
,
244 sizeof(struct cpuidle_monitor
*) * MONITORS_MAX
);
245 avail_monitors
= hits
;
248 void list_monitors(void)
254 for (mon
= 0; mon
< avail_monitors
; mon
++) {
255 printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
257 monitors
[mon
]->name
, monitors
[mon
]->hw_states_num
,
258 monitors
[mon
]->overflow_s
);
260 for (state
= 0; state
< monitors
[mon
]->hw_states_num
; state
++) {
261 s
= monitors
[mon
]->hw_states
[state
];
263 * ToDo show more state capabilities:
264 * percent, time (granlarity)
266 printf("%s\t[%c] -> %s\n", s
.name
, range_abbr
[s
.range
],
272 int fork_it(char **argv
)
276 unsigned long long timediff
;
278 struct timespec start
, end
;
281 clock_gettime(CLOCK_REALTIME
, &start
);
283 for (num
= 0; num
< avail_monitors
; num
++)
284 monitors
[num
]->start();
288 execvp(argv
[0], argv
);
291 if (child_pid
== -1) {
296 signal(SIGINT
, SIG_IGN
);
297 signal(SIGQUIT
, SIG_IGN
);
298 if (waitpid(child_pid
, &status
, 0) == -1) {
303 clock_gettime(CLOCK_REALTIME
, &end
);
304 for (num
= 0; num
< avail_monitors
; num
++)
305 monitors
[num
]->stop();
307 timediff
= timespec_diff_us(start
, end
);
308 if (WIFEXITED(status
))
309 printf(_("%s took %.5f seconds and exited with status %d\n"),
310 argv
[0], timediff
/ (1000.0 * 1000),
311 WEXITSTATUS(status
));
315 int do_interval_measure(int i
)
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();
325 for (num
= 0; num
< avail_monitors
; num
++)
326 monitors
[num
]->stop();
331 static void cmdline(int argc
, char *argv
[])
334 progname
= basename(argv
[0]);
336 while ((opt
= getopt(argc
, argv
, "+li:m:")) != -1) {
340 print_wrong_arg_exit();
344 /* only allow -i with -m or no option */
345 if (mode
&& mode
!= show
)
346 print_wrong_arg_exit();
347 interval
= atoi(optarg
);
351 print_wrong_arg_exit();
353 show_monitors_param
= optarg
;
356 print_wrong_arg_exit();
363 int cmd_monitor(int argc
, char **argv
)
366 struct cpuidle_monitor
*test_mon
;
370 cpu_count
= get_cpu_topology(&cpu_top
);
372 printf(_("Cannot read number of available processors\n"));
376 /* Default is: monitor all CPUs */
377 if (bitmask_isallclear(cpus_chosen
))
378 bitmask_setall(cpus_chosen
);
380 dprint("System has up to %d CPU cores\n", cpu_count
);
382 for (num
= 0; all_monitors
[num
]; num
++) {
383 dprint("Try to register: %s\n", all_monitors
[num
]->name
);
384 test_mon
= all_monitors
[num
]->do_register();
386 if (test_mon
->needs_root
&& !run_as_root
) {
387 fprintf(stderr
, _("Available monitor %s needs "
388 "root access\n"), test_mon
->name
);
391 monitors
[avail_monitors
] = test_mon
;
392 dprint("%s registered\n", all_monitors
[num
]->name
);
397 if (avail_monitors
== 0) {
398 printf(_("No HW Cstate monitors found\n"));
408 parse_monitor_param(show_monitors_param
);
410 dprint("Packages: %d - Cores: %d - CPUs: %d\n",
411 cpu_top
.pkgs
, cpu_top
.cores
, cpu_count
);
414 * if any params left, it must be a command to fork
417 fork_it(argv
+ optind
);
419 do_interval_measure(interval
);
421 /* ToDo: Topology parsing needs fixing first to do
422 this more generically */
423 if (cpu_top
.pkgs
> 1)
428 for (cpu
= 0; cpu
< cpu_count
; cpu
++) {
429 if (cpu_top
.pkgs
> 1)
430 print_results(3, cpu
);
432 print_results(1, cpu
);
435 for (num
= 0; num
< avail_monitors
; num
++)
436 monitors
[num
]->unregister();
438 cpu_topology_release(cpu_top
);