turbostat.c 26.4 KB
Newer Older
L
Len Brown 已提交
1 2 3 4
/*
 * turbostat -- show CPU frequency and C-state residency
 * on modern Intel turbo-capable processors.
 *
L
Len Brown 已提交
5
 * Copyright (c) 2012 Intel Corporation.
L
Len Brown 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Len Brown <len.brown@intel.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */

22
#define _GNU_SOURCE
L
Len Brown 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <ctype.h>
36
#include <sched.h>
L
Len Brown 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#define MSR_TSC	0x10
#define MSR_NEHALEM_PLATFORM_INFO	0xCE
#define MSR_NEHALEM_TURBO_RATIO_LIMIT	0x1AD
#define MSR_APERF	0xE8
#define MSR_MPERF	0xE7
#define MSR_PKG_C2_RESIDENCY	0x60D	/* SNB only */
#define MSR_PKG_C3_RESIDENCY	0x3F8
#define MSR_PKG_C6_RESIDENCY	0x3F9
#define MSR_PKG_C7_RESIDENCY	0x3FA	/* SNB only */
#define MSR_CORE_C3_RESIDENCY	0x3FC
#define MSR_CORE_C6_RESIDENCY	0x3FD
#define MSR_CORE_C7_RESIDENCY	0x3FE	/* SNB only */

char *proc_stat = "/proc/stat";
unsigned int interval_sec = 5;	/* set with -i interval_sec */
unsigned int verbose;		/* set with -v */
L
Len Brown 已提交
54
unsigned int summary_only;	/* set with -s */
L
Len Brown 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
unsigned int skip_c0;
unsigned int skip_c1;
unsigned int do_nhm_cstates;
unsigned int do_snb_cstates;
unsigned int has_aperf;
unsigned int units = 1000000000;	/* Ghz etc */
unsigned int genuine_intel;
unsigned int has_invariant_tsc;
unsigned int do_nehalem_platform_info;
unsigned int do_nehalem_turbo_ratio_limit;
unsigned int extra_msr_offset;
double bclk;
unsigned int show_pkg;
unsigned int show_core;
unsigned int show_cpu;

int aperf_mperf_unstable;
int backwards_count;
char *progname;

int num_cpus;
76 77
cpu_set_t *cpu_present_set, *cpu_mask;
size_t cpu_present_setsize, cpu_mask_size;
L
Len Brown 已提交
78

L
Len Brown 已提交
79
struct counters {
L
Len Brown 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	unsigned long long tsc;		/* per thread */
	unsigned long long aperf;	/* per thread */
	unsigned long long mperf;	/* per thread */
	unsigned long long c1;	/* per thread (calculated) */
	unsigned long long c3;	/* per core */
	unsigned long long c6;	/* per core */
	unsigned long long c7;	/* per core */
	unsigned long long pc2;	/* per package */
	unsigned long long pc3;	/* per package */
	unsigned long long pc6;	/* per package */
	unsigned long long pc7;	/* per package */
	unsigned long long extra_msr;	/* per thread */
	int pkg;
	int core;
	int cpu;
L
Len Brown 已提交
95 96
	struct counters *next;
};
L
Len Brown 已提交
97

L
Len Brown 已提交
98 99 100 101
struct counters *cnt_even;
struct counters *cnt_odd;
struct counters *cnt_delta;
struct counters *cnt_average;
L
Len Brown 已提交
102 103 104 105
struct timeval tv_even;
struct timeval tv_odd;
struct timeval tv_delta;

