xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 01a67adfc5d73d24e999d9cf65b5b8a6687187b8)
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>
36d8af6f5fSLen Brown #include <getopt.h>
37103a8feaSLen Brown #include <dirent.h>
38103a8feaSLen Brown #include <string.h>
39103a8feaSLen Brown #include <ctype.h>
4088c3281fSLen Brown #include <sched.h>
412a0609c0SLen Brown #include <time.h>
422b92865eSJosh Triplett #include <cpuid.h>
4398481e79SLen Brown #include <linux/capability.h>
4498481e79SLen Brown #include <errno.h>
45103a8feaSLen Brown 
46103a8feaSLen Brown char *proc_stat = "/proc/stat";
47b7d8c148SLen Brown FILE *outf;
4836229897SLen Brown int *fd_percpu;
492a0609c0SLen Brown struct timespec interval_ts = {5, 0};
50d8af6f5fSLen Brown unsigned int debug;
51d8af6f5fSLen Brown unsigned int rapl_joules;
52d8af6f5fSLen Brown unsigned int summary_only;
53d8af6f5fSLen Brown unsigned int dump_only;
54103a8feaSLen Brown unsigned int do_nhm_cstates;
55103a8feaSLen Brown unsigned int do_snb_cstates;
56fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
57ee7e38e3SLen Brown unsigned int do_pc2;
58ee7e38e3SLen Brown unsigned int do_pc3;
59ee7e38e3SLen Brown unsigned int do_pc6;
60ee7e38e3SLen Brown unsigned int do_pc7;
61ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10;
620b2bb692SLen Brown unsigned int do_skl_residency;
63144b44b1SLen Brown unsigned int do_slm_cstates;
64144b44b1SLen Brown unsigned int use_c1_residency_msr;
65103a8feaSLen Brown unsigned int has_aperf;
66889facbeSLen Brown unsigned int has_epb;
675a63426eSLen Brown unsigned int do_irtl_snb;
685a63426eSLen Brown unsigned int do_irtl_hsw;
69fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
70103a8feaSLen Brown unsigned int genuine_intel;
71103a8feaSLen Brown unsigned int has_invariant_tsc;
72d7899447SLen Brown unsigned int do_nhm_platform_info;
732f32edf1SLen Brown unsigned int extra_msr_offset32;
742f32edf1SLen Brown unsigned int extra_msr_offset64;
758e180f3cSLen Brown unsigned int extra_delta_offset32;
768e180f3cSLen Brown unsigned int extra_delta_offset64;
77b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
78562a2d37SLen Brown int do_irq = 1;
791ed51011SLen Brown int do_smi;
80103a8feaSLen Brown double bclk;
81a2b7b749SLen Brown double base_hz;
8221ed5574SLen Brown unsigned int has_base_hz;
83a2b7b749SLen Brown double tsc_tweak = 1.0;
84103a8feaSLen Brown unsigned int show_pkg;
85103a8feaSLen Brown unsigned int show_core;
86103a8feaSLen Brown unsigned int show_cpu;
87c98d5d94SLen Brown unsigned int show_pkg_only;
88c98d5d94SLen Brown unsigned int show_core_only;
89c98d5d94SLen Brown char *output_buffer, *outp;
90889facbeSLen Brown unsigned int do_rapl;
91889facbeSLen Brown unsigned int do_dts;
92889facbeSLen Brown unsigned int do_ptm;
93fdf676e5SLen Brown unsigned int do_gfx_rc6_ms;
94fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
9527d47356SLen Brown unsigned int do_gfx_mhz;
9627d47356SLen Brown unsigned int gfx_cur_mhz;
97889facbeSLen Brown unsigned int tcc_activation_temp;
98889facbeSLen Brown unsigned int tcc_activation_temp_override;
9940ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
10040ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
101889facbeSLen Brown double rapl_joule_counter_range;
1023a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
1033a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
1043a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
1058a5bdf41SLen Brown unsigned int crystal_hz;
1068a5bdf41SLen Brown unsigned long long tsc_hz;
1077ce7d5deSPrarit Bhargava int base_cpu;
10821ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
1097f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
1107f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
1117f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
1127f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1137f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1147f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
115889facbeSLen Brown 
116889facbeSLen Brown #define RAPL_PKG		(1 << 0)
117e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
118e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
119e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
120e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
121e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
122e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
123e6f9bb3cSLen Brown 
124889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
125e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
126e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
127e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
128e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1290b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1300b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
131e6f9bb3cSLen Brown 
1320b2bb692SLen Brown #define RAPL_CORES		(1 << 6)
133e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
134e6f9bb3cSLen Brown 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1350b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
136e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
137e6f9bb3cSLen Brown 
1380b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
139e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
140e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
141e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
142889facbeSLen Brown #define	TJMAX_DEFAULT	100
143889facbeSLen Brown 
144889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
145103a8feaSLen Brown 
146103a8feaSLen Brown int backwards_count;
147103a8feaSLen Brown char *progname;
148103a8feaSLen Brown 
149c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set;
150c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize;
151c98d5d94SLen Brown 
152c98d5d94SLen Brown struct thread_data {
153c98d5d94SLen Brown 	unsigned long long tsc;
154c98d5d94SLen Brown 	unsigned long long aperf;
155c98d5d94SLen Brown 	unsigned long long mperf;
156144b44b1SLen Brown 	unsigned long long c1;
1572f32edf1SLen Brown 	unsigned long long extra_msr64;
1588e180f3cSLen Brown 	unsigned long long extra_delta64;
1598e180f3cSLen Brown 	unsigned long long extra_msr32;
1608e180f3cSLen Brown 	unsigned long long extra_delta32;
161562a2d37SLen Brown 	unsigned int irq_count;
1621ed51011SLen Brown 	unsigned int smi_count;
163c98d5d94SLen Brown 	unsigned int cpu_id;
164c98d5d94SLen Brown 	unsigned int flags;
165c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
166c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
167c98d5d94SLen Brown } *thread_even, *thread_odd;
168c98d5d94SLen Brown 
169c98d5d94SLen Brown struct core_data {
170c98d5d94SLen Brown 	unsigned long long c3;
171c98d5d94SLen Brown 	unsigned long long c6;
172c98d5d94SLen Brown 	unsigned long long c7;
173889facbeSLen Brown 	unsigned int core_temp_c;
174c98d5d94SLen Brown 	unsigned int core_id;
175c98d5d94SLen Brown } *core_even, *core_odd;
176c98d5d94SLen Brown 
177c98d5d94SLen Brown struct pkg_data {
178c98d5d94SLen Brown 	unsigned long long pc2;
179c98d5d94SLen Brown 	unsigned long long pc3;
180c98d5d94SLen Brown 	unsigned long long pc6;
181c98d5d94SLen Brown 	unsigned long long pc7;
182ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
183ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
184ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1850b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1860b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1870b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1880b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
1899185e988SLen Brown 	long long gfx_rc6_ms;
19027d47356SLen Brown 	unsigned int gfx_mhz;
191c98d5d94SLen Brown 	unsigned int package_id;
192889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
193889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
194889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
195889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
196889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
197889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
198889facbeSLen Brown 	unsigned int pkg_temp_c;
199889facbeSLen Brown 
200c98d5d94SLen Brown } *package_even, *package_odd;
201c98d5d94SLen Brown 
202c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
203c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
204c98d5d94SLen Brown 
205c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
206c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
207c98d5d94SLen Brown 		topo.num_threads_per_core + \
208c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
209c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
210c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
211c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
212c98d5d94SLen Brown 
213c98d5d94SLen Brown struct system_summary {
214c98d5d94SLen Brown 	struct thread_data threads;
215c98d5d94SLen Brown 	struct core_data cores;
216c98d5d94SLen Brown 	struct pkg_data packages;
217c98d5d94SLen Brown } sum, average;
218c98d5d94SLen Brown 
219c98d5d94SLen Brown 
220c98d5d94SLen Brown struct topo_params {
221c98d5d94SLen Brown 	int num_packages;
222103a8feaSLen Brown 	int num_cpus;
223c98d5d94SLen Brown 	int num_cores;
224c98d5d94SLen Brown 	int max_cpu_num;
225c98d5d94SLen Brown 	int num_cores_per_pkg;
226c98d5d94SLen Brown 	int num_threads_per_core;
227c98d5d94SLen Brown } topo;
228103a8feaSLen Brown 
229c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
230103a8feaSLen Brown 
231562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
232562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
233562a2d37SLen Brown 
234c98d5d94SLen Brown void setup_all_buffers(void);
235103a8feaSLen Brown 
236c98d5d94SLen Brown int cpu_is_not_present(int cpu)
237d15cf7c1SLen Brown {
238c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
239c98d5d94SLen Brown }
240c98d5d94SLen Brown /*
241c98d5d94SLen Brown  * run func(thread, core, package) in topology order
242c98d5d94SLen Brown  * skip non-present cpus
243c98d5d94SLen Brown  */
244c98d5d94SLen Brown 
245c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
246c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
247c98d5d94SLen Brown {
248c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
249c98d5d94SLen Brown 
250c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
251c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
252c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
253c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
254c98d5d94SLen Brown 				struct thread_data *t;
255c98d5d94SLen Brown 				struct core_data *c;
256c98d5d94SLen Brown 				struct pkg_data *p;
257c98d5d94SLen Brown 
258c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
259c98d5d94SLen Brown 
260c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
261c98d5d94SLen Brown 					continue;
262c98d5d94SLen Brown 
263c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
264c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
265c98d5d94SLen Brown 
266c98d5d94SLen Brown 				retval = func(t, c, p);
267c98d5d94SLen Brown 				if (retval)
268c98d5d94SLen Brown 					return retval;
269c98d5d94SLen Brown 			}
270c98d5d94SLen Brown 		}
271c98d5d94SLen Brown 	}
272d15cf7c1SLen Brown 	return 0;
273d15cf7c1SLen Brown }
274d15cf7c1SLen Brown 
27588c3281fSLen Brown int cpu_migrate(int cpu)
27688c3281fSLen Brown {
277c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
278c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
279c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
28088c3281fSLen Brown 		return -1;
28188c3281fSLen Brown 	else
28288c3281fSLen Brown 		return 0;
28388c3281fSLen Brown }
28436229897SLen Brown int get_msr_fd(int cpu)
285103a8feaSLen Brown {
286103a8feaSLen Brown 	char pathname[32];
287103a8feaSLen Brown 	int fd;
288103a8feaSLen Brown 
28936229897SLen Brown 	fd = fd_percpu[cpu];
29036229897SLen Brown 
29136229897SLen Brown 	if (fd)
29236229897SLen Brown 		return fd;
29336229897SLen Brown 
294103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
295103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
29615aaa346SLen Brown 	if (fd < 0)
29798481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
298103a8feaSLen Brown 
29936229897SLen Brown 	fd_percpu[cpu] = fd;
30036229897SLen Brown 
30136229897SLen Brown 	return fd;
30236229897SLen Brown }
30336229897SLen Brown 
30436229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
30536229897SLen Brown {
30636229897SLen Brown 	ssize_t retval;
30736229897SLen Brown 
30836229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
30915aaa346SLen Brown 
31098481e79SLen Brown 	if (retval != sizeof *msr)
31136229897SLen Brown 		err(-1, "msr %d offset 0x%llx read failed", cpu, (unsigned long long)offset);
31215aaa346SLen Brown 
31315aaa346SLen Brown 	return 0;
314103a8feaSLen Brown }
315103a8feaSLen Brown 
316fc04cc67SLen Brown /*
317fc04cc67SLen Brown  * Example Format w/ field column widths:
318fc04cc67SLen Brown  *
31927d47356SLen Brown  *  Package    Core     CPU Avg_MHz Bzy_MHz TSC_MHz     IRQ   SMI   Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp  PkgTmp  GFXMHz Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
320562a2d37SLen Brown  * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
321fc04cc67SLen Brown  */
322fc04cc67SLen Brown 
323a829eb4dSLen Brown void print_header(void)
324103a8feaSLen Brown {
325103a8feaSLen Brown 	if (show_pkg)
3263d109de2SLen Brown 		outp += sprintf(outp, "\tPackage");
327103a8feaSLen Brown 	if (show_core)
3283d109de2SLen Brown 		outp += sprintf(outp, "\tCore");
329103a8feaSLen Brown 	if (show_cpu)
3303d109de2SLen Brown 		outp += sprintf(outp, "\tCPU");
331103a8feaSLen Brown 	if (has_aperf)
3323d109de2SLen Brown 		outp += sprintf(outp, "\tAvg_MHz");
333d7899447SLen Brown 	if (has_aperf)
3343d109de2SLen Brown 		outp += sprintf(outp, "\tBusy%%");
335fc04cc67SLen Brown 	if (has_aperf)
3363d109de2SLen Brown 		outp += sprintf(outp, "\tBzy_MHz");
3373d109de2SLen Brown 	outp += sprintf(outp, "\tTSC_MHz");
3381cc21f7bSLen Brown 
3398e180f3cSLen Brown 	if (extra_delta_offset32)
3403d109de2SLen Brown 		outp += sprintf(outp, "\tcount 0x%03X", extra_delta_offset32);
3418e180f3cSLen Brown 	if (extra_delta_offset64)
3423d109de2SLen Brown 		outp += sprintf(outp, "\tCOUNT 0x%03X", extra_delta_offset64);
3432f32edf1SLen Brown 	if (extra_msr_offset32)
3443d109de2SLen Brown 		outp += sprintf(outp, "\tMSR 0x%03X", extra_msr_offset32);
3452f32edf1SLen Brown 	if (extra_msr_offset64)
3463d109de2SLen Brown 		outp += sprintf(outp, "\tMSR 0x%03X", extra_msr_offset64);
3471cc21f7bSLen Brown 
3481cc21f7bSLen Brown 	if (!debug)
3491cc21f7bSLen Brown 		goto done;
3501cc21f7bSLen Brown 
351562a2d37SLen Brown 	if (do_irq)
3523d109de2SLen Brown 		outp += sprintf(outp, "\tIRQ");
3531cc21f7bSLen Brown 	if (do_smi)
3543d109de2SLen Brown 		outp += sprintf(outp, "\tSMI");
3551cc21f7bSLen Brown 
356103a8feaSLen Brown 	if (do_nhm_cstates)
3573d109de2SLen Brown 		outp += sprintf(outp, "\tCPU%%c1");
358fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
3593d109de2SLen Brown 		outp += sprintf(outp, "\tCPU%%c3");
360103a8feaSLen Brown 	if (do_nhm_cstates)
3613d109de2SLen Brown 		outp += sprintf(outp, "\tCPU%%c6");
362103a8feaSLen Brown 	if (do_snb_cstates)
3633d109de2SLen Brown 		outp += sprintf(outp, "\tCPU%%c7");
364889facbeSLen Brown 
365889facbeSLen Brown 	if (do_dts)
3663d109de2SLen Brown 		outp += sprintf(outp, "\tCoreTmp");
367889facbeSLen Brown 	if (do_ptm)
3683d109de2SLen Brown 		outp += sprintf(outp, "\tPkgTmp");
369889facbeSLen Brown 
370fdf676e5SLen Brown 	if (do_gfx_rc6_ms)
3713d109de2SLen Brown 		outp += sprintf(outp, "\tGFX%%rc6");
372fdf676e5SLen Brown 
37327d47356SLen Brown 	if (do_gfx_mhz)
3743d109de2SLen Brown 		outp += sprintf(outp, "\tGFXMHz");
37527d47356SLen Brown 
3760b2bb692SLen Brown 	if (do_skl_residency) {
3773d109de2SLen Brown 		outp += sprintf(outp, "\tTotl%%C0");
3783d109de2SLen Brown 		outp += sprintf(outp, "\tAny%%C0");
3793d109de2SLen Brown 		outp += sprintf(outp, "\tGFX%%C0");
3803d109de2SLen Brown 		outp += sprintf(outp, "\tCPUGFX%%");
3810b2bb692SLen Brown 	}
3820b2bb692SLen Brown 
383ee7e38e3SLen Brown 	if (do_pc2)
3843d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc2");
385ee7e38e3SLen Brown 	if (do_pc3)
3863d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc3");
387ee7e38e3SLen Brown 	if (do_pc6)
3883d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc6");
389ee7e38e3SLen Brown 	if (do_pc7)
3903d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc7");
391ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
3923d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc8");
3933d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc9");
3943d109de2SLen Brown 		outp += sprintf(outp, "\tPk%%pc10");
395ca58710fSKristen Carlson Accardi 	}
396103a8feaSLen Brown 
3975c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
398889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
3993d109de2SLen Brown 			outp += sprintf(outp, "\tPkgWatt");
400889facbeSLen Brown 		if (do_rapl & RAPL_CORES)
4013d109de2SLen Brown 			outp += sprintf(outp, "\tCorWatt");
402889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
4033d109de2SLen Brown 			outp += sprintf(outp, "\tGFXWatt");
404889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
4053d109de2SLen Brown 			outp += sprintf(outp, "\tRAMWatt");
406889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
4073d109de2SLen Brown 			outp += sprintf(outp, "\tPKG_%%");
408889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
4093d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_%%");
410d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
4115c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
4123d109de2SLen Brown 			outp += sprintf(outp, "\tPkg_J");
4135c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
4143d109de2SLen Brown 			outp += sprintf(outp, "\tCor_J");
4155c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
4163d109de2SLen Brown 			outp += sprintf(outp, "\tGFX_J");
4175c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
4183d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_J");
4195c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
4203d109de2SLen Brown 			outp += sprintf(outp, "\tPKG_%%");
4215c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
4223d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_%%");
4235c56be9aSDirk Brandewie 	}
4241cc21f7bSLen Brown     done:
425c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
426103a8feaSLen Brown }
427103a8feaSLen Brown 
428c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
429c98d5d94SLen Brown 	struct pkg_data *p)
430103a8feaSLen Brown {
4313b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
432c98d5d94SLen Brown 
433c98d5d94SLen Brown 	if (t) {
4343b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
4353b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
4363b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
4373b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
4383b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
4393b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
4403b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %08llX\n",
4418e180f3cSLen Brown 			extra_delta_offset32, t->extra_delta32);
4423b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %016llX\n",
4438e180f3cSLen Brown 			extra_delta_offset64, t->extra_delta64);
4443b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %08llX\n",
4452f32edf1SLen Brown 			extra_msr_offset32, t->extra_msr32);
4463b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %016llX\n",
4472f32edf1SLen Brown 			extra_msr_offset64, t->extra_msr64);
448562a2d37SLen Brown 		if (do_irq)
449562a2d37SLen Brown 			outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
4501ed51011SLen Brown 		if (do_smi)
4513b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
452103a8feaSLen Brown 	}
453103a8feaSLen Brown 
454c98d5d94SLen Brown 	if (c) {
4553b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
4563b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
4573b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
4583b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
4593b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
460c98d5d94SLen Brown 	}
461103a8feaSLen Brown 
462c98d5d94SLen Brown 	if (p) {
4633b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
4640b2bb692SLen Brown 
4650b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
4660b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
4670b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
4680b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
4690b2bb692SLen Brown 
4703b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
471ee7e38e3SLen Brown 		if (do_pc3)
4723b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
473ee7e38e3SLen Brown 		if (do_pc6)
4743b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
475ee7e38e3SLen Brown 		if (do_pc7)
4763b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
4773b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
4783b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
4793b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
4803b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
4813b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
4823b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
4833b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
4843b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
4853b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
4863b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
4873b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
4883b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
489c98d5d94SLen Brown 	}
4903b4d5c7fSAndy Shevchenko 
4913b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
4923b4d5c7fSAndy Shevchenko 
493c98d5d94SLen Brown 	return 0;
494103a8feaSLen Brown }
495103a8feaSLen Brown 
496e23da037SLen Brown /*
497e23da037SLen Brown  * column formatting convention & formats
498e23da037SLen Brown  */
499c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
500c98d5d94SLen Brown 	struct pkg_data *p)
501103a8feaSLen Brown {
502103a8feaSLen Brown 	double interval_float;
503fc04cc67SLen Brown 	char *fmt8;
504103a8feaSLen Brown 
505c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
506c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
507c98d5d94SLen Brown 		return 0;
508c98d5d94SLen Brown 
509c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
510c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
511c98d5d94SLen Brown 		return 0;
512c98d5d94SLen Brown 
513103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
514103a8feaSLen Brown 
515c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
516c98d5d94SLen Brown 	if (t == &average.threads) {
517103a8feaSLen Brown 		if (show_pkg)
5183d109de2SLen Brown 			outp += sprintf(outp, "\t-");
519103a8feaSLen Brown 		if (show_core)
5203d109de2SLen Brown 			outp += sprintf(outp, "\t-");
521103a8feaSLen Brown 		if (show_cpu)
5223d109de2SLen Brown 			outp += sprintf(outp, "\t-");
523103a8feaSLen Brown 	} else {
524c98d5d94SLen Brown 		if (show_pkg) {
525c98d5d94SLen Brown 			if (p)
5263d109de2SLen Brown 				outp += sprintf(outp, "\t%d", p->package_id);
527c98d5d94SLen Brown 			else
5283d109de2SLen Brown 				outp += sprintf(outp, "\t-");
529c98d5d94SLen Brown 		}
530c98d5d94SLen Brown 		if (show_core) {
531c98d5d94SLen Brown 			if (c)
5323d109de2SLen Brown 				outp += sprintf(outp, "\t%d", c->core_id);
533c98d5d94SLen Brown 			else
5343d109de2SLen Brown 				outp += sprintf(outp, "\t-");
535c98d5d94SLen Brown 		}
536103a8feaSLen Brown 		if (show_cpu)
5373d109de2SLen Brown 			outp += sprintf(outp, "\t%d", t->cpu_id);
538103a8feaSLen Brown 	}
539fc04cc67SLen Brown 
540d7899447SLen Brown 	/* Avg_MHz */
541fc04cc67SLen Brown 	if (has_aperf)
5423d109de2SLen Brown 		outp += sprintf(outp, "\t%.0f",
543fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
544fc04cc67SLen Brown 
54575d2e44eSLen Brown 	/* Busy% */
546ba3dec99SLen Brown 	if (has_aperf)
5473d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
548103a8feaSLen Brown 
549d7899447SLen Brown 	/* Bzy_MHz */
55021ed5574SLen Brown 	if (has_aperf) {
55121ed5574SLen Brown 		if (has_base_hz)
5523d109de2SLen Brown 			outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
55321ed5574SLen Brown 		else
5543d109de2SLen Brown 			outp += sprintf(outp, "\t%.0f",
55521ed5574SLen Brown 				1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
55621ed5574SLen Brown 	}
557103a8feaSLen Brown 
558d7899447SLen Brown 	/* TSC_MHz */
5593d109de2SLen Brown 	outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
560103a8feaSLen Brown 
5618e180f3cSLen Brown 	/* delta */
5628e180f3cSLen Brown 	if (extra_delta_offset32)
5633d109de2SLen Brown 		outp += sprintf(outp, "\t%11llu", t->extra_delta32);
5648e180f3cSLen Brown 
5658e180f3cSLen Brown 	/* DELTA */
5668e180f3cSLen Brown 	if (extra_delta_offset64)
5673d109de2SLen Brown 		outp += sprintf(outp, "\t%11llu", t->extra_delta64);
5682f32edf1SLen Brown 	/* msr */
5692f32edf1SLen Brown 	if (extra_msr_offset32)
5703d109de2SLen Brown 		outp += sprintf(outp, "\t0x%08llx", t->extra_msr32);
5712f32edf1SLen Brown 
572130ff304SLen Brown 	/* MSR */
5732f32edf1SLen Brown 	if (extra_msr_offset64)
5743d109de2SLen Brown 		outp += sprintf(outp, "\t0x%016llx", t->extra_msr64);
575130ff304SLen Brown 
5761cc21f7bSLen Brown 	if (!debug)
5771cc21f7bSLen Brown 		goto done;
5781cc21f7bSLen Brown 
579562a2d37SLen Brown 	/* IRQ */
580562a2d37SLen Brown 	if (do_irq)
5813d109de2SLen Brown 		outp += sprintf(outp, "\t%d", t->irq_count);
582562a2d37SLen Brown 
5831cc21f7bSLen Brown 	/* SMI */
5841cc21f7bSLen Brown 	if (do_smi)
5853d109de2SLen Brown 		outp += sprintf(outp, "\t%d", t->smi_count);
5861cc21f7bSLen Brown 
587ba3dec99SLen Brown 	if (do_nhm_cstates)
5883d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
589c98d5d94SLen Brown 
590c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
591c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
592c98d5d94SLen Brown 		goto done;
593c98d5d94SLen Brown 
594fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
5953d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
596103a8feaSLen Brown 	if (do_nhm_cstates)
5973d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
598103a8feaSLen Brown 	if (do_snb_cstates)
5993d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
600c98d5d94SLen Brown 
601889facbeSLen Brown 	if (do_dts)
6023d109de2SLen Brown 		outp += sprintf(outp, "\t%d", c->core_temp_c);
603889facbeSLen Brown 
604c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
605c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
606c98d5d94SLen Brown 		goto done;
607c98d5d94SLen Brown 
6080b2bb692SLen Brown 	/* PkgTmp */
609889facbeSLen Brown 	if (do_ptm)
6103d109de2SLen Brown 		outp += sprintf(outp, "\t%d", p->pkg_temp_c);
611889facbeSLen Brown 
612fdf676e5SLen Brown 	/* GFXrc6 */
6139185e988SLen Brown 	if (do_gfx_rc6_ms) {
614ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
6153d109de2SLen Brown 			outp += sprintf(outp, "\t**.**");
6169185e988SLen Brown 		} else {
6173d109de2SLen Brown 			outp += sprintf(outp, "\t%.2f",
6189185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
6199185e988SLen Brown 		}
6209185e988SLen Brown 	}
621fdf676e5SLen Brown 
62227d47356SLen Brown 	/* GFXMHz */
62327d47356SLen Brown 	if (do_gfx_mhz)
6243d109de2SLen Brown 		outp += sprintf(outp, "\t%d", p->gfx_mhz);
62527d47356SLen Brown 
6260b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
6270b2bb692SLen Brown 	if (do_skl_residency) {
6283d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
6293d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
6303d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
6313d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
6320b2bb692SLen Brown 	}
6330b2bb692SLen Brown 
634ee7e38e3SLen Brown 	if (do_pc2)
6353d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
636ee7e38e3SLen Brown 	if (do_pc3)
6373d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
638ee7e38e3SLen Brown 	if (do_pc6)
6393d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
640ee7e38e3SLen Brown 	if (do_pc7)
6413d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
642ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
6433d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
6443d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
6453d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
646ca58710fSKristen Carlson Accardi 	}
647889facbeSLen Brown 
648889facbeSLen Brown 	/*
649889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
650889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
651889facbeSLen Brown  	 */
652fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
6533d109de2SLen Brown 		fmt8 = "\t%.2f";
654fc04cc67SLen Brown 	else
655fc04cc67SLen Brown 		fmt8 = "%6.0f**";
656889facbeSLen Brown 
6575c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
658889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
659fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
660889facbeSLen Brown 		if (do_rapl & RAPL_CORES)
661fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
662889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
663fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
664889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
66540ee8e3bSAndrey Semin 			outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
666889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
667fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
668889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
669fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
670d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
6715c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
672fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6735c56be9aSDirk Brandewie 					p->energy_pkg * rapl_energy_units);
6745c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
675fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6765c56be9aSDirk Brandewie 					p->energy_cores * rapl_energy_units);
6775c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
678fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6795c56be9aSDirk Brandewie 					p->energy_gfx * rapl_energy_units);
6805c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
681fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
68240ee8e3bSAndrey Semin 					p->energy_dram * rapl_dram_energy_units);
6835c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
684fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
6855c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
686fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
6875c56be9aSDirk Brandewie 	}
688c98d5d94SLen Brown done:
689c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
690c98d5d94SLen Brown 
691c98d5d94SLen Brown 	return 0;
692103a8feaSLen Brown }
693103a8feaSLen Brown 
694b7d8c148SLen Brown void flush_output_stdout(void)
695103a8feaSLen Brown {
696b7d8c148SLen Brown 	FILE *filep;
697b7d8c148SLen Brown 
698b7d8c148SLen Brown 	if (outf == stderr)
699b7d8c148SLen Brown 		filep = stdout;
700b7d8c148SLen Brown 	else
701b7d8c148SLen Brown 		filep = outf;
702b7d8c148SLen Brown 
703b7d8c148SLen Brown 	fputs(output_buffer, filep);
704b7d8c148SLen Brown 	fflush(filep);
705b7d8c148SLen Brown 
706c98d5d94SLen Brown 	outp = output_buffer;
707c98d5d94SLen Brown }
708b7d8c148SLen Brown void flush_output_stderr(void)
709c98d5d94SLen Brown {
710b7d8c148SLen Brown 	fputs(output_buffer, outf);
711b7d8c148SLen Brown 	fflush(outf);
712c98d5d94SLen Brown 	outp = output_buffer;
713c98d5d94SLen Brown }
714c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
715c98d5d94SLen Brown {
716e23da037SLen Brown 	static int printed;
717103a8feaSLen Brown 
718e23da037SLen Brown 	if (!printed || !summary_only)
719103a8feaSLen Brown 		print_header();
720103a8feaSLen Brown 
721c98d5d94SLen Brown 	if (topo.num_cpus > 1)
722c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
723c98d5d94SLen Brown 			&average.packages);
724103a8feaSLen Brown 
725e23da037SLen Brown 	printed = 1;
726e23da037SLen Brown 
727e23da037SLen Brown 	if (summary_only)
728e23da037SLen Brown 		return;
729e23da037SLen Brown 
730c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
731103a8feaSLen Brown }
732103a8feaSLen Brown 
733889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
734889facbeSLen Brown 	if (new > old) {			\
735889facbeSLen Brown 		old = new - old;		\
736889facbeSLen Brown 	} else {				\
737889facbeSLen Brown 		old = 0x100000000 + new - old;	\
738889facbeSLen Brown 	}
739889facbeSLen Brown 
740ba3dec99SLen Brown int
741c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
742103a8feaSLen Brown {
7430b2bb692SLen Brown 
7440b2bb692SLen Brown 	if (do_skl_residency) {
7450b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
7460b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
7470b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
7480b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
7490b2bb692SLen Brown 	}
750c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
751ee7e38e3SLen Brown 	if (do_pc3)
752c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
753ee7e38e3SLen Brown 	if (do_pc6)
754c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
755ee7e38e3SLen Brown 	if (do_pc7)
756c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
757ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
758ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
759ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
760889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
761889facbeSLen Brown 
7629185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
7639185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
7649185e988SLen Brown 		old->gfx_rc6_ms = -1;
7659185e988SLen Brown 	else
766fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
7679185e988SLen Brown 
76827d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
76927d47356SLen Brown 
770889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
771889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
772889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
773889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
774889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
775889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
776ba3dec99SLen Brown 
777ba3dec99SLen Brown 	return 0;
778103a8feaSLen Brown }
779103a8feaSLen Brown 
780c98d5d94SLen Brown void
781c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
782c98d5d94SLen Brown {
783c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
784c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
785c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
786889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
787103a8feaSLen Brown }
788c98d5d94SLen Brown 
789c3ae331dSLen Brown /*
790c3ae331dSLen Brown  * old = new - old
791c3ae331dSLen Brown  */
792ba3dec99SLen Brown int
793c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
794c98d5d94SLen Brown 	struct core_data *core_delta)
795c98d5d94SLen Brown {
796c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
797c98d5d94SLen Brown 
798103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
799b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
800b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
801b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
802b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
803103a8feaSLen Brown 
804c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
805c98d5d94SLen Brown 
806a729617cSLen Brown 	if (has_aperf) {
807c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
808c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
809c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
810c98d5d94SLen Brown 		} else {
811ba3dec99SLen Brown 			return -1;
812103a8feaSLen Brown 		}
813a729617cSLen Brown 	}
814103a8feaSLen Brown 
815c98d5d94SLen Brown 
816144b44b1SLen Brown 	if (use_c1_residency_msr) {
817144b44b1SLen Brown 		/*
818144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
819144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
820144b44b1SLen Brown 		 */
821144b44b1SLen Brown 	} else {
822103a8feaSLen Brown 		/*
823c3ae331dSLen Brown 		 * As counter collection is not atomic,
824c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
825103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
826103a8feaSLen Brown 		 */
827c3ae331dSLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
828c98d5d94SLen Brown 			old->c1 = 0;
829c98d5d94SLen Brown 		else {
830c98d5d94SLen Brown 			/* normal case, derive c1 */
831c98d5d94SLen Brown 			old->c1 = old->tsc - old->mperf - core_delta->c3
832c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
833c98d5d94SLen Brown 		}
834144b44b1SLen Brown 	}
835c3ae331dSLen Brown 
836c98d5d94SLen Brown 	if (old->mperf == 0) {
837b7d8c148SLen Brown 		if (debug > 1)
838b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
839c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
840c98d5d94SLen Brown 	}
841103a8feaSLen Brown 
8428e180f3cSLen Brown 	old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
8438e180f3cSLen Brown 	old->extra_delta32 &= 0xFFFFFFFF;
8448e180f3cSLen Brown 
8458e180f3cSLen Brown 	old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
8468e180f3cSLen Brown 
847103a8feaSLen Brown 	/*
8488e180f3cSLen Brown 	 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
849103a8feaSLen Brown 	 */
8502f32edf1SLen Brown 	old->extra_msr32 = new->extra_msr32;
8512f32edf1SLen Brown 	old->extra_msr64 = new->extra_msr64;
8521ed51011SLen Brown 
853562a2d37SLen Brown 	if (do_irq)
854562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
855562a2d37SLen Brown 
8561ed51011SLen Brown 	if (do_smi)
8571ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
858ba3dec99SLen Brown 
859ba3dec99SLen Brown 	return 0;
860103a8feaSLen Brown }
861c98d5d94SLen Brown 
862c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
863c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
864c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
865c98d5d94SLen Brown {
866ba3dec99SLen Brown 	int retval = 0;
867ba3dec99SLen Brown 
868c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
869c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
870c98d5d94SLen Brown 		delta_core(c, c2);
871c98d5d94SLen Brown 
872c98d5d94SLen Brown 	/* always calculate thread delta */
873ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
874ba3dec99SLen Brown 	if (retval)
875ba3dec99SLen Brown 		return retval;
876c98d5d94SLen Brown 
877c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
878c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
879ba3dec99SLen Brown 		retval = delta_package(p, p2);
880c98d5d94SLen Brown 
881ba3dec99SLen Brown 	return retval;
882103a8feaSLen Brown }
883103a8feaSLen Brown 
884c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
885103a8feaSLen Brown {
886c98d5d94SLen Brown 	t->tsc = 0;
887c98d5d94SLen Brown 	t->aperf = 0;
888c98d5d94SLen Brown 	t->mperf = 0;
889c98d5d94SLen Brown 	t->c1 = 0;
890103a8feaSLen Brown 
8918e180f3cSLen Brown 	t->extra_delta32 = 0;
8928e180f3cSLen Brown 	t->extra_delta64 = 0;
8938e180f3cSLen Brown 
894562a2d37SLen Brown 	t->irq_count = 0;
895562a2d37SLen Brown 	t->smi_count = 0;
896562a2d37SLen Brown 
897c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
898c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
899c98d5d94SLen Brown 
900c98d5d94SLen Brown 	c->c3 = 0;
901c98d5d94SLen Brown 	c->c6 = 0;
902c98d5d94SLen Brown 	c->c7 = 0;
903889facbeSLen Brown 	c->core_temp_c = 0;
904c98d5d94SLen Brown 
9050b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
9060b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
9070b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
9080b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
9090b2bb692SLen Brown 
910c98d5d94SLen Brown 	p->pc2 = 0;
911ee7e38e3SLen Brown 	if (do_pc3)
912c98d5d94SLen Brown 		p->pc3 = 0;
913ee7e38e3SLen Brown 	if (do_pc6)
914c98d5d94SLen Brown 		p->pc6 = 0;
915ee7e38e3SLen Brown 	if (do_pc7)
916c98d5d94SLen Brown 		p->pc7 = 0;
917ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
918ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
919ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
920889facbeSLen Brown 
921889facbeSLen Brown 	p->energy_pkg = 0;
922889facbeSLen Brown 	p->energy_dram = 0;
923889facbeSLen Brown 	p->energy_cores = 0;
924889facbeSLen Brown 	p->energy_gfx = 0;
925889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
926889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
927889facbeSLen Brown 	p->pkg_temp_c = 0;
92827d47356SLen Brown 
929fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
93027d47356SLen Brown 	p->gfx_mhz = 0;
931103a8feaSLen Brown }
932c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
933c98d5d94SLen Brown 	struct pkg_data *p)
934103a8feaSLen Brown {
935c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
936c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
937c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
938c98d5d94SLen Brown 	average.threads.c1 += t->c1;
93915aaa346SLen Brown 
9408e180f3cSLen Brown 	average.threads.extra_delta32 += t->extra_delta32;
9418e180f3cSLen Brown 	average.threads.extra_delta64 += t->extra_delta64;
9428e180f3cSLen Brown 
943562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
944562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
945562a2d37SLen Brown 
946c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
947c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
948c98d5d94SLen Brown 		return 0;
949c98d5d94SLen Brown 
950c98d5d94SLen Brown 	average.cores.c3 += c->c3;
951c98d5d94SLen Brown 	average.cores.c6 += c->c6;
952c98d5d94SLen Brown 	average.cores.c7 += c->c7;
953c98d5d94SLen Brown 
954889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
955889facbeSLen Brown 
956c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
957c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
958c98d5d94SLen Brown 		return 0;
959c98d5d94SLen Brown 
9600b2bb692SLen Brown 	if (do_skl_residency) {
9610b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
9620b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
9630b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
9640b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
9650b2bb692SLen Brown 	}
9660b2bb692SLen Brown 
967c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
968ee7e38e3SLen Brown 	if (do_pc3)
969c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
970ee7e38e3SLen Brown 	if (do_pc6)
971c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
972ee7e38e3SLen Brown 	if (do_pc7)
973c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
974ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
975ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
976ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
977c98d5d94SLen Brown 
978889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
979889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
980889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
981889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
982889facbeSLen Brown 
983fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
98427d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
98527d47356SLen Brown 
986889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
987889facbeSLen Brown 
988889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
989889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
990c98d5d94SLen Brown 	return 0;
991c98d5d94SLen Brown }
992c98d5d94SLen Brown /*
993c98d5d94SLen Brown  * sum the counters for all cpus in the system
994c98d5d94SLen Brown  * compute the weighted average
995c98d5d94SLen Brown  */
996c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
997c98d5d94SLen Brown 	struct pkg_data *p)
998c98d5d94SLen Brown {
999c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1000c98d5d94SLen Brown 
1001c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1002c98d5d94SLen Brown 
1003c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1004c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1005c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1006c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1007c98d5d94SLen Brown 
10088e180f3cSLen Brown 	average.threads.extra_delta32 /= topo.num_cpus;
10098e180f3cSLen Brown 	average.threads.extra_delta32 &= 0xFFFFFFFF;
10108e180f3cSLen Brown 
10118e180f3cSLen Brown 	average.threads.extra_delta64 /= topo.num_cpus;
10128e180f3cSLen Brown 
1013c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1014c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1015c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
1016c98d5d94SLen Brown 
10170b2bb692SLen Brown 	if (do_skl_residency) {
10180b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
10190b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
10200b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
10210b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
10220b2bb692SLen Brown 	}
10230b2bb692SLen Brown 
1024c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
1025ee7e38e3SLen Brown 	if (do_pc3)
1026c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
1027ee7e38e3SLen Brown 	if (do_pc6)
1028c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
1029ee7e38e3SLen Brown 	if (do_pc7)
1030c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1031ca58710fSKristen Carlson Accardi 
1032ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1033ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1034ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1035c98d5d94SLen Brown }
1036c98d5d94SLen Brown 
1037c98d5d94SLen Brown static unsigned long long rdtsc(void)
1038c98d5d94SLen Brown {
1039c98d5d94SLen Brown 	unsigned int low, high;
1040c98d5d94SLen Brown 
1041c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1042c98d5d94SLen Brown 
1043c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1044c98d5d94SLen Brown }
1045c98d5d94SLen Brown 
1046c98d5d94SLen Brown /*
1047c98d5d94SLen Brown  * get_counters(...)
1048c98d5d94SLen Brown  * migrate to cpu
1049c98d5d94SLen Brown  * acquire and record local counters for that cpu
1050c98d5d94SLen Brown  */
1051c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1052c98d5d94SLen Brown {
1053c98d5d94SLen Brown 	int cpu = t->cpu_id;
1054889facbeSLen Brown 	unsigned long long msr;
10550102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1056c98d5d94SLen Brown 
1057e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1058b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
105915aaa346SLen Brown 		return -1;
1060e52966c0SLen Brown 	}
106115aaa346SLen Brown 
10620102b067SLen Brown retry:
1063c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
106415aaa346SLen Brown 
106515aaa346SLen Brown 	if (has_aperf) {
10660102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
10670102b067SLen Brown 
10680102b067SLen Brown 		/*
10690102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
10700102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
10710102b067SLen Brown 		 *
10720102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
10730102b067SLen Brown 		 * individual system call, so delays may occur
10740102b067SLen Brown 		 * between them.  If the time to read them
10750102b067SLen Brown 		 * varies by a large amount, we re-read them.
10760102b067SLen Brown 		 */
10770102b067SLen Brown 
10780102b067SLen Brown 		/*
10790102b067SLen Brown 		 * This initial dummy APERF read has been seen to
10800102b067SLen Brown 		 * reduce jitter in the subsequent reads.
10810102b067SLen Brown 		 */
10820102b067SLen Brown 
10839c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1084c98d5d94SLen Brown 			return -3;
10850102b067SLen Brown 
10860102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
10870102b067SLen Brown 
10880102b067SLen Brown 		tsc_before = t->tsc;
10890102b067SLen Brown 
10900102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
10910102b067SLen Brown 			return -3;
10920102b067SLen Brown 
10930102b067SLen Brown 		tsc_between = rdtsc();
10940102b067SLen Brown 
10959c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1096c98d5d94SLen Brown 			return -4;
10970102b067SLen Brown 
10980102b067SLen Brown 		tsc_after = rdtsc();
10990102b067SLen Brown 
11000102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
11010102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
11020102b067SLen Brown 
11030102b067SLen Brown 		/*
11040102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
11050102b067SLen Brown 		 * differ by more than 2x, then try again.
11060102b067SLen Brown 		 */
11070102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
11080102b067SLen Brown 			aperf_mperf_retry_count++;
11090102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
11100102b067SLen Brown 				goto retry;
11110102b067SLen Brown 			else
11120102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
11130102b067SLen Brown 					cpu, aperf_time, mperf_time);
11140102b067SLen Brown 		}
11150102b067SLen Brown 		aperf_mperf_retry_count = 0;
11160102b067SLen Brown 
1117b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1118b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
111988c3281fSLen Brown 	}
112088c3281fSLen Brown 
1121562a2d37SLen Brown 	if (do_irq)
1122562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
11231ed51011SLen Brown 	if (do_smi) {
11241ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
11251ed51011SLen Brown 			return -5;
11261ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
11271ed51011SLen Brown 	}
11288e180f3cSLen Brown 	if (extra_delta_offset32) {
1129889facbeSLen Brown 		if (get_msr(cpu, extra_delta_offset32, &msr))
11302f32edf1SLen Brown 			return -5;
1131889facbeSLen Brown 		t->extra_delta32 = msr & 0xFFFFFFFF;
11328e180f3cSLen Brown 	}
11338e180f3cSLen Brown 
11348e180f3cSLen Brown 	if (extra_delta_offset64)
11358e180f3cSLen Brown 		if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
11368e180f3cSLen Brown 			return -5;
11378e180f3cSLen Brown 
11388e180f3cSLen Brown 	if (extra_msr_offset32) {
1139889facbeSLen Brown 		if (get_msr(cpu, extra_msr_offset32, &msr))
11408e180f3cSLen Brown 			return -5;
1141889facbeSLen Brown 		t->extra_msr32 = msr & 0xFFFFFFFF;
11428e180f3cSLen Brown 	}
11432f32edf1SLen Brown 
11442f32edf1SLen Brown 	if (extra_msr_offset64)
11452f32edf1SLen Brown 		if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
1146c98d5d94SLen Brown 			return -5;
1147c98d5d94SLen Brown 
1148144b44b1SLen Brown 	if (use_c1_residency_msr) {
1149144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1150144b44b1SLen Brown 			return -6;
1151144b44b1SLen Brown 	}
1152144b44b1SLen Brown 
1153c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1154c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1155c98d5d94SLen Brown 		return 0;
1156c98d5d94SLen Brown 
1157fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
1158c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1159c98d5d94SLen Brown 			return -6;
1160144b44b1SLen Brown 	}
1161144b44b1SLen Brown 
1162fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_knl_cstates) {
1163c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1164c98d5d94SLen Brown 			return -7;
1165fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1166fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1167fb5d4327SDasaratharaman Chandramouli 			return -7;
1168103a8feaSLen Brown 	}
116915aaa346SLen Brown 
117015aaa346SLen Brown 	if (do_snb_cstates)
1171c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1172c98d5d94SLen Brown 			return -8;
1173c98d5d94SLen Brown 
1174889facbeSLen Brown 	if (do_dts) {
1175889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1176889facbeSLen Brown 			return -9;
1177889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1178889facbeSLen Brown 	}
1179889facbeSLen Brown 
1180889facbeSLen Brown 
1181c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1182c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1183c98d5d94SLen Brown 		return 0;
118415aaa346SLen Brown 
11850b2bb692SLen Brown 	if (do_skl_residency) {
11860b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
11870b2bb692SLen Brown 			return -10;
11880b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
11890b2bb692SLen Brown 			return -11;
11900b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
11910b2bb692SLen Brown 			return -12;
11920b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
11930b2bb692SLen Brown 			return -13;
11940b2bb692SLen Brown 	}
1195ee7e38e3SLen Brown 	if (do_pc3)
1196c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1197c98d5d94SLen Brown 			return -9;
1198ee7e38e3SLen Brown 	if (do_pc6)
1199c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1200c98d5d94SLen Brown 			return -10;
1201ee7e38e3SLen Brown 	if (do_pc2)
1202c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1203c98d5d94SLen Brown 			return -11;
1204ee7e38e3SLen Brown 	if (do_pc7)
1205c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1206c98d5d94SLen Brown 			return -12;
1207ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
1208ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1209ca58710fSKristen Carlson Accardi 			return -13;
1210ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1211ca58710fSKristen Carlson Accardi 			return -13;
1212ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1213ca58710fSKristen Carlson Accardi 			return -13;
1214ca58710fSKristen Carlson Accardi 	}
1215889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1216889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1217889facbeSLen Brown 			return -13;
1218889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1219889facbeSLen Brown 	}
1220889facbeSLen Brown 	if (do_rapl & RAPL_CORES) {
1221889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1222889facbeSLen Brown 			return -14;
1223889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1224889facbeSLen Brown 	}
1225889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1226889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1227889facbeSLen Brown 			return -15;
1228889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1229889facbeSLen Brown 	}
1230889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1231889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1232889facbeSLen Brown 			return -16;
1233889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1234889facbeSLen Brown 	}
1235889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1236889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1237889facbeSLen Brown 			return -16;
1238889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1239889facbeSLen Brown 	}
1240889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1241889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1242889facbeSLen Brown 			return -16;
1243889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1244889facbeSLen Brown 	}
1245889facbeSLen Brown 	if (do_ptm) {
1246889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1247889facbeSLen Brown 			return -17;
1248889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1249889facbeSLen Brown 	}
1250fdf676e5SLen Brown 
1251fdf676e5SLen Brown 	if (do_gfx_rc6_ms)
1252fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1253fdf676e5SLen Brown 
125427d47356SLen Brown 	if (do_gfx_mhz)
125527d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
125627d47356SLen Brown 
125715aaa346SLen Brown 	return 0;
1258103a8feaSLen Brown }
1259103a8feaSLen Brown 
1260ee7e38e3SLen Brown /*
1261ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1262ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1263ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1264ee7e38e3SLen Brown  */
1265ee7e38e3SLen Brown 
1266ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1267ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1268ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1269ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1270ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1271ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1272ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1273ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1274ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1275ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1276ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1277ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
12780b2bb692SLen Brown #define PCL__8 12 /* PC8 */
12790b2bb692SLen Brown #define PCL__9 13 /* PC9 */
12800b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1281ee7e38e3SLen Brown 
1282ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1283ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
12840b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1285ee7e38e3SLen Brown 
1286e9257f5fSLen Brown int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1287e9257f5fSLen Brown int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1288e9257f5fSLen Brown int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1289e9257f5fSLen Brown int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1290e9257f5fSLen Brown int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1291e9257f5fSLen Brown int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1292e4085d54SLen Brown int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1293ee7e38e3SLen Brown 
1294a2b7b749SLen Brown 
1295a2b7b749SLen Brown static void
1296a2b7b749SLen Brown calculate_tsc_tweak()
1297a2b7b749SLen Brown {
1298a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1299a2b7b749SLen Brown }
1300a2b7b749SLen Brown 
1301fcd17211SLen Brown static void
1302fcd17211SLen Brown dump_nhm_platform_info(void)
1303103a8feaSLen Brown {
1304103a8feaSLen Brown 	unsigned long long msr;
1305103a8feaSLen Brown 	unsigned int ratio;
1306103a8feaSLen Brown 
1307ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1308103a8feaSLen Brown 
1309b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
13106574a5d5SLen Brown 
1311103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1312b7d8c148SLen Brown 	fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1313103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1314103a8feaSLen Brown 
1315103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1316b7d8c148SLen Brown 	fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n",
1317103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1318103a8feaSLen Brown 
13197ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1320b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1321bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
132267920418SLen Brown 
1323fcd17211SLen Brown 	return;
1324fcd17211SLen Brown }
1325fcd17211SLen Brown 
1326fcd17211SLen Brown static void
1327fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1328fcd17211SLen Brown {
1329fcd17211SLen Brown 	unsigned long long msr;
1330fcd17211SLen Brown 	unsigned int ratio;
1331fcd17211SLen Brown 
13327ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1333fcd17211SLen Brown 
1334b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1335fcd17211SLen Brown 
1336fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1337fcd17211SLen Brown 	if (ratio)
1338b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1339fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1340fcd17211SLen Brown 
1341fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1342fcd17211SLen Brown 	if (ratio)
1343b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1344fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1345fcd17211SLen Brown 	return;
1346fcd17211SLen Brown }
1347fcd17211SLen Brown 
1348fcd17211SLen Brown static void
1349fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1350fcd17211SLen Brown {
1351fcd17211SLen Brown 	unsigned long long msr;
1352fcd17211SLen Brown 	unsigned int ratio;
13536574a5d5SLen Brown 
13547ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
13556574a5d5SLen Brown 
1356b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
13576574a5d5SLen Brown 
13586574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
13596574a5d5SLen Brown 	if (ratio)
1360b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
13616574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
13626574a5d5SLen Brown 
13636574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
13646574a5d5SLen Brown 	if (ratio)
1365b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
13666574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
13676574a5d5SLen Brown 
13686574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
13696574a5d5SLen Brown 	if (ratio)
1370b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
13716574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
13726574a5d5SLen Brown 
13736574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
13746574a5d5SLen Brown 	if (ratio)
1375b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
13766574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
13776574a5d5SLen Brown 
13786574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
13796574a5d5SLen Brown 	if (ratio)
1380b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
13816574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
13826574a5d5SLen Brown 
13836574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
13846574a5d5SLen Brown 	if (ratio)
1385b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
13866574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
13876574a5d5SLen Brown 
13886574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
13896574a5d5SLen Brown 	if (ratio)
1390b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
13916574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
13926574a5d5SLen Brown 
13936574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
13946574a5d5SLen Brown 	if (ratio)
1395b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
13966574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1397103a8feaSLen Brown 	return;
1398fcd17211SLen Brown }
1399fcd17211SLen Brown 
1400fcd17211SLen Brown static void
1401fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void)
1402fcd17211SLen Brown {
1403fcd17211SLen Brown 	unsigned long long msr;
1404fcd17211SLen Brown 	unsigned int ratio;
1405103a8feaSLen Brown 
14067ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1407103a8feaSLen Brown 
1408b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
14096574a5d5SLen Brown 
14106574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
14116574a5d5SLen Brown 	if (ratio)
1412b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
14136574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
14146574a5d5SLen Brown 
14156574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
14166574a5d5SLen Brown 	if (ratio)
1417b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
14186574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
14196574a5d5SLen Brown 
14206574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
14216574a5d5SLen Brown 	if (ratio)
1422b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
14236574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
14246574a5d5SLen Brown 
14256574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
14266574a5d5SLen Brown 	if (ratio)
1427b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
14286574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
14296574a5d5SLen Brown 
1430103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
1431103a8feaSLen Brown 	if (ratio)
1432b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1433103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1434103a8feaSLen Brown 
1435103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
1436103a8feaSLen Brown 	if (ratio)
1437b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1438103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1439103a8feaSLen Brown 
1440103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1441103a8feaSLen Brown 	if (ratio)
1442b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1443103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1444103a8feaSLen Brown 
1445103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
1446103a8feaSLen Brown 	if (ratio)
1447b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1448103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1449fcd17211SLen Brown 	return;
1450fcd17211SLen Brown }
14513a9a941dSLen Brown 
1452fcd17211SLen Brown static void
1453fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
1454fb5d4327SDasaratharaman Chandramouli {
1455cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
1456cbf97abaSHubert Chrzaniuk 
1457fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
1458cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
1459cbf97abaSHubert Chrzaniuk 	int i, b_nr;
1460cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
1461cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
1462fb5d4327SDasaratharaman Chandramouli 
1463ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1464fb5d4327SDasaratharaman Chandramouli 
1465b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
1466bfae2052SLen Brown 		base_cpu, msr);
1467fb5d4327SDasaratharaman Chandramouli 
1468fb5d4327SDasaratharaman Chandramouli 	/**
1469fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
1470cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
1471cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
1472fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
1473fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
1474fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
1475fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
1476fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
1477fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
1478fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
1479fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
1480fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
1481fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
1482fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
1483fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
1484fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
1485fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
1486fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
1487fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
1488fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
1489fb5d4327SDasaratharaman Chandramouli 	 */
1490fb5d4327SDasaratharaman Chandramouli 
1491cbf97abaSHubert Chrzaniuk 	b_nr = 0;
1492cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
1493cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
1494cbf97abaSHubert Chrzaniuk 
1495cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
1496fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
1497cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
1498fb5d4327SDasaratharaman Chandramouli 
1499cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
1500cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1501cbf97abaSHubert Chrzaniuk 		b_nr++;
1502fb5d4327SDasaratharaman Chandramouli 	}
1503cbf97abaSHubert Chrzaniuk 
1504cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
1505cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
1506b7d8c148SLen Brown 			fprintf(outf,
1507fb5d4327SDasaratharaman Chandramouli 				"%d * %.0f = %.0f MHz max turbo %d active cores\n",
1508cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
1509fb5d4327SDasaratharaman Chandramouli }
1510fb5d4327SDasaratharaman Chandramouli 
1511fb5d4327SDasaratharaman Chandramouli static void
1512fcd17211SLen Brown dump_nhm_cst_cfg(void)
1513fcd17211SLen Brown {
1514fcd17211SLen Brown 	unsigned long long msr;
1515fcd17211SLen Brown 
15167ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1517fcd17211SLen Brown 
1518fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
1519fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
1520fcd17211SLen Brown 
1521b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
1522fcd17211SLen Brown 
1523b7d8c148SLen Brown 	fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1524fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1525fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1526fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1527fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1528fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
15296c34f160SLen Brown 		(unsigned int)msr & 0xF,
1530fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
1531fcd17211SLen Brown 	return;
1532103a8feaSLen Brown }
1533103a8feaSLen Brown 
15346fb3143bSLen Brown static void
15356fb3143bSLen Brown dump_config_tdp(void)
15366fb3143bSLen Brown {
15376fb3143bSLen Brown 	unsigned long long msr;
15386fb3143bSLen Brown 
15396fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1540b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1541685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
15426fb3143bSLen Brown 
15436fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1544b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
15456fb3143bSLen Brown 	if (msr) {
1546685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1547685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1548685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1549685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
15506fb3143bSLen Brown 	}
1551b7d8c148SLen Brown 	fprintf(outf, ")\n");
15526fb3143bSLen Brown 
15536fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1554b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
15556fb3143bSLen Brown 	if (msr) {
1556685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1557685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1558685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1559685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
15606fb3143bSLen Brown 	}
1561b7d8c148SLen Brown 	fprintf(outf, ")\n");
15626fb3143bSLen Brown 
15636fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1564b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
15656fb3143bSLen Brown 	if ((msr) & 0x3)
1566b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1567b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1568b7d8c148SLen Brown 	fprintf(outf, ")\n");
15696fb3143bSLen Brown 
15706fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1571b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
1572685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
1573b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1574b7d8c148SLen Brown 	fprintf(outf, ")\n");
15756fb3143bSLen Brown }
15765a63426eSLen Brown 
15775a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
15785a63426eSLen Brown 
15795a63426eSLen Brown void print_irtl(void)
15805a63426eSLen Brown {
15815a63426eSLen Brown 	unsigned long long msr;
15825a63426eSLen Brown 
15835a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
15845a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
15855a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
15865a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
15875a63426eSLen Brown 
15885a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
15895a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
15905a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
15915a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
15925a63426eSLen Brown 
15935a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
15945a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
15955a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
15965a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
15975a63426eSLen Brown 
15985a63426eSLen Brown 	if (!do_irtl_hsw)
15995a63426eSLen Brown 		return;
16005a63426eSLen Brown 
16015a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
16025a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
16035a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
16045a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
16055a63426eSLen Brown 
16065a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
16075a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
16085a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
16095a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
16105a63426eSLen Brown 
16115a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
16125a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
16135a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
16145a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
16155a63426eSLen Brown 
16165a63426eSLen Brown }
161736229897SLen Brown void free_fd_percpu(void)
161836229897SLen Brown {
161936229897SLen Brown 	int i;
162036229897SLen Brown 
1621*01a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
162236229897SLen Brown 		if (fd_percpu[i] != 0)
162336229897SLen Brown 			close(fd_percpu[i]);
162436229897SLen Brown 	}
162536229897SLen Brown 
162636229897SLen Brown 	free(fd_percpu);
16276fb3143bSLen Brown }
16286fb3143bSLen Brown 
1629c98d5d94SLen Brown void free_all_buffers(void)
1630103a8feaSLen Brown {
1631c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
1632c98d5d94SLen Brown 	cpu_present_set = NULL;
163336229897SLen Brown 	cpu_present_setsize = 0;
1634103a8feaSLen Brown 
1635c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
1636c98d5d94SLen Brown 	cpu_affinity_set = NULL;
1637c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
1638103a8feaSLen Brown 
1639c98d5d94SLen Brown 	free(thread_even);
1640c98d5d94SLen Brown 	free(core_even);
1641c98d5d94SLen Brown 	free(package_even);
1642103a8feaSLen Brown 
1643c98d5d94SLen Brown 	thread_even = NULL;
1644c98d5d94SLen Brown 	core_even = NULL;
1645c98d5d94SLen Brown 	package_even = NULL;
1646103a8feaSLen Brown 
1647c98d5d94SLen Brown 	free(thread_odd);
1648c98d5d94SLen Brown 	free(core_odd);
1649c98d5d94SLen Brown 	free(package_odd);
1650103a8feaSLen Brown 
1651c98d5d94SLen Brown 	thread_odd = NULL;
1652c98d5d94SLen Brown 	core_odd = NULL;
1653c98d5d94SLen Brown 	package_odd = NULL;
1654103a8feaSLen Brown 
1655c98d5d94SLen Brown 	free(output_buffer);
1656c98d5d94SLen Brown 	output_buffer = NULL;
1657c98d5d94SLen Brown 	outp = NULL;
165836229897SLen Brown 
165936229897SLen Brown 	free_fd_percpu();
1660562a2d37SLen Brown 
1661562a2d37SLen Brown 	free(irq_column_2_cpu);
1662562a2d37SLen Brown 	free(irqs_per_cpu);
1663103a8feaSLen Brown }
1664103a8feaSLen Brown 
1665103a8feaSLen Brown /*
166657a42a34SJosh Triplett  * Open a file, and exit on failure
166757a42a34SJosh Triplett  */
166857a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode)
166957a42a34SJosh Triplett {
1670b7d8c148SLen Brown 	FILE *filep = fopen(path, mode);
1671b2c95d90SJosh Triplett 	if (!filep)
1672b2c95d90SJosh Triplett 		err(1, "%s: open failed", path);
167357a42a34SJosh Triplett 	return filep;
167457a42a34SJosh Triplett }
167557a42a34SJosh Triplett 
167657a42a34SJosh Triplett /*
167795aebc44SJosh Triplett  * Parse a file containing a single int.
167895aebc44SJosh Triplett  */
167995aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
168095aebc44SJosh Triplett {
168195aebc44SJosh Triplett 	va_list args;
168295aebc44SJosh Triplett 	char path[PATH_MAX];
168395aebc44SJosh Triplett 	FILE *filep;
168495aebc44SJosh Triplett 	int value;
168595aebc44SJosh Triplett 
168695aebc44SJosh Triplett 	va_start(args, fmt);
168795aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
168895aebc44SJosh Triplett 	va_end(args);
168957a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1690b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
1691b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
169295aebc44SJosh Triplett 	fclose(filep);
169395aebc44SJosh Triplett 	return value;
169495aebc44SJosh Triplett }
169595aebc44SJosh Triplett 
169695aebc44SJosh Triplett /*
1697e275b388SDasaratharaman Chandramouli  * get_cpu_position_in_core(cpu)
1698e275b388SDasaratharaman Chandramouli  * return the position of the CPU among its HT siblings in the core
1699e275b388SDasaratharaman Chandramouli  * return -1 if the sibling is not in list
1700103a8feaSLen Brown  */
1701e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu)
1702103a8feaSLen Brown {
1703e275b388SDasaratharaman Chandramouli 	char path[64];
1704e275b388SDasaratharaman Chandramouli 	FILE *filep;
1705e275b388SDasaratharaman Chandramouli 	int this_cpu;
1706e275b388SDasaratharaman Chandramouli 	char character;
1707e275b388SDasaratharaman Chandramouli 	int i;
1708e275b388SDasaratharaman Chandramouli 
1709e275b388SDasaratharaman Chandramouli 	sprintf(path,
1710e275b388SDasaratharaman Chandramouli 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1711e275b388SDasaratharaman Chandramouli 		cpu);
1712e275b388SDasaratharaman Chandramouli 	filep = fopen(path, "r");
1713e275b388SDasaratharaman Chandramouli 	if (filep == NULL) {
1714e275b388SDasaratharaman Chandramouli 		perror(path);
1715e275b388SDasaratharaman Chandramouli 		exit(1);
1716e275b388SDasaratharaman Chandramouli 	}
1717e275b388SDasaratharaman Chandramouli 
1718e275b388SDasaratharaman Chandramouli 	for (i = 0; i < topo.num_threads_per_core; i++) {
1719e275b388SDasaratharaman Chandramouli 		fscanf(filep, "%d", &this_cpu);
1720e275b388SDasaratharaman Chandramouli 		if (this_cpu == cpu) {
1721e275b388SDasaratharaman Chandramouli 			fclose(filep);
1722e275b388SDasaratharaman Chandramouli 			return i;
1723e275b388SDasaratharaman Chandramouli 		}
1724e275b388SDasaratharaman Chandramouli 
1725e275b388SDasaratharaman Chandramouli 		/* Account for no separator after last thread*/
1726e275b388SDasaratharaman Chandramouli 		if (i != (topo.num_threads_per_core - 1))
1727e275b388SDasaratharaman Chandramouli 			fscanf(filep, "%c", &character);
1728e275b388SDasaratharaman Chandramouli 	}
1729e275b388SDasaratharaman Chandramouli 
1730e275b388SDasaratharaman Chandramouli 	fclose(filep);
1731e275b388SDasaratharaman Chandramouli 	return -1;
1732c98d5d94SLen Brown }
1733103a8feaSLen Brown 
1734c98d5d94SLen Brown /*
1735c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
1736c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
1737c98d5d94SLen Brown  */
1738c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
1739c98d5d94SLen Brown {
174095aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1741103a8feaSLen Brown }
1742103a8feaSLen Brown 
1743103a8feaSLen Brown int get_physical_package_id(int cpu)
1744103a8feaSLen Brown {
174595aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1746103a8feaSLen Brown }
1747103a8feaSLen Brown 
1748103a8feaSLen Brown int get_core_id(int cpu)
1749103a8feaSLen Brown {
175095aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1751103a8feaSLen Brown }
1752103a8feaSLen Brown 
1753c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
1754c98d5d94SLen Brown {
1755c98d5d94SLen Brown 	char path[80];
1756c98d5d94SLen Brown 	FILE *filep;
1757e275b388SDasaratharaman Chandramouli 	int sib1;
1758e275b388SDasaratharaman Chandramouli 	int matches = 0;
1759c98d5d94SLen Brown 	char character;
1760e275b388SDasaratharaman Chandramouli 	char str[100];
1761e275b388SDasaratharaman Chandramouli 	char *ch;
1762c98d5d94SLen Brown 
1763c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
176457a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1765e275b388SDasaratharaman Chandramouli 
1766103a8feaSLen Brown 	/*
1767c98d5d94SLen Brown 	 * file format:
1768e275b388SDasaratharaman Chandramouli 	 * A ',' separated or '-' separated set of numbers
1769e275b388SDasaratharaman Chandramouli 	 * (eg 1-2 or 1,3,4,5)
1770c98d5d94SLen Brown 	 */
1771e275b388SDasaratharaman Chandramouli 	fscanf(filep, "%d%c\n", &sib1, &character);
1772e275b388SDasaratharaman Chandramouli 	fseek(filep, 0, SEEK_SET);
1773e275b388SDasaratharaman Chandramouli 	fgets(str, 100, filep);
1774e275b388SDasaratharaman Chandramouli 	ch = strchr(str, character);
1775e275b388SDasaratharaman Chandramouli 	while (ch != NULL) {
1776e275b388SDasaratharaman Chandramouli 		matches++;
1777e275b388SDasaratharaman Chandramouli 		ch = strchr(ch+1, character);
1778e275b388SDasaratharaman Chandramouli 	}
1779c98d5d94SLen Brown 
1780c98d5d94SLen Brown 	fclose(filep);
1781e275b388SDasaratharaman Chandramouli 	return matches+1;
1782c98d5d94SLen Brown }
1783c98d5d94SLen Brown 
1784c98d5d94SLen Brown /*
1785c98d5d94SLen Brown  * run func(thread, core, package) in topology order
1786c98d5d94SLen Brown  * skip non-present cpus
1787103a8feaSLen Brown  */
1788103a8feaSLen Brown 
1789c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1790c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
1791c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
1792c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
1793c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
1794c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
1795c98d5d94SLen Brown {
1796c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
1797c98d5d94SLen Brown 
1798c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1799c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1800c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
1801c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
1802c98d5d94SLen Brown 				struct thread_data *t, *t2;
1803c98d5d94SLen Brown 				struct core_data *c, *c2;
1804c98d5d94SLen Brown 				struct pkg_data *p, *p2;
1805c98d5d94SLen Brown 
1806c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1807c98d5d94SLen Brown 
1808c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
1809c98d5d94SLen Brown 					continue;
1810c98d5d94SLen Brown 
1811c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1812c98d5d94SLen Brown 
1813c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
1814c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
1815c98d5d94SLen Brown 
1816c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
1817c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
1818c98d5d94SLen Brown 
1819c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
1820c98d5d94SLen Brown 				if (retval)
1821c98d5d94SLen Brown 					return retval;
1822c98d5d94SLen Brown 			}
1823c98d5d94SLen Brown 		}
1824c98d5d94SLen Brown 	}
1825c98d5d94SLen Brown 	return 0;
1826c98d5d94SLen Brown }
1827c98d5d94SLen Brown 
1828c98d5d94SLen Brown /*
1829c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
1830c98d5d94SLen Brown  * return max_cpu number
1831c98d5d94SLen Brown  */
1832c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
1833103a8feaSLen Brown {
1834103a8feaSLen Brown 	FILE *fp;
1835c98d5d94SLen Brown 	int cpu_num;
1836103a8feaSLen Brown 	int retval;
1837103a8feaSLen Brown 
183857a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
1839103a8feaSLen Brown 
1840103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1841b2c95d90SJosh Triplett 	if (retval != 0)
1842b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
1843103a8feaSLen Brown 
1844c98d5d94SLen Brown 	while (1) {
1845c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1846103a8feaSLen Brown 		if (retval != 1)
1847103a8feaSLen Brown 			break;
1848103a8feaSLen Brown 
1849c98d5d94SLen Brown 		retval = func(cpu_num);
1850c98d5d94SLen Brown 		if (retval) {
1851c98d5d94SLen Brown 			fclose(fp);
1852c98d5d94SLen Brown 			return(retval);
1853c98d5d94SLen Brown 		}
1854103a8feaSLen Brown 	}
1855103a8feaSLen Brown 	fclose(fp);
1856c98d5d94SLen Brown 	return 0;
1857103a8feaSLen Brown }
1858103a8feaSLen Brown 
1859103a8feaSLen Brown void re_initialize(void)
1860103a8feaSLen Brown {
1861c98d5d94SLen Brown 	free_all_buffers();
1862c98d5d94SLen Brown 	setup_all_buffers();
1863c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1864103a8feaSLen Brown }
1865103a8feaSLen Brown 
1866c98d5d94SLen Brown 
1867103a8feaSLen Brown /*
1868c98d5d94SLen Brown  * count_cpus()
1869c98d5d94SLen Brown  * remember the last one seen, it will be the max
1870103a8feaSLen Brown  */
1871c98d5d94SLen Brown int count_cpus(int cpu)
1872103a8feaSLen Brown {
1873c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
1874c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
1875103a8feaSLen Brown 
1876c98d5d94SLen Brown 	topo.num_cpus += 1;
1877c98d5d94SLen Brown 	return 0;
1878103a8feaSLen Brown }
1879c98d5d94SLen Brown int mark_cpu_present(int cpu)
1880c98d5d94SLen Brown {
1881c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
188215aaa346SLen Brown 	return 0;
1883103a8feaSLen Brown }
1884103a8feaSLen Brown 
1885562a2d37SLen Brown /*
1886562a2d37SLen Brown  * snapshot_proc_interrupts()
1887562a2d37SLen Brown  *
1888562a2d37SLen Brown  * read and record summary of /proc/interrupts
1889562a2d37SLen Brown  *
1890562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
1891562a2d37SLen Brown  */
1892562a2d37SLen Brown int snapshot_proc_interrupts(void)
1893562a2d37SLen Brown {
1894562a2d37SLen Brown 	static FILE *fp;
1895562a2d37SLen Brown 	int column, retval;
1896562a2d37SLen Brown 
1897562a2d37SLen Brown 	if (fp == NULL)
1898562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
1899562a2d37SLen Brown 	else
1900562a2d37SLen Brown 		rewind(fp);
1901562a2d37SLen Brown 
1902562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
1903562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
1904562a2d37SLen Brown 		int cpu_number;
1905562a2d37SLen Brown 
1906562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
1907562a2d37SLen Brown 		if (retval != 1)
1908562a2d37SLen Brown 			break;
1909562a2d37SLen Brown 
1910562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
1911562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
1912562a2d37SLen Brown 			return 1;
1913562a2d37SLen Brown 		}
1914562a2d37SLen Brown 
1915562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
1916562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
1917562a2d37SLen Brown 	}
1918562a2d37SLen Brown 
1919562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
1920562a2d37SLen Brown 	while (1) {
1921562a2d37SLen Brown 		int column;
1922562a2d37SLen Brown 		char buf[64];
1923562a2d37SLen Brown 
1924562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
1925562a2d37SLen Brown 		if (retval != 1)
1926562a2d37SLen Brown 			break;
1927562a2d37SLen Brown 
1928562a2d37SLen Brown 		/* read the count per cpu */
1929562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
1930562a2d37SLen Brown 
1931562a2d37SLen Brown 			int cpu_number, irq_count;
1932562a2d37SLen Brown 
1933562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
1934562a2d37SLen Brown 			if (retval != 1)
1935562a2d37SLen Brown 				break;
1936562a2d37SLen Brown 
1937562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
1938562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
1939562a2d37SLen Brown 
1940562a2d37SLen Brown 		}
1941562a2d37SLen Brown 
1942562a2d37SLen Brown 		while (getc(fp) != '\n')
1943562a2d37SLen Brown 			;	/* flush interrupt description */
1944562a2d37SLen Brown 
1945562a2d37SLen Brown 	}
1946562a2d37SLen Brown 	return 0;
1947562a2d37SLen Brown }
194827d47356SLen Brown /*
1949fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
1950fdf676e5SLen Brown  *
1951fdf676e5SLen Brown  * record snapshot of
1952fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
1953fdf676e5SLen Brown  *
1954fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
1955fdf676e5SLen Brown  */
1956fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
1957fdf676e5SLen Brown {
1958fdf676e5SLen Brown 	FILE *fp;
1959fdf676e5SLen Brown 	int retval;
1960fdf676e5SLen Brown 
1961fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
1962fdf676e5SLen Brown 
1963fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
1964fdf676e5SLen Brown 	if (retval != 1)
1965fdf676e5SLen Brown 		err(1, "GFX rc6");
1966fdf676e5SLen Brown 
1967fdf676e5SLen Brown 	fclose(fp);
1968fdf676e5SLen Brown 
1969fdf676e5SLen Brown 	return 0;
1970fdf676e5SLen Brown }
1971fdf676e5SLen Brown /*
197227d47356SLen Brown  * snapshot_gfx_mhz()
197327d47356SLen Brown  *
197427d47356SLen Brown  * record snapshot of
197527d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
197627d47356SLen Brown  *
197727d47356SLen Brown  * return 1 if config change requires a restart, else return 0
197827d47356SLen Brown  */
197927d47356SLen Brown int snapshot_gfx_mhz(void)
198027d47356SLen Brown {
198127d47356SLen Brown 	static FILE *fp;
198227d47356SLen Brown 	int retval;
198327d47356SLen Brown 
198427d47356SLen Brown 	if (fp == NULL)
198527d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
198627d47356SLen Brown 	else
198727d47356SLen Brown 		rewind(fp);
198827d47356SLen Brown 
198927d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
199027d47356SLen Brown 	if (retval != 1)
199127d47356SLen Brown 		err(1, "GFX MHz");
199227d47356SLen Brown 
199327d47356SLen Brown 	return 0;
199427d47356SLen Brown }
1995562a2d37SLen Brown 
1996562a2d37SLen Brown /*
1997562a2d37SLen Brown  * snapshot /proc and /sys files
1998562a2d37SLen Brown  *
1999562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2000562a2d37SLen Brown  */
2001562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2002562a2d37SLen Brown {
2003562a2d37SLen Brown 	if (snapshot_proc_interrupts())
2004562a2d37SLen Brown 		return 1;
2005562a2d37SLen Brown 
2006fdf676e5SLen Brown 	if (do_gfx_rc6_ms)
2007fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2008fdf676e5SLen Brown 
200927d47356SLen Brown 	if (do_gfx_mhz)
201027d47356SLen Brown 		snapshot_gfx_mhz();
201127d47356SLen Brown 
2012562a2d37SLen Brown 	return 0;
2013562a2d37SLen Brown }
2014562a2d37SLen Brown 
2015103a8feaSLen Brown void turbostat_loop()
2016103a8feaSLen Brown {
2017c98d5d94SLen Brown 	int retval;
2018e52966c0SLen Brown 	int restarted = 0;
2019c98d5d94SLen Brown 
2020103a8feaSLen Brown restart:
2021e52966c0SLen Brown 	restarted++;
2022e52966c0SLen Brown 
2023562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2024c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2025d91bb17cSLen Brown 	if (retval < -1) {
2026d91bb17cSLen Brown 		exit(retval);
2027d91bb17cSLen Brown 	} else if (retval == -1) {
2028e52966c0SLen Brown 		if (restarted > 1) {
2029e52966c0SLen Brown 			exit(retval);
2030e52966c0SLen Brown 		}
2031c98d5d94SLen Brown 		re_initialize();
2032c98d5d94SLen Brown 		goto restart;
2033c98d5d94SLen Brown 	}
2034e52966c0SLen Brown 	restarted = 0;
2035103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2036103a8feaSLen Brown 
2037103a8feaSLen Brown 	while (1) {
2038c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2039103a8feaSLen Brown 			re_initialize();
2040103a8feaSLen Brown 			goto restart;
2041103a8feaSLen Brown 		}
20422a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2043562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2044562a2d37SLen Brown 			goto restart;
2045c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
2046d91bb17cSLen Brown 		if (retval < -1) {
2047d91bb17cSLen Brown 			exit(retval);
2048d91bb17cSLen Brown 		} else if (retval == -1) {
204915aaa346SLen Brown 			re_initialize();
205015aaa346SLen Brown 			goto restart;
205115aaa346SLen Brown 		}
2052103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
2053103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
2054ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2055ba3dec99SLen Brown 			re_initialize();
2056ba3dec99SLen Brown 			goto restart;
2057ba3dec99SLen Brown 		}
2058c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
2059c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
2060b7d8c148SLen Brown 		flush_output_stdout();
20612a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2062562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2063562a2d37SLen Brown 			goto restart;
2064c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2065d91bb17cSLen Brown 		if (retval < -1) {
2066d91bb17cSLen Brown 			exit(retval);
2067d91bb17cSLen Brown 		} else if (retval == -1) {
2068103a8feaSLen Brown 			re_initialize();
2069103a8feaSLen Brown 			goto restart;
2070103a8feaSLen Brown 		}
2071103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
2072103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
2073ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2074ba3dec99SLen Brown 			re_initialize();
2075ba3dec99SLen Brown 			goto restart;
2076ba3dec99SLen Brown 		}
2077c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
2078c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
2079b7d8c148SLen Brown 		flush_output_stdout();
2080103a8feaSLen Brown 	}
2081103a8feaSLen Brown }
2082103a8feaSLen Brown 
2083103a8feaSLen Brown void check_dev_msr()
2084103a8feaSLen Brown {
2085103a8feaSLen Brown 	struct stat sb;
20867ce7d5deSPrarit Bhargava 	char pathname[32];
2087103a8feaSLen Brown 
20887ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
20897ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
2090a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2091d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2092103a8feaSLen Brown }
2093103a8feaSLen Brown 
209498481e79SLen Brown void check_permissions()
2095103a8feaSLen Brown {
209698481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
209798481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
209898481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
209998481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
210098481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
210198481e79SLen Brown 	int do_exit = 0;
21027ce7d5deSPrarit Bhargava 	char pathname[32];
210398481e79SLen Brown 
210498481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
210598481e79SLen Brown 	cap_header->pid = getpid();
210698481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
210798481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
210898481e79SLen Brown 		err(-6, "capget(2) failed");
210998481e79SLen Brown 
211098481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
211198481e79SLen Brown 		do_exit++;
211298481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
211398481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
211498481e79SLen Brown 	}
211598481e79SLen Brown 
211698481e79SLen Brown 	/* test file permissions */
21177ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
21187ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
211998481e79SLen Brown 		do_exit++;
212098481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
212198481e79SLen Brown 	}
212298481e79SLen Brown 
212398481e79SLen Brown 	/* if all else fails, thell them to be root */
212498481e79SLen Brown 	if (do_exit)
2125b2c95d90SJosh Triplett 		if (getuid() != 0)
2126d7899447SLen Brown 			warnx("... or simply run as root");
212798481e79SLen Brown 
212898481e79SLen Brown 	if (do_exit)
212998481e79SLen Brown 		exit(-6);
2130103a8feaSLen Brown }
2131103a8feaSLen Brown 
2132d7899447SLen Brown /*
2133d7899447SLen Brown  * NHM adds support for additional MSRs:
2134d7899447SLen Brown  *
2135d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
2136d7899447SLen Brown  *
2137ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
2138d7899447SLen Brown  * MSR_NHM_SNB_PKG_CST_CFG_CTL     0x000000e2
2139d7899447SLen Brown  *
2140d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
2141d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
2142d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
2143d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
2144d7899447SLen Brown  *
2145ee7e38e3SLen Brown  * Side effect:
2146ee7e38e3SLen Brown  * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
2147d7899447SLen Brown  */
2148ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
2149103a8feaSLen Brown {
2150ee7e38e3SLen Brown 	unsigned long long msr;
215121ed5574SLen Brown 	unsigned int base_ratio;
2152ee7e38e3SLen Brown 	int *pkg_cstate_limits;
2153ee7e38e3SLen Brown 
2154103a8feaSLen Brown 	if (!genuine_intel)
2155103a8feaSLen Brown 		return 0;
2156103a8feaSLen Brown 
2157103a8feaSLen Brown 	if (family != 6)
2158103a8feaSLen Brown 		return 0;
2159103a8feaSLen Brown 
216021ed5574SLen Brown 	bclk = discover_bclk(family, model);
216121ed5574SLen Brown 
2162103a8feaSLen Brown 	switch (model) {
2163103a8feaSLen Brown 	case 0x1A:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2164103a8feaSLen Brown 	case 0x1E:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2165103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
2166103a8feaSLen Brown 	case 0x25:	/* Westmere Client - Clarkdale, Arrandale */
2167103a8feaSLen Brown 	case 0x2C:	/* Westmere EP - Gulftown */
2168ee7e38e3SLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
2169ee7e38e3SLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
2170ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
2171ee7e38e3SLen Brown 		break;
2172103a8feaSLen Brown 	case 0x2A:	/* SNB */
2173103a8feaSLen Brown 	case 0x2D:	/* SNB Xeon */
2174553575f1SLen Brown 	case 0x3A:	/* IVB */
21751300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
2176ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
2177ee7e38e3SLen Brown 		break;
217870b43400SLen Brown 	case 0x3C:	/* HSW */
2179e6f9bb3cSLen Brown 	case 0x3F:	/* HSX */
218070b43400SLen Brown 	case 0x45:	/* HSW */
2181149c2319SLen Brown 	case 0x46:	/* HSW */
21824e8e863fSLen Brown 	case 0x3D:	/* BDW */
218348a0631cSLen Brown 	case 0x47:	/* BDW */
21844e8e863fSLen Brown 	case 0x4F:	/* BDX */
21854e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
21860b2bb692SLen Brown 	case 0x4E:	/* SKL */
21870b2bb692SLen Brown 	case 0x5E:	/* SKL */
2188cdc57272SLen Brown 	case 0x8E:	/* KBL */
2189cdc57272SLen Brown 	case 0x9E:	/* KBL */
2190ec53e594SLen Brown 	case 0x55:	/* SKX */
2191ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
2192ee7e38e3SLen Brown 		break;
2193ee7e38e3SLen Brown 	case 0x37:	/* BYT */
2194ee7e38e3SLen Brown 	case 0x4D:	/* AVN */
2195ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
2196ee7e38e3SLen Brown 		break;
2197ee7e38e3SLen Brown 	case 0x4C:	/* AMT */
2198ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
2199ee7e38e3SLen Brown 		break;
2200ee7e38e3SLen Brown 	case 0x57:	/* PHI */
2201ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
2202ee7e38e3SLen Brown 		break;
2203e4085d54SLen Brown 	case 0x5C:	/* BXT */
2204e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
2205e4085d54SLen Brown 		break;
2206103a8feaSLen Brown 	default:
2207103a8feaSLen Brown 		return 0;
2208103a8feaSLen Brown 	}
22097ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
2210e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2211ee7e38e3SLen Brown 
2212ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
221321ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
221421ed5574SLen Brown 
221521ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
221621ed5574SLen Brown 	has_base_hz = 1;
2217ee7e38e3SLen Brown 	return 1;
2218103a8feaSLen Brown }
2219d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2220d7899447SLen Brown {
2221d7899447SLen Brown 	switch (model) {
2222d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
2223d7899447SLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
2224d7899447SLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
2225cbf97abaSHubert Chrzaniuk 	case 0x57:	/* PHI - Knights Landing (different MSR definition) */
2226d7899447SLen Brown 		return 0;
2227d7899447SLen Brown 	default:
2228d7899447SLen Brown 		return 1;
2229d7899447SLen Brown 	}
2230d7899447SLen Brown }
22316574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
22326574a5d5SLen Brown {
22336574a5d5SLen Brown 	if (!genuine_intel)
22346574a5d5SLen Brown 		return 0;
22356574a5d5SLen Brown 
22366574a5d5SLen Brown 	if (family != 6)
22376574a5d5SLen Brown 		return 0;
22386574a5d5SLen Brown 
22396574a5d5SLen Brown 	switch (model) {
22406574a5d5SLen Brown 	case 0x3E:	/* IVB Xeon */
2241fcd17211SLen Brown 	case 0x3F:	/* HSW Xeon */
22426574a5d5SLen Brown 		return 1;
22436574a5d5SLen Brown 	default:
22446574a5d5SLen Brown 		return 0;
22456574a5d5SLen Brown 	}
22466574a5d5SLen Brown }
2247fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2248fcd17211SLen Brown {
2249fcd17211SLen Brown 	if (!genuine_intel)
2250fcd17211SLen Brown 		return 0;
2251fcd17211SLen Brown 
2252fcd17211SLen Brown 	if (family != 6)
2253fcd17211SLen Brown 		return 0;
2254fcd17211SLen Brown 
2255fcd17211SLen Brown 	switch (model) {
2256fcd17211SLen Brown 	case 0x3F:	/* HSW Xeon */
2257fcd17211SLen Brown 		return 1;
2258fcd17211SLen Brown 	default:
2259fcd17211SLen Brown 		return 0;
2260fcd17211SLen Brown 	}
2261fcd17211SLen Brown }
2262fcd17211SLen Brown 
2263fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2264fb5d4327SDasaratharaman Chandramouli {
2265fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
2266fb5d4327SDasaratharaman Chandramouli 		return 0;
2267fb5d4327SDasaratharaman Chandramouli 
2268fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
2269fb5d4327SDasaratharaman Chandramouli 		return 0;
2270fb5d4327SDasaratharaman Chandramouli 
2271fb5d4327SDasaratharaman Chandramouli 	switch (model) {
2272fb5d4327SDasaratharaman Chandramouli 	case 0x57:	/* Knights Landing */
2273fb5d4327SDasaratharaman Chandramouli 		return 1;
2274fb5d4327SDasaratharaman Chandramouli 	default:
2275fb5d4327SDasaratharaman Chandramouli 		return 0;
2276fb5d4327SDasaratharaman Chandramouli 	}
2277fb5d4327SDasaratharaman Chandramouli }
22786fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
22796fb3143bSLen Brown {
22806fb3143bSLen Brown 	if (!genuine_intel)
22816fb3143bSLen Brown 		return 0;
22826fb3143bSLen Brown 
22836fb3143bSLen Brown 	if (family != 6)
22846fb3143bSLen Brown 		return 0;
22856fb3143bSLen Brown 
22866fb3143bSLen Brown 	switch (model) {
22876fb3143bSLen Brown 	case 0x3A:	/* IVB */
22886fb3143bSLen Brown 	case 0x3C:	/* HSW */
22896fb3143bSLen Brown 	case 0x3F:	/* HSX */
22906fb3143bSLen Brown 	case 0x45:	/* HSW */
22916fb3143bSLen Brown 	case 0x46:	/* HSW */
22926fb3143bSLen Brown 	case 0x3D:	/* BDW */
22936fb3143bSLen Brown 	case 0x47:	/* BDW */
22946fb3143bSLen Brown 	case 0x4F:	/* BDX */
22956fb3143bSLen Brown 	case 0x56:	/* BDX-DE */
22966fb3143bSLen Brown 	case 0x4E:	/* SKL */
22976fb3143bSLen Brown 	case 0x5E:	/* SKL */
2298cdc57272SLen Brown 	case 0x8E:	/* KBL */
2299cdc57272SLen Brown 	case 0x9E:	/* KBL */
2300ec53e594SLen Brown 	case 0x55:	/* SKX */
23016fb3143bSLen Brown 
23026fb3143bSLen Brown 	case 0x57:	/* Knights Landing */
23036fb3143bSLen Brown 		return 1;
23046fb3143bSLen Brown 	default:
23056fb3143bSLen Brown 		return 0;
23066fb3143bSLen Brown 	}
23076fb3143bSLen Brown }
23086fb3143bSLen Brown 
2309fcd17211SLen Brown static void
23101b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
2311fcd17211SLen Brown {
2312fcd17211SLen Brown 	if (!do_nhm_platform_info)
2313fcd17211SLen Brown 		return;
2314fcd17211SLen Brown 
2315fcd17211SLen Brown 	dump_nhm_platform_info();
2316fcd17211SLen Brown 
2317fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
2318fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
2319fcd17211SLen Brown 
2320fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
2321fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
2322fcd17211SLen Brown 
2323fcd17211SLen Brown 	if (has_nhm_turbo_ratio_limit(family, model))
2324fcd17211SLen Brown 		dump_nhm_turbo_ratio_limits();
2325fcd17211SLen Brown 
2326fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
2327fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
2328fb5d4327SDasaratharaman Chandramouli 
23296fb3143bSLen Brown 	if (has_config_tdp(family, model))
23306fb3143bSLen Brown 		dump_config_tdp();
23316fb3143bSLen Brown 
2332fcd17211SLen Brown 	dump_nhm_cst_cfg();
2333fcd17211SLen Brown }
2334fcd17211SLen Brown 
23356574a5d5SLen Brown 
2336889facbeSLen Brown /*
2337889facbeSLen Brown  * print_epb()
2338889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
2339889facbeSLen Brown  */
2340889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2341889facbeSLen Brown {
2342889facbeSLen Brown 	unsigned long long msr;
2343889facbeSLen Brown 	char *epb_string;
2344889facbeSLen Brown 	int cpu;
2345889facbeSLen Brown 
2346889facbeSLen Brown 	if (!has_epb)
2347889facbeSLen Brown 		return 0;
2348889facbeSLen Brown 
2349889facbeSLen Brown 	cpu = t->cpu_id;
2350889facbeSLen Brown 
2351889facbeSLen Brown 	/* EPB is per-package */
2352889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2353889facbeSLen Brown 		return 0;
2354889facbeSLen Brown 
2355889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2356b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2357889facbeSLen Brown 		return -1;
2358889facbeSLen Brown 	}
2359889facbeSLen Brown 
2360889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2361889facbeSLen Brown 		return 0;
2362889facbeSLen Brown 
2363e9be7dd6SLen Brown 	switch (msr & 0xF) {
2364889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
2365889facbeSLen Brown 		epb_string = "performance";
2366889facbeSLen Brown 		break;
2367889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
2368889facbeSLen Brown 		epb_string = "balanced";
2369889facbeSLen Brown 		break;
2370889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
2371889facbeSLen Brown 		epb_string = "powersave";
2372889facbeSLen Brown 		break;
2373889facbeSLen Brown 	default:
2374889facbeSLen Brown 		epb_string = "custom";
2375889facbeSLen Brown 		break;
2376889facbeSLen Brown 	}
2377b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2378889facbeSLen Brown 
2379889facbeSLen Brown 	return 0;
2380889facbeSLen Brown }
23817f5c258eSLen Brown /*
23827f5c258eSLen Brown  * print_hwp()
23837f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
23847f5c258eSLen Brown  */
23857f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
23867f5c258eSLen Brown {
23877f5c258eSLen Brown 	unsigned long long msr;
23887f5c258eSLen Brown 	int cpu;
23897f5c258eSLen Brown 
23907f5c258eSLen Brown 	if (!has_hwp)
23917f5c258eSLen Brown 		return 0;
23927f5c258eSLen Brown 
23937f5c258eSLen Brown 	cpu = t->cpu_id;
23947f5c258eSLen Brown 
23957f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
23967f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
23977f5c258eSLen Brown 		return 0;
23987f5c258eSLen Brown 
23997f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
2400b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
24017f5c258eSLen Brown 		return -1;
24027f5c258eSLen Brown 	}
24037f5c258eSLen Brown 
24047f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
24057f5c258eSLen Brown 		return 0;
24067f5c258eSLen Brown 
2407b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
24087f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
24097f5c258eSLen Brown 
24107f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
24117f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
24127f5c258eSLen Brown 		return 0;
24137f5c258eSLen Brown 
24147f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
24157f5c258eSLen Brown 		return 0;
24167f5c258eSLen Brown 
2417b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
24187f5c258eSLen Brown 			"(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
24197f5c258eSLen Brown 			cpu, msr,
24207f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
24217f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
24227f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
24237f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
24247f5c258eSLen Brown 
24257f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
24267f5c258eSLen Brown 		return 0;
24277f5c258eSLen Brown 
2428b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
24297f5c258eSLen Brown 			"(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
24307f5c258eSLen Brown 			cpu, msr,
24317f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
24327f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
24337f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
24347f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
24357f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
24367f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
24377f5c258eSLen Brown 
24387f5c258eSLen Brown 	if (has_hwp_pkg) {
24397f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
24407f5c258eSLen Brown 			return 0;
24417f5c258eSLen Brown 
2442b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
24437f5c258eSLen Brown 			"(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
24447f5c258eSLen Brown 			cpu, msr,
24457f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
24467f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
24477f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
24487f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
24497f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
24507f5c258eSLen Brown 	}
24517f5c258eSLen Brown 	if (has_hwp_notify) {
24527f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
24537f5c258eSLen Brown 			return 0;
24547f5c258eSLen Brown 
2455b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
24567f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
24577f5c258eSLen Brown 			cpu, msr,
24587f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
24597f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
24607f5c258eSLen Brown 	}
24617f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
24627f5c258eSLen Brown 		return 0;
24637f5c258eSLen Brown 
2464b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
24657f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
24667f5c258eSLen Brown 			cpu, msr,
24677f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
24687f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
2469889facbeSLen Brown 
2470889facbeSLen Brown 	return 0;
2471889facbeSLen Brown }
2472889facbeSLen Brown 
24733a9a941dSLen Brown /*
24743a9a941dSLen Brown  * print_perf_limit()
24753a9a941dSLen Brown  */
24763a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
24773a9a941dSLen Brown {
24783a9a941dSLen Brown 	unsigned long long msr;
24793a9a941dSLen Brown 	int cpu;
24803a9a941dSLen Brown 
24813a9a941dSLen Brown 	cpu = t->cpu_id;
24823a9a941dSLen Brown 
24833a9a941dSLen Brown 	/* per-package */
24843a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
24853a9a941dSLen Brown 		return 0;
24863a9a941dSLen Brown 
24873a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
2488b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
24893a9a941dSLen Brown 		return -1;
24903a9a941dSLen Brown 	}
24913a9a941dSLen Brown 
24923a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
24933a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
2494b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2495b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
2496e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
24973a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
2498e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
2499e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2500e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
2501e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
2502e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
2503e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
2504e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
2505e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
2506e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
2507e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
2508e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
2509e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
2510b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
2511e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
25123a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
2513e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
2514e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2515e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
2516e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
2517e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
2518e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
2519e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
2520e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
2521e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
2522e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
2523e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
2524e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
25253a9a941dSLen Brown 
25263a9a941dSLen Brown 	}
25273a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
25283a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
2529b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2530b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
25313a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
25323a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
25333a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
25343a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
25353a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
25363a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
25373a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
25383a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
2539b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
25403a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
25413a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
25423a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
25433a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
25443a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
25453a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
25463a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
25473a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
25483a9a941dSLen Brown 	}
25493a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
25503a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2551b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2552b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
25533a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
25543a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
25553a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
25563a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
25573a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
25583a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
2559b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
25603a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
25613a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
25623a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
25633a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
25643a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
25653a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
25663a9a941dSLen Brown 	}
25673a9a941dSLen Brown 	return 0;
25683a9a941dSLen Brown }
25693a9a941dSLen Brown 
2570889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
2571889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
2572889facbeSLen Brown 
25731b69317dSColin Ian King double get_tdp(unsigned int model)
2574144b44b1SLen Brown {
2575144b44b1SLen Brown 	unsigned long long msr;
2576144b44b1SLen Brown 
2577144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
25787ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
2579144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2580144b44b1SLen Brown 
2581144b44b1SLen Brown 	switch (model) {
2582144b44b1SLen Brown 	case 0x37:
2583144b44b1SLen Brown 	case 0x4D:
2584144b44b1SLen Brown 		return 30.0;
2585144b44b1SLen Brown 	default:
2586144b44b1SLen Brown 		return 135.0;
2587144b44b1SLen Brown 	}
2588144b44b1SLen Brown }
2589144b44b1SLen Brown 
259040ee8e3bSAndrey Semin /*
259140ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
259240ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
259340ee8e3bSAndrey Semin  */
259440ee8e3bSAndrey Semin static double
259540ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
259640ee8e3bSAndrey Semin {
259740ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
259840ee8e3bSAndrey Semin 
259940ee8e3bSAndrey Semin 	switch (model) {
260040ee8e3bSAndrey Semin 	case 0x3F:	/* HSX */
260140ee8e3bSAndrey Semin 	case 0x4F:	/* BDX */
260240ee8e3bSAndrey Semin 	case 0x56:	/* BDX-DE */
2603fb5d4327SDasaratharaman Chandramouli 	case 0x57:	/* KNL */
260440ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
260540ee8e3bSAndrey Semin 	default:
260640ee8e3bSAndrey Semin 		return (rapl_energy_units);
260740ee8e3bSAndrey Semin 	}
260840ee8e3bSAndrey Semin }
260940ee8e3bSAndrey Semin 
2610144b44b1SLen Brown 
2611889facbeSLen Brown /*
2612889facbeSLen Brown  * rapl_probe()
2613889facbeSLen Brown  *
2614144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2615889facbeSLen Brown  */
2616889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
2617889facbeSLen Brown {
2618889facbeSLen Brown 	unsigned long long msr;
2619144b44b1SLen Brown 	unsigned int time_unit;
2620889facbeSLen Brown 	double tdp;
2621889facbeSLen Brown 
2622889facbeSLen Brown 	if (!genuine_intel)
2623889facbeSLen Brown 		return;
2624889facbeSLen Brown 
2625889facbeSLen Brown 	if (family != 6)
2626889facbeSLen Brown 		return;
2627889facbeSLen Brown 
2628889facbeSLen Brown 	switch (model) {
2629889facbeSLen Brown 	case 0x2A:
2630889facbeSLen Brown 	case 0x3A:
263170b43400SLen Brown 	case 0x3C:	/* HSW */
263270b43400SLen Brown 	case 0x45:	/* HSW */
2633149c2319SLen Brown 	case 0x46:	/* HSW */
26344e8e863fSLen Brown 	case 0x3D:	/* BDW */
263548a0631cSLen Brown 	case 0x47:	/* BDW */
2636144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
2637889facbeSLen Brown 		break;
2638e4085d54SLen Brown 	case 0x5C:	/* BXT */
2639e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
2640e4085d54SLen Brown 		break;
26410b2bb692SLen Brown 	case 0x4E:	/* SKL */
26420b2bb692SLen Brown 	case 0x5E:	/* SKL */
2643cdc57272SLen Brown 	case 0x8E:	/* KBL */
2644cdc57272SLen Brown 	case 0x9E:	/* KBL */
26450b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
26460b2bb692SLen Brown 		break;
2647e6f9bb3cSLen Brown 	case 0x3F:	/* HSX */
26484e8e863fSLen Brown 	case 0x4F:	/* BDX */
26494e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
2650ec53e594SLen Brown 	case 0x55:	/* SKX */
2651fb5d4327SDasaratharaman Chandramouli 	case 0x57:	/* KNL */
26520b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2653e6f9bb3cSLen Brown 		break;
2654889facbeSLen Brown 	case 0x2D:
2655889facbeSLen Brown 	case 0x3E:
26560b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
2657144b44b1SLen Brown 		break;
2658144b44b1SLen Brown 	case 0x37:	/* BYT */
2659144b44b1SLen Brown 	case 0x4D:	/* AVN */
2660144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES ;
2661889facbeSLen Brown 		break;
2662889facbeSLen Brown 	default:
2663889facbeSLen Brown 		return;
2664889facbeSLen Brown 	}
2665889facbeSLen Brown 
2666889facbeSLen Brown 	/* units on package 0, verify later other packages match */
26677ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
2668889facbeSLen Brown 		return;
2669889facbeSLen Brown 
2670889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
2671144b44b1SLen Brown 	if (model == 0x37)
2672144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2673144b44b1SLen Brown 	else
2674889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
2675889facbeSLen Brown 
267640ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
267740ee8e3bSAndrey Semin 
2678144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
2679144b44b1SLen Brown 	if (time_unit == 0)
2680144b44b1SLen Brown 		time_unit = 0xA;
2681889facbeSLen Brown 
2682144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
2683144b44b1SLen Brown 
2684144b44b1SLen Brown 	tdp = get_tdp(model);
2685889facbeSLen Brown 
2686889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
2687d8af6f5fSLen Brown 	if (debug)
2688b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
2689889facbeSLen Brown 
2690889facbeSLen Brown 	return;
2691889facbeSLen Brown }
2692889facbeSLen Brown 
26931b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
26943a9a941dSLen Brown {
26953a9a941dSLen Brown 	if (!genuine_intel)
26963a9a941dSLen Brown 		return;
26973a9a941dSLen Brown 
26983a9a941dSLen Brown 	if (family != 6)
26993a9a941dSLen Brown 		return;
27003a9a941dSLen Brown 
27013a9a941dSLen Brown 	switch (model) {
27023a9a941dSLen Brown 	case 0x3C:	/* HSW */
27033a9a941dSLen Brown 	case 0x45:	/* HSW */
27043a9a941dSLen Brown 	case 0x46:	/* HSW */
27053a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
27063a9a941dSLen Brown 	case 0x3F:	/* HSX */
27073a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
27083a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
27093a9a941dSLen Brown 	default:
27103a9a941dSLen Brown 		return;
27113a9a941dSLen Brown 	}
27123a9a941dSLen Brown }
27133a9a941dSLen Brown 
2714889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2715889facbeSLen Brown {
2716889facbeSLen Brown 	unsigned long long msr;
2717889facbeSLen Brown 	unsigned int dts;
2718889facbeSLen Brown 	int cpu;
2719889facbeSLen Brown 
2720889facbeSLen Brown 	if (!(do_dts || do_ptm))
2721889facbeSLen Brown 		return 0;
2722889facbeSLen Brown 
2723889facbeSLen Brown 	cpu = t->cpu_id;
2724889facbeSLen Brown 
2725889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
2726889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2727889facbeSLen Brown 		return 0;
2728889facbeSLen Brown 
2729889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2730b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2731889facbeSLen Brown 		return -1;
2732889facbeSLen Brown 	}
2733889facbeSLen Brown 
2734889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2735889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2736889facbeSLen Brown 			return 0;
2737889facbeSLen Brown 
2738889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2739b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2740889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
2741889facbeSLen Brown 
2742889facbeSLen Brown #ifdef	THERM_DEBUG
2743889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2744889facbeSLen Brown 			return 0;
2745889facbeSLen Brown 
2746889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2747889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
2748b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2749889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2750889facbeSLen Brown #endif
2751889facbeSLen Brown 	}
2752889facbeSLen Brown 
2753889facbeSLen Brown 
2754889facbeSLen Brown 	if (do_dts) {
2755889facbeSLen Brown 		unsigned int resolution;
2756889facbeSLen Brown 
2757889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2758889facbeSLen Brown 			return 0;
2759889facbeSLen Brown 
2760889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2761889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
2762b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2763889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
2764889facbeSLen Brown 
2765889facbeSLen Brown #ifdef THERM_DEBUG
2766889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2767889facbeSLen Brown 			return 0;
2768889facbeSLen Brown 
2769889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2770889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
2771b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2772889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2773889facbeSLen Brown #endif
2774889facbeSLen Brown 	}
2775889facbeSLen Brown 
2776889facbeSLen Brown 	return 0;
2777889facbeSLen Brown }
2778889facbeSLen Brown 
2779889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2780889facbeSLen Brown {
2781b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2782889facbeSLen Brown 		cpu, label,
2783889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
2784889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
2785889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2786889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
2787889facbeSLen Brown 
2788889facbeSLen Brown 	return;
2789889facbeSLen Brown }
2790889facbeSLen Brown 
2791889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2792889facbeSLen Brown {
2793889facbeSLen Brown 	unsigned long long msr;
2794889facbeSLen Brown 	int cpu;
2795889facbeSLen Brown 
2796889facbeSLen Brown 	if (!do_rapl)
2797889facbeSLen Brown 		return 0;
2798889facbeSLen Brown 
2799889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
2800889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2801889facbeSLen Brown 		return 0;
2802889facbeSLen Brown 
2803889facbeSLen Brown 	cpu = t->cpu_id;
2804889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2805b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2806889facbeSLen Brown 		return -1;
2807889facbeSLen Brown 	}
2808889facbeSLen Brown 
2809889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2810889facbeSLen Brown 		return -1;
2811889facbeSLen Brown 
2812d8af6f5fSLen Brown 	if (debug) {
2813b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2814889facbeSLen Brown 			"(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
2815144b44b1SLen Brown 			rapl_power_units, rapl_energy_units, rapl_time_units);
2816889facbeSLen Brown 	}
2817144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
2818144b44b1SLen Brown 
2819889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2820889facbeSLen Brown                 	return -5;
2821889facbeSLen Brown 
2822889facbeSLen Brown 
2823b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2824889facbeSLen Brown 			cpu, msr,
2825889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2826889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2827889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2828889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2829889facbeSLen Brown 
2830144b44b1SLen Brown 	}
2831144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
2832144b44b1SLen Brown 
2833889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2834889facbeSLen Brown 			return -9;
2835889facbeSLen Brown 
2836b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2837889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "": "UN");
2838889facbeSLen Brown 
2839889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
2840b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2841889facbeSLen Brown 			cpu,
2842889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
2843889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
2844889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2845889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
2846889facbeSLen Brown 	}
2847889facbeSLen Brown 
28480b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
2849889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2850889facbeSLen Brown                 	return -6;
2851889facbeSLen Brown 
2852b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2853889facbeSLen Brown 			cpu, msr,
2854889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2855889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2856889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2857889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
28580b2bb692SLen Brown 	}
28590b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
2860889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2861889facbeSLen Brown 			return -9;
2862b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2863889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "": "UN");
2864889facbeSLen Brown 
2865889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
2866889facbeSLen Brown 	}
2867144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
2868d8af6f5fSLen Brown 		if (debug) {
2869889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2870889facbeSLen Brown 				return -7;
2871889facbeSLen Brown 
2872b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
2873144b44b1SLen Brown 		}
2874144b44b1SLen Brown 	}
2875144b44b1SLen Brown 	if (do_rapl & RAPL_CORES) {
2876d8af6f5fSLen Brown 		if (debug) {
2877889facbeSLen Brown 
2878889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2879889facbeSLen Brown 				return -9;
2880b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2881889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
2882889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "Cores Limit");
2883889facbeSLen Brown 		}
2884889facbeSLen Brown 	}
2885889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
2886d8af6f5fSLen Brown 		if (debug) {
2887889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2888889facbeSLen Brown 				return -8;
2889889facbeSLen Brown 
2890b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2891889facbeSLen Brown 
2892889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2893889facbeSLen Brown 				return -9;
2894b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2895889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
2896889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "GFX Limit");
2897889facbeSLen Brown 		}
2898889facbeSLen Brown 	}
2899889facbeSLen Brown 	return 0;
2900889facbeSLen Brown }
2901889facbeSLen Brown 
2902d7899447SLen Brown /*
2903d7899447SLen Brown  * SNB adds support for additional MSRs:
2904d7899447SLen Brown  *
2905d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
2906d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
2907d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
2908d7899447SLen Brown  */
2909103a8feaSLen Brown 
2910d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
2911103a8feaSLen Brown {
2912103a8feaSLen Brown 	if (!genuine_intel)
2913103a8feaSLen Brown 		return 0;
2914103a8feaSLen Brown 
2915103a8feaSLen Brown 	switch (model) {
2916103a8feaSLen Brown 	case 0x2A:
2917103a8feaSLen Brown 	case 0x2D:
2918650a37f3SLen Brown 	case 0x3A:	/* IVB */
29191300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
292070b43400SLen Brown 	case 0x3C:	/* HSW */
292170b43400SLen Brown 	case 0x3F:	/* HSW */
292270b43400SLen Brown 	case 0x45:	/* HSW */
2923149c2319SLen Brown 	case 0x46:	/* HSW */
29244e8e863fSLen Brown 	case 0x3D:	/* BDW */
292548a0631cSLen Brown 	case 0x47:	/* BDW */
29264e8e863fSLen Brown 	case 0x4F:	/* BDX */
29274e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
29280b2bb692SLen Brown 	case 0x4E:	/* SKL */
29290b2bb692SLen Brown 	case 0x5E:	/* SKL */
2930cdc57272SLen Brown 	case 0x8E:	/* KBL */
2931cdc57272SLen Brown 	case 0x9E:	/* KBL */
2932ec53e594SLen Brown 	case 0x55:	/* SKX */
2933e4085d54SLen Brown 	case 0x5C:	/* BXT */
2934103a8feaSLen Brown 		return 1;
2935103a8feaSLen Brown 	}
2936103a8feaSLen Brown 	return 0;
2937103a8feaSLen Brown }
2938103a8feaSLen Brown 
2939d7899447SLen Brown /*
2940d7899447SLen Brown  * HSW adds support for additional MSRs:
2941d7899447SLen Brown  *
2942d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
2943d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
2944d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
29455a63426eSLen Brown  *
29465a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
29475a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
29485a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
29495a63426eSLen Brown  *
2950d7899447SLen Brown  */
2951d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
2952ca58710fSKristen Carlson Accardi {
2953ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
2954ca58710fSKristen Carlson Accardi 		return 0;
2955ca58710fSKristen Carlson Accardi 
2956ca58710fSKristen Carlson Accardi 	switch (model) {
29574e8e863fSLen Brown 	case 0x45:	/* HSW */
29584e8e863fSLen Brown 	case 0x3D:	/* BDW */
29590b2bb692SLen Brown 	case 0x4E:	/* SKL */
29600b2bb692SLen Brown 	case 0x5E:	/* SKL */
2961cdc57272SLen Brown 	case 0x8E:	/* KBL */
2962cdc57272SLen Brown 	case 0x9E:	/* KBL */
2963e4085d54SLen Brown 	case 0x5C:	/* BXT */
2964ca58710fSKristen Carlson Accardi 		return 1;
2965ca58710fSKristen Carlson Accardi 	}
2966ca58710fSKristen Carlson Accardi 	return 0;
2967ca58710fSKristen Carlson Accardi }
2968ca58710fSKristen Carlson Accardi 
29690b2bb692SLen Brown /*
29700b2bb692SLen Brown  * SKL adds support for additional MSRS:
29710b2bb692SLen Brown  *
29720b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
29730b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
29740b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
29750b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
29760b2bb692SLen Brown  */
29770b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
29780b2bb692SLen Brown {
29790b2bb692SLen Brown 	if (!genuine_intel)
29800b2bb692SLen Brown 		return 0;
29810b2bb692SLen Brown 
29820b2bb692SLen Brown 	switch (model) {
29830b2bb692SLen Brown 	case 0x4E:	/* SKL */
29840b2bb692SLen Brown 	case 0x5E:	/* SKL */
2985cdc57272SLen Brown 	case 0x8E:	/* KBL */
2986cdc57272SLen Brown 	case 0x9E:	/* KBL */
29870b2bb692SLen Brown 		return 1;
29880b2bb692SLen Brown 	}
29890b2bb692SLen Brown 	return 0;
29900b2bb692SLen Brown }
29910b2bb692SLen Brown 
29920b2bb692SLen Brown 
2993ca58710fSKristen Carlson Accardi 
2994144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
2995144b44b1SLen Brown {
2996144b44b1SLen Brown 	if (!genuine_intel)
2997144b44b1SLen Brown 		return 0;
2998144b44b1SLen Brown 	switch (model) {
2999144b44b1SLen Brown 	case 0x37:	/* BYT */
3000144b44b1SLen Brown 	case 0x4D:	/* AVN */
3001144b44b1SLen Brown 		return 1;
3002144b44b1SLen Brown 	}
3003144b44b1SLen Brown 	return 0;
3004144b44b1SLen Brown }
3005144b44b1SLen Brown 
3006fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
3007fb5d4327SDasaratharaman Chandramouli {
3008fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3009fb5d4327SDasaratharaman Chandramouli 		return 0;
3010fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3011fb5d4327SDasaratharaman Chandramouli 	case 0x57:	/* KNL */
3012fb5d4327SDasaratharaman Chandramouli 		return 1;
3013fb5d4327SDasaratharaman Chandramouli 	}
3014fb5d4327SDasaratharaman Chandramouli 	return 0;
3015fb5d4327SDasaratharaman Chandramouli }
3016fb5d4327SDasaratharaman Chandramouli 
3017b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3018b2b34dfeSHubert Chrzaniuk {
3019b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
3020b2b34dfeSHubert Chrzaniuk 		return 1024;
3021b2b34dfeSHubert Chrzaniuk 	return 1;
3022b2b34dfeSHubert Chrzaniuk }
3023b2b34dfeSHubert Chrzaniuk 
3024144b44b1SLen Brown #define SLM_BCLK_FREQS 5
3025144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3026144b44b1SLen Brown 
3027144b44b1SLen Brown double slm_bclk(void)
3028144b44b1SLen Brown {
3029144b44b1SLen Brown 	unsigned long long msr = 3;
3030144b44b1SLen Brown 	unsigned int i;
3031144b44b1SLen Brown 	double freq;
3032144b44b1SLen Brown 
30337ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3034b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
3035144b44b1SLen Brown 
3036144b44b1SLen Brown 	i = msr & 0xf;
3037144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
3038b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
3039144b44b1SLen Brown 		msr = 3;
3040144b44b1SLen Brown 	}
3041144b44b1SLen Brown 	freq = slm_freq_table[i];
3042144b44b1SLen Brown 
3043b7d8c148SLen Brown 	fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3044144b44b1SLen Brown 
3045144b44b1SLen Brown 	return freq;
3046144b44b1SLen Brown }
3047144b44b1SLen Brown 
3048103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
3049103a8feaSLen Brown {
3050121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
3051103a8feaSLen Brown 		return 100.00;
3052144b44b1SLen Brown 	else if (is_slm(family, model))
3053144b44b1SLen Brown 		return slm_bclk();
3054103a8feaSLen Brown 	else
3055103a8feaSLen Brown 		return 133.33;
3056103a8feaSLen Brown }
3057103a8feaSLen Brown 
3058889facbeSLen Brown /*
3059889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3060889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
3061889facbeSLen Brown  * This is usually equal to tjMax.
3062889facbeSLen Brown  *
3063889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
3064889facbeSLen Brown  * but also allow cmdline over-ride with -T.
3065889facbeSLen Brown  *
3066889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
3067889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
3068889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3069889facbeSLen Brown  */
3070889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3071889facbeSLen Brown {
3072889facbeSLen Brown 	unsigned long long msr;
3073889facbeSLen Brown 	unsigned int target_c_local;
3074889facbeSLen Brown 	int cpu;
3075889facbeSLen Brown 
3076889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
3077889facbeSLen Brown 	if (!(do_dts || do_ptm))
3078889facbeSLen Brown 		return 0;
3079889facbeSLen Brown 
3080889facbeSLen Brown 	/* this is a per-package concept */
3081889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3082889facbeSLen Brown 		return 0;
3083889facbeSLen Brown 
3084889facbeSLen Brown 	cpu = t->cpu_id;
3085889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3086b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3087889facbeSLen Brown 		return -1;
3088889facbeSLen Brown 	}
3089889facbeSLen Brown 
3090889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
3091889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
3092b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
3093889facbeSLen Brown 			cpu, tcc_activation_temp);
3094889facbeSLen Brown 		return 0;
3095889facbeSLen Brown 	}
3096889facbeSLen Brown 
3097889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
3098d7899447SLen Brown 	if (!do_nhm_platform_info)
3099889facbeSLen Brown 		goto guess;
3100889facbeSLen Brown 
31017ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
3102889facbeSLen Brown 		goto guess;
3103889facbeSLen Brown 
31043482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
3105889facbeSLen Brown 
3106d8af6f5fSLen Brown 	if (debug)
3107b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3108889facbeSLen Brown 			cpu, msr, target_c_local);
3109889facbeSLen Brown 
31103482124aSJean Delvare 	if (!target_c_local)
3111889facbeSLen Brown 		goto guess;
3112889facbeSLen Brown 
3113889facbeSLen Brown 	tcc_activation_temp = target_c_local;
3114889facbeSLen Brown 
3115889facbeSLen Brown 	return 0;
3116889facbeSLen Brown 
3117889facbeSLen Brown guess:
3118889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
3119b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3120889facbeSLen Brown 		cpu, tcc_activation_temp);
3121889facbeSLen Brown 
3122889facbeSLen Brown 	return 0;
3123889facbeSLen Brown }
312469807a63SLen Brown 
3125aa8d8cc7SLen Brown void decode_feature_control_msr(void)
3126aa8d8cc7SLen Brown {
3127aa8d8cc7SLen Brown 	unsigned long long msr;
3128aa8d8cc7SLen Brown 
3129aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3130aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3131aa8d8cc7SLen Brown 			base_cpu, msr,
3132aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3133aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
3134aa8d8cc7SLen Brown }
3135aa8d8cc7SLen Brown 
313669807a63SLen Brown void decode_misc_enable_msr(void)
313769807a63SLen Brown {
313869807a63SLen Brown 	unsigned long long msr;
313969807a63SLen Brown 
314069807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
3141b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
314269807a63SLen Brown 			base_cpu, msr,
314369807a63SLen Brown 			msr & (1 << 3) ? "TCC" : "",
314469807a63SLen Brown 			msr & (1 << 16) ? "EIST" : "",
314569807a63SLen Brown 			msr & (1 << 18) ? "MONITOR" : "");
314669807a63SLen Brown }
314769807a63SLen Brown 
3148f0057310SLen Brown /*
3149f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
3150f0057310SLen Brown  *
3151f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
3152f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
3153f0057310SLen Brown  * bit[1] less so...
3154f0057310SLen Brown  */
3155f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
3156f0057310SLen Brown {
3157f0057310SLen Brown 	unsigned long long msr;
3158f0057310SLen Brown 
3159f0057310SLen Brown 	if (!do_nhm_platform_info)
3160f0057310SLen Brown 		return;
3161f0057310SLen Brown 
3162f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
3163b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB)\n",
3164f0057310SLen Brown 			base_cpu, msr,
3165f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
3166f0057310SLen Brown 			msr & (1 << 1) ? "EN" : "DIS");
3167f0057310SLen Brown }
31687f5c258eSLen Brown 
3169fcd17211SLen Brown void process_cpuid()
3170103a8feaSLen Brown {
317161a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
3172103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
3173103a8feaSLen Brown 
3174103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
3175103a8feaSLen Brown 
31765aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
3177103a8feaSLen Brown 
3178103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3179103a8feaSLen Brown 		genuine_intel = 1;
3180103a8feaSLen Brown 
3181d8af6f5fSLen Brown 	if (debug)
3182b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
3183103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
3184103a8feaSLen Brown 
31855aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
3186103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
3187103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
3188103a8feaSLen Brown 	stepping = fms & 0xf;
3189103a8feaSLen Brown 	if (family == 6 || family == 0xf)
3190103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
3191103a8feaSLen Brown 
319269807a63SLen Brown 	if (debug) {
3193b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
3194103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
3195aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
319669807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
319769807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
3198aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
319969807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
320069807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
320169807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
320269807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
320369807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
320469807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
320569807a63SLen Brown 	}
3206103a8feaSLen Brown 
3207b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
3208b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
3209103a8feaSLen Brown 
3210103a8feaSLen Brown 	/*
3211103a8feaSLen Brown 	 * check max extended function levels of CPUID.
3212103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
3213103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
3214103a8feaSLen Brown 	 */
3215103a8feaSLen Brown 	ebx = ecx = edx = 0;
32165aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
3217103a8feaSLen Brown 
321861a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
3219103a8feaSLen Brown 
3220103a8feaSLen Brown 		/*
3221103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3222103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
3223103a8feaSLen Brown 		 */
32245aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
32258209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
3226d7899447SLen Brown 	}
3227103a8feaSLen Brown 
3228103a8feaSLen Brown 	/*
3229103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3230103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
3231103a8feaSLen Brown 	 */
3232103a8feaSLen Brown 
32335aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
32348209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
3235889facbeSLen Brown 	do_dts = eax & (1 << 0);
3236889facbeSLen Brown 	do_ptm = eax & (1 << 6);
32377f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
32387f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
32397f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
32407f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
32417f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
3242889facbeSLen Brown 	has_epb = ecx & (1 << 3);
3243889facbeSLen Brown 
3244d8af6f5fSLen Brown 	if (debug)
3245b7d8c148SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
32467f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
32477f5c258eSLen Brown 			has_aperf ? "" : "No-",
32487f5c258eSLen Brown 			do_dts ? "" : "No-",
32497f5c258eSLen Brown 			do_ptm ? "" : "No-",
32507f5c258eSLen Brown 			has_hwp ? "" : "No-",
32517f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
32527f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
32537f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
32547f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
32557f5c258eSLen Brown 			has_epb ? "" : "No-");
3256103a8feaSLen Brown 
325769807a63SLen Brown 	if (debug)
325869807a63SLen Brown 		decode_misc_enable_msr();
325969807a63SLen Brown 
32608ae72255SLen Brown 	if (max_level >= 0x7 && debug) {
3261aa8d8cc7SLen Brown 		int has_sgx;
3262aa8d8cc7SLen Brown 
3263aa8d8cc7SLen Brown 		ecx = 0;
3264aa8d8cc7SLen Brown 
3265aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3266aa8d8cc7SLen Brown 
3267aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
3268aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3269aa8d8cc7SLen Brown 
3270aa8d8cc7SLen Brown 		if (has_sgx)
3271aa8d8cc7SLen Brown 			decode_feature_control_msr();
3272aa8d8cc7SLen Brown 	}
3273aa8d8cc7SLen Brown 
327461a87ba7SLen Brown 	if (max_level >= 0x15) {
32758a5bdf41SLen Brown 		unsigned int eax_crystal;
32768a5bdf41SLen Brown 		unsigned int ebx_tsc;
32778a5bdf41SLen Brown 
32788a5bdf41SLen Brown 		/*
32798a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
32808a5bdf41SLen Brown 		 */
32818a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
32825aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
32838a5bdf41SLen Brown 
32848a5bdf41SLen Brown 		if (ebx_tsc != 0) {
32858a5bdf41SLen Brown 
32868a5bdf41SLen Brown 			if (debug && (ebx != 0))
3287b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
32888a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
32898a5bdf41SLen Brown 
32908a5bdf41SLen Brown 			if (crystal_hz == 0)
32918a5bdf41SLen Brown 				switch(model) {
32928a5bdf41SLen Brown 				case 0x4E:	/* SKL */
32938a5bdf41SLen Brown 				case 0x5E:	/* SKL */
3294cdc57272SLen Brown 				case 0x8E:	/* KBL */
3295cdc57272SLen Brown 				case 0x9E:	/* KBL */
3296e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
3297e8efbc80SLen Brown 					break;
3298ec53e594SLen Brown 				case 0x55:	/* SKX */
3299ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
3300ec53e594SLen Brown 					break;
3301e8efbc80SLen Brown 				case 0x5C:	/* BXT */
3302e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
33038a5bdf41SLen Brown 					break;
33048a5bdf41SLen Brown 				default:
33058a5bdf41SLen Brown 					crystal_hz = 0;
33068a5bdf41SLen Brown 			}
33078a5bdf41SLen Brown 
33088a5bdf41SLen Brown 			if (crystal_hz) {
33098a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
33108a5bdf41SLen Brown 				if (debug)
3311b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
33128a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
33138a5bdf41SLen Brown 			}
33148a5bdf41SLen Brown 		}
33158a5bdf41SLen Brown 	}
331661a87ba7SLen Brown 	if (max_level >= 0x16) {
331761a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
331861a87ba7SLen Brown 
331961a87ba7SLen Brown 		/*
332061a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
332161a87ba7SLen Brown 		 */
332261a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
332361a87ba7SLen Brown 
33245aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
332561a87ba7SLen Brown 		if (debug)
3326b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
332761a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
332861a87ba7SLen Brown 	}
33298a5bdf41SLen Brown 
3330b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
3331b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3332b2b34dfeSHubert Chrzaniuk 
3333ee7e38e3SLen Brown 	do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
3334d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
33355a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
3336ee7e38e3SLen Brown 	do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3337ee7e38e3SLen Brown 	do_pc3 = (pkg_cstate_limit >= PCL__3);
3338ee7e38e3SLen Brown 	do_pc6 = (pkg_cstate_limit >= PCL__6);
3339ee7e38e3SLen Brown 	do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
3340d7899447SLen Brown 	do_c8_c9_c10 = has_hsw_msrs(family, model);
33415a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
33420b2bb692SLen Brown 	do_skl_residency = has_skl_msrs(family, model);
3343144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
3344fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
3345103a8feaSLen Brown 
3346f0057310SLen Brown 	if (debug)
3347f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
3348f0057310SLen Brown 
3349889facbeSLen Brown 	rapl_probe(family, model);
33503a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
3351889facbeSLen Brown 
3352fcd17211SLen Brown 	if (debug)
33531b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
3354fcd17211SLen Brown 
3355a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
3356a2b7b749SLen Brown 		calculate_tsc_tweak();
3357a2b7b749SLen Brown 
3358fdf676e5SLen Brown 	do_gfx_rc6_ms = !access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK);
3359fdf676e5SLen Brown 
336027d47356SLen Brown 	do_gfx_mhz = !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK);
336127d47356SLen Brown 
3362889facbeSLen Brown 	return;
3363103a8feaSLen Brown }
3364103a8feaSLen Brown 
3365d8af6f5fSLen Brown void help()
3366103a8feaSLen Brown {
3367b7d8c148SLen Brown 	fprintf(outf,
3368d8af6f5fSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3369d8af6f5fSLen Brown 	"\n"
3370d8af6f5fSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
3371d8af6f5fSLen Brown 	"when COMMAND completes.\n"
3372d8af6f5fSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3373d8af6f5fSLen Brown 	"to print statistics, until interrupted.\n"
3374d8af6f5fSLen Brown 	"--debug	run in \"debug\" mode\n"
3375d8af6f5fSLen Brown 	"--interval sec	Override default 5-second measurement interval\n"
3376d8af6f5fSLen Brown 	"--help		print this help message\n"
3377d8af6f5fSLen Brown 	"--counter msr	print 32-bit counter at address \"msr\"\n"
3378d8af6f5fSLen Brown 	"--Counter msr	print 64-bit Counter at address \"msr\"\n"
3379b7d8c148SLen Brown 	"--out file	create or truncate \"file\" for all output\n"
3380d8af6f5fSLen Brown 	"--msr msr	print 32-bit value at address \"msr\"\n"
3381d8af6f5fSLen Brown 	"--MSR msr	print 64-bit Value at address \"msr\"\n"
3382d8af6f5fSLen Brown 	"--version	print version information\n"
3383d8af6f5fSLen Brown 	"\n"
3384d8af6f5fSLen Brown 	"For more help, run \"man turbostat\"\n");
3385103a8feaSLen Brown }
3386103a8feaSLen Brown 
3387103a8feaSLen Brown 
3388103a8feaSLen Brown /*
3389103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
3390103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
3391103a8feaSLen Brown  */
3392103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
3393103a8feaSLen Brown {
3394103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
3395103a8feaSLen Brown 		return 1;
3396103a8feaSLen Brown 	else
3397103a8feaSLen Brown 		return 0;
3398103a8feaSLen Brown }
3399103a8feaSLen Brown 
3400103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
3401103a8feaSLen Brown {
3402103a8feaSLen Brown 	return 0;
3403103a8feaSLen Brown }
3404103a8feaSLen Brown 
3405c98d5d94SLen Brown void topology_probe()
3406c98d5d94SLen Brown {
3407c98d5d94SLen Brown 	int i;
3408c98d5d94SLen Brown 	int max_core_id = 0;
3409c98d5d94SLen Brown 	int max_package_id = 0;
3410c98d5d94SLen Brown 	int max_siblings = 0;
3411c98d5d94SLen Brown 	struct cpu_topology {
3412c98d5d94SLen Brown 		int core_id;
3413c98d5d94SLen Brown 		int physical_package_id;
3414c98d5d94SLen Brown 	} *cpus;
3415c98d5d94SLen Brown 
3416c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
3417c98d5d94SLen Brown 	topo.num_cpus = 0;
3418c98d5d94SLen Brown 	topo.max_cpu_num = 0;
3419c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
3420c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
3421c98d5d94SLen Brown 		show_cpu = 1;
3422c98d5d94SLen Brown 
3423d8af6f5fSLen Brown 	if (debug > 1)
3424b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
3425c98d5d94SLen Brown 
3426c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
3427b2c95d90SJosh Triplett 	if (cpus == NULL)
3428b2c95d90SJosh Triplett 		err(1, "calloc cpus");
3429c98d5d94SLen Brown 
3430c98d5d94SLen Brown 	/*
3431c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
3432c98d5d94SLen Brown 	 */
3433c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
3434b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
3435b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
3436c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3437c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3438c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
3439c98d5d94SLen Brown 
3440c98d5d94SLen Brown 	/*
3441c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
3442c98d5d94SLen Brown 	 */
3443c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
3444b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
3445b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
3446c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3447c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3448c98d5d94SLen Brown 
3449c98d5d94SLen Brown 
3450c98d5d94SLen Brown 	/*
3451c98d5d94SLen Brown 	 * For online cpus
3452c98d5d94SLen Brown 	 * find max_core_id, max_package_id
3453c98d5d94SLen Brown 	 */
3454c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
3455c98d5d94SLen Brown 		int siblings;
3456c98d5d94SLen Brown 
3457c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
3458d8af6f5fSLen Brown 			if (debug > 1)
3459b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
3460c98d5d94SLen Brown 			continue;
3461c98d5d94SLen Brown 		}
3462c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
3463c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
3464c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
3465c98d5d94SLen Brown 
3466c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
3467c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
3468c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
3469c98d5d94SLen Brown 
3470c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
3471c98d5d94SLen Brown 		if (siblings > max_siblings)
3472c98d5d94SLen Brown 			max_siblings = siblings;
3473d8af6f5fSLen Brown 		if (debug > 1)
3474b7d8c148SLen Brown 			fprintf(outf, "cpu %d pkg %d core %d\n",
3475c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
3476c98d5d94SLen Brown 	}
3477c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
3478d8af6f5fSLen Brown 	if (debug > 1)
3479b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
3480c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
34811cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_cores_per_pkg > 1)
3482c98d5d94SLen Brown 		show_core = 1;
3483c98d5d94SLen Brown 
3484c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
3485d8af6f5fSLen Brown 	if (debug > 1)
3486b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
3487c98d5d94SLen Brown 			max_package_id, topo.num_packages);
34881cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_packages > 1)
3489c98d5d94SLen Brown 		show_pkg = 1;
3490c98d5d94SLen Brown 
3491c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
3492d8af6f5fSLen Brown 	if (debug > 1)
3493b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
3494c98d5d94SLen Brown 
3495c98d5d94SLen Brown 	free(cpus);
3496c98d5d94SLen Brown }
3497c98d5d94SLen Brown 
3498c98d5d94SLen Brown void
3499c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3500c98d5d94SLen Brown {
3501c98d5d94SLen Brown 	int i;
3502c98d5d94SLen Brown 
3503c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3504c98d5d94SLen Brown 		topo.num_packages, sizeof(struct thread_data));
3505c98d5d94SLen Brown 	if (*t == NULL)
3506c98d5d94SLen Brown 		goto error;
3507c98d5d94SLen Brown 
3508c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
3509c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
3510c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
3511c98d5d94SLen Brown 
3512c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
3513c98d5d94SLen Brown 		sizeof(struct core_data));
3514c98d5d94SLen Brown 	if (*c == NULL)
3515c98d5d94SLen Brown 		goto error;
3516c98d5d94SLen Brown 
3517c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3518c98d5d94SLen Brown 		(*c)[i].core_id = -1;
3519c98d5d94SLen Brown 
3520c98d5d94SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
3521c98d5d94SLen Brown 	if (*p == NULL)
3522c98d5d94SLen Brown 		goto error;
3523c98d5d94SLen Brown 
3524c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
3525c98d5d94SLen Brown 		(*p)[i].package_id = i;
3526c98d5d94SLen Brown 
3527c98d5d94SLen Brown 	return;
3528c98d5d94SLen Brown error:
3529b2c95d90SJosh Triplett 	err(1, "calloc counters");
3530c98d5d94SLen Brown }
3531c98d5d94SLen Brown /*
3532c98d5d94SLen Brown  * init_counter()
3533c98d5d94SLen Brown  *
3534c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
3535c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3536c98d5d94SLen Brown  *
3537c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
3538c98d5d94SLen Brown  */
3539c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3540c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
3541c98d5d94SLen Brown 	int pkg_num, int cpu_id)
3542c98d5d94SLen Brown {
3543c98d5d94SLen Brown 	struct thread_data *t;
3544c98d5d94SLen Brown 	struct core_data *c;
3545c98d5d94SLen Brown 	struct pkg_data *p;
3546c98d5d94SLen Brown 
3547c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3548c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
3549c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
3550c98d5d94SLen Brown 
3551c98d5d94SLen Brown 	t->cpu_id = cpu_id;
3552c98d5d94SLen Brown 	if (thread_num == 0) {
3553c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3554c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
3555c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3556c98d5d94SLen Brown 	}
3557c98d5d94SLen Brown 
3558c98d5d94SLen Brown 	c->core_id = core_num;
3559c98d5d94SLen Brown 	p->package_id = pkg_num;
3560c98d5d94SLen Brown }
3561c98d5d94SLen Brown 
3562c98d5d94SLen Brown 
3563c98d5d94SLen Brown int initialize_counters(int cpu_id)
3564c98d5d94SLen Brown {
3565c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
3566c98d5d94SLen Brown 
3567c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
3568c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
3569e275b388SDasaratharaman Chandramouli 	my_thread_id = get_cpu_position_in_core(cpu_id);
3570e275b388SDasaratharaman Chandramouli 	if (!my_thread_id)
3571c98d5d94SLen Brown 		topo.num_cores++;
3572c98d5d94SLen Brown 
3573c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3574c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3575c98d5d94SLen Brown 	return 0;
3576c98d5d94SLen Brown }
3577c98d5d94SLen Brown 
3578c98d5d94SLen Brown void allocate_output_buffer()
3579c98d5d94SLen Brown {
35803b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
3581c98d5d94SLen Brown 	outp = output_buffer;
3582b2c95d90SJosh Triplett 	if (outp == NULL)
3583b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
3584c98d5d94SLen Brown }
358536229897SLen Brown void allocate_fd_percpu(void)
358636229897SLen Brown {
3587*01a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
358836229897SLen Brown 	if (fd_percpu == NULL)
358936229897SLen Brown 		err(-1, "calloc fd_percpu");
359036229897SLen Brown }
3591562a2d37SLen Brown void allocate_irq_buffers(void)
3592562a2d37SLen Brown {
3593562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
3594562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
3595562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
3596c98d5d94SLen Brown 
3597*01a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
3598562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
3599*01a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
3600562a2d37SLen Brown }
3601c98d5d94SLen Brown void setup_all_buffers(void)
3602c98d5d94SLen Brown {
3603c98d5d94SLen Brown 	topology_probe();
3604562a2d37SLen Brown 	allocate_irq_buffers();
360536229897SLen Brown 	allocate_fd_percpu();
3606c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
3607c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
3608c98d5d94SLen Brown 	allocate_output_buffer();
3609c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
3610c98d5d94SLen Brown }
36113b4d5c7fSAndy Shevchenko 
36127ce7d5deSPrarit Bhargava void set_base_cpu(void)
36137ce7d5deSPrarit Bhargava {
36147ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
36157ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
36167ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
36177ce7d5deSPrarit Bhargava 
36187ce7d5deSPrarit Bhargava 	if (debug > 1)
3619b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
36207ce7d5deSPrarit Bhargava }
36217ce7d5deSPrarit Bhargava 
3622103a8feaSLen Brown void turbostat_init()
3623103a8feaSLen Brown {
36247ce7d5deSPrarit Bhargava 	setup_all_buffers();
36257ce7d5deSPrarit Bhargava 	set_base_cpu();
3626103a8feaSLen Brown 	check_dev_msr();
362798481e79SLen Brown 	check_permissions();
3628fcd17211SLen Brown 	process_cpuid();
3629103a8feaSLen Brown 
3630103a8feaSLen Brown 
3631d8af6f5fSLen Brown 	if (debug)
36327f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
36337f5c258eSLen Brown 
36347f5c258eSLen Brown 	if (debug)
3635889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
3636889facbeSLen Brown 
3637d8af6f5fSLen Brown 	if (debug)
36383a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
36393a9a941dSLen Brown 
3640d8af6f5fSLen Brown 	if (debug)
3641889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
3642889facbeSLen Brown 
3643889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
3644889facbeSLen Brown 
3645d8af6f5fSLen Brown 	if (debug)
3646889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
36475a63426eSLen Brown 
36485a63426eSLen Brown 	if (debug && do_irtl_snb)
36495a63426eSLen Brown 		print_irtl();
3650103a8feaSLen Brown }
3651103a8feaSLen Brown 
3652103a8feaSLen Brown int fork_it(char **argv)
3653103a8feaSLen Brown {
3654103a8feaSLen Brown 	pid_t child_pid;
3655d91bb17cSLen Brown 	int status;
3656d15cf7c1SLen Brown 
3657d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
3658d91bb17cSLen Brown 	if (status)
3659d91bb17cSLen Brown 		exit(status);
3660d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
3661d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
3662103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3663103a8feaSLen Brown 
3664103a8feaSLen Brown 	child_pid = fork();
3665103a8feaSLen Brown 	if (!child_pid) {
3666103a8feaSLen Brown 		/* child */
3667103a8feaSLen Brown 		execvp(argv[0], argv);
3668103a8feaSLen Brown 	} else {
3669103a8feaSLen Brown 
3670103a8feaSLen Brown 		/* parent */
3671b2c95d90SJosh Triplett 		if (child_pid == -1)
3672b2c95d90SJosh Triplett 			err(1, "fork");
3673103a8feaSLen Brown 
3674103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
3675103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
3676b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
3677b2c95d90SJosh Triplett 			err(status, "waitpid");
3678103a8feaSLen Brown 	}
3679c98d5d94SLen Brown 	/*
3680c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
3681c98d5d94SLen Brown 	 * because re-starting is problematic when forking
3682c98d5d94SLen Brown 	 */
3683c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
3684103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
3685103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
3686ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
3687ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
3688ba3dec99SLen Brown 	else {
3689c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3690c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3691ba3dec99SLen Brown 	}
3692103a8feaSLen Brown 
3693b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3694b7d8c148SLen Brown 
3695b7d8c148SLen Brown 	flush_output_stderr();
3696103a8feaSLen Brown 
3697d91bb17cSLen Brown 	return status;
3698103a8feaSLen Brown }
3699103a8feaSLen Brown 
37003b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
37013b4d5c7fSAndy Shevchenko {
37023b4d5c7fSAndy Shevchenko 	int status;
37033b4d5c7fSAndy Shevchenko 
37043b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
37053b4d5c7fSAndy Shevchenko 	if (status)
37063b4d5c7fSAndy Shevchenko 		return status;
37073b4d5c7fSAndy Shevchenko 
37083b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
37093b4d5c7fSAndy Shevchenko 	if (status)
37103b4d5c7fSAndy Shevchenko 		return status;
37113b4d5c7fSAndy Shevchenko 
3712b7d8c148SLen Brown 	flush_output_stdout();
37133b4d5c7fSAndy Shevchenko 
37143b4d5c7fSAndy Shevchenko 	return status;
37153b4d5c7fSAndy Shevchenko }
37163b4d5c7fSAndy Shevchenko 
3717d8af6f5fSLen Brown void print_version() {
37183d109de2SLen Brown 	fprintf(outf, "turbostat version 4.14 22 Apr 2016"
3719d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
3720d8af6f5fSLen Brown }
3721d8af6f5fSLen Brown 
3722103a8feaSLen Brown void cmdline(int argc, char **argv)
3723103a8feaSLen Brown {
3724103a8feaSLen Brown 	int opt;
3725d8af6f5fSLen Brown 	int option_index = 0;
3726d8af6f5fSLen Brown 	static struct option long_options[] = {
3727d8af6f5fSLen Brown 		{"Counter",	required_argument,	0, 'C'},
3728d8af6f5fSLen Brown 		{"counter",	required_argument,	0, 'c'},
3729d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
3730d8af6f5fSLen Brown 		{"debug",	no_argument,		0, 'd'},
3731d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
3732d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
3733d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
3734d8af6f5fSLen Brown 		{"MSR",		required_argument,	0, 'M'},
3735d8af6f5fSLen Brown 		{"msr",		required_argument,	0, 'm'},
3736b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
3737d8af6f5fSLen Brown 		{"Package",	no_argument,		0, 'p'},
3738d8af6f5fSLen Brown 		{"processor",	no_argument,		0, 'p'},
3739d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
3740d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
3741d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
3742d8af6f5fSLen Brown 		{0,		0,			0,  0 }
3743d8af6f5fSLen Brown 	};
3744103a8feaSLen Brown 
3745103a8feaSLen Brown 	progname = argv[0];
3746103a8feaSLen Brown 
3747b7d8c148SLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
3748d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
3749103a8feaSLen Brown 		switch (opt) {
3750d8af6f5fSLen Brown 		case 'C':
3751d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset64);
3752103a8feaSLen Brown 			break;
3753f9240813SLen Brown 		case 'c':
37548e180f3cSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset32);
37558e180f3cSLen Brown 			break;
3756d8af6f5fSLen Brown 		case 'D':
3757d8af6f5fSLen Brown 			dump_only++;
37588e180f3cSLen Brown 			break;
3759d8af6f5fSLen Brown 		case 'd':
3760d8af6f5fSLen Brown 			debug++;
37612f32edf1SLen Brown 			break;
3762d8af6f5fSLen Brown 		case 'h':
3763d8af6f5fSLen Brown 		default:
3764d8af6f5fSLen Brown 			help();
3765d8af6f5fSLen Brown 			exit(1);
3766d8af6f5fSLen Brown 		case 'i':
37672a0609c0SLen Brown 			{
37682a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
37692a0609c0SLen Brown 
37702a0609c0SLen Brown 				if (interval < 0.001) {
3771b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
37722a0609c0SLen Brown 						interval);
37732a0609c0SLen Brown 					exit(2);
37742a0609c0SLen Brown 				}
37752a0609c0SLen Brown 
37762a0609c0SLen Brown 				interval_ts.tv_sec = interval;
37772a0609c0SLen Brown 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
37782a0609c0SLen Brown 			}
3779889facbeSLen Brown 			break;
37805c56be9aSDirk Brandewie 		case 'J':
37815c56be9aSDirk Brandewie 			rapl_joules++;
37825c56be9aSDirk Brandewie 			break;
3783d8af6f5fSLen Brown 		case 'M':
3784d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_msr_offset64);
3785d8af6f5fSLen Brown 			break;
3786d8af6f5fSLen Brown 		case 'm':
3787d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_msr_offset32);
3788d8af6f5fSLen Brown 			break;
3789b7d8c148SLen Brown 		case 'o':
3790b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
3791b7d8c148SLen Brown 			break;
3792d8af6f5fSLen Brown 		case 'P':
3793d8af6f5fSLen Brown 			show_pkg_only++;
3794d8af6f5fSLen Brown 			break;
3795d8af6f5fSLen Brown 		case 'p':
3796d8af6f5fSLen Brown 			show_core_only++;
3797d8af6f5fSLen Brown 			break;
3798d8af6f5fSLen Brown 		case 'S':
3799d8af6f5fSLen Brown 			summary_only++;
3800d8af6f5fSLen Brown 			break;
3801d8af6f5fSLen Brown 		case 'T':
3802d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
3803d8af6f5fSLen Brown 			break;
3804d8af6f5fSLen Brown 		case 'v':
3805d8af6f5fSLen Brown 			print_version();
3806d8af6f5fSLen Brown 			exit(0);
3807d8af6f5fSLen Brown 			break;
3808103a8feaSLen Brown 		}
3809103a8feaSLen Brown 	}
3810103a8feaSLen Brown }
3811103a8feaSLen Brown 
3812103a8feaSLen Brown int main(int argc, char **argv)
3813103a8feaSLen Brown {
3814b7d8c148SLen Brown 	outf = stderr;
3815b7d8c148SLen Brown 
3816103a8feaSLen Brown 	cmdline(argc, argv);
3817103a8feaSLen Brown 
3818d8af6f5fSLen Brown 	if (debug)
3819d8af6f5fSLen Brown 		print_version();
3820103a8feaSLen Brown 
3821103a8feaSLen Brown 	turbostat_init();
3822103a8feaSLen Brown 
38233b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
38243b4d5c7fSAndy Shevchenko 	if (dump_only)
38253b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
38263b4d5c7fSAndy Shevchenko 
3827103a8feaSLen Brown 	/*
3828103a8feaSLen Brown 	 * if any params left, it must be a command to fork
3829103a8feaSLen Brown 	 */
3830103a8feaSLen Brown 	if (argc - optind)
3831103a8feaSLen Brown 		return fork_it(argv + optind);
3832103a8feaSLen Brown 	else
3833103a8feaSLen Brown 		turbostat_loop();
3834103a8feaSLen Brown 
3835103a8feaSLen Brown 	return 0;
3836103a8feaSLen Brown }
3837