cpupower-monitor.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *  (C) 2010,2011       Thomas Renninger <trenn@suse.de>, Novell Inc.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 *  Output format inspired by Len Brown's <lenb@kernel.org> turbostat tool.
 *
 */


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <libgen.h>

#include "idle_monitor/cpupower-monitor.h"
#include "idle_monitor/idle_monitors.h"
#include "helpers/helpers.h"

/* Define pointers to all monitors.  */
#define DEF(x) & x ## _monitor ,
27
struct cpuidle_monitor *all_monitors[] = {
28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include "idle_monitors.def"
0
};

static struct cpuidle_monitor *monitors[MONITORS_MAX];
static unsigned int avail_monitors;

static char *progname;

enum operation_mode_e { list = 1, show, show_all };
static int mode;
static int interval = 1;
static char *show_monitors_param;
static struct cpupower_topology cpu_top;
42
static unsigned int wake_cpus;
43 44 45 46

/* ToDo: Document this in the manpage */
static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };

47 48 49 50 51 52
static void print_wrong_arg_exit(void)
{
	printf(_("invalid or unknown argument\n"));
	exit(EXIT_FAILURE);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
long long timespec_diff_us(struct timespec start, struct timespec end)
{
	struct timespec temp;
	if ((end.tv_nsec - start.tv_nsec) < 0) {
		temp.tv_sec = end.tv_sec - start.tv_sec - 1;
		temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
	} else {
		temp.tv_sec = end.tv_sec - start.tv_sec;
		temp.tv_nsec = end.tv_nsec - start.tv_nsec;
	}
	return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
}

void print_n_spaces(int n)
{
	int x;
	for (x = 0; x < n; x++)
		printf(" ");
71
}
72

73 74 75
/*s is filled with left and right spaces
 *to make its length atleast n+1
 */
76 77
int fill_string_with_spaces(char *s, int n)
{
78
	char *temp;
79
	int len = strlen(s);
80 81

	if (len >= n)
82
		return -1;
83 84

	temp = malloc(sizeof(char) * (n+1));
85 86 87
	for (; len < n; len++)
		s[len] = ' ';
	s[len] = '\0';
88 89 90
	snprintf(temp, n+1, " %s", s);
	strcpy(s, temp);
	free(temp);
91
	return 0;
92
}
93

94
#define MAX_COL_WIDTH 6
95 96 97
void print_header(int topology_depth)
{
	int unsigned mon;
P
Palmer Cox 已提交
98
	int state, need_len;
99 100 101 102 103 104 105
	cstate_t s;
	char buf[128] = "";

	fill_string_with_spaces(buf, topology_depth * 5 - 1);
	printf("%s|", buf);

	for (mon = 0; mon < avail_monitors; mon++) {
106
		need_len = monitors[mon]->hw_states_num * (MAX_COL_WIDTH + 1)
107
			- 1;
108 109
		if (mon != 0)
			printf("||");
110 111 112 113 114 115 116
		sprintf(buf, "%s", monitors[mon]->name);
		fill_string_with_spaces(buf, need_len);
		printf("%s", buf);
	}
	printf("\n");

	if (topology_depth > 2)
117
		printf(" PKG|");
118 119 120
	if (topology_depth > 1)
		printf("CORE|");
	if (topology_depth > 0)
121
		printf(" CPU|");
122 123 124

	for (mon = 0; mon < avail_monitors; mon++) {
		if (mon != 0)
125
			printf("||");
126 127
		for (state = 0; state < monitors[mon]->hw_states_num; state++) {
			if (state != 0)
128
				printf("|");
129 130
			s = monitors[mon]->hw_states[state];
			sprintf(buf, "%s", s.name);
131
			fill_string_with_spaces(buf, MAX_COL_WIDTH);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
			printf("%s", buf);
		}
		printf(" ");
	}
	printf("\n");
}


void print_results(int topology_depth, int cpu)
{
	unsigned int mon;
	int state, ret;
	double percent;
	unsigned long long result;
	cstate_t s;

148 149 150
	/* Be careful CPUs may got resorted for pkg value do not just use cpu */
	if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
		return;
151 152 153
	if (!cpu_top.core_info[cpu].is_online &&
	    cpu_top.core_info[cpu].pkg == -1)
		return;
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	if (topology_depth > 2)
		printf("%4d|", cpu_top.core_info[cpu].pkg);
	if (topology_depth > 1)
		printf("%4d|", cpu_top.core_info[cpu].core);
	if (topology_depth > 0)
		printf("%4d|", cpu_top.core_info[cpu].cpu);

	for (mon = 0; mon < avail_monitors; mon++) {
		if (mon != 0)
			printf("||");

		for (state = 0; state < monitors[mon]->hw_states_num; state++) {
			if (state != 0)
				printf("|");

			s = monitors[mon]->hw_states[state];

			if (s.get_count_percent) {
				ret = s.get_count_percent(s.id, &percent,
						  cpu_top.core_info[cpu].cpu);
175
				if (ret)
176
					printf("******");
177
				else if (percent >= 100.0)
178 179 180
					printf("%6.1f", percent);
				else
					printf("%6.2f", percent);
181
			} else if (s.get_count) {
182 183
				ret = s.get_count(s.id, &result,
						  cpu_top.core_info[cpu].cpu);
184
				if (ret)
185
					printf("******");
186
				else
187
					printf("%6llu", result);
188
			} else {
189 190 191
				printf(_("Monitor %s, Counter %s has no count "
					 "function. Implementation error\n"),
				       monitors[mon]->name, s.name);
192
				exit(EXIT_FAILURE);
193 194 195
			}
		}
	}
196 197 198 199 200 201
	/*
	 * The monitor could still provide useful data, for example
	 * AMD HW counters partly sit in PCI config space.
	 * It's up to the monitor plug-in to check .is_online, this one
	 * is just for additional info.
	 */
202 203
	if (!cpu_top.core_info[cpu].is_online &&
	    cpu_top.core_info[cpu].pkg != -1) {
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		printf(_(" *is offline\n"));
		return;
	} else
		printf("\n");
}


/* param: string passed by -m param (The list of monitors to show)
 *
 * Monitors must have been registered already, matching monitors
 * are picked out and available monitors array is overridden
 * with matching ones
 *
 * Monitors get sorted in the same order the user passes them
*/

220
static void parse_monitor_param(char *param)
221 222 223 224 225 226 227
{
	unsigned int num;
	int mon, hits = 0;
	char *tmp = param, *token;
	struct cpuidle_monitor *tmp_mons[MONITORS_MAX];


228
	for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		token = strtok(tmp, ",");
		if (token == NULL)
			break;
		if (strlen(token) >= MONITOR_NAME_LEN) {
			printf(_("%s: max monitor name length"
				 " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
			continue;
		}

		for (num = 0; num < avail_monitors; num++) {
			if (!strcmp(monitors[num]->name, token)) {
				dprint("Found requested monitor: %s\n", token);
				tmp_mons[hits] = monitors[num];
				hits++;
			}
244
		}
245 246 247 248 249 250 251
	}
	if (hits == 0) {
		printf(_("No matching monitor found in %s, "
			 "try -l option\n"), param);
		exit(EXIT_FAILURE);
	}
	/* Override detected/registerd monitors array with requested one */
252 253
	memcpy(monitors, tmp_mons,
		sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
254 255 256
	avail_monitors = hits;
}

257 258
void list_monitors(void)
{
259 260 261 262 263 264
	unsigned int mon;
	int state;
	cstate_t s;

	for (mon = 0; mon < avail_monitors; mon++) {
		printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
265 266 267 268
			 "s\n"),
			monitors[mon]->name, monitors[mon]->hw_states_num,
			monitors[mon]->overflow_s);

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
		for (state = 0; state < monitors[mon]->hw_states_num; state++) {
			s = monitors[mon]->hw_states[state];
			/*
			 * ToDo show more state capabilities:
			 * percent, time (granlarity)
			 */
			printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
			       gettext(s.desc));
		}
	}
}