106 107 108 109 110 111
int mark_cpu_present(int pkg, int core, int cpu)
{
	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
	return 0;
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/*
 * cpu_mask_init(ncpus)
 *
 * allocate and clear cpu_mask
 * set cpu_mask_size
 */
void cpu_mask_init(int ncpus)
{
	cpu_mask = CPU_ALLOC(ncpus);
	if (cpu_mask == NULL) {
		perror("CPU_ALLOC");
		exit(3);
	}
	cpu_mask_size = CPU_ALLOC_SIZE(ncpus);
	CPU_ZERO_S(cpu_mask_size, cpu_mask);
127 128 129 130 131 132 133 134 135 136 137 138

	/*
	 * Allocate and initialize cpu_present_set
	 */
	cpu_present_set = CPU_ALLOC(ncpus);
	if (cpu_present_set == NULL) {
		perror("CPU_ALLOC");
		exit(3);
	}
	cpu_present_setsize = CPU_ALLOC_SIZE(ncpus);
	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
	for_all_cpus(mark_cpu_present);
139 140 141 142 143 144 145
}

void cpu_mask_uninit()
{
	CPU_FREE(cpu_mask);
	cpu_mask = NULL;
	cpu_mask_size = 0;
146 147 148
	CPU_FREE(cpu_present_set);
	cpu_present_set = NULL;
	cpu_present_setsize = 0;
149 150 151 152 153 154 155 156 157 158 159 160
}

int cpu_migrate(int cpu)
{
	CPU_ZERO_S(cpu_mask_size, cpu_mask);
	CPU_SET_S(cpu, cpu_mask_size, cpu_mask);
	if (sched_setaffinity(0, cpu_mask_size, cpu_mask) == -1)
		return -1;
	else
		return 0;
}

161
int get_msr(int cpu, off_t offset, unsigned long long *msr)
L
Len Brown 已提交
162 163 164 165 166 167 168
{
	ssize_t retval;
	char pathname[32];
	int fd;

	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
	fd = open(pathname, O_RDONLY);
169 170
	if (fd < 0)
		return -1;
L
Len Brown 已提交
171

172
	retval = pread(fd, msr, sizeof *msr, offset);
L
Len Brown 已提交
173
	close(fd);
174 175 176 177 178

	if (retval != sizeof *msr)
		return -1;

	return 0;
L
Len Brown 已提交
179 180
}

L
Len Brown 已提交
181
void print_header(void)
L
Len Brown 已提交
182 183
{
	if (show_pkg)
184
		fprintf(stderr, "pk");
L
Len Brown 已提交
185 186
	if (show_pkg)
		fprintf(stderr, " ");
L
Len Brown 已提交
187
	if (show_core)
L
Len Brown 已提交
188
		fprintf(stderr, "cor");
L
Len Brown 已提交
189 190
	if (show_cpu)
		fprintf(stderr, " CPU");
L
Len Brown 已提交
191 192
	if (show_pkg || show_core || show_cpu)
		fprintf(stderr, " ");
L
Len Brown 已提交
193
	if (do_nhm_cstates)
L
Len Brown 已提交
194
		fprintf(stderr, "   %%c0");
L
Len Brown 已提交
195
	if (has_aperf)
L
Len Brown 已提交
196
		fprintf(stderr, "  GHz");
L
Len Brown 已提交
197 198
	fprintf(stderr, "  TSC");
	if (do_nhm_cstates)
199
		fprintf(stderr, "    %%c1");
L
Len Brown 已提交
200
	if (do_nhm_cstates)
201
		fprintf(stderr, "    %%c3");
L
Len Brown 已提交
202
	if (do_nhm_cstates)
203
		fprintf(stderr, "    %%c6");
L
Len Brown 已提交
204
	if (do_snb_cstates)
205
		fprintf(stderr, "    %%c7");
L
Len Brown 已提交
206
	if (do_snb_cstates)
L
Len Brown 已提交
207
		fprintf(stderr, "   %%pc2");
L
Len Brown 已提交
208
	if (do_nhm_cstates)
L
Len Brown 已提交
209
		fprintf(stderr, "   %%pc3");
L
Len Brown 已提交
210
	if (do_nhm_cstates)
L
Len Brown 已提交
211
		fprintf(stderr, "   %%pc6");
L
Len Brown 已提交
212
	if (do_snb_cstates)
L
Len Brown 已提交
213
		fprintf(stderr, "   %%pc7");
L
Len Brown 已提交
214
	if (extra_msr_offset)
215
		fprintf(stderr, "        MSR 0x%x ", extra_msr_offset);
L
Len Brown 已提交
216 217 218 219

	putc('\n', stderr);
}

L
Len Brown 已提交
220
void dump_cnt(struct counters *cnt)
L
Len Brown 已提交
221
{
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	if (!cnt)
		return;
	if (cnt->pkg) fprintf(stderr, "package: %d ", cnt->pkg);
	if (cnt->core) fprintf(stderr, "core:: %d ", cnt->core);
	if (cnt->cpu) fprintf(stderr, "CPU: %d ", cnt->cpu);
	if (cnt->tsc) fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
	if (cnt->c3) fprintf(stderr, "c3: %016llX\n", cnt->c3);
	if (cnt->c6) fprintf(stderr, "c6: %016llX\n", cnt->c6);
	if (cnt->c7) fprintf(stderr, "c7: %016llX\n", cnt->c7);
	if (cnt->aperf) fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
	if (cnt->pc2) fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
	if (cnt->pc3) fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
	if (cnt->pc6) fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
	if (cnt->pc7) fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
	if (cnt->extra_msr) fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
L
Len Brown 已提交
237 238
}

L
Len Brown 已提交
239
void dump_list(struct counters *cnt)
L
Len Brown 已提交
240
{
L
Len Brown 已提交
241
	printf("dump_list 0x%p\n", cnt);
L
Len Brown 已提交
242

L
Len Brown 已提交
243 244
	for (; cnt; cnt = cnt->next)
		dump_cnt(cnt);
L
Len Brown 已提交
245 246
}

L
Len Brown 已提交
247 248 249 250 251 252 253 254 255
/*
 * column formatting convention & formats
 * package: "pk" 2 columns %2d
 * core: "cor" 3 columns %3d
 * CPU: "CPU" 3 columns %3d
 * GHz: "GHz" 3 columns %3.2
 * TSC: "TSC" 3 columns %3.2
 * percentage " %pc3" %6.2
 */
L
Len Brown 已提交
256
void print_cnt(struct counters *p)
L
Len Brown 已提交
257 258 259 260 261 262
{
	double interval_float;

	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;

	/* topology columns, print blanks on 1st (average) line */
L
Len Brown 已提交
263
	if (p == cnt_average) {
L
Len Brown 已提交
264
		if (show_pkg)
L
Len Brown 已提交
265 266
			fprintf(stderr, "  ");
		if (show_pkg && show_core)
267
			fprintf(stderr, " ");
L
Len Brown 已提交
268
		if (show_core)
L
Len Brown 已提交
269
			fprintf(stderr, "   ");
L
Len Brown 已提交
270
		if (show_cpu)
L
Len Brown 已提交
271
			fprintf(stderr, " " "   ");
L
Len Brown 已提交
272 273
	} else {
		if (show_pkg)
L
Len Brown 已提交
274 275 276
			fprintf(stderr, "%2d", p->pkg);
		if (show_pkg && show_core)
			fprintf(stderr, " ");
L
Len Brown 已提交
277
		if (show_core)
L
Len Brown 已提交
278
			fprintf(stderr, "%3d", p->core);
L
Len Brown 已提交
279
		if (show_cpu)
L
Len Brown 已提交
280
			fprintf(stderr, " %3d", p->cpu);
L
Len Brown 已提交
281 282 283 284
	}

	/* %c0 */
	if (do_nhm_cstates) {
L
Len Brown 已提交
285 286
		if (show_pkg || show_core || show_cpu)
			fprintf(stderr, " ");
L
Len Brown 已提交
287
		if (!skip_c0)
L
Len Brown 已提交
288
			fprintf(stderr, "%6.2f", 100.0 * p->mperf/p->tsc);
L
Len Brown 已提交
289
		else
L
Len Brown 已提交
290
			fprintf(stderr, "  ****");
L
Len Brown 已提交
291 292 293 294 295
	}

	/* GHz */
	if (has_aperf) {
		if (!aperf_mperf_unstable) {
L
Len Brown 已提交
296
			fprintf(stderr, " %3.2f",
L
Len Brown 已提交
297 298 299 300
				1.0 * p->tsc / units * p->aperf /
				p->mperf / interval_float);
		} else {
			if (p->aperf > p->tsc || p->mperf > p->tsc) {
L
Len Brown 已提交
301
				fprintf(stderr, " ***");
L
Len Brown 已提交
302
			} else {
L
Len Brown 已提交
303
				fprintf(stderr, "%3.1f*",
L
Len Brown 已提交
304 305 306 307 308 309 310 311 312 313 314 315
					1.0 * p->tsc /
					units * p->aperf /
					p->mperf / interval_float);
			}
		}
	}

	/* TSC */
	fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float);

	if (do_nhm_cstates) {
		if (!skip_c1)
L
Len Brown 已提交
316
			fprintf(stderr, " %6.2f", 100.0 * p->c1/p->tsc);
L
Len Brown 已提交
317
		else
318
			fprintf(stderr, "  ****");
L
Len Brown 已提交
319 320
	}
	if (do_nhm_cstates)
321
		fprintf(stderr, " %6.2f", 100.0 * p->c3/p->tsc);
L
Len Brown 已提交
322
	if (do_nhm_cstates)
323
		fprintf(stderr, " %6.2f", 100.0 * p->c6/p->tsc);
L
Len Brown 已提交
324
	if (do_snb_cstates)
325
		fprintf(stderr, " %6.2f", 100.0 * p->c7/p->tsc);
L
Len Brown 已提交
326
	if (do_snb_cstates)
L
Len Brown 已提交
327
		fprintf(stderr, " %6.2f", 100.0 * p->pc2/p->tsc);
L
Len Brown 已提交
328
	if (do_nhm_cstates)
L
Len Brown 已提交
329
		fprintf(stderr, " %6.2f", 100.0 * p->pc3/p->tsc);
L
Len Brown 已提交
330
	if (do_nhm_cstates)
L
Len Brown 已提交
331
		fprintf(stderr, " %6.2f", 100.0 * p->pc6/p->tsc);
L
Len Brown 已提交
332
	if (do_snb_cstates)
L
Len Brown 已提交
333
		fprintf(stderr, " %6.2f", 100.0 * p->pc7/p->tsc);
L
Len Brown 已提交
334 335 336 337 338
	if (extra_msr_offset)
		fprintf(stderr, "  0x%016llx", p->extra_msr);
	putc('\n', stderr);
}

