1103a8feaSLen Brown /*
2103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
3103a8feaSLen Brown  * on modern Intel turbo-capable processors.
4103a8feaSLen Brown  *
5e23da037SLen Brown  * Copyright (c) 2012 Intel Corporation.
6103a8feaSLen Brown  * Len Brown <len.brown@intel.com>
7103a8feaSLen Brown  *
8103a8feaSLen Brown  * This program is free software; you can redistribute it and/or modify it
9103a8feaSLen Brown  * under the terms and conditions of the GNU General Public License,
10103a8feaSLen Brown  * version 2, as published by the Free Software Foundation.
11103a8feaSLen Brown  *
12103a8feaSLen Brown  * This program is distributed in the hope it will be useful, but WITHOUT
13103a8feaSLen Brown  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14103a8feaSLen Brown  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15103a8feaSLen Brown  * more details.
16103a8feaSLen Brown  *
17103a8feaSLen Brown  * You should have received a copy of the GNU General Public License along with
18103a8feaSLen Brown  * this program; if not, write to the Free Software Foundation, Inc.,
19103a8feaSLen Brown  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20103a8feaSLen Brown  */
21103a8feaSLen Brown 
2288c3281fSLen Brown #define _GNU_SOURCE
23103a8feaSLen Brown #include <stdio.h>
24103a8feaSLen Brown #include <unistd.h>
25103a8feaSLen Brown #include <sys/types.h>
26103a8feaSLen Brown #include <sys/wait.h>
27103a8feaSLen Brown #include <sys/stat.h>
28103a8feaSLen Brown #include <sys/resource.h>
29103a8feaSLen Brown #include <fcntl.h>
30103a8feaSLen Brown #include <signal.h>
31103a8feaSLen Brown #include <sys/time.h>
32103a8feaSLen Brown #include <stdlib.h>
33103a8feaSLen Brown #include <dirent.h>
34103a8feaSLen Brown #include <string.h>
35103a8feaSLen Brown #include <ctype.h>
3688c3281fSLen Brown #include <sched.h>
37103a8feaSLen Brown 
38103a8feaSLen Brown #define MSR_TSC	0x10
39103a8feaSLen Brown #define MSR_NEHALEM_PLATFORM_INFO	0xCE
40103a8feaSLen Brown #define MSR_NEHALEM_TURBO_RATIO_LIMIT	0x1AD
41103a8feaSLen Brown #define MSR_APERF	0xE8
42103a8feaSLen Brown #define MSR_MPERF	0xE7
43103a8feaSLen Brown #define MSR_PKG_C2_RESIDENCY	0x60D	/* SNB only */
44103a8feaSLen Brown #define MSR_PKG_C3_RESIDENCY	0x3F8
45103a8feaSLen Brown #define MSR_PKG_C6_RESIDENCY	0x3F9
46103a8feaSLen Brown #define MSR_PKG_C7_RESIDENCY	0x3FA	/* SNB only */
47103a8feaSLen Brown #define MSR_CORE_C3_RESIDENCY	0x3FC
48103a8feaSLen Brown #define MSR_CORE_C6_RESIDENCY	0x3FD
49103a8feaSLen Brown #define MSR_CORE_C7_RESIDENCY	0x3FE	/* SNB only */
50103a8feaSLen Brown 
51103a8feaSLen Brown char *proc_stat = "/proc/stat";
52103a8feaSLen Brown unsigned int interval_sec = 5;	/* set with -i interval_sec */
53103a8feaSLen Brown unsigned int verbose;		/* set with -v */
54e23da037SLen Brown unsigned int summary_only;	/* set with -s */
55103a8feaSLen Brown unsigned int skip_c0;
56103a8feaSLen Brown unsigned int skip_c1;
57103a8feaSLen Brown unsigned int do_nhm_cstates;
58103a8feaSLen Brown unsigned int do_snb_cstates;
59103a8feaSLen Brown unsigned int has_aperf;
60103a8feaSLen Brown unsigned int units = 1000000000;	/* Ghz etc */
61103a8feaSLen Brown unsigned int genuine_intel;
62103a8feaSLen Brown unsigned int has_invariant_tsc;
63103a8feaSLen Brown unsigned int do_nehalem_platform_info;
64103a8feaSLen Brown unsigned int do_nehalem_turbo_ratio_limit;
65103a8feaSLen Brown unsigned int extra_msr_offset;
66103a8feaSLen Brown double bclk;
67103a8feaSLen Brown unsigned int show_pkg;
68103a8feaSLen Brown unsigned int show_core;
69103a8feaSLen Brown unsigned int show_cpu;
70103a8feaSLen Brown 
71103a8feaSLen Brown int aperf_mperf_unstable;
72103a8feaSLen Brown int backwards_count;
73103a8feaSLen Brown char *progname;
74103a8feaSLen Brown 
75103a8feaSLen Brown int num_cpus;
76d15cf7c1SLen Brown cpu_set_t *cpu_present_set, *cpu_mask;
77d15cf7c1SLen Brown size_t cpu_present_setsize, cpu_mask_size;
78103a8feaSLen Brown 
79a829eb4dSLen Brown struct counters {
80103a8feaSLen Brown 	unsigned long long tsc;		/* per thread */
81103a8feaSLen Brown 	unsigned long long aperf;	/* per thread */
82103a8feaSLen Brown 	unsigned long long mperf;	/* per thread */
83103a8feaSLen Brown 	unsigned long long c1;	/* per thread (calculated) */
84103a8feaSLen Brown 	unsigned long long c3;	/* per core */
85103a8feaSLen Brown 	unsigned long long c6;	/* per core */
86103a8feaSLen Brown 	unsigned long long c7;	/* per core */
87103a8feaSLen Brown 	unsigned long long pc2;	/* per package */
88103a8feaSLen Brown 	unsigned long long pc3;	/* per package */
89103a8feaSLen Brown 	unsigned long long pc6;	/* per package */
90103a8feaSLen Brown 	unsigned long long pc7;	/* per package */
91103a8feaSLen Brown 	unsigned long long extra_msr;	/* per thread */
92103a8feaSLen Brown 	int pkg;
93103a8feaSLen Brown 	int core;
94103a8feaSLen Brown 	int cpu;
95a829eb4dSLen Brown 	struct counters *next;
96a829eb4dSLen Brown };
97103a8feaSLen Brown 
98a829eb4dSLen Brown struct counters *cnt_even;
99a829eb4dSLen Brown struct counters *cnt_odd;
100a829eb4dSLen Brown struct counters *cnt_delta;
101a829eb4dSLen Brown struct counters *cnt_average;
102103a8feaSLen Brown struct timeval tv_even;
103103a8feaSLen Brown struct timeval tv_odd;
104103a8feaSLen Brown struct timeval tv_delta;
105103a8feaSLen Brown 
106d15cf7c1SLen Brown int mark_cpu_present(int pkg, int core, int cpu)
107d15cf7c1SLen Brown {
108d15cf7c1SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
109d15cf7c1SLen Brown 	return 0;
110d15cf7c1SLen Brown }
111d15cf7c1SLen Brown 
11288c3281fSLen Brown /*
11388c3281fSLen Brown  * cpu_mask_init(ncpus)
11488c3281fSLen Brown  *
11588c3281fSLen Brown  * allocate and clear cpu_mask
11688c3281fSLen Brown  * set cpu_mask_size
11788c3281fSLen Brown  */
11888c3281fSLen Brown void cpu_mask_init(int ncpus)
11988c3281fSLen Brown {
12088c3281fSLen Brown 	cpu_mask = CPU_ALLOC(ncpus);
12188c3281fSLen Brown 	if (cpu_mask == NULL) {
12288c3281fSLen Brown 		perror("CPU_ALLOC");
12388c3281fSLen Brown 		exit(3);
12488c3281fSLen Brown 	}
12588c3281fSLen Brown 	cpu_mask_size = CPU_ALLOC_SIZE(ncpus);
12688c3281fSLen Brown 	CPU_ZERO_S(cpu_mask_size, cpu_mask);
127d15cf7c1SLen Brown 
128d15cf7c1SLen Brown 	/*
129d15cf7c1SLen Brown 	 * Allocate and initialize cpu_present_set
130d15cf7c1SLen Brown 	 */
131d15cf7c1SLen Brown 	cpu_present_set = CPU_ALLOC(ncpus);
132d15cf7c1SLen Brown 	if (cpu_present_set == NULL) {
133d15cf7c1SLen Brown 		perror("CPU_ALLOC");
134d15cf7c1SLen Brown 		exit(3);
135d15cf7c1SLen Brown 	}
136d15cf7c1SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE(ncpus);
137d15cf7c1SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
138d15cf7c1SLen Brown 	for_all_cpus(mark_cpu_present);
13988c3281fSLen Brown }
14088c3281fSLen Brown 
14188c3281fSLen Brown void cpu_mask_uninit()
14288c3281fSLen Brown {
14388c3281fSLen Brown 	CPU_FREE(cpu_mask);
14488c3281fSLen Brown 	cpu_mask = NULL;
14588c3281fSLen Brown 	cpu_mask_size = 0;
146d15cf7c1SLen Brown 	CPU_FREE(cpu_present_set);
147d15cf7c1SLen Brown 	cpu_present_set = NULL;
148d15cf7c1SLen Brown 	cpu_present_setsize = 0;
14988c3281fSLen Brown }
15088c3281fSLen Brown 
15188c3281fSLen Brown int cpu_migrate(int cpu)
15288c3281fSLen Brown {
15388c3281fSLen Brown 	CPU_ZERO_S(cpu_mask_size, cpu_mask);
15488c3281fSLen Brown 	CPU_SET_S(cpu, cpu_mask_size, cpu_mask);
15588c3281fSLen Brown 	if (sched_setaffinity(0, cpu_mask_size, cpu_mask) == -1)
15688c3281fSLen Brown 		return -1;
15788c3281fSLen Brown 	else
15888c3281fSLen Brown 		return 0;
15988c3281fSLen Brown }
16088c3281fSLen Brown 
16115aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
162103a8feaSLen Brown {
163103a8feaSLen Brown 	ssize_t retval;
164103a8feaSLen Brown 	char pathname[32];
165103a8feaSLen Brown 	int fd;
166103a8feaSLen Brown 
167103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
168103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
16915aaa346SLen Brown 	if (fd < 0)
17015aaa346SLen Brown 		return -1;
171103a8feaSLen Brown 
17215aaa346SLen Brown 	retval = pread(fd, msr, sizeof *msr, offset);
173103a8feaSLen Brown 	close(fd);
17415aaa346SLen Brown 
17515aaa346SLen Brown 	if (retval != sizeof *msr)
17615aaa346SLen Brown 		return -1;
17715aaa346SLen Brown 
17815aaa346SLen Brown 	return 0;
179103a8feaSLen Brown }
180103a8feaSLen Brown 
181a829eb4dSLen Brown void print_header(void)
182103a8feaSLen Brown {
183103a8feaSLen Brown 	if (show_pkg)
184d30c4b7aSLen Brown 		fprintf(stderr, "pk");
185e23da037SLen Brown 	if (show_pkg)
186e23da037SLen Brown 		fprintf(stderr, " ");
187103a8feaSLen Brown 	if (show_core)
188e23da037SLen Brown 		fprintf(stderr, "cor");
189103a8feaSLen Brown 	if (show_cpu)
190103a8feaSLen Brown 		fprintf(stderr, " CPU");
191e23da037SLen Brown 	if (show_pkg || show_core || show_cpu)
192e23da037SLen Brown 		fprintf(stderr, " ");
193103a8feaSLen Brown 	if (do_nhm_cstates)
194103a8feaSLen Brown 		fprintf(stderr, "   %%c0");
195103a8feaSLen Brown 	if (has_aperf)
196103a8feaSLen Brown 		fprintf(stderr, "  GHz");
197103a8feaSLen Brown 	fprintf(stderr, "  TSC");
198103a8feaSLen Brown 	if (do_nhm_cstates)
199103a8feaSLen Brown 		fprintf(stderr, "    %%c1");
200103a8feaSLen Brown 	if (do_nhm_cstates)
201103a8feaSLen Brown 		fprintf(stderr, "    %%c3");
202103a8feaSLen Brown 	if (do_nhm_cstates)
203103a8feaSLen Brown 		fprintf(stderr, "    %%c6");
204103a8feaSLen Brown 	if (do_snb_cstates)
205103a8feaSLen Brown 		fprintf(stderr, "    %%c7");
206103a8feaSLen Brown 	if (do_snb_cstates)
207103a8feaSLen Brown 		fprintf(stderr, "   %%pc2");
208103a8feaSLen Brown 	if (do_nhm_cstates)
209103a8feaSLen Brown 		fprintf(stderr, "   %%pc3");
210103a8feaSLen Brown 	if (do_nhm_cstates)
211103a8feaSLen Brown 		fprintf(stderr, "   %%pc6");
212103a8feaSLen Brown 	if (do_snb_cstates)
213103a8feaSLen Brown 		fprintf(stderr, "   %%pc7");
214103a8feaSLen Brown 	if (extra_msr_offset)
215103a8feaSLen Brown 		fprintf(stderr, "        MSR 0x%x ", extra_msr_offset);
216103a8feaSLen Brown 
217103a8feaSLen Brown 	putc('\n', stderr);
218103a8feaSLen Brown }
219103a8feaSLen Brown 
220a829eb4dSLen Brown void dump_cnt(struct counters *cnt)
221103a8feaSLen Brown {
222aeae1e92SLen Brown 	if (!cnt)
223aeae1e92SLen Brown 		return;
224aeae1e92SLen Brown 	if (cnt->pkg) fprintf(stderr, "package: %d ", cnt->pkg);
225aeae1e92SLen Brown 	if (cnt->core) fprintf(stderr, "core:: %d ", cnt->core);
226aeae1e92SLen Brown 	if (cnt->cpu) fprintf(stderr, "CPU: %d ", cnt->cpu);
227aeae1e92SLen Brown 	if (cnt->tsc) fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
228aeae1e92SLen Brown 	if (cnt->c3) fprintf(stderr, "c3: %016llX\n", cnt->c3);
229aeae1e92SLen Brown 	if (cnt->c6) fprintf(stderr, "c6: %016llX\n", cnt->c6);
230aeae1e92SLen Brown 	if (cnt->c7) fprintf(stderr, "c7: %016llX\n", cnt->c7);
231aeae1e92SLen Brown 	if (cnt->aperf) fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
232aeae1e92SLen Brown 	if (cnt->pc2) fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
233aeae1e92SLen Brown 	if (cnt->pc3) fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
234aeae1e92SLen Brown 	if (cnt->pc6) fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
235aeae1e92SLen Brown 	if (cnt->pc7) fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
236aeae1e92SLen Brown 	if (cnt->extra_msr) fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
237103a8feaSLen Brown }
238103a8feaSLen Brown 
239a829eb4dSLen Brown void dump_list(struct counters *cnt)
240103a8feaSLen Brown {
241a829eb4dSLen Brown 	printf("dump_list 0x%p\n", cnt);
242103a8feaSLen Brown 
243a829eb4dSLen Brown 	for (; cnt; cnt = cnt->next)
244a829eb4dSLen Brown 		dump_cnt(cnt);
245103a8feaSLen Brown }
246103a8feaSLen Brown 
247e23da037SLen Brown /*
248e23da037SLen Brown  * column formatting convention & formats
249e23da037SLen Brown  * package: "pk" 2 columns %2d
250e23da037SLen Brown  * core: "cor" 3 columns %3d
251e23da037SLen Brown  * CPU: "CPU" 3 columns %3d
252e23da037SLen Brown  * GHz: "GHz" 3 columns %3.2
253e23da037SLen Brown  * TSC: "TSC" 3 columns %3.2
254e23da037SLen Brown  * percentage " %pc3" %6.2
255e23da037SLen Brown  */
256a829eb4dSLen Brown void print_cnt(struct counters *p)
257103a8feaSLen Brown {
258103a8feaSLen Brown 	double interval_float;
259103a8feaSLen Brown 
260103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
261103a8feaSLen Brown 
262103a8feaSLen Brown 	/* topology columns, print blanks on 1st (average) line */
263a829eb4dSLen Brown 	if (p == cnt_average) {
264103a8feaSLen Brown 		if (show_pkg)
265103a8feaSLen Brown 			fprintf(stderr, "  ");
266e23da037SLen Brown 		if (show_pkg && show_core)
267e23da037SLen Brown 			fprintf(stderr, " ");
268103a8feaSLen Brown 		if (show_core)
269103a8feaSLen Brown 			fprintf(stderr, "   ");
270103a8feaSLen Brown 		if (show_cpu)
271e23da037SLen Brown 			fprintf(stderr, " " "   ");
272103a8feaSLen Brown 	} else {
273103a8feaSLen Brown 		if (show_pkg)
274e23da037SLen Brown 			fprintf(stderr, "%2d", p->pkg);
275e23da037SLen Brown 		if (show_pkg && show_core)
276e23da037SLen Brown 			fprintf(stderr, " ");
277103a8feaSLen Brown 		if (show_core)
278e23da037SLen Brown 			fprintf(stderr, "%3d", p->core);
279103a8feaSLen Brown 		if (show_cpu)
280e23da037SLen Brown 			fprintf(stderr, " %3d", p->cpu);
281103a8feaSLen Brown 	}
282103a8feaSLen Brown 
283103a8feaSLen Brown 	/* %c0 */
284103a8feaSLen Brown 	if (do_nhm_cstates) {
285e23da037SLen Brown 		if (show_pkg || show_core || show_cpu)
286e23da037SLen Brown 			fprintf(stderr, " ");
287103a8feaSLen Brown 		if (!skip_c0)
288e23da037SLen Brown 			fprintf(stderr, "%6.2f", 100.0 * p->mperf/p->tsc);
289103a8feaSLen Brown 		else
290103a8feaSLen Brown 			fprintf(stderr, "  ****");
291103a8feaSLen Brown 	}
292103a8feaSLen Brown 
293103a8feaSLen Brown 	/* GHz */
294103a8feaSLen Brown 	if (has_aperf) {
295103a8feaSLen Brown 		if (!aperf_mperf_unstable) {
296e23da037SLen Brown 			fprintf(stderr, " %3.2f",
297103a8feaSLen Brown 				1.0 * p->tsc / units * p->aperf /
298103a8feaSLen Brown 				p->mperf / interval_float);
299103a8feaSLen Brown 		} else {
300103a8feaSLen Brown 			if (p->aperf > p->tsc || p->mperf > p->tsc) {
301e23da037SLen Brown 				fprintf(stderr, " ***");
302103a8feaSLen Brown 			} else {
303e23da037SLen Brown 				fprintf(stderr, "%3.1f*",
304103a8feaSLen Brown 					1.0 * p->tsc /
305103a8feaSLen Brown 					units * p->aperf /
306103a8feaSLen Brown 					p->mperf / interval_float);
307103a8feaSLen Brown 			}
308103a8feaSLen Brown 		}
309103a8feaSLen Brown 	}
310103a8feaSLen Brown 
311103a8feaSLen Brown 	/* TSC */
312103a8feaSLen Brown 	fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float);
313103a8feaSLen Brown 
314103a8feaSLen Brown 	if (do_nhm_cstates) {
315103a8feaSLen Brown 		if (!skip_c1)
316e23da037SLen Brown 			fprintf(stderr, " %6.2f", 100.0 * p->c1/p->tsc);
317103a8feaSLen Brown 		else
318103a8feaSLen Brown 			fprintf(stderr, "  ****");
319103a8feaSLen Brown 	}
320103a8feaSLen Brown 	if (do_nhm_cstates)
321d30c4b7aSLen Brown 		fprintf(stderr, " %6.2f", 100.0 * p->c3/p->tsc);
322103a8feaSLen Brown 	if (do_nhm_cstates)
323d30c4b7aSLen Brown 		fprintf(stderr, " %6.2f", 100.0 * p->c6/p->tsc);
324103a8feaSLen Brown 	if (do_snb_cstates)
325d30c4b7aSLen Brown 		fprintf(stderr, " %6.2f", 100.0 * p->c7/p->tsc);
326103a8feaSLen Brown 	if (do_snb_cstates)
327e23da037SLen Brown 		fprintf(stderr, " %6.2f", 100.0 * p->pc2/p->tsc);
328103a8feaSLen Brown 	if (do_nhm_cstates)
329e23da037SLen Brown 		fprintf(stderr, " %6.2f", 100.0 * p->pc3/p->tsc);
330103a8feaSLen Brown 	if (do_nhm_cstates)
331e23da037SLen Brown 		fprintf(stderr, " %6.2f", 100.0 * p->pc6/p->tsc);
332103a8feaSLen Brown 	if (do_snb_cstates)
333e23da037SLen Brown 		fprintf(stderr, " %6.2f", 100.0 * p->pc7/p->tsc);
334103a8feaSLen Brown 	if (extra_msr_offset)
335103a8feaSLen Brown 		fprintf(stderr, "  0x%016llx", p->extra_msr);
336103a8feaSLen Brown 	putc('\n', stderr);
337103a8feaSLen Brown }
338103a8feaSLen Brown 
339a829eb4dSLen Brown void print_counters(struct counters *counters)
340103a8feaSLen Brown {
341a829eb4dSLen Brown 	struct counters *cnt;
342e23da037SLen Brown 	static int printed;
343103a8feaSLen Brown 
344e23da037SLen Brown 
345e23da037SLen Brown 	if (!printed || !summary_only)
346103a8feaSLen Brown 		print_header();
347103a8feaSLen Brown 
348103a8feaSLen Brown 	if (num_cpus > 1)
349a829eb4dSLen Brown 		print_cnt(cnt_average);
350103a8feaSLen Brown 
351e23da037SLen Brown 	printed = 1;
352e23da037SLen Brown 
353e23da037SLen Brown 	if (summary_only)
354e23da037SLen Brown 		return;
355e23da037SLen Brown 
356a829eb4dSLen Brown 	for (cnt = counters; cnt != NULL; cnt = cnt->next)
357a829eb4dSLen Brown 		print_cnt(cnt);
358103a8feaSLen Brown 
359103a8feaSLen Brown }
360103a8feaSLen Brown 
361103a8feaSLen Brown #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
362103a8feaSLen Brown 
363a829eb4dSLen Brown int compute_delta(struct counters *after,
364a829eb4dSLen Brown 	struct counters *before, struct counters *delta)
365103a8feaSLen Brown {
366103a8feaSLen Brown 	int errors = 0;
367103a8feaSLen Brown 	int perf_err = 0;
368103a8feaSLen Brown 
369103a8feaSLen Brown 	skip_c0 = skip_c1 = 0;
370103a8feaSLen Brown 
371103a8feaSLen Brown 	for ( ; after && before && delta;
372103a8feaSLen Brown 		after = after->next, before = before->next, delta = delta->next) {
373103a8feaSLen Brown 		if (before->cpu != after->cpu) {
374103a8feaSLen Brown 			printf("cpu configuration changed: %d != %d\n",
375103a8feaSLen Brown 				before->cpu, after->cpu);
376103a8feaSLen Brown 			return -1;
377103a8feaSLen Brown 		}
378103a8feaSLen Brown 
379103a8feaSLen Brown 		if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) {
380103a8feaSLen Brown 			fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n",
381103a8feaSLen Brown 				before->cpu, before->tsc, after->tsc);
382103a8feaSLen Brown 			errors++;
383103a8feaSLen Brown 		}
384103a8feaSLen Brown 		/* check for TSC < 1 Mcycles over interval */
385103a8feaSLen Brown 		if (delta->tsc < (1000 * 1000)) {
386103a8feaSLen Brown 			fprintf(stderr, "Insanely slow TSC rate,"
387103a8feaSLen Brown 				" TSC stops in idle?\n");
388103a8feaSLen Brown 			fprintf(stderr, "You can disable all c-states"
389103a8feaSLen Brown 				" by booting with \"idle=poll\"\n");
390103a8feaSLen Brown 			fprintf(stderr, "or just the deep ones with"
391103a8feaSLen Brown 				" \"processor.max_cstate=1\"\n");
392103a8feaSLen Brown 			exit(-3);
393103a8feaSLen Brown 		}
394103a8feaSLen Brown 		if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) {
395103a8feaSLen Brown 			fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n",
396103a8feaSLen Brown 				before->cpu, before->c3, after->c3);
397103a8feaSLen Brown 			errors++;
398103a8feaSLen Brown 		}
399103a8feaSLen Brown 		if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) {
400103a8feaSLen Brown 			fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n",
401103a8feaSLen Brown 				before->cpu, before->c6, after->c6);
402103a8feaSLen Brown 			errors++;
403103a8feaSLen Brown 		}
404103a8feaSLen Brown 		if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) {
405103a8feaSLen Brown 			fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n",
406103a8feaSLen Brown 				before->cpu, before->c7, after->c7);
407103a8feaSLen Brown 			errors++;
408103a8feaSLen Brown 		}
409103a8feaSLen Brown 		if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) {
410103a8feaSLen Brown 			fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n",
411103a8feaSLen Brown 				before->cpu, before->pc2, after->pc2);
412103a8feaSLen Brown 			errors++;
413103a8feaSLen Brown 		}
414103a8feaSLen Brown 		if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) {
415103a8feaSLen Brown 			fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n",
416103a8feaSLen Brown 				before->cpu, before->pc3, after->pc3);
417103a8feaSLen Brown 			errors++;
418103a8feaSLen Brown 		}
419103a8feaSLen Brown 		if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) {
420103a8feaSLen Brown 			fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n",
421103a8feaSLen Brown 				before->cpu, before->pc6, after->pc6);
422103a8feaSLen Brown 			errors++;
423103a8feaSLen Brown 		}
424103a8feaSLen Brown 		if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) {
425103a8feaSLen Brown 			fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n",
426103a8feaSLen Brown 				before->cpu, before->pc7, after->pc7);
427103a8feaSLen Brown 			errors++;
428103a8feaSLen Brown 		}
429103a8feaSLen Brown 
430103a8feaSLen Brown 		perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
431103a8feaSLen Brown 		if (perf_err) {
432103a8feaSLen Brown 			fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
433103a8feaSLen Brown 				before->cpu, before->aperf, after->aperf);
434103a8feaSLen Brown 		}
435103a8feaSLen Brown 		perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
436103a8feaSLen Brown 		if (perf_err) {
437103a8feaSLen Brown 			fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
438103a8feaSLen Brown 				before->cpu, before->mperf, after->mperf);
439103a8feaSLen Brown 		}
440103a8feaSLen Brown 		if (perf_err) {
441103a8feaSLen Brown 			if (!aperf_mperf_unstable) {
442103a8feaSLen Brown 				fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
443103a8feaSLen Brown 				fprintf(stderr, "* Frequency results do not cover entire interval *\n");
444103a8feaSLen Brown 				fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
445103a8feaSLen Brown 
446103a8feaSLen Brown 				aperf_mperf_unstable = 1;
447103a8feaSLen Brown 			}
448103a8feaSLen Brown 			/*
449103a8feaSLen Brown 			 * mperf delta is likely a huge "positive" number
450103a8feaSLen Brown 			 * can not use it for calculating c0 time
451103a8feaSLen Brown 			 */
452103a8feaSLen Brown 			skip_c0 = 1;
453103a8feaSLen Brown 			skip_c1 = 1;
454103a8feaSLen Brown 		}
455103a8feaSLen Brown 
456103a8feaSLen Brown 		/*
457103a8feaSLen Brown 		 * As mperf and tsc collection are not atomic,
458103a8feaSLen Brown 		 * it is possible for mperf's non-halted cycles
459103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
460103a8feaSLen Brown 		 */
461103a8feaSLen Brown 		if (delta->mperf > delta->tsc)
462103a8feaSLen Brown 			delta->c1 = 0;
463103a8feaSLen Brown 		else /* normal case, derive c1 */
464103a8feaSLen Brown 			delta->c1 = delta->tsc - delta->mperf
465103a8feaSLen Brown 				- delta->c3 - delta->c6 - delta->c7;
466103a8feaSLen Brown 
467103a8feaSLen Brown 		if (delta->mperf == 0)
468103a8feaSLen Brown 			delta->mperf = 1;	/* divide by 0 protection */
469103a8feaSLen Brown 
470103a8feaSLen Brown 		/*
471103a8feaSLen Brown 		 * for "extra msr", just copy the latest w/o subtracting
472103a8feaSLen Brown 		 */
473103a8feaSLen Brown 		delta->extra_msr = after->extra_msr;
474103a8feaSLen Brown 		if (errors) {
475103a8feaSLen Brown 			fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
476a829eb4dSLen Brown 			dump_cnt(before);
477103a8feaSLen Brown 			fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
478a829eb4dSLen Brown 			dump_cnt(after);
479103a8feaSLen Brown 			errors = 0;
480103a8feaSLen Brown 		}
481103a8feaSLen Brown 	}
482103a8feaSLen Brown 	return 0;
483103a8feaSLen Brown }
484103a8feaSLen Brown 
485a829eb4dSLen Brown void compute_average(struct counters *delta, struct counters *avg)
486103a8feaSLen Brown {
487a829eb4dSLen Brown 	struct counters *sum;
488103a8feaSLen Brown 
489a829eb4dSLen Brown 	sum = calloc(1, sizeof(struct counters));
490103a8feaSLen Brown 	if (sum == NULL) {
491103a8feaSLen Brown 		perror("calloc sum");
492103a8feaSLen Brown 		exit(1);
493103a8feaSLen Brown 	}
494103a8feaSLen Brown 
495103a8feaSLen Brown 	for (; delta; delta = delta->next) {
496103a8feaSLen Brown 		sum->tsc += delta->tsc;
497103a8feaSLen Brown 		sum->c1 += delta->c1;
498103a8feaSLen Brown 		sum->c3 += delta->c3;
499103a8feaSLen Brown 		sum->c6 += delta->c6;
500103a8feaSLen Brown 		sum->c7 += delta->c7;
501103a8feaSLen Brown 		sum->aperf += delta->aperf;
502103a8feaSLen Brown 		sum->mperf += delta->mperf;
503103a8feaSLen Brown 		sum->pc2 += delta->pc2;
504103a8feaSLen Brown 		sum->pc3 += delta->pc3;
505103a8feaSLen Brown 		sum->pc6 += delta->pc6;
506103a8feaSLen Brown 		sum->pc7 += delta->pc7;
507103a8feaSLen Brown 	}
508103a8feaSLen Brown 	avg->tsc = sum->tsc/num_cpus;
509103a8feaSLen Brown 	avg->c1 = sum->c1/num_cpus;
510103a8feaSLen Brown 	avg->c3 = sum->c3/num_cpus;
511103a8feaSLen Brown 	avg->c6 = sum->c6/num_cpus;
512103a8feaSLen Brown 	avg->c7 = sum->c7/num_cpus;
513103a8feaSLen Brown 	avg->aperf = sum->aperf/num_cpus;
514103a8feaSLen Brown 	avg->mperf = sum->mperf/num_cpus;
515103a8feaSLen Brown 	avg->pc2 = sum->pc2/num_cpus;
516103a8feaSLen Brown 	avg->pc3 = sum->pc3/num_cpus;
517103a8feaSLen Brown 	avg->pc6 = sum->pc6/num_cpus;
518103a8feaSLen Brown 	avg->pc7 = sum->pc7/num_cpus;
519103a8feaSLen Brown 
520103a8feaSLen Brown 	free(sum);
521103a8feaSLen Brown }
522103a8feaSLen Brown 
52315aaa346SLen Brown int get_counters(struct counters *cnt)
524103a8feaSLen Brown {
525a829eb4dSLen Brown 	for ( ; cnt; cnt = cnt->next) {
52615aaa346SLen Brown 
52715aaa346SLen Brown 		if (cpu_migrate(cnt->cpu))
52815aaa346SLen Brown 			return -1;
52915aaa346SLen Brown 
53015aaa346SLen Brown 		if (get_msr(cnt->cpu, MSR_TSC, &cnt->tsc))
53115aaa346SLen Brown 			return -1;
53215aaa346SLen Brown 
53315aaa346SLen Brown 		if (has_aperf) {
53415aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_APERF, &cnt->aperf))
53515aaa346SLen Brown 				return -1;
53615aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_MPERF, &cnt->mperf))
53715aaa346SLen Brown 				return -1;
53888c3281fSLen Brown 		}
53988c3281fSLen Brown 
54015aaa346SLen Brown 		if (do_nhm_cstates) {
54115aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY, &cnt->c3))
54215aaa346SLen Brown 				return -1;
54315aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY, &cnt->c6))
54415aaa346SLen Brown 				return -1;
545103a8feaSLen Brown 		}
54615aaa346SLen Brown 
54715aaa346SLen Brown 		if (do_snb_cstates)
54815aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY, &cnt->c7))
54915aaa346SLen Brown 				return -1;
55015aaa346SLen Brown 
55115aaa346SLen Brown 		if (do_nhm_cstates) {
55215aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY, &cnt->pc3))
55315aaa346SLen Brown 				return -1;
55415aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY, &cnt->pc6))
55515aaa346SLen Brown 				return -1;
55615aaa346SLen Brown 		}
55715aaa346SLen Brown 		if (do_snb_cstates) {
55815aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY, &cnt->pc2))
55915aaa346SLen Brown 				return -1;
56015aaa346SLen Brown 			if (get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY, &cnt->pc7))
56115aaa346SLen Brown 				return -1;
56215aaa346SLen Brown 		}
56315aaa346SLen Brown 		if (extra_msr_offset)
56415aaa346SLen Brown 			if (get_msr(cnt->cpu, extra_msr_offset, &cnt->extra_msr))
56515aaa346SLen Brown 				return -1;
56615aaa346SLen Brown 	}
56715aaa346SLen Brown 	return 0;
568103a8feaSLen Brown }
569103a8feaSLen Brown 
570a829eb4dSLen Brown void print_nehalem_info(void)
571103a8feaSLen Brown {
572103a8feaSLen Brown 	unsigned long long msr;
573103a8feaSLen Brown 	unsigned int ratio;
574103a8feaSLen Brown 
575103a8feaSLen Brown 	if (!do_nehalem_platform_info)
576103a8feaSLen Brown 		return;
577103a8feaSLen Brown 
57815aaa346SLen Brown 	get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
579103a8feaSLen Brown 
580103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
581103a8feaSLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
582103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
583103a8feaSLen Brown 
584103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
585103a8feaSLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
586103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
587103a8feaSLen Brown 
588103a8feaSLen Brown 	if (verbose > 1)
589103a8feaSLen Brown 		fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
590103a8feaSLen Brown 
591103a8feaSLen Brown 	if (!do_nehalem_turbo_ratio_limit)
592103a8feaSLen Brown 		return;
593103a8feaSLen Brown 
59415aaa346SLen Brown 	get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
595103a8feaSLen Brown 
596103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
597103a8feaSLen Brown 	if (ratio)
598103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
599103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
600103a8feaSLen Brown 
601103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
602103a8feaSLen Brown 	if (ratio)
603103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
604103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
605103a8feaSLen Brown 
606103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
607103a8feaSLen Brown 	if (ratio)
608103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
609103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
610103a8feaSLen Brown 
611103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
612103a8feaSLen Brown 	if (ratio)
613103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
614103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
615103a8feaSLen Brown 
616103a8feaSLen Brown }
617103a8feaSLen Brown 
618a829eb4dSLen Brown void free_counter_list(struct counters *list)
619103a8feaSLen Brown {
620a829eb4dSLen Brown 	struct counters *p;
621103a8feaSLen Brown 
622103a8feaSLen Brown 	for (p = list; p; ) {
623a829eb4dSLen Brown 		struct counters *free_me;
624103a8feaSLen Brown 
625103a8feaSLen Brown 		free_me = p;
626103a8feaSLen Brown 		p = p->next;
627103a8feaSLen Brown 		free(free_me);
628103a8feaSLen Brown 	}
629103a8feaSLen Brown }
630103a8feaSLen Brown 
631103a8feaSLen Brown void free_all_counters(void)
632103a8feaSLen Brown {
633a829eb4dSLen Brown 	free_counter_list(cnt_even);
634a829eb4dSLen Brown 	cnt_even = NULL;
635103a8feaSLen Brown 
636a829eb4dSLen Brown 	free_counter_list(cnt_odd);
637a829eb4dSLen Brown 	cnt_odd = NULL;
638103a8feaSLen Brown 
639a829eb4dSLen Brown 	free_counter_list(cnt_delta);
640a829eb4dSLen Brown 	cnt_delta = NULL;
641103a8feaSLen Brown 
642a829eb4dSLen Brown 	free_counter_list(cnt_average);
643a829eb4dSLen Brown 	cnt_average = NULL;
644103a8feaSLen Brown }
645103a8feaSLen Brown 
646a829eb4dSLen Brown void insert_counters(struct counters **list,
647a829eb4dSLen Brown 	struct counters *new)
648103a8feaSLen Brown {
649a829eb4dSLen Brown 	struct counters *prev;
650103a8feaSLen Brown 
651103a8feaSLen Brown 	/*
652103a8feaSLen Brown 	 * list was empty
653103a8feaSLen Brown 	 */
654103a8feaSLen Brown 	if (*list == NULL) {
655103a8feaSLen Brown 		new->next = *list;
656103a8feaSLen Brown 		*list = new;
657103a8feaSLen Brown 		return;
658103a8feaSLen Brown 	}
659103a8feaSLen Brown 
660e23da037SLen Brown 	if (!summary_only)
661103a8feaSLen Brown 		show_cpu = 1;	/* there is more than one CPU */
662103a8feaSLen Brown 
663103a8feaSLen Brown 	/*
664103a8feaSLen Brown 	 * insert on front of list.
665103a8feaSLen Brown 	 * It is sorted by ascending package#, core#, cpu#
666103a8feaSLen Brown 	 */
667103a8feaSLen Brown 	if (((*list)->pkg > new->pkg) ||
668103a8feaSLen Brown 	    (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
669103a8feaSLen Brown 	    (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
670103a8feaSLen Brown 		new->next = *list;
671103a8feaSLen Brown 		*list = new;
672103a8feaSLen Brown 		return;
673103a8feaSLen Brown 	}
674103a8feaSLen Brown 
675103a8feaSLen Brown 	prev = *list;
676103a8feaSLen Brown 
677103a8feaSLen Brown 	while (prev->next && (prev->next->pkg < new->pkg)) {
678103a8feaSLen Brown 		prev = prev->next;
679e23da037SLen Brown 		if (!summary_only)
680103a8feaSLen Brown 			show_pkg = 1;	/* there is more than 1 package */
681103a8feaSLen Brown 	}
682103a8feaSLen Brown 
683103a8feaSLen Brown 	while (prev->next && (prev->next->pkg == new->pkg)
684103a8feaSLen Brown 		&& (prev->next->core < new->core)) {
685103a8feaSLen Brown 		prev = prev->next;
686e23da037SLen Brown 		if (!summary_only)
687103a8feaSLen Brown 			show_core = 1;	/* there is more than 1 core */
688103a8feaSLen Brown 	}
689103a8feaSLen Brown 
690103a8feaSLen Brown 	while (prev->next && (prev->next->pkg == new->pkg)
691103a8feaSLen Brown 		&& (prev->next->core == new->core)
692103a8feaSLen Brown 		&& (prev->next->cpu < new->cpu)) {
693103a8feaSLen Brown 		prev = prev->next;
694103a8feaSLen Brown 	}
695103a8feaSLen Brown 
696103a8feaSLen Brown 	/*
697103a8feaSLen Brown 	 * insert after "prev"
698103a8feaSLen Brown 	 */
699103a8feaSLen Brown 	new->next = prev->next;
700103a8feaSLen Brown 	prev->next = new;
701103a8feaSLen Brown }
702103a8feaSLen Brown 
703a829eb4dSLen Brown void alloc_new_counters(int pkg, int core, int cpu)
704103a8feaSLen Brown {
705a829eb4dSLen Brown 	struct counters *new;
706103a8feaSLen Brown 
707103a8feaSLen Brown 	if (verbose > 1)
708103a8feaSLen Brown 		printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
709103a8feaSLen Brown 
710a829eb4dSLen Brown 	new = (struct counters *)calloc(1, sizeof(struct counters));
711103a8feaSLen Brown 	if (new == NULL) {
712103a8feaSLen Brown 		perror("calloc");
713103a8feaSLen Brown 		exit(1);
714103a8feaSLen Brown 	}
715103a8feaSLen Brown 	new->pkg = pkg;
716103a8feaSLen Brown 	new->core = core;
717103a8feaSLen Brown 	new->cpu = cpu;
718a829eb4dSLen Brown 	insert_counters(&cnt_odd, new);
719103a8feaSLen Brown 
720a829eb4dSLen Brown 	new = (struct counters *)calloc(1,
721a829eb4dSLen Brown 		sizeof(struct counters));
722103a8feaSLen Brown 	if (new == NULL) {
723103a8feaSLen Brown 		perror("calloc");
724103a8feaSLen Brown 		exit(1);
725103a8feaSLen Brown 	}
726103a8feaSLen Brown 	new->pkg = pkg;
727103a8feaSLen Brown 	new->core = core;
728103a8feaSLen Brown 	new->cpu = cpu;
729a829eb4dSLen Brown 	insert_counters(&cnt_even, new);
730103a8feaSLen Brown 
731a829eb4dSLen Brown 	new = (struct counters *)calloc(1, sizeof(struct counters));
732103a8feaSLen Brown 	if (new == NULL) {
733103a8feaSLen Brown 		perror("calloc");
734103a8feaSLen Brown 		exit(1);
735103a8feaSLen Brown 	}
736103a8feaSLen Brown 	new->pkg = pkg;
737103a8feaSLen Brown 	new->core = core;
738103a8feaSLen Brown 	new->cpu = cpu;
739a829eb4dSLen Brown 	insert_counters(&cnt_delta, new);
740103a8feaSLen Brown 
741a829eb4dSLen Brown 	new = (struct counters *)calloc(1, sizeof(struct counters));
742103a8feaSLen Brown 	if (new == NULL) {
743103a8feaSLen Brown 		perror("calloc");
744103a8feaSLen Brown 		exit(1);
745103a8feaSLen Brown 	}
746103a8feaSLen Brown 	new->pkg = pkg;
747103a8feaSLen Brown 	new->core = core;
748103a8feaSLen Brown 	new->cpu = cpu;
749a829eb4dSLen Brown 	cnt_average = new;
750103a8feaSLen Brown }
751103a8feaSLen Brown 
752103a8feaSLen Brown int get_physical_package_id(int cpu)
753103a8feaSLen Brown {
754103a8feaSLen Brown 	char path[64];
755103a8feaSLen Brown 	FILE *filep;
756103a8feaSLen Brown 	int pkg;
757103a8feaSLen Brown 
758103a8feaSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
759103a8feaSLen Brown 	filep = fopen(path, "r");
760103a8feaSLen Brown 	if (filep == NULL) {
761103a8feaSLen Brown 		perror(path);
762103a8feaSLen Brown 		exit(1);
763103a8feaSLen Brown 	}
764103a8feaSLen Brown 	fscanf(filep, "%d", &pkg);
765103a8feaSLen Brown 	fclose(filep);
766103a8feaSLen Brown 	return pkg;
767103a8feaSLen Brown }
768103a8feaSLen Brown 
769103a8feaSLen Brown int get_core_id(int cpu)
770103a8feaSLen Brown {
771103a8feaSLen Brown 	char path[64];
772103a8feaSLen Brown 	FILE *filep;
773103a8feaSLen Brown 	int core;
774103a8feaSLen Brown 
775103a8feaSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
776103a8feaSLen Brown 	filep = fopen(path, "r");
777103a8feaSLen Brown 	if (filep == NULL) {
778103a8feaSLen Brown 		perror(path);
779103a8feaSLen Brown 		exit(1);
780103a8feaSLen Brown 	}
781103a8feaSLen Brown 	fscanf(filep, "%d", &core);
782103a8feaSLen Brown 	fclose(filep);
783103a8feaSLen Brown 	return core;
784103a8feaSLen Brown }
785103a8feaSLen Brown 
786103a8feaSLen Brown /*
78715aaa346SLen Brown  * run func(pkg, core, cpu) on every cpu in /proc/stat
788103a8feaSLen Brown  */
789103a8feaSLen Brown 
790103a8feaSLen Brown int for_all_cpus(void (func)(int, int, int))
791103a8feaSLen Brown {
792103a8feaSLen Brown 	FILE *fp;
793103a8feaSLen Brown 	int cpu_count;
794103a8feaSLen Brown 	int retval;
795103a8feaSLen Brown 
796103a8feaSLen Brown 	fp = fopen(proc_stat, "r");
797103a8feaSLen Brown 	if (fp == NULL) {
798103a8feaSLen Brown 		perror(proc_stat);
799103a8feaSLen Brown 		exit(1);
800103a8feaSLen Brown 	}
801103a8feaSLen Brown 
802103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
803103a8feaSLen Brown 	if (retval != 0) {
804103a8feaSLen Brown 		perror("/proc/stat format");
805103a8feaSLen Brown 		exit(1);
806103a8feaSLen Brown 	}
807103a8feaSLen Brown 
808103a8feaSLen Brown 	for (cpu_count = 0; ; cpu_count++) {
809103a8feaSLen Brown 		int cpu;
810103a8feaSLen Brown 
811103a8feaSLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
812103a8feaSLen Brown 		if (retval != 1)
813103a8feaSLen Brown 			break;
814103a8feaSLen Brown 
815103a8feaSLen Brown 		func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
816103a8feaSLen Brown 	}
817103a8feaSLen Brown 	fclose(fp);
818103a8feaSLen Brown 	return cpu_count;
819103a8feaSLen Brown }
820103a8feaSLen Brown 
821103a8feaSLen Brown void re_initialize(void)
822103a8feaSLen Brown {
823103a8feaSLen Brown 	free_all_counters();
824a829eb4dSLen Brown 	num_cpus = for_all_cpus(alloc_new_counters);
82588c3281fSLen Brown 	cpu_mask_uninit();
82688c3281fSLen Brown 	cpu_mask_init(num_cpus);
82715aaa346SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", num_cpus);
828103a8feaSLen Brown }
829103a8feaSLen Brown 
830103a8feaSLen Brown void dummy(int pkg, int core, int cpu) { return; }
831103a8feaSLen Brown /*
832103a8feaSLen Brown  * check to see if a cpu came on-line
833103a8feaSLen Brown  */
83415aaa346SLen Brown int verify_num_cpus(void)
835103a8feaSLen Brown {
836103a8feaSLen Brown 	int new_num_cpus;
837103a8feaSLen Brown 
838103a8feaSLen Brown 	new_num_cpus = for_all_cpus(dummy);
839103a8feaSLen Brown 
840103a8feaSLen Brown 	if (new_num_cpus != num_cpus) {
841103a8feaSLen Brown 		if (verbose)
842103a8feaSLen Brown 			printf("num_cpus was %d, is now  %d\n",
843103a8feaSLen Brown 				num_cpus, new_num_cpus);
84415aaa346SLen Brown 		return -1;
845103a8feaSLen Brown 	}
84615aaa346SLen Brown 	return 0;
847103a8feaSLen Brown }
848103a8feaSLen Brown 
849103a8feaSLen Brown void turbostat_loop()
850103a8feaSLen Brown {
851103a8feaSLen Brown restart:
852a829eb4dSLen Brown 	get_counters(cnt_even);
853103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
854103a8feaSLen Brown 
855103a8feaSLen Brown 	while (1) {
85615aaa346SLen Brown 		if (verify_num_cpus()) {
857103a8feaSLen Brown 			re_initialize();
858103a8feaSLen Brown 			goto restart;
859103a8feaSLen Brown 		}
860103a8feaSLen Brown 		sleep(interval_sec);
86115aaa346SLen Brown 		if (get_counters(cnt_odd)) {
86215aaa346SLen Brown 			re_initialize();
86315aaa346SLen Brown 			goto restart;
86415aaa346SLen Brown 		}
865103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
866a829eb4dSLen Brown 		compute_delta(cnt_odd, cnt_even, cnt_delta);
867103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
868a829eb4dSLen Brown 		compute_average(cnt_delta, cnt_average);
869a829eb4dSLen Brown 		print_counters(cnt_delta);
87015aaa346SLen Brown 		sleep(interval_sec);
87115aaa346SLen Brown 		if (get_counters(cnt_even)) {
872103a8feaSLen Brown 			re_initialize();
873103a8feaSLen Brown 			goto restart;
874103a8feaSLen Brown 		}
875103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
876a829eb4dSLen Brown 		compute_delta(cnt_even, cnt_odd, cnt_delta);
877103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
878a829eb4dSLen Brown 		compute_average(cnt_delta, cnt_average);
879a829eb4dSLen Brown 		print_counters(cnt_delta);
880103a8feaSLen Brown 	}
881103a8feaSLen Brown }
882103a8feaSLen Brown 
883103a8feaSLen Brown void check_dev_msr()
884103a8feaSLen Brown {
885103a8feaSLen Brown 	struct stat sb;
886103a8feaSLen Brown 
887103a8feaSLen Brown 	if (stat("/dev/cpu/0/msr", &sb)) {
888103a8feaSLen Brown 		fprintf(stderr, "no /dev/cpu/0/msr\n");
889103a8feaSLen Brown 		fprintf(stderr, "Try \"# modprobe msr\"\n");
890103a8feaSLen Brown 		exit(-5);
891103a8feaSLen Brown 	}
892103a8feaSLen Brown }
893103a8feaSLen Brown 
894103a8feaSLen Brown void check_super_user()
895103a8feaSLen Brown {
896103a8feaSLen Brown 	if (getuid() != 0) {
897103a8feaSLen Brown 		fprintf(stderr, "must be root\n");
898103a8feaSLen Brown 		exit(-6);
899103a8feaSLen Brown 	}
900103a8feaSLen Brown }
901103a8feaSLen Brown 
902103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
903103a8feaSLen Brown {
904103a8feaSLen Brown 	if (!genuine_intel)
905103a8feaSLen Brown 		return 0;
906103a8feaSLen Brown 
907103a8feaSLen Brown 	if (family != 6)
908103a8feaSLen Brown 		return 0;
909103a8feaSLen Brown 
910103a8feaSLen Brown 	switch (model) {
911103a8feaSLen Brown 	case 0x1A:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
912103a8feaSLen Brown 	case 0x1E:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
913103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
914103a8feaSLen Brown 	case 0x25:	/* Westmere Client - Clarkdale, Arrandale */
915103a8feaSLen Brown 	case 0x2C:	/* Westmere EP - Gulftown */
916103a8feaSLen Brown 	case 0x2A:	/* SNB */
917103a8feaSLen Brown 	case 0x2D:	/* SNB Xeon */
918553575f1SLen Brown 	case 0x3A:	/* IVB */
919553575f1SLen Brown 	case 0x3D:	/* IVB Xeon */
920103a8feaSLen Brown 		return 1;
921103a8feaSLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
922103a8feaSLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
923103a8feaSLen Brown 	default:
924103a8feaSLen Brown 		return 0;
925103a8feaSLen Brown 	}
926103a8feaSLen Brown }
927103a8feaSLen Brown 
928103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model)
929103a8feaSLen Brown {
930103a8feaSLen Brown 	if (!genuine_intel)
931103a8feaSLen Brown 		return 0;
932103a8feaSLen Brown 
933103a8feaSLen Brown 	switch (model) {
934103a8feaSLen Brown 	case 0x2A:
935103a8feaSLen Brown 	case 0x2D:
936103a8feaSLen Brown 		return 1;
937103a8feaSLen Brown 	}
938103a8feaSLen Brown 	return 0;
939103a8feaSLen Brown }
940103a8feaSLen Brown 
941103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
942103a8feaSLen Brown {
943103a8feaSLen Brown 	if (is_snb(family, model))
944103a8feaSLen Brown 		return 100.00;
945103a8feaSLen Brown 	else
946103a8feaSLen Brown 		return 133.33;
947103a8feaSLen Brown }
948103a8feaSLen Brown 
949103a8feaSLen Brown void check_cpuid()
950103a8feaSLen Brown {
951103a8feaSLen Brown 	unsigned int eax, ebx, ecx, edx, max_level;
952103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
953103a8feaSLen Brown 
954103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
955103a8feaSLen Brown 
956103a8feaSLen Brown 	asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
957103a8feaSLen Brown 
958103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
959103a8feaSLen Brown 		genuine_intel = 1;
960103a8feaSLen Brown 
961103a8feaSLen Brown 	if (verbose)
962103a8feaSLen Brown 		fprintf(stderr, "%.4s%.4s%.4s ",
963103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
964103a8feaSLen Brown 
965103a8feaSLen Brown 	asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
966103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
967103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
968103a8feaSLen Brown 	stepping = fms & 0xf;
969103a8feaSLen Brown 	if (family == 6 || family == 0xf)
970103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
971103a8feaSLen Brown 
972103a8feaSLen Brown 	if (verbose)
973103a8feaSLen Brown 		fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
974103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
975103a8feaSLen Brown 
976103a8feaSLen Brown 	if (!(edx & (1 << 5))) {
977103a8feaSLen Brown 		fprintf(stderr, "CPUID: no MSR\n");
978103a8feaSLen Brown 		exit(1);
979103a8feaSLen Brown 	}
980103a8feaSLen Brown 
981103a8feaSLen Brown 	/*
982103a8feaSLen Brown 	 * check max extended function levels of CPUID.
983103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
984103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
985103a8feaSLen Brown 	 */
986103a8feaSLen Brown 	ebx = ecx = edx = 0;
987103a8feaSLen Brown 	asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
988103a8feaSLen Brown 
989103a8feaSLen Brown 	if (max_level < 0x80000007) {
990103a8feaSLen Brown 		fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
991103a8feaSLen Brown 		exit(1);
992103a8feaSLen Brown 	}
993103a8feaSLen Brown 
994103a8feaSLen Brown 	/*
995103a8feaSLen Brown 	 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
996103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
997103a8feaSLen Brown 	 */
998103a8feaSLen Brown 	asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
9998209e054SThomas Renninger 	has_invariant_tsc = edx & (1 << 8);
1000103a8feaSLen Brown 
1001103a8feaSLen Brown 	if (!has_invariant_tsc) {
1002103a8feaSLen Brown 		fprintf(stderr, "No invariant TSC\n");
1003103a8feaSLen Brown 		exit(1);
1004103a8feaSLen Brown 	}
1005103a8feaSLen Brown 
1006103a8feaSLen Brown 	/*
1007103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1008103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
1009103a8feaSLen Brown 	 */
1010103a8feaSLen Brown 
1011103a8feaSLen Brown 	asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
10128209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
1013103a8feaSLen Brown 	if (!has_aperf) {
1014103a8feaSLen Brown 		fprintf(stderr, "No APERF MSR\n");
1015103a8feaSLen Brown 		exit(1);
1016103a8feaSLen Brown 	}
1017103a8feaSLen Brown 
1018103a8feaSLen Brown 	do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1019103a8feaSLen Brown 	do_nhm_cstates = genuine_intel;	/* all Intel w/ non-stop TSC have NHM counters */
1020103a8feaSLen Brown 	do_snb_cstates = is_snb(family, model);
1021103a8feaSLen Brown 	bclk = discover_bclk(family, model);
1022103a8feaSLen Brown 
1023103a8feaSLen Brown 	do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1024103a8feaSLen Brown }
1025103a8feaSLen Brown 
1026103a8feaSLen Brown 
1027103a8feaSLen Brown void usage()
1028103a8feaSLen Brown {
1029103a8feaSLen Brown 	fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
1030103a8feaSLen Brown 		progname);
1031103a8feaSLen Brown 	exit(1);
1032103a8feaSLen Brown }
1033103a8feaSLen Brown 
1034103a8feaSLen Brown 
1035103a8feaSLen Brown /*
1036103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
1037103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
1038103a8feaSLen Brown  */
1039103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
1040103a8feaSLen Brown {
1041103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
1042103a8feaSLen Brown 		return 1;
1043103a8feaSLen Brown 	else
1044103a8feaSLen Brown 		return 0;
1045103a8feaSLen Brown }
1046103a8feaSLen Brown 
1047103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
1048103a8feaSLen Brown {
1049103a8feaSLen Brown 	return 0;
1050103a8feaSLen Brown }
1051103a8feaSLen Brown 
1052103a8feaSLen Brown void turbostat_init()
1053103a8feaSLen Brown {
1054103a8feaSLen Brown 	check_cpuid();
1055103a8feaSLen Brown 
1056103a8feaSLen Brown 	check_dev_msr();
1057103a8feaSLen Brown 	check_super_user();
1058103a8feaSLen Brown 
1059a829eb4dSLen Brown 	num_cpus = for_all_cpus(alloc_new_counters);
106088c3281fSLen Brown 	cpu_mask_init(num_cpus);
1061103a8feaSLen Brown 
1062103a8feaSLen Brown 	if (verbose)
1063103a8feaSLen Brown 		print_nehalem_info();
1064103a8feaSLen Brown }
1065103a8feaSLen Brown 
1066103a8feaSLen Brown int fork_it(char **argv)
1067103a8feaSLen Brown {
1068103a8feaSLen Brown 	int retval;
1069103a8feaSLen Brown 	pid_t child_pid;
1070a829eb4dSLen Brown 	get_counters(cnt_even);
1071d15cf7c1SLen Brown 
1072d15cf7c1SLen Brown         /* clear affinity side-effect of get_counters() */
1073d15cf7c1SLen Brown         sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
1074103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
1075103a8feaSLen Brown 
1076103a8feaSLen Brown 	child_pid = fork();
1077103a8feaSLen Brown 	if (!child_pid) {
1078103a8feaSLen Brown 		/* child */
1079103a8feaSLen Brown 		execvp(argv[0], argv);
1080103a8feaSLen Brown 	} else {
1081103a8feaSLen Brown 		int status;
1082103a8feaSLen Brown 
1083103a8feaSLen Brown 		/* parent */
1084103a8feaSLen Brown 		if (child_pid == -1) {
1085103a8feaSLen Brown 			perror("fork");
1086103a8feaSLen Brown 			exit(1);
1087103a8feaSLen Brown 		}
1088103a8feaSLen Brown 
1089103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
1090103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
1091103a8feaSLen Brown 		if (waitpid(child_pid, &status, 0) == -1) {
1092103a8feaSLen Brown 			perror("wait");
1093103a8feaSLen Brown 			exit(1);
1094103a8feaSLen Brown 		}
1095103a8feaSLen Brown 	}
1096a829eb4dSLen Brown 	get_counters(cnt_odd);
1097103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
1098a829eb4dSLen Brown 	retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
1099103a8feaSLen Brown 
1100103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
1101a829eb4dSLen Brown 	compute_average(cnt_delta, cnt_average);
1102103a8feaSLen Brown 	if (!retval)
1103a829eb4dSLen Brown 		print_counters(cnt_delta);
1104103a8feaSLen Brown 
11056eab04a8SJustin P. Mattock 	fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
1106103a8feaSLen Brown 
1107103a8feaSLen Brown 	return 0;
1108103a8feaSLen Brown }
1109103a8feaSLen Brown 
1110103a8feaSLen Brown void cmdline(int argc, char **argv)
1111103a8feaSLen Brown {
1112103a8feaSLen Brown 	int opt;
1113103a8feaSLen Brown 
1114103a8feaSLen Brown 	progname = argv[0];
1115103a8feaSLen Brown 
1116e23da037SLen Brown 	while ((opt = getopt(argc, argv, "+svi:M:")) != -1) {
1117103a8feaSLen Brown 		switch (opt) {
1118e23da037SLen Brown 		case 's':
1119e23da037SLen Brown 			summary_only++;
1120e23da037SLen Brown 			break;
1121103a8feaSLen Brown 		case 'v':
1122103a8feaSLen Brown 			verbose++;
1123103a8feaSLen Brown 			break;
1124103a8feaSLen Brown 		case 'i':
1125103a8feaSLen Brown 			interval_sec = atoi(optarg);
1126103a8feaSLen Brown 			break;
1127103a8feaSLen Brown 		case 'M':
1128103a8feaSLen Brown 			sscanf(optarg, "%x", &extra_msr_offset);
1129103a8feaSLen Brown 			if (verbose > 1)
1130103a8feaSLen Brown 				fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1131103a8feaSLen Brown 			break;
1132103a8feaSLen Brown 		default:
1133103a8feaSLen Brown 			usage();
1134103a8feaSLen Brown 		}
1135103a8feaSLen Brown 	}
1136103a8feaSLen Brown }
1137103a8feaSLen Brown 
1138103a8feaSLen Brown int main(int argc, char **argv)
1139103a8feaSLen Brown {
1140103a8feaSLen Brown 	cmdline(argc, argv);
1141103a8feaSLen Brown 
1142103a8feaSLen Brown 	if (verbose > 1)
1143103a8feaSLen Brown 		fprintf(stderr, "turbostat Dec 6, 2010"
1144103a8feaSLen Brown 			" - Len Brown <lenb@kernel.org>\n");
1145103a8feaSLen Brown 	if (verbose > 1)
1146103a8feaSLen Brown 		fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n");
1147103a8feaSLen Brown 
1148103a8feaSLen Brown 	turbostat_init();
1149103a8feaSLen Brown 
1150103a8feaSLen Brown 	/*
1151103a8feaSLen Brown 	 * if any params left, it must be a command to fork
1152103a8feaSLen Brown 	 */
1153103a8feaSLen Brown 	if (argc - optind)
1154103a8feaSLen Brown 		return fork_it(argv + optind);
1155103a8feaSLen Brown 	else
1156103a8feaSLen Brown 		turbostat_loop();
1157103a8feaSLen Brown 
1158103a8feaSLen Brown 	return 0;
1159103a8feaSLen Brown }
1160