xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision ac980e1357244299f4c929d9f2c45428b35eeb86)
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
24869ce69eSLen Brown #include INTEL_FAMILY_HEADER
2595aebc44SJosh Triplett #include <stdarg.h>
26103a8feaSLen Brown #include <stdio.h>
27b2c95d90SJosh Triplett #include <err.h>
28103a8feaSLen Brown #include <unistd.h>
29103a8feaSLen Brown #include <sys/types.h>
30103a8feaSLen Brown #include <sys/wait.h>
31103a8feaSLen Brown #include <sys/stat.h>
32b9ad8ee0SLen Brown #include <sys/select.h>
33103a8feaSLen Brown #include <sys/resource.h>
34103a8feaSLen Brown #include <fcntl.h>
35103a8feaSLen Brown #include <signal.h>
36103a8feaSLen Brown #include <sys/time.h>
37103a8feaSLen Brown #include <stdlib.h>
38d8af6f5fSLen Brown #include <getopt.h>
39103a8feaSLen Brown #include <dirent.h>
40103a8feaSLen Brown #include <string.h>
41103a8feaSLen Brown #include <ctype.h>
4288c3281fSLen Brown #include <sched.h>
432a0609c0SLen Brown #include <time.h>
442b92865eSJosh Triplett #include <cpuid.h>
4598481e79SLen Brown #include <linux/capability.h>
4698481e79SLen Brown #include <errno.h>
47103a8feaSLen Brown 
48103a8feaSLen Brown char *proc_stat = "/proc/stat";
49b7d8c148SLen Brown FILE *outf;
5036229897SLen Brown int *fd_percpu;
51b9ad8ee0SLen Brown struct timeval interval_tv = {5, 0};
5247936f94SArtem Bityutskiy struct timespec interval_ts = {5, 0};
53b9ad8ee0SLen Brown struct timespec one_msec = {0, 1000000};
54d8af6f5fSLen Brown unsigned int debug;
5596e47158SLen Brown unsigned int quiet;
563f44a5c6SLen Brown unsigned int shown;
570de6c0dfSLen Brown unsigned int sums_need_wide_columns;
58d8af6f5fSLen Brown unsigned int rapl_joules;
59d8af6f5fSLen Brown unsigned int summary_only;
60c8ade361SLen Brown unsigned int list_header_only;
61d8af6f5fSLen Brown unsigned int dump_only;
62103a8feaSLen Brown unsigned int do_snb_cstates;
63fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
64144b44b1SLen Brown unsigned int do_slm_cstates;
65144b44b1SLen Brown unsigned int use_c1_residency_msr;
66103a8feaSLen Brown unsigned int has_aperf;
67889facbeSLen Brown unsigned int has_epb;
685a63426eSLen Brown unsigned int do_irtl_snb;
695a63426eSLen Brown unsigned int do_irtl_hsw;
70fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
71103a8feaSLen Brown unsigned int genuine_intel;
72103a8feaSLen Brown unsigned int has_invariant_tsc;
73d7899447SLen Brown unsigned int do_nhm_platform_info;
74cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
75b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
76103a8feaSLen Brown double bclk;
77a2b7b749SLen Brown double base_hz;
7821ed5574SLen Brown unsigned int has_base_hz;
79a2b7b749SLen Brown double tsc_tweak = 1.0;
80c98d5d94SLen Brown unsigned int show_pkg_only;
81c98d5d94SLen Brown unsigned int show_core_only;
82c98d5d94SLen Brown char *output_buffer, *outp;
83889facbeSLen Brown unsigned int do_rapl;
84889facbeSLen Brown unsigned int do_dts;
85889facbeSLen Brown unsigned int do_ptm;
86fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
8727d47356SLen Brown unsigned int gfx_cur_mhz;
88889facbeSLen Brown unsigned int tcc_activation_temp;
89889facbeSLen Brown unsigned int tcc_activation_temp_override;
9040ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
9140ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
92889facbeSLen Brown double rapl_joule_counter_range;
933a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
94*ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
953a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
963a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
978a5bdf41SLen Brown unsigned int crystal_hz;
988a5bdf41SLen Brown unsigned long long tsc_hz;
997ce7d5deSPrarit Bhargava int base_cpu;
10021ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
1017f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
1027f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
1037f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
1047f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1057f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1067f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
10733148d67SLen Brown unsigned int has_misc_feature_control;
108889facbeSLen Brown 
109889facbeSLen Brown #define RAPL_PKG		(1 << 0)
110e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
111e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
112e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
113e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
114e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
115e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
116e6f9bb3cSLen Brown 
117889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
118e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
119e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
120e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
121e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1220b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1230b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
124e6f9bb3cSLen Brown 
1259148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
126e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1270b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
128e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
129e6f9bb3cSLen Brown 
1300b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
131e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
132e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
133e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1349148494cSJacob Pan 
1359148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1369148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1379148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
138889facbeSLen Brown #define	TJMAX_DEFAULT	100
139889facbeSLen Brown 
140889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
141103a8feaSLen Brown 
142388e9c81SLen Brown /*
143388e9c81SLen Brown  * buffer size used by sscanf() for added column names
144388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
145388e9c81SLen Brown  */
146388e9c81SLen Brown #define	NAME_BYTES 20
147495c7654SLen Brown #define PATH_BYTES 128
148388e9c81SLen Brown 
149103a8feaSLen Brown int backwards_count;
150103a8feaSLen Brown char *progname;
151103a8feaSLen Brown 
1521ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
1531ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
1541ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
155678a3bd1SLen Brown #define MAX_ADDED_COUNTERS 16
156c98d5d94SLen Brown 
157c98d5d94SLen Brown struct thread_data {
158f4fdf2b4SLen Brown 	struct timeval tv_begin;
159f4fdf2b4SLen Brown 	struct timeval tv_end;
160c98d5d94SLen Brown 	unsigned long long tsc;
161c98d5d94SLen Brown 	unsigned long long aperf;
162c98d5d94SLen Brown 	unsigned long long mperf;
163144b44b1SLen Brown 	unsigned long long c1;
1640de6c0dfSLen Brown 	unsigned long long  irq_count;
1651ed51011SLen Brown 	unsigned int smi_count;
166c98d5d94SLen Brown 	unsigned int cpu_id;
167c98d5d94SLen Brown 	unsigned int flags;
168c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
169c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
170678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
171c98d5d94SLen Brown } *thread_even, *thread_odd;
172c98d5d94SLen Brown 
173c98d5d94SLen Brown struct core_data {
174c98d5d94SLen Brown 	unsigned long long c3;
175c98d5d94SLen Brown 	unsigned long long c6;
176c98d5d94SLen Brown 	unsigned long long c7;
1770539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
178889facbeSLen Brown 	unsigned int core_temp_c;
179c98d5d94SLen Brown 	unsigned int core_id;
180678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
181c98d5d94SLen Brown } *core_even, *core_odd;
182c98d5d94SLen Brown 
183c98d5d94SLen Brown struct pkg_data {
184c98d5d94SLen Brown 	unsigned long long pc2;
185c98d5d94SLen Brown 	unsigned long long pc3;
186c98d5d94SLen Brown 	unsigned long long pc6;
187c98d5d94SLen Brown 	unsigned long long pc7;
188ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
189ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
190ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1910b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1920b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1930b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1940b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
1959185e988SLen Brown 	long long gfx_rc6_ms;
19627d47356SLen Brown 	unsigned int gfx_mhz;
197c98d5d94SLen Brown 	unsigned int package_id;
198889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
199889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
200889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
201889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
202889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
203889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
204889facbeSLen Brown 	unsigned int pkg_temp_c;
205678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
206c98d5d94SLen Brown } *package_even, *package_odd;
207c98d5d94SLen Brown 
208c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
209c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
210c98d5d94SLen Brown 
211c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
212c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
213c98d5d94SLen Brown 		topo.num_threads_per_core + \
214c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
215c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
216c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
217c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
218c98d5d94SLen Brown 
219388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
22041618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
221388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
222388e9c81SLen Brown 
223388e9c81SLen Brown struct msr_counter {
224388e9c81SLen Brown 	unsigned int msr_num;
225388e9c81SLen Brown 	char name[NAME_BYTES];
226495c7654SLen Brown 	char path[PATH_BYTES];
227388e9c81SLen Brown 	unsigned int width;
228388e9c81SLen Brown 	enum counter_type type;
229388e9c81SLen Brown 	enum counter_format format;
230388e9c81SLen Brown 	struct msr_counter *next;
231812db3f7SLen Brown 	unsigned int flags;
232812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
233812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
23441618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
235388e9c81SLen Brown };
236388e9c81SLen Brown 
237388e9c81SLen Brown struct sys_counters {
238678a3bd1SLen Brown 	unsigned int added_thread_counters;
239678a3bd1SLen Brown 	unsigned int added_core_counters;
240678a3bd1SLen Brown 	unsigned int added_package_counters;
241388e9c81SLen Brown 	struct msr_counter *tp;
242388e9c81SLen Brown 	struct msr_counter *cp;
243388e9c81SLen Brown 	struct msr_counter *pp;
244388e9c81SLen Brown } sys;
245388e9c81SLen Brown 
246c98d5d94SLen Brown struct system_summary {
247c98d5d94SLen Brown 	struct thread_data threads;
248c98d5d94SLen Brown 	struct core_data cores;
249c98d5d94SLen Brown 	struct pkg_data packages;
250388e9c81SLen Brown } average;
251c98d5d94SLen Brown 
252c98d5d94SLen Brown 
253c98d5d94SLen Brown struct topo_params {
254c98d5d94SLen Brown 	int num_packages;
255103a8feaSLen Brown 	int num_cpus;
256c98d5d94SLen Brown 	int num_cores;
257c98d5d94SLen Brown 	int max_cpu_num;
258c98d5d94SLen Brown 	int num_cores_per_pkg;
259c98d5d94SLen Brown 	int num_threads_per_core;
260c98d5d94SLen Brown } topo;
261103a8feaSLen Brown 
262c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
263103a8feaSLen Brown 
264562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
265562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
266562a2d37SLen Brown 
267c98d5d94SLen Brown void setup_all_buffers(void);
268103a8feaSLen Brown 
269c98d5d94SLen Brown int cpu_is_not_present(int cpu)
270d15cf7c1SLen Brown {
271c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
272c98d5d94SLen Brown }
273c98d5d94SLen Brown /*
274c98d5d94SLen Brown  * run func(thread, core, package) in topology order
275c98d5d94SLen Brown  * skip non-present cpus
276c98d5d94SLen Brown  */
277c98d5d94SLen Brown 
278c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
279c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
280c98d5d94SLen Brown {
281c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
282c98d5d94SLen Brown 
283c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
284c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
285c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
286c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
287c98d5d94SLen Brown 				struct thread_data *t;
288c98d5d94SLen Brown 				struct core_data *c;
289c98d5d94SLen Brown 				struct pkg_data *p;
290c98d5d94SLen Brown 
291c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
292c98d5d94SLen Brown 
293c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
294c98d5d94SLen Brown 					continue;
295c98d5d94SLen Brown 
296c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
297c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
298c98d5d94SLen Brown 
299c98d5d94SLen Brown 				retval = func(t, c, p);
300c98d5d94SLen Brown 				if (retval)
301c98d5d94SLen Brown 					return retval;
302c98d5d94SLen Brown 			}
303c98d5d94SLen Brown 		}
304c98d5d94SLen Brown 	}
305d15cf7c1SLen Brown 	return 0;
306d15cf7c1SLen Brown }
307d15cf7c1SLen Brown 
30888c3281fSLen Brown int cpu_migrate(int cpu)
30988c3281fSLen Brown {
310c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
311c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
312c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
31388c3281fSLen Brown 		return -1;
31488c3281fSLen Brown 	else
31588c3281fSLen Brown 		return 0;
31688c3281fSLen Brown }
31736229897SLen Brown int get_msr_fd(int cpu)
318103a8feaSLen Brown {
319103a8feaSLen Brown 	char pathname[32];
320103a8feaSLen Brown 	int fd;
321103a8feaSLen Brown 
32236229897SLen Brown 	fd = fd_percpu[cpu];
32336229897SLen Brown 
32436229897SLen Brown 	if (fd)
32536229897SLen Brown 		return fd;
32636229897SLen Brown 
327103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
328103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
32915aaa346SLen Brown 	if (fd < 0)
33098481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
331103a8feaSLen Brown 
33236229897SLen Brown 	fd_percpu[cpu] = fd;
33336229897SLen Brown 
33436229897SLen Brown 	return fd;
33536229897SLen Brown }
33636229897SLen Brown 
33736229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
33836229897SLen Brown {
33936229897SLen Brown 	ssize_t retval;
34036229897SLen Brown 
34136229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
34215aaa346SLen Brown 
34398481e79SLen Brown 	if (retval != sizeof *msr)
344cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
34515aaa346SLen Brown 
34615aaa346SLen Brown 	return 0;
347103a8feaSLen Brown }
348103a8feaSLen Brown 
349fc04cc67SLen Brown /*
350812db3f7SLen Brown  * Each string in this array is compared in --show and --hide cmdline.
351812db3f7SLen Brown  * Thus, strings that are proper sub-sets must follow their more specific peers.
352fc04cc67SLen Brown  */
353812db3f7SLen Brown struct msr_counter bic[] = {
3543f44a5c6SLen Brown 	{ 0x0, "usec" },
3553f44a5c6SLen Brown 	{ 0x0, "Time_Of_Day_Seconds" },
356812db3f7SLen Brown 	{ 0x0, "Package" },
357812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
358812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
359812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
360812db3f7SLen Brown 	{ 0x0, "IRQ" },
361495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
362812db3f7SLen Brown 	{ 0x0, "Busy%" },
363812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
364812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
365812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
366812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
367812db3f7SLen Brown 	{ 0x0, "ThreadC" },
368812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
369812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
370812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
371812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
372812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
373812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
374812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
375812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
376812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
3770f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
3780f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
3790f47c08dSLen Brown 	{ 0x0, "Pkg%pc10" },
380812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
381812db3f7SLen Brown 	{ 0x0, "CorWatt" },
382812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
383812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
384812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
385812db3f7SLen Brown 	{ 0x0, "PKG_%" },
386812db3f7SLen Brown 	{ 0x0, "RAM_%" },
387812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
388812db3f7SLen Brown 	{ 0x0, "Cor_J" },
389812db3f7SLen Brown 	{ 0x0, "GFX_J" },
390812db3f7SLen Brown 	{ 0x0, "RAM_J" },
391812db3f7SLen Brown 	{ 0x0, "Core" },
392812db3f7SLen Brown 	{ 0x0, "CPU" },
3930539ba11SLen Brown 	{ 0x0, "Mod%c6" },
39441618e63SLen Brown 	{ 0x0, "sysfs" },
395a99d8730SLen Brown 	{ 0x0, "Totl%C0" },
396a99d8730SLen Brown 	{ 0x0, "Any%C0" },
397a99d8730SLen Brown 	{ 0x0, "GFX%C0" },
398a99d8730SLen Brown 	{ 0x0, "CPUGFX%" },
399812db3f7SLen Brown };
400812db3f7SLen Brown 
401a99d8730SLen Brown 
402a99d8730SLen Brown 
403812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
4043f44a5c6SLen Brown #define	BIC_USEC	(1ULL << 0)
4053f44a5c6SLen Brown #define	BIC_TOD		(1ULL << 1)
4063f44a5c6SLen Brown #define	BIC_Package	(1ULL << 2)
4073f44a5c6SLen Brown #define	BIC_Avg_MHz	(1ULL << 3)
4083f44a5c6SLen Brown #define	BIC_Bzy_MHz	(1ULL << 4)
4093f44a5c6SLen Brown #define	BIC_TSC_MHz	(1ULL << 5)
4103f44a5c6SLen Brown #define	BIC_IRQ		(1ULL << 6)
4113f44a5c6SLen Brown #define	BIC_SMI		(1ULL << 7)
4123f44a5c6SLen Brown #define	BIC_Busy	(1ULL << 8)
4133f44a5c6SLen Brown #define	BIC_CPU_c1	(1ULL << 9)
4143f44a5c6SLen Brown #define	BIC_CPU_c3	(1ULL << 10)
4153f44a5c6SLen Brown #define	BIC_CPU_c6	(1ULL << 11)
4163f44a5c6SLen Brown #define	BIC_CPU_c7	(1ULL << 12)
4173f44a5c6SLen Brown #define	BIC_ThreadC	(1ULL << 13)
4183f44a5c6SLen Brown #define	BIC_CoreTmp	(1ULL << 14)
4193f44a5c6SLen Brown #define	BIC_CoreCnt	(1ULL << 15)
4203f44a5c6SLen Brown #define	BIC_PkgTmp	(1ULL << 16)
4213f44a5c6SLen Brown #define	BIC_GFX_rc6	(1ULL << 17)
4223f44a5c6SLen Brown #define	BIC_GFXMHz	(1ULL << 18)
4233f44a5c6SLen Brown #define	BIC_Pkgpc2	(1ULL << 19)
4243f44a5c6SLen Brown #define	BIC_Pkgpc3	(1ULL << 20)
4253f44a5c6SLen Brown #define	BIC_Pkgpc6	(1ULL << 21)
4263f44a5c6SLen Brown #define	BIC_Pkgpc7	(1ULL << 22)
4273f44a5c6SLen Brown #define	BIC_Pkgpc8	(1ULL << 23)
4283f44a5c6SLen Brown #define	BIC_Pkgpc9	(1ULL << 24)
4293f44a5c6SLen Brown #define	BIC_Pkgpc10	(1ULL << 25)
4303f44a5c6SLen Brown #define	BIC_PkgWatt	(1ULL << 26)
4313f44a5c6SLen Brown #define	BIC_CorWatt	(1ULL << 27)
4323f44a5c6SLen Brown #define	BIC_GFXWatt	(1ULL << 28)
4333f44a5c6SLen Brown #define	BIC_PkgCnt	(1ULL << 29)
4343f44a5c6SLen Brown #define	BIC_RAMWatt	(1ULL << 30)
4353f44a5c6SLen Brown #define	BIC_PKG__	(1ULL << 31)
4363f44a5c6SLen Brown #define	BIC_RAM__	(1ULL << 32)
4373f44a5c6SLen Brown #define	BIC_Pkg_J	(1ULL << 33)
4383f44a5c6SLen Brown #define	BIC_Cor_J	(1ULL << 34)
4393f44a5c6SLen Brown #define	BIC_GFX_J	(1ULL << 35)
4403f44a5c6SLen Brown #define	BIC_RAM_J	(1ULL << 36)
4413f44a5c6SLen Brown #define	BIC_Core	(1ULL << 37)
4423f44a5c6SLen Brown #define	BIC_CPU		(1ULL << 38)
4433f44a5c6SLen Brown #define	BIC_Mod_c6	(1ULL << 39)
4443f44a5c6SLen Brown #define	BIC_sysfs	(1ULL << 40)
4453f44a5c6SLen Brown #define	BIC_Totl_c0	(1ULL << 41)
4463f44a5c6SLen Brown #define	BIC_Any_c0	(1ULL << 42)
4473f44a5c6SLen Brown #define	BIC_GFX_c0	(1ULL << 43)
4483f44a5c6SLen Brown #define	BIC_CPUGFX	(1ULL << 44)
449812db3f7SLen Brown 
4503f44a5c6SLen Brown #define BIC_DISABLED_BY_DEFAULT	(BIC_USEC | BIC_TOD)
4513f44a5c6SLen Brown 
4523f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
4533f44a5c6SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
454812db3f7SLen Brown 
455812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
4563f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
457812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
4580f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
459812db3f7SLen Brown 
4603f44a5c6SLen Brown 
461dd778a5eSLen Brown #define MAX_DEFERRED 16
462dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
463dd778a5eSLen Brown int deferred_skip_index;
464dd778a5eSLen Brown 
465dd778a5eSLen Brown /*
466dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
467dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
468dd778a5eSLen Brown  */
469dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
470dd778a5eSLen Brown 
471dd778a5eSLen Brown void help(void)
472dd778a5eSLen Brown {
473dd778a5eSLen Brown 	fprintf(outf,
474dd778a5eSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
475dd778a5eSLen Brown 	"\n"
476dd778a5eSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
477dd778a5eSLen Brown 	"when COMMAND completes.\n"
478dd778a5eSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
479dd778a5eSLen Brown 	"to print statistics, until interrupted.\n"
480dd778a5eSLen Brown 	"--add		add a counter\n"
481dd778a5eSLen Brown 	"		eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
482dd778a5eSLen Brown 	"--cpu	cpu-set	limit output to summary plus cpu-set:\n"
483dd778a5eSLen Brown 	"		{core | package | j,k,l..m,n-p }\n"
484dd778a5eSLen Brown 	"--quiet	skip decoding system configuration header\n"
485b9ad8ee0SLen Brown 	"--interval sec.subsec	Override default 5-second measurement interval\n"
486dd778a5eSLen Brown 	"--help		print this help message\n"
487dd778a5eSLen Brown 	"--list		list column headers only\n"
488dd778a5eSLen Brown 	"--out file	create or truncate \"file\" for all output\n"
489dd778a5eSLen Brown 	"--version	print version information\n"
490dd778a5eSLen Brown 	"\n"
491dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
492dd778a5eSLen Brown }
493dd778a5eSLen Brown 
494812db3f7SLen Brown /*
495812db3f7SLen Brown  * bic_lookup
496812db3f7SLen Brown  * for all the strings in comma separate name_list,
497812db3f7SLen Brown  * set the approprate bit in return value.
498812db3f7SLen Brown  */
499dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
500812db3f7SLen Brown {
501812db3f7SLen Brown 	int i;
502812db3f7SLen Brown 	unsigned long long retval = 0;
503812db3f7SLen Brown 
504812db3f7SLen Brown 	while (name_list) {
505812db3f7SLen Brown 		char *comma;
506812db3f7SLen Brown 
507812db3f7SLen Brown 		comma = strchr(name_list, ',');
508812db3f7SLen Brown 
509812db3f7SLen Brown 		if (comma)
510812db3f7SLen Brown 			*comma = '\0';
511812db3f7SLen Brown 
5123f44a5c6SLen Brown 		if (!strcmp(name_list, "all"))
5133f44a5c6SLen Brown 			return ~0;
5143f44a5c6SLen Brown 
515812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
516812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
517812db3f7SLen Brown 				retval |= (1ULL << i);
518812db3f7SLen Brown 				break;
519812db3f7SLen Brown 			}
520812db3f7SLen Brown 		}
521812db3f7SLen Brown 		if (i == MAX_BIC) {
522dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
523812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
524812db3f7SLen Brown 				exit(-1);
525812db3f7SLen Brown 			}
526dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
527dd778a5eSLen Brown 			if (debug)
528dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
529dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
530dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
531dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
532dd778a5eSLen Brown 				help();
533dd778a5eSLen Brown 				exit(1);
534dd778a5eSLen Brown 			}
535dd778a5eSLen Brown 		}
536812db3f7SLen Brown 
537812db3f7SLen Brown 		name_list = comma;
538812db3f7SLen Brown 		if (name_list)
539812db3f7SLen Brown 			name_list++;
540812db3f7SLen Brown 
541812db3f7SLen Brown 	}
542812db3f7SLen Brown 	return retval;
543812db3f7SLen Brown }
544fc04cc67SLen Brown 
545dd778a5eSLen Brown 
546c8ade361SLen Brown void print_header(char *delim)
547103a8feaSLen Brown {
548388e9c81SLen Brown 	struct msr_counter *mp;
5496168c2e0SLen Brown 	int printed = 0;
550388e9c81SLen Brown 
5513f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
5523f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
5533f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
5543f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
555812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
5566168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
557812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
5586168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
559812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
5606168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
561812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
5626168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
563812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
5646168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
565812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
5666168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
567812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
5686168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
5691cc21f7bSLen Brown 
5700de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
5710de6c0dfSLen Brown 		if (sums_need_wide_columns)
5726168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
5730de6c0dfSLen Brown 		else
5746168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
5750de6c0dfSLen Brown 	}
5760de6c0dfSLen Brown 
577812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
5786168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
5791cc21f7bSLen Brown 
580388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
581dd778a5eSLen Brown 
582388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
583388e9c81SLen Brown 			if (mp->width == 64)
584dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
585388e9c81SLen Brown 			else
586dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
587388e9c81SLen Brown 		} else {
5880de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
589dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
5900de6c0dfSLen Brown 			else
591dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
592388e9c81SLen Brown 		}
593388e9c81SLen Brown 	}
594388e9c81SLen Brown 
59541618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
5966168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
597812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
5986168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
599812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
6006168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
601812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
6026168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
603678a3bd1SLen Brown 
6040539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
6056168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
606678a3bd1SLen Brown 
607812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
6086168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
609388e9c81SLen Brown 
610388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
611388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
612388e9c81SLen Brown 			if (mp->width == 64)
613c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
614388e9c81SLen Brown 			else
615c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
616388e9c81SLen Brown 		} else {
6170de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6180de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6190de6c0dfSLen Brown 			else
620c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
621388e9c81SLen Brown 		}
622388e9c81SLen Brown 	}
623388e9c81SLen Brown 
624812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
6256168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
626889facbeSLen Brown 
627812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
6286168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
629fdf676e5SLen Brown 
630812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
6316168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
63227d47356SLen Brown 
633a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
6346168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
635a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
6366168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
637a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
6386168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
639a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
6406168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
6410b2bb692SLen Brown 
6420f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
6436168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
6440f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
6456168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
6460f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
6476168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
6480f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
6496168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
6500f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
6516168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
6520f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
6536168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
6540f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
6556168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
656103a8feaSLen Brown 
6575c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
658812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
6596168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
660812db3f7SLen Brown 		if (DO_BIC(BIC_CorWatt))
6616168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
662812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
6636168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
664812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
6656168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
666812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
6676168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
668812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
6696168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
670d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
671812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
6726168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
673812db3f7SLen Brown 		if (DO_BIC(BIC_Cor_J))
6746168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
675812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
6766168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
677812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
6786168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
679812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
6806168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
681812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
6826168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
6835c56be9aSDirk Brandewie 	}
684388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
685388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
686388e9c81SLen Brown 			if (mp->width == 64)
687c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
688388e9c81SLen Brown 			else
689c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
690388e9c81SLen Brown 		} else {
6910de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6920de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6930de6c0dfSLen Brown 			else
694c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
695388e9c81SLen Brown 		}
696388e9c81SLen Brown 	}
697388e9c81SLen Brown 
698c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
699103a8feaSLen Brown }
700103a8feaSLen Brown 
701c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
702c98d5d94SLen Brown 	struct pkg_data *p)
703103a8feaSLen Brown {
704388e9c81SLen Brown 	int i;
705388e9c81SLen Brown 	struct msr_counter *mp;
706388e9c81SLen Brown 
7073b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
708c98d5d94SLen Brown 
709c98d5d94SLen Brown 	if (t) {
7103b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
7113b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
7123b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
7133b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
7143b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
7153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
7166886fee4SLen Brown 
717812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
7180de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
719812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
720218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
721388e9c81SLen Brown 
722388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
723388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
724388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
725388e9c81SLen Brown 		}
726103a8feaSLen Brown 	}
727103a8feaSLen Brown 
728c98d5d94SLen Brown 	if (c) {
7293b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
7303b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
7313b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
7323b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
7333b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
734388e9c81SLen Brown 
735388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
736388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
737388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
738388e9c81SLen Brown 		}
7390539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
740c98d5d94SLen Brown 	}
741103a8feaSLen Brown 
742c98d5d94SLen Brown 	if (p) {
7433b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
7440b2bb692SLen Brown 
7450b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
7460b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
7470b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
7480b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
7490b2bb692SLen Brown 
7503b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
7510f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
7523b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
7530f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
7543b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
7550f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
7563b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
7573b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
7583b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
7593b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
7603b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
7613b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
7623b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
7633b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
7643b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
7653b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
7663b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
7673b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
7683b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
769388e9c81SLen Brown 
770388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
771388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
772388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
773388e9c81SLen Brown 		}
774c98d5d94SLen Brown 	}
7753b4d5c7fSAndy Shevchenko 
7763b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
7773b4d5c7fSAndy Shevchenko 
778c98d5d94SLen Brown 	return 0;
779103a8feaSLen Brown }
780103a8feaSLen Brown 
781e23da037SLen Brown /*
782e23da037SLen Brown  * column formatting convention & formats
783e23da037SLen Brown  */
784c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
785c98d5d94SLen Brown 	struct pkg_data *p)
786103a8feaSLen Brown {
787008d396eSLen Brown 	double interval_float, tsc;
788fc04cc67SLen Brown 	char *fmt8;
789388e9c81SLen Brown 	int i;
790388e9c81SLen Brown 	struct msr_counter *mp;
7916168c2e0SLen Brown 	char *delim = "\t";
7926168c2e0SLen Brown 	int printed = 0;
793103a8feaSLen Brown 
794c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
795c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
796c98d5d94SLen Brown 		return 0;
797c98d5d94SLen Brown 
798c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
799c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
800c98d5d94SLen Brown 		return 0;
801c98d5d94SLen Brown 
8021ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
8031ef7d21aSLen Brown 	if ((t != &average.threads) &&
8041ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
8051ef7d21aSLen Brown 		return 0;
8061ef7d21aSLen Brown 
8073f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
808f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
809f4fdf2b4SLen Brown 		struct timeval tv;
810f4fdf2b4SLen Brown 
811f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
812f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
813f4fdf2b4SLen Brown 	}
814f4fdf2b4SLen Brown 
8153f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
8163f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
8173f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
8183f44a5c6SLen Brown 
819103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
820103a8feaSLen Brown 
821008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
822008d396eSLen Brown 
823c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
824c98d5d94SLen Brown 	if (t == &average.threads) {
825812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
8266168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
827812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
8286168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
829812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
8306168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
831103a8feaSLen Brown 	} else {
832812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
833c98d5d94SLen Brown 			if (p)
8346168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
835c98d5d94SLen Brown 			else
8366168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
837c98d5d94SLen Brown 		}
838812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
839c98d5d94SLen Brown 			if (c)
8406168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
841c98d5d94SLen Brown 			else
8426168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
843c98d5d94SLen Brown 		}
844812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
8456168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
846103a8feaSLen Brown 	}
847fc04cc67SLen Brown 
848812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
8496168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
850fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
851fc04cc67SLen Brown 
852812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
8536168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
854103a8feaSLen Brown 
855812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
85621ed5574SLen Brown 		if (has_base_hz)
8576168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
85821ed5574SLen Brown 		else
8596168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
860008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
86121ed5574SLen Brown 	}
862103a8feaSLen Brown 
863812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
8646168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
865103a8feaSLen Brown 
866562a2d37SLen Brown 	/* IRQ */
8670de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
8680de6c0dfSLen Brown 		if (sums_need_wide_columns)
8696168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
8700de6c0dfSLen Brown 		else
8716168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
8720de6c0dfSLen Brown 	}
873562a2d37SLen Brown 
8741cc21f7bSLen Brown 	/* SMI */
875812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
8766168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
8771cc21f7bSLen Brown 
878678a3bd1SLen Brown 	/* Added counters */
879678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
880678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
881678a3bd1SLen Brown 			if (mp->width == 32)
8825f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
883678a3bd1SLen Brown 			else
8846168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
885678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
8860de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
8876168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
8880de6c0dfSLen Brown 			else
8896168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
890678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
89141618e63SLen Brown 			if (mp->type == COUNTER_USEC)
8926168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
89341618e63SLen Brown 			else
8946168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
895678a3bd1SLen Brown 		}
896678a3bd1SLen Brown 	}
897678a3bd1SLen Brown 
89841618e63SLen Brown 	/* C1 */
89941618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
9006168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
90141618e63SLen Brown 
90241618e63SLen Brown 
903c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
904c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
905c98d5d94SLen Brown 		goto done;
906c98d5d94SLen Brown 
907812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
9086168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
909812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
9106168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
911812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
9126168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
913c98d5d94SLen Brown 
9140539ba11SLen Brown 	/* Mod%c6 */
9150539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
9166168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
9170539ba11SLen Brown 
918812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
9196168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
920889facbeSLen Brown 
921388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
922388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
923388e9c81SLen Brown 			if (mp->width == 32)
9245f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
925388e9c81SLen Brown 			else
9266168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
927388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
9280de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9296168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
9300de6c0dfSLen Brown 			else
9316168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
932388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
9336168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
934388e9c81SLen Brown 		}
935388e9c81SLen Brown 	}
936388e9c81SLen Brown 
937c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
938c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
939c98d5d94SLen Brown 		goto done;
940c98d5d94SLen Brown 
9410b2bb692SLen Brown 	/* PkgTmp */
942812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
9436168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
944889facbeSLen Brown 
945fdf676e5SLen Brown 	/* GFXrc6 */
946812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
947ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
9486168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
9499185e988SLen Brown 		} else {
9506168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
9519185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
9529185e988SLen Brown 		}
9539185e988SLen Brown 	}
954fdf676e5SLen Brown 
95527d47356SLen Brown 	/* GFXMHz */
956812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
9576168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
95827d47356SLen Brown 
9590b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
960a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
9616168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
962a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
9636168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
964a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
9656168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
966a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
9676168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
9680b2bb692SLen Brown 
9690f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
9706168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
9710f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
9726168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
9730f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
9746168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
9750f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
9766168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
9770f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
9786168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
9790f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
9806168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
9810f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
9826168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
983889facbeSLen Brown 
984889facbeSLen Brown 	/*
985889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
986889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
987889facbeSLen Brown  	 */
988fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
9896168c2e0SLen Brown 		fmt8 = "%s%.2f";
990fc04cc67SLen Brown 	else
991fc04cc67SLen Brown 		fmt8 = "%6.0f**";
992889facbeSLen Brown 
993812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
9946168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
995812db3f7SLen Brown 	if (DO_BIC(BIC_CorWatt))
9966168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
997812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
9986168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
999812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
10006168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1001812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
10026168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1003812db3f7SLen Brown 	if (DO_BIC(BIC_Cor_J))
10046168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1005812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
10066168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1007812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
10086168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1009812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
10106168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1011812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
10126168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1013812db3f7SLen Brown 
1014388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1015388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1016388e9c81SLen Brown 			if (mp->width == 32)
10175f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1018388e9c81SLen Brown 			else
10196168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1020388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10210de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10226168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
10230de6c0dfSLen Brown 			else
10246168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1025388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10266168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1027388e9c81SLen Brown 		}
1028388e9c81SLen Brown 	}
1029388e9c81SLen Brown 
1030c98d5d94SLen Brown done:
1031c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
1032c98d5d94SLen Brown 
1033c98d5d94SLen Brown 	return 0;
1034103a8feaSLen Brown }
1035103a8feaSLen Brown 
1036b7d8c148SLen Brown void flush_output_stdout(void)
1037103a8feaSLen Brown {
1038b7d8c148SLen Brown 	FILE *filep;
1039b7d8c148SLen Brown 
1040b7d8c148SLen Brown 	if (outf == stderr)
1041b7d8c148SLen Brown 		filep = stdout;
1042b7d8c148SLen Brown 	else
1043b7d8c148SLen Brown 		filep = outf;
1044b7d8c148SLen Brown 
1045b7d8c148SLen Brown 	fputs(output_buffer, filep);
1046b7d8c148SLen Brown 	fflush(filep);
1047b7d8c148SLen Brown 
1048c98d5d94SLen Brown 	outp = output_buffer;
1049c98d5d94SLen Brown }
1050b7d8c148SLen Brown void flush_output_stderr(void)
1051c98d5d94SLen Brown {
1052b7d8c148SLen Brown 	fputs(output_buffer, outf);
1053b7d8c148SLen Brown 	fflush(outf);
1054c98d5d94SLen Brown 	outp = output_buffer;
1055c98d5d94SLen Brown }
1056c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1057c98d5d94SLen Brown {
1058e23da037SLen Brown 	static int printed;
1059103a8feaSLen Brown 
1060e23da037SLen Brown 	if (!printed || !summary_only)
1061c8ade361SLen Brown 		print_header("\t");
1062103a8feaSLen Brown 
1063c98d5d94SLen Brown 	if (topo.num_cpus > 1)
1064c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
1065c98d5d94SLen Brown 			&average.packages);
1066103a8feaSLen Brown 
1067e23da037SLen Brown 	printed = 1;
1068e23da037SLen Brown 
1069e23da037SLen Brown 	if (summary_only)
1070e23da037SLen Brown 		return;
1071e23da037SLen Brown 
1072c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1073103a8feaSLen Brown }
1074103a8feaSLen Brown 
1075889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1076889facbeSLen Brown 	if (new > old) {			\
1077889facbeSLen Brown 		old = new - old;		\
1078889facbeSLen Brown 	} else {				\
1079889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1080889facbeSLen Brown 	}
1081889facbeSLen Brown 
1082ba3dec99SLen Brown int
1083c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1084103a8feaSLen Brown {
1085388e9c81SLen Brown 	int i;
1086388e9c81SLen Brown 	struct msr_counter *mp;
10870b2bb692SLen Brown 
1088a99d8730SLen Brown 
1089a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
10900b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1091a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
10920b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1093a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
10940b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1095a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
10960b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1097a99d8730SLen Brown 
1098c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
10990f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1100c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
11010f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1102c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
11030f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1104c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1105ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1106ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1107ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1108889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1109889facbeSLen Brown 
11109185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
11119185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
11129185e988SLen Brown 		old->gfx_rc6_ms = -1;
11139185e988SLen Brown 	else
1114fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
11159185e988SLen Brown 
111627d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
111727d47356SLen Brown 
1118889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1119889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1120889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1121889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1122889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1123889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1124ba3dec99SLen Brown 
1125388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1126388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1127388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1128388e9c81SLen Brown 		else
1129388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1130388e9c81SLen Brown 	}
1131388e9c81SLen Brown 
1132ba3dec99SLen Brown 	return 0;
1133103a8feaSLen Brown }
1134103a8feaSLen Brown 
1135c98d5d94SLen Brown void
1136c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1137c98d5d94SLen Brown {
1138388e9c81SLen Brown 	int i;
1139388e9c81SLen Brown 	struct msr_counter *mp;
1140388e9c81SLen Brown 
1141c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1142c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1143c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1144889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
11450539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1146388e9c81SLen Brown 
1147388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1148388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1149388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1150388e9c81SLen Brown 		else
1151388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1152388e9c81SLen Brown 	}
1153103a8feaSLen Brown }
1154c98d5d94SLen Brown 
1155c3ae331dSLen Brown /*
1156c3ae331dSLen Brown  * old = new - old
1157c3ae331dSLen Brown  */
1158ba3dec99SLen Brown int
1159c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1160c98d5d94SLen Brown 	struct core_data *core_delta)
1161c98d5d94SLen Brown {
1162388e9c81SLen Brown 	int i;
1163388e9c81SLen Brown 	struct msr_counter *mp;
1164388e9c81SLen Brown 
11653f44a5c6SLen Brown 	/*
11663f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
11673f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
11683f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
11693f44a5c6SLen Brown 	 */
11703f44a5c6SLen Brown 
11713f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
11723f44a5c6SLen Brown 	old->tv_end = new->tv_end;
11733f44a5c6SLen Brown 
1174c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1175c98d5d94SLen Brown 
1176103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1177b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1178b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1179b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1180b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1181103a8feaSLen Brown 
1182c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1183c98d5d94SLen Brown 
1184812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1185c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1186c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1187c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1188c98d5d94SLen Brown 		} else {
1189ba3dec99SLen Brown 			return -1;
1190103a8feaSLen Brown 		}
1191a729617cSLen Brown 	}
1192103a8feaSLen Brown 
1193c98d5d94SLen Brown 
1194144b44b1SLen Brown 	if (use_c1_residency_msr) {
1195144b44b1SLen Brown 		/*
1196144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1197144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1198144b44b1SLen Brown 		 */
1199144b44b1SLen Brown 	} else {
1200103a8feaSLen Brown 		/*
1201c3ae331dSLen Brown 		 * As counter collection is not atomic,
1202c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1203103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1204103a8feaSLen Brown 		 */
120595149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1206c98d5d94SLen Brown 			old->c1 = 0;
1207c98d5d94SLen Brown 		else {
1208c98d5d94SLen Brown 			/* normal case, derive c1 */
1209008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1210c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1211c98d5d94SLen Brown 		}
1212144b44b1SLen Brown 	}
1213c3ae331dSLen Brown 
1214c98d5d94SLen Brown 	if (old->mperf == 0) {
1215b7d8c148SLen Brown 		if (debug > 1)
1216b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1217c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1218c98d5d94SLen Brown 	}
1219103a8feaSLen Brown 
1220812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1221562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1222562a2d37SLen Brown 
1223812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
12241ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1225ba3dec99SLen Brown 
1226388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1227388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1228388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1229388e9c81SLen Brown 		else
1230388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1231388e9c81SLen Brown 	}
1232ba3dec99SLen Brown 	return 0;
1233103a8feaSLen Brown }
1234c98d5d94SLen Brown 
1235c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1236c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1237c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1238c98d5d94SLen Brown {
1239ba3dec99SLen Brown 	int retval = 0;
1240ba3dec99SLen Brown 
1241c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1242c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1243c98d5d94SLen Brown 		delta_core(c, c2);
1244c98d5d94SLen Brown 
1245c98d5d94SLen Brown 	/* always calculate thread delta */
1246ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1247ba3dec99SLen Brown 	if (retval)
1248ba3dec99SLen Brown 		return retval;
1249c98d5d94SLen Brown 
1250c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1251c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1252ba3dec99SLen Brown 		retval = delta_package(p, p2);
1253c98d5d94SLen Brown 
1254ba3dec99SLen Brown 	return retval;
1255103a8feaSLen Brown }
1256103a8feaSLen Brown 
1257c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1258103a8feaSLen Brown {
1259388e9c81SLen Brown 	int i;
1260388e9c81SLen Brown 	struct msr_counter  *mp;
1261388e9c81SLen Brown 
12623f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
12633f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
12643f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
12653f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
12663f44a5c6SLen Brown 
1267c98d5d94SLen Brown 	t->tsc = 0;
1268c98d5d94SLen Brown 	t->aperf = 0;
1269c98d5d94SLen Brown 	t->mperf = 0;
1270c98d5d94SLen Brown 	t->c1 = 0;
1271103a8feaSLen Brown 
1272562a2d37SLen Brown 	t->irq_count = 0;
1273562a2d37SLen Brown 	t->smi_count = 0;
1274562a2d37SLen Brown 
1275c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1276c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1277c98d5d94SLen Brown 
1278c98d5d94SLen Brown 	c->c3 = 0;
1279c98d5d94SLen Brown 	c->c6 = 0;
1280c98d5d94SLen Brown 	c->c7 = 0;
12810539ba11SLen Brown 	c->mc6_us = 0;
1282889facbeSLen Brown 	c->core_temp_c = 0;
1283c98d5d94SLen Brown 
12840b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
12850b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
12860b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
12870b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
12880b2bb692SLen Brown 
1289c98d5d94SLen Brown 	p->pc2 = 0;
12900f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1291c98d5d94SLen Brown 		p->pc3 = 0;
12920f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1293c98d5d94SLen Brown 		p->pc6 = 0;
12940f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1295c98d5d94SLen Brown 		p->pc7 = 0;
1296ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1297ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1298ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1299889facbeSLen Brown 
1300889facbeSLen Brown 	p->energy_pkg = 0;
1301889facbeSLen Brown 	p->energy_dram = 0;
1302889facbeSLen Brown 	p->energy_cores = 0;
1303889facbeSLen Brown 	p->energy_gfx = 0;
1304889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1305889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1306889facbeSLen Brown 	p->pkg_temp_c = 0;
130727d47356SLen Brown 
1308fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
130927d47356SLen Brown 	p->gfx_mhz = 0;
1310388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1311388e9c81SLen Brown 		t->counter[i] = 0;
1312388e9c81SLen Brown 
1313388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1314388e9c81SLen Brown 		c->counter[i] = 0;
1315388e9c81SLen Brown 
1316388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1317388e9c81SLen Brown 		p->counter[i] = 0;
1318103a8feaSLen Brown }
1319c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1320c98d5d94SLen Brown 	struct pkg_data *p)
1321103a8feaSLen Brown {
1322388e9c81SLen Brown 	int i;
1323388e9c81SLen Brown 	struct msr_counter *mp;
1324388e9c81SLen Brown 
13253f44a5c6SLen Brown 	/* remember first tv_begin */
13263f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
13273f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
13283f44a5c6SLen Brown 
13293f44a5c6SLen Brown 	/* remember last tv_end */
13303f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
13313f44a5c6SLen Brown 
1332c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1333c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1334c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1335c98d5d94SLen Brown 	average.threads.c1 += t->c1;
133615aaa346SLen Brown 
1337562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1338562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1339562a2d37SLen Brown 
1340388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1341388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1342388e9c81SLen Brown 			continue;
1343388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1344388e9c81SLen Brown 	}
1345388e9c81SLen Brown 
1346c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1347c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1348c98d5d94SLen Brown 		return 0;
1349c98d5d94SLen Brown 
1350c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1351c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1352c98d5d94SLen Brown 	average.cores.c7 += c->c7;
13530539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1354c98d5d94SLen Brown 
1355889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1356889facbeSLen Brown 
1357388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1358388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1359388e9c81SLen Brown 			continue;
1360388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1361388e9c81SLen Brown 	}
1362388e9c81SLen Brown 
1363c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1364c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1365c98d5d94SLen Brown 		return 0;
1366c98d5d94SLen Brown 
1367a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
13680b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1369a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
13700b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1371a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
13720b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1373a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
13740b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
13750b2bb692SLen Brown 
1376c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
13770f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1378c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
13790f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1380c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
13810f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1382c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1383ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1384ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1385ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1386c98d5d94SLen Brown 
1387889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1388889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1389889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1390889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1391889facbeSLen Brown 
1392fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
139327d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
139427d47356SLen Brown 
1395889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1396889facbeSLen Brown 
1397889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1398889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1399388e9c81SLen Brown 
1400388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1401388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1402388e9c81SLen Brown 			continue;
1403388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1404388e9c81SLen Brown 	}
1405c98d5d94SLen Brown 	return 0;
1406c98d5d94SLen Brown }
1407c98d5d94SLen Brown /*
1408c98d5d94SLen Brown  * sum the counters for all cpus in the system
1409c98d5d94SLen Brown  * compute the weighted average
1410c98d5d94SLen Brown  */
1411c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1412c98d5d94SLen Brown 	struct pkg_data *p)
1413c98d5d94SLen Brown {
1414388e9c81SLen Brown 	int i;
1415388e9c81SLen Brown 	struct msr_counter *mp;
1416388e9c81SLen Brown 
1417c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1418c98d5d94SLen Brown 
1419c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1420c98d5d94SLen Brown 
1421c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1422c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1423c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1424c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1425c98d5d94SLen Brown 
14260de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
14270de6c0dfSLen Brown 		sums_need_wide_columns = 1;
14280de6c0dfSLen Brown 
1429c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1430c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1431c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
14320539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1433c98d5d94SLen Brown 
1434a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
14350b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1436a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
14370b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1438a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
14390b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1440a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
14410b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
14420b2bb692SLen Brown 
1443c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
14440f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1445c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
14460f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1447c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
14480f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1449c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1450ca58710fSKristen Carlson Accardi 
1451ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1452ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1453ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1454388e9c81SLen Brown 
1455388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1456388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1457388e9c81SLen Brown 			continue;
14580de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14590de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
14600de6c0dfSLen Brown 				sums_need_wide_columns = 1;
146141618e63SLen Brown 			continue;
14620de6c0dfSLen Brown 		}
1463388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1464388e9c81SLen Brown 	}
1465388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1466388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1467388e9c81SLen Brown 			continue;
14680de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14690de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
14700de6c0dfSLen Brown 				sums_need_wide_columns = 1;
14710de6c0dfSLen Brown 		}
1472388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1473388e9c81SLen Brown 	}
1474388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1475388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1476388e9c81SLen Brown 			continue;
14770de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14780de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
14790de6c0dfSLen Brown 				sums_need_wide_columns = 1;
14800de6c0dfSLen Brown 		}
1481388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1482388e9c81SLen Brown 	}
1483c98d5d94SLen Brown }
1484c98d5d94SLen Brown 
1485c98d5d94SLen Brown static unsigned long long rdtsc(void)
1486c98d5d94SLen Brown {
1487c98d5d94SLen Brown 	unsigned int low, high;
1488c98d5d94SLen Brown 
1489c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1490c98d5d94SLen Brown 
1491c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1492c98d5d94SLen Brown }
1493c98d5d94SLen Brown 
1494c98d5d94SLen Brown /*
1495495c7654SLen Brown  * Open a file, and exit on failure
1496495c7654SLen Brown  */
1497495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1498495c7654SLen Brown {
1499495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1500495c7654SLen Brown 
1501495c7654SLen Brown 	if (!filep)
1502495c7654SLen Brown 		err(1, "%s: open failed", path);
1503495c7654SLen Brown 	return filep;
1504495c7654SLen Brown }
1505495c7654SLen Brown /*
1506495c7654SLen Brown  * snapshot_sysfs_counter()
1507495c7654SLen Brown  *
1508495c7654SLen Brown  * return snapshot of given counter
1509495c7654SLen Brown  */
1510495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1511495c7654SLen Brown {
1512495c7654SLen Brown 	FILE *fp;
1513495c7654SLen Brown 	int retval;
1514495c7654SLen Brown 	unsigned long long counter;
1515495c7654SLen Brown 
1516495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1517495c7654SLen Brown 
1518495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1519495c7654SLen Brown 	if (retval != 1)
1520495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1521495c7654SLen Brown 
1522495c7654SLen Brown 	fclose(fp);
1523495c7654SLen Brown 
1524495c7654SLen Brown 	return counter;
1525495c7654SLen Brown }
1526495c7654SLen Brown 
1527495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1528495c7654SLen Brown {
1529495c7654SLen Brown 	if (mp->msr_num != 0) {
1530495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1531495c7654SLen Brown 			return -1;
1532495c7654SLen Brown 	} else {
153346c27978SLen Brown 		char path[128 + PATH_BYTES];
153441618e63SLen Brown 
153541618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
153641618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
153741618e63SLen Brown 				 cpu, mp->path);
153841618e63SLen Brown 
153941618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
154041618e63SLen Brown 		} else {
1541495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1542495c7654SLen Brown 		}
154341618e63SLen Brown 	}
1544495c7654SLen Brown 
1545495c7654SLen Brown 	return 0;
1546495c7654SLen Brown }
1547495c7654SLen Brown 
1548495c7654SLen Brown /*
1549c98d5d94SLen Brown  * get_counters(...)
1550c98d5d94SLen Brown  * migrate to cpu
1551c98d5d94SLen Brown  * acquire and record local counters for that cpu
1552c98d5d94SLen Brown  */
1553c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1554c98d5d94SLen Brown {
1555c98d5d94SLen Brown 	int cpu = t->cpu_id;
1556889facbeSLen Brown 	unsigned long long msr;
15570102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1558388e9c81SLen Brown 	struct msr_counter *mp;
1559388e9c81SLen Brown 	int i;
1560c98d5d94SLen Brown 
1561f4fdf2b4SLen Brown 
1562f4fdf2b4SLen Brown 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1563f4fdf2b4SLen Brown 
1564e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1565b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
156615aaa346SLen Brown 		return -1;
1567e52966c0SLen Brown 	}
156815aaa346SLen Brown 
15690102b067SLen Brown retry:
1570c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
157115aaa346SLen Brown 
1572812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
15730102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
15740102b067SLen Brown 
15750102b067SLen Brown 		/*
15760102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
15770102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
15780102b067SLen Brown 		 *
15790102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
15800102b067SLen Brown 		 * individual system call, so delays may occur
15810102b067SLen Brown 		 * between them.  If the time to read them
15820102b067SLen Brown 		 * varies by a large amount, we re-read them.
15830102b067SLen Brown 		 */
15840102b067SLen Brown 
15850102b067SLen Brown 		/*
15860102b067SLen Brown 		 * This initial dummy APERF read has been seen to
15870102b067SLen Brown 		 * reduce jitter in the subsequent reads.
15880102b067SLen Brown 		 */
15890102b067SLen Brown 
15909c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1591c98d5d94SLen Brown 			return -3;
15920102b067SLen Brown 
15930102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
15940102b067SLen Brown 
15950102b067SLen Brown 		tsc_before = t->tsc;
15960102b067SLen Brown 
15970102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
15980102b067SLen Brown 			return -3;
15990102b067SLen Brown 
16000102b067SLen Brown 		tsc_between = rdtsc();
16010102b067SLen Brown 
16029c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1603c98d5d94SLen Brown 			return -4;
16040102b067SLen Brown 
16050102b067SLen Brown 		tsc_after = rdtsc();
16060102b067SLen Brown 
16070102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
16080102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
16090102b067SLen Brown 
16100102b067SLen Brown 		/*
16110102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
16120102b067SLen Brown 		 * differ by more than 2x, then try again.
16130102b067SLen Brown 		 */
16140102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
16150102b067SLen Brown 			aperf_mperf_retry_count++;
16160102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
16170102b067SLen Brown 				goto retry;
16180102b067SLen Brown 			else
16190102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
16200102b067SLen Brown 					cpu, aperf_time, mperf_time);
16210102b067SLen Brown 		}
16220102b067SLen Brown 		aperf_mperf_retry_count = 0;
16230102b067SLen Brown 
1624b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1625b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
162688c3281fSLen Brown 	}
162788c3281fSLen Brown 
1628812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1629562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1630812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
16311ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
16321ed51011SLen Brown 			return -5;
16331ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
16341ed51011SLen Brown 	}
16350539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1636144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1637144b44b1SLen Brown 			return -6;
1638144b44b1SLen Brown 	}
1639144b44b1SLen Brown 
1640388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1641495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1642388e9c81SLen Brown 			return -10;
1643388e9c81SLen Brown 	}
1644388e9c81SLen Brown 
1645c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1646c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1647f4fdf2b4SLen Brown 		goto done;
1648c98d5d94SLen Brown 
1649812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
1650c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1651c98d5d94SLen Brown 			return -6;
1652144b44b1SLen Brown 	}
1653144b44b1SLen Brown 
1654812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1655c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1656c98d5d94SLen Brown 			return -7;
1657fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1658fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1659fb5d4327SDasaratharaman Chandramouli 			return -7;
1660103a8feaSLen Brown 	}
166115aaa346SLen Brown 
1662812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1663c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1664c98d5d94SLen Brown 			return -8;
1665c98d5d94SLen Brown 
16660539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
16670539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
16680539ba11SLen Brown 			return -8;
16690539ba11SLen Brown 
1670812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1671889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1672889facbeSLen Brown 			return -9;
1673889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1674889facbeSLen Brown 	}
1675889facbeSLen Brown 
1676388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1677495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1678388e9c81SLen Brown 			return -10;
1679388e9c81SLen Brown 	}
1680889facbeSLen Brown 
1681c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1682c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1683f4fdf2b4SLen Brown 		goto done;
168415aaa346SLen Brown 
1685a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
16860b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
16870b2bb692SLen Brown 			return -10;
1688a99d8730SLen Brown 	}
1689a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
16900b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
16910b2bb692SLen Brown 			return -11;
1692a99d8730SLen Brown 	}
1693a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
16940b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
16950b2bb692SLen Brown 			return -12;
1696a99d8730SLen Brown 	}
1697a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
16980b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
16990b2bb692SLen Brown 			return -13;
17000b2bb692SLen Brown 	}
17010f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1702c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1703c98d5d94SLen Brown 			return -9;
17040f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
17050539ba11SLen Brown 		if (do_slm_cstates) {
17060539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
17070539ba11SLen Brown 				return -10;
17080539ba11SLen Brown 		} else {
1709c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1710c98d5d94SLen Brown 				return -10;
17110539ba11SLen Brown 		}
17120539ba11SLen Brown 	}
17130539ba11SLen Brown 
17140f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1715c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1716c98d5d94SLen Brown 			return -11;
17170f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1718c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1719c98d5d94SLen Brown 			return -12;
17200f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1721ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1722ca58710fSKristen Carlson Accardi 			return -13;
17230f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1724ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1725ca58710fSKristen Carlson Accardi 			return -13;
17260f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1727ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1728ca58710fSKristen Carlson Accardi 			return -13;
17290f47c08dSLen Brown 
1730889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1731889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1732889facbeSLen Brown 			return -13;
1733889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1734889facbeSLen Brown 	}
17359148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1736889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1737889facbeSLen Brown 			return -14;
1738889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1739889facbeSLen Brown 	}
1740889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1741889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1742889facbeSLen Brown 			return -15;
1743889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1744889facbeSLen Brown 	}
1745889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1746889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1747889facbeSLen Brown 			return -16;
1748889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1749889facbeSLen Brown 	}
1750889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1751889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1752889facbeSLen Brown 			return -16;
1753889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1754889facbeSLen Brown 	}
1755889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1756889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1757889facbeSLen Brown 			return -16;
1758889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1759889facbeSLen Brown 	}
1760812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1761889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1762889facbeSLen Brown 			return -17;
1763889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1764889facbeSLen Brown 	}
1765fdf676e5SLen Brown 
1766812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1767fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1768fdf676e5SLen Brown 
1769812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
177027d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
177127d47356SLen Brown 
1772388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1773495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
1774388e9c81SLen Brown 			return -10;
1775388e9c81SLen Brown 	}
1776f4fdf2b4SLen Brown done:
1777f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
1778388e9c81SLen Brown 
177915aaa346SLen Brown 	return 0;
1780103a8feaSLen Brown }
1781103a8feaSLen Brown 
1782ee7e38e3SLen Brown /*
1783ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1784ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1785ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1786ee7e38e3SLen Brown  */
1787ee7e38e3SLen Brown 
1788ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1789ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1790ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1791ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1792ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1793ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1794ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1795ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1796ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1797ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1798ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1799ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
18000b2bb692SLen Brown #define PCL__8 12 /* PC8 */
18010b2bb692SLen Brown #define PCL__9 13 /* PC9 */
18020b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1803ee7e38e3SLen Brown 
1804ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1805ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
18060b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1807ee7e38e3SLen Brown 
1808e9257f5fSLen 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};
1809e9257f5fSLen 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};
1810e9257f5fSLen 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};
18110539ba11SLen 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, PCL__6, PCL__7};
1812f2642888SLen Brown int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1813e9257f5fSLen 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};
1814e4085d54SLen 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};
18152085e124SArtem Bityutskiy int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1816ee7e38e3SLen Brown 
1817a2b7b749SLen Brown 
1818a2b7b749SLen Brown static void
1819a2b7b749SLen Brown calculate_tsc_tweak()
1820a2b7b749SLen Brown {
1821a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1822a2b7b749SLen Brown }
1823a2b7b749SLen Brown 
1824fcd17211SLen Brown static void
1825fcd17211SLen Brown dump_nhm_platform_info(void)
1826103a8feaSLen Brown {
1827103a8feaSLen Brown 	unsigned long long msr;
1828103a8feaSLen Brown 	unsigned int ratio;
1829103a8feaSLen Brown 
1830ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1831103a8feaSLen Brown 
1832b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
18336574a5d5SLen Brown 
1834103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1835710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
1836103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1837103a8feaSLen Brown 
1838103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1839710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1840103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1841103a8feaSLen Brown 
18427ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1843b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1844bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
184567920418SLen Brown 
1846fcd17211SLen Brown 	return;
1847fcd17211SLen Brown }
1848fcd17211SLen Brown 
1849fcd17211SLen Brown static void
1850fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1851fcd17211SLen Brown {
1852fcd17211SLen Brown 	unsigned long long msr;
1853fcd17211SLen Brown 	unsigned int ratio;
1854fcd17211SLen Brown 
18557ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1856fcd17211SLen Brown 
1857b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1858fcd17211SLen Brown 
1859fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1860fcd17211SLen Brown 	if (ratio)
1861710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
1862fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1863fcd17211SLen Brown 
1864fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1865fcd17211SLen Brown 	if (ratio)
1866710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
1867fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1868fcd17211SLen Brown 	return;
1869fcd17211SLen Brown }
1870fcd17211SLen Brown 
1871fcd17211SLen Brown static void
1872fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1873fcd17211SLen Brown {
1874fcd17211SLen Brown 	unsigned long long msr;
1875fcd17211SLen Brown 	unsigned int ratio;
18766574a5d5SLen Brown 
18777ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
18786574a5d5SLen Brown 
1879b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
18806574a5d5SLen Brown 
18816574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
18826574a5d5SLen Brown 	if (ratio)
1883710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
18846574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18856574a5d5SLen Brown 
18866574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
18876574a5d5SLen Brown 	if (ratio)
1888710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
18896574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18906574a5d5SLen Brown 
18916574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
18926574a5d5SLen Brown 	if (ratio)
1893710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
18946574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18956574a5d5SLen Brown 
18966574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
18976574a5d5SLen Brown 	if (ratio)
1898710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
18996574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
19006574a5d5SLen Brown 
19016574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
19026574a5d5SLen Brown 	if (ratio)
1903710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
19046574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
19056574a5d5SLen Brown 
19066574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
19076574a5d5SLen Brown 	if (ratio)
1908710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
19096574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
19106574a5d5SLen Brown 
19116574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
19126574a5d5SLen Brown 	if (ratio)
1913710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
19146574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
19156574a5d5SLen Brown 
19166574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
19176574a5d5SLen Brown 	if (ratio)
1918710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
19196574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1920103a8feaSLen Brown 	return;
1921fcd17211SLen Brown }
192231e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
192331e07522SLen Brown {
192431e07522SLen Brown 
192531e07522SLen Brown 	if (!genuine_intel)
192631e07522SLen Brown 		return 0;
192731e07522SLen Brown 
192831e07522SLen Brown 	switch (model) {
192931e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
193031e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
193131e07522SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
193231e07522SLen Brown 		return 1;
193331e07522SLen Brown 	}
193431e07522SLen Brown 	return 0;
193531e07522SLen Brown }
1936fcd17211SLen Brown 
1937fcd17211SLen Brown static void
193831e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
1939fcd17211SLen Brown {
194031e07522SLen Brown 	unsigned long long msr, core_counts;
194131e07522SLen Brown 	unsigned int ratio, group_size;
1942103a8feaSLen Brown 
19437ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1944b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
19456574a5d5SLen Brown 
194631e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
194731e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
194831e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
194931e07522SLen Brown 	} else {
195031e07522SLen Brown 		core_counts = 0x0807060504030201;
195131e07522SLen Brown 	}
195231e07522SLen Brown 
19536574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
195431e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
19556574a5d5SLen Brown 	if (ratio)
195631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
195731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19586574a5d5SLen Brown 
19596574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
196031e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
19616574a5d5SLen Brown 	if (ratio)
196231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
196331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19646574a5d5SLen Brown 
19656574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
196631e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
19676574a5d5SLen Brown 	if (ratio)
196831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
196931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19706574a5d5SLen Brown 
19716574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
197231e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
19736574a5d5SLen Brown 	if (ratio)
197431e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
197531e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19766574a5d5SLen Brown 
1977103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
197831e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
1979103a8feaSLen Brown 	if (ratio)
198031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
198131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1982103a8feaSLen Brown 
1983103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
198431e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
1985103a8feaSLen Brown 	if (ratio)
198631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
198731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1988103a8feaSLen Brown 
1989103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
199031e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
1991103a8feaSLen Brown 	if (ratio)
199231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
199331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1994103a8feaSLen Brown 
1995103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
199631e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
1997103a8feaSLen Brown 	if (ratio)
199831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
199931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2000fcd17211SLen Brown 	return;
2001fcd17211SLen Brown }
20023a9a941dSLen Brown 
2003fcd17211SLen Brown static void
20040f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
20050f7887c4SLen Brown {
20060f7887c4SLen Brown 	unsigned long long msr;
20070f7887c4SLen Brown 	unsigned int ratio;
20080f7887c4SLen Brown 
20090f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
20100f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
20110f7887c4SLen Brown 
20120f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
20130f7887c4SLen Brown 	if (ratio)
20140f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
20150f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20160f7887c4SLen Brown 
20170f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
20180f7887c4SLen Brown 	if (ratio)
20190f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
20200f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20210f7887c4SLen Brown 
20220f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
20230f7887c4SLen Brown 	if (ratio)
20240f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
20250f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20260f7887c4SLen Brown 
20270f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
20280f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
20290f7887c4SLen Brown 
20300f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
20310f7887c4SLen Brown 	if (ratio)
20320f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
20330f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20340f7887c4SLen Brown 
20350f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
20360f7887c4SLen Brown 	if (ratio)
20370f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
20380f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20390f7887c4SLen Brown 
20400f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
20410f7887c4SLen Brown 	if (ratio)
20420f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
20430f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20440f7887c4SLen Brown 
20450f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
20460f7887c4SLen Brown 	if (ratio)
20470f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
20480f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20490f7887c4SLen Brown }
20500f7887c4SLen Brown 
20510f7887c4SLen Brown static void
2052fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2053fb5d4327SDasaratharaman Chandramouli {
2054cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2055cbf97abaSHubert Chrzaniuk 
2056fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2057cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2058cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2059cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2060cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2061fb5d4327SDasaratharaman Chandramouli 
2062ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2063fb5d4327SDasaratharaman Chandramouli 
2064b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2065bfae2052SLen Brown 		base_cpu, msr);
2066fb5d4327SDasaratharaman Chandramouli 
2067fb5d4327SDasaratharaman Chandramouli 	/**
2068fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2069cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2070cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2071fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2072fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2073fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2074fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2075fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2076fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2077fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2078fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2079fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2080fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2081fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2082fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2083fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2084fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2085fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2086fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2087fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2088fb5d4327SDasaratharaman Chandramouli 	 */
2089fb5d4327SDasaratharaman Chandramouli 
2090cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2091cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2092cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2093cbf97abaSHubert Chrzaniuk 
2094cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2095fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2096cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2097fb5d4327SDasaratharaman Chandramouli 
2098cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2099cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2100cbf97abaSHubert Chrzaniuk 		b_nr++;
2101fb5d4327SDasaratharaman Chandramouli 	}
2102cbf97abaSHubert Chrzaniuk 
2103cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2104cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2105b7d8c148SLen Brown 			fprintf(outf,
2106710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2107cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2108fb5d4327SDasaratharaman Chandramouli }
2109fb5d4327SDasaratharaman Chandramouli 
2110fb5d4327SDasaratharaman Chandramouli static void
2111fcd17211SLen Brown dump_nhm_cst_cfg(void)
2112fcd17211SLen Brown {
2113fcd17211SLen Brown 	unsigned long long msr;
2114fcd17211SLen Brown 
21151df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2116fcd17211SLen Brown 
2117fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
2118fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
2119fcd17211SLen Brown 
21201df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2121fcd17211SLen Brown 
2122*ac980e13SArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s",
2123fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2124fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2125fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2126fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2127fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
21286c34f160SLen Brown 		(unsigned int)msr & 0xF,
2129fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2130*ac980e13SArtem Bityutskiy 
2131*ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2132*ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
2133*ac980e13SArtem Bityutskiy 		fprintf(outf, ", automatic c-state conversion=%s",
2134*ac980e13SArtem Bityutskiy 			(msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2135*ac980e13SArtem Bityutskiy 	}
2136*ac980e13SArtem Bityutskiy 
2137*ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2138*ac980e13SArtem Bityutskiy 
2139fcd17211SLen Brown 	return;
2140103a8feaSLen Brown }
2141103a8feaSLen Brown 
21426fb3143bSLen Brown static void
21436fb3143bSLen Brown dump_config_tdp(void)
21446fb3143bSLen Brown {
21456fb3143bSLen Brown 	unsigned long long msr;
21466fb3143bSLen Brown 
21476fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2148b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2149685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
21506fb3143bSLen Brown 
21516fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2152b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
21536fb3143bSLen Brown 	if (msr) {
2154685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2155685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2156685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2157685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
21586fb3143bSLen Brown 	}
2159b7d8c148SLen Brown 	fprintf(outf, ")\n");
21606fb3143bSLen Brown 
21616fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2162b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
21636fb3143bSLen Brown 	if (msr) {
2164685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2165685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2166685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2167685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
21686fb3143bSLen Brown 	}
2169b7d8c148SLen Brown 	fprintf(outf, ")\n");
21706fb3143bSLen Brown 
21716fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2172b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
21736fb3143bSLen Brown 	if ((msr) & 0x3)
2174b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2175b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2176b7d8c148SLen Brown 	fprintf(outf, ")\n");
21776fb3143bSLen Brown 
21786fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2179b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2180685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2181b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2182b7d8c148SLen Brown 	fprintf(outf, ")\n");
21836fb3143bSLen Brown }
21845a63426eSLen Brown 
21855a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
21865a63426eSLen Brown 
21875a63426eSLen Brown void print_irtl(void)
21885a63426eSLen Brown {
21895a63426eSLen Brown 	unsigned long long msr;
21905a63426eSLen Brown 
21915a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
21925a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
21935a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21945a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21955a63426eSLen Brown 
21965a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
21975a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
21985a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21995a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
22005a63426eSLen Brown 
22015a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
22025a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
22035a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
22045a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
22055a63426eSLen Brown 
22065a63426eSLen Brown 	if (!do_irtl_hsw)
22075a63426eSLen Brown 		return;
22085a63426eSLen Brown 
22095a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
22105a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
22115a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
22125a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
22135a63426eSLen Brown 
22145a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
22155a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
22165a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
22175a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
22185a63426eSLen Brown 
22195a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
22205a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
22215a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
22225a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
22235a63426eSLen Brown 
22245a63426eSLen Brown }
222536229897SLen Brown void free_fd_percpu(void)
222636229897SLen Brown {
222736229897SLen Brown 	int i;
222836229897SLen Brown 
222901a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
223036229897SLen Brown 		if (fd_percpu[i] != 0)
223136229897SLen Brown 			close(fd_percpu[i]);
223236229897SLen Brown 	}
223336229897SLen Brown 
223436229897SLen Brown 	free(fd_percpu);
22356fb3143bSLen Brown }
22366fb3143bSLen Brown 
2237c98d5d94SLen Brown void free_all_buffers(void)
2238103a8feaSLen Brown {
2239c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2240c98d5d94SLen Brown 	cpu_present_set = NULL;
224136229897SLen Brown 	cpu_present_setsize = 0;
2242103a8feaSLen Brown 
2243c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2244c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2245c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2246103a8feaSLen Brown 
2247c98d5d94SLen Brown 	free(thread_even);
2248c98d5d94SLen Brown 	free(core_even);
2249c98d5d94SLen Brown 	free(package_even);
2250103a8feaSLen Brown 
2251c98d5d94SLen Brown 	thread_even = NULL;
2252c98d5d94SLen Brown 	core_even = NULL;
2253c98d5d94SLen Brown 	package_even = NULL;
2254103a8feaSLen Brown 
2255c98d5d94SLen Brown 	free(thread_odd);
2256c98d5d94SLen Brown 	free(core_odd);
2257c98d5d94SLen Brown 	free(package_odd);
2258103a8feaSLen Brown 
2259c98d5d94SLen Brown 	thread_odd = NULL;
2260c98d5d94SLen Brown 	core_odd = NULL;
2261c98d5d94SLen Brown 	package_odd = NULL;
2262103a8feaSLen Brown 
2263c98d5d94SLen Brown 	free(output_buffer);
2264c98d5d94SLen Brown 	output_buffer = NULL;
2265c98d5d94SLen Brown 	outp = NULL;
226636229897SLen Brown 
226736229897SLen Brown 	free_fd_percpu();
2268562a2d37SLen Brown 
2269562a2d37SLen Brown 	free(irq_column_2_cpu);
2270562a2d37SLen Brown 	free(irqs_per_cpu);
2271103a8feaSLen Brown }
2272103a8feaSLen Brown 
227357a42a34SJosh Triplett 
227457a42a34SJosh Triplett /*
227595aebc44SJosh Triplett  * Parse a file containing a single int.
227695aebc44SJosh Triplett  */
227795aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
227895aebc44SJosh Triplett {
227995aebc44SJosh Triplett 	va_list args;
228095aebc44SJosh Triplett 	char path[PATH_MAX];
228195aebc44SJosh Triplett 	FILE *filep;
228295aebc44SJosh Triplett 	int value;
228395aebc44SJosh Triplett 
228495aebc44SJosh Triplett 	va_start(args, fmt);
228595aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
228695aebc44SJosh Triplett 	va_end(args);
228757a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2288b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2289b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
229095aebc44SJosh Triplett 	fclose(filep);
229195aebc44SJosh Triplett 	return value;
229295aebc44SJosh Triplett }
229395aebc44SJosh Triplett 
229495aebc44SJosh Triplett /*
2295e275b388SDasaratharaman Chandramouli  * get_cpu_position_in_core(cpu)
2296e275b388SDasaratharaman Chandramouli  * return the position of the CPU among its HT siblings in the core
2297e275b388SDasaratharaman Chandramouli  * return -1 if the sibling is not in list
2298103a8feaSLen Brown  */
2299e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu)
2300103a8feaSLen Brown {
2301e275b388SDasaratharaman Chandramouli 	char path[64];
2302e275b388SDasaratharaman Chandramouli 	FILE *filep;
2303e275b388SDasaratharaman Chandramouli 	int this_cpu;
2304e275b388SDasaratharaman Chandramouli 	char character;
2305e275b388SDasaratharaman Chandramouli 	int i;
2306e275b388SDasaratharaman Chandramouli 
2307e275b388SDasaratharaman Chandramouli 	sprintf(path,
2308e275b388SDasaratharaman Chandramouli 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2309e275b388SDasaratharaman Chandramouli 		cpu);
2310e275b388SDasaratharaman Chandramouli 	filep = fopen(path, "r");
2311e275b388SDasaratharaman Chandramouli 	if (filep == NULL) {
2312e275b388SDasaratharaman Chandramouli 		perror(path);
2313e275b388SDasaratharaman Chandramouli 		exit(1);
2314e275b388SDasaratharaman Chandramouli 	}
2315e275b388SDasaratharaman Chandramouli 
2316e275b388SDasaratharaman Chandramouli 	for (i = 0; i < topo.num_threads_per_core; i++) {
2317e275b388SDasaratharaman Chandramouli 		fscanf(filep, "%d", &this_cpu);
2318e275b388SDasaratharaman Chandramouli 		if (this_cpu == cpu) {
2319e275b388SDasaratharaman Chandramouli 			fclose(filep);
2320e275b388SDasaratharaman Chandramouli 			return i;
2321e275b388SDasaratharaman Chandramouli 		}
2322e275b388SDasaratharaman Chandramouli 
2323e275b388SDasaratharaman Chandramouli 		/* Account for no separator after last thread*/
2324e275b388SDasaratharaman Chandramouli 		if (i != (topo.num_threads_per_core - 1))
2325e275b388SDasaratharaman Chandramouli 			fscanf(filep, "%c", &character);
2326e275b388SDasaratharaman Chandramouli 	}
2327e275b388SDasaratharaman Chandramouli 
2328e275b388SDasaratharaman Chandramouli 	fclose(filep);
2329e275b388SDasaratharaman Chandramouli 	return -1;
2330c98d5d94SLen Brown }
2331103a8feaSLen Brown 
2332c98d5d94SLen Brown /*
2333c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2334c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2335c98d5d94SLen Brown  */
2336c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2337c98d5d94SLen Brown {
233895aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2339103a8feaSLen Brown }
2340103a8feaSLen Brown 
2341103a8feaSLen Brown int get_physical_package_id(int cpu)
2342103a8feaSLen Brown {
234395aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2344103a8feaSLen Brown }
2345103a8feaSLen Brown 
2346103a8feaSLen Brown int get_core_id(int cpu)
2347103a8feaSLen Brown {
234895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2349103a8feaSLen Brown }
2350103a8feaSLen Brown 
2351c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
2352c98d5d94SLen Brown {
2353c98d5d94SLen Brown 	char path[80];
2354c98d5d94SLen Brown 	FILE *filep;
2355e275b388SDasaratharaman Chandramouli 	int sib1;
2356e275b388SDasaratharaman Chandramouli 	int matches = 0;
2357c98d5d94SLen Brown 	char character;
2358e275b388SDasaratharaman Chandramouli 	char str[100];
2359e275b388SDasaratharaman Chandramouli 	char *ch;
2360c98d5d94SLen Brown 
2361c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
236257a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2363e275b388SDasaratharaman Chandramouli 
2364103a8feaSLen Brown 	/*
2365c98d5d94SLen Brown 	 * file format:
2366e275b388SDasaratharaman Chandramouli 	 * A ',' separated or '-' separated set of numbers
2367e275b388SDasaratharaman Chandramouli 	 * (eg 1-2 or 1,3,4,5)
2368c98d5d94SLen Brown 	 */
2369e275b388SDasaratharaman Chandramouli 	fscanf(filep, "%d%c\n", &sib1, &character);
2370e275b388SDasaratharaman Chandramouli 	fseek(filep, 0, SEEK_SET);
2371e275b388SDasaratharaman Chandramouli 	fgets(str, 100, filep);
2372e275b388SDasaratharaman Chandramouli 	ch = strchr(str, character);
2373e275b388SDasaratharaman Chandramouli 	while (ch != NULL) {
2374e275b388SDasaratharaman Chandramouli 		matches++;
2375e275b388SDasaratharaman Chandramouli 		ch = strchr(ch+1, character);
2376e275b388SDasaratharaman Chandramouli 	}
2377c98d5d94SLen Brown 
2378c98d5d94SLen Brown 	fclose(filep);
2379e275b388SDasaratharaman Chandramouli 	return matches+1;
2380c98d5d94SLen Brown }
2381c98d5d94SLen Brown 
2382c98d5d94SLen Brown /*
2383c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2384c98d5d94SLen Brown  * skip non-present cpus
2385103a8feaSLen Brown  */
2386103a8feaSLen Brown 
2387c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2388c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2389c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2390c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2391c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2392c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2393c98d5d94SLen Brown {
2394c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
2395c98d5d94SLen Brown 
2396c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2397c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2398c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
2399c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
2400c98d5d94SLen Brown 				struct thread_data *t, *t2;
2401c98d5d94SLen Brown 				struct core_data *c, *c2;
2402c98d5d94SLen Brown 				struct pkg_data *p, *p2;
2403c98d5d94SLen Brown 
2404c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2405c98d5d94SLen Brown 
2406c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
2407c98d5d94SLen Brown 					continue;
2408c98d5d94SLen Brown 
2409c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2410c98d5d94SLen Brown 
2411c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
2412c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
2413c98d5d94SLen Brown 
2414c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
2415c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
2416c98d5d94SLen Brown 
2417c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
2418c98d5d94SLen Brown 				if (retval)
2419c98d5d94SLen Brown 					return retval;
2420c98d5d94SLen Brown 			}
2421c98d5d94SLen Brown 		}
2422c98d5d94SLen Brown 	}
2423c98d5d94SLen Brown 	return 0;
2424c98d5d94SLen Brown }
2425c98d5d94SLen Brown 
2426c98d5d94SLen Brown /*
2427c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2428c98d5d94SLen Brown  * return max_cpu number
2429c98d5d94SLen Brown  */
2430c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2431103a8feaSLen Brown {
2432103a8feaSLen Brown 	FILE *fp;
2433c98d5d94SLen Brown 	int cpu_num;
2434103a8feaSLen Brown 	int retval;
2435103a8feaSLen Brown 
243657a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2437103a8feaSLen Brown 
2438103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2439b2c95d90SJosh Triplett 	if (retval != 0)
2440b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2441103a8feaSLen Brown 
2442c98d5d94SLen Brown 	while (1) {
2443c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2444103a8feaSLen Brown 		if (retval != 1)
2445103a8feaSLen Brown 			break;
2446103a8feaSLen Brown 
2447c98d5d94SLen Brown 		retval = func(cpu_num);
2448c98d5d94SLen Brown 		if (retval) {
2449c98d5d94SLen Brown 			fclose(fp);
2450c98d5d94SLen Brown 			return(retval);
2451c98d5d94SLen Brown 		}
2452103a8feaSLen Brown 	}
2453103a8feaSLen Brown 	fclose(fp);
2454c98d5d94SLen Brown 	return 0;
2455103a8feaSLen Brown }
2456103a8feaSLen Brown 
2457103a8feaSLen Brown void re_initialize(void)
2458103a8feaSLen Brown {
2459c98d5d94SLen Brown 	free_all_buffers();
2460c98d5d94SLen Brown 	setup_all_buffers();
2461c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2462103a8feaSLen Brown }
2463103a8feaSLen Brown 
2464c98d5d94SLen Brown 
2465103a8feaSLen Brown /*
2466c98d5d94SLen Brown  * count_cpus()
2467c98d5d94SLen Brown  * remember the last one seen, it will be the max
2468103a8feaSLen Brown  */
2469c98d5d94SLen Brown int count_cpus(int cpu)
2470103a8feaSLen Brown {
2471c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
2472c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
2473103a8feaSLen Brown 
2474c98d5d94SLen Brown 	topo.num_cpus += 1;
2475c98d5d94SLen Brown 	return 0;
2476103a8feaSLen Brown }
2477c98d5d94SLen Brown int mark_cpu_present(int cpu)
2478c98d5d94SLen Brown {
2479c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
248015aaa346SLen Brown 	return 0;
2481103a8feaSLen Brown }
2482103a8feaSLen Brown 
2483562a2d37SLen Brown /*
2484562a2d37SLen Brown  * snapshot_proc_interrupts()
2485562a2d37SLen Brown  *
2486562a2d37SLen Brown  * read and record summary of /proc/interrupts
2487562a2d37SLen Brown  *
2488562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2489562a2d37SLen Brown  */
2490562a2d37SLen Brown int snapshot_proc_interrupts(void)
2491562a2d37SLen Brown {
2492562a2d37SLen Brown 	static FILE *fp;
2493562a2d37SLen Brown 	int column, retval;
2494562a2d37SLen Brown 
2495562a2d37SLen Brown 	if (fp == NULL)
2496562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2497562a2d37SLen Brown 	else
2498562a2d37SLen Brown 		rewind(fp);
2499562a2d37SLen Brown 
2500562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2501562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2502562a2d37SLen Brown 		int cpu_number;
2503562a2d37SLen Brown 
2504562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2505562a2d37SLen Brown 		if (retval != 1)
2506562a2d37SLen Brown 			break;
2507562a2d37SLen Brown 
2508562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2509562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2510562a2d37SLen Brown 			return 1;
2511562a2d37SLen Brown 		}
2512562a2d37SLen Brown 
2513562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2514562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2515562a2d37SLen Brown 	}
2516562a2d37SLen Brown 
2517562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2518562a2d37SLen Brown 	while (1) {
2519562a2d37SLen Brown 		int column;
2520562a2d37SLen Brown 		char buf[64];
2521562a2d37SLen Brown 
2522562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2523562a2d37SLen Brown 		if (retval != 1)
2524562a2d37SLen Brown 			break;
2525562a2d37SLen Brown 
2526562a2d37SLen Brown 		/* read the count per cpu */
2527562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2528562a2d37SLen Brown 
2529562a2d37SLen Brown 			int cpu_number, irq_count;
2530562a2d37SLen Brown 
2531562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2532562a2d37SLen Brown 			if (retval != 1)
2533562a2d37SLen Brown 				break;
2534562a2d37SLen Brown 
2535562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2536562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2537562a2d37SLen Brown 
2538562a2d37SLen Brown 		}
2539562a2d37SLen Brown 
2540562a2d37SLen Brown 		while (getc(fp) != '\n')
2541562a2d37SLen Brown 			;	/* flush interrupt description */
2542562a2d37SLen Brown 
2543562a2d37SLen Brown 	}
2544562a2d37SLen Brown 	return 0;
2545562a2d37SLen Brown }
254627d47356SLen Brown /*
2547fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2548fdf676e5SLen Brown  *
2549fdf676e5SLen Brown  * record snapshot of
2550fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2551fdf676e5SLen Brown  *
2552fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2553fdf676e5SLen Brown  */
2554fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2555fdf676e5SLen Brown {
2556fdf676e5SLen Brown 	FILE *fp;
2557fdf676e5SLen Brown 	int retval;
2558fdf676e5SLen Brown 
2559fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2560fdf676e5SLen Brown 
2561fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2562fdf676e5SLen Brown 	if (retval != 1)
2563fdf676e5SLen Brown 		err(1, "GFX rc6");
2564fdf676e5SLen Brown 
2565fdf676e5SLen Brown 	fclose(fp);
2566fdf676e5SLen Brown 
2567fdf676e5SLen Brown 	return 0;
2568fdf676e5SLen Brown }
2569fdf676e5SLen Brown /*
257027d47356SLen Brown  * snapshot_gfx_mhz()
257127d47356SLen Brown  *
257227d47356SLen Brown  * record snapshot of
257327d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
257427d47356SLen Brown  *
257527d47356SLen Brown  * return 1 if config change requires a restart, else return 0
257627d47356SLen Brown  */
257727d47356SLen Brown int snapshot_gfx_mhz(void)
257827d47356SLen Brown {
257927d47356SLen Brown 	static FILE *fp;
258027d47356SLen Brown 	int retval;
258127d47356SLen Brown 
258227d47356SLen Brown 	if (fp == NULL)
258327d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
258422048c54SLen Brown 	else {
258527d47356SLen Brown 		rewind(fp);
258622048c54SLen Brown 		fflush(fp);
258722048c54SLen Brown 	}
258827d47356SLen Brown 
258927d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
259027d47356SLen Brown 	if (retval != 1)
259127d47356SLen Brown 		err(1, "GFX MHz");
259227d47356SLen Brown 
259327d47356SLen Brown 	return 0;
259427d47356SLen Brown }
2595562a2d37SLen Brown 
2596562a2d37SLen Brown /*
2597562a2d37SLen Brown  * snapshot /proc and /sys files
2598562a2d37SLen Brown  *
2599562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2600562a2d37SLen Brown  */
2601562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2602562a2d37SLen Brown {
2603218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2604562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2605562a2d37SLen Brown 			return 1;
2606562a2d37SLen Brown 
2607812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2608fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2609fdf676e5SLen Brown 
2610812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
261127d47356SLen Brown 		snapshot_gfx_mhz();
261227d47356SLen Brown 
2613562a2d37SLen Brown 	return 0;
2614562a2d37SLen Brown }
2615562a2d37SLen Brown 
26168aa2ed0bSLen Brown int exit_requested;
26178aa2ed0bSLen Brown 
26188aa2ed0bSLen Brown static void signal_handler (int signal)
26198aa2ed0bSLen Brown {
26208aa2ed0bSLen Brown 	switch (signal) {
26218aa2ed0bSLen Brown 	case SIGINT:
26228aa2ed0bSLen Brown 		exit_requested = 1;
26238aa2ed0bSLen Brown 		if (debug)
26248aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
26258aa2ed0bSLen Brown 		break;
262607211960SLen Brown 	case SIGUSR1:
262707211960SLen Brown 		if (debug > 1)
262807211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
262907211960SLen Brown 		break;
26308aa2ed0bSLen Brown 	}
2631b9ad8ee0SLen Brown 	/* make sure this manually-invoked interval is at least 1ms long */
2632b9ad8ee0SLen Brown 	nanosleep(&one_msec, NULL);
26338aa2ed0bSLen Brown }
26348aa2ed0bSLen Brown 
26358aa2ed0bSLen Brown void setup_signal_handler(void)
26368aa2ed0bSLen Brown {
26378aa2ed0bSLen Brown 	struct sigaction sa;
26388aa2ed0bSLen Brown 
26398aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
26408aa2ed0bSLen Brown 
26418aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
26428aa2ed0bSLen Brown 
26438aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
26448aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
264507211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
264607211960SLen Brown 		err(1, "sigaction SIGUSR1");
26478aa2ed0bSLen Brown }
2648b9ad8ee0SLen Brown 
264947936f94SArtem Bityutskiy void do_sleep(void)
2650b9ad8ee0SLen Brown {
2651b9ad8ee0SLen Brown 	struct timeval select_timeout;
2652b9ad8ee0SLen Brown 	fd_set readfds;
2653b9ad8ee0SLen Brown 	int retval;
2654b9ad8ee0SLen Brown 
2655b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
2656b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
2657b9ad8ee0SLen Brown 
265847936f94SArtem Bityutskiy 	if (!isatty(fileno(stdin))) {
265947936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
266047936f94SArtem Bityutskiy 		return;
266147936f94SArtem Bityutskiy 	}
2662b9ad8ee0SLen Brown 
266347936f94SArtem Bityutskiy 	select_timeout = interval_tv;
2664b9ad8ee0SLen Brown 	retval = select(1, &readfds, NULL, NULL, &select_timeout);
2665b9ad8ee0SLen Brown 
2666b9ad8ee0SLen Brown 	if (retval == 1) {
2667b9ad8ee0SLen Brown 		switch (getc(stdin)) {
2668b9ad8ee0SLen Brown 		case 'q':
2669b9ad8ee0SLen Brown 			exit_requested = 1;
2670b9ad8ee0SLen Brown 			break;
2671b9ad8ee0SLen Brown 		}
2672b9ad8ee0SLen Brown 		/* make sure this manually-invoked interval is at least 1ms long */
2673b9ad8ee0SLen Brown 		nanosleep(&one_msec, NULL);
2674b9ad8ee0SLen Brown 	}
2675b9ad8ee0SLen Brown }
267647936f94SArtem Bityutskiy 
2677103a8feaSLen Brown void turbostat_loop()
2678103a8feaSLen Brown {
2679c98d5d94SLen Brown 	int retval;
2680e52966c0SLen Brown 	int restarted = 0;
2681c98d5d94SLen Brown 
26828aa2ed0bSLen Brown 	setup_signal_handler();
26838aa2ed0bSLen Brown 
2684103a8feaSLen Brown restart:
2685e52966c0SLen Brown 	restarted++;
2686e52966c0SLen Brown 
2687562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2688c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2689d91bb17cSLen Brown 	if (retval < -1) {
2690d91bb17cSLen Brown 		exit(retval);
2691d91bb17cSLen Brown 	} else if (retval == -1) {
2692e52966c0SLen Brown 		if (restarted > 1) {
2693e52966c0SLen Brown 			exit(retval);
2694e52966c0SLen Brown 		}
2695c98d5d94SLen Brown 		re_initialize();
2696c98d5d94SLen Brown 		goto restart;
2697c98d5d94SLen Brown 	}
2698e52966c0SLen Brown 	restarted = 0;
2699103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2700103a8feaSLen Brown 
2701103a8feaSLen Brown 	while (1) {
2702c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2703103a8feaSLen Brown 			re_initialize();
2704103a8feaSLen Brown 			goto restart;
2705103a8feaSLen Brown 		}
2706b9ad8ee0SLen Brown 		do_sleep();
2707562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2708562a2d37SLen Brown 			goto restart;
2709c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
2710d91bb17cSLen Brown 		if (retval < -1) {
2711d91bb17cSLen Brown 			exit(retval);
2712d91bb17cSLen Brown 		} else if (retval == -1) {
271315aaa346SLen Brown 			re_initialize();
271415aaa346SLen Brown 			goto restart;
271515aaa346SLen Brown 		}
2716103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
2717103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
2718ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2719ba3dec99SLen Brown 			re_initialize();
2720ba3dec99SLen Brown 			goto restart;
2721ba3dec99SLen Brown 		}
2722c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
2723c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
2724b7d8c148SLen Brown 		flush_output_stdout();
27258aa2ed0bSLen Brown 		if (exit_requested)
27268aa2ed0bSLen Brown 			break;
2727b9ad8ee0SLen Brown 		do_sleep();
2728562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2729562a2d37SLen Brown 			goto restart;
2730c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2731d91bb17cSLen Brown 		if (retval < -1) {
2732d91bb17cSLen Brown 			exit(retval);
2733d91bb17cSLen Brown 		} else if (retval == -1) {
2734103a8feaSLen Brown 			re_initialize();
2735103a8feaSLen Brown 			goto restart;
2736103a8feaSLen Brown 		}
2737103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
2738103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
2739ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2740ba3dec99SLen Brown 			re_initialize();
2741ba3dec99SLen Brown 			goto restart;
2742ba3dec99SLen Brown 		}
2743c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
2744c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
2745b7d8c148SLen Brown 		flush_output_stdout();
27468aa2ed0bSLen Brown 		if (exit_requested)
27478aa2ed0bSLen Brown 			break;
2748103a8feaSLen Brown 	}
2749103a8feaSLen Brown }
2750103a8feaSLen Brown 
2751103a8feaSLen Brown void check_dev_msr()
2752103a8feaSLen Brown {
2753103a8feaSLen Brown 	struct stat sb;
27547ce7d5deSPrarit Bhargava 	char pathname[32];
2755103a8feaSLen Brown 
27567ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
27577ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
2758a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2759d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2760103a8feaSLen Brown }
2761103a8feaSLen Brown 
276298481e79SLen Brown void check_permissions()
2763103a8feaSLen Brown {
276498481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
276598481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
276698481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
276798481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
276898481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
276998481e79SLen Brown 	int do_exit = 0;
27707ce7d5deSPrarit Bhargava 	char pathname[32];
277198481e79SLen Brown 
277298481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
277398481e79SLen Brown 	cap_header->pid = getpid();
277498481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
277598481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
277698481e79SLen Brown 		err(-6, "capget(2) failed");
277798481e79SLen Brown 
277898481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
277998481e79SLen Brown 		do_exit++;
278098481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
278198481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
278298481e79SLen Brown 	}
278398481e79SLen Brown 
278498481e79SLen Brown 	/* test file permissions */
27857ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
27867ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
278798481e79SLen Brown 		do_exit++;
278898481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
278998481e79SLen Brown 	}
279098481e79SLen Brown 
279198481e79SLen Brown 	/* if all else fails, thell them to be root */
279298481e79SLen Brown 	if (do_exit)
2793b2c95d90SJosh Triplett 		if (getuid() != 0)
2794d7899447SLen Brown 			warnx("... or simply run as root");
279598481e79SLen Brown 
279698481e79SLen Brown 	if (do_exit)
279798481e79SLen Brown 		exit(-6);
2798103a8feaSLen Brown }
2799103a8feaSLen Brown 
2800d7899447SLen Brown /*
2801d7899447SLen Brown  * NHM adds support for additional MSRs:
2802d7899447SLen Brown  *
2803d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
2804d7899447SLen Brown  *
2805ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
28061df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
2807d7899447SLen Brown  *
2808cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
2809cf4cbe53SLen Brown  *
2810d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
2811d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
2812d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
2813d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
2814d7899447SLen Brown  *
2815ee7e38e3SLen Brown  * Side effect:
28161df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
281733148d67SLen Brown  * sets has_misc_feature_control
2818d7899447SLen Brown  */
2819ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
2820103a8feaSLen Brown {
2821ee7e38e3SLen Brown 	unsigned long long msr;
282221ed5574SLen Brown 	unsigned int base_ratio;
2823ee7e38e3SLen Brown 	int *pkg_cstate_limits;
2824ee7e38e3SLen Brown 
2825103a8feaSLen Brown 	if (!genuine_intel)
2826103a8feaSLen Brown 		return 0;
2827103a8feaSLen Brown 
2828103a8feaSLen Brown 	if (family != 6)
2829103a8feaSLen Brown 		return 0;
2830103a8feaSLen Brown 
283121ed5574SLen Brown 	bclk = discover_bclk(family, model);
283221ed5574SLen Brown 
2833103a8feaSLen Brown 	switch (model) {
2834869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2835869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2836103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
2837869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
2838869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
2839869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2840869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2841ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
2842ee7e38e3SLen Brown 		break;
2843869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
2844869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
2845869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2846869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2847ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
284833148d67SLen Brown 		has_misc_feature_control = 1;
2849ee7e38e3SLen Brown 		break;
2850869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2851869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2852869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2853869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2854869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2855869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2856869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2857869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2858869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2859869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2860869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2861869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2862ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
286333148d67SLen Brown 		has_misc_feature_control = 1;
2864ee7e38e3SLen Brown 		break;
2865d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
2866d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
286733148d67SLen Brown 		has_misc_feature_control = 1;
2868d8ebb442SLen Brown 		break;
2869869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
2870cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2871869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
2872ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
2873ee7e38e3SLen Brown 		break;
2874869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
2875ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
2876cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2877ee7e38e3SLen Brown 		break;
2878869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
2879005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2880ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
2881ee7e38e3SLen Brown 		break;
2882869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
2883ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
2884869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
2885e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
2886e4085d54SLen Brown 		break;
2887103a8feaSLen Brown 	default:
2888103a8feaSLen Brown 		return 0;
2889103a8feaSLen Brown 	}
28901df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2891e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2892ee7e38e3SLen Brown 
2893ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
289421ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
289521ed5574SLen Brown 
289621ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
289721ed5574SLen Brown 	has_base_hz = 1;
2898ee7e38e3SLen Brown 	return 1;
2899103a8feaSLen Brown }
29000f7887c4SLen Brown /*
2901495c7654SLen Brown  * SLV client has support for unique MSRs:
29020f7887c4SLen Brown  *
29030f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
29040f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
29050f7887c4SLen Brown  */
29060f7887c4SLen Brown 
29070f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
29080f7887c4SLen Brown {
29090f7887c4SLen Brown 	if (!genuine_intel)
29100f7887c4SLen Brown 		return 0;
29110f7887c4SLen Brown 
29120f7887c4SLen Brown 	switch (model) {
29130f7887c4SLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
29140f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MERRIFIELD:
29150f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MOOREFIELD:
29160f7887c4SLen Brown 		return 1;
29170f7887c4SLen Brown 	}
29180f7887c4SLen Brown 	return 0;
29190f7887c4SLen Brown }
29207170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
29217170a374SLen Brown {
29227170a374SLen Brown 
29237170a374SLen Brown 	if (!genuine_intel)
29247170a374SLen Brown 		return 0;
29257170a374SLen Brown 
29267170a374SLen Brown 	switch (model) {
29277170a374SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
29287170a374SLen Brown 		return 1;
29297170a374SLen Brown 	}
29307170a374SLen Brown 	return 0;
29317170a374SLen Brown }
2932ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
2933ade0ebacSLen Brown {
2934ade0ebacSLen Brown 
2935ade0ebacSLen Brown 	if (!genuine_intel)
2936ade0ebacSLen Brown 		return 0;
2937ade0ebacSLen Brown 
2938ade0ebacSLen Brown 	switch (model) {
2939ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
2940ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:
2941ade0ebacSLen Brown 		return 1;
2942ade0ebacSLen Brown 	}
2943ade0ebacSLen Brown 	return 0;
2944ade0ebacSLen Brown }
294534c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
294634c76197SLen Brown {
294734c76197SLen Brown 
294834c76197SLen Brown 	if (!genuine_intel)
294934c76197SLen Brown 		return 0;
295034c76197SLen Brown 
295134c76197SLen Brown 	switch (model) {
295234c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
295334c76197SLen Brown 		return 1;
295434c76197SLen Brown 	}
295534c76197SLen Brown 	return 0;
295634c76197SLen Brown }
29570f7887c4SLen Brown 
295831e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
2959d7899447SLen Brown {
29600f7887c4SLen Brown 	if (has_slv_msrs(family, model))
29610f7887c4SLen Brown 		return 0;
29620f7887c4SLen Brown 
2963d7899447SLen Brown 	switch (model) {
2964d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
2965869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2966869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2967869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
2968005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2969d7899447SLen Brown 		return 0;
2970d7899447SLen Brown 	default:
2971d7899447SLen Brown 		return 1;
2972d7899447SLen Brown 	}
2973d7899447SLen Brown }
29740f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
29750f7887c4SLen Brown {
29760f7887c4SLen Brown 	if (has_slv_msrs(family, model))
29770f7887c4SLen Brown 		return 1;
29780f7887c4SLen Brown 
29790f7887c4SLen Brown 	return 0;
29800f7887c4SLen Brown }
29816574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
29826574a5d5SLen Brown {
29836574a5d5SLen Brown 	if (!genuine_intel)
29846574a5d5SLen Brown 		return 0;
29856574a5d5SLen Brown 
29866574a5d5SLen Brown 	if (family != 6)
29876574a5d5SLen Brown 		return 0;
29886574a5d5SLen Brown 
29896574a5d5SLen Brown 	switch (model) {
2990869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2991869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
29926574a5d5SLen Brown 		return 1;
29936574a5d5SLen Brown 	default:
29946574a5d5SLen Brown 		return 0;
29956574a5d5SLen Brown 	}
29966574a5d5SLen Brown }
2997fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2998fcd17211SLen Brown {
2999fcd17211SLen Brown 	if (!genuine_intel)
3000fcd17211SLen Brown 		return 0;
3001fcd17211SLen Brown 
3002fcd17211SLen Brown 	if (family != 6)
3003fcd17211SLen Brown 		return 0;
3004fcd17211SLen Brown 
3005fcd17211SLen Brown 	switch (model) {
3006869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3007fcd17211SLen Brown 		return 1;
3008fcd17211SLen Brown 	default:
3009fcd17211SLen Brown 		return 0;
3010fcd17211SLen Brown 	}
3011fcd17211SLen Brown }
3012fcd17211SLen Brown 
3013fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3014fb5d4327SDasaratharaman Chandramouli {
3015fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3016fb5d4327SDasaratharaman Chandramouli 		return 0;
3017fb5d4327SDasaratharaman Chandramouli 
3018fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3019fb5d4327SDasaratharaman Chandramouli 		return 0;
3020fb5d4327SDasaratharaman Chandramouli 
3021fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3022869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3023005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3024fb5d4327SDasaratharaman Chandramouli 		return 1;
3025fb5d4327SDasaratharaman Chandramouli 	default:
3026fb5d4327SDasaratharaman Chandramouli 		return 0;
3027fb5d4327SDasaratharaman Chandramouli 	}
3028fb5d4327SDasaratharaman Chandramouli }
302931e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
303031e07522SLen Brown {
303131e07522SLen Brown 	if (!genuine_intel)
303231e07522SLen Brown 		return 0;
303331e07522SLen Brown 
303431e07522SLen Brown 	if (family != 6)
303531e07522SLen Brown 		return 0;
303631e07522SLen Brown 
303731e07522SLen Brown 	switch (model) {
303831e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
303931e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
304031e07522SLen Brown 		return 1;
304131e07522SLen Brown 	default:
304231e07522SLen Brown 		return 0;
304331e07522SLen Brown 	}
304431e07522SLen Brown }
30456fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
30466fb3143bSLen Brown {
30476fb3143bSLen Brown 	if (!genuine_intel)
30486fb3143bSLen Brown 		return 0;
30496fb3143bSLen Brown 
30506fb3143bSLen Brown 	if (family != 6)
30516fb3143bSLen Brown 		return 0;
30526fb3143bSLen Brown 
30536fb3143bSLen Brown 	switch (model) {
3054869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3055869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3056869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3057869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3058869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3059869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3060869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3061869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3062869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3063869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3064869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3065869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3066869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3067869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
30686fb3143bSLen Brown 
3069869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3070005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
30716fb3143bSLen Brown 		return 1;
30726fb3143bSLen Brown 	default:
30736fb3143bSLen Brown 		return 0;
30746fb3143bSLen Brown 	}
30756fb3143bSLen Brown }
30766fb3143bSLen Brown 
3077fcd17211SLen Brown static void
30781b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3079fcd17211SLen Brown {
3080fcd17211SLen Brown 	if (!do_nhm_platform_info)
3081fcd17211SLen Brown 		return;
3082fcd17211SLen Brown 
3083fcd17211SLen Brown 	dump_nhm_platform_info();
3084fcd17211SLen Brown 
3085fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
3086fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
3087fcd17211SLen Brown 
3088fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
3089fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
3090fcd17211SLen Brown 
309131e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
309231e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
3093fcd17211SLen Brown 
30940f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
30950f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
30960f7887c4SLen Brown 
3097fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
3098fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
3099fb5d4327SDasaratharaman Chandramouli 
31006fb3143bSLen Brown 	if (has_config_tdp(family, model))
31016fb3143bSLen Brown 		dump_config_tdp();
31026fb3143bSLen Brown 
3103fcd17211SLen Brown 	dump_nhm_cst_cfg();
3104fcd17211SLen Brown }
3105fcd17211SLen Brown 
310641618e63SLen Brown static void
310741618e63SLen Brown dump_sysfs_cstate_config(void)
310841618e63SLen Brown {
310941618e63SLen Brown 	char path[64];
311041618e63SLen Brown 	char name_buf[16];
311141618e63SLen Brown 	char desc[64];
311241618e63SLen Brown 	FILE *input;
311341618e63SLen Brown 	int state;
311441618e63SLen Brown 	char *sp;
311541618e63SLen Brown 
311641618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
311741618e63SLen Brown 		return;
311841618e63SLen Brown 
311941618e63SLen Brown 	for (state = 0; state < 10; ++state) {
312041618e63SLen Brown 
312141618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
312241618e63SLen Brown 			base_cpu, state);
312341618e63SLen Brown 		input = fopen(path, "r");
312441618e63SLen Brown 		if (input == NULL)
312541618e63SLen Brown 			continue;
312641618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
312741618e63SLen Brown 
312841618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
312941618e63SLen Brown 		sp = strchr(name_buf, '-');
313041618e63SLen Brown 		if (!sp)
313141618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
313241618e63SLen Brown 		*sp = '\0';
313341618e63SLen Brown 
313441618e63SLen Brown 		fclose(input);
313541618e63SLen Brown 
313641618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
313741618e63SLen Brown 			base_cpu, state);
313841618e63SLen Brown 		input = fopen(path, "r");
313941618e63SLen Brown 		if (input == NULL)
314041618e63SLen Brown 			continue;
314141618e63SLen Brown 		fgets(desc, sizeof(desc), input);
314241618e63SLen Brown 
314341618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
314441618e63SLen Brown 		fclose(input);
314541618e63SLen Brown 	}
314641618e63SLen Brown }
31477293fccdSLen Brown static void
31487293fccdSLen Brown dump_sysfs_pstate_config(void)
31497293fccdSLen Brown {
31507293fccdSLen Brown 	char path[64];
31517293fccdSLen Brown 	char driver_buf[64];
31527293fccdSLen Brown 	char governor_buf[64];
31537293fccdSLen Brown 	FILE *input;
31547293fccdSLen Brown 	int turbo;
31557293fccdSLen Brown 
31567293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
31577293fccdSLen Brown 			base_cpu);
31587293fccdSLen Brown 	input = fopen(path, "r");
31597293fccdSLen Brown 	if (input == NULL) {
31607293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
31617293fccdSLen Brown 		return;
31627293fccdSLen Brown 	}
31637293fccdSLen Brown 	fgets(driver_buf, sizeof(driver_buf), input);
31647293fccdSLen Brown 	fclose(input);
31657293fccdSLen Brown 
31667293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
31677293fccdSLen Brown 			base_cpu);
31687293fccdSLen Brown 	input = fopen(path, "r");
31697293fccdSLen Brown 	if (input == NULL) {
31707293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
31717293fccdSLen Brown 		return;
31727293fccdSLen Brown 	}
31737293fccdSLen Brown 	fgets(governor_buf, sizeof(governor_buf), input);
31747293fccdSLen Brown 	fclose(input);
31757293fccdSLen Brown 
31767293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
31777293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
31787293fccdSLen Brown 
31797293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
31807293fccdSLen Brown 	input = fopen(path, "r");
31817293fccdSLen Brown 	if (input != NULL) {
31827293fccdSLen Brown 		fscanf(input, "%d", &turbo);
31837293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
31847293fccdSLen Brown 		fclose(input);
31857293fccdSLen Brown 	}
31867293fccdSLen Brown 
31877293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
31887293fccdSLen Brown 	input = fopen(path, "r");
31897293fccdSLen Brown 	if (input != NULL) {
31907293fccdSLen Brown 		fscanf(input, "%d", &turbo);
31917293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
31927293fccdSLen Brown 		fclose(input);
31937293fccdSLen Brown 	}
31947293fccdSLen Brown }
319541618e63SLen Brown 
31966574a5d5SLen Brown 
3197889facbeSLen Brown /*
3198889facbeSLen Brown  * print_epb()
3199889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3200889facbeSLen Brown  */
3201889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3202889facbeSLen Brown {
3203889facbeSLen Brown 	unsigned long long msr;
3204889facbeSLen Brown 	char *epb_string;
3205889facbeSLen Brown 	int cpu;
3206889facbeSLen Brown 
3207889facbeSLen Brown 	if (!has_epb)
3208889facbeSLen Brown 		return 0;
3209889facbeSLen Brown 
3210889facbeSLen Brown 	cpu = t->cpu_id;
3211889facbeSLen Brown 
3212889facbeSLen Brown 	/* EPB is per-package */
3213889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3214889facbeSLen Brown 		return 0;
3215889facbeSLen Brown 
3216889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3217b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3218889facbeSLen Brown 		return -1;
3219889facbeSLen Brown 	}
3220889facbeSLen Brown 
3221889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3222889facbeSLen Brown 		return 0;
3223889facbeSLen Brown 
3224e9be7dd6SLen Brown 	switch (msr & 0xF) {
3225889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3226889facbeSLen Brown 		epb_string = "performance";
3227889facbeSLen Brown 		break;
3228889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3229889facbeSLen Brown 		epb_string = "balanced";
3230889facbeSLen Brown 		break;
3231889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3232889facbeSLen Brown 		epb_string = "powersave";
3233889facbeSLen Brown 		break;
3234889facbeSLen Brown 	default:
3235889facbeSLen Brown 		epb_string = "custom";
3236889facbeSLen Brown 		break;
3237889facbeSLen Brown 	}
3238b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3239889facbeSLen Brown 
3240889facbeSLen Brown 	return 0;
3241889facbeSLen Brown }
32427f5c258eSLen Brown /*
32437f5c258eSLen Brown  * print_hwp()
32447f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
32457f5c258eSLen Brown  */
32467f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
32477f5c258eSLen Brown {
32487f5c258eSLen Brown 	unsigned long long msr;
32497f5c258eSLen Brown 	int cpu;
32507f5c258eSLen Brown 
32517f5c258eSLen Brown 	if (!has_hwp)
32527f5c258eSLen Brown 		return 0;
32537f5c258eSLen Brown 
32547f5c258eSLen Brown 	cpu = t->cpu_id;
32557f5c258eSLen Brown 
32567f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
32577f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
32587f5c258eSLen Brown 		return 0;
32597f5c258eSLen Brown 
32607f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3261b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
32627f5c258eSLen Brown 		return -1;
32637f5c258eSLen Brown 	}
32647f5c258eSLen Brown 
32657f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
32667f5c258eSLen Brown 		return 0;
32677f5c258eSLen Brown 
3268b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
32697f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
32707f5c258eSLen Brown 
32717f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
32727f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
32737f5c258eSLen Brown 		return 0;
32747f5c258eSLen Brown 
32757f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
32767f5c258eSLen Brown 		return 0;
32777f5c258eSLen Brown 
3278b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
32796dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
32807f5c258eSLen Brown 			cpu, msr,
32817f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
32827f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
32837f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
32847f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
32857f5c258eSLen Brown 
32867f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
32877f5c258eSLen Brown 		return 0;
32887f5c258eSLen Brown 
3289b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
32906dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
32917f5c258eSLen Brown 			cpu, msr,
32927f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
32937f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
32947f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
32957f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
32967f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
32977f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
32987f5c258eSLen Brown 
32997f5c258eSLen Brown 	if (has_hwp_pkg) {
33007f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
33017f5c258eSLen Brown 			return 0;
33027f5c258eSLen Brown 
3303b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
33046dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
33057f5c258eSLen Brown 			cpu, msr,
33067f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
33077f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
33087f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
33097f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
33107f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
33117f5c258eSLen Brown 	}
33127f5c258eSLen Brown 	if (has_hwp_notify) {
33137f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
33147f5c258eSLen Brown 			return 0;
33157f5c258eSLen Brown 
3316b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
33177f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
33187f5c258eSLen Brown 			cpu, msr,
33197f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
33207f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
33217f5c258eSLen Brown 	}
33227f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
33237f5c258eSLen Brown 		return 0;
33247f5c258eSLen Brown 
3325b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
33267f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
33277f5c258eSLen Brown 			cpu, msr,
33287f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
33297f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3330889facbeSLen Brown 
3331889facbeSLen Brown 	return 0;
3332889facbeSLen Brown }
3333889facbeSLen Brown 
33343a9a941dSLen Brown /*
33353a9a941dSLen Brown  * print_perf_limit()
33363a9a941dSLen Brown  */
33373a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
33383a9a941dSLen Brown {
33393a9a941dSLen Brown 	unsigned long long msr;
33403a9a941dSLen Brown 	int cpu;
33413a9a941dSLen Brown 
33423a9a941dSLen Brown 	cpu = t->cpu_id;
33433a9a941dSLen Brown 
33443a9a941dSLen Brown 	/* per-package */
33453a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
33463a9a941dSLen Brown 		return 0;
33473a9a941dSLen Brown 
33483a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3349b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
33503a9a941dSLen Brown 		return -1;
33513a9a941dSLen Brown 	}
33523a9a941dSLen Brown 
33533a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
33543a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3355b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3356b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3357e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
33583a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3359e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3360e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3361e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3362e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3363e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3364e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3365e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3366e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3367e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3368e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3369e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3370e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3371b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3372e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
33733a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3374e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3375e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3376e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3377e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3378e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3379e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3380e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3381e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3382e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3383e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3384e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3385e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
33863a9a941dSLen Brown 
33873a9a941dSLen Brown 	}
33883a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
33893a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3390b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3391b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
33923a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
33933a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
33943a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
33953a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
33963a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
33973a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
33983a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
33993a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3400b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
34013a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
34023a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
34033a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
34043a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
34053a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
34063a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
34073a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
34083a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
34093a9a941dSLen Brown 	}
34103a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
34113a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3412b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3413b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
34143a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
34153a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
34163a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
34173a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
34183a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
34193a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3420b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
34213a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
34223a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
34233a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
34243a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
34253a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
34263a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
34273a9a941dSLen Brown 	}
34283a9a941dSLen Brown 	return 0;
34293a9a941dSLen Brown }
34303a9a941dSLen Brown 
3431889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3432889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3433889facbeSLen Brown 
34341b69317dSColin Ian King double get_tdp(unsigned int model)
3435144b44b1SLen Brown {
3436144b44b1SLen Brown 	unsigned long long msr;
3437144b44b1SLen Brown 
3438144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
34397ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3440144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3441144b44b1SLen Brown 
3442144b44b1SLen Brown 	switch (model) {
3443869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
3444869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:
3445144b44b1SLen Brown 		return 30.0;
3446144b44b1SLen Brown 	default:
3447144b44b1SLen Brown 		return 135.0;
3448144b44b1SLen Brown 	}
3449144b44b1SLen Brown }
3450144b44b1SLen Brown 
345140ee8e3bSAndrey Semin /*
345240ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
345340ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
345440ee8e3bSAndrey Semin  */
345540ee8e3bSAndrey Semin static double
345640ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
345740ee8e3bSAndrey Semin {
345840ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
345940ee8e3bSAndrey Semin 
346040ee8e3bSAndrey Semin 	switch (model) {
3461869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3462869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3463869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3464869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3465005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
346640ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
346740ee8e3bSAndrey Semin 	default:
346840ee8e3bSAndrey Semin 		return (rapl_energy_units);
346940ee8e3bSAndrey Semin 	}
347040ee8e3bSAndrey Semin }
347140ee8e3bSAndrey Semin 
3472144b44b1SLen Brown 
3473889facbeSLen Brown /*
3474889facbeSLen Brown  * rapl_probe()
3475889facbeSLen Brown  *
3476144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3477889facbeSLen Brown  */
3478889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
3479889facbeSLen Brown {
3480889facbeSLen Brown 	unsigned long long msr;
3481144b44b1SLen Brown 	unsigned int time_unit;
3482889facbeSLen Brown 	double tdp;
3483889facbeSLen Brown 
3484889facbeSLen Brown 	if (!genuine_intel)
3485889facbeSLen Brown 		return;
3486889facbeSLen Brown 
3487889facbeSLen Brown 	if (family != 6)
3488889facbeSLen Brown 		return;
3489889facbeSLen Brown 
3490889facbeSLen Brown 	switch (model) {
3491869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3492869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3493869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3494869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3495869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3496869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3497869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3498144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3499812db3f7SLen Brown 		if (rapl_joules) {
3500812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3501812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3502812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3503812db3f7SLen Brown 		} else {
3504812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3505812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3506812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3507812db3f7SLen Brown 		}
3508889facbeSLen Brown 		break;
3509869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3510ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3511e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3512812db3f7SLen Brown 		if (rapl_joules)
3513812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3514812db3f7SLen Brown 		else
3515812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3516e4085d54SLen Brown 		break;
3517869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3518869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3519869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3520869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
352181824921SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO;
3522812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3523812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3524812db3f7SLen Brown 		if (rapl_joules) {
3525812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3526812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3527812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
352881824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3529812db3f7SLen Brown 		} else {
3530812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3531812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3532812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
353381824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3534812db3f7SLen Brown 		}
35350b2bb692SLen Brown 		break;
3536869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3537869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3538869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3539869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3540869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3541005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
35420b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3543812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3544812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3545812db3f7SLen Brown 		if (rapl_joules) {
3546812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3547812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3548812db3f7SLen Brown 		} else {
3549812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3550812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3551812db3f7SLen Brown 		}
3552e6f9bb3cSLen Brown 		break;
3553869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3554869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
35550b2bb692SLen 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;
3556812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3557812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3558812db3f7SLen Brown 		if (rapl_joules) {
3559812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3560812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3561812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3562812db3f7SLen Brown 		} else {
3563812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3564812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3565812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3566812db3f7SLen Brown 		}
3567144b44b1SLen Brown 		break;
3568869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3569869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3570144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3571812db3f7SLen Brown 		if (rapl_joules) {
3572812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3573812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3574812db3f7SLen Brown 		} else {
3575812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3576812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3577812db3f7SLen Brown 		}
3578889facbeSLen Brown 		break;
3579869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
35800f644909SJacob Pan 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
3581812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3582812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3583812db3f7SLen Brown 		if (rapl_joules) {
3584812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3585812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3586812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3587812db3f7SLen Brown 		} else {
3588812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3589812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3590812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3591812db3f7SLen Brown 		}
35920f644909SJacob Pan 		break;
3593889facbeSLen Brown 	default:
3594889facbeSLen Brown 		return;
3595889facbeSLen Brown 	}
3596889facbeSLen Brown 
3597889facbeSLen Brown 	/* units on package 0, verify later other packages match */
35987ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3599889facbeSLen Brown 		return;
3600889facbeSLen Brown 
3601889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3602869ce69eSLen Brown 	if (model == INTEL_FAM6_ATOM_SILVERMONT1)
3603144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3604144b44b1SLen Brown 	else
3605889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3606889facbeSLen Brown 
360740ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
360840ee8e3bSAndrey Semin 
3609144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3610144b44b1SLen Brown 	if (time_unit == 0)
3611144b44b1SLen Brown 		time_unit = 0xA;
3612889facbeSLen Brown 
3613144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3614144b44b1SLen Brown 
3615144b44b1SLen Brown 	tdp = get_tdp(model);
3616889facbeSLen Brown 
3617889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
361896e47158SLen Brown 	if (!quiet)
3619b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3620889facbeSLen Brown 
3621889facbeSLen Brown 	return;
3622889facbeSLen Brown }
3623889facbeSLen Brown 
36241b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
36253a9a941dSLen Brown {
36263a9a941dSLen Brown 	if (!genuine_intel)
36273a9a941dSLen Brown 		return;
36283a9a941dSLen Brown 
36293a9a941dSLen Brown 	if (family != 6)
36303a9a941dSLen Brown 		return;
36313a9a941dSLen Brown 
36323a9a941dSLen Brown 	switch (model) {
3633869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3634869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3635869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
36363a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
3637869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
36383a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
36393a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
36403a9a941dSLen Brown 	default:
36413a9a941dSLen Brown 		return;
36423a9a941dSLen Brown 	}
36433a9a941dSLen Brown }
36443a9a941dSLen Brown 
3645*ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3646*ac980e13SArtem Bityutskiy {
3647*ac980e13SArtem Bityutskiy 	if (is_skx(family, model) || is_bdx(family, model))
3648*ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
3649*ac980e13SArtem Bityutskiy }
3650*ac980e13SArtem Bityutskiy 
3651889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3652889facbeSLen Brown {
3653889facbeSLen Brown 	unsigned long long msr;
3654f4896fa5SLen Brown 	unsigned int dts, dts2;
3655889facbeSLen Brown 	int cpu;
3656889facbeSLen Brown 
3657889facbeSLen Brown 	if (!(do_dts || do_ptm))
3658889facbeSLen Brown 		return 0;
3659889facbeSLen Brown 
3660889facbeSLen Brown 	cpu = t->cpu_id;
3661889facbeSLen Brown 
3662889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
3663889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3664889facbeSLen Brown 		return 0;
3665889facbeSLen Brown 
3666889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3667b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3668889facbeSLen Brown 		return -1;
3669889facbeSLen Brown 	}
3670889facbeSLen Brown 
3671889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3672889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3673889facbeSLen Brown 			return 0;
3674889facbeSLen Brown 
3675889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3676b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3677889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
3678889facbeSLen Brown 
3679889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3680889facbeSLen Brown 			return 0;
3681889facbeSLen Brown 
3682889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3683889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3684b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3685889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3686889facbeSLen Brown 	}
3687889facbeSLen Brown 
3688889facbeSLen Brown 
3689f4896fa5SLen Brown 	if (do_dts && debug) {
3690889facbeSLen Brown 		unsigned int resolution;
3691889facbeSLen Brown 
3692889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3693889facbeSLen Brown 			return 0;
3694889facbeSLen Brown 
3695889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3696889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
3697b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3698889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
3699889facbeSLen Brown 
3700889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3701889facbeSLen Brown 			return 0;
3702889facbeSLen Brown 
3703889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3704889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3705b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3706889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3707889facbeSLen Brown 	}
3708889facbeSLen Brown 
3709889facbeSLen Brown 	return 0;
3710889facbeSLen Brown }
3711889facbeSLen Brown 
3712889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3713889facbeSLen Brown {
3714b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
3715889facbeSLen Brown 		cpu, label,
3716889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
3717889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
3718889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3719889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
3720889facbeSLen Brown 
3721889facbeSLen Brown 	return;
3722889facbeSLen Brown }
3723889facbeSLen Brown 
3724889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3725889facbeSLen Brown {
3726889facbeSLen Brown 	unsigned long long msr;
3727889facbeSLen Brown 	int cpu;
3728889facbeSLen Brown 
3729889facbeSLen Brown 	if (!do_rapl)
3730889facbeSLen Brown 		return 0;
3731889facbeSLen Brown 
3732889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
3733889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3734889facbeSLen Brown 		return 0;
3735889facbeSLen Brown 
3736889facbeSLen Brown 	cpu = t->cpu_id;
3737889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3738b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3739889facbeSLen Brown 		return -1;
3740889facbeSLen Brown 	}
3741889facbeSLen Brown 
3742889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3743889facbeSLen Brown 		return -1;
3744889facbeSLen Brown 
374596e47158SLen Brown 	fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3746144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
374796e47158SLen Brown 
3748144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
3749144b44b1SLen Brown 
3750889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3751889facbeSLen Brown                 	return -5;
3752889facbeSLen Brown 
3753889facbeSLen Brown 
3754b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3755889facbeSLen Brown 			cpu, msr,
3756889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3757889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3758889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3759889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3760889facbeSLen Brown 
3761144b44b1SLen Brown 	}
3762144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
3763144b44b1SLen Brown 
3764889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3765889facbeSLen Brown 			return -9;
3766889facbeSLen Brown 
3767b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
3768889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
3769889facbeSLen Brown 
3770889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
3771b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
3772889facbeSLen Brown 			cpu,
3773889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
3774889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
3775889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3776889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
3777889facbeSLen Brown 	}
3778889facbeSLen Brown 
37790b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
3780889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3781889facbeSLen Brown                 	return -6;
3782889facbeSLen Brown 
3783b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3784889facbeSLen Brown 			cpu, msr,
3785889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3786889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3787889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3788889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
37890b2bb692SLen Brown 	}
37900b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
3791889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3792889facbeSLen Brown 			return -9;
3793b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
3794889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3795889facbeSLen Brown 
3796889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
3797889facbeSLen Brown 	}
3798144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
3799889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3800889facbeSLen Brown 			return -7;
3801889facbeSLen Brown 
3802b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
3803144b44b1SLen Brown 	}
38049148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
3805889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3806889facbeSLen Brown 			return -9;
3807b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3808889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3809889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
3810889facbeSLen Brown 	}
3811889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
3812889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3813889facbeSLen Brown 			return -8;
3814889facbeSLen Brown 
3815b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
3816889facbeSLen Brown 
3817889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3818889facbeSLen Brown 			return -9;
3819b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3820889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3821889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
3822889facbeSLen Brown 	}
3823889facbeSLen Brown 	return 0;
3824889facbeSLen Brown }
3825889facbeSLen Brown 
3826d7899447SLen Brown /*
3827d7899447SLen Brown  * SNB adds support for additional MSRs:
3828d7899447SLen Brown  *
3829d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
3830d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
3831d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
3832d7899447SLen Brown  */
3833103a8feaSLen Brown 
3834d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
3835103a8feaSLen Brown {
3836103a8feaSLen Brown 	if (!genuine_intel)
3837103a8feaSLen Brown 		return 0;
3838103a8feaSLen Brown 
3839103a8feaSLen Brown 	switch (model) {
3840869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3841869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3842869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3843869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3844869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3845869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
3846869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3847869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3848869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3849869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3850869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3851869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3852869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3853869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3854869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3855869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3856869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3857869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3858ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
38595bbac26eSXiaolong Wang 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3860103a8feaSLen Brown 		return 1;
3861103a8feaSLen Brown 	}
3862103a8feaSLen Brown 	return 0;
3863103a8feaSLen Brown }
3864103a8feaSLen Brown 
3865d7899447SLen Brown /*
3866d7899447SLen Brown  * HSW adds support for additional MSRs:
3867d7899447SLen Brown  *
3868d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
3869d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
3870d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
38715a63426eSLen Brown  *
38725a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
38735a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
38745a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
38755a63426eSLen Brown  *
3876d7899447SLen Brown  */
3877d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
3878ca58710fSKristen Carlson Accardi {
3879ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
3880ca58710fSKristen Carlson Accardi 		return 0;
3881ca58710fSKristen Carlson Accardi 
3882ca58710fSKristen Carlson Accardi 	switch (model) {
3883869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3884869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3885869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3886869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3887869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3888869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3889869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3890ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3891ca58710fSKristen Carlson Accardi 		return 1;
3892ca58710fSKristen Carlson Accardi 	}
3893ca58710fSKristen Carlson Accardi 	return 0;
3894ca58710fSKristen Carlson Accardi }
3895ca58710fSKristen Carlson Accardi 
38960b2bb692SLen Brown /*
38970b2bb692SLen Brown  * SKL adds support for additional MSRS:
38980b2bb692SLen Brown  *
38990b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
39000b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
39010b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
39020b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
39030b2bb692SLen Brown  */
39040b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
39050b2bb692SLen Brown {
39060b2bb692SLen Brown 	if (!genuine_intel)
39070b2bb692SLen Brown 		return 0;
39080b2bb692SLen Brown 
39090b2bb692SLen Brown 	switch (model) {
3910869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3911869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3912869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3913869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
39140b2bb692SLen Brown 		return 1;
39150b2bb692SLen Brown 	}
39160b2bb692SLen Brown 	return 0;
39170b2bb692SLen Brown }
39180b2bb692SLen Brown 
3919144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
3920144b44b1SLen Brown {
3921144b44b1SLen Brown 	if (!genuine_intel)
3922144b44b1SLen Brown 		return 0;
3923144b44b1SLen Brown 	switch (model) {
3924869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3925869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3926144b44b1SLen Brown 		return 1;
3927144b44b1SLen Brown 	}
3928144b44b1SLen Brown 	return 0;
3929144b44b1SLen Brown }
3930144b44b1SLen Brown 
3931fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
3932fb5d4327SDasaratharaman Chandramouli {
3933fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3934fb5d4327SDasaratharaman Chandramouli 		return 0;
3935fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3936869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3937005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3938fb5d4327SDasaratharaman Chandramouli 		return 1;
3939fb5d4327SDasaratharaman Chandramouli 	}
3940fb5d4327SDasaratharaman Chandramouli 	return 0;
3941fb5d4327SDasaratharaman Chandramouli }
3942fb5d4327SDasaratharaman Chandramouli 
3943b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3944b2b34dfeSHubert Chrzaniuk {
3945b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
3946b2b34dfeSHubert Chrzaniuk 		return 1024;
3947b2b34dfeSHubert Chrzaniuk 	return 1;
3948b2b34dfeSHubert Chrzaniuk }
3949b2b34dfeSHubert Chrzaniuk 
3950144b44b1SLen Brown #define SLM_BCLK_FREQS 5
3951144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3952144b44b1SLen Brown 
3953144b44b1SLen Brown double slm_bclk(void)
3954144b44b1SLen Brown {
3955144b44b1SLen Brown 	unsigned long long msr = 3;
3956144b44b1SLen Brown 	unsigned int i;
3957144b44b1SLen Brown 	double freq;
3958144b44b1SLen Brown 
39597ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3960b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
3961144b44b1SLen Brown 
3962144b44b1SLen Brown 	i = msr & 0xf;
3963144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
3964b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
39650a91e551SColin Ian King 		i = 3;
3966144b44b1SLen Brown 	}
3967144b44b1SLen Brown 	freq = slm_freq_table[i];
3968144b44b1SLen Brown 
396996e47158SLen Brown 	if (!quiet)
3970b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3971144b44b1SLen Brown 
3972144b44b1SLen Brown 	return freq;
3973144b44b1SLen Brown }
3974144b44b1SLen Brown 
3975103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
3976103a8feaSLen Brown {
3977121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
3978103a8feaSLen Brown 		return 100.00;
3979144b44b1SLen Brown 	else if (is_slm(family, model))
3980144b44b1SLen Brown 		return slm_bclk();
3981103a8feaSLen Brown 	else
3982103a8feaSLen Brown 		return 133.33;
3983103a8feaSLen Brown }
3984103a8feaSLen Brown 
3985889facbeSLen Brown /*
3986889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3987889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
3988889facbeSLen Brown  * This is usually equal to tjMax.
3989889facbeSLen Brown  *
3990889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
3991889facbeSLen Brown  * but also allow cmdline over-ride with -T.
3992889facbeSLen Brown  *
3993889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
3994889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
3995889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3996889facbeSLen Brown  */
3997889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3998889facbeSLen Brown {
3999889facbeSLen Brown 	unsigned long long msr;
4000889facbeSLen Brown 	unsigned int target_c_local;
4001889facbeSLen Brown 	int cpu;
4002889facbeSLen Brown 
4003889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
4004889facbeSLen Brown 	if (!(do_dts || do_ptm))
4005889facbeSLen Brown 		return 0;
4006889facbeSLen Brown 
4007889facbeSLen Brown 	/* this is a per-package concept */
4008889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4009889facbeSLen Brown 		return 0;
4010889facbeSLen Brown 
4011889facbeSLen Brown 	cpu = t->cpu_id;
4012889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4013b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4014889facbeSLen Brown 		return -1;
4015889facbeSLen Brown 	}
4016889facbeSLen Brown 
4017889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
4018889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
4019b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4020889facbeSLen Brown 			cpu, tcc_activation_temp);
4021889facbeSLen Brown 		return 0;
4022889facbeSLen Brown 	}
4023889facbeSLen Brown 
4024889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
4025d7899447SLen Brown 	if (!do_nhm_platform_info)
4026889facbeSLen Brown 		goto guess;
4027889facbeSLen Brown 
40287ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4029889facbeSLen Brown 		goto guess;
4030889facbeSLen Brown 
40313482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
4032889facbeSLen Brown 
403396e47158SLen Brown 	if (!quiet)
4034b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4035889facbeSLen Brown 			cpu, msr, target_c_local);
4036889facbeSLen Brown 
40373482124aSJean Delvare 	if (!target_c_local)
4038889facbeSLen Brown 		goto guess;
4039889facbeSLen Brown 
4040889facbeSLen Brown 	tcc_activation_temp = target_c_local;
4041889facbeSLen Brown 
4042889facbeSLen Brown 	return 0;
4043889facbeSLen Brown 
4044889facbeSLen Brown guess:
4045889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
4046b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4047889facbeSLen Brown 		cpu, tcc_activation_temp);
4048889facbeSLen Brown 
4049889facbeSLen Brown 	return 0;
4050889facbeSLen Brown }
405169807a63SLen Brown 
4052aa8d8cc7SLen Brown void decode_feature_control_msr(void)
4053aa8d8cc7SLen Brown {
4054aa8d8cc7SLen Brown 	unsigned long long msr;
4055aa8d8cc7SLen Brown 
4056aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4057aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4058aa8d8cc7SLen Brown 			base_cpu, msr,
4059aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4060aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
4061aa8d8cc7SLen Brown }
4062aa8d8cc7SLen Brown 
406369807a63SLen Brown void decode_misc_enable_msr(void)
406469807a63SLen Brown {
406569807a63SLen Brown 	unsigned long long msr;
406669807a63SLen Brown 
4067f26b1519SLen Brown 	if (!genuine_intel)
4068f26b1519SLen Brown 		return;
4069f26b1519SLen Brown 
407069807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4071e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
407269807a63SLen Brown 			base_cpu, msr,
4073e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4074e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4075fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4076e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4077e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
407869807a63SLen Brown }
407969807a63SLen Brown 
408033148d67SLen Brown void decode_misc_feature_control(void)
408133148d67SLen Brown {
408233148d67SLen Brown 	unsigned long long msr;
408333148d67SLen Brown 
408433148d67SLen Brown 	if (!has_misc_feature_control)
408533148d67SLen Brown 		return;
408633148d67SLen Brown 
408733148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
408833148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
408933148d67SLen Brown 			base_cpu, msr,
409033148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
409133148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
409233148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
409333148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
409433148d67SLen Brown }
4095f0057310SLen Brown /*
4096f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
4097f0057310SLen Brown  *
4098f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
4099f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
4100f0057310SLen Brown  * bit[1] less so...
4101f0057310SLen Brown  */
4102f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
4103f0057310SLen Brown {
4104f0057310SLen Brown 	unsigned long long msr;
4105f0057310SLen Brown 
4106f0057310SLen Brown 	if (!do_nhm_platform_info)
4107f0057310SLen Brown 		return;
4108f0057310SLen Brown 
4109cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
4110cf4cbe53SLen Brown 		return;
4111cf4cbe53SLen Brown 
4112f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4113ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4114f0057310SLen Brown 			base_cpu, msr,
4115f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
4116ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
4117ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
4118f0057310SLen Brown }
411971616c8eSLen Brown /*
412071616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
412171616c8eSLen Brown  *
412271616c8eSLen Brown  * This MSRs are present on Silvermont processors,
412371616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
412471616c8eSLen Brown  */
412571616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
412671616c8eSLen Brown {
412771616c8eSLen Brown 	unsigned long long msr;
412871616c8eSLen Brown 
412971616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
413071616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
413171616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
413271616c8eSLen Brown 
413371616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
413471616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
413571616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
413671616c8eSLen Brown }
41377f5c258eSLen Brown 
4138fcd17211SLen Brown void process_cpuid()
4139103a8feaSLen Brown {
414061a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
4141103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
4142b3a34e93SLen Brown 	unsigned int has_turbo;
4143103a8feaSLen Brown 
4144103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4145103a8feaSLen Brown 
41465aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4147103a8feaSLen Brown 
4148103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4149103a8feaSLen Brown 		genuine_intel = 1;
4150103a8feaSLen Brown 
415196e47158SLen Brown 	if (!quiet)
4152b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4153103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4154103a8feaSLen Brown 
41555aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4156103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4157103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4158103a8feaSLen Brown 	stepping = fms & 0xf;
4159103a8feaSLen Brown 	if (family == 6 || family == 0xf)
4160103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
4161103a8feaSLen Brown 
416296e47158SLen Brown 	if (!quiet) {
4163b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
4164103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
4165aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
416669807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
416769807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
4168aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
416969807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
417069807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
417169807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
417269807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
417369807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
417469807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
417569807a63SLen Brown 	}
4176103a8feaSLen Brown 
4177b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
4178b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
4179103a8feaSLen Brown 
4180103a8feaSLen Brown 	/*
4181103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4182103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4183103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4184103a8feaSLen Brown 	 */
4185103a8feaSLen Brown 	ebx = ecx = edx = 0;
41865aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4187103a8feaSLen Brown 
418861a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4189103a8feaSLen Brown 
4190103a8feaSLen Brown 		/*
4191103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4192103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4193103a8feaSLen Brown 		 */
41945aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
41958209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4196d7899447SLen Brown 	}
4197103a8feaSLen Brown 
4198103a8feaSLen Brown 	/*
4199103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4200103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4201103a8feaSLen Brown 	 */
4202103a8feaSLen Brown 
42035aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
42048209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4205812db3f7SLen Brown 	if (has_aperf) {
4206812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4207812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4208812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4209812db3f7SLen Brown 	}
4210889facbeSLen Brown 	do_dts = eax & (1 << 0);
4211812db3f7SLen Brown 	if (do_dts)
4212812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4213b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4214889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4215812db3f7SLen Brown 	if (do_ptm)
4216812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
42177f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
42187f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
42197f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
42207f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
42217f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4222889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4223889facbeSLen Brown 
422496e47158SLen Brown 	if (!quiet)
4225b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
42267f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
42277f5c258eSLen Brown 			has_aperf ? "" : "No-",
4228b3a34e93SLen Brown 			has_turbo ? "" : "No-",
42297f5c258eSLen Brown 			do_dts ? "" : "No-",
42307f5c258eSLen Brown 			do_ptm ? "" : "No-",
42317f5c258eSLen Brown 			has_hwp ? "" : "No-",
42327f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
42337f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
42347f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
42357f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
42367f5c258eSLen Brown 			has_epb ? "" : "No-");
4237103a8feaSLen Brown 
423896e47158SLen Brown 	if (!quiet)
423969807a63SLen Brown 		decode_misc_enable_msr();
424069807a63SLen Brown 
424133148d67SLen Brown 
424296e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4243aa8d8cc7SLen Brown 		int has_sgx;
4244aa8d8cc7SLen Brown 
4245aa8d8cc7SLen Brown 		ecx = 0;
4246aa8d8cc7SLen Brown 
4247aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4248aa8d8cc7SLen Brown 
4249aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4250aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4251aa8d8cc7SLen Brown 
4252aa8d8cc7SLen Brown 		if (has_sgx)
4253aa8d8cc7SLen Brown 			decode_feature_control_msr();
4254aa8d8cc7SLen Brown 	}
4255aa8d8cc7SLen Brown 
425661a87ba7SLen Brown 	if (max_level >= 0x15) {
42578a5bdf41SLen Brown 		unsigned int eax_crystal;
42588a5bdf41SLen Brown 		unsigned int ebx_tsc;
42598a5bdf41SLen Brown 
42608a5bdf41SLen Brown 		/*
42618a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
42628a5bdf41SLen Brown 		 */
42638a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
42645aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
42658a5bdf41SLen Brown 
42668a5bdf41SLen Brown 		if (ebx_tsc != 0) {
42678a5bdf41SLen Brown 
426896e47158SLen Brown 			if (!quiet && (ebx != 0))
4269b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
42708a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
42718a5bdf41SLen Brown 
42728a5bdf41SLen Brown 			if (crystal_hz == 0)
42738a5bdf41SLen Brown 				switch(model) {
4274869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4275869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4276869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4277869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4278e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4279e8efbc80SLen Brown 					break;
42807268d407SLen Brown 				case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
4281ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4282ec53e594SLen Brown 					break;
4283869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4284ac01ac13SLen Brown 				case INTEL_FAM6_ATOM_GEMINI_LAKE:
4285e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
42868a5bdf41SLen Brown 					break;
42878a5bdf41SLen Brown 				default:
42888a5bdf41SLen Brown 					crystal_hz = 0;
42898a5bdf41SLen Brown 			}
42908a5bdf41SLen Brown 
42918a5bdf41SLen Brown 			if (crystal_hz) {
42928a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
429396e47158SLen Brown 				if (!quiet)
4294b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
42958a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
42968a5bdf41SLen Brown 			}
42978a5bdf41SLen Brown 		}
42988a5bdf41SLen Brown 	}
429961a87ba7SLen Brown 	if (max_level >= 0x16) {
430061a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
430161a87ba7SLen Brown 
430261a87ba7SLen Brown 		/*
430361a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
430461a87ba7SLen Brown 		 */
430561a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
430661a87ba7SLen Brown 
43075aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
430896e47158SLen Brown 		if (!quiet)
4309b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
431061a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
431161a87ba7SLen Brown 	}
43128a5bdf41SLen Brown 
4313b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4314b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4315b2b34dfeSHubert Chrzaniuk 
4316812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4317812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4318812db3f7SLen Brown 
4319812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4320812db3f7SLen Brown 		do_nhm_platform_info = 1;
4321812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4322812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4323812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4324812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4325812db3f7SLen Brown 	}
4326d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4327812db3f7SLen Brown 
4328812db3f7SLen Brown 	if (do_snb_cstates)
4329812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4330812db3f7SLen Brown 
43315a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
43320f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
43330f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
43340f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
43350f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
43360f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
43370f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
43380f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
43390f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
43400539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
43410f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
43420f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
43430f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
43440f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
43450539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
43460539ba11SLen Brown 		use_c1_residency_msr = 1;
43470539ba11SLen Brown 	}
43487170a374SLen Brown 	if (is_dnv(family, model)) {
43497170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
43507170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
43517170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
43527170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
43537170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
43547170a374SLen Brown 		use_c1_residency_msr = 1;
43557170a374SLen Brown 	}
435634c76197SLen Brown 	if (is_skx(family, model)) {
435734c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
435834c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
435934c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
436034c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
436134c76197SLen Brown 	}
4362ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4363ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4364ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4365ade0ebacSLen Brown 	}
43660f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
43670f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
43680f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
43690f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
43700f47c08dSLen Brown 	}
43715a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
4372a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4373a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4374a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4375a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4376a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4377a99d8730SLen Brown 	}
4378144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4379fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4380103a8feaSLen Brown 
438196e47158SLen Brown 	if (!quiet)
4382f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4383f0057310SLen Brown 
438496e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
438571616c8eSLen Brown 		decode_c6_demotion_policy_msr();
438671616c8eSLen Brown 
4387889facbeSLen Brown 	rapl_probe(family, model);
43883a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4389*ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
4390889facbeSLen Brown 
439196e47158SLen Brown 	if (!quiet)
43921b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4393fcd17211SLen Brown 
439441618e63SLen Brown 	if (!quiet)
439541618e63SLen Brown 		dump_sysfs_cstate_config();
43967293fccdSLen Brown 	if (!quiet)
43977293fccdSLen Brown 		dump_sysfs_pstate_config();
439841618e63SLen Brown 
4399a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4400a2b7b749SLen Brown 		calculate_tsc_tweak();
4401a2b7b749SLen Brown 
4402812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4403812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4404fdf676e5SLen Brown 
4405812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4406812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
440727d47356SLen Brown 
440896e47158SLen Brown 	if (!quiet)
440933148d67SLen Brown 		decode_misc_feature_control();
441033148d67SLen Brown 
4411889facbeSLen Brown 	return;
4412103a8feaSLen Brown }
4413103a8feaSLen Brown 
4414103a8feaSLen Brown 
4415103a8feaSLen Brown /*
4416103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4417103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4418103a8feaSLen Brown  */
4419103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4420103a8feaSLen Brown {
4421103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4422103a8feaSLen Brown 		return 1;
4423103a8feaSLen Brown 	else
4424103a8feaSLen Brown 		return 0;
4425103a8feaSLen Brown }
4426103a8feaSLen Brown 
4427103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4428103a8feaSLen Brown {
4429103a8feaSLen Brown 	return 0;
4430103a8feaSLen Brown }
4431103a8feaSLen Brown 
4432c98d5d94SLen Brown void topology_probe()
4433c98d5d94SLen Brown {
4434c98d5d94SLen Brown 	int i;
4435c98d5d94SLen Brown 	int max_core_id = 0;
4436c98d5d94SLen Brown 	int max_package_id = 0;
4437c98d5d94SLen Brown 	int max_siblings = 0;
4438c98d5d94SLen Brown 	struct cpu_topology {
4439c98d5d94SLen Brown 		int core_id;
4440c98d5d94SLen Brown 		int physical_package_id;
4441c98d5d94SLen Brown 	} *cpus;
4442c98d5d94SLen Brown 
4443c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4444c98d5d94SLen Brown 	topo.num_cpus = 0;
4445c98d5d94SLen Brown 	topo.max_cpu_num = 0;
4446c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4447c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4448812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4449c98d5d94SLen Brown 
4450d8af6f5fSLen Brown 	if (debug > 1)
4451b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4452c98d5d94SLen Brown 
4453c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4454b2c95d90SJosh Triplett 	if (cpus == NULL)
4455b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4456c98d5d94SLen Brown 
4457c98d5d94SLen Brown 	/*
4458c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4459c98d5d94SLen Brown 	 */
4460c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4461b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4462b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4463c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4464c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4465c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4466c98d5d94SLen Brown 
4467c98d5d94SLen Brown 	/*
44681ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
44691ef7d21aSLen Brown 	 */
44701ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
44711ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
44721ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
44731ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
44741ef7d21aSLen Brown 	}
44751ef7d21aSLen Brown 
44761ef7d21aSLen Brown 	/*
4477c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4478c98d5d94SLen Brown 	 */
4479c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4480b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4481b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4482c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4483c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4484c98d5d94SLen Brown 
4485c98d5d94SLen Brown 
4486c98d5d94SLen Brown 	/*
4487c98d5d94SLen Brown 	 * For online cpus
4488c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4489c98d5d94SLen Brown 	 */
4490c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4491c98d5d94SLen Brown 		int siblings;
4492c98d5d94SLen Brown 
4493c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4494d8af6f5fSLen Brown 			if (debug > 1)
4495b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4496c98d5d94SLen Brown 			continue;
4497c98d5d94SLen Brown 		}
4498c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
4499c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
4500c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
4501c98d5d94SLen Brown 
4502c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4503c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4504c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4505c98d5d94SLen Brown 
4506c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
4507c98d5d94SLen Brown 		if (siblings > max_siblings)
4508c98d5d94SLen Brown 			max_siblings = siblings;
4509d8af6f5fSLen Brown 		if (debug > 1)
4510b7d8c148SLen Brown 			fprintf(outf, "cpu %d pkg %d core %d\n",
4511c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
4512c98d5d94SLen Brown 	}
4513c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
4514d8af6f5fSLen Brown 	if (debug > 1)
4515b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
4516c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
45170f47c08dSLen Brown 	if (!summary_only && topo.num_cores_per_pkg > 1)
4518812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
4519c98d5d94SLen Brown 
4520c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
4521d8af6f5fSLen Brown 	if (debug > 1)
4522b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4523c98d5d94SLen Brown 			max_package_id, topo.num_packages);
45247da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
4525812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
4526c98d5d94SLen Brown 
4527c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
4528d8af6f5fSLen Brown 	if (debug > 1)
4529b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
4530c98d5d94SLen Brown 
4531c98d5d94SLen Brown 	free(cpus);
4532c98d5d94SLen Brown }
4533c98d5d94SLen Brown 
4534c98d5d94SLen Brown void
4535c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4536c98d5d94SLen Brown {
4537c98d5d94SLen Brown 	int i;
4538c98d5d94SLen Brown 
4539c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
4540678a3bd1SLen Brown 		topo.num_packages, sizeof(struct thread_data));
4541c98d5d94SLen Brown 	if (*t == NULL)
4542c98d5d94SLen Brown 		goto error;
4543c98d5d94SLen Brown 
4544c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
4545c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
4546c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
4547c98d5d94SLen Brown 
4548c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
4549678a3bd1SLen Brown 		sizeof(struct core_data));
4550c98d5d94SLen Brown 	if (*c == NULL)
4551c98d5d94SLen Brown 		goto error;
4552c98d5d94SLen Brown 
4553c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4554c98d5d94SLen Brown 		(*c)[i].core_id = -1;
4555c98d5d94SLen Brown 
4556678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
4557c98d5d94SLen Brown 	if (*p == NULL)
4558c98d5d94SLen Brown 		goto error;
4559c98d5d94SLen Brown 
4560c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
4561c98d5d94SLen Brown 		(*p)[i].package_id = i;
4562c98d5d94SLen Brown 
4563c98d5d94SLen Brown 	return;
4564c98d5d94SLen Brown error:
4565b2c95d90SJosh Triplett 	err(1, "calloc counters");
4566c98d5d94SLen Brown }
4567c98d5d94SLen Brown /*
4568c98d5d94SLen Brown  * init_counter()
4569c98d5d94SLen Brown  *
4570c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
4571c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4572c98d5d94SLen Brown  *
4573c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
4574c98d5d94SLen Brown  */
4575c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4576c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
4577c98d5d94SLen Brown 	int pkg_num, int cpu_id)
4578c98d5d94SLen Brown {
4579c98d5d94SLen Brown 	struct thread_data *t;
4580c98d5d94SLen Brown 	struct core_data *c;
4581c98d5d94SLen Brown 	struct pkg_data *p;
4582c98d5d94SLen Brown 
4583c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4584c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
4585c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
4586c98d5d94SLen Brown 
4587c98d5d94SLen Brown 	t->cpu_id = cpu_id;
4588c98d5d94SLen Brown 	if (thread_num == 0) {
4589c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4590c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
4591c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4592c98d5d94SLen Brown 	}
4593c98d5d94SLen Brown 
4594c98d5d94SLen Brown 	c->core_id = core_num;
4595c98d5d94SLen Brown 	p->package_id = pkg_num;
4596c98d5d94SLen Brown }
4597c98d5d94SLen Brown 
4598c98d5d94SLen Brown 
4599c98d5d94SLen Brown int initialize_counters(int cpu_id)
4600c98d5d94SLen Brown {
4601c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
4602c98d5d94SLen Brown 
4603c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
4604c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
4605e275b388SDasaratharaman Chandramouli 	my_thread_id = get_cpu_position_in_core(cpu_id);
4606e275b388SDasaratharaman Chandramouli 	if (!my_thread_id)
4607c98d5d94SLen Brown 		topo.num_cores++;
4608c98d5d94SLen Brown 
4609c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4610c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4611c98d5d94SLen Brown 	return 0;
4612c98d5d94SLen Brown }
4613c98d5d94SLen Brown 
4614c98d5d94SLen Brown void allocate_output_buffer()
4615c98d5d94SLen Brown {
46163b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4617c98d5d94SLen Brown 	outp = output_buffer;
4618b2c95d90SJosh Triplett 	if (outp == NULL)
4619b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
4620c98d5d94SLen Brown }
462136229897SLen Brown void allocate_fd_percpu(void)
462236229897SLen Brown {
462301a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
462436229897SLen Brown 	if (fd_percpu == NULL)
462536229897SLen Brown 		err(-1, "calloc fd_percpu");
462636229897SLen Brown }
4627562a2d37SLen Brown void allocate_irq_buffers(void)
4628562a2d37SLen Brown {
4629562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4630562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
4631562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
4632c98d5d94SLen Brown 
463301a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4634562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
463501a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
4636562a2d37SLen Brown }
4637c98d5d94SLen Brown void setup_all_buffers(void)
4638c98d5d94SLen Brown {
4639c98d5d94SLen Brown 	topology_probe();
4640562a2d37SLen Brown 	allocate_irq_buffers();
464136229897SLen Brown 	allocate_fd_percpu();
4642c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
4643c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
4644c98d5d94SLen Brown 	allocate_output_buffer();
4645c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
4646c98d5d94SLen Brown }
46473b4d5c7fSAndy Shevchenko 
46487ce7d5deSPrarit Bhargava void set_base_cpu(void)
46497ce7d5deSPrarit Bhargava {
46507ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
46517ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
46527ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
46537ce7d5deSPrarit Bhargava 
46547ce7d5deSPrarit Bhargava 	if (debug > 1)
4655b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
46567ce7d5deSPrarit Bhargava }
46577ce7d5deSPrarit Bhargava 
4658103a8feaSLen Brown void turbostat_init()
4659103a8feaSLen Brown {
46607ce7d5deSPrarit Bhargava 	setup_all_buffers();
46617ce7d5deSPrarit Bhargava 	set_base_cpu();
4662103a8feaSLen Brown 	check_dev_msr();
466398481e79SLen Brown 	check_permissions();
4664fcd17211SLen Brown 	process_cpuid();
4665103a8feaSLen Brown 
4666103a8feaSLen Brown 
466796e47158SLen Brown 	if (!quiet)
46687f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
46697f5c258eSLen Brown 
467096e47158SLen Brown 	if (!quiet)
4671889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
4672889facbeSLen Brown 
467396e47158SLen Brown 	if (!quiet)
46743a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
46753a9a941dSLen Brown 
467696e47158SLen Brown 	if (!quiet)
4677889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
4678889facbeSLen Brown 
4679889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
4680889facbeSLen Brown 
468196e47158SLen Brown 	if (!quiet)
4682889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
46835a63426eSLen Brown 
468496e47158SLen Brown 	if (!quiet && do_irtl_snb)
46855a63426eSLen Brown 		print_irtl();
4686103a8feaSLen Brown }
4687103a8feaSLen Brown 
4688103a8feaSLen Brown int fork_it(char **argv)
4689103a8feaSLen Brown {
4690103a8feaSLen Brown 	pid_t child_pid;
4691d91bb17cSLen Brown 	int status;
4692d15cf7c1SLen Brown 
4693218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
4694d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
4695d91bb17cSLen Brown 	if (status)
4696d91bb17cSLen Brown 		exit(status);
4697d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
4698d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
4699103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
4700103a8feaSLen Brown 
4701103a8feaSLen Brown 	child_pid = fork();
4702103a8feaSLen Brown 	if (!child_pid) {
4703103a8feaSLen Brown 		/* child */
4704103a8feaSLen Brown 		execvp(argv[0], argv);
47050815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
4706103a8feaSLen Brown 	} else {
4707103a8feaSLen Brown 
4708103a8feaSLen Brown 		/* parent */
4709b2c95d90SJosh Triplett 		if (child_pid == -1)
4710b2c95d90SJosh Triplett 			err(1, "fork");
4711103a8feaSLen Brown 
4712103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
4713103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
4714b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
4715b2c95d90SJosh Triplett 			err(status, "waitpid");
4716103a8feaSLen Brown 	}
4717c98d5d94SLen Brown 	/*
4718c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
4719c98d5d94SLen Brown 	 * because re-starting is problematic when forking
4720c98d5d94SLen Brown 	 */
4721218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
4722c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
4723103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
4724103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
4725ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4726ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
4727ba3dec99SLen Brown 	else {
4728c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
4729c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
4730ba3dec99SLen Brown 	}
4731103a8feaSLen Brown 
4732b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4733b7d8c148SLen Brown 
4734b7d8c148SLen Brown 	flush_output_stderr();
4735103a8feaSLen Brown 
4736d91bb17cSLen Brown 	return status;
4737103a8feaSLen Brown }
4738103a8feaSLen Brown 
47393b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
47403b4d5c7fSAndy Shevchenko {
47413b4d5c7fSAndy Shevchenko 	int status;
47423b4d5c7fSAndy Shevchenko 
4743218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
47443b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
47453b4d5c7fSAndy Shevchenko 	if (status)
47463b4d5c7fSAndy Shevchenko 		return status;
47473b4d5c7fSAndy Shevchenko 
47483b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
47493b4d5c7fSAndy Shevchenko 	if (status)
47503b4d5c7fSAndy Shevchenko 		return status;
47513b4d5c7fSAndy Shevchenko 
4752b7d8c148SLen Brown 	flush_output_stdout();
47533b4d5c7fSAndy Shevchenko 
47543b4d5c7fSAndy Shevchenko 	return status;
47553b4d5c7fSAndy Shevchenko }
47563b4d5c7fSAndy Shevchenko 
4757d8af6f5fSLen Brown void print_version() {
4758f7d44a8fSLen Brown 	fprintf(outf, "turbostat version 17.06.23"
4759d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
4760d8af6f5fSLen Brown }
4761d8af6f5fSLen Brown 
4762495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
4763495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
476441618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
4765388e9c81SLen Brown {
4766388e9c81SLen Brown 	struct msr_counter *msrp;
4767388e9c81SLen Brown 
4768388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
4769388e9c81SLen Brown 	if (msrp == NULL) {
4770388e9c81SLen Brown 		perror("calloc");
4771388e9c81SLen Brown 		exit(1);
4772388e9c81SLen Brown 	}
4773388e9c81SLen Brown 
4774388e9c81SLen Brown 	msrp->msr_num = msr_num;
4775388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
4776495c7654SLen Brown 	if (path)
4777495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
4778388e9c81SLen Brown 	msrp->width = width;
4779388e9c81SLen Brown 	msrp->type = type;
4780388e9c81SLen Brown 	msrp->format = format;
478141618e63SLen Brown 	msrp->flags = flags;
4782388e9c81SLen Brown 
4783388e9c81SLen Brown 	switch (scope) {
4784388e9c81SLen Brown 
4785388e9c81SLen Brown 	case SCOPE_CPU:
4786388e9c81SLen Brown 		msrp->next = sys.tp;
4787388e9c81SLen Brown 		sys.tp = msrp;
4788678a3bd1SLen Brown 		sys.added_thread_counters++;
4789678a3bd1SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4790678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
4791678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4792678a3bd1SLen Brown 			exit(-1);
4793678a3bd1SLen Brown 		}
4794388e9c81SLen Brown 		break;
4795388e9c81SLen Brown 
4796388e9c81SLen Brown 	case SCOPE_CORE:
4797388e9c81SLen Brown 		msrp->next = sys.cp;
4798388e9c81SLen Brown 		sys.cp = msrp;
4799678a3bd1SLen Brown 		sys.added_core_counters++;
4800678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4801678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
4802678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4803678a3bd1SLen Brown 			exit(-1);
4804678a3bd1SLen Brown 		}
4805388e9c81SLen Brown 		break;
4806388e9c81SLen Brown 
4807388e9c81SLen Brown 	case SCOPE_PACKAGE:
4808388e9c81SLen Brown 		msrp->next = sys.pp;
4809388e9c81SLen Brown 		sys.pp = msrp;
4810678a3bd1SLen Brown 		sys.added_package_counters++;
4811678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4812678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
4813678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4814678a3bd1SLen Brown 			exit(-1);
4815678a3bd1SLen Brown 		}
4816388e9c81SLen Brown 		break;
4817388e9c81SLen Brown 	}
4818388e9c81SLen Brown 
4819388e9c81SLen Brown 	return 0;
4820388e9c81SLen Brown }
4821388e9c81SLen Brown 
4822388e9c81SLen Brown void parse_add_command(char *add_command)
4823388e9c81SLen Brown {
4824388e9c81SLen Brown 	int msr_num = 0;
4825495c7654SLen Brown 	char *path = NULL;
48260f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
4827388e9c81SLen Brown 	int width = 64;
4828388e9c81SLen Brown 	int fail = 0;
4829388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
4830388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
4831388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
4832388e9c81SLen Brown 
4833388e9c81SLen Brown 	while (add_command) {
4834388e9c81SLen Brown 
4835388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4836388e9c81SLen Brown 			goto next;
4837388e9c81SLen Brown 
4838388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
4839388e9c81SLen Brown 			goto next;
4840388e9c81SLen Brown 
4841495c7654SLen Brown 		if (*add_command == '/') {
4842495c7654SLen Brown 			path = add_command;
4843495c7654SLen Brown 			goto next;
4844495c7654SLen Brown 		}
4845495c7654SLen Brown 
4846388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
4847388e9c81SLen Brown 			if ((width == 32) || (width == 64))
4848388e9c81SLen Brown 				goto next;
4849388e9c81SLen Brown 			width = 64;
4850388e9c81SLen Brown 		}
4851388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4852388e9c81SLen Brown 			scope = SCOPE_CPU;
4853388e9c81SLen Brown 			goto next;
4854388e9c81SLen Brown 		}
4855388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
4856388e9c81SLen Brown 			scope = SCOPE_CORE;
4857388e9c81SLen Brown 			goto next;
4858388e9c81SLen Brown 		}
4859388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
4860388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
4861388e9c81SLen Brown 			goto next;
4862388e9c81SLen Brown 		}
4863388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4864388e9c81SLen Brown 			type = COUNTER_CYCLES;
4865388e9c81SLen Brown 			goto next;
4866388e9c81SLen Brown 		}
4867388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4868388e9c81SLen Brown 			type = COUNTER_SECONDS;
4869388e9c81SLen Brown 			goto next;
4870388e9c81SLen Brown 		}
487141618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
487241618e63SLen Brown 			type = COUNTER_USEC;
487341618e63SLen Brown 			goto next;
487441618e63SLen Brown 		}
4875388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
4876388e9c81SLen Brown 			format = FORMAT_RAW;
4877388e9c81SLen Brown 			goto next;
4878388e9c81SLen Brown 		}
4879388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
4880388e9c81SLen Brown 			format = FORMAT_DELTA;
4881388e9c81SLen Brown 			goto next;
4882388e9c81SLen Brown 		}
4883388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
4884388e9c81SLen Brown 			format = FORMAT_PERCENT;
4885388e9c81SLen Brown 			goto next;
4886388e9c81SLen Brown 		}
4887388e9c81SLen Brown 
4888388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
4889388e9c81SLen Brown 			char *eos;
4890388e9c81SLen Brown 
4891388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
4892388e9c81SLen Brown 			if (eos)
4893388e9c81SLen Brown 				*eos = '\0';
4894388e9c81SLen Brown 			goto next;
4895388e9c81SLen Brown 		}
4896388e9c81SLen Brown 
4897388e9c81SLen Brown next:
4898388e9c81SLen Brown 		add_command = strchr(add_command, ',');
4899495c7654SLen Brown 		if (add_command) {
4900495c7654SLen Brown 			*add_command = '\0';
4901388e9c81SLen Brown 			add_command++;
4902495c7654SLen Brown 		}
4903388e9c81SLen Brown 
4904388e9c81SLen Brown 	}
4905495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
4906495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
4907388e9c81SLen Brown 		fail++;
4908388e9c81SLen Brown 	}
4909388e9c81SLen Brown 
4910388e9c81SLen Brown 	/* generate default column header */
4911388e9c81SLen Brown 	if (*name_buffer == '\0') {
4912388e9c81SLen Brown 		if (width == 32)
49135f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4914388e9c81SLen Brown 		else
49155f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4916388e9c81SLen Brown 	}
4917388e9c81SLen Brown 
491841618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
4919388e9c81SLen Brown 		fail++;
4920388e9c81SLen Brown 
4921388e9c81SLen Brown 	if (fail) {
4922388e9c81SLen Brown 		help();
4923388e9c81SLen Brown 		exit(1);
4924388e9c81SLen Brown 	}
4925388e9c81SLen Brown }
492641618e63SLen Brown 
4927dd778a5eSLen Brown int is_deferred_skip(char *name)
4928dd778a5eSLen Brown {
4929dd778a5eSLen Brown 	int i;
4930dd778a5eSLen Brown 
4931dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
4932dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
4933dd778a5eSLen Brown 			return 1;
4934dd778a5eSLen Brown 	return 0;
4935dd778a5eSLen Brown }
4936dd778a5eSLen Brown 
493741618e63SLen Brown void probe_sysfs(void)
493841618e63SLen Brown {
493941618e63SLen Brown 	char path[64];
494041618e63SLen Brown 	char name_buf[16];
494141618e63SLen Brown 	FILE *input;
494241618e63SLen Brown 	int state;
494341618e63SLen Brown 	char *sp;
494441618e63SLen Brown 
494541618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
494641618e63SLen Brown 		return;
494741618e63SLen Brown 
494841618e63SLen Brown 	for (state = 10; state > 0; --state) {
494941618e63SLen Brown 
495041618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
495141618e63SLen Brown 			base_cpu, state);
495241618e63SLen Brown 		input = fopen(path, "r");
495341618e63SLen Brown 		if (input == NULL)
495441618e63SLen Brown 			continue;
495541618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
495641618e63SLen Brown 
495741618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
495841618e63SLen Brown 		sp = strchr(name_buf, '-');
495941618e63SLen Brown 		if (!sp)
496041618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
496141618e63SLen Brown 		*sp = '%';
496241618e63SLen Brown 		*(sp + 1) = '\0';
496341618e63SLen Brown 
496441618e63SLen Brown 		fclose(input);
496541618e63SLen Brown 
496641618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
496741618e63SLen Brown 
4968dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
4969dd778a5eSLen Brown 			continue;
4970dd778a5eSLen Brown 
497141618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
497241618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
497341618e63SLen Brown 	}
497441618e63SLen Brown 
497541618e63SLen Brown 	for (state = 10; state > 0; --state) {
497641618e63SLen Brown 
497741618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
497841618e63SLen Brown 			base_cpu, state);
497941618e63SLen Brown 		input = fopen(path, "r");
498041618e63SLen Brown 		if (input == NULL)
498141618e63SLen Brown 			continue;
498241618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
498341618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
498441618e63SLen Brown 		sp = strchr(name_buf, '-');
498541618e63SLen Brown 		if (!sp)
498641618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
498741618e63SLen Brown 		*sp = '\0';
498841618e63SLen Brown 		fclose(input);
498941618e63SLen Brown 
499041618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
499141618e63SLen Brown 
4992dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
4993dd778a5eSLen Brown 			continue;
4994dd778a5eSLen Brown 
499541618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
499641618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
499741618e63SLen Brown 	}
499841618e63SLen Brown 
499941618e63SLen Brown }
500041618e63SLen Brown 
50011ef7d21aSLen Brown 
50021ef7d21aSLen Brown /*
50031ef7d21aSLen Brown  * parse cpuset with following syntax
50041ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
50051ef7d21aSLen Brown  */
50061ef7d21aSLen Brown void parse_cpu_command(char *optarg)
50071ef7d21aSLen Brown {
50081ef7d21aSLen Brown 	unsigned int start, end;
50091ef7d21aSLen Brown 	char *next;
50101ef7d21aSLen Brown 
50114e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
50124e4e1e7cSLen Brown 		if (cpu_subset)
50134e4e1e7cSLen Brown 			goto error;
50144e4e1e7cSLen Brown 		show_core_only++;
50154e4e1e7cSLen Brown 		return;
50164e4e1e7cSLen Brown 	}
50174e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
50184e4e1e7cSLen Brown 		if (cpu_subset)
50194e4e1e7cSLen Brown 			goto error;
50204e4e1e7cSLen Brown 		show_pkg_only++;
50214e4e1e7cSLen Brown 		return;
50224e4e1e7cSLen Brown 	}
50234e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
50244e4e1e7cSLen Brown 		goto error;
50254e4e1e7cSLen Brown 
50261ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
50271ef7d21aSLen Brown 	if (cpu_subset == NULL)
50281ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
50291ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
50301ef7d21aSLen Brown 
50311ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
50321ef7d21aSLen Brown 
50331ef7d21aSLen Brown 	next = optarg;
50341ef7d21aSLen Brown 
50351ef7d21aSLen Brown 	while (next && *next) {
50361ef7d21aSLen Brown 
50371ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
50381ef7d21aSLen Brown 			goto error;
50391ef7d21aSLen Brown 
50401ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
50411ef7d21aSLen Brown 
50421ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
50431ef7d21aSLen Brown 			goto error;
50441ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
50451ef7d21aSLen Brown 
50461ef7d21aSLen Brown 		if (*next == '\0')
50471ef7d21aSLen Brown 			break;
50481ef7d21aSLen Brown 
50491ef7d21aSLen Brown 		if (*next == ',') {
50501ef7d21aSLen Brown 			next += 1;
50511ef7d21aSLen Brown 			continue;
50521ef7d21aSLen Brown 		}
50531ef7d21aSLen Brown 
50541ef7d21aSLen Brown 		if (*next == '-') {
50551ef7d21aSLen Brown 			next += 1;	/* start range */
50561ef7d21aSLen Brown 		} else if (*next == '.') {
50571ef7d21aSLen Brown 			next += 1;
50581ef7d21aSLen Brown 			if (*next == '.')
50591ef7d21aSLen Brown 				next += 1;	/* start range */
50601ef7d21aSLen Brown 			else
50611ef7d21aSLen Brown 				goto error;
50621ef7d21aSLen Brown 		}
50631ef7d21aSLen Brown 
50641ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
50651ef7d21aSLen Brown 		if (end <= start)
50661ef7d21aSLen Brown 			goto error;
50671ef7d21aSLen Brown 
50681ef7d21aSLen Brown 		while (++start <= end) {
50691ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
50701ef7d21aSLen Brown 				goto error;
50711ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
50721ef7d21aSLen Brown 		}
50731ef7d21aSLen Brown 
50741ef7d21aSLen Brown 		if (*next == ',')
50751ef7d21aSLen Brown 			next += 1;
50761ef7d21aSLen Brown 		else if (*next != '\0')
50771ef7d21aSLen Brown 			goto error;
50781ef7d21aSLen Brown 	}
50791ef7d21aSLen Brown 
50801ef7d21aSLen Brown 	return;
50811ef7d21aSLen Brown 
50821ef7d21aSLen Brown error:
50834e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
50844e4e1e7cSLen Brown 	help();
50851ef7d21aSLen Brown 	exit(-1);
50861ef7d21aSLen Brown }
50871ef7d21aSLen Brown 
5088812db3f7SLen Brown 
5089103a8feaSLen Brown void cmdline(int argc, char **argv)
5090103a8feaSLen Brown {
5091103a8feaSLen Brown 	int opt;
5092d8af6f5fSLen Brown 	int option_index = 0;
5093d8af6f5fSLen Brown 	static struct option long_options[] = {
5094388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
50951ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5096d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
509796e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
50983f44a5c6SLen Brown 		{"enable",	required_argument,	0, 'e'},
5099d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5100d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5101812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5102d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5103c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5104b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
510596e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5106812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5107d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5108d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5109d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5110d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5111d8af6f5fSLen Brown 	};
5112103a8feaSLen Brown 
5113103a8feaSLen Brown 	progname = argv[0];
5114103a8feaSLen Brown 
51153f44a5c6SLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jo:qST:v",
5116d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5117103a8feaSLen Brown 		switch (opt) {
5118388e9c81SLen Brown 		case 'a':
5119388e9c81SLen Brown 			parse_add_command(optarg);
5120388e9c81SLen Brown 			break;
51211ef7d21aSLen Brown 		case 'c':
51221ef7d21aSLen Brown 			parse_cpu_command(optarg);
51231ef7d21aSLen Brown 			break;
5124d8af6f5fSLen Brown 		case 'D':
5125d8af6f5fSLen Brown 			dump_only++;
51268e180f3cSLen Brown 			break;
51273f44a5c6SLen Brown 		case 'e':
51283f44a5c6SLen Brown 			/* --enable specified counter */
51293f44a5c6SLen Brown 			bic_enabled |= bic_lookup(optarg, SHOW_LIST);
51303f44a5c6SLen Brown 			break;
5131d8af6f5fSLen Brown 		case 'd':
5132d8af6f5fSLen Brown 			debug++;
51333f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
51342f32edf1SLen Brown 			break;
5135812db3f7SLen Brown 		case 'H':
51363f44a5c6SLen Brown 			/*
51373f44a5c6SLen Brown 			 * --hide: do not show those specified
51383f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
51393f44a5c6SLen Brown 			 */
51403f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5141812db3f7SLen Brown 			break;
5142d8af6f5fSLen Brown 		case 'h':
5143d8af6f5fSLen Brown 		default:
5144d8af6f5fSLen Brown 			help();
5145d8af6f5fSLen Brown 			exit(1);
5146d8af6f5fSLen Brown 		case 'i':
51472a0609c0SLen Brown 			{
51482a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
51492a0609c0SLen Brown 
51502a0609c0SLen Brown 				if (interval < 0.001) {
5151b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
51522a0609c0SLen Brown 						interval);
51532a0609c0SLen Brown 					exit(2);
51542a0609c0SLen Brown 				}
51552a0609c0SLen Brown 
515647936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
5157b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
515847936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
51592a0609c0SLen Brown 			}
5160889facbeSLen Brown 			break;
51615c56be9aSDirk Brandewie 		case 'J':
51625c56be9aSDirk Brandewie 			rapl_joules++;
51635c56be9aSDirk Brandewie 			break;
5164c8ade361SLen Brown 		case 'l':
51653f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5166c8ade361SLen Brown 			list_header_only++;
5167c8ade361SLen Brown 			quiet++;
5168c8ade361SLen Brown 			break;
5169b7d8c148SLen Brown 		case 'o':
5170b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5171b7d8c148SLen Brown 			break;
517296e47158SLen Brown 		case 'q':
517396e47158SLen Brown 			quiet = 1;
517496e47158SLen Brown 			break;
5175812db3f7SLen Brown 		case 's':
51763f44a5c6SLen Brown 			/*
51773f44a5c6SLen Brown 			 * --show: show only those specified
51783f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
51793f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
51803f44a5c6SLen Brown 			 */
51813f44a5c6SLen Brown 			if (shown == 0)
51823f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
51833f44a5c6SLen Brown 			else
51843f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
51853f44a5c6SLen Brown 			shown = 1;
5186812db3f7SLen Brown 			break;
5187d8af6f5fSLen Brown 		case 'S':
5188d8af6f5fSLen Brown 			summary_only++;
5189d8af6f5fSLen Brown 			break;
5190d8af6f5fSLen Brown 		case 'T':
5191d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5192d8af6f5fSLen Brown 			break;
5193d8af6f5fSLen Brown 		case 'v':
5194d8af6f5fSLen Brown 			print_version();
5195d8af6f5fSLen Brown 			exit(0);
5196d8af6f5fSLen Brown 			break;
5197103a8feaSLen Brown 		}
5198103a8feaSLen Brown 	}
5199103a8feaSLen Brown }
5200103a8feaSLen Brown 
5201103a8feaSLen Brown int main(int argc, char **argv)
5202103a8feaSLen Brown {
5203b7d8c148SLen Brown 	outf = stderr;
5204b7d8c148SLen Brown 
5205103a8feaSLen Brown 	cmdline(argc, argv);
5206103a8feaSLen Brown 
520796e47158SLen Brown 	if (!quiet)
5208d8af6f5fSLen Brown 		print_version();
5209103a8feaSLen Brown 
521041618e63SLen Brown 	probe_sysfs();
521141618e63SLen Brown 
5212103a8feaSLen Brown 	turbostat_init();
5213103a8feaSLen Brown 
52143b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
52153b4d5c7fSAndy Shevchenko 	if (dump_only)
52163b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
52173b4d5c7fSAndy Shevchenko 
5218c8ade361SLen Brown 	/* list header and exit */
5219c8ade361SLen Brown 	if (list_header_only) {
5220c8ade361SLen Brown 		print_header(",");
5221c8ade361SLen Brown 		flush_output_stdout();
5222c8ade361SLen Brown 		return 0;
5223c8ade361SLen Brown 	}
5224c8ade361SLen Brown 
5225103a8feaSLen Brown 	/*
5226103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5227103a8feaSLen Brown 	 */
5228103a8feaSLen Brown 	if (argc - optind)
5229103a8feaSLen Brown 		return fork_it(argv + optind);
5230103a8feaSLen Brown 	else
5231103a8feaSLen Brown 		turbostat_loop();
5232103a8feaSLen Brown 
5233103a8feaSLen Brown 	return 0;
5234103a8feaSLen Brown }
5235