L
Len Brown 已提交
339
void print_counters(struct counters *counters)
L
Len Brown 已提交
340
{
L
Len Brown 已提交
341
	struct counters *cnt;
L
Len Brown 已提交
342
	static int printed;
L
Len Brown 已提交
343

L
Len Brown 已提交
344 345 346

	if (!printed || !summary_only)
		print_header();
L
Len Brown 已提交
347 348

	if (num_cpus > 1)
L
Len Brown 已提交
349
		print_cnt(cnt_average);
L
Len Brown 已提交
350

L
Len Brown 已提交
351 352 353 354 355
	printed = 1;

	if (summary_only)
		return;

L
Len Brown 已提交
356 357
	for (cnt = counters; cnt != NULL; cnt = cnt->next)
		print_cnt(cnt);
L
Len Brown 已提交
358 359 360 361 362

}

#define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))

L
Len Brown 已提交
363 364
int compute_delta(struct counters *after,
	struct counters *before, struct counters *delta)
L
Len Brown 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 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 462 463 464 465 466 467 468 469 470 471 472 473 474 475
{
	int errors = 0;
	int perf_err = 0;

	skip_c0 = skip_c1 = 0;

	for ( ; after && before && delta;
		after = after->next, before = before->next, delta = delta->next) {
		if (before->cpu != after->cpu) {
			printf("cpu configuration changed: %d != %d\n",
				before->cpu, after->cpu);
			return -1;
		}

		if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) {
			fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n",
				before->cpu, before->tsc, after->tsc);
			errors++;
		}
		/* check for TSC < 1 Mcycles over interval */
		if (delta->tsc < (1000 * 1000)) {
			fprintf(stderr, "Insanely slow TSC rate,"
				" TSC stops in idle?\n");
			fprintf(stderr, "You can disable all c-states"
				" by booting with \"idle=poll\"\n");
			fprintf(stderr, "or just the deep ones with"
				" \"processor.max_cstate=1\"\n");
			exit(-3);
		}
		if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) {
			fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n",
				before->cpu, before->c3, after->c3);
			errors++;
		}
		if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) {
			fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n",
				before->cpu, before->c6, after->c6);
			errors++;
		}
		if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) {
			fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n",
				before->cpu, before->c7, after->c7);
			errors++;
		}
		if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) {
			fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n",
				before->cpu, before->pc2, after->pc2);
			errors++;
		}
		if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) {
			fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n",
				before->cpu, before->pc3, after->pc3);
			errors++;
		}
		if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) {
			fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n",
				before->cpu, before->pc6, after->pc6);
			errors++;
		}
		if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) {
			fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n",
				before->cpu, before->pc7, after->pc7);
			errors++;
		}

		perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
		if (perf_err) {
			fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
				before->cpu, before->aperf, after->aperf);
		}
		perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
		if (perf_err) {
			fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
				before->cpu, before->mperf, after->mperf);
		}
		if (perf_err) {
			if (!aperf_mperf_unstable) {
				fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
				fprintf(stderr, "* Frequency results do not cover entire interval *\n");
				fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");

				aperf_mperf_unstable = 1;
			}
			/*
			 * mperf delta is likely a huge "positive" number
			 * can not use it for calculating c0 time
			 */
			skip_c0 = 1;
			skip_c1 = 1;
		}

		/*
		 * As mperf and tsc collection are not atomic,
		 * it is possible for mperf's non-halted cycles
		 * to exceed TSC's all cycles: show c1 = 0% in that case.
		 */
		if (delta->mperf > delta->tsc)
			delta->c1 = 0;
		else /* normal case, derive c1 */
			delta->c1 = delta->tsc - delta->mperf
				- delta->c3 - delta->c6 - delta->c7;

		if (delta->mperf == 0)
			delta->mperf = 1;	/* divide by 0 protection */

		/*
		 * for "extra msr", just copy the latest w/o subtracting
		 */
		delta->extra_msr = after->extra_msr;
		if (errors) {
			fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
L
Len Brown 已提交
476
			dump_cnt(before);
L
Len Brown 已提交
477
			fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
L
Len Brown 已提交
478
			dump_cnt(after);
L
Len Brown 已提交
479 480 481 482 483 484
			errors = 0;
		}
	}
	return 0;
}