int fork_it(char **argv)
{
	int status;
	unsigned int num;
	unsigned long long timediff;
	pid_t child_pid;
	struct timespec start, end;

	child_pid = fork();
	clock_gettime(CLOCK_REALTIME, &start);

	for (num = 0; num < avail_monitors; num++)
		monitors[num]->start();

	if (!child_pid) {
		/* child */
		execvp(argv[0], argv);
	} else {
		/* parent */
		if (child_pid == -1) {
			perror("fork");
			exit(1);
		}

		signal(SIGINT, SIG_IGN);
		signal(SIGQUIT, SIG_IGN);
		if (waitpid(child_pid, &status, 0) == -1) {
			perror("wait");
			exit(1);
		}
	}
	clock_gettime(CLOCK_REALTIME, &end);
	for (num = 0; num < avail_monitors; num++)
		monitors[num]->stop();

	timediff = timespec_diff_us(start, end);
	if (WIFEXITED(status))
		printf(_("%s took %.5f seconds and exited with status %d\n"),
319 320
			argv[0], timediff / (1000.0 * 1000),
			WEXITSTATUS(status));
321 322 323 324 325 326
	return 0;
}

int do_interval_measure(int i)
{
	unsigned int num;
327 328 329 330 331
	int cpu;

	if (wake_cpus)
		for (cpu = 0; cpu < cpu_count; cpu++)
			bind_cpu(cpu);
332 333 334 335 336 337

	for (num = 0; num < avail_monitors; num++) {
		dprint("HW C-state residency monitor: %s - States: %d\n",
		       monitors[num]->name, monitors[num]->hw_states_num);
		monitors[num]->start();
	}
338

339
	sleep(i);
340 341 342 343 344

	if (wake_cpus)
		for (cpu = 0; cpu < cpu_count; cpu++)
			bind_cpu(cpu);

345
	for (num = 0; num < avail_monitors; num++)
346
		monitors[num]->stop();
347

348

349 350 351 352 353 354 355 356
	return 0;
}

