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