L
Len Brown 已提交
485
void compute_average(struct counters *delta, struct counters *avg)
L
Len Brown 已提交
486
{
L
Len Brown 已提交
487
	struct counters *sum;
L
Len Brown 已提交
488

L
Len Brown 已提交
489
	sum = calloc(1, sizeof(struct counters));
L
Len Brown 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	if (sum == NULL) {
		perror("calloc sum");
		exit(1);
	}

	for (; delta; delta = delta->next) {
		sum->tsc += delta->tsc;
		sum->c1 += delta->c1;
		sum->c3 += delta->c3;
		sum->c6 += delta->c6;
		sum->c7 += delta->c7;
		sum->aperf += delta->aperf;
		sum->mperf += delta->mperf;
		sum->pc2 += delta->pc2;
		sum->pc3 += delta->pc3;
		sum->pc6 += delta->pc6;
		sum->pc7 += delta->pc7;
	}
	avg->tsc = sum->tsc/num_cpus;
	avg->c1 = sum->c1/num_cpus;
	avg->c3 = sum->c3/num_cpus;
	avg->c6 = sum->c6/num_cpus;
	avg->c7 = sum->c7/num_cpus;
	avg->aperf = sum->aperf/num_cpus;
	avg->mperf = sum->mperf/num_cpus;
	avg->pc2 = sum->pc2/num_cpus;
	avg->pc3 = sum->pc3/num_cpus;
	avg->pc6 = sum->pc6/num_cpus;
	avg->pc7 = sum->pc7/num_cpus;

	free(sum);
}