static void cmdline(int argc, char *argv[])
{
	int opt;
	progname = basename(argv[0]);

357
	while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
358 359
		switch (opt) {
		case 'l':
360 361
			if (mode)
				print_wrong_arg_exit();
362 363 364 365
			mode = list;
			break;
		case 'i':
			/* only allow -i with -m or no option */
366 367
			if (mode && mode != show)
				print_wrong_arg_exit();
368 369 370
			interval = atoi(optarg);
			break;
		case 'm':
371 372
			if (mode)
				print_wrong_arg_exit();
373 374 375
			mode = show;
			show_monitors_param = optarg;
			break;
376 377 378
		case 'c':
			wake_cpus = 1;
			break;
379
		default:
380
			print_wrong_arg_exit();
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
		}
	}
	if (!mode)
		mode = show_all;
}

int cmd_monitor(int argc, char **argv)
{
	unsigned int num;
	struct cpuidle_monitor *test_mon;
	int cpu;

	cmdline(argc, argv);
	cpu_count = get_cpu_topology(&cpu_top);
	if (cpu_count < 0) {
		printf(_("Cannot read number of available processors\n"));
		return EXIT_FAILURE;
	}

400 401 402
	if (!cpu_top.core_info[0].is_online)
		printf("WARNING: at least one cpu is offline\n");

403 404 405 406
	/* Default is: monitor all CPUs */
	if (bitmask_isallclear(cpus_chosen))
		bitmask_setall(cpus_chosen);

407
	dprint("System has up to %d CPU cores\n", cpu_count);
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	for (num = 0; all_monitors[num]; num++) {
		dprint("Try to register: %s\n", all_monitors[num]->name);
		test_mon = all_monitors[num]->do_register();
		if (test_mon) {
			if (test_mon->needs_root && !run_as_root) {
				fprintf(stderr, _("Available monitor %s needs "
					  "root access\n"), test_mon->name);
				continue;
			}
			monitors[avail_monitors] = test_mon;
			dprint("%s registered\n", all_monitors[num]->name);
			avail_monitors++;
		}
	}

	if (avail_monitors == 0) {
		printf(_("No HW Cstate monitors found\n"));
		return 1;
	}

	if (mode == list) {
		list_monitors();
		exit(EXIT_SUCCESS);
	}

	if (mode == show)
		parse_monitor_param(show_monitors_param);

	dprint("Packages: %d - Cores: %d - CPUs: %d\n",
	       cpu_top.pkgs, cpu_top.cores, cpu_count);

	/*
	 * if any params left, it must be a command to fork
	 */
	if (argc - optind)
		fork_it(argv + optind);
	else
		do_interval_measure(interval);

	/* ToDo: Topology parsing needs fixing first to do
	   this more generically */
	if (cpu_top.pkgs > 1)
		print_header(3);
	else
		print_header(1);

	for (cpu = 0; cpu < cpu_count; cpu++) {
		if (cpu_top.pkgs > 1)
			print_results(3, cpu);
		else
			print_results(1, cpu);
	}

462
	for (num = 0; num < avail_monitors; num++)
463
		monitors[num]->unregister();
464

465 466 467
	cpu_topology_release(cpu_top);
	return 0;
}