• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 int cpu_count;
33 
34 static struct cpuidle_monitor *monitors[MONITORS_MAX];
35 static unsigned int avail_monitors;
36 
37 static char *progname;
38 
39 enum operation_mode_e { list = 1, show, show_all };
40 static int mode;
41 static int interval = 1;
42 static char *show_monitors_param;
43 static struct cpupower_topology cpu_top;
44 static unsigned int wake_cpus;
45 
46 /* ToDo: Document this in the manpage */
47 static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };
48 
print_wrong_arg_exit(void)49 static void print_wrong_arg_exit(void)
50 {
51 	printf(_("invalid or unknown argument\n"));
52 	exit(EXIT_FAILURE);
53 }
54 
timespec_diff_us(struct timespec start,struct timespec end)55 long long timespec_diff_us(struct timespec start, struct timespec end)
56 {
57 	struct timespec temp;
58 	if ((end.tv_nsec - start.tv_nsec) < 0) {
59 		temp.tv_sec = end.tv_sec - start.tv_sec - 1;
60 		temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
61 	} else {
62 		temp.tv_sec = end.tv_sec - start.tv_sec;
63 		temp.tv_nsec = end.tv_nsec - start.tv_nsec;
64 	}
65 	return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
66 }
67 
print_n_spaces(int n)68 void print_n_spaces(int n)
69 {
70 	int x;
71 	for (x = 0; x < n; x++)
72 		printf(" ");
73 }
74 
75 /* size of s must be at least n + 1 */
fill_string_with_spaces(char * s,int n)76 int fill_string_with_spaces(char *s, int n)
77 {
78 	int len = strlen(s);
79 	if (len > n)
80 		return -1;
81 	for (; len < n; len++)
82 		s[len] = ' ';
83 	s[len] = '\0';
84 	return 0;
85 }
86 
print_header(int topology_depth)87 void print_header(int topology_depth)
88 {
89 	int unsigned mon;
90 	int state, need_len;
91 	cstate_t s;
92 	char buf[128] = "";
93 	int percent_width = 4;
94 
95 	fill_string_with_spaces(buf, topology_depth * 5 - 1);
96 	printf("%s|", buf);
97 
98 	for (mon = 0; mon < avail_monitors; mon++) {
99 		need_len = monitors[mon]->hw_states_num * (percent_width + 3)
100 			- 1;
101 		if (mon != 0) {
102 			printf("|| ");
103 			need_len--;
104 		}
105 		sprintf(buf, "%s", monitors[mon]->name);
106 		fill_string_with_spaces(buf, need_len);
107 		printf("%s", buf);
108 	}
109 	printf("\n");
110 
111 	if (topology_depth > 2)
112 		printf("PKG |");
113 	if (topology_depth > 1)
114 		printf("CORE|");
115 	if (topology_depth > 0)
116 		printf("CPU |");
117 
118 	for (mon = 0; mon < avail_monitors; mon++) {
119 		if (mon != 0)
120 			printf("|| ");
121 		else
122 			printf(" ");
123 		for (state = 0; state < monitors[mon]->hw_states_num; state++) {
124 			if (state != 0)
125 				printf(" | ");
126 			s = monitors[mon]->hw_states[state];
127 			sprintf(buf, "%s", s.name);
128 			fill_string_with_spaces(buf, percent_width);
129 			printf("%s", buf);
130 		}
131 		printf(" ");
132 	}
133 	printf("\n");
134 }
135 
136 
print_results(int topology_depth,int cpu)137 void print_results(int topology_depth, int cpu)
138 {
139 	unsigned int mon;
140 	int state, ret;
141 	double percent;
142 	unsigned long long result;
143 	cstate_t s;
144 
145 	/* Be careful CPUs may got resorted for pkg value do not just use cpu */
146 	if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
147 		return;
148 	if (!cpu_top.core_info[cpu].is_online &&
149 	    cpu_top.core_info[cpu].pkg == -1)
150 		return;
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 			} else if (s.get_count) {
179 				ret = s.get_count(s.id, &result,
180 						  cpu_top.core_info[cpu].cpu);
181 				if (ret)
182 					printf("******");
183 				else
184 					printf("%6llu", result);
185 			} else {
186 				printf(_("Monitor %s, Counter %s has no count "
187 					 "function. Implementation error\n"),
188 				       monitors[mon]->name, s.name);
189 				exit(EXIT_FAILURE);
190 			}
191 		}
192 	}
193 	/*
194 	 * The monitor could still provide useful data, for example
195 	 * AMD HW counters partly sit in PCI config space.
196 	 * It's up to the monitor plug-in to check .is_online, this one
197 	 * is just for additional info.
198 	 */
199 	if (!cpu_top.core_info[cpu].is_online &&
200 	    cpu_top.core_info[cpu].pkg != -1) {
201 		printf(_(" *is offline\n"));
202 		return;
203 	} else
204 		printf("\n");
205 }
206 
207 
208 /* param: string passed by -m param (The list of monitors to show)
209  *
210  * Monitors must have been registered already, matching monitors
211  * are picked out and available monitors array is overridden
212  * with matching ones
213  *
214  * Monitors get sorted in the same order the user passes them
215 */
216 
parse_monitor_param(char * param)217 static void parse_monitor_param(char *param)
218 {
219 	unsigned int num;
220 	int mon, hits = 0;
221 	char *tmp = param, *token;
222 	struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
223 
224 
225 	for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
226 		token = strtok(tmp, ",");
227 		if (token == NULL)
228 			break;
229 		if (strlen(token) >= MONITOR_NAME_LEN) {
230 			printf(_("%s: max monitor name length"
231 				 " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
232 			continue;
233 		}
234 
235 		for (num = 0; num < avail_monitors; num++) {
236 			if (!strcmp(monitors[num]->name, token)) {
237 				dprint("Found requested monitor: %s\n", token);
238 				tmp_mons[hits] = monitors[num];
239 				hits++;
240 			}
241 		}
242 	}
243 	if (hits == 0) {
244 		printf(_("No matching monitor found in %s, "
245 			 "try -l option\n"), param);
246 		exit(EXIT_FAILURE);
247 	}
248 	/* Override detected/registerd monitors array with requested one */
249 	memcpy(monitors, tmp_mons,
250 		sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
251 	avail_monitors = hits;
252 }
253 
list_monitors(void)254 void list_monitors(void)
255 {
256 	unsigned int mon;
257 	int state;
258 	cstate_t s;
259 
260 	for (mon = 0; mon < avail_monitors; mon++) {
261 		printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
262 			 "s\n"),
263 			monitors[mon]->name, monitors[mon]->hw_states_num,
264 			monitors[mon]->overflow_s);
265 
266 		for (state = 0; state < monitors[mon]->hw_states_num; state++) {
267 			s = monitors[mon]->hw_states[state];
268 			/*
269 			 * ToDo show more state capabilities:
270 			 * percent, time (granlarity)
271 			 */
272 			printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
273 			       gettext(s.desc));
274 		}
275 	}
276 }
277 
fork_it(char ** argv)278 int fork_it(char **argv)
279 {
280 	int status;
281 	unsigned int num;
282 	unsigned long long timediff;
283 	pid_t child_pid;
284 	struct timespec start, end;
285 
286 	child_pid = fork();
287 	clock_gettime(CLOCK_REALTIME, &start);
288 
289 	for (num = 0; num < avail_monitors; num++)
290 		monitors[num]->start();
291 
292 	if (!child_pid) {
293 		/* child */
294 		execvp(argv[0], argv);
295 	} else {
296 		/* parent */
297 		if (child_pid == -1) {
298 			perror("fork");
299 			exit(1);
300 		}
301 
302 		signal(SIGINT, SIG_IGN);
303 		signal(SIGQUIT, SIG_IGN);
304 		if (waitpid(child_pid, &status, 0) == -1) {
305 			perror("wait");
306 			exit(1);
307 		}
308 	}
309 	clock_gettime(CLOCK_REALTIME, &end);
310 	for (num = 0; num < avail_monitors; num++)
311 		monitors[num]->stop();
312 
313 	timediff = timespec_diff_us(start, end);
314 	if (WIFEXITED(status))
315 		printf(_("%s took %.5f seconds and exited with status %d\n"),
316 			argv[0], timediff / (1000.0 * 1000),
317 			WEXITSTATUS(status));
318 	return 0;
319 }
320 
do_interval_measure(int i)321 int do_interval_measure(int i)
322 {
323 	unsigned int num;
324 	int cpu;
325 
326 	if (wake_cpus)
327 		for (cpu = 0; cpu < cpu_count; cpu++)
328 			bind_cpu(cpu);
329 
330 	for (num = 0; num < avail_monitors; num++) {
331 		dprint("HW C-state residency monitor: %s - States: %d\n",
332 		       monitors[num]->name, monitors[num]->hw_states_num);
333 		monitors[num]->start();
334 	}
335 
336 	sleep(i);
337 
338 	if (wake_cpus)
339 		for (cpu = 0; cpu < cpu_count; cpu++)
340 			bind_cpu(cpu);
341 
342 	for (num = 0; num < avail_monitors; num++)
343 		monitors[num]->stop();
344 
345 
346 	return 0;
347 }
348 
cmdline(int argc,char * argv[])349 static void cmdline(int argc, char *argv[])
350 {
351 	int opt;
352 	progname = basename(argv[0]);
353 
354 	while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
355 		switch (opt) {
356 		case 'l':
357 			if (mode)
358 				print_wrong_arg_exit();
359 			mode = list;
360 			break;
361 		case 'i':
362 			/* only allow -i with -m or no option */
363 			if (mode && mode != show)
364 				print_wrong_arg_exit();
365 			interval = atoi(optarg);
366 			break;
367 		case 'm':
368 			if (mode)
369 				print_wrong_arg_exit();
370 			mode = show;
371 			show_monitors_param = optarg;
372 			break;
373 		case 'c':
374 			wake_cpus = 1;
375 			break;
376 		default:
377 			print_wrong_arg_exit();
378 		}
379 	}
380 	if (!mode)
381 		mode = show_all;
382 }
383 
cmd_monitor(int argc,char ** argv)384 int cmd_monitor(int argc, char **argv)
385 {
386 	unsigned int num;
387 	struct cpuidle_monitor *test_mon;
388 	int cpu;
389 
390 	cmdline(argc, argv);
391 	cpu_count = get_cpu_topology(&cpu_top);
392 	if (cpu_count < 0) {
393 		printf(_("Cannot read number of available processors\n"));
394 		return EXIT_FAILURE;
395 	}
396 
397 	if (!cpu_top.core_info[0].is_online)
398 		printf("WARNING: at least one cpu is offline\n");
399 
400 	/* Default is: monitor all CPUs */
401 	if (bitmask_isallclear(cpus_chosen))
402 		bitmask_setall(cpus_chosen);
403 
404 	dprint("System has up to %d CPU cores\n", cpu_count);
405 
406 	for (num = 0; all_monitors[num]; num++) {
407 		dprint("Try to register: %s\n", all_monitors[num]->name);
408 		test_mon = all_monitors[num]->do_register();
409 		if (test_mon) {
410 			if (test_mon->needs_root && !run_as_root) {
411 				fprintf(stderr, _("Available monitor %s needs "
412 					  "root access\n"), test_mon->name);
413 				continue;
414 			}
415 			monitors[avail_monitors] = test_mon;
416 			dprint("%s registered\n", all_monitors[num]->name);
417 			avail_monitors++;
418 		}
419 	}
420 
421 	if (avail_monitors == 0) {
422 		printf(_("No HW Cstate monitors found\n"));
423 		return 1;
424 	}
425 
426 	if (mode == list) {
427 		list_monitors();
428 		exit(EXIT_SUCCESS);
429 	}
430 
431 	if (mode == show)
432 		parse_monitor_param(show_monitors_param);
433 
434 	dprint("Packages: %d - Cores: %d - CPUs: %d\n",
435 	       cpu_top.pkgs, cpu_top.cores, cpu_count);
436 
437 	/*
438 	 * if any params left, it must be a command to fork
439 	 */
440 	if (argc - optind)
441 		fork_it(argv + optind);
442 	else
443 		do_interval_measure(interval);
444 
445 	/* ToDo: Topology parsing needs fixing first to do
446 	   this more generically */
447 	if (cpu_top.pkgs > 1)
448 		print_header(3);
449 	else
450 		print_header(1);
451 
452 	for (cpu = 0; cpu < cpu_count; cpu++) {
453 		if (cpu_top.pkgs > 1)
454 			print_results(3, cpu);
455 		else
456 			print_results(1, cpu);
457 	}
458 
459 	for (num = 0; num < avail_monitors; num++)
460 		monitors[num]->unregister();
461 
462 	cpu_topology_release(cpu_top);
463 	return 0;
464 }
465