523
int get_counters(struct counters *cnt)
L
Len Brown 已提交
524
{
L
Len Brown 已提交
525
	for ( ; cnt; cnt = cnt->next) {
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

		if (cpu_migrate(cnt->cpu))
			return -1;

		if (get_msr(cnt->cpu, MSR_TSC, &cnt->tsc))
			return -1;

		if (has_aperf) {
			if (get_msr(cnt->cpu, MSR_APERF, &cnt->aperf))
				return -1;
			if (get_msr(cnt->cpu, MSR_MPERF, &cnt->mperf))
				return -1;
		}

		if (do_nhm_cstates) {
			if (get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY, &cnt->c3))
				return -1;
			if (get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY, &cnt->c6))
				return -1;
545 546
		}

L
Len Brown 已提交
547
		if (do_snb_cstates)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
			if (get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY, &cnt->c7))
				return -1;

		if (do_nhm_cstates) {
			if (get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY, &cnt->pc3))
				return -1;
			if (get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY, &cnt->pc6))
				return -1;
		}
		if (do_snb_cstates) {
			if (get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY, &cnt->pc2))
				return -1;
			if (get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY, &cnt->pc7))
				return -1;
		}
L
Len Brown 已提交
563
		if (extra_msr_offset)
564 565
			if (get_msr(cnt->cpu, extra_msr_offset, &cnt->extra_msr))
				return -1;
L
Len Brown 已提交
566
	}
567
	return 0;
L
Len Brown 已提交
568 569
}

L
Len Brown 已提交
570
void print_nehalem_info(void)
L
Len Brown 已提交
571 572 573 574 575 576 577
{
	unsigned long long msr;
	unsigned int ratio;

	if (!do_nehalem_platform_info)
		return;

578
	get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
L
Len Brown 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

	ratio = (msr >> 40) & 0xFF;
	fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
		ratio, bclk, ratio * bclk);

	ratio = (msr >> 8) & 0xFF;
	fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
		ratio, bclk, ratio * bclk);

	if (verbose > 1)
		fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);

	if (!do_nehalem_turbo_ratio_limit)
		return;

594
	get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
L
Len Brown 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

	ratio = (msr >> 24) & 0xFF;
	if (ratio)
		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
			ratio, bclk, ratio * bclk);

	ratio = (msr >> 16) & 0xFF;
	if (ratio)
		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
			ratio, bclk, ratio * bclk);

	ratio = (msr >> 8) & 0xFF;
	if (ratio)
		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
			ratio, bclk, ratio * bclk);

	ratio = (msr >> 0) & 0xFF;
	if (ratio)
		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
			ratio, bclk, ratio * bclk);

}

L
Len Brown 已提交
618
void free_counter_list(struct counters *list)
L
Len Brown 已提交
619
{
L
Len Brown 已提交
620
	struct counters *p;
L
Len Brown 已提交
621 622

	for (p = list; p; ) {
L
Len Brown 已提交
623
		struct counters *free_me;
L
Len Brown 已提交
624 625 626 627 628 629 630 631 632

		free_me = p;
		p = p->next;
		free(free_me);
	}
}

