xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 98481e79b60a50d699b79292ff1b7e56e7fa8425)
1103a8feaSLen Brown /*
2103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
3103a8feaSLen Brown  * on modern Intel turbo-capable processors.
4103a8feaSLen Brown  *
5144b44b1SLen Brown  * Copyright (c) 2013 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
23b731f311SJosh Triplett #include MSRHEADER
2495aebc44SJosh Triplett #include <stdarg.h>
25103a8feaSLen Brown #include <stdio.h>
26b2c95d90SJosh Triplett #include <err.h>
27103a8feaSLen Brown #include <unistd.h>
28103a8feaSLen Brown #include <sys/types.h>
29103a8feaSLen Brown #include <sys/wait.h>
30103a8feaSLen Brown #include <sys/stat.h>
31103a8feaSLen Brown #include <sys/resource.h>
32103a8feaSLen Brown #include <fcntl.h>
33103a8feaSLen Brown #include <signal.h>
34103a8feaSLen Brown #include <sys/time.h>
35103a8feaSLen Brown #include <stdlib.h>
36103a8feaSLen Brown #include <dirent.h>
37103a8feaSLen Brown #include <string.h>
38103a8feaSLen Brown #include <ctype.h>
3988c3281fSLen Brown #include <sched.h>
402b92865eSJosh Triplett #include <cpuid.h>
41*98481e79SLen Brown #include <linux/capability.h>
42*98481e79SLen Brown #include <errno.h>
43103a8feaSLen Brown 
44103a8feaSLen Brown char *proc_stat = "/proc/stat";
45103a8feaSLen Brown unsigned int interval_sec = 5;	/* set with -i interval_sec */
46103a8feaSLen Brown unsigned int verbose;		/* set with -v */
47889facbeSLen Brown unsigned int rapl_verbose;	/* set with -R */
485c56be9aSDirk Brandewie unsigned int rapl_joules;	/* set with -J */
49889facbeSLen Brown unsigned int thermal_verbose;	/* set with -T */
50f591c38bSAndy Shevchenko unsigned int summary_only;	/* set with -S */
513b4d5c7fSAndy Shevchenko unsigned int dump_only;		/* set with -s */
52103a8feaSLen Brown unsigned int skip_c0;
53103a8feaSLen Brown unsigned int skip_c1;
54103a8feaSLen Brown unsigned int do_nhm_cstates;
55103a8feaSLen Brown unsigned int do_snb_cstates;
56ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10;
57144b44b1SLen Brown unsigned int do_slm_cstates;
58144b44b1SLen Brown unsigned int use_c1_residency_msr;
59103a8feaSLen Brown unsigned int has_aperf;
60889facbeSLen Brown unsigned int has_epb;
61fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
62103a8feaSLen Brown unsigned int genuine_intel;
63103a8feaSLen Brown unsigned int has_invariant_tsc;
64103a8feaSLen Brown unsigned int do_nehalem_platform_info;
65103a8feaSLen Brown unsigned int do_nehalem_turbo_ratio_limit;
666574a5d5SLen Brown unsigned int do_ivt_turbo_ratio_limit;
672f32edf1SLen Brown unsigned int extra_msr_offset32;
682f32edf1SLen Brown unsigned int extra_msr_offset64;
698e180f3cSLen Brown unsigned int extra_delta_offset32;
708e180f3cSLen Brown unsigned int extra_delta_offset64;
711ed51011SLen Brown int do_smi;
72103a8feaSLen Brown double bclk;
73103a8feaSLen Brown unsigned int show_pkg;
74103a8feaSLen Brown unsigned int show_core;
75103a8feaSLen Brown unsigned int show_cpu;
76c98d5d94SLen Brown unsigned int show_pkg_only;
77c98d5d94SLen Brown unsigned int show_core_only;
78c98d5d94SLen Brown char *output_buffer, *outp;
79889facbeSLen Brown unsigned int do_rapl;
80889facbeSLen Brown unsigned int do_dts;
81889facbeSLen Brown unsigned int do_ptm;
82889facbeSLen Brown unsigned int tcc_activation_temp;
83889facbeSLen Brown unsigned int tcc_activation_temp_override;
84889facbeSLen Brown double rapl_power_units, rapl_energy_units, rapl_time_units;
85889facbeSLen Brown double rapl_joule_counter_range;
86889facbeSLen Brown 
87889facbeSLen Brown #define RAPL_PKG		(1 << 0)
88e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
89e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
90e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
91e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
92e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
93e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
94e6f9bb3cSLen Brown 
95889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
96e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
97e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
98e6f9bb3cSLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
99e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
100e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
101e6f9bb3cSLen Brown 
102e6f9bb3cSLen Brown #define RAPL_CORES		(1 << 5)
103e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
104e6f9bb3cSLen Brown 					/* 0x639 MSR_PP0_ENERGY_STATUS */
105e6f9bb3cSLen Brown #define RAPL_CORE_POLICY	(1 << 6)
106e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
107e6f9bb3cSLen Brown 
108e6f9bb3cSLen Brown 
109e6f9bb3cSLen Brown #define RAPL_GFX		(1 << 7)
110e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
111e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
112e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
113889facbeSLen Brown #define	TJMAX_DEFAULT	100
114889facbeSLen Brown 
115889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
116103a8feaSLen Brown 
117103a8feaSLen Brown int aperf_mperf_unstable;
118103a8feaSLen Brown int backwards_count;
119103a8feaSLen Brown char *progname;
120103a8feaSLen Brown 
121c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set;
122c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize;
123c98d5d94SLen Brown 
124c98d5d94SLen Brown struct thread_data {
125c98d5d94SLen Brown 	unsigned long long tsc;
126c98d5d94SLen Brown 	unsigned long long aperf;
127c98d5d94SLen Brown 	unsigned long long mperf;
128144b44b1SLen Brown 	unsigned long long c1;
1292f32edf1SLen Brown 	unsigned long long extra_msr64;
1308e180f3cSLen Brown 	unsigned long long extra_delta64;
1318e180f3cSLen Brown 	unsigned long long extra_msr32;
1328e180f3cSLen Brown 	unsigned long long extra_delta32;
1331ed51011SLen Brown 	unsigned int smi_count;
134c98d5d94SLen Brown 	unsigned int cpu_id;
135c98d5d94SLen Brown 	unsigned int flags;
136c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
137c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
138c98d5d94SLen Brown } *thread_even, *thread_odd;
139c98d5d94SLen Brown 
140c98d5d94SLen Brown struct core_data {
141c98d5d94SLen Brown 	unsigned long long c3;
142c98d5d94SLen Brown 	unsigned long long c6;
143c98d5d94SLen Brown 	unsigned long long c7;
144889facbeSLen Brown 	unsigned int core_temp_c;
145c98d5d94SLen Brown 	unsigned int core_id;
146c98d5d94SLen Brown } *core_even, *core_odd;
147c98d5d94SLen Brown 
148c98d5d94SLen Brown struct pkg_data {
149c98d5d94SLen Brown 	unsigned long long pc2;
150c98d5d94SLen Brown 	unsigned long long pc3;
151c98d5d94SLen Brown 	unsigned long long pc6;
152c98d5d94SLen Brown 	unsigned long long pc7;
153ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
154ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
155ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
156c98d5d94SLen Brown 	unsigned int package_id;
157889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
158889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
159889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
160889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
161889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
162889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
163889facbeSLen Brown 	unsigned int pkg_temp_c;
164889facbeSLen Brown 
165c98d5d94SLen Brown } *package_even, *package_odd;
166c98d5d94SLen Brown 
167c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
168c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
169c98d5d94SLen Brown 
170c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
171c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
172c98d5d94SLen Brown 		topo.num_threads_per_core + \
173c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
174c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
175c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
176c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
177c98d5d94SLen Brown 
178c98d5d94SLen Brown struct system_summary {
179c98d5d94SLen Brown 	struct thread_data threads;
180c98d5d94SLen Brown 	struct core_data cores;
181c98d5d94SLen Brown 	struct pkg_data packages;
182c98d5d94SLen Brown } sum, average;
183c98d5d94SLen Brown 
184c98d5d94SLen Brown 
185c98d5d94SLen Brown struct topo_params {
186c98d5d94SLen Brown 	int num_packages;
187103a8feaSLen Brown 	int num_cpus;
188c98d5d94SLen Brown 	int num_cores;
189c98d5d94SLen Brown 	int max_cpu_num;
190c98d5d94SLen Brown 	int num_cores_per_pkg;
191c98d5d94SLen Brown 	int num_threads_per_core;
192c98d5d94SLen Brown } topo;
193103a8feaSLen Brown 
194c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
195103a8feaSLen Brown 
196c98d5d94SLen Brown void setup_all_buffers(void);
197103a8feaSLen Brown 
198c98d5d94SLen Brown int cpu_is_not_present(int cpu)
199d15cf7c1SLen Brown {
200c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
201c98d5d94SLen Brown }
202c98d5d94SLen Brown /*
203c98d5d94SLen Brown  * run func(thread, core, package) in topology order
204c98d5d94SLen Brown  * skip non-present cpus
205c98d5d94SLen Brown  */
206c98d5d94SLen Brown 
207c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
208c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
209c98d5d94SLen Brown {
210c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
211c98d5d94SLen Brown 
212c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
213c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
214c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
215c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
216c98d5d94SLen Brown 				struct thread_data *t;
217c98d5d94SLen Brown 				struct core_data *c;
218c98d5d94SLen Brown 				struct pkg_data *p;
219c98d5d94SLen Brown 
220c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
221c98d5d94SLen Brown 
222c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
223c98d5d94SLen Brown 					continue;
224c98d5d94SLen Brown 
225c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
226c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
227c98d5d94SLen Brown 
228c98d5d94SLen Brown 				retval = func(t, c, p);
229c98d5d94SLen Brown 				if (retval)
230c98d5d94SLen Brown 					return retval;
231c98d5d94SLen Brown 			}
232c98d5d94SLen Brown 		}
233c98d5d94SLen Brown 	}
234d15cf7c1SLen Brown 	return 0;
235d15cf7c1SLen Brown }
236d15cf7c1SLen Brown 
23788c3281fSLen Brown int cpu_migrate(int cpu)
23888c3281fSLen Brown {
239c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
240c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
241c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
24288c3281fSLen Brown 		return -1;
24388c3281fSLen Brown 	else
24488c3281fSLen Brown 		return 0;
24588c3281fSLen Brown }
24688c3281fSLen Brown 
24715aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
248103a8feaSLen Brown {
249103a8feaSLen Brown 	ssize_t retval;
250103a8feaSLen Brown 	char pathname[32];
251103a8feaSLen Brown 	int fd;
252103a8feaSLen Brown 
253103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
254103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
25515aaa346SLen Brown 	if (fd < 0)
256*98481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
257103a8feaSLen Brown 
25815aaa346SLen Brown 	retval = pread(fd, msr, sizeof *msr, offset);
259103a8feaSLen Brown 	close(fd);
26015aaa346SLen Brown 
261*98481e79SLen Brown 	if (retval != sizeof *msr)
262*98481e79SLen Brown 		err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
26315aaa346SLen Brown 
26415aaa346SLen Brown 	return 0;
265103a8feaSLen Brown }
266103a8feaSLen Brown 
267fc04cc67SLen Brown /*
268fc04cc67SLen Brown  * Example Format w/ field column widths:
269fc04cc67SLen Brown  *
270fc04cc67SLen Brown  *  Package    Core     CPU Avg_MHz Bzy_MHz TSC_MHz     SMI   %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp  PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
271e7c95ff3SLen Brown  * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
272fc04cc67SLen Brown  */
273fc04cc67SLen Brown 
274a829eb4dSLen Brown void print_header(void)
275103a8feaSLen Brown {
276103a8feaSLen Brown 	if (show_pkg)
277fc04cc67SLen Brown 		outp += sprintf(outp, " Package");
278103a8feaSLen Brown 	if (show_core)
279fc04cc67SLen Brown 		outp += sprintf(outp, "    Core");
280103a8feaSLen Brown 	if (show_cpu)
281c98d5d94SLen Brown 		outp += sprintf(outp, "     CPU");
282103a8feaSLen Brown 	if (has_aperf)
283fc04cc67SLen Brown 		outp += sprintf(outp, " Avg_MHz");
284fc04cc67SLen Brown 	if (do_nhm_cstates)
285fc04cc67SLen Brown 		outp += sprintf(outp, "   %%Busy");
286fc04cc67SLen Brown 	if (has_aperf)
287fc04cc67SLen Brown 		outp += sprintf(outp, " Bzy_MHz");
288fc04cc67SLen Brown 	outp += sprintf(outp, " TSC_MHz");
2891ed51011SLen Brown 	if (do_smi)
2901ed51011SLen Brown 		outp += sprintf(outp, "     SMI");
2918e180f3cSLen Brown 	if (extra_delta_offset32)
292f9240813SLen Brown 		outp += sprintf(outp, "  count 0x%03X", extra_delta_offset32);
2938e180f3cSLen Brown 	if (extra_delta_offset64)
294f9240813SLen Brown 		outp += sprintf(outp, "  COUNT 0x%03X", extra_delta_offset64);
2952f32edf1SLen Brown 	if (extra_msr_offset32)
2968e180f3cSLen Brown 		outp += sprintf(outp, "   MSR 0x%03X", extra_msr_offset32);
2972f32edf1SLen Brown 	if (extra_msr_offset64)
2988e180f3cSLen Brown 		outp += sprintf(outp, "           MSR 0x%03X", extra_msr_offset64);
299103a8feaSLen Brown 	if (do_nhm_cstates)
300fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c1");
301144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates)
302fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c3");
303103a8feaSLen Brown 	if (do_nhm_cstates)
304fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c6");
305103a8feaSLen Brown 	if (do_snb_cstates)
306fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c7");
307889facbeSLen Brown 
308889facbeSLen Brown 	if (do_dts)
309fc04cc67SLen Brown 		outp += sprintf(outp, " CoreTmp");
310889facbeSLen Brown 	if (do_ptm)
311fc04cc67SLen Brown 		outp += sprintf(outp, "  PkgTmp");
312889facbeSLen Brown 
313103a8feaSLen Brown 	if (do_snb_cstates)
314fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc2");
315144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates)
316fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc3");
317144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates)
318fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc6");
319103a8feaSLen Brown 	if (do_snb_cstates)
320fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc7");
321ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
322fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc8");
323fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc9");
324fc04cc67SLen Brown 		outp += sprintf(outp, " Pk%%pc10");
325ca58710fSKristen Carlson Accardi 	}
326103a8feaSLen Brown 
3275c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
328889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
329fc04cc67SLen Brown 			outp += sprintf(outp, " PkgWatt");
330889facbeSLen Brown 		if (do_rapl & RAPL_CORES)
331fc04cc67SLen Brown 			outp += sprintf(outp, " CorWatt");
332889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
333fc04cc67SLen Brown 			outp += sprintf(outp, " GFXWatt");
334889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
335fc04cc67SLen Brown 			outp += sprintf(outp, " RAMWatt");
336889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
337889facbeSLen Brown 			outp += sprintf(outp, "   PKG_%%");
338889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
339889facbeSLen Brown 			outp += sprintf(outp, "   RAM_%%");
3405c56be9aSDirk Brandewie 	} else {
3415c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
3425c56be9aSDirk Brandewie 			outp += sprintf(outp, "   Pkg_J");
3435c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
3445c56be9aSDirk Brandewie 			outp += sprintf(outp, "   Cor_J");
3455c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
3465c56be9aSDirk Brandewie 			outp += sprintf(outp, "   GFX_J");
3475c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
3485c56be9aSDirk Brandewie 			outp += sprintf(outp, "   RAM_W");
3495c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
3505c56be9aSDirk Brandewie 			outp += sprintf(outp, "   PKG_%%");
3515c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
3525c56be9aSDirk Brandewie 			outp += sprintf(outp, "   RAM_%%");
3535c56be9aSDirk Brandewie 		outp += sprintf(outp, "   time");
354889facbeSLen Brown 
3555c56be9aSDirk Brandewie 	}
356c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
357103a8feaSLen Brown }
358103a8feaSLen Brown 
359c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
360c98d5d94SLen Brown 	struct pkg_data *p)
361103a8feaSLen Brown {
3623b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
363c98d5d94SLen Brown 
364c98d5d94SLen Brown 	if (t) {
3653b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
3663b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
3673b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
3683b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
3693b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
3703b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
3713b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %08llX\n",
3728e180f3cSLen Brown 			extra_delta_offset32, t->extra_delta32);
3733b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %016llX\n",
3748e180f3cSLen Brown 			extra_delta_offset64, t->extra_delta64);
3753b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %08llX\n",
3762f32edf1SLen Brown 			extra_msr_offset32, t->extra_msr32);
3773b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %016llX\n",
3782f32edf1SLen Brown 			extra_msr_offset64, t->extra_msr64);
3791ed51011SLen Brown 		if (do_smi)
3803b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
381103a8feaSLen Brown 	}
382103a8feaSLen Brown 
383c98d5d94SLen Brown 	if (c) {
3843b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
3853b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
3863b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
3873b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
3883b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
389c98d5d94SLen Brown 	}
390103a8feaSLen Brown 
391c98d5d94SLen Brown 	if (p) {
3923b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
3933b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
3943b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
3953b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
3963b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
3973b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
3983b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
3993b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
4003b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
4013b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
4023b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
4033b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
4043b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
4053b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
4063b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
4073b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
4083b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
409c98d5d94SLen Brown 	}
4103b4d5c7fSAndy Shevchenko 
4113b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
4123b4d5c7fSAndy Shevchenko 
413c98d5d94SLen Brown 	return 0;
414103a8feaSLen Brown }
415103a8feaSLen Brown 
416e23da037SLen Brown /*
417e23da037SLen Brown  * column formatting convention & formats
418e23da037SLen Brown  */
419c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
420c98d5d94SLen Brown 	struct pkg_data *p)
421103a8feaSLen Brown {
422103a8feaSLen Brown 	double interval_float;
423fc04cc67SLen Brown 	char *fmt8;
424103a8feaSLen Brown 
425c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
426c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
427c98d5d94SLen Brown 		return 0;
428c98d5d94SLen Brown 
429c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
430c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
431c98d5d94SLen Brown 		return 0;
432c98d5d94SLen Brown 
433103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
434103a8feaSLen Brown 
435c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
436c98d5d94SLen Brown 	if (t == &average.threads) {
437103a8feaSLen Brown 		if (show_pkg)
438fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
439103a8feaSLen Brown 		if (show_core)
440fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
441103a8feaSLen Brown 		if (show_cpu)
442fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
443103a8feaSLen Brown 	} else {
444c98d5d94SLen Brown 		if (show_pkg) {
445c98d5d94SLen Brown 			if (p)
446fc04cc67SLen Brown 				outp += sprintf(outp, "%8d", p->package_id);
447c98d5d94SLen Brown 			else
448fc04cc67SLen Brown 				outp += sprintf(outp, "       -");
449c98d5d94SLen Brown 		}
450c98d5d94SLen Brown 		if (show_core) {
451c98d5d94SLen Brown 			if (c)
452fc04cc67SLen Brown 				outp += sprintf(outp, "%8d", c->core_id);
453c98d5d94SLen Brown 			else
454fc04cc67SLen Brown 				outp += sprintf(outp, "       -");
455c98d5d94SLen Brown 		}
456103a8feaSLen Brown 		if (show_cpu)
457fc04cc67SLen Brown 			outp += sprintf(outp, "%8d", t->cpu_id);
458103a8feaSLen Brown 	}
459fc04cc67SLen Brown 
460fc04cc67SLen Brown 	/* AvgMHz */
461fc04cc67SLen Brown 	if (has_aperf)
462fc04cc67SLen Brown 		outp += sprintf(outp, "%8.0f",
463fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
464fc04cc67SLen Brown 
465103a8feaSLen Brown 	/* %c0 */
466103a8feaSLen Brown 	if (do_nhm_cstates) {
467103a8feaSLen Brown 		if (!skip_c0)
468fc04cc67SLen Brown 			outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
469103a8feaSLen Brown 		else
470fc04cc67SLen Brown 			outp += sprintf(outp, "********");
471103a8feaSLen Brown 	}
472103a8feaSLen Brown 
473fc04cc67SLen Brown 	/* BzyMHz */
474fc04cc67SLen Brown 	if (has_aperf)
475fc04cc67SLen Brown 		outp += sprintf(outp, "%8.0f",
476fc04cc67SLen Brown 			1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
477103a8feaSLen Brown 
478103a8feaSLen Brown 	/* TSC */
479fc04cc67SLen Brown 	outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
480103a8feaSLen Brown 
4811ed51011SLen Brown 	/* SMI */
4821ed51011SLen Brown 	if (do_smi)
483fc04cc67SLen Brown 		outp += sprintf(outp, "%8d", t->smi_count);
4841ed51011SLen Brown 
4858e180f3cSLen Brown 	/* delta */
4868e180f3cSLen Brown 	if (extra_delta_offset32)
4878e180f3cSLen Brown 		outp += sprintf(outp, "  %11llu", t->extra_delta32);
4888e180f3cSLen Brown 
4898e180f3cSLen Brown 	/* DELTA */
4908e180f3cSLen Brown 	if (extra_delta_offset64)
4918e180f3cSLen Brown 		outp += sprintf(outp, "  %11llu", t->extra_delta64);
4922f32edf1SLen Brown 	/* msr */
4932f32edf1SLen Brown 	if (extra_msr_offset32)
4948e180f3cSLen Brown 		outp += sprintf(outp, "  0x%08llx", t->extra_msr32);
4952f32edf1SLen Brown 
496130ff304SLen Brown 	/* MSR */
4972f32edf1SLen Brown 	if (extra_msr_offset64)
4982f32edf1SLen Brown 		outp += sprintf(outp, "  0x%016llx", t->extra_msr64);
499130ff304SLen Brown 
500103a8feaSLen Brown 	if (do_nhm_cstates) {
501103a8feaSLen Brown 		if (!skip_c1)
502fc04cc67SLen Brown 			outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
503103a8feaSLen Brown 		else
504fc04cc67SLen Brown 			outp += sprintf(outp, "********");
505103a8feaSLen Brown 	}
506c98d5d94SLen Brown 
507c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
508c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
509c98d5d94SLen Brown 		goto done;
510c98d5d94SLen Brown 
511144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates)
512fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
513103a8feaSLen Brown 	if (do_nhm_cstates)
514fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
515103a8feaSLen Brown 	if (do_snb_cstates)
516fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
517c98d5d94SLen Brown 
518889facbeSLen Brown 	if (do_dts)
519fc04cc67SLen Brown 		outp += sprintf(outp, "%8d", c->core_temp_c);
520889facbeSLen Brown 
521c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
522c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
523c98d5d94SLen Brown 		goto done;
524c98d5d94SLen Brown 
525889facbeSLen Brown 	if (do_ptm)
526fc04cc67SLen Brown 		outp += sprintf(outp, "%8d", p->pkg_temp_c);
527889facbeSLen Brown 
528103a8feaSLen Brown 	if (do_snb_cstates)
529fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
530144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates)
531fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
532144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates)
533fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
534103a8feaSLen Brown 	if (do_snb_cstates)
535fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
536ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
537fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
538fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
539fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
540ca58710fSKristen Carlson Accardi 	}
541889facbeSLen Brown 
542889facbeSLen Brown 	/*
543889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
544889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
545889facbeSLen Brown  	 */
546fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
547fc04cc67SLen Brown 		fmt8 = "%8.2f";
548fc04cc67SLen Brown 	else
549fc04cc67SLen Brown 		fmt8 = " %6.0f**";
550889facbeSLen Brown 
5515c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
552889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
553fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
554889facbeSLen Brown 		if (do_rapl & RAPL_CORES)
555fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
556889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
557fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
558889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
559fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_dram * rapl_energy_units / interval_float);
560889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
561fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
562889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
563fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
5645c56be9aSDirk Brandewie 	} else {
5655c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
566fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
5675c56be9aSDirk Brandewie 					p->energy_pkg * rapl_energy_units);
5685c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
569fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
5705c56be9aSDirk Brandewie 					p->energy_cores * rapl_energy_units);
5715c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
572fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
5735c56be9aSDirk Brandewie 					p->energy_gfx * rapl_energy_units);
5745c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
575fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
5765c56be9aSDirk Brandewie 					p->energy_dram * rapl_energy_units);
5775c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
578fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
5795c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
580fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
581fc04cc67SLen Brown 	outp += sprintf(outp, fmt8, interval_float);
582889facbeSLen Brown 
5835c56be9aSDirk Brandewie 	}
584c98d5d94SLen Brown done:
585c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
586c98d5d94SLen Brown 
587c98d5d94SLen Brown 	return 0;
588103a8feaSLen Brown }
589103a8feaSLen Brown 
590c98d5d94SLen Brown void flush_stdout()
591103a8feaSLen Brown {
592c98d5d94SLen Brown 	fputs(output_buffer, stdout);
593ddac0d68SLen Brown 	fflush(stdout);
594c98d5d94SLen Brown 	outp = output_buffer;
595c98d5d94SLen Brown }
596c98d5d94SLen Brown void flush_stderr()
597c98d5d94SLen Brown {
598c98d5d94SLen Brown 	fputs(output_buffer, stderr);
599c98d5d94SLen Brown 	outp = output_buffer;
600c98d5d94SLen Brown }
601c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
602c98d5d94SLen Brown {
603e23da037SLen Brown 	static int printed;
604103a8feaSLen Brown 
605e23da037SLen Brown 	if (!printed || !summary_only)
606103a8feaSLen Brown 		print_header();
607103a8feaSLen Brown 
608c98d5d94SLen Brown 	if (topo.num_cpus > 1)
609c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
610c98d5d94SLen Brown 			&average.packages);
611103a8feaSLen Brown 
612e23da037SLen Brown 	printed = 1;
613e23da037SLen Brown 
614e23da037SLen Brown 	if (summary_only)
615e23da037SLen Brown 		return;
616e23da037SLen Brown 
617c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
618103a8feaSLen Brown }
619103a8feaSLen Brown 
620889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
621889facbeSLen Brown 	if (new > old) {			\
622889facbeSLen Brown 		old = new - old;		\
623889facbeSLen Brown 	} else {				\
624889facbeSLen Brown 		old = 0x100000000 + new - old;	\
625889facbeSLen Brown 	}
626889facbeSLen Brown 
627c98d5d94SLen Brown void
628c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
629103a8feaSLen Brown {
630c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
631c98d5d94SLen Brown 	old->pc3 = new->pc3 - old->pc3;
632c98d5d94SLen Brown 	old->pc6 = new->pc6 - old->pc6;
633c98d5d94SLen Brown 	old->pc7 = new->pc7 - old->pc7;
634ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
635ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
636ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
637889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
638889facbeSLen Brown 
639889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
640889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
641889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
642889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
643889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
644889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
645103a8feaSLen Brown }
646103a8feaSLen Brown 
647c98d5d94SLen Brown void
648c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
649c98d5d94SLen Brown {
650c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
651c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
652c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
653889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
654103a8feaSLen Brown }
655c98d5d94SLen Brown 
656c3ae331dSLen Brown /*
657c3ae331dSLen Brown  * old = new - old
658c3ae331dSLen Brown  */
659c98d5d94SLen Brown void
660c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
661c98d5d94SLen Brown 	struct core_data *core_delta)
662c98d5d94SLen Brown {
663c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
664c98d5d94SLen Brown 
665103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
666b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
667b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
668b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
669b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
670103a8feaSLen Brown 
671c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
672c98d5d94SLen Brown 
673c98d5d94SLen Brown 	if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
674c98d5d94SLen Brown 		old->aperf = new->aperf - old->aperf;
675c98d5d94SLen Brown 		old->mperf = new->mperf - old->mperf;
676c98d5d94SLen Brown 	} else {
677c98d5d94SLen Brown 
678103a8feaSLen Brown 		if (!aperf_mperf_unstable) {
679103a8feaSLen Brown 			fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
680103a8feaSLen Brown 			fprintf(stderr, "* Frequency results do not cover entire interval *\n");
681103a8feaSLen Brown 			fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
682103a8feaSLen Brown 
683103a8feaSLen Brown 			aperf_mperf_unstable = 1;
684103a8feaSLen Brown 		}
685103a8feaSLen Brown 		/*
686103a8feaSLen Brown 		 * mperf delta is likely a huge "positive" number
687103a8feaSLen Brown 		 * can not use it for calculating c0 time
688103a8feaSLen Brown 		 */
689103a8feaSLen Brown 		skip_c0 = 1;
690103a8feaSLen Brown 		skip_c1 = 1;
691103a8feaSLen Brown 	}
692103a8feaSLen Brown 
693c98d5d94SLen Brown 
694144b44b1SLen Brown 	if (use_c1_residency_msr) {
695144b44b1SLen Brown 		/*
696144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
697144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
698144b44b1SLen Brown 		 */
699144b44b1SLen Brown 	} else {
700103a8feaSLen Brown 		/*
701c3ae331dSLen Brown 		 * As counter collection is not atomic,
702c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
703103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
704103a8feaSLen Brown 		 */
705c3ae331dSLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
706c98d5d94SLen Brown 			old->c1 = 0;
707c98d5d94SLen Brown 		else {
708c98d5d94SLen Brown 			/* normal case, derive c1 */
709c98d5d94SLen Brown 			old->c1 = old->tsc - old->mperf - core_delta->c3
710c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
711c98d5d94SLen Brown 		}
712144b44b1SLen Brown 	}
713c3ae331dSLen Brown 
714c98d5d94SLen Brown 	if (old->mperf == 0) {
715c3ae331dSLen Brown 		if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
716c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
717c98d5d94SLen Brown 	}
718103a8feaSLen Brown 
7198e180f3cSLen Brown 	old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
7208e180f3cSLen Brown 	old->extra_delta32 &= 0xFFFFFFFF;
7218e180f3cSLen Brown 
7228e180f3cSLen Brown 	old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
7238e180f3cSLen Brown 
724103a8feaSLen Brown 	/*
7258e180f3cSLen Brown 	 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
726103a8feaSLen Brown 	 */
7272f32edf1SLen Brown 	old->extra_msr32 = new->extra_msr32;
7282f32edf1SLen Brown 	old->extra_msr64 = new->extra_msr64;
7291ed51011SLen Brown 
7301ed51011SLen Brown 	if (do_smi)
7311ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
732103a8feaSLen Brown }
733c98d5d94SLen Brown 
734c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
735c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
736c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
737c98d5d94SLen Brown {
738c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
739c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
740c98d5d94SLen Brown 		delta_core(c, c2);
741c98d5d94SLen Brown 
742c98d5d94SLen Brown 	/* always calculate thread delta */
743c98d5d94SLen Brown 	delta_thread(t, t2, c2);	/* c2 is core delta */
744c98d5d94SLen Brown 
745c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
746c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
747c98d5d94SLen Brown 		delta_package(p, p2);
748c98d5d94SLen Brown 
749103a8feaSLen Brown 	return 0;
750103a8feaSLen Brown }
751103a8feaSLen Brown 
752c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
753103a8feaSLen Brown {
754c98d5d94SLen Brown 	t->tsc = 0;
755c98d5d94SLen Brown 	t->aperf = 0;
756c98d5d94SLen Brown 	t->mperf = 0;
757c98d5d94SLen Brown 	t->c1 = 0;
758103a8feaSLen Brown 
7591ed51011SLen Brown 	t->smi_count = 0;
7608e180f3cSLen Brown 	t->extra_delta32 = 0;
7618e180f3cSLen Brown 	t->extra_delta64 = 0;
7628e180f3cSLen Brown 
763c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
764c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
765c98d5d94SLen Brown 
766c98d5d94SLen Brown 	c->c3 = 0;
767c98d5d94SLen Brown 	c->c6 = 0;
768c98d5d94SLen Brown 	c->c7 = 0;
769889facbeSLen Brown 	c->core_temp_c = 0;
770c98d5d94SLen Brown 
771c98d5d94SLen Brown 	p->pc2 = 0;
772c98d5d94SLen Brown 	p->pc3 = 0;
773c98d5d94SLen Brown 	p->pc6 = 0;
774c98d5d94SLen Brown 	p->pc7 = 0;
775ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
776ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
777ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
778889facbeSLen Brown 
779889facbeSLen Brown 	p->energy_pkg = 0;
780889facbeSLen Brown 	p->energy_dram = 0;
781889facbeSLen Brown 	p->energy_cores = 0;
782889facbeSLen Brown 	p->energy_gfx = 0;
783889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
784889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
785889facbeSLen Brown 	p->pkg_temp_c = 0;
786103a8feaSLen Brown }
787c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
788c98d5d94SLen Brown 	struct pkg_data *p)
789103a8feaSLen Brown {
790c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
791c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
792c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
793c98d5d94SLen Brown 	average.threads.c1 += t->c1;
79415aaa346SLen Brown 
7958e180f3cSLen Brown 	average.threads.extra_delta32 += t->extra_delta32;
7968e180f3cSLen Brown 	average.threads.extra_delta64 += t->extra_delta64;
7978e180f3cSLen Brown 
798c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
799c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
800c98d5d94SLen Brown 		return 0;
801c98d5d94SLen Brown 
802c98d5d94SLen Brown 	average.cores.c3 += c->c3;
803c98d5d94SLen Brown 	average.cores.c6 += c->c6;
804c98d5d94SLen Brown 	average.cores.c7 += c->c7;
805c98d5d94SLen Brown 
806889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
807889facbeSLen Brown 
808c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
809c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
810c98d5d94SLen Brown 		return 0;
811c98d5d94SLen Brown 
812c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
813c98d5d94SLen Brown 	average.packages.pc3 += p->pc3;
814c98d5d94SLen Brown 	average.packages.pc6 += p->pc6;
815c98d5d94SLen Brown 	average.packages.pc7 += p->pc7;
816ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
817ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
818ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
819c98d5d94SLen Brown 
820889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
821889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
822889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
823889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
824889facbeSLen Brown 
825889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
826889facbeSLen Brown 
827889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
828889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
829c98d5d94SLen Brown 	return 0;
830c98d5d94SLen Brown }
831c98d5d94SLen Brown /*
832c98d5d94SLen Brown  * sum the counters for all cpus in the system
833c98d5d94SLen Brown  * compute the weighted average
834c98d5d94SLen Brown  */
835c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
836c98d5d94SLen Brown 	struct pkg_data *p)
837c98d5d94SLen Brown {
838c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
839c98d5d94SLen Brown 
840c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
841c98d5d94SLen Brown 
842c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
843c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
844c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
845c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
846c98d5d94SLen Brown 
8478e180f3cSLen Brown 	average.threads.extra_delta32 /= topo.num_cpus;
8488e180f3cSLen Brown 	average.threads.extra_delta32 &= 0xFFFFFFFF;
8498e180f3cSLen Brown 
8508e180f3cSLen Brown 	average.threads.extra_delta64 /= topo.num_cpus;
8518e180f3cSLen Brown 
852c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
853c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
854c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
855c98d5d94SLen Brown 
856c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
857c98d5d94SLen Brown 	average.packages.pc3 /= topo.num_packages;
858c98d5d94SLen Brown 	average.packages.pc6 /= topo.num_packages;
859c98d5d94SLen Brown 	average.packages.pc7 /= topo.num_packages;
860ca58710fSKristen Carlson Accardi 
861ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
862ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
863ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
864c98d5d94SLen Brown }
865c98d5d94SLen Brown 
866c98d5d94SLen Brown static unsigned long long rdtsc(void)
867c98d5d94SLen Brown {
868c98d5d94SLen Brown 	unsigned int low, high;
869c98d5d94SLen Brown 
870c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
871c98d5d94SLen Brown 
872c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
873c98d5d94SLen Brown }
874c98d5d94SLen Brown 
875c98d5d94SLen Brown 
876c98d5d94SLen Brown /*
877c98d5d94SLen Brown  * get_counters(...)
878c98d5d94SLen Brown  * migrate to cpu
879c98d5d94SLen Brown  * acquire and record local counters for that cpu
880c98d5d94SLen Brown  */
881c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
882c98d5d94SLen Brown {
883c98d5d94SLen Brown 	int cpu = t->cpu_id;
884889facbeSLen Brown 	unsigned long long msr;
885c98d5d94SLen Brown 
886e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
887e52966c0SLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
88815aaa346SLen Brown 		return -1;
889e52966c0SLen Brown 	}
89015aaa346SLen Brown 
891c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
89215aaa346SLen Brown 
89315aaa346SLen Brown 	if (has_aperf) {
8949c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
895c98d5d94SLen Brown 			return -3;
8969c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
897c98d5d94SLen Brown 			return -4;
89888c3281fSLen Brown 	}
89988c3281fSLen Brown 
9001ed51011SLen Brown 	if (do_smi) {
9011ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
9021ed51011SLen Brown 			return -5;
9031ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
9041ed51011SLen Brown 	}
9058e180f3cSLen Brown 	if (extra_delta_offset32) {
906889facbeSLen Brown 		if (get_msr(cpu, extra_delta_offset32, &msr))
9072f32edf1SLen Brown 			return -5;
908889facbeSLen Brown 		t->extra_delta32 = msr & 0xFFFFFFFF;
9098e180f3cSLen Brown 	}
9108e180f3cSLen Brown 
9118e180f3cSLen Brown 	if (extra_delta_offset64)
9128e180f3cSLen Brown 		if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
9138e180f3cSLen Brown 			return -5;
9148e180f3cSLen Brown 
9158e180f3cSLen Brown 	if (extra_msr_offset32) {
916889facbeSLen Brown 		if (get_msr(cpu, extra_msr_offset32, &msr))
9178e180f3cSLen Brown 			return -5;
918889facbeSLen Brown 		t->extra_msr32 = msr & 0xFFFFFFFF;
9198e180f3cSLen Brown 	}
9202f32edf1SLen Brown 
9212f32edf1SLen Brown 	if (extra_msr_offset64)
9222f32edf1SLen Brown 		if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
923c98d5d94SLen Brown 			return -5;
924c98d5d94SLen Brown 
925144b44b1SLen Brown 	if (use_c1_residency_msr) {
926144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
927144b44b1SLen Brown 			return -6;
928144b44b1SLen Brown 	}
929144b44b1SLen Brown 
930c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
931c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
932c98d5d94SLen Brown 		return 0;
933c98d5d94SLen Brown 
934144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates) {
935c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
936c98d5d94SLen Brown 			return -6;
937144b44b1SLen Brown 	}
938144b44b1SLen Brown 
939144b44b1SLen Brown 	if (do_nhm_cstates) {
940c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
941c98d5d94SLen Brown 			return -7;
942103a8feaSLen Brown 	}
94315aaa346SLen Brown 
94415aaa346SLen Brown 	if (do_snb_cstates)
945c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
946c98d5d94SLen Brown 			return -8;
947c98d5d94SLen Brown 
948889facbeSLen Brown 	if (do_dts) {
949889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
950889facbeSLen Brown 			return -9;
951889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
952889facbeSLen Brown 	}
953889facbeSLen Brown 
954889facbeSLen Brown 
955c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
956c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
957c98d5d94SLen Brown 		return 0;
95815aaa346SLen Brown 
959144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates) {
960c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
961c98d5d94SLen Brown 			return -9;
962c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
963c98d5d94SLen Brown 			return -10;
96415aaa346SLen Brown 	}
96515aaa346SLen Brown 	if (do_snb_cstates) {
966c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
967c98d5d94SLen Brown 			return -11;
968c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
969c98d5d94SLen Brown 			return -12;
97015aaa346SLen Brown 	}
971ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
972ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
973ca58710fSKristen Carlson Accardi 			return -13;
974ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
975ca58710fSKristen Carlson Accardi 			return -13;
976ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
977ca58710fSKristen Carlson Accardi 			return -13;
978ca58710fSKristen Carlson Accardi 	}
979889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
980889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
981889facbeSLen Brown 			return -13;
982889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
983889facbeSLen Brown 	}
984889facbeSLen Brown 	if (do_rapl & RAPL_CORES) {
985889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
986889facbeSLen Brown 			return -14;
987889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
988889facbeSLen Brown 	}
989889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
990889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
991889facbeSLen Brown 			return -15;
992889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
993889facbeSLen Brown 	}
994889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
995889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
996889facbeSLen Brown 			return -16;
997889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
998889facbeSLen Brown 	}
999889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1000889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1001889facbeSLen Brown 			return -16;
1002889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1003889facbeSLen Brown 	}
1004889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1005889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1006889facbeSLen Brown 			return -16;
1007889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1008889facbeSLen Brown 	}
1009889facbeSLen Brown 	if (do_ptm) {
1010889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1011889facbeSLen Brown 			return -17;
1012889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1013889facbeSLen Brown 	}
101415aaa346SLen Brown 	return 0;
1015103a8feaSLen Brown }
1016103a8feaSLen Brown 
1017c98d5d94SLen Brown void print_verbose_header(void)
1018103a8feaSLen Brown {
1019103a8feaSLen Brown 	unsigned long long msr;
1020103a8feaSLen Brown 	unsigned int ratio;
1021103a8feaSLen Brown 
1022103a8feaSLen Brown 	if (!do_nehalem_platform_info)
1023103a8feaSLen Brown 		return;
1024103a8feaSLen Brown 
10259c63a650SLen Brown 	get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
1026103a8feaSLen Brown 
1027889facbeSLen Brown 	fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
10286574a5d5SLen Brown 
1029103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1030103a8feaSLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
1031103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1032103a8feaSLen Brown 
1033103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1034103a8feaSLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
1035103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1036103a8feaSLen Brown 
103767920418SLen Brown 	get_msr(0, MSR_IA32_POWER_CTL, &msr);
1038144b44b1SLen Brown 	fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
103967920418SLen Brown 		msr, msr & 0x2 ? "EN" : "DIS");
104067920418SLen Brown 
10416574a5d5SLen Brown 	if (!do_ivt_turbo_ratio_limit)
10426574a5d5SLen Brown 		goto print_nhm_turbo_ratio_limits;
10436574a5d5SLen Brown 
10446574a5d5SLen Brown 	get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
10456574a5d5SLen Brown 
1046889facbeSLen Brown 	fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
10476574a5d5SLen Brown 
10486574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
10496574a5d5SLen Brown 	if (ratio)
10506574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
10516574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
10526574a5d5SLen Brown 
10536574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
10546574a5d5SLen Brown 	if (ratio)
10556574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
10566574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
10576574a5d5SLen Brown 
10586574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
10596574a5d5SLen Brown 	if (ratio)
10606574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
10616574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
10626574a5d5SLen Brown 
10636574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
10646574a5d5SLen Brown 	if (ratio)
10656574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
10666574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
10676574a5d5SLen Brown 
10686574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
10696574a5d5SLen Brown 	if (ratio)
10706574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
10716574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
10726574a5d5SLen Brown 
10736574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
10746574a5d5SLen Brown 	if (ratio)
10756574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
10766574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
10776574a5d5SLen Brown 
10786574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
10796574a5d5SLen Brown 	if (ratio)
10806574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
10816574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
10826574a5d5SLen Brown 
10836574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
10846574a5d5SLen Brown 	if (ratio)
10856574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
10866574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
10876574a5d5SLen Brown 
10886574a5d5SLen Brown print_nhm_turbo_ratio_limits:
1089889facbeSLen Brown 	get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1090889facbeSLen Brown 
1091889facbeSLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
1092889facbeSLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
1093889facbeSLen Brown 
1094889facbeSLen Brown 	fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1095889facbeSLen Brown 
1096889facbeSLen Brown 	fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1097889facbeSLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1098889facbeSLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1099889facbeSLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1100889facbeSLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1101889facbeSLen Brown 		(msr & (1 << 15)) ? "" : "UN",
1102889facbeSLen Brown 		(unsigned int)msr & 7);
1103889facbeSLen Brown 
1104889facbeSLen Brown 
1105889facbeSLen Brown 	switch(msr & 0x7) {
1106889facbeSLen Brown 	case 0:
1107144b44b1SLen Brown 		fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
1108889facbeSLen Brown 		break;
1109889facbeSLen Brown 	case 1:
1110144b44b1SLen Brown 		fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
1111889facbeSLen Brown 		break;
1112889facbeSLen Brown 	case 2:
1113144b44b1SLen Brown 		fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
1114889facbeSLen Brown 		break;
1115889facbeSLen Brown 	case 3:
1116144b44b1SLen Brown 		fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
1117889facbeSLen Brown 		break;
1118889facbeSLen Brown 	case 4:
1119144b44b1SLen Brown 		fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
1120889facbeSLen Brown 		break;
1121889facbeSLen Brown 	case 5:
1122144b44b1SLen Brown 		fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1123144b44b1SLen Brown 		break;
1124144b44b1SLen Brown 	case 6:
1125144b44b1SLen Brown 		fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
1126889facbeSLen Brown 		break;
1127889facbeSLen Brown 	case 7:
1128144b44b1SLen Brown 		fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
1129889facbeSLen Brown 		break;
1130889facbeSLen Brown 	default:
1131889facbeSLen Brown 		fprintf(stderr, "invalid");
1132889facbeSLen Brown 	}
1133889facbeSLen Brown 	fprintf(stderr, ")\n");
1134103a8feaSLen Brown 
1135103a8feaSLen Brown 	if (!do_nehalem_turbo_ratio_limit)
1136103a8feaSLen Brown 		return;
1137103a8feaSLen Brown 
11389c63a650SLen Brown 	get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1139103a8feaSLen Brown 
1140889facbeSLen Brown 	fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
11416574a5d5SLen Brown 
11426574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
11436574a5d5SLen Brown 	if (ratio)
11446574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
11456574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
11466574a5d5SLen Brown 
11476574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
11486574a5d5SLen Brown 	if (ratio)
11496574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
11506574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
11516574a5d5SLen Brown 
11526574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
11536574a5d5SLen Brown 	if (ratio)
11546574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
11556574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
11566574a5d5SLen Brown 
11576574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
11586574a5d5SLen Brown 	if (ratio)
11596574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
11606574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
11616574a5d5SLen Brown 
1162103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
1163103a8feaSLen Brown 	if (ratio)
1164103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1165103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1166103a8feaSLen Brown 
1167103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
1168103a8feaSLen Brown 	if (ratio)
1169103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1170103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1171103a8feaSLen Brown 
1172103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1173103a8feaSLen Brown 	if (ratio)
1174103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1175103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1176103a8feaSLen Brown 
1177103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
1178103a8feaSLen Brown 	if (ratio)
1179103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1180103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1181103a8feaSLen Brown }
1182103a8feaSLen Brown 
1183c98d5d94SLen Brown void free_all_buffers(void)
1184103a8feaSLen Brown {
1185c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
1186c98d5d94SLen Brown 	cpu_present_set = NULL;
1187c98d5d94SLen Brown 	cpu_present_set = 0;
1188103a8feaSLen Brown 
1189c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
1190c98d5d94SLen Brown 	cpu_affinity_set = NULL;
1191c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
1192103a8feaSLen Brown 
1193c98d5d94SLen Brown 	free(thread_even);
1194c98d5d94SLen Brown 	free(core_even);
1195c98d5d94SLen Brown 	free(package_even);
1196103a8feaSLen Brown 
1197c98d5d94SLen Brown 	thread_even = NULL;
1198c98d5d94SLen Brown 	core_even = NULL;
1199c98d5d94SLen Brown 	package_even = NULL;
1200103a8feaSLen Brown 
1201c98d5d94SLen Brown 	free(thread_odd);
1202c98d5d94SLen Brown 	free(core_odd);
1203c98d5d94SLen Brown 	free(package_odd);
1204103a8feaSLen Brown 
1205c98d5d94SLen Brown 	thread_odd = NULL;
1206c98d5d94SLen Brown 	core_odd = NULL;
1207c98d5d94SLen Brown 	package_odd = NULL;
1208103a8feaSLen Brown 
1209c98d5d94SLen Brown 	free(output_buffer);
1210c98d5d94SLen Brown 	output_buffer = NULL;
1211c98d5d94SLen Brown 	outp = NULL;
1212103a8feaSLen Brown }
1213103a8feaSLen Brown 
1214103a8feaSLen Brown /*
121557a42a34SJosh Triplett  * Open a file, and exit on failure
121657a42a34SJosh Triplett  */
121757a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode)
121857a42a34SJosh Triplett {
121957a42a34SJosh Triplett 	FILE *filep = fopen(path, "r");
1220b2c95d90SJosh Triplett 	if (!filep)
1221b2c95d90SJosh Triplett 		err(1, "%s: open failed", path);
122257a42a34SJosh Triplett 	return filep;
122357a42a34SJosh Triplett }
122457a42a34SJosh Triplett 
122557a42a34SJosh Triplett /*
122695aebc44SJosh Triplett  * Parse a file containing a single int.
122795aebc44SJosh Triplett  */
122895aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
122995aebc44SJosh Triplett {
123095aebc44SJosh Triplett 	va_list args;
123195aebc44SJosh Triplett 	char path[PATH_MAX];
123295aebc44SJosh Triplett 	FILE *filep;
123395aebc44SJosh Triplett 	int value;
123495aebc44SJosh Triplett 
123595aebc44SJosh Triplett 	va_start(args, fmt);
123695aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
123795aebc44SJosh Triplett 	va_end(args);
123857a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1239b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
1240b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
124195aebc44SJosh Triplett 	fclose(filep);
124295aebc44SJosh Triplett 	return value;
124395aebc44SJosh Triplett }
124495aebc44SJosh Triplett 
124595aebc44SJosh Triplett /*
1246c98d5d94SLen Brown  * cpu_is_first_sibling_in_core(cpu)
1247c98d5d94SLen Brown  * return 1 if given CPU is 1st HT sibling in the core
1248103a8feaSLen Brown  */
1249c98d5d94SLen Brown int cpu_is_first_sibling_in_core(int cpu)
1250103a8feaSLen Brown {
125195aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1252c98d5d94SLen Brown }
1253103a8feaSLen Brown 
1254c98d5d94SLen Brown /*
1255c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
1256c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
1257c98d5d94SLen Brown  */
1258c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
1259c98d5d94SLen Brown {
126095aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1261103a8feaSLen Brown }
1262103a8feaSLen Brown 
1263103a8feaSLen Brown int get_physical_package_id(int cpu)
1264103a8feaSLen Brown {
126595aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1266103a8feaSLen Brown }
1267103a8feaSLen Brown 
1268103a8feaSLen Brown int get_core_id(int cpu)
1269103a8feaSLen Brown {
127095aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1271103a8feaSLen Brown }
1272103a8feaSLen Brown 
1273c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
1274c98d5d94SLen Brown {
1275c98d5d94SLen Brown 	char path[80];
1276c98d5d94SLen Brown 	FILE *filep;
1277c98d5d94SLen Brown 	int sib1, sib2;
1278c98d5d94SLen Brown 	int matches;
1279c98d5d94SLen Brown 	char character;
1280c98d5d94SLen Brown 
1281c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
128257a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1283103a8feaSLen Brown 	/*
1284c98d5d94SLen Brown 	 * file format:
1285c98d5d94SLen Brown 	 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1286c98d5d94SLen Brown 	 * otherwinse 1 sibling (self).
1287c98d5d94SLen Brown 	 */
1288c98d5d94SLen Brown 	matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1289c98d5d94SLen Brown 
1290c98d5d94SLen Brown 	fclose(filep);
1291c98d5d94SLen Brown 
1292c98d5d94SLen Brown 	if (matches == 3)
1293c98d5d94SLen Brown 		return 2;
1294c98d5d94SLen Brown 	else
1295c98d5d94SLen Brown 		return 1;
1296c98d5d94SLen Brown }
1297c98d5d94SLen Brown 
1298c98d5d94SLen Brown /*
1299c98d5d94SLen Brown  * run func(thread, core, package) in topology order
1300c98d5d94SLen Brown  * skip non-present cpus
1301103a8feaSLen Brown  */
1302103a8feaSLen Brown 
1303c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1304c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
1305c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
1306c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
1307c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
1308c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
1309c98d5d94SLen Brown {
1310c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
1311c98d5d94SLen Brown 
1312c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1313c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1314c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
1315c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
1316c98d5d94SLen Brown 				struct thread_data *t, *t2;
1317c98d5d94SLen Brown 				struct core_data *c, *c2;
1318c98d5d94SLen Brown 				struct pkg_data *p, *p2;
1319c98d5d94SLen Brown 
1320c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1321c98d5d94SLen Brown 
1322c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
1323c98d5d94SLen Brown 					continue;
1324c98d5d94SLen Brown 
1325c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1326c98d5d94SLen Brown 
1327c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
1328c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
1329c98d5d94SLen Brown 
1330c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
1331c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
1332c98d5d94SLen Brown 
1333c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
1334c98d5d94SLen Brown 				if (retval)
1335c98d5d94SLen Brown 					return retval;
1336c98d5d94SLen Brown 			}
1337c98d5d94SLen Brown 		}
1338c98d5d94SLen Brown 	}
1339c98d5d94SLen Brown 	return 0;
1340c98d5d94SLen Brown }
1341c98d5d94SLen Brown 
1342c98d5d94SLen Brown /*
1343c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
1344c98d5d94SLen Brown  * return max_cpu number
1345c98d5d94SLen Brown  */
1346c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
1347103a8feaSLen Brown {
1348103a8feaSLen Brown 	FILE *fp;
1349c98d5d94SLen Brown 	int cpu_num;
1350103a8feaSLen Brown 	int retval;
1351103a8feaSLen Brown 
135257a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
1353103a8feaSLen Brown 
1354103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1355b2c95d90SJosh Triplett 	if (retval != 0)
1356b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
1357103a8feaSLen Brown 
1358c98d5d94SLen Brown 	while (1) {
1359c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1360103a8feaSLen Brown 		if (retval != 1)
1361103a8feaSLen Brown 			break;
1362103a8feaSLen Brown 
1363c98d5d94SLen Brown 		retval = func(cpu_num);
1364c98d5d94SLen Brown 		if (retval) {
1365c98d5d94SLen Brown 			fclose(fp);
1366c98d5d94SLen Brown 			return(retval);
1367c98d5d94SLen Brown 		}
1368103a8feaSLen Brown 	}
1369103a8feaSLen Brown 	fclose(fp);
1370c98d5d94SLen Brown 	return 0;
1371103a8feaSLen Brown }
1372103a8feaSLen Brown 
1373103a8feaSLen Brown void re_initialize(void)
1374103a8feaSLen Brown {
1375c98d5d94SLen Brown 	free_all_buffers();
1376c98d5d94SLen Brown 	setup_all_buffers();
1377c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1378103a8feaSLen Brown }
1379103a8feaSLen Brown 
1380c98d5d94SLen Brown 
1381103a8feaSLen Brown /*
1382c98d5d94SLen Brown  * count_cpus()
1383c98d5d94SLen Brown  * remember the last one seen, it will be the max
1384103a8feaSLen Brown  */
1385c98d5d94SLen Brown int count_cpus(int cpu)
1386103a8feaSLen Brown {
1387c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
1388c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
1389103a8feaSLen Brown 
1390c98d5d94SLen Brown 	topo.num_cpus += 1;
1391c98d5d94SLen Brown 	return 0;
1392103a8feaSLen Brown }
1393c98d5d94SLen Brown int mark_cpu_present(int cpu)
1394c98d5d94SLen Brown {
1395c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
139615aaa346SLen Brown 	return 0;
1397103a8feaSLen Brown }
1398103a8feaSLen Brown 
1399103a8feaSLen Brown void turbostat_loop()
1400103a8feaSLen Brown {
1401c98d5d94SLen Brown 	int retval;
1402e52966c0SLen Brown 	int restarted = 0;
1403c98d5d94SLen Brown 
1404103a8feaSLen Brown restart:
1405e52966c0SLen Brown 	restarted++;
1406e52966c0SLen Brown 
1407c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1408d91bb17cSLen Brown 	if (retval < -1) {
1409d91bb17cSLen Brown 		exit(retval);
1410d91bb17cSLen Brown 	} else if (retval == -1) {
1411e52966c0SLen Brown 		if (restarted > 1) {
1412e52966c0SLen Brown 			exit(retval);
1413e52966c0SLen Brown 		}
1414c98d5d94SLen Brown 		re_initialize();
1415c98d5d94SLen Brown 		goto restart;
1416c98d5d94SLen Brown 	}
1417e52966c0SLen Brown 	restarted = 0;
1418103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
1419103a8feaSLen Brown 
1420103a8feaSLen Brown 	while (1) {
1421c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
1422103a8feaSLen Brown 			re_initialize();
1423103a8feaSLen Brown 			goto restart;
1424103a8feaSLen Brown 		}
1425103a8feaSLen Brown 		sleep(interval_sec);
1426c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
1427d91bb17cSLen Brown 		if (retval < -1) {
1428d91bb17cSLen Brown 			exit(retval);
1429d91bb17cSLen Brown 		} else if (retval == -1) {
143015aaa346SLen Brown 			re_initialize();
143115aaa346SLen Brown 			goto restart;
143215aaa346SLen Brown 		}
1433103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
1434103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
1435c98d5d94SLen Brown 		for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1436c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
1437c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
1438c98d5d94SLen Brown 		flush_stdout();
143915aaa346SLen Brown 		sleep(interval_sec);
1440c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1441d91bb17cSLen Brown 		if (retval < -1) {
1442d91bb17cSLen Brown 			exit(retval);
1443d91bb17cSLen Brown 		} else if (retval == -1) {
1444103a8feaSLen Brown 			re_initialize();
1445103a8feaSLen Brown 			goto restart;
1446103a8feaSLen Brown 		}
1447103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
1448103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
1449c98d5d94SLen Brown 		for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1450c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
1451c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
1452c98d5d94SLen Brown 		flush_stdout();
1453103a8feaSLen Brown 	}
1454103a8feaSLen Brown }
1455103a8feaSLen Brown 
1456103a8feaSLen Brown void check_dev_msr()
1457103a8feaSLen Brown {
1458103a8feaSLen Brown 	struct stat sb;
1459103a8feaSLen Brown 
1460b2c95d90SJosh Triplett 	if (stat("/dev/cpu/0/msr", &sb))
1461b2c95d90SJosh Triplett 		err(-5, "no /dev/cpu/0/msr\n"
1462b2c95d90SJosh Triplett 		    "Try \"# modprobe msr\"");
1463103a8feaSLen Brown }
1464103a8feaSLen Brown 
1465*98481e79SLen Brown void check_permissions()
1466103a8feaSLen Brown {
1467*98481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
1468*98481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
1469*98481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
1470*98481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
1471*98481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1472*98481e79SLen Brown 	int do_exit = 0;
1473*98481e79SLen Brown 
1474*98481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
1475*98481e79SLen Brown 	cap_header->pid = getpid();
1476*98481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
1477*98481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
1478*98481e79SLen Brown 		err(-6, "capget(2) failed");
1479*98481e79SLen Brown 
1480*98481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1481*98481e79SLen Brown 		do_exit++;
1482*98481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
1483*98481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
1484*98481e79SLen Brown 	}
1485*98481e79SLen Brown 
1486*98481e79SLen Brown 	/* test file permissions */
1487*98481e79SLen Brown 	if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1488*98481e79SLen Brown 		do_exit++;
1489*98481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1490*98481e79SLen Brown 	}
1491*98481e79SLen Brown 
1492*98481e79SLen Brown 	/* if all else fails, thell them to be root */
1493*98481e79SLen Brown 	if (do_exit)
1494b2c95d90SJosh Triplett 		if (getuid() != 0)
1495*98481e79SLen Brown 			warnx("Or simply run as root");
1496*98481e79SLen Brown 
1497*98481e79SLen Brown 	if (do_exit)
1498*98481e79SLen Brown 		exit(-6);
1499103a8feaSLen Brown }
1500103a8feaSLen Brown 
1501103a8feaSLen Brown int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1502103a8feaSLen Brown {
1503103a8feaSLen Brown 	if (!genuine_intel)
1504103a8feaSLen Brown 		return 0;
1505103a8feaSLen Brown 
1506103a8feaSLen Brown 	if (family != 6)
1507103a8feaSLen Brown 		return 0;
1508103a8feaSLen Brown 
1509103a8feaSLen Brown 	switch (model) {
1510103a8feaSLen Brown 	case 0x1A:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1511103a8feaSLen Brown 	case 0x1E:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1512103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
1513103a8feaSLen Brown 	case 0x25:	/* Westmere Client - Clarkdale, Arrandale */
1514103a8feaSLen Brown 	case 0x2C:	/* Westmere EP - Gulftown */
1515103a8feaSLen Brown 	case 0x2A:	/* SNB */
1516103a8feaSLen Brown 	case 0x2D:	/* SNB Xeon */
1517553575f1SLen Brown 	case 0x3A:	/* IVB */
15181300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
151970b43400SLen Brown 	case 0x3C:	/* HSW */
1520e6f9bb3cSLen Brown 	case 0x3F:	/* HSX */
152170b43400SLen Brown 	case 0x45:	/* HSW */
1522149c2319SLen Brown 	case 0x46:	/* HSW */
1523144b44b1SLen Brown 	case 0x37:	/* BYT */
1524144b44b1SLen Brown 	case 0x4D:	/* AVN */
15254e8e863fSLen Brown 	case 0x3D:	/* BDW */
15264e8e863fSLen Brown 	case 0x4F:	/* BDX */
15274e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
1528103a8feaSLen Brown 		return 1;
1529103a8feaSLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
1530103a8feaSLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
1531103a8feaSLen Brown 	default:
1532103a8feaSLen Brown 		return 0;
1533103a8feaSLen Brown 	}
1534103a8feaSLen Brown }
15356574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
15366574a5d5SLen Brown {
15376574a5d5SLen Brown 	if (!genuine_intel)
15386574a5d5SLen Brown 		return 0;
15396574a5d5SLen Brown 
15406574a5d5SLen Brown 	if (family != 6)
15416574a5d5SLen Brown 		return 0;
15426574a5d5SLen Brown 
15436574a5d5SLen Brown 	switch (model) {
15446574a5d5SLen Brown 	case 0x3E:	/* IVB Xeon */
15456574a5d5SLen Brown 		return 1;
15466574a5d5SLen Brown 	default:
15476574a5d5SLen Brown 		return 0;
15486574a5d5SLen Brown 	}
15496574a5d5SLen Brown }
15506574a5d5SLen Brown 
1551889facbeSLen Brown /*
1552889facbeSLen Brown  * print_epb()
1553889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
1554889facbeSLen Brown  */
1555889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1556889facbeSLen Brown {
1557889facbeSLen Brown 	unsigned long long msr;
1558889facbeSLen Brown 	char *epb_string;
1559889facbeSLen Brown 	int cpu;
1560889facbeSLen Brown 
1561889facbeSLen Brown 	if (!has_epb)
1562889facbeSLen Brown 		return 0;
1563889facbeSLen Brown 
1564889facbeSLen Brown 	cpu = t->cpu_id;
1565889facbeSLen Brown 
1566889facbeSLen Brown 	/* EPB is per-package */
1567889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1568889facbeSLen Brown 		return 0;
1569889facbeSLen Brown 
1570889facbeSLen Brown 	if (cpu_migrate(cpu)) {
1571889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1572889facbeSLen Brown 		return -1;
1573889facbeSLen Brown 	}
1574889facbeSLen Brown 
1575889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1576889facbeSLen Brown 		return 0;
1577889facbeSLen Brown 
1578889facbeSLen Brown 	switch (msr & 0x7) {
1579889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
1580889facbeSLen Brown 		epb_string = "performance";
1581889facbeSLen Brown 		break;
1582889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
1583889facbeSLen Brown 		epb_string = "balanced";
1584889facbeSLen Brown 		break;
1585889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
1586889facbeSLen Brown 		epb_string = "powersave";
1587889facbeSLen Brown 		break;
1588889facbeSLen Brown 	default:
1589889facbeSLen Brown 		epb_string = "custom";
1590889facbeSLen Brown 		break;
1591889facbeSLen Brown 	}
1592889facbeSLen Brown 	fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1593889facbeSLen Brown 
1594889facbeSLen Brown 	return 0;
1595889facbeSLen Brown }
1596889facbeSLen Brown 
1597889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
1598889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
1599889facbeSLen Brown 
1600144b44b1SLen Brown double get_tdp(model)
1601144b44b1SLen Brown {
1602144b44b1SLen Brown 	unsigned long long msr;
1603144b44b1SLen Brown 
1604144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
1605144b44b1SLen Brown 		if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1606144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1607144b44b1SLen Brown 
1608144b44b1SLen Brown 	switch (model) {
1609144b44b1SLen Brown 	case 0x37:
1610144b44b1SLen Brown 	case 0x4D:
1611144b44b1SLen Brown 		return 30.0;
1612144b44b1SLen Brown 	default:
1613144b44b1SLen Brown 		return 135.0;
1614144b44b1SLen Brown 	}
1615144b44b1SLen Brown }
1616144b44b1SLen Brown 
1617144b44b1SLen Brown 
1618889facbeSLen Brown /*
1619889facbeSLen Brown  * rapl_probe()
1620889facbeSLen Brown  *
1621144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
1622889facbeSLen Brown  */
1623889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
1624889facbeSLen Brown {
1625889facbeSLen Brown 	unsigned long long msr;
1626144b44b1SLen Brown 	unsigned int time_unit;
1627889facbeSLen Brown 	double tdp;
1628889facbeSLen Brown 
1629889facbeSLen Brown 	if (!genuine_intel)
1630889facbeSLen Brown 		return;
1631889facbeSLen Brown 
1632889facbeSLen Brown 	if (family != 6)
1633889facbeSLen Brown 		return;
1634889facbeSLen Brown 
1635889facbeSLen Brown 	switch (model) {
1636889facbeSLen Brown 	case 0x2A:
1637889facbeSLen Brown 	case 0x3A:
163870b43400SLen Brown 	case 0x3C:	/* HSW */
163970b43400SLen Brown 	case 0x45:	/* HSW */
1640149c2319SLen Brown 	case 0x46:	/* HSW */
16414e8e863fSLen Brown 	case 0x3D:	/* BDW */
1642144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1643889facbeSLen Brown 		break;
1644e6f9bb3cSLen Brown 	case 0x3F:	/* HSX */
16454e8e863fSLen Brown 	case 0x4F:	/* BDX */
16464e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
1647e6f9bb3cSLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1648e6f9bb3cSLen Brown 		break;
1649889facbeSLen Brown 	case 0x2D:
1650889facbeSLen Brown 	case 0x3E:
1651144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1652144b44b1SLen Brown 		break;
1653144b44b1SLen Brown 	case 0x37:	/* BYT */
1654144b44b1SLen Brown 	case 0x4D:	/* AVN */
1655144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES ;
1656889facbeSLen Brown 		break;
1657889facbeSLen Brown 	default:
1658889facbeSLen Brown 		return;
1659889facbeSLen Brown 	}
1660889facbeSLen Brown 
1661889facbeSLen Brown 	/* units on package 0, verify later other packages match */
1662889facbeSLen Brown 	if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1663889facbeSLen Brown 		return;
1664889facbeSLen Brown 
1665889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
1666144b44b1SLen Brown 	if (model == 0x37)
1667144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1668144b44b1SLen Brown 	else
1669889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1670889facbeSLen Brown 
1671144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
1672144b44b1SLen Brown 	if (time_unit == 0)
1673144b44b1SLen Brown 		time_unit = 0xA;
1674889facbeSLen Brown 
1675144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
1676144b44b1SLen Brown 
1677144b44b1SLen Brown 	tdp = get_tdp(model);
1678889facbeSLen Brown 
1679889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1680889facbeSLen Brown 	if (verbose)
1681144b44b1SLen Brown 		fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
1682889facbeSLen Brown 
1683889facbeSLen Brown 	return;
1684889facbeSLen Brown }
1685889facbeSLen Brown 
1686889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1687889facbeSLen Brown {
1688889facbeSLen Brown 	unsigned long long msr;
1689889facbeSLen Brown 	unsigned int dts;
1690889facbeSLen Brown 	int cpu;
1691889facbeSLen Brown 
1692889facbeSLen Brown 	if (!(do_dts || do_ptm))
1693889facbeSLen Brown 		return 0;
1694889facbeSLen Brown 
1695889facbeSLen Brown 	cpu = t->cpu_id;
1696889facbeSLen Brown 
1697889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
1698889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1699889facbeSLen Brown 		return 0;
1700889facbeSLen Brown 
1701889facbeSLen Brown 	if (cpu_migrate(cpu)) {
1702889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1703889facbeSLen Brown 		return -1;
1704889facbeSLen Brown 	}
1705889facbeSLen Brown 
1706889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1707889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1708889facbeSLen Brown 			return 0;
1709889facbeSLen Brown 
1710889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
1711889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1712889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
1713889facbeSLen Brown 
1714889facbeSLen Brown #ifdef	THERM_DEBUG
1715889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1716889facbeSLen Brown 			return 0;
1717889facbeSLen Brown 
1718889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
1719889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
1720889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1721889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1722889facbeSLen Brown #endif
1723889facbeSLen Brown 	}
1724889facbeSLen Brown 
1725889facbeSLen Brown 
1726889facbeSLen Brown 	if (do_dts) {
1727889facbeSLen Brown 		unsigned int resolution;
1728889facbeSLen Brown 
1729889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1730889facbeSLen Brown 			return 0;
1731889facbeSLen Brown 
1732889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
1733889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
1734889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1735889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
1736889facbeSLen Brown 
1737889facbeSLen Brown #ifdef THERM_DEBUG
1738889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1739889facbeSLen Brown 			return 0;
1740889facbeSLen Brown 
1741889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
1742889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
1743889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1744889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1745889facbeSLen Brown #endif
1746889facbeSLen Brown 	}
1747889facbeSLen Brown 
1748889facbeSLen Brown 	return 0;
1749889facbeSLen Brown }
1750889facbeSLen Brown 
1751889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1752889facbeSLen Brown {
1753889facbeSLen Brown 	fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1754889facbeSLen Brown 		cpu, label,
1755889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
1756889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
1757889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1758889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
1759889facbeSLen Brown 
1760889facbeSLen Brown 	return;
1761889facbeSLen Brown }
1762889facbeSLen Brown 
1763889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1764889facbeSLen Brown {
1765889facbeSLen Brown 	unsigned long long msr;
1766889facbeSLen Brown 	int cpu;
1767889facbeSLen Brown 
1768889facbeSLen Brown 	if (!do_rapl)
1769889facbeSLen Brown 		return 0;
1770889facbeSLen Brown 
1771889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
1772889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1773889facbeSLen Brown 		return 0;
1774889facbeSLen Brown 
1775889facbeSLen Brown 	cpu = t->cpu_id;
1776889facbeSLen Brown 	if (cpu_migrate(cpu)) {
1777889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1778889facbeSLen Brown 		return -1;
1779889facbeSLen Brown 	}
1780889facbeSLen Brown 
1781889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1782889facbeSLen Brown 		return -1;
1783889facbeSLen Brown 
1784889facbeSLen Brown 	if (verbose) {
1785889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1786889facbeSLen Brown 			"(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
1787144b44b1SLen Brown 			rapl_power_units, rapl_energy_units, rapl_time_units);
1788889facbeSLen Brown 	}
1789144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
1790144b44b1SLen Brown 
1791889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1792889facbeSLen Brown                 	return -5;
1793889facbeSLen Brown 
1794889facbeSLen Brown 
1795889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1796889facbeSLen Brown 			cpu, msr,
1797889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1798889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1799889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1800889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1801889facbeSLen Brown 
1802144b44b1SLen Brown 	}
1803144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
1804144b44b1SLen Brown 
1805889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1806889facbeSLen Brown 			return -9;
1807889facbeSLen Brown 
1808889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1809889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "": "UN");
1810889facbeSLen Brown 
1811889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
1812889facbeSLen Brown 		fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1813889facbeSLen Brown 			cpu,
1814889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
1815889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
1816889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1817889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
1818889facbeSLen Brown 	}
1819889facbeSLen Brown 
1820889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1821889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1822889facbeSLen Brown                 	return -6;
1823889facbeSLen Brown 
1824889facbeSLen Brown 
1825889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1826889facbeSLen Brown 			cpu, msr,
1827889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1828889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1829889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1830889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1831889facbeSLen Brown 
1832889facbeSLen Brown 
1833889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1834889facbeSLen Brown 			return -9;
1835889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1836889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "": "UN");
1837889facbeSLen Brown 
1838889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
1839889facbeSLen Brown 	}
1840144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
1841889facbeSLen Brown 		if (verbose) {
1842889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1843889facbeSLen Brown 				return -7;
1844889facbeSLen Brown 
1845889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
1846144b44b1SLen Brown 		}
1847144b44b1SLen Brown 	}
1848144b44b1SLen Brown 	if (do_rapl & RAPL_CORES) {
1849144b44b1SLen Brown 		if (verbose) {
1850889facbeSLen Brown 
1851889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1852889facbeSLen Brown 				return -9;
1853889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1854889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
1855889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "Cores Limit");
1856889facbeSLen Brown 		}
1857889facbeSLen Brown 	}
1858889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1859889facbeSLen Brown 		if (verbose) {
1860889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1861889facbeSLen Brown 				return -8;
1862889facbeSLen Brown 
1863889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1864889facbeSLen Brown 
1865889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1866889facbeSLen Brown 				return -9;
1867889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1868889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
1869889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "GFX Limit");
1870889facbeSLen Brown 		}
1871889facbeSLen Brown 	}
1872889facbeSLen Brown 	return 0;
1873889facbeSLen Brown }
1874889facbeSLen Brown 
1875103a8feaSLen Brown 
1876103a8feaSLen Brown int is_snb(unsigned int family, unsigned int model)
1877103a8feaSLen Brown {
1878103a8feaSLen Brown 	if (!genuine_intel)
1879103a8feaSLen Brown 		return 0;
1880103a8feaSLen Brown 
1881103a8feaSLen Brown 	switch (model) {
1882103a8feaSLen Brown 	case 0x2A:
1883103a8feaSLen Brown 	case 0x2D:
1884650a37f3SLen Brown 	case 0x3A:	/* IVB */
18851300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
188670b43400SLen Brown 	case 0x3C:	/* HSW */
188770b43400SLen Brown 	case 0x3F:	/* HSW */
188870b43400SLen Brown 	case 0x45:	/* HSW */
1889149c2319SLen Brown 	case 0x46:	/* HSW */
18904e8e863fSLen Brown 	case 0x3D:	/* BDW */
18914e8e863fSLen Brown 	case 0x4F:	/* BDX */
18924e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
1893103a8feaSLen Brown 		return 1;
1894103a8feaSLen Brown 	}
1895103a8feaSLen Brown 	return 0;
1896103a8feaSLen Brown }
1897103a8feaSLen Brown 
1898ca58710fSKristen Carlson Accardi int has_c8_c9_c10(unsigned int family, unsigned int model)
1899ca58710fSKristen Carlson Accardi {
1900ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
1901ca58710fSKristen Carlson Accardi 		return 0;
1902ca58710fSKristen Carlson Accardi 
1903ca58710fSKristen Carlson Accardi 	switch (model) {
19044e8e863fSLen Brown 	case 0x45:	/* HSW */
19054e8e863fSLen Brown 	case 0x3D:	/* BDW */
1906ca58710fSKristen Carlson Accardi 		return 1;
1907ca58710fSKristen Carlson Accardi 	}
1908ca58710fSKristen Carlson Accardi 	return 0;
1909ca58710fSKristen Carlson Accardi }
1910ca58710fSKristen Carlson Accardi 
1911ca58710fSKristen Carlson Accardi 
1912144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
1913144b44b1SLen Brown {
1914144b44b1SLen Brown 	if (!genuine_intel)
1915144b44b1SLen Brown 		return 0;
1916144b44b1SLen Brown 	switch (model) {
1917144b44b1SLen Brown 	case 0x37:	/* BYT */
1918144b44b1SLen Brown 	case 0x4D:	/* AVN */
1919144b44b1SLen Brown 		return 1;
1920144b44b1SLen Brown 	}
1921144b44b1SLen Brown 	return 0;
1922144b44b1SLen Brown }
1923144b44b1SLen Brown 
1924144b44b1SLen Brown #define SLM_BCLK_FREQS 5
1925144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1926144b44b1SLen Brown 
1927144b44b1SLen Brown double slm_bclk(void)
1928144b44b1SLen Brown {
1929144b44b1SLen Brown 	unsigned long long msr = 3;
1930144b44b1SLen Brown 	unsigned int i;
1931144b44b1SLen Brown 	double freq;
1932144b44b1SLen Brown 
1933144b44b1SLen Brown 	if (get_msr(0, MSR_FSB_FREQ, &msr))
1934144b44b1SLen Brown 		fprintf(stderr, "SLM BCLK: unknown\n");
1935144b44b1SLen Brown 
1936144b44b1SLen Brown 	i = msr & 0xf;
1937144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
1938144b44b1SLen Brown 		fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1939144b44b1SLen Brown 		msr = 3;
1940144b44b1SLen Brown 	}
1941144b44b1SLen Brown 	freq = slm_freq_table[i];
1942144b44b1SLen Brown 
1943144b44b1SLen Brown 	fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1944144b44b1SLen Brown 
1945144b44b1SLen Brown 	return freq;
1946144b44b1SLen Brown }
1947144b44b1SLen Brown 
1948103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
1949103a8feaSLen Brown {
1950103a8feaSLen Brown 	if (is_snb(family, model))
1951103a8feaSLen Brown 		return 100.00;
1952144b44b1SLen Brown 	else if (is_slm(family, model))
1953144b44b1SLen Brown 		return slm_bclk();
1954103a8feaSLen Brown 	else
1955103a8feaSLen Brown 		return 133.33;
1956103a8feaSLen Brown }
1957103a8feaSLen Brown 
1958889facbeSLen Brown /*
1959889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1960889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
1961889facbeSLen Brown  * This is usually equal to tjMax.
1962889facbeSLen Brown  *
1963889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
1964889facbeSLen Brown  * but also allow cmdline over-ride with -T.
1965889facbeSLen Brown  *
1966889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
1967889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
1968889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1969889facbeSLen Brown  */
1970889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1971889facbeSLen Brown {
1972889facbeSLen Brown 	unsigned long long msr;
1973889facbeSLen Brown 	unsigned int target_c_local;
1974889facbeSLen Brown 	int cpu;
1975889facbeSLen Brown 
1976889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
1977889facbeSLen Brown 	if (!(do_dts || do_ptm))
1978889facbeSLen Brown 		return 0;
1979889facbeSLen Brown 
1980889facbeSLen Brown 	/* this is a per-package concept */
1981889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1982889facbeSLen Brown 		return 0;
1983889facbeSLen Brown 
1984889facbeSLen Brown 	cpu = t->cpu_id;
1985889facbeSLen Brown 	if (cpu_migrate(cpu)) {
1986889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1987889facbeSLen Brown 		return -1;
1988889facbeSLen Brown 	}
1989889facbeSLen Brown 
1990889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
1991889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
1992889facbeSLen Brown 		fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1993889facbeSLen Brown 			cpu, tcc_activation_temp);
1994889facbeSLen Brown 		return 0;
1995889facbeSLen Brown 	}
1996889facbeSLen Brown 
1997889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
1998889facbeSLen Brown 	if (!do_nehalem_platform_info)
1999889facbeSLen Brown 		goto guess;
2000889facbeSLen Brown 
2001889facbeSLen Brown 	if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2002889facbeSLen Brown 		goto guess;
2003889facbeSLen Brown 
20043482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
2005889facbeSLen Brown 
2006889facbeSLen Brown 	if (verbose)
2007889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2008889facbeSLen Brown 			cpu, msr, target_c_local);
2009889facbeSLen Brown 
20103482124aSJean Delvare 	if (!target_c_local)
2011889facbeSLen Brown 		goto guess;
2012889facbeSLen Brown 
2013889facbeSLen Brown 	tcc_activation_temp = target_c_local;
2014889facbeSLen Brown 
2015889facbeSLen Brown 	return 0;
2016889facbeSLen Brown 
2017889facbeSLen Brown guess:
2018889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
2019889facbeSLen Brown 	fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2020889facbeSLen Brown 		cpu, tcc_activation_temp);
2021889facbeSLen Brown 
2022889facbeSLen Brown 	return 0;
2023889facbeSLen Brown }
2024103a8feaSLen Brown void check_cpuid()
2025103a8feaSLen Brown {
2026103a8feaSLen Brown 	unsigned int eax, ebx, ecx, edx, max_level;
2027103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
2028103a8feaSLen Brown 
2029103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
2030103a8feaSLen Brown 
20312b92865eSJosh Triplett 	__get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2032103a8feaSLen Brown 
2033103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2034103a8feaSLen Brown 		genuine_intel = 1;
2035103a8feaSLen Brown 
2036103a8feaSLen Brown 	if (verbose)
2037889facbeSLen Brown 		fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2038103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
2039103a8feaSLen Brown 
20402b92865eSJosh Triplett 	__get_cpuid(1, &fms, &ebx, &ecx, &edx);
2041103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
2042103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
2043103a8feaSLen Brown 	stepping = fms & 0xf;
2044103a8feaSLen Brown 	if (family == 6 || family == 0xf)
2045103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
2046103a8feaSLen Brown 
2047103a8feaSLen Brown 	if (verbose)
2048103a8feaSLen Brown 		fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2049103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
2050103a8feaSLen Brown 
2051b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
2052b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
2053103a8feaSLen Brown 
2054103a8feaSLen Brown 	/*
2055103a8feaSLen Brown 	 * check max extended function levels of CPUID.
2056103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
2057103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
2058103a8feaSLen Brown 	 */
2059103a8feaSLen Brown 	ebx = ecx = edx = 0;
20602b92865eSJosh Triplett 	__get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
2061103a8feaSLen Brown 
2062b2c95d90SJosh Triplett 	if (max_level < 0x80000007)
2063b2c95d90SJosh Triplett 		errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level);
2064103a8feaSLen Brown 
2065103a8feaSLen Brown 	/*
2066103a8feaSLen Brown 	 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2067103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
2068103a8feaSLen Brown 	 */
20692b92865eSJosh Triplett 	__get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
20708209e054SThomas Renninger 	has_invariant_tsc = edx & (1 << 8);
2071103a8feaSLen Brown 
2072b2c95d90SJosh Triplett 	if (!has_invariant_tsc)
2073b2c95d90SJosh Triplett 		errx(1, "No invariant TSC");
2074103a8feaSLen Brown 
2075103a8feaSLen Brown 	/*
2076103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2077103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
2078103a8feaSLen Brown 	 */
2079103a8feaSLen Brown 
20802b92865eSJosh Triplett 	__get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
20818209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
2082889facbeSLen Brown 	do_dts = eax & (1 << 0);
2083889facbeSLen Brown 	do_ptm = eax & (1 << 6);
2084889facbeSLen Brown 	has_epb = ecx & (1 << 3);
2085889facbeSLen Brown 
2086889facbeSLen Brown 	if (verbose)
2087889facbeSLen Brown 		fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2088889facbeSLen Brown 			has_aperf ? "APERF" : "No APERF!",
2089889facbeSLen Brown 			do_dts ? ", DTS" : "",
2090889facbeSLen Brown 			do_ptm ? ", PTM": "",
2091889facbeSLen Brown 			has_epb ? ", EPB": "");
2092889facbeSLen Brown 
2093889facbeSLen Brown 	if (!has_aperf)
2094b2c95d90SJosh Triplett 		errx(-1, "No APERF");
2095103a8feaSLen Brown 
2096103a8feaSLen Brown 	do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2097103a8feaSLen Brown 	do_nhm_cstates = genuine_intel;	/* all Intel w/ non-stop TSC have NHM counters */
20981ed51011SLen Brown 	do_smi = do_nhm_cstates;
2099103a8feaSLen Brown 	do_snb_cstates = is_snb(family, model);
2100ca58710fSKristen Carlson Accardi 	do_c8_c9_c10 = has_c8_c9_c10(family, model);
2101144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
2102103a8feaSLen Brown 	bclk = discover_bclk(family, model);
2103103a8feaSLen Brown 
2104103a8feaSLen Brown 	do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
21056574a5d5SLen Brown 	do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
2106889facbeSLen Brown 	rapl_probe(family, model);
2107889facbeSLen Brown 
2108889facbeSLen Brown 	return;
2109103a8feaSLen Brown }
2110103a8feaSLen Brown 
2111103a8feaSLen Brown 
2112103a8feaSLen Brown void usage()
2113103a8feaSLen Brown {
2114f591c38bSAndy Shevchenko 	errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR#][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
2115103a8feaSLen Brown 	     progname);
2116103a8feaSLen Brown }
2117103a8feaSLen Brown 
2118103a8feaSLen Brown 
2119103a8feaSLen Brown /*
2120103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
2121103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
2122103a8feaSLen Brown  */
2123103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
2124103a8feaSLen Brown {
2125103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
2126103a8feaSLen Brown 		return 1;
2127103a8feaSLen Brown 	else
2128103a8feaSLen Brown 		return 0;
2129103a8feaSLen Brown }
2130103a8feaSLen Brown 
2131103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
2132103a8feaSLen Brown {
2133103a8feaSLen Brown 	return 0;
2134103a8feaSLen Brown }
2135103a8feaSLen Brown 
2136c98d5d94SLen Brown void topology_probe()
2137c98d5d94SLen Brown {
2138c98d5d94SLen Brown 	int i;
2139c98d5d94SLen Brown 	int max_core_id = 0;
2140c98d5d94SLen Brown 	int max_package_id = 0;
2141c98d5d94SLen Brown 	int max_siblings = 0;
2142c98d5d94SLen Brown 	struct cpu_topology {
2143c98d5d94SLen Brown 		int core_id;
2144c98d5d94SLen Brown 		int physical_package_id;
2145c98d5d94SLen Brown 	} *cpus;
2146c98d5d94SLen Brown 
2147c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
2148c98d5d94SLen Brown 	topo.num_cpus = 0;
2149c98d5d94SLen Brown 	topo.max_cpu_num = 0;
2150c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
2151c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
2152c98d5d94SLen Brown 		show_cpu = 1;
2153c98d5d94SLen Brown 
2154c98d5d94SLen Brown 	if (verbose > 1)
2155c98d5d94SLen Brown 		fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2156c98d5d94SLen Brown 
2157c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
2158b2c95d90SJosh Triplett 	if (cpus == NULL)
2159b2c95d90SJosh Triplett 		err(1, "calloc cpus");
2160c98d5d94SLen Brown 
2161c98d5d94SLen Brown 	/*
2162c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
2163c98d5d94SLen Brown 	 */
2164c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2165b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
2166b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
2167c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2168c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2169c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
2170c98d5d94SLen Brown 
2171c98d5d94SLen Brown 	/*
2172c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
2173c98d5d94SLen Brown 	 */
2174c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2175b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
2176b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
2177c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2178c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2179c98d5d94SLen Brown 
2180c98d5d94SLen Brown 
2181c98d5d94SLen Brown 	/*
2182c98d5d94SLen Brown 	 * For online cpus
2183c98d5d94SLen Brown 	 * find max_core_id, max_package_id
2184c98d5d94SLen Brown 	 */
2185c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
2186c98d5d94SLen Brown 		int siblings;
2187c98d5d94SLen Brown 
2188c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
2189c98d5d94SLen Brown 			if (verbose > 1)
2190c98d5d94SLen Brown 				fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2191c98d5d94SLen Brown 			continue;
2192c98d5d94SLen Brown 		}
2193c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
2194c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
2195c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
2196c98d5d94SLen Brown 
2197c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
2198c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
2199c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
2200c98d5d94SLen Brown 
2201c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
2202c98d5d94SLen Brown 		if (siblings > max_siblings)
2203c98d5d94SLen Brown 			max_siblings = siblings;
2204c98d5d94SLen Brown 		if (verbose > 1)
2205c98d5d94SLen Brown 			fprintf(stderr, "cpu %d pkg %d core %d\n",
2206c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
2207c98d5d94SLen Brown 	}
2208c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
2209c98d5d94SLen Brown 	if (verbose > 1)
2210c98d5d94SLen Brown 		fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2211c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
2212c98d5d94SLen Brown 	if (!summary_only && topo.num_cores_per_pkg > 1)
2213c98d5d94SLen Brown 		show_core = 1;
2214c98d5d94SLen Brown 
2215c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
2216c98d5d94SLen Brown 	if (verbose > 1)
2217c98d5d94SLen Brown 		fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2218c98d5d94SLen Brown 			max_package_id, topo.num_packages);
2219c98d5d94SLen Brown 	if (!summary_only && topo.num_packages > 1)
2220c98d5d94SLen Brown 		show_pkg = 1;
2221c98d5d94SLen Brown 
2222c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
2223c98d5d94SLen Brown 	if (verbose > 1)
2224c98d5d94SLen Brown 		fprintf(stderr, "max_siblings %d\n", max_siblings);
2225c98d5d94SLen Brown 
2226c98d5d94SLen Brown 	free(cpus);
2227c98d5d94SLen Brown }
2228c98d5d94SLen Brown 
2229c98d5d94SLen Brown void
2230c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2231c98d5d94SLen Brown {
2232c98d5d94SLen Brown 	int i;
2233c98d5d94SLen Brown 
2234c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2235c98d5d94SLen Brown 		topo.num_packages, sizeof(struct thread_data));
2236c98d5d94SLen Brown 	if (*t == NULL)
2237c98d5d94SLen Brown 		goto error;
2238c98d5d94SLen Brown 
2239c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
2240c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
2241c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
2242c98d5d94SLen Brown 
2243c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2244c98d5d94SLen Brown 		sizeof(struct core_data));
2245c98d5d94SLen Brown 	if (*c == NULL)
2246c98d5d94SLen Brown 		goto error;
2247c98d5d94SLen Brown 
2248c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2249c98d5d94SLen Brown 		(*c)[i].core_id = -1;
2250c98d5d94SLen Brown 
2251c98d5d94SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
2252c98d5d94SLen Brown 	if (*p == NULL)
2253c98d5d94SLen Brown 		goto error;
2254c98d5d94SLen Brown 
2255c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
2256c98d5d94SLen Brown 		(*p)[i].package_id = i;
2257c98d5d94SLen Brown 
2258c98d5d94SLen Brown 	return;
2259c98d5d94SLen Brown error:
2260b2c95d90SJosh Triplett 	err(1, "calloc counters");
2261c98d5d94SLen Brown }
2262c98d5d94SLen Brown /*
2263c98d5d94SLen Brown  * init_counter()
2264c98d5d94SLen Brown  *
2265c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
2266c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2267c98d5d94SLen Brown  *
2268c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
2269c98d5d94SLen Brown  */
2270c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2271c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
2272c98d5d94SLen Brown 	int pkg_num, int cpu_id)
2273c98d5d94SLen Brown {
2274c98d5d94SLen Brown 	struct thread_data *t;
2275c98d5d94SLen Brown 	struct core_data *c;
2276c98d5d94SLen Brown 	struct pkg_data *p;
2277c98d5d94SLen Brown 
2278c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2279c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
2280c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
2281c98d5d94SLen Brown 
2282c98d5d94SLen Brown 	t->cpu_id = cpu_id;
2283c98d5d94SLen Brown 	if (thread_num == 0) {
2284c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2285c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
2286c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2287c98d5d94SLen Brown 	}
2288c98d5d94SLen Brown 
2289c98d5d94SLen Brown 	c->core_id = core_num;
2290c98d5d94SLen Brown 	p->package_id = pkg_num;
2291c98d5d94SLen Brown }
2292c98d5d94SLen Brown 
2293c98d5d94SLen Brown 
2294c98d5d94SLen Brown int initialize_counters(int cpu_id)
2295c98d5d94SLen Brown {
2296c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
2297c98d5d94SLen Brown 
2298c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
2299c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
2300c98d5d94SLen Brown 
2301c98d5d94SLen Brown 	if (cpu_is_first_sibling_in_core(cpu_id)) {
2302c98d5d94SLen Brown 		my_thread_id = 0;
2303c98d5d94SLen Brown 		topo.num_cores++;
2304c98d5d94SLen Brown 	} else {
2305c98d5d94SLen Brown 		my_thread_id = 1;
2306c98d5d94SLen Brown 	}
2307c98d5d94SLen Brown 
2308c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2309c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2310c98d5d94SLen Brown 	return 0;
2311c98d5d94SLen Brown }
2312c98d5d94SLen Brown 
2313c98d5d94SLen Brown void allocate_output_buffer()
2314c98d5d94SLen Brown {
23153b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
2316c98d5d94SLen Brown 	outp = output_buffer;
2317b2c95d90SJosh Triplett 	if (outp == NULL)
2318b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
2319c98d5d94SLen Brown }
2320c98d5d94SLen Brown 
2321c98d5d94SLen Brown void setup_all_buffers(void)
2322c98d5d94SLen Brown {
2323c98d5d94SLen Brown 	topology_probe();
2324c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
2325c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
2326c98d5d94SLen Brown 	allocate_output_buffer();
2327c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
2328c98d5d94SLen Brown }
23293b4d5c7fSAndy Shevchenko 
2330103a8feaSLen Brown void turbostat_init()
2331103a8feaSLen Brown {
2332103a8feaSLen Brown 	check_dev_msr();
2333*98481e79SLen Brown 	check_permissions();
2334*98481e79SLen Brown 	check_cpuid();
2335103a8feaSLen Brown 
2336c98d5d94SLen Brown 	setup_all_buffers();
2337103a8feaSLen Brown 
2338103a8feaSLen Brown 	if (verbose)
2339c98d5d94SLen Brown 		print_verbose_header();
2340889facbeSLen Brown 
2341889facbeSLen Brown 	if (verbose)
2342889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
2343889facbeSLen Brown 
2344889facbeSLen Brown 	if (verbose)
2345889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
2346889facbeSLen Brown 
2347889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
2348889facbeSLen Brown 
2349889facbeSLen Brown 	if (verbose)
2350889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
2351103a8feaSLen Brown }
2352103a8feaSLen Brown 
2353103a8feaSLen Brown int fork_it(char **argv)
2354103a8feaSLen Brown {
2355103a8feaSLen Brown 	pid_t child_pid;
2356d91bb17cSLen Brown 	int status;
2357d15cf7c1SLen Brown 
2358d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
2359d91bb17cSLen Brown 	if (status)
2360d91bb17cSLen Brown 		exit(status);
2361d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
2362d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2363103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2364103a8feaSLen Brown 
2365103a8feaSLen Brown 	child_pid = fork();
2366103a8feaSLen Brown 	if (!child_pid) {
2367103a8feaSLen Brown 		/* child */
2368103a8feaSLen Brown 		execvp(argv[0], argv);
2369103a8feaSLen Brown 	} else {
2370103a8feaSLen Brown 
2371103a8feaSLen Brown 		/* parent */
2372b2c95d90SJosh Triplett 		if (child_pid == -1)
2373b2c95d90SJosh Triplett 			err(1, "fork");
2374103a8feaSLen Brown 
2375103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
2376103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
2377b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
2378b2c95d90SJosh Triplett 			err(status, "waitpid");
2379103a8feaSLen Brown 	}
2380c98d5d94SLen Brown 	/*
2381c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
2382c98d5d94SLen Brown 	 * because re-starting is problematic when forking
2383c98d5d94SLen Brown 	 */
2384c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
2385103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
2386103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
2387c98d5d94SLen Brown 	for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2388c98d5d94SLen Brown 	compute_average(EVEN_COUNTERS);
2389c98d5d94SLen Brown 	format_all_counters(EVEN_COUNTERS);
2390c98d5d94SLen Brown 	flush_stderr();
2391103a8feaSLen Brown 
23926eab04a8SJustin P. Mattock 	fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2393103a8feaSLen Brown 
2394d91bb17cSLen Brown 	return status;
2395103a8feaSLen Brown }
2396103a8feaSLen Brown 
23973b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
23983b4d5c7fSAndy Shevchenko {
23993b4d5c7fSAndy Shevchenko 	int status;
24003b4d5c7fSAndy Shevchenko 
24013b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
24023b4d5c7fSAndy Shevchenko 	if (status)
24033b4d5c7fSAndy Shevchenko 		return status;
24043b4d5c7fSAndy Shevchenko 
24053b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
24063b4d5c7fSAndy Shevchenko 	if (status)
24073b4d5c7fSAndy Shevchenko 		return status;
24083b4d5c7fSAndy Shevchenko 
24093b4d5c7fSAndy Shevchenko 	flush_stdout();
24103b4d5c7fSAndy Shevchenko 
24113b4d5c7fSAndy Shevchenko 	return status;
24123b4d5c7fSAndy Shevchenko }
24133b4d5c7fSAndy Shevchenko 
2414103a8feaSLen Brown void cmdline(int argc, char **argv)
2415103a8feaSLen Brown {
2416103a8feaSLen Brown 	int opt;
2417103a8feaSLen Brown 
2418103a8feaSLen Brown 	progname = argv[0];
2419103a8feaSLen Brown 
24203b4d5c7fSAndy Shevchenko 	while ((opt = getopt(argc, argv, "+pPsSvi:c:C:m:M:RJT:")) != -1) {
2421103a8feaSLen Brown 		switch (opt) {
2422f9240813SLen Brown 		case 'p':
2423c98d5d94SLen Brown 			show_core_only++;
2424c98d5d94SLen Brown 			break;
2425f9240813SLen Brown 		case 'P':
2426c98d5d94SLen Brown 			show_pkg_only++;
2427c98d5d94SLen Brown 			break;
24283b4d5c7fSAndy Shevchenko 		case 's':
24293b4d5c7fSAndy Shevchenko 			dump_only++;
24303b4d5c7fSAndy Shevchenko 			break;
2431f9240813SLen Brown 		case 'S':
2432e23da037SLen Brown 			summary_only++;
2433e23da037SLen Brown 			break;
2434103a8feaSLen Brown 		case 'v':
2435103a8feaSLen Brown 			verbose++;
2436103a8feaSLen Brown 			break;
2437103a8feaSLen Brown 		case 'i':
2438103a8feaSLen Brown 			interval_sec = atoi(optarg);
2439103a8feaSLen Brown 			break;
2440f9240813SLen Brown 		case 'c':
24418e180f3cSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset32);
24428e180f3cSLen Brown 			break;
2443f9240813SLen Brown 		case 'C':
24448e180f3cSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset64);
24458e180f3cSLen Brown 			break;
24462f32edf1SLen Brown 		case 'm':
24472f32edf1SLen Brown 			sscanf(optarg, "%x", &extra_msr_offset32);
24482f32edf1SLen Brown 			break;
24492f32edf1SLen Brown 		case 'M':
24502f32edf1SLen Brown 			sscanf(optarg, "%x", &extra_msr_offset64);
2451103a8feaSLen Brown 			break;
2452889facbeSLen Brown 		case 'R':
2453889facbeSLen Brown 			rapl_verbose++;
2454889facbeSLen Brown 			break;
2455889facbeSLen Brown 		case 'T':
2456889facbeSLen Brown 			tcc_activation_temp_override = atoi(optarg);
2457889facbeSLen Brown 			break;
24585c56be9aSDirk Brandewie 		case 'J':
24595c56be9aSDirk Brandewie 			rapl_joules++;
24605c56be9aSDirk Brandewie 			break;
24615c56be9aSDirk Brandewie 
2462103a8feaSLen Brown 		default:
2463103a8feaSLen Brown 			usage();
2464103a8feaSLen Brown 		}
2465103a8feaSLen Brown 	}
2466103a8feaSLen Brown }
2467103a8feaSLen Brown 
2468103a8feaSLen Brown int main(int argc, char **argv)
2469103a8feaSLen Brown {
2470103a8feaSLen Brown 	cmdline(argc, argv);
2471103a8feaSLen Brown 
2472889facbeSLen Brown 	if (verbose)
2473*98481e79SLen Brown 		fprintf(stderr, "turbostat v3.8 14-Aug 2014"
2474103a8feaSLen Brown 			" - Len Brown <lenb@kernel.org>\n");
2475103a8feaSLen Brown 
2476103a8feaSLen Brown 	turbostat_init();
2477103a8feaSLen Brown 
24783b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
24793b4d5c7fSAndy Shevchenko 	if (dump_only)
24803b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
24813b4d5c7fSAndy Shevchenko 
2482103a8feaSLen Brown 	/*
2483103a8feaSLen Brown 	 * if any params left, it must be a command to fork
2484103a8feaSLen Brown 	 */
2485103a8feaSLen Brown 	if (argc - optind)
2486103a8feaSLen Brown 		return fork_it(argv + optind);
2487103a8feaSLen Brown 	else
2488103a8feaSLen Brown 		turbostat_loop();
2489103a8feaSLen Brown 
2490103a8feaSLen Brown 	return 0;
2491103a8feaSLen Brown }
2492