xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 8e180f3cb6b7510a3bdf14e16ce87c9f5d86f102)
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_NEHALEM_PLATFORM_INFO	0xCE
39103a8feaSLen Brown #define MSR_NEHALEM_TURBO_RATIO_LIMIT	0x1AD
406574a5d5SLen Brown #define MSR_IVT_TURBO_RATIO_LIMIT	0x1AE
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;
656574a5d5SLen Brown unsigned int do_ivt_turbo_ratio_limit;
662f32edf1SLen Brown unsigned int extra_msr_offset32;
672f32edf1SLen Brown unsigned int extra_msr_offset64;
68*8e180f3cSLen Brown unsigned int extra_delta_offset32;
69*8e180f3cSLen Brown unsigned int extra_delta_offset64;
70103a8feaSLen Brown double bclk;
71103a8feaSLen Brown unsigned int show_pkg;
72103a8feaSLen Brown unsigned int show_core;
73103a8feaSLen Brown unsigned int show_cpu;
74c98d5d94SLen Brown unsigned int show_pkg_only;
75c98d5d94SLen Brown unsigned int show_core_only;
76c98d5d94SLen Brown char *output_buffer, *outp;
77103a8feaSLen Brown 
78103a8feaSLen Brown int aperf_mperf_unstable;
79103a8feaSLen Brown int backwards_count;
80103a8feaSLen Brown char *progname;
81103a8feaSLen Brown 
82c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set;
83c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize;
84c98d5d94SLen Brown 
85c98d5d94SLen Brown struct thread_data {
86c98d5d94SLen Brown 	unsigned long long tsc;
87c98d5d94SLen Brown 	unsigned long long aperf;
88c98d5d94SLen Brown 	unsigned long long mperf;
89c98d5d94SLen Brown 	unsigned long long c1;	/* derived */
902f32edf1SLen Brown 	unsigned long long extra_msr64;
91*8e180f3cSLen Brown 	unsigned long long extra_delta64;
92*8e180f3cSLen Brown 	unsigned long long extra_msr32;
93*8e180f3cSLen Brown 	unsigned long long extra_delta32;
94c98d5d94SLen Brown 	unsigned int cpu_id;
95c98d5d94SLen Brown 	unsigned int flags;
96c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
97c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
98c98d5d94SLen Brown } *thread_even, *thread_odd;
99c98d5d94SLen Brown 
100c98d5d94SLen Brown struct core_data {
101c98d5d94SLen Brown 	unsigned long long c3;
102c98d5d94SLen Brown 	unsigned long long c6;
103c98d5d94SLen Brown 	unsigned long long c7;
104c98d5d94SLen Brown 	unsigned int core_id;
105c98d5d94SLen Brown } *core_even, *core_odd;
106c98d5d94SLen Brown 
107c98d5d94SLen Brown struct pkg_data {
108c98d5d94SLen Brown 	unsigned long long pc2;
109c98d5d94SLen Brown 	unsigned long long pc3;
110c98d5d94SLen Brown 	unsigned long long pc6;
111c98d5d94SLen Brown 	unsigned long long pc7;
112c98d5d94SLen Brown 	unsigned int package_id;
113c98d5d94SLen Brown } *package_even, *package_odd;
114c98d5d94SLen Brown 
115c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
116c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
117c98d5d94SLen Brown 
118c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
119c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
120c98d5d94SLen Brown 		topo.num_threads_per_core + \
121c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
122c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
123c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
124c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
125c98d5d94SLen Brown 
126c98d5d94SLen Brown struct system_summary {
127c98d5d94SLen Brown 	struct thread_data threads;
128c98d5d94SLen Brown 	struct core_data cores;
129c98d5d94SLen Brown 	struct pkg_data packages;
130c98d5d94SLen Brown } sum, average;
131c98d5d94SLen Brown 
132c98d5d94SLen Brown 
133c98d5d94SLen Brown struct topo_params {
134c98d5d94SLen Brown 	int num_packages;
135103a8feaSLen Brown 	int num_cpus;
136c98d5d94SLen Brown 	int num_cores;
137c98d5d94SLen Brown 	int max_cpu_num;
138c98d5d94SLen Brown 	int num_cores_per_pkg;
139c98d5d94SLen Brown 	int num_threads_per_core;
140c98d5d94SLen Brown } topo;
141103a8feaSLen Brown 
142c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
143103a8feaSLen Brown 
144c98d5d94SLen Brown void setup_all_buffers(void);
145103a8feaSLen Brown 
146c98d5d94SLen Brown int cpu_is_not_present(int cpu)
147d15cf7c1SLen Brown {
148c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
149c98d5d94SLen Brown }
150c98d5d94SLen Brown /*
151c98d5d94SLen Brown  * run func(thread, core, package) in topology order
152c98d5d94SLen Brown  * skip non-present cpus
153c98d5d94SLen Brown  */
154c98d5d94SLen Brown 
155c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
156c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
157c98d5d94SLen Brown {
158c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
159c98d5d94SLen Brown 
160c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
161c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
162c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
163c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
164c98d5d94SLen Brown 				struct thread_data *t;
165c98d5d94SLen Brown 				struct core_data *c;
166c98d5d94SLen Brown 				struct pkg_data *p;
167c98d5d94SLen Brown 
168c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
169c98d5d94SLen Brown 
170c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
171c98d5d94SLen Brown 					continue;
172c98d5d94SLen Brown 
173c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
174c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
175c98d5d94SLen Brown 
176c98d5d94SLen Brown 				retval = func(t, c, p);
177c98d5d94SLen Brown 				if (retval)
178c98d5d94SLen Brown 					return retval;
179c98d5d94SLen Brown 			}
180c98d5d94SLen Brown 		}
181c98d5d94SLen Brown 	}
182d15cf7c1SLen Brown 	return 0;
183d15cf7c1SLen Brown }
184d15cf7c1SLen Brown 
18588c3281fSLen Brown int cpu_migrate(int cpu)
18688c3281fSLen Brown {
187c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
188c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
189c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
19088c3281fSLen Brown 		return -1;
19188c3281fSLen Brown 	else
19288c3281fSLen Brown 		return 0;
19388c3281fSLen Brown }
19488c3281fSLen Brown 
19515aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
196103a8feaSLen Brown {
197103a8feaSLen Brown 	ssize_t retval;
198103a8feaSLen Brown 	char pathname[32];
199103a8feaSLen Brown 	int fd;
200103a8feaSLen Brown 
201103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
202103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
20315aaa346SLen Brown 	if (fd < 0)
20415aaa346SLen Brown 		return -1;
205103a8feaSLen Brown 
20615aaa346SLen Brown 	retval = pread(fd, msr, sizeof *msr, offset);
207103a8feaSLen Brown 	close(fd);
20815aaa346SLen Brown 
20915aaa346SLen Brown 	if (retval != sizeof *msr)
21015aaa346SLen Brown 		return -1;
21115aaa346SLen Brown 
21215aaa346SLen Brown 	return 0;
213103a8feaSLen Brown }
214103a8feaSLen Brown 
215a829eb4dSLen Brown void print_header(void)
216103a8feaSLen Brown {
217103a8feaSLen Brown 	if (show_pkg)
218c98d5d94SLen Brown 		outp += sprintf(outp, "pk");
219e23da037SLen Brown 	if (show_pkg)
220c98d5d94SLen Brown 		outp += sprintf(outp, " ");
221103a8feaSLen Brown 	if (show_core)
222c98d5d94SLen Brown 		outp += sprintf(outp, "cor");
223103a8feaSLen Brown 	if (show_cpu)
224c98d5d94SLen Brown 		outp += sprintf(outp, " CPU");
225e23da037SLen Brown 	if (show_pkg || show_core || show_cpu)
226c98d5d94SLen Brown 		outp += sprintf(outp, " ");
227103a8feaSLen Brown 	if (do_nhm_cstates)
228c98d5d94SLen Brown 		outp += sprintf(outp, "   %%c0");
229103a8feaSLen Brown 	if (has_aperf)
230c98d5d94SLen Brown 		outp += sprintf(outp, "  GHz");
231c98d5d94SLen Brown 	outp += sprintf(outp, "  TSC");
232*8e180f3cSLen Brown 	if (extra_delta_offset32)
233*8e180f3cSLen Brown 		outp += sprintf(outp, "  delta 0x%03X", extra_delta_offset32);
234*8e180f3cSLen Brown 	if (extra_delta_offset64)
235*8e180f3cSLen Brown 		outp += sprintf(outp, "  DELTA 0x%03X", extra_delta_offset64);
2362f32edf1SLen Brown 	if (extra_msr_offset32)
237*8e180f3cSLen Brown 		outp += sprintf(outp, "   MSR 0x%03X", extra_msr_offset32);
2382f32edf1SLen Brown 	if (extra_msr_offset64)
239*8e180f3cSLen Brown 		outp += sprintf(outp, "           MSR 0x%03X", extra_msr_offset64);
240103a8feaSLen Brown 	if (do_nhm_cstates)
241c98d5d94SLen Brown 		outp += sprintf(outp, "    %%c1");
242103a8feaSLen Brown 	if (do_nhm_cstates)
243c98d5d94SLen Brown 		outp += sprintf(outp, "    %%c3");
244103a8feaSLen Brown 	if (do_nhm_cstates)
245c98d5d94SLen Brown 		outp += sprintf(outp, "    %%c6");
246103a8feaSLen Brown 	if (do_snb_cstates)
247c98d5d94SLen Brown 		outp += sprintf(outp, "    %%c7");
248103a8feaSLen Brown 	if (do_snb_cstates)
249c98d5d94SLen Brown 		outp += sprintf(outp, "   %%pc2");
250103a8feaSLen Brown 	if (do_nhm_cstates)
251c98d5d94SLen Brown 		outp += sprintf(outp, "   %%pc3");
252103a8feaSLen Brown 	if (do_nhm_cstates)
253c98d5d94SLen Brown 		outp += sprintf(outp, "   %%pc6");
254103a8feaSLen Brown 	if (do_snb_cstates)
255c98d5d94SLen Brown 		outp += sprintf(outp, "   %%pc7");
256103a8feaSLen Brown 
257c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
258103a8feaSLen Brown }
259103a8feaSLen Brown 
260c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
261c98d5d94SLen Brown 	struct pkg_data *p)
262103a8feaSLen Brown {
263c98d5d94SLen Brown 	fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
264c98d5d94SLen Brown 
265c98d5d94SLen Brown 	if (t) {
266c98d5d94SLen Brown 		fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
267c98d5d94SLen Brown 		fprintf(stderr, "TSC: %016llX\n", t->tsc);
268c98d5d94SLen Brown 		fprintf(stderr, "aperf: %016llX\n", t->aperf);
269c98d5d94SLen Brown 		fprintf(stderr, "mperf: %016llX\n", t->mperf);
270c98d5d94SLen Brown 		fprintf(stderr, "c1: %016llX\n", t->c1);
271*8e180f3cSLen Brown 		fprintf(stderr, "msr0x%x: %08llX\n",
272*8e180f3cSLen Brown 			extra_delta_offset32, t->extra_delta32);
273*8e180f3cSLen Brown 		fprintf(stderr, "msr0x%x: %016llX\n",
274*8e180f3cSLen Brown 			extra_delta_offset64, t->extra_delta64);
275*8e180f3cSLen Brown 		fprintf(stderr, "msr0x%x: %08llX\n",
2762f32edf1SLen Brown 			extra_msr_offset32, t->extra_msr32);
277c98d5d94SLen Brown 		fprintf(stderr, "msr0x%x: %016llX\n",
2782f32edf1SLen Brown 			extra_msr_offset64, t->extra_msr64);
279103a8feaSLen Brown 	}
280103a8feaSLen Brown 
281c98d5d94SLen Brown 	if (c) {
282c98d5d94SLen Brown 		fprintf(stderr, "core: %d\n", c->core_id);
283c98d5d94SLen Brown 		fprintf(stderr, "c3: %016llX\n", c->c3);
284c98d5d94SLen Brown 		fprintf(stderr, "c6: %016llX\n", c->c6);
285c98d5d94SLen Brown 		fprintf(stderr, "c7: %016llX\n", c->c7);
286c98d5d94SLen Brown 	}
287103a8feaSLen Brown 
288c98d5d94SLen Brown 	if (p) {
289c98d5d94SLen Brown 		fprintf(stderr, "package: %d\n", p->package_id);
290c98d5d94SLen Brown 		fprintf(stderr, "pc2: %016llX\n", p->pc2);
291c98d5d94SLen Brown 		fprintf(stderr, "pc3: %016llX\n", p->pc3);
292c98d5d94SLen Brown 		fprintf(stderr, "pc6: %016llX\n", p->pc6);
293c98d5d94SLen Brown 		fprintf(stderr, "pc7: %016llX\n", p->pc7);
294c98d5d94SLen Brown 	}
295c98d5d94SLen Brown 	return 0;
296103a8feaSLen Brown }
297103a8feaSLen Brown 
298e23da037SLen Brown /*
299e23da037SLen Brown  * column formatting convention & formats
300e23da037SLen Brown  * package: "pk" 2 columns %2d
301e23da037SLen Brown  * core: "cor" 3 columns %3d
302e23da037SLen Brown  * CPU: "CPU" 3 columns %3d
303e23da037SLen Brown  * GHz: "GHz" 3 columns %3.2
304e23da037SLen Brown  * TSC: "TSC" 3 columns %3.2
305e23da037SLen Brown  * percentage " %pc3" %6.2
306e23da037SLen Brown  */
307c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
308c98d5d94SLen Brown 	struct pkg_data *p)
309103a8feaSLen Brown {
310103a8feaSLen Brown 	double interval_float;
311103a8feaSLen Brown 
312c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
313c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
314c98d5d94SLen Brown 		return 0;
315c98d5d94SLen Brown 
316c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
317c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
318c98d5d94SLen Brown 		return 0;
319c98d5d94SLen Brown 
320103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
321103a8feaSLen Brown 
322c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
323c98d5d94SLen Brown 	if (t == &average.threads) {
324103a8feaSLen Brown 		if (show_pkg)
325c98d5d94SLen Brown 			outp += sprintf(outp, "  ");
326e23da037SLen Brown 		if (show_pkg && show_core)
327c98d5d94SLen Brown 			outp += sprintf(outp, " ");
328103a8feaSLen Brown 		if (show_core)
329c98d5d94SLen Brown 			outp += sprintf(outp, "   ");
330103a8feaSLen Brown 		if (show_cpu)
331c98d5d94SLen Brown 			outp += sprintf(outp, " " "   ");
332103a8feaSLen Brown 	} else {
333c98d5d94SLen Brown 		if (show_pkg) {
334c98d5d94SLen Brown 			if (p)
335c98d5d94SLen Brown 				outp += sprintf(outp, "%2d", p->package_id);
336c98d5d94SLen Brown 			else
337c98d5d94SLen Brown 				outp += sprintf(outp, "  ");
338c98d5d94SLen Brown 		}
339e23da037SLen Brown 		if (show_pkg && show_core)
340c98d5d94SLen Brown 			outp += sprintf(outp, " ");
341c98d5d94SLen Brown 		if (show_core) {
342c98d5d94SLen Brown 			if (c)
343c98d5d94SLen Brown 				outp += sprintf(outp, "%3d", c->core_id);
344c98d5d94SLen Brown 			else
345c98d5d94SLen Brown 				outp += sprintf(outp, "   ");
346c98d5d94SLen Brown 		}
347103a8feaSLen Brown 		if (show_cpu)
348c98d5d94SLen Brown 			outp += sprintf(outp, " %3d", t->cpu_id);
349103a8feaSLen Brown 	}
350103a8feaSLen Brown 
351103a8feaSLen Brown 	/* %c0 */
352103a8feaSLen Brown 	if (do_nhm_cstates) {
353e23da037SLen Brown 		if (show_pkg || show_core || show_cpu)
354c98d5d94SLen Brown 			outp += sprintf(outp, " ");
355103a8feaSLen Brown 		if (!skip_c0)
356c98d5d94SLen Brown 			outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
357103a8feaSLen Brown 		else
358c98d5d94SLen Brown 			outp += sprintf(outp, "  ****");
359103a8feaSLen Brown 	}
360103a8feaSLen Brown 
361103a8feaSLen Brown 	/* GHz */
362103a8feaSLen Brown 	if (has_aperf) {
363103a8feaSLen Brown 		if (!aperf_mperf_unstable) {
364c98d5d94SLen Brown 			outp += sprintf(outp, " %3.2f",
365c98d5d94SLen Brown 				1.0 * t->tsc / units * t->aperf /
366c98d5d94SLen Brown 				t->mperf / interval_float);
367103a8feaSLen Brown 		} else {
368c98d5d94SLen Brown 			if (t->aperf > t->tsc || t->mperf > t->tsc) {
369c98d5d94SLen Brown 				outp += sprintf(outp, " ***");
370103a8feaSLen Brown 			} else {
371c98d5d94SLen Brown 				outp += sprintf(outp, "%3.1f*",
372c98d5d94SLen Brown 					1.0 * t->tsc /
373c98d5d94SLen Brown 					units * t->aperf /
374c98d5d94SLen Brown 					t->mperf / interval_float);
375103a8feaSLen Brown 			}
376103a8feaSLen Brown 		}
377103a8feaSLen Brown 	}
378103a8feaSLen Brown 
379103a8feaSLen Brown 	/* TSC */
380c98d5d94SLen Brown 	outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
381103a8feaSLen Brown 
382*8e180f3cSLen Brown 	/* delta */
383*8e180f3cSLen Brown 	if (extra_delta_offset32)
384*8e180f3cSLen Brown 		outp += sprintf(outp, "  %11llu", t->extra_delta32);
385*8e180f3cSLen Brown 
386*8e180f3cSLen Brown 	/* DELTA */
387*8e180f3cSLen Brown 	if (extra_delta_offset64)
388*8e180f3cSLen Brown 		outp += sprintf(outp, "  %11llu", t->extra_delta64);
3892f32edf1SLen Brown 	/* msr */
3902f32edf1SLen Brown 	if (extra_msr_offset32)
391*8e180f3cSLen Brown 		outp += sprintf(outp, "  0x%08llx", t->extra_msr32);
3922f32edf1SLen Brown 
393130ff304SLen Brown 	/* MSR */
3942f32edf1SLen Brown 	if (extra_msr_offset64)
3952f32edf1SLen Brown 		outp += sprintf(outp, "  0x%016llx", t->extra_msr64);
396130ff304SLen Brown 
397103a8feaSLen Brown 	if (do_nhm_cstates) {
398103a8feaSLen Brown 		if (!skip_c1)
399c98d5d94SLen Brown 			outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
400103a8feaSLen Brown 		else
401c98d5d94SLen Brown 			outp += sprintf(outp, "  ****");
402103a8feaSLen Brown 	}
403c98d5d94SLen Brown 
404c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
405c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
406c98d5d94SLen Brown 		goto done;
407c98d5d94SLen Brown 
408103a8feaSLen Brown 	if (do_nhm_cstates)
409c98d5d94SLen Brown 		outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
410103a8feaSLen Brown 	if (do_nhm_cstates)
411c98d5d94SLen Brown 		outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
412103a8feaSLen Brown 	if (do_snb_cstates)
413c98d5d94SLen Brown 		outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
414c98d5d94SLen Brown 
415c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
416c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
417c98d5d94SLen Brown 		goto done;
418c98d5d94SLen Brown 
419103a8feaSLen Brown 	if (do_snb_cstates)
420c98d5d94SLen Brown 		outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
421103a8feaSLen Brown 	if (do_nhm_cstates)
422c98d5d94SLen Brown 		outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
423103a8feaSLen Brown 	if (do_nhm_cstates)
424c98d5d94SLen Brown 		outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
425103a8feaSLen Brown 	if (do_snb_cstates)
426c98d5d94SLen Brown 		outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
427c98d5d94SLen Brown done:
428c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
429c98d5d94SLen Brown 
430c98d5d94SLen Brown 	return 0;
431103a8feaSLen Brown }
432103a8feaSLen Brown 
433c98d5d94SLen Brown void flush_stdout()
434103a8feaSLen Brown {
435c98d5d94SLen Brown 	fputs(output_buffer, stdout);
436c98d5d94SLen Brown 	outp = output_buffer;
437c98d5d94SLen Brown }
438c98d5d94SLen Brown void flush_stderr()
439c98d5d94SLen Brown {
440c98d5d94SLen Brown 	fputs(output_buffer, stderr);
441c98d5d94SLen Brown 	outp = output_buffer;
442c98d5d94SLen Brown }
443c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
444c98d5d94SLen Brown {
445e23da037SLen Brown 	static int printed;
446103a8feaSLen Brown 
447e23da037SLen Brown 	if (!printed || !summary_only)
448103a8feaSLen Brown 		print_header();
449103a8feaSLen Brown 
450c98d5d94SLen Brown 	if (topo.num_cpus > 1)
451c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
452c98d5d94SLen Brown 			&average.packages);
453103a8feaSLen Brown 
454e23da037SLen Brown 	printed = 1;
455e23da037SLen Brown 
456e23da037SLen Brown 	if (summary_only)
457e23da037SLen Brown 		return;
458e23da037SLen Brown 
459c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
460103a8feaSLen Brown }
461103a8feaSLen Brown 
462c98d5d94SLen Brown void
463c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
464103a8feaSLen Brown {
465c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
466c98d5d94SLen Brown 	old->pc3 = new->pc3 - old->pc3;
467c98d5d94SLen Brown 	old->pc6 = new->pc6 - old->pc6;
468c98d5d94SLen Brown 	old->pc7 = new->pc7 - old->pc7;
469103a8feaSLen Brown }
470103a8feaSLen Brown 
471c98d5d94SLen Brown void
472c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
473c98d5d94SLen Brown {
474c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
475c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
476c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
477103a8feaSLen Brown }
478c98d5d94SLen Brown 
479c3ae331dSLen Brown /*
480c3ae331dSLen Brown  * old = new - old
481c3ae331dSLen Brown  */
482c98d5d94SLen Brown void
483c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
484c98d5d94SLen Brown 	struct core_data *core_delta)
485c98d5d94SLen Brown {
486c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
487c98d5d94SLen Brown 
488103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
489c98d5d94SLen Brown 	if (old->tsc < (1000 * 1000)) {
490c98d5d94SLen Brown 		fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
491c98d5d94SLen Brown 		fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
492c98d5d94SLen Brown 		fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
493103a8feaSLen Brown 		exit(-3);
494103a8feaSLen Brown 	}
495103a8feaSLen Brown 
496c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
497c98d5d94SLen Brown 
498c98d5d94SLen Brown 	if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
499c98d5d94SLen Brown 		old->aperf = new->aperf - old->aperf;
500c98d5d94SLen Brown 		old->mperf = new->mperf - old->mperf;
501c98d5d94SLen Brown 	} else {
502c98d5d94SLen Brown 
503103a8feaSLen Brown 		if (!aperf_mperf_unstable) {
504103a8feaSLen Brown 			fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
505103a8feaSLen Brown 			fprintf(stderr, "* Frequency results do not cover entire interval *\n");
506103a8feaSLen Brown 			fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
507103a8feaSLen Brown 
508103a8feaSLen Brown 			aperf_mperf_unstable = 1;
509103a8feaSLen Brown 		}
510103a8feaSLen Brown 		/*
511103a8feaSLen Brown 		 * mperf delta is likely a huge "positive" number
512103a8feaSLen Brown 		 * can not use it for calculating c0 time
513103a8feaSLen Brown 		 */
514103a8feaSLen Brown 		skip_c0 = 1;
515103a8feaSLen Brown 		skip_c1 = 1;
516103a8feaSLen Brown 	}
517103a8feaSLen Brown 
518c98d5d94SLen Brown 
519103a8feaSLen Brown 	/*
520c3ae331dSLen Brown 	 * As counter collection is not atomic,
521c3ae331dSLen Brown 	 * it is possible for mperf's non-halted cycles + idle states
522103a8feaSLen Brown 	 * to exceed TSC's all cycles: show c1 = 0% in that case.
523103a8feaSLen Brown 	 */
524c3ae331dSLen Brown 	if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
525c98d5d94SLen Brown 		old->c1 = 0;
526c98d5d94SLen Brown 	else {
527c98d5d94SLen Brown 		/* normal case, derive c1 */
528c98d5d94SLen Brown 		old->c1 = old->tsc - old->mperf - core_delta->c3
529c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
530c98d5d94SLen Brown 	}
531c3ae331dSLen Brown 
532c98d5d94SLen Brown 	if (old->mperf == 0) {
533c3ae331dSLen Brown 		if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
534c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
535c98d5d94SLen Brown 	}
536103a8feaSLen Brown 
537*8e180f3cSLen Brown 	old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
538*8e180f3cSLen Brown 	old->extra_delta32 &= 0xFFFFFFFF;
539*8e180f3cSLen Brown 
540*8e180f3cSLen Brown 	old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
541*8e180f3cSLen Brown 
542103a8feaSLen Brown 	/*
543*8e180f3cSLen Brown 	 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
544103a8feaSLen Brown 	 */
5452f32edf1SLen Brown 	old->extra_msr32 = new->extra_msr32;
5462f32edf1SLen Brown 	old->extra_msr64 = new->extra_msr64;
547103a8feaSLen Brown }
548c98d5d94SLen Brown 
549c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
550c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
551c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
552c98d5d94SLen Brown {
553c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
554c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
555c98d5d94SLen Brown 		delta_core(c, c2);
556c98d5d94SLen Brown 
557c98d5d94SLen Brown 	/* always calculate thread delta */
558c98d5d94SLen Brown 	delta_thread(t, t2, c2);	/* c2 is core delta */
559c98d5d94SLen Brown 
560c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
561c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
562c98d5d94SLen Brown 		delta_package(p, p2);
563c98d5d94SLen Brown 
564103a8feaSLen Brown 	return 0;
565103a8feaSLen Brown }
566103a8feaSLen Brown 
567c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
568103a8feaSLen Brown {
569c98d5d94SLen Brown 	t->tsc = 0;
570c98d5d94SLen Brown 	t->aperf = 0;
571c98d5d94SLen Brown 	t->mperf = 0;
572c98d5d94SLen Brown 	t->c1 = 0;
573103a8feaSLen Brown 
574*8e180f3cSLen Brown 	t->extra_delta32 = 0;
575*8e180f3cSLen Brown 	t->extra_delta64 = 0;
576*8e180f3cSLen Brown 
577c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
578c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
579c98d5d94SLen Brown 
580c98d5d94SLen Brown 	c->c3 = 0;
581c98d5d94SLen Brown 	c->c6 = 0;
582c98d5d94SLen Brown 	c->c7 = 0;
583c98d5d94SLen Brown 
584c98d5d94SLen Brown 	p->pc2 = 0;
585c98d5d94SLen Brown 	p->pc3 = 0;
586c98d5d94SLen Brown 	p->pc6 = 0;
587c98d5d94SLen Brown 	p->pc7 = 0;
588103a8feaSLen Brown }
589c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
590c98d5d94SLen Brown 	struct pkg_data *p)
591103a8feaSLen Brown {
592c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
593c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
594c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
595c98d5d94SLen Brown 	average.threads.c1 += t->c1;
59615aaa346SLen Brown 
597*8e180f3cSLen Brown 	average.threads.extra_delta32 += t->extra_delta32;
598*8e180f3cSLen Brown 	average.threads.extra_delta64 += t->extra_delta64;
599*8e180f3cSLen Brown 
600c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
601c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
602c98d5d94SLen Brown 		return 0;
603c98d5d94SLen Brown 
604c98d5d94SLen Brown 	average.cores.c3 += c->c3;
605c98d5d94SLen Brown 	average.cores.c6 += c->c6;
606c98d5d94SLen Brown 	average.cores.c7 += c->c7;
607c98d5d94SLen Brown 
608c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
609c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
610c98d5d94SLen Brown 		return 0;
611c98d5d94SLen Brown 
612c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
613c98d5d94SLen Brown 	average.packages.pc3 += p->pc3;
614c98d5d94SLen Brown 	average.packages.pc6 += p->pc6;
615c98d5d94SLen Brown 	average.packages.pc7 += p->pc7;
616c98d5d94SLen Brown 
617c98d5d94SLen Brown 	return 0;
618c98d5d94SLen Brown }
619c98d5d94SLen Brown /*
620c98d5d94SLen Brown  * sum the counters for all cpus in the system
621c98d5d94SLen Brown  * compute the weighted average
622c98d5d94SLen Brown  */
623c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
624c98d5d94SLen Brown 	struct pkg_data *p)
625c98d5d94SLen Brown {
626c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
627c98d5d94SLen Brown 
628c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
629c98d5d94SLen Brown 
630c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
631c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
632c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
633c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
634c98d5d94SLen Brown 
635*8e180f3cSLen Brown 	average.threads.extra_delta32 /= topo.num_cpus;
636*8e180f3cSLen Brown 	average.threads.extra_delta32 &= 0xFFFFFFFF;
637*8e180f3cSLen Brown 
638*8e180f3cSLen Brown 	average.threads.extra_delta64 /= topo.num_cpus;
639*8e180f3cSLen Brown 
640c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
641c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
642c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
643c98d5d94SLen Brown 
644c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
645c98d5d94SLen Brown 	average.packages.pc3 /= topo.num_packages;
646c98d5d94SLen Brown 	average.packages.pc6 /= topo.num_packages;
647c98d5d94SLen Brown 	average.packages.pc7 /= topo.num_packages;
648c98d5d94SLen Brown }
649c98d5d94SLen Brown 
650c98d5d94SLen Brown static unsigned long long rdtsc(void)
651c98d5d94SLen Brown {
652c98d5d94SLen Brown 	unsigned int low, high;
653c98d5d94SLen Brown 
654c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
655c98d5d94SLen Brown 
656c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
657c98d5d94SLen Brown }
658c98d5d94SLen Brown 
659c98d5d94SLen Brown 
660c98d5d94SLen Brown /*
661c98d5d94SLen Brown  * get_counters(...)
662c98d5d94SLen Brown  * migrate to cpu
663c98d5d94SLen Brown  * acquire and record local counters for that cpu
664c98d5d94SLen Brown  */
665c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
666c98d5d94SLen Brown {
667c98d5d94SLen Brown 	int cpu = t->cpu_id;
668c98d5d94SLen Brown 
669c98d5d94SLen Brown 	if (cpu_migrate(cpu))
67015aaa346SLen Brown 		return -1;
67115aaa346SLen Brown 
672c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
67315aaa346SLen Brown 
67415aaa346SLen Brown 	if (has_aperf) {
675c98d5d94SLen Brown 		if (get_msr(cpu, MSR_APERF, &t->aperf))
676c98d5d94SLen Brown 			return -3;
677c98d5d94SLen Brown 		if (get_msr(cpu, MSR_MPERF, &t->mperf))
678c98d5d94SLen Brown 			return -4;
67988c3281fSLen Brown 	}
68088c3281fSLen Brown 
681*8e180f3cSLen Brown 	if (extra_delta_offset32) {
682*8e180f3cSLen Brown 		if (get_msr(cpu, extra_delta_offset32, &t->extra_delta32))
6832f32edf1SLen Brown 			return -5;
684*8e180f3cSLen Brown 		t->extra_delta32 &= 0xFFFFFFFF;
685*8e180f3cSLen Brown 	}
686*8e180f3cSLen Brown 
687*8e180f3cSLen Brown 	if (extra_delta_offset64)
688*8e180f3cSLen Brown 		if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
689*8e180f3cSLen Brown 			return -5;
690*8e180f3cSLen Brown 
691*8e180f3cSLen Brown 	if (extra_msr_offset32) {
692*8e180f3cSLen Brown 		if (get_msr(cpu, extra_msr_offset32, &t->extra_msr32))
693*8e180f3cSLen Brown 			return -5;
694*8e180f3cSLen Brown 		t->extra_msr32 &= 0xFFFFFFFF;
695*8e180f3cSLen Brown 	}
6962f32edf1SLen Brown 
6972f32edf1SLen Brown 	if (extra_msr_offset64)
6982f32edf1SLen Brown 		if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
699c98d5d94SLen Brown 			return -5;
700c98d5d94SLen Brown 
701c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
702c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
703c98d5d94SLen Brown 		return 0;
704c98d5d94SLen Brown 
70515aaa346SLen Brown 	if (do_nhm_cstates) {
706c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
707c98d5d94SLen Brown 			return -6;
708c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
709c98d5d94SLen Brown 			return -7;
710103a8feaSLen Brown 	}
71115aaa346SLen Brown 
71215aaa346SLen Brown 	if (do_snb_cstates)
713c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
714c98d5d94SLen Brown 			return -8;
715c98d5d94SLen Brown 
716c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
717c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
718c98d5d94SLen Brown 		return 0;
71915aaa346SLen Brown 
72015aaa346SLen Brown 	if (do_nhm_cstates) {
721c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
722c98d5d94SLen Brown 			return -9;
723c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
724c98d5d94SLen Brown 			return -10;
72515aaa346SLen Brown 	}
72615aaa346SLen Brown 	if (do_snb_cstates) {
727c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
728c98d5d94SLen Brown 			return -11;
729c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
730c98d5d94SLen Brown 			return -12;
73115aaa346SLen Brown 	}
73215aaa346SLen Brown 	return 0;
733103a8feaSLen Brown }
734103a8feaSLen Brown 
735c98d5d94SLen Brown void print_verbose_header(void)
736103a8feaSLen Brown {
737103a8feaSLen Brown 	unsigned long long msr;
738103a8feaSLen Brown 	unsigned int ratio;
739103a8feaSLen Brown 
740103a8feaSLen Brown 	if (!do_nehalem_platform_info)
741103a8feaSLen Brown 		return;
742103a8feaSLen Brown 
74315aaa346SLen Brown 	get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
744103a8feaSLen Brown 
7456574a5d5SLen Brown 	if (verbose > 1)
7466574a5d5SLen Brown 		fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
7476574a5d5SLen Brown 
748103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
749103a8feaSLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
750103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
751103a8feaSLen Brown 
752103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
753103a8feaSLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
754103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
755103a8feaSLen Brown 
7566574a5d5SLen Brown 	if (!do_ivt_turbo_ratio_limit)
7576574a5d5SLen Brown 		goto print_nhm_turbo_ratio_limits;
7586574a5d5SLen Brown 
7596574a5d5SLen Brown 	get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
7606574a5d5SLen Brown 
761103a8feaSLen Brown 	if (verbose > 1)
7626574a5d5SLen Brown 		fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
7636574a5d5SLen Brown 
7646574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
7656574a5d5SLen Brown 	if (ratio)
7666574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
7676574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
7686574a5d5SLen Brown 
7696574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
7706574a5d5SLen Brown 	if (ratio)
7716574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
7726574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
7736574a5d5SLen Brown 
7746574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
7756574a5d5SLen Brown 	if (ratio)
7766574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
7776574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
7786574a5d5SLen Brown 
7796574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
7806574a5d5SLen Brown 	if (ratio)
7816574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
7826574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
7836574a5d5SLen Brown 
7846574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
7856574a5d5SLen Brown 	if (ratio)
7866574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
7876574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
7886574a5d5SLen Brown 
7896574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
7906574a5d5SLen Brown 	if (ratio)
7916574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
7926574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
7936574a5d5SLen Brown 
7946574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
7956574a5d5SLen Brown 	if (ratio)
7966574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
7976574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
7986574a5d5SLen Brown 
7996574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
8006574a5d5SLen Brown 	if (ratio)
8016574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
8026574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
8036574a5d5SLen Brown 
8046574a5d5SLen Brown print_nhm_turbo_ratio_limits:
805103a8feaSLen Brown 
806103a8feaSLen Brown 	if (!do_nehalem_turbo_ratio_limit)
807103a8feaSLen Brown 		return;
808103a8feaSLen Brown 
80915aaa346SLen Brown 	get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
810103a8feaSLen Brown 
8116574a5d5SLen Brown 	if (verbose > 1)
8126574a5d5SLen Brown 		fprintf(stderr, "MSR_NEHALEM_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
8136574a5d5SLen Brown 
8146574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
8156574a5d5SLen Brown 	if (ratio)
8166574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
8176574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
8186574a5d5SLen Brown 
8196574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
8206574a5d5SLen Brown 	if (ratio)
8216574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
8226574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
8236574a5d5SLen Brown 
8246574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
8256574a5d5SLen Brown 	if (ratio)
8266574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
8276574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
8286574a5d5SLen Brown 
8296574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
8306574a5d5SLen Brown 	if (ratio)
8316574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
8326574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
8336574a5d5SLen Brown 
834103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
835103a8feaSLen Brown 	if (ratio)
836103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
837103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
838103a8feaSLen Brown 
839103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
840103a8feaSLen Brown 	if (ratio)
841103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
842103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
843103a8feaSLen Brown 
844103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
845103a8feaSLen Brown 	if (ratio)
846103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
847103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
848103a8feaSLen Brown 
849103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
850103a8feaSLen Brown 	if (ratio)
851103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
852103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
853103a8feaSLen Brown }
854103a8feaSLen Brown 
855c98d5d94SLen Brown void free_all_buffers(void)
856103a8feaSLen Brown {
857c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
858c98d5d94SLen Brown 	cpu_present_set = NULL;
859c98d5d94SLen Brown 	cpu_present_set = 0;
860103a8feaSLen Brown 
861c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
862c98d5d94SLen Brown 	cpu_affinity_set = NULL;
863c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
864103a8feaSLen Brown 
865c98d5d94SLen Brown 	free(thread_even);
866c98d5d94SLen Brown 	free(core_even);
867c98d5d94SLen Brown 	free(package_even);
868103a8feaSLen Brown 
869c98d5d94SLen Brown 	thread_even = NULL;
870c98d5d94SLen Brown 	core_even = NULL;
871c98d5d94SLen Brown 	package_even = NULL;
872103a8feaSLen Brown 
873c98d5d94SLen Brown 	free(thread_odd);
874c98d5d94SLen Brown 	free(core_odd);
875c98d5d94SLen Brown 	free(package_odd);
876103a8feaSLen Brown 
877c98d5d94SLen Brown 	thread_odd = NULL;
878c98d5d94SLen Brown 	core_odd = NULL;
879c98d5d94SLen Brown 	package_odd = NULL;
880103a8feaSLen Brown 
881c98d5d94SLen Brown 	free(output_buffer);
882c98d5d94SLen Brown 	output_buffer = NULL;
883c98d5d94SLen Brown 	outp = NULL;
884103a8feaSLen Brown }
885103a8feaSLen Brown 
886103a8feaSLen Brown /*
887c98d5d94SLen Brown  * cpu_is_first_sibling_in_core(cpu)
888c98d5d94SLen Brown  * return 1 if given CPU is 1st HT sibling in the core
889103a8feaSLen Brown  */
890c98d5d94SLen Brown int cpu_is_first_sibling_in_core(int cpu)
891103a8feaSLen Brown {
892c98d5d94SLen Brown 	char path[64];
893c98d5d94SLen Brown 	FILE *filep;
894c98d5d94SLen Brown 	int first_cpu;
895103a8feaSLen Brown 
896c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
897c98d5d94SLen Brown 	filep = fopen(path, "r");
898c98d5d94SLen Brown 	if (filep == NULL) {
899c98d5d94SLen Brown 		perror(path);
900103a8feaSLen Brown 		exit(1);
901103a8feaSLen Brown 	}
902c98d5d94SLen Brown 	fscanf(filep, "%d", &first_cpu);
903c98d5d94SLen Brown 	fclose(filep);
904c98d5d94SLen Brown 	return (cpu == first_cpu);
905c98d5d94SLen Brown }
906103a8feaSLen Brown 
907c98d5d94SLen Brown /*
908c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
909c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
910c98d5d94SLen Brown  */
911c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
912c98d5d94SLen Brown {
913c98d5d94SLen Brown 	char path[64];
914c98d5d94SLen Brown 	FILE *filep;
915c98d5d94SLen Brown 	int first_cpu;
916c98d5d94SLen Brown 
917c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
918c98d5d94SLen Brown 	filep = fopen(path, "r");
919c98d5d94SLen Brown 	if (filep == NULL) {
920c98d5d94SLen Brown 		perror(path);
921103a8feaSLen Brown 		exit(1);
922103a8feaSLen Brown 	}
923c98d5d94SLen Brown 	fscanf(filep, "%d", &first_cpu);
924c98d5d94SLen Brown 	fclose(filep);
925c98d5d94SLen Brown 	return (cpu == first_cpu);
926103a8feaSLen Brown }
927103a8feaSLen Brown 
928103a8feaSLen Brown int get_physical_package_id(int cpu)
929103a8feaSLen Brown {
930c98d5d94SLen Brown 	char path[80];
931103a8feaSLen Brown 	FILE *filep;
932103a8feaSLen Brown 	int pkg;
933103a8feaSLen Brown 
934103a8feaSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
935103a8feaSLen Brown 	filep = fopen(path, "r");
936103a8feaSLen Brown 	if (filep == NULL) {
937103a8feaSLen Brown 		perror(path);
938103a8feaSLen Brown 		exit(1);
939103a8feaSLen Brown 	}
940103a8feaSLen Brown 	fscanf(filep, "%d", &pkg);
941103a8feaSLen Brown 	fclose(filep);
942103a8feaSLen Brown 	return pkg;
943103a8feaSLen Brown }
944103a8feaSLen Brown 
945103a8feaSLen Brown int get_core_id(int cpu)
946103a8feaSLen Brown {
947c98d5d94SLen Brown 	char path[80];
948103a8feaSLen Brown 	FILE *filep;
949103a8feaSLen Brown 	int core;
950103a8feaSLen Brown 
951103a8feaSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
952103a8feaSLen Brown 	filep = fopen(path, "r");
953103a8feaSLen Brown 	if (filep == NULL) {
954103a8feaSLen Brown 		perror(path);
955103a8feaSLen Brown 		exit(1);
956103a8feaSLen Brown 	}
957103a8feaSLen Brown 	fscanf(filep, "%d", &core);
958103a8feaSLen Brown 	fclose(filep);
959103a8feaSLen Brown 	return core;
960103a8feaSLen Brown }
961103a8feaSLen Brown 
962c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
963c98d5d94SLen Brown {
964c98d5d94SLen Brown 	char path[80];
965c98d5d94SLen Brown 	FILE *filep;
966c98d5d94SLen Brown 	int sib1, sib2;
967c98d5d94SLen Brown 	int matches;
968c98d5d94SLen Brown 	char character;
969c98d5d94SLen Brown 
970c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
971c98d5d94SLen Brown 	filep = fopen(path, "r");
972c98d5d94SLen Brown 	if (filep == NULL) {
973c98d5d94SLen Brown 		perror(path);
974c98d5d94SLen Brown 		exit(1);
975c98d5d94SLen Brown 	}
976103a8feaSLen Brown 	/*
977c98d5d94SLen Brown 	 * file format:
978c98d5d94SLen Brown 	 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
979c98d5d94SLen Brown 	 * otherwinse 1 sibling (self).
980c98d5d94SLen Brown 	 */
981c98d5d94SLen Brown 	matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
982c98d5d94SLen Brown 
983c98d5d94SLen Brown 	fclose(filep);
984c98d5d94SLen Brown 
985c98d5d94SLen Brown 	if (matches == 3)
986c98d5d94SLen Brown 		return 2;
987c98d5d94SLen Brown 	else
988c98d5d94SLen Brown 		return 1;
989c98d5d94SLen Brown }
990c98d5d94SLen Brown 
991c98d5d94SLen Brown /*
992c98d5d94SLen Brown  * run func(thread, core, package) in topology order
993c98d5d94SLen Brown  * skip non-present cpus
994103a8feaSLen Brown  */
995103a8feaSLen Brown 
996c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
997c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
998c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
999c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
1000c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
1001c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
1002c98d5d94SLen Brown {
1003c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
1004c98d5d94SLen Brown 
1005c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1006c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1007c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
1008c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
1009c98d5d94SLen Brown 				struct thread_data *t, *t2;
1010c98d5d94SLen Brown 				struct core_data *c, *c2;
1011c98d5d94SLen Brown 				struct pkg_data *p, *p2;
1012c98d5d94SLen Brown 
1013c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1014c98d5d94SLen Brown 
1015c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
1016c98d5d94SLen Brown 					continue;
1017c98d5d94SLen Brown 
1018c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1019c98d5d94SLen Brown 
1020c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
1021c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
1022c98d5d94SLen Brown 
1023c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
1024c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
1025c98d5d94SLen Brown 
1026c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
1027c98d5d94SLen Brown 				if (retval)
1028c98d5d94SLen Brown 					return retval;
1029c98d5d94SLen Brown 			}
1030c98d5d94SLen Brown 		}
1031c98d5d94SLen Brown 	}
1032c98d5d94SLen Brown 	return 0;
1033c98d5d94SLen Brown }
1034c98d5d94SLen Brown 
1035c98d5d94SLen Brown /*
1036c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
1037c98d5d94SLen Brown  * return max_cpu number
1038c98d5d94SLen Brown  */
1039c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
1040103a8feaSLen Brown {
1041103a8feaSLen Brown 	FILE *fp;
1042c98d5d94SLen Brown 	int cpu_num;
1043103a8feaSLen Brown 	int retval;
1044103a8feaSLen Brown 
1045103a8feaSLen Brown 	fp = fopen(proc_stat, "r");
1046103a8feaSLen Brown 	if (fp == NULL) {
1047103a8feaSLen Brown 		perror(proc_stat);
1048103a8feaSLen Brown 		exit(1);
1049103a8feaSLen Brown 	}
1050103a8feaSLen Brown 
1051103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1052103a8feaSLen Brown 	if (retval != 0) {
1053103a8feaSLen Brown 		perror("/proc/stat format");
1054103a8feaSLen Brown 		exit(1);
1055103a8feaSLen Brown 	}
1056103a8feaSLen Brown 
1057c98d5d94SLen Brown 	while (1) {
1058c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1059103a8feaSLen Brown 		if (retval != 1)
1060103a8feaSLen Brown 			break;
1061103a8feaSLen Brown 
1062c98d5d94SLen Brown 		retval = func(cpu_num);
1063c98d5d94SLen Brown 		if (retval) {
1064c98d5d94SLen Brown 			fclose(fp);
1065c98d5d94SLen Brown 			return(retval);
1066c98d5d94SLen Brown 		}
1067103a8feaSLen Brown 	}
1068103a8feaSLen Brown 	fclose(fp);
1069c98d5d94SLen Brown 	return 0;
1070103a8feaSLen Brown }
1071103a8feaSLen Brown 
1072103a8feaSLen Brown void re_initialize(void)
1073103a8feaSLen Brown {
1074c98d5d94SLen Brown 	free_all_buffers();
1075c98d5d94SLen Brown 	setup_all_buffers();
1076c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1077103a8feaSLen Brown }
1078103a8feaSLen Brown 
1079c98d5d94SLen Brown 
1080103a8feaSLen Brown /*
1081c98d5d94SLen Brown  * count_cpus()
1082c98d5d94SLen Brown  * remember the last one seen, it will be the max
1083103a8feaSLen Brown  */
1084c98d5d94SLen Brown int count_cpus(int cpu)
1085103a8feaSLen Brown {
1086c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
1087c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
1088103a8feaSLen Brown 
1089c98d5d94SLen Brown 	topo.num_cpus += 1;
1090c98d5d94SLen Brown 	return 0;
1091103a8feaSLen Brown }
1092c98d5d94SLen Brown int mark_cpu_present(int cpu)
1093c98d5d94SLen Brown {
1094c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
109515aaa346SLen Brown 	return 0;
1096103a8feaSLen Brown }
1097103a8feaSLen Brown 
1098103a8feaSLen Brown void turbostat_loop()
1099103a8feaSLen Brown {
1100c98d5d94SLen Brown 	int retval;
1101c98d5d94SLen Brown 
1102103a8feaSLen Brown restart:
1103c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1104c98d5d94SLen Brown 	if (retval) {
1105c98d5d94SLen Brown 		re_initialize();
1106c98d5d94SLen Brown 		goto restart;
1107c98d5d94SLen Brown 	}
1108103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
1109103a8feaSLen Brown 
1110103a8feaSLen Brown 	while (1) {
1111c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
1112103a8feaSLen Brown 			re_initialize();
1113103a8feaSLen Brown 			goto restart;
1114103a8feaSLen Brown 		}
1115103a8feaSLen Brown 		sleep(interval_sec);
1116c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
1117c98d5d94SLen Brown 		if (retval) {
111815aaa346SLen Brown 			re_initialize();
111915aaa346SLen Brown 			goto restart;
112015aaa346SLen Brown 		}
1121103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
1122103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
1123c98d5d94SLen Brown 		for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1124c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
1125c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
1126c98d5d94SLen Brown 		flush_stdout();
112715aaa346SLen Brown 		sleep(interval_sec);
1128c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1129c98d5d94SLen Brown 		if (retval) {
1130103a8feaSLen Brown 			re_initialize();
1131103a8feaSLen Brown 			goto restart;
1132103a8feaSLen Brown 		}
1133103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
1134103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
1135c98d5d94SLen Brown 		for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1136c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
1137c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
1138c98d5d94SLen Brown 		flush_stdout();
1139103a8feaSLen Brown 	}
1140103a8feaSLen Brown }
1141103a8feaSLen Brown 
1142103a8feaSLen Brown void check_dev_msr()
1143103a8feaSLen Brown {
1144103a8feaSLen Brown 	struct stat sb;
1145103a8feaSLen Brown 
1146103a8feaSLen Brown 	if (stat("/dev/cpu/0/msr", &sb)) {
1147103a8feaSLen Brown 		fprintf(stderr, "no /dev/cpu/0/msr\n");
1148103a8feaSLen Brown 		fprintf(stderr, "Try \"# modprobe msr\"\n");
1149103a8feaSLen Brown 		exit(-5);
1150103a8feaSLen Brown 	}
1151103a8feaSLen Brown }
1152103a8feaSLen Brown 
1153103a8feaSLen Brown void check_super_user()
1154103a8feaSLen Brown {
1155103a8feaSLen Brown 	if (getuid() != 0) {
1156103a8feaSLen Brown 		fprintf(stderr, "must be root\n");
1157103a8feaSLen Brown 		exit(-6);
1158103a8feaSLen Brown 	}
1159103a8feaSLen Brown }
1160103a8feaSLen Brown 
1161103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1162103a8feaSLen Brown {
1163103a8feaSLen Brown 	if (!genuine_intel)
1164103a8feaSLen Brown 		return 0;
1165103a8feaSLen Brown 
1166103a8feaSLen Brown 	if (family != 6)
1167103a8feaSLen Brown 		return 0;
1168103a8feaSLen Brown 
1169103a8feaSLen Brown 	switch (model) {
1170103a8feaSLen Brown 	case 0x1A:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1171103a8feaSLen Brown 	case 0x1E:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1172103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
1173103a8feaSLen Brown 	case 0x25:	/* Westmere Client - Clarkdale, Arrandale */
1174103a8feaSLen Brown 	case 0x2C:	/* Westmere EP - Gulftown */
1175103a8feaSLen Brown 	case 0x2A:	/* SNB */
1176103a8feaSLen Brown 	case 0x2D:	/* SNB Xeon */
1177553575f1SLen Brown 	case 0x3A:	/* IVB */
11781300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
1179103a8feaSLen Brown 		return 1;
1180103a8feaSLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
1181103a8feaSLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
1182103a8feaSLen Brown 	default:
1183103a8feaSLen Brown 		return 0;
1184103a8feaSLen Brown 	}
1185103a8feaSLen Brown }
11866574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
11876574a5d5SLen Brown {
11886574a5d5SLen Brown 	if (!genuine_intel)
11896574a5d5SLen Brown 		return 0;
11906574a5d5SLen Brown 
11916574a5d5SLen Brown 	if (family != 6)
11926574a5d5SLen Brown 		return 0;
11936574a5d5SLen Brown 
11946574a5d5SLen Brown 	switch (model) {
11956574a5d5SLen Brown 	case 0x3E:	/* IVB Xeon */
11966574a5d5SLen Brown 		return 1;
11976574a5d5SLen Brown 	default:
11986574a5d5SLen Brown 		return 0;
11996574a5d5SLen Brown 	}
12006574a5d5SLen Brown }
12016574a5d5SLen Brown 
1202103a8feaSLen Brown 
1203103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model)
1204103a8feaSLen Brown {
1205103a8feaSLen Brown 	if (!genuine_intel)
1206103a8feaSLen Brown 		return 0;
1207103a8feaSLen Brown 
1208103a8feaSLen Brown 	switch (model) {
1209103a8feaSLen Brown 	case 0x2A:
1210103a8feaSLen Brown 	case 0x2D:
1211650a37f3SLen Brown 	case 0x3A:	/* IVB */
12121300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
1213103a8feaSLen Brown 		return 1;
1214103a8feaSLen Brown 	}
1215103a8feaSLen Brown 	return 0;
1216103a8feaSLen Brown }
1217103a8feaSLen Brown 
1218103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
1219103a8feaSLen Brown {
1220103a8feaSLen Brown 	if (is_snb(family, model))
1221103a8feaSLen Brown 		return 100.00;
1222103a8feaSLen Brown 	else
1223103a8feaSLen Brown 		return 133.33;
1224103a8feaSLen Brown }
1225103a8feaSLen Brown 
1226103a8feaSLen Brown void check_cpuid()
1227103a8feaSLen Brown {
1228103a8feaSLen Brown 	unsigned int eax, ebx, ecx, edx, max_level;
1229103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
1230103a8feaSLen Brown 
1231103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
1232103a8feaSLen Brown 
1233103a8feaSLen Brown 	asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1234103a8feaSLen Brown 
1235103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1236103a8feaSLen Brown 		genuine_intel = 1;
1237103a8feaSLen Brown 
1238103a8feaSLen Brown 	if (verbose)
1239103a8feaSLen Brown 		fprintf(stderr, "%.4s%.4s%.4s ",
1240103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
1241103a8feaSLen Brown 
1242103a8feaSLen Brown 	asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1243103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
1244103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
1245103a8feaSLen Brown 	stepping = fms & 0xf;
1246103a8feaSLen Brown 	if (family == 6 || family == 0xf)
1247103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
1248103a8feaSLen Brown 
1249103a8feaSLen Brown 	if (verbose)
1250103a8feaSLen Brown 		fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1251103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
1252103a8feaSLen Brown 
1253103a8feaSLen Brown 	if (!(edx & (1 << 5))) {
1254103a8feaSLen Brown 		fprintf(stderr, "CPUID: no MSR\n");
1255103a8feaSLen Brown 		exit(1);
1256103a8feaSLen Brown 	}
1257103a8feaSLen Brown 
1258103a8feaSLen Brown 	/*
1259103a8feaSLen Brown 	 * check max extended function levels of CPUID.
1260103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
1261103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
1262103a8feaSLen Brown 	 */
1263103a8feaSLen Brown 	ebx = ecx = edx = 0;
1264103a8feaSLen Brown 	asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1265103a8feaSLen Brown 
1266103a8feaSLen Brown 	if (max_level < 0x80000007) {
1267103a8feaSLen Brown 		fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1268103a8feaSLen Brown 		exit(1);
1269103a8feaSLen Brown 	}
1270103a8feaSLen Brown 
1271103a8feaSLen Brown 	/*
1272103a8feaSLen Brown 	 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1273103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
1274103a8feaSLen Brown 	 */
1275103a8feaSLen Brown 	asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
12768209e054SThomas Renninger 	has_invariant_tsc = edx & (1 << 8);
1277103a8feaSLen Brown 
1278103a8feaSLen Brown 	if (!has_invariant_tsc) {
1279103a8feaSLen Brown 		fprintf(stderr, "No invariant TSC\n");
1280103a8feaSLen Brown 		exit(1);
1281103a8feaSLen Brown 	}
1282103a8feaSLen Brown 
1283103a8feaSLen Brown 	/*
1284103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1285103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
1286103a8feaSLen Brown 	 */
1287103a8feaSLen Brown 
1288103a8feaSLen Brown 	asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
12898209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
1290103a8feaSLen Brown 	if (!has_aperf) {
1291103a8feaSLen Brown 		fprintf(stderr, "No APERF MSR\n");
1292103a8feaSLen Brown 		exit(1);
1293103a8feaSLen Brown 	}
1294103a8feaSLen Brown 
1295103a8feaSLen Brown 	do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1296103a8feaSLen Brown 	do_nhm_cstates = genuine_intel;	/* all Intel w/ non-stop TSC have NHM counters */
1297103a8feaSLen Brown 	do_snb_cstates = is_snb(family, model);
1298103a8feaSLen Brown 	bclk = discover_bclk(family, model);
1299103a8feaSLen Brown 
1300103a8feaSLen Brown 	do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
13016574a5d5SLen Brown 	do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
1302103a8feaSLen Brown }
1303103a8feaSLen Brown 
1304103a8feaSLen Brown 
1305103a8feaSLen Brown void usage()
1306103a8feaSLen Brown {
1307*8e180f3cSLen Brown 	fprintf(stderr, "%s: [-v][-d MSR#][-D MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
1308103a8feaSLen Brown 		progname);
1309103a8feaSLen Brown 	exit(1);
1310103a8feaSLen Brown }
1311103a8feaSLen Brown 
1312103a8feaSLen Brown 
1313103a8feaSLen Brown /*
1314103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
1315103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
1316103a8feaSLen Brown  */
1317103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
1318103a8feaSLen Brown {
1319103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
1320103a8feaSLen Brown 		return 1;
1321103a8feaSLen Brown 	else
1322103a8feaSLen Brown 		return 0;
1323103a8feaSLen Brown }
1324103a8feaSLen Brown 
1325103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
1326103a8feaSLen Brown {
1327103a8feaSLen Brown 	return 0;
1328103a8feaSLen Brown }
1329103a8feaSLen Brown 
1330c98d5d94SLen Brown void topology_probe()
1331c98d5d94SLen Brown {
1332c98d5d94SLen Brown 	int i;
1333c98d5d94SLen Brown 	int max_core_id = 0;
1334c98d5d94SLen Brown 	int max_package_id = 0;
1335c98d5d94SLen Brown 	int max_siblings = 0;
1336c98d5d94SLen Brown 	struct cpu_topology {
1337c98d5d94SLen Brown 		int core_id;
1338c98d5d94SLen Brown 		int physical_package_id;
1339c98d5d94SLen Brown 	} *cpus;
1340c98d5d94SLen Brown 
1341c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
1342c98d5d94SLen Brown 	topo.num_cpus = 0;
1343c98d5d94SLen Brown 	topo.max_cpu_num = 0;
1344c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
1345c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
1346c98d5d94SLen Brown 		show_cpu = 1;
1347c98d5d94SLen Brown 
1348c98d5d94SLen Brown 	if (verbose > 1)
1349c98d5d94SLen Brown 		fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1350c98d5d94SLen Brown 
1351c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
1352c98d5d94SLen Brown 	if (cpus == NULL) {
1353c98d5d94SLen Brown 		perror("calloc cpus");
1354c98d5d94SLen Brown 		exit(1);
1355c98d5d94SLen Brown 	}
1356c98d5d94SLen Brown 
1357c98d5d94SLen Brown 	/*
1358c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
1359c98d5d94SLen Brown 	 */
1360c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1361c98d5d94SLen Brown 	if (cpu_present_set == NULL) {
1362c98d5d94SLen Brown 		perror("CPU_ALLOC");
1363c98d5d94SLen Brown 		exit(3);
1364c98d5d94SLen Brown 	}
1365c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1366c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1367c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
1368c98d5d94SLen Brown 
1369c98d5d94SLen Brown 	/*
1370c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
1371c98d5d94SLen Brown 	 */
1372c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1373c98d5d94SLen Brown 	if (cpu_affinity_set == NULL) {
1374c98d5d94SLen Brown 		perror("CPU_ALLOC");
1375c98d5d94SLen Brown 		exit(3);
1376c98d5d94SLen Brown 	}
1377c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1378c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1379c98d5d94SLen Brown 
1380c98d5d94SLen Brown 
1381c98d5d94SLen Brown 	/*
1382c98d5d94SLen Brown 	 * For online cpus
1383c98d5d94SLen Brown 	 * find max_core_id, max_package_id
1384c98d5d94SLen Brown 	 */
1385c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
1386c98d5d94SLen Brown 		int siblings;
1387c98d5d94SLen Brown 
1388c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
1389c98d5d94SLen Brown 			if (verbose > 1)
1390c98d5d94SLen Brown 				fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1391c98d5d94SLen Brown 			continue;
1392c98d5d94SLen Brown 		}
1393c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
1394c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
1395c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
1396c98d5d94SLen Brown 
1397c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
1398c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
1399c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
1400c98d5d94SLen Brown 
1401c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
1402c98d5d94SLen Brown 		if (siblings > max_siblings)
1403c98d5d94SLen Brown 			max_siblings = siblings;
1404c98d5d94SLen Brown 		if (verbose > 1)
1405c98d5d94SLen Brown 			fprintf(stderr, "cpu %d pkg %d core %d\n",
1406c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
1407c98d5d94SLen Brown 	}
1408c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
1409c98d5d94SLen Brown 	if (verbose > 1)
1410c98d5d94SLen Brown 		fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
1411c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
1412c98d5d94SLen Brown 	if (!summary_only && topo.num_cores_per_pkg > 1)
1413c98d5d94SLen Brown 		show_core = 1;
1414c98d5d94SLen Brown 
1415c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
1416c98d5d94SLen Brown 	if (verbose > 1)
1417c98d5d94SLen Brown 		fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
1418c98d5d94SLen Brown 			max_package_id, topo.num_packages);
1419c98d5d94SLen Brown 	if (!summary_only && topo.num_packages > 1)
1420c98d5d94SLen Brown 		show_pkg = 1;
1421c98d5d94SLen Brown 
1422c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
1423c98d5d94SLen Brown 	if (verbose > 1)
1424c98d5d94SLen Brown 		fprintf(stderr, "max_siblings %d\n", max_siblings);
1425c98d5d94SLen Brown 
1426c98d5d94SLen Brown 	free(cpus);
1427c98d5d94SLen Brown }
1428c98d5d94SLen Brown 
1429c98d5d94SLen Brown void
1430c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1431c98d5d94SLen Brown {
1432c98d5d94SLen Brown 	int i;
1433c98d5d94SLen Brown 
1434c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1435c98d5d94SLen Brown 		topo.num_packages, sizeof(struct thread_data));
1436c98d5d94SLen Brown 	if (*t == NULL)
1437c98d5d94SLen Brown 		goto error;
1438c98d5d94SLen Brown 
1439c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
1440c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
1441c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
1442c98d5d94SLen Brown 
1443c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1444c98d5d94SLen Brown 		sizeof(struct core_data));
1445c98d5d94SLen Brown 	if (*c == NULL)
1446c98d5d94SLen Brown 		goto error;
1447c98d5d94SLen Brown 
1448c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1449c98d5d94SLen Brown 		(*c)[i].core_id = -1;
1450c98d5d94SLen Brown 
1451c98d5d94SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
1452c98d5d94SLen Brown 	if (*p == NULL)
1453c98d5d94SLen Brown 		goto error;
1454c98d5d94SLen Brown 
1455c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
1456c98d5d94SLen Brown 		(*p)[i].package_id = i;
1457c98d5d94SLen Brown 
1458c98d5d94SLen Brown 	return;
1459c98d5d94SLen Brown error:
1460c98d5d94SLen Brown 	perror("calloc counters");
1461c98d5d94SLen Brown 	exit(1);
1462c98d5d94SLen Brown }
1463c98d5d94SLen Brown /*
1464c98d5d94SLen Brown  * init_counter()
1465c98d5d94SLen Brown  *
1466c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
1467c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1468c98d5d94SLen Brown  *
1469c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
1470c98d5d94SLen Brown  */
1471c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1472c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
1473c98d5d94SLen Brown 	int pkg_num, int cpu_id)
1474c98d5d94SLen Brown {
1475c98d5d94SLen Brown 	struct thread_data *t;
1476c98d5d94SLen Brown 	struct core_data *c;
1477c98d5d94SLen Brown 	struct pkg_data *p;
1478c98d5d94SLen Brown 
1479c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1480c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
1481c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
1482c98d5d94SLen Brown 
1483c98d5d94SLen Brown 	t->cpu_id = cpu_id;
1484c98d5d94SLen Brown 	if (thread_num == 0) {
1485c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1486c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
1487c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1488c98d5d94SLen Brown 	}
1489c98d5d94SLen Brown 
1490c98d5d94SLen Brown 	c->core_id = core_num;
1491c98d5d94SLen Brown 	p->package_id = pkg_num;
1492c98d5d94SLen Brown }
1493c98d5d94SLen Brown 
1494c98d5d94SLen Brown 
1495c98d5d94SLen Brown int initialize_counters(int cpu_id)
1496c98d5d94SLen Brown {
1497c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
1498c98d5d94SLen Brown 
1499c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
1500c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
1501c98d5d94SLen Brown 
1502c98d5d94SLen Brown 	if (cpu_is_first_sibling_in_core(cpu_id)) {
1503c98d5d94SLen Brown 		my_thread_id = 0;
1504c98d5d94SLen Brown 		topo.num_cores++;
1505c98d5d94SLen Brown 	} else {
1506c98d5d94SLen Brown 		my_thread_id = 1;
1507c98d5d94SLen Brown 	}
1508c98d5d94SLen Brown 
1509c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1510c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1511c98d5d94SLen Brown 	return 0;
1512c98d5d94SLen Brown }
1513c98d5d94SLen Brown 
1514c98d5d94SLen Brown void allocate_output_buffer()
1515c98d5d94SLen Brown {
1516c98d5d94SLen Brown 	output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1517c98d5d94SLen Brown 	outp = output_buffer;
1518c98d5d94SLen Brown 	if (outp == NULL) {
1519c98d5d94SLen Brown 		perror("calloc");
1520c98d5d94SLen Brown 		exit(-1);
1521c98d5d94SLen Brown 	}
1522c98d5d94SLen Brown }
1523c98d5d94SLen Brown 
1524c98d5d94SLen Brown void setup_all_buffers(void)
1525c98d5d94SLen Brown {
1526c98d5d94SLen Brown 	topology_probe();
1527c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
1528c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
1529c98d5d94SLen Brown 	allocate_output_buffer();
1530c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
1531c98d5d94SLen Brown }
1532103a8feaSLen Brown void turbostat_init()
1533103a8feaSLen Brown {
1534103a8feaSLen Brown 	check_cpuid();
1535103a8feaSLen Brown 
1536103a8feaSLen Brown 	check_dev_msr();
1537103a8feaSLen Brown 	check_super_user();
1538103a8feaSLen Brown 
1539c98d5d94SLen Brown 	setup_all_buffers();
1540103a8feaSLen Brown 
1541103a8feaSLen Brown 	if (verbose)
1542c98d5d94SLen Brown 		print_verbose_header();
1543103a8feaSLen Brown }
1544103a8feaSLen Brown 
1545103a8feaSLen Brown int fork_it(char **argv)
1546103a8feaSLen Brown {
1547103a8feaSLen Brown 	pid_t child_pid;
1548d15cf7c1SLen Brown 
1549c98d5d94SLen Brown 	for_all_cpus(get_counters, EVEN_COUNTERS);
1550d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
1551d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
1552103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
1553103a8feaSLen Brown 
1554103a8feaSLen Brown 	child_pid = fork();
1555103a8feaSLen Brown 	if (!child_pid) {
1556103a8feaSLen Brown 		/* child */
1557103a8feaSLen Brown 		execvp(argv[0], argv);
1558103a8feaSLen Brown 	} else {
1559103a8feaSLen Brown 		int status;
1560103a8feaSLen Brown 
1561103a8feaSLen Brown 		/* parent */
1562103a8feaSLen Brown 		if (child_pid == -1) {
1563103a8feaSLen Brown 			perror("fork");
1564103a8feaSLen Brown 			exit(1);
1565103a8feaSLen Brown 		}
1566103a8feaSLen Brown 
1567103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
1568103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
1569103a8feaSLen Brown 		if (waitpid(child_pid, &status, 0) == -1) {
1570103a8feaSLen Brown 			perror("wait");
1571103a8feaSLen Brown 			exit(1);
1572103a8feaSLen Brown 		}
1573103a8feaSLen Brown 	}
1574c98d5d94SLen Brown 	/*
1575c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
1576c98d5d94SLen Brown 	 * because re-starting is problematic when forking
1577c98d5d94SLen Brown 	 */
1578c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
1579103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
1580103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
1581c98d5d94SLen Brown 	for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1582c98d5d94SLen Brown 	compute_average(EVEN_COUNTERS);
1583c98d5d94SLen Brown 	format_all_counters(EVEN_COUNTERS);
1584c98d5d94SLen Brown 	flush_stderr();
1585103a8feaSLen Brown 
15866eab04a8SJustin P. Mattock 	fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
1587103a8feaSLen Brown 
1588103a8feaSLen Brown 	return 0;
1589103a8feaSLen Brown }
1590103a8feaSLen Brown 
1591103a8feaSLen Brown void cmdline(int argc, char **argv)
1592103a8feaSLen Brown {
1593103a8feaSLen Brown 	int opt;
1594103a8feaSLen Brown 
1595103a8feaSLen Brown 	progname = argv[0];
1596103a8feaSLen Brown 
1597*8e180f3cSLen Brown 	while ((opt = getopt(argc, argv, "+cpsvid:D:m:M:")) != -1) {
1598103a8feaSLen Brown 		switch (opt) {
1599c98d5d94SLen Brown 		case 'c':
1600c98d5d94SLen Brown 			show_core_only++;
1601c98d5d94SLen Brown 			break;
1602c98d5d94SLen Brown 		case 'p':
1603c98d5d94SLen Brown 			show_pkg_only++;
1604c98d5d94SLen Brown 			break;
1605e23da037SLen Brown 		case 's':
1606e23da037SLen Brown 			summary_only++;
1607e23da037SLen Brown 			break;
1608103a8feaSLen Brown 		case 'v':
1609103a8feaSLen Brown 			verbose++;
1610103a8feaSLen Brown 			break;
1611103a8feaSLen Brown 		case 'i':
1612103a8feaSLen Brown 			interval_sec = atoi(optarg);
1613103a8feaSLen Brown 			break;
1614*8e180f3cSLen Brown 		case 'd':
1615*8e180f3cSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset32);
1616*8e180f3cSLen Brown 			break;
1617*8e180f3cSLen Brown 		case 'D':
1618*8e180f3cSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset64);
1619*8e180f3cSLen Brown 			break;
16202f32edf1SLen Brown 		case 'm':
16212f32edf1SLen Brown 			sscanf(optarg, "%x", &extra_msr_offset32);
16222f32edf1SLen Brown 			break;
16232f32edf1SLen Brown 		case 'M':
16242f32edf1SLen Brown 			sscanf(optarg, "%x", &extra_msr_offset64);
1625103a8feaSLen Brown 			break;
1626103a8feaSLen Brown 		default:
1627103a8feaSLen Brown 			usage();
1628103a8feaSLen Brown 		}
1629103a8feaSLen Brown 	}
1630103a8feaSLen Brown }
1631103a8feaSLen Brown 
1632103a8feaSLen Brown int main(int argc, char **argv)
1633103a8feaSLen Brown {
1634103a8feaSLen Brown 	cmdline(argc, argv);
1635103a8feaSLen Brown 
1636103a8feaSLen Brown 	if (verbose > 1)
1637c98d5d94SLen Brown 		fprintf(stderr, "turbostat v2.0 May 16, 2012"
1638103a8feaSLen Brown 			" - Len Brown <lenb@kernel.org>\n");
1639103a8feaSLen Brown 
1640103a8feaSLen Brown 	turbostat_init();
1641103a8feaSLen Brown 
1642103a8feaSLen Brown 	/*
1643103a8feaSLen Brown 	 * if any params left, it must be a command to fork
1644103a8feaSLen Brown 	 */
1645103a8feaSLen Brown 	if (argc - optind)
1646103a8feaSLen Brown 		return fork_it(argv + optind);
1647103a8feaSLen Brown 	else
1648103a8feaSLen Brown 		turbostat_loop();
1649103a8feaSLen Brown 
1650103a8feaSLen Brown 	return 0;
1651103a8feaSLen Brown }
1652