void free_all_counters(void)
{
L
Len Brown 已提交
633 634
	free_counter_list(cnt_even);
	cnt_even = NULL;
L
Len Brown 已提交
635

L
Len Brown 已提交
636 637
	free_counter_list(cnt_odd);
	cnt_odd = NULL;
L
Len Brown 已提交
638

L
Len Brown 已提交
639 640
	free_counter_list(cnt_delta);
	cnt_delta = NULL;
L
Len Brown 已提交
641

L
Len Brown 已提交
642 643
	free_counter_list(cnt_average);
	cnt_average = NULL;
L
Len Brown 已提交
644 645
}

L
Len Brown 已提交
646 647
void insert_counters(struct counters **list,
	struct counters *new)
L
Len Brown 已提交
648
{
L
Len Brown 已提交
649
	struct counters *prev;
L
Len Brown 已提交
650 651 652 653 654 655 656 657 658 659

	/*
	 * list was empty
	 */
	if (*list == NULL) {
		new->next = *list;
		*list = new;
		return;
	}

L
Len Brown 已提交
660 661
	if (!summary_only)
		show_cpu = 1;	/* there is more than one CPU */
L
Len Brown 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

	/*
	 * insert on front of list.
	 * It is sorted by ascending package#, core#, cpu#
	 */
	if (((*list)->pkg > new->pkg) ||
	    (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
	    (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
		new->next = *list;
		*list = new;
		return;
	}

	prev = *list;

	while (prev->next && (prev->next->pkg < new->pkg)) {
		prev = prev->next;
L
Len Brown 已提交
679 680
		if (!summary_only)
			show_pkg = 1;	/* there is more than 1 package */
L
Len Brown 已提交
681 682 683 684 685
	}

	while (prev->next && (prev->next->pkg == new->pkg)
		&& (prev->next->core < new->core)) {
		prev = prev->next;
L
Len Brown 已提交
686 687
		if (!summary_only)
			show_core = 1;	/* there is more than 1 core */
L
Len Brown 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	}

	while (prev->next && (prev->next->pkg == new->pkg)
		&& (prev->next->core == new->core)
		&& (prev->next->cpu < new->cpu)) {
		prev = prev->next;
	}

	/*
	 * insert after "prev"
	 */
	new->next = prev->next;
	prev->next = new;
}

L
Len Brown 已提交
703
void alloc_new_counters(int pkg, int core, int cpu)
L
Len Brown 已提交
704
{
L
Len Brown 已提交
705
	struct counters *new;
L
Len Brown 已提交
706 707 708 709

	if (verbose > 1)
		printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);

L
Len Brown 已提交
710
	new = (struct counters *)calloc(1, sizeof(struct counters));
L
Len Brown 已提交
711 712 713 714 715 716 717
	if (new == NULL) {
		perror("calloc");
		exit(1);
	}
	new->pkg = pkg;
	new->core = core;
	new->cpu = cpu;
L
Len Brown 已提交
718
	insert_counters(&cnt_odd, new);
L
Len Brown 已提交
719

L
Len Brown 已提交
720 721
	new = (struct counters *)calloc(1,
		sizeof(struct counters));
L
Len Brown 已提交
722 723 724 725 726 727 728
	if (new == NULL) {
		perror("calloc");
		exit(1);
	}
	new->pkg = pkg;
	new->core = core;
	new->cpu = cpu;
L
Len Brown 已提交
729
	insert_counters(&cnt_even, new);
L
Len Brown 已提交
730

L
Len Brown 已提交
731
	new = (struct counters *)calloc(1, sizeof(struct counters));
L
Len Brown 已提交
732 733 734 735 736 737 738
	if (new == NULL) {
		perror("calloc");
		exit(1);
	}
	new->pkg = pkg;
	new->core = core;
	new->cpu = cpu;
L
Len Brown 已提交
739
	insert_counters(&cnt_delta, new);
L
Len Brown 已提交
740

L
Len Brown 已提交
741
	new = (struct counters *)calloc(1, sizeof(struct counters));
L
Len Brown 已提交
742 743 744 745 746 747 748
	if (new == NULL) {
		perror("calloc");
		exit(1);
	}
	new->pkg = pkg;
	new->core = core;
	new->cpu = cpu;
L
Len Brown 已提交
749
	cnt_average = new;
L
Len Brown 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
}

int get_physical_package_id(int cpu)
{
	char path[64];
	FILE *filep;
	int pkg;

	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
	filep = fopen(path, "r");
	if (filep == NULL) {
		perror(path);
		exit(1);
	}
	fscanf(filep, "%d", &pkg);
	fclose(filep);
	return pkg;
}

int get_core_id(int cpu)
{
	char path[64];
	FILE *filep;
	int core;

	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
	filep = fopen(path, "r");
	if (filep == NULL) {
		perror(path);
		exit(1);
	}
	fscanf(filep, "%d", &core);
	fclose(filep);
	return core;
}

/*
787
 * run func(pkg, core, cpu) on every cpu in /proc/stat
L
Len Brown 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
 */

int for_all_cpus(void (func)(int, int, int))
{
	FILE *fp;
	int cpu_count;
	int retval;

	fp = fopen(proc_stat, "r");
	if (fp == NULL) {
		perror(proc_stat);
		exit(1);
	}

	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
	if (retval != 0) {
		perror("/proc/stat format");
		exit(1);
	}

	for (cpu_count = 0; ; cpu_count++) {
		int cpu;

		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
		if (retval != 1)
			break;

		func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
	}
	fclose(fp);
	return cpu_count;
}

void re_initialize(void)
{
	free_all_counters();
L
Len Brown 已提交
824
	num_cpus = for_all_cpus(alloc_new_counters);
825 826
	cpu_mask_uninit();
	cpu_mask_init(num_cpus);
827
	printf("turbostat: re-initialized with num_cpus %d\n", num_cpus);
L
Len Brown 已提交
828 829 830 831 832 833
}

void dummy(int pkg, int core, int cpu) { return; }
/*
 * check to see if a cpu came on-line
 */
834
int verify_num_cpus(void)
L
Len Brown 已提交
835 836 837 838 839 840 841 842 843
{
	int new_num_cpus;

	new_num_cpus = for_all_cpus(dummy);

	if (new_num_cpus != num_cpus) {
		if (verbose)
			printf("num_cpus was %d, is now  %d\n",
				num_cpus, new_num_cpus);
844
		return -1;
L
Len Brown 已提交
845
	}
846
	return 0;
L
Len Brown 已提交
847 848 849 850 851
}

void turbostat_loop()
{
restart:
L
Len Brown 已提交
852
	get_counters(cnt_even);
L
Len Brown 已提交
853 854 855
	gettimeofday(&tv_even, (struct timezone *)NULL);

	while (1) {
856
		if (verify_num_cpus()) {
L
Len Brown 已提交
857 858 859 860
			re_initialize();
			goto restart;
		}
		sleep(interval_sec);
861 862 863 864
		if (get_counters(cnt_odd)) {
			re_initialize();
			goto restart;
		}
L
Len Brown 已提交
865
		gettimeofday(&tv_odd, (struct timezone *)NULL);
L
Len Brown 已提交
866
		compute_delta(cnt_odd, cnt_even, cnt_delta);
L
Len Brown 已提交
867
		timersub(&tv_odd, &tv_even, &tv_delta);
L
Len Brown 已提交
868 869
		compute_average(cnt_delta, cnt_average);
		print_counters(cnt_delta);
870 871
		sleep(interval_sec);
		if (get_counters(cnt_even)) {
L
Len Brown 已提交
872 873 874 875
			re_initialize();
			goto restart;
		}
		gettimeofday(&tv_even, (struct timezone *)NULL);
L
Len Brown 已提交
876
		compute_delta(cnt_even, cnt_odd, cnt_delta);
L
Len Brown 已提交
877
		timersub(&tv_even, &tv_odd, &tv_delta);
L
Len Brown 已提交
878 879
		compute_average(cnt_delta, cnt_average);
		print_counters(cnt_delta);
L
Len Brown 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
	}
}

void check_dev_msr()
{
	struct stat sb;

	if (stat("/dev/cpu/0/msr", &sb)) {
		fprintf(stderr, "no /dev/cpu/0/msr\n");
		fprintf(stderr, "Try \"# modprobe msr\"\n");
		exit(-5);
	}
}

void check_super_user()
{
	if (getuid() != 0) {
		fprintf(stderr, "must be root\n");
		exit(-6);
	}
}

int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
{
	if (!genuine_intel)
		return 0;

	if (family != 6)
		return 0;

	switch (model) {
	case 0x1A:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
	case 0x1E:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
	case 0x25:	/* Westmere Client - Clarkdale, Arrandale */
	case 0x2C:	/* Westmere EP - Gulftown */
	case 0x2A:	/* SNB */
	case 0x2D:	/* SNB Xeon */
918 919
	case 0x3A:	/* IVB */
	case 0x3D:	/* IVB Xeon */
L
Len Brown 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
		return 1;
	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
	default:
		return 0;
	}
}

int is_snb(unsigned int family, unsigned int model)
{
	if (!genuine_intel)
		return 0;

	switch (model) {
	case 0x2A:
	case 0x2D:
		return 1;
	}
	return 0;
}

double discover_bclk(unsigned int family, unsigned int model)
{
	if (is_snb(family, model))
		return 100.00;
	else
		return 133.33;
}

void check_cpuid()
{
	unsigned int eax, ebx, ecx, edx, max_level;
	unsigned int fms, family, model, stepping;

	eax = ebx = ecx = edx = 0;

	asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));

	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
		genuine_intel = 1;

	if (verbose)
		fprintf(stderr, "%.4s%.4s%.4s ",
			(char *)&ebx, (char *)&edx, (char *)&ecx);

	asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
	family = (fms >> 8) & 0xf;
	model = (fms >> 4) & 0xf;
	stepping = fms & 0xf;
	if (family == 6 || family == 0xf)
		model += ((fms >> 16) & 0xf) << 4;

	if (verbose)
		fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
			max_level, family, model, stepping, family, model, stepping);

	if (!(edx & (1 << 5))) {
		fprintf(stderr, "CPUID: no MSR\n");
		exit(1);
	}

	/*
	 * check max extended function levels of CPUID.
	 * This is needed to check for invariant TSC.
	 * This check is valid for both Intel and AMD.
	 */
	ebx = ecx = edx = 0;
	asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));

	if (max_level < 0x80000007) {
		fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
		exit(1);
	}

	/*
	 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
	 * this check is valid for both Intel and AMD
	 */
	asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
999
	has_invariant_tsc = edx & (1 << 8);
L
Len Brown 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

	if (!has_invariant_tsc) {
		fprintf(stderr, "No invariant TSC\n");
		exit(1);
	}

	/*
	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
	 * this check is valid for both Intel and AMD
	 */

	asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
1012
	has_aperf = ecx & (1 << 0);
L
Len Brown 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	if (!has_aperf) {
		fprintf(stderr, "No APERF MSR\n");
		exit(1);
	}

	do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
	do_nhm_cstates = genuine_intel;	/* all Intel w/ non-stop TSC have NHM counters */
	do_snb_cstates = is_snb(family, model);
	bclk = discover_bclk(family, model);

	do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
}


void usage()
{
	fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
		progname);
	exit(1);
}


/*
 * in /dev/cpu/ return success for names that are numbers
 * ie. filter out ".", "..", "microcode".
 */
int dir_filter(const struct dirent *dirp)
{
	if (isdigit(dirp->d_name[0]))
		return 1;
	else
		return 0;
}

int open_dev_cpu_msr(int dummy1)
{
	return 0;
}

void turbostat_init()
{
	check_cpuid();

	check_dev_msr();
	check_super_user();

L
Len Brown 已提交
1059
	num_cpus = for_all_cpus(alloc_new_counters);
1060
	cpu_mask_init(num_cpus);
L
Len Brown 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069

	if (verbose)
		print_nehalem_info();
}

int fork_it(char **argv)
{
	int retval;
	pid_t child_pid;
L
Len Brown 已提交
1070
	get_counters(cnt_even);
1071 1072 1073

        /* clear affinity side-effect of get_counters() */
        sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
L
Len Brown 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	gettimeofday(&tv_even, (struct timezone *)NULL);

	child_pid = fork();
	if (!child_pid) {
		/* child */
		execvp(argv[0], argv);
	} else {
		int status;

		/* 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);
		}
	}
L
Len Brown 已提交
1096
	get_counters(cnt_odd);
L
Len Brown 已提交
1097
	gettimeofday(&tv_odd, (struct timezone *)NULL);
L
Len Brown 已提交
1098
	retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
L
Len Brown 已提交
1099 1100

	timersub(&tv_odd, &tv_even, &tv_delta);
L
Len Brown 已提交
1101
	compute_average(cnt_delta, cnt_average);
L
Len Brown 已提交
1102
	if (!retval)
L
Len Brown 已提交
1103
		print_counters(cnt_delta);
L
Len Brown 已提交
1104

1105
	fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
L
Len Brown 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

	return 0;
}

void cmdline(int argc, char **argv)
{
	int opt;

	progname = argv[0];

L
Len Brown 已提交
1116
	while ((opt = getopt(argc, argv, "+svi:M:")) != -1) {
L
Len Brown 已提交
1117
		switch (opt) {
L
Len Brown 已提交
1118 1119 1120
		case 's':
			summary_only++;
			break;
L
Len Brown 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
		case 'v':
			verbose++;
			break;
		case 'i':
			interval_sec = atoi(optarg);
			break;
		case 'M':
			sscanf(optarg, "%x", &extra_msr_offset);
			if (verbose > 1)
				fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
			break;
		default:
			usage();
		}
	}
}

int main(int argc, char **argv)
{
	cmdline(argc, argv);

	if (verbose > 1)
		fprintf(stderr, "turbostat Dec 6, 2010"
			" - Len Brown <lenb@kernel.org>\n");
	if (verbose > 1)
		fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n");

	turbostat_init();

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

	return 0;
}