xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 9d83601a9cc1884d1b5706ee2acc661d558c6838)
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};
54023fe0acSChen Yu unsigned int num_iterations;
55d8af6f5fSLen Brown unsigned int debug;
5696e47158SLen Brown unsigned int quiet;
573f44a5c6SLen Brown unsigned int shown;
580de6c0dfSLen Brown unsigned int sums_need_wide_columns;
59d8af6f5fSLen Brown unsigned int rapl_joules;
60d8af6f5fSLen Brown unsigned int summary_only;
61c8ade361SLen Brown unsigned int list_header_only;
62d8af6f5fSLen Brown unsigned int dump_only;
63103a8feaSLen Brown unsigned int do_snb_cstates;
64fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
65144b44b1SLen Brown unsigned int do_slm_cstates;
66997e5395SSrinivas Pandruvada unsigned int do_cnl_cstates;
67144b44b1SLen Brown unsigned int use_c1_residency_msr;
68103a8feaSLen Brown unsigned int has_aperf;
69889facbeSLen Brown unsigned int has_epb;
705a63426eSLen Brown unsigned int do_irtl_snb;
715a63426eSLen Brown unsigned int do_irtl_hsw;
72fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
73103a8feaSLen Brown unsigned int genuine_intel;
74103a8feaSLen Brown unsigned int has_invariant_tsc;
75d7899447SLen Brown unsigned int do_nhm_platform_info;
76cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
77b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
78103a8feaSLen Brown double bclk;
79a2b7b749SLen Brown double base_hz;
8021ed5574SLen Brown unsigned int has_base_hz;
81a2b7b749SLen Brown double tsc_tweak = 1.0;
82c98d5d94SLen Brown unsigned int show_pkg_only;
83c98d5d94SLen Brown unsigned int show_core_only;
84c98d5d94SLen Brown char *output_buffer, *outp;
85889facbeSLen Brown unsigned int do_rapl;
86889facbeSLen Brown unsigned int do_dts;
87889facbeSLen Brown unsigned int do_ptm;
88fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
89be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us;
90be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us;
9127d47356SLen Brown unsigned int gfx_cur_mhz;
92889facbeSLen Brown unsigned int tcc_activation_temp;
93889facbeSLen Brown unsigned int tcc_activation_temp_override;
9440ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
9540ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
96889facbeSLen Brown double rapl_joule_counter_range;
973a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
98ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
993a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
1003a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
1018a5bdf41SLen Brown unsigned int crystal_hz;
1028a5bdf41SLen Brown unsigned long long tsc_hz;
1037ce7d5deSPrarit Bhargava int base_cpu;
10421ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
1057f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
1067f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
1077f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
1087f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1097f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1107f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
11133148d67SLen Brown unsigned int has_misc_feature_control;
1124c2122d4SLen Brown unsigned int first_counter_read = 1;
113889facbeSLen Brown 
114889facbeSLen Brown #define RAPL_PKG		(1 << 0)
115e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
116e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
117e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
118e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
119e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
120e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
121e6f9bb3cSLen Brown 
122889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
123e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
124e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
125e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
126e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1270b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1280b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
129e6f9bb3cSLen Brown 
1309148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
131e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1320b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
133e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
134e6f9bb3cSLen Brown 
1350b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
136e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
137e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
138e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1399148494cSJacob Pan 
1409148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1419148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1429148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
143889facbeSLen Brown #define	TJMAX_DEFAULT	100
144889facbeSLen Brown 
145889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
146103a8feaSLen Brown 
147388e9c81SLen Brown /*
148388e9c81SLen Brown  * buffer size used by sscanf() for added column names
149388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
150388e9c81SLen Brown  */
151388e9c81SLen Brown #define	NAME_BYTES 20
152495c7654SLen Brown #define PATH_BYTES 128
153388e9c81SLen Brown 
154103a8feaSLen Brown int backwards_count;
155103a8feaSLen Brown char *progname;
156103a8feaSLen Brown 
1571ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
1581ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
1591ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
1600748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8
1610748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24
1620e2d8f05SLen Brown #define BITMASK_SIZE 32
163c98d5d94SLen Brown 
164c98d5d94SLen Brown struct thread_data {
165f4fdf2b4SLen Brown 	struct timeval tv_begin;
166f4fdf2b4SLen Brown 	struct timeval tv_end;
167c98d5d94SLen Brown 	unsigned long long tsc;
168c98d5d94SLen Brown 	unsigned long long aperf;
169c98d5d94SLen Brown 	unsigned long long mperf;
170144b44b1SLen Brown 	unsigned long long c1;
1710de6c0dfSLen Brown 	unsigned long long  irq_count;
1721ed51011SLen Brown 	unsigned int smi_count;
173c98d5d94SLen Brown 	unsigned int cpu_id;
1744c2122d4SLen Brown 	unsigned int apic_id;
1754c2122d4SLen Brown 	unsigned int x2apic_id;
176c98d5d94SLen Brown 	unsigned int flags;
177c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
178c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
1790748eaf0SLen Brown 	unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
180c98d5d94SLen Brown } *thread_even, *thread_odd;
181c98d5d94SLen Brown 
182c98d5d94SLen Brown struct core_data {
183c98d5d94SLen Brown 	unsigned long long c3;
184c98d5d94SLen Brown 	unsigned long long c6;
185c98d5d94SLen Brown 	unsigned long long c7;
1860539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
187889facbeSLen Brown 	unsigned int core_temp_c;
188c98d5d94SLen Brown 	unsigned int core_id;
189678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
190c98d5d94SLen Brown } *core_even, *core_odd;
191c98d5d94SLen Brown 
192c98d5d94SLen Brown struct pkg_data {
193c98d5d94SLen Brown 	unsigned long long pc2;
194c98d5d94SLen Brown 	unsigned long long pc3;
195c98d5d94SLen Brown 	unsigned long long pc6;
196c98d5d94SLen Brown 	unsigned long long pc7;
197ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
198ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
199ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
200be0e54c4SLen Brown 	unsigned long long cpu_lpi;
201be0e54c4SLen Brown 	unsigned long long sys_lpi;
2020b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
2030b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
2040b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
2050b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
2069185e988SLen Brown 	long long gfx_rc6_ms;
20727d47356SLen Brown 	unsigned int gfx_mhz;
208c98d5d94SLen Brown 	unsigned int package_id;
209889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
210889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
211889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
212889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
213889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
214889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
215889facbeSLen Brown 	unsigned int pkg_temp_c;
216678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
217c98d5d94SLen Brown } *package_even, *package_odd;
218c98d5d94SLen Brown 
219c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
220c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
221c98d5d94SLen Brown 
22240f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no)	      \
22340f5cfe7SPrarit Bhargava 	((thread_base) +						      \
22440f5cfe7SPrarit Bhargava 	 ((pkg_no) *							      \
22540f5cfe7SPrarit Bhargava 	  topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
22640f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node * topo.threads_per_core) +	      \
22740f5cfe7SPrarit Bhargava 	 ((core_no) * topo.threads_per_core) +				      \
22840f5cfe7SPrarit Bhargava 	 (thread_no))
22940f5cfe7SPrarit Bhargava 
23040f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no)			\
23140f5cfe7SPrarit Bhargava 	((core_base) +							\
23240f5cfe7SPrarit Bhargava 	 ((pkg_no) *  topo.nodes_per_pkg * topo.cores_per_node) +	\
23340f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node) +				\
23440f5cfe7SPrarit Bhargava 	 (core_no))
23540f5cfe7SPrarit Bhargava 
23640f5cfe7SPrarit Bhargava 
237c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
238c98d5d94SLen Brown 
239388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
24041618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
241388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
242388e9c81SLen Brown 
243388e9c81SLen Brown struct msr_counter {
244388e9c81SLen Brown 	unsigned int msr_num;
245388e9c81SLen Brown 	char name[NAME_BYTES];
246495c7654SLen Brown 	char path[PATH_BYTES];
247388e9c81SLen Brown 	unsigned int width;
248388e9c81SLen Brown 	enum counter_type type;
249388e9c81SLen Brown 	enum counter_format format;
250388e9c81SLen Brown 	struct msr_counter *next;
251812db3f7SLen Brown 	unsigned int flags;
252812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
253812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
25441618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
255388e9c81SLen Brown };
256388e9c81SLen Brown 
257388e9c81SLen Brown struct sys_counters {
258678a3bd1SLen Brown 	unsigned int added_thread_counters;
259678a3bd1SLen Brown 	unsigned int added_core_counters;
260678a3bd1SLen Brown 	unsigned int added_package_counters;
261388e9c81SLen Brown 	struct msr_counter *tp;
262388e9c81SLen Brown 	struct msr_counter *cp;
263388e9c81SLen Brown 	struct msr_counter *pp;
264388e9c81SLen Brown } sys;
265388e9c81SLen Brown 
266c98d5d94SLen Brown struct system_summary {
267c98d5d94SLen Brown 	struct thread_data threads;
268c98d5d94SLen Brown 	struct core_data cores;
269c98d5d94SLen Brown 	struct pkg_data packages;
270388e9c81SLen Brown } average;
271c98d5d94SLen Brown 
2720e2d8f05SLen Brown struct cpu_topology {
2730e2d8f05SLen Brown 	int physical_package_id;
2740e2d8f05SLen Brown 	int logical_cpu_id;
275ef605741SPrarit Bhargava 	int physical_node_id;
276ef605741SPrarit Bhargava 	int logical_node_id;	/* 0-based count within the package */
2770e2d8f05SLen Brown 	int physical_core_id;
2788cb48b32SPrarit Bhargava 	int thread_id;
2790e2d8f05SLen Brown 	cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
2800e2d8f05SLen Brown } *cpus;
281c98d5d94SLen Brown 
282c98d5d94SLen Brown struct topo_params {
283c98d5d94SLen Brown 	int num_packages;
284103a8feaSLen Brown 	int num_cpus;
285c98d5d94SLen Brown 	int num_cores;
286c98d5d94SLen Brown 	int max_cpu_num;
287ef605741SPrarit Bhargava 	int max_node_num;
28870a9c6e8SPrarit Bhargava 	int nodes_per_pkg;
28970a9c6e8SPrarit Bhargava 	int cores_per_node;
29070a9c6e8SPrarit Bhargava 	int threads_per_core;
291c98d5d94SLen Brown } topo;
292103a8feaSLen Brown 
293c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
294103a8feaSLen Brown 
295562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
296562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
297562a2d37SLen Brown 
298c98d5d94SLen Brown void setup_all_buffers(void);
299103a8feaSLen Brown 
300c98d5d94SLen Brown int cpu_is_not_present(int cpu)
301d15cf7c1SLen Brown {
302c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
303c98d5d94SLen Brown }
304c98d5d94SLen Brown /*
305c98d5d94SLen Brown  * run func(thread, core, package) in topology order
306c98d5d94SLen Brown  * skip non-present cpus
307c98d5d94SLen Brown  */
308c98d5d94SLen Brown 
309c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
310c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
311c98d5d94SLen Brown {
31240f5cfe7SPrarit Bhargava 	int retval, pkg_no, core_no, thread_no, node_no;
313c98d5d94SLen Brown 
314c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
31570a9c6e8SPrarit Bhargava 		for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
31640f5cfe7SPrarit Bhargava 			for (node_no = 0; node_no < topo.nodes_per_pkg;
31740f5cfe7SPrarit Bhargava 			     node_no++) {
318c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
31970a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
320c98d5d94SLen Brown 					struct thread_data *t;
321c98d5d94SLen Brown 					struct core_data *c;
322c98d5d94SLen Brown 					struct pkg_data *p;
323c98d5d94SLen Brown 
32440f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
32540f5cfe7SPrarit Bhargava 						       core_no, node_no,
32640f5cfe7SPrarit Bhargava 						       pkg_no);
327c98d5d94SLen Brown 
328c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
329c98d5d94SLen Brown 						continue;
330c98d5d94SLen Brown 
33140f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
33240f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
333c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
334c98d5d94SLen Brown 
335c98d5d94SLen Brown 					retval = func(t, c, p);
336c98d5d94SLen Brown 					if (retval)
337c98d5d94SLen Brown 						return retval;
338c98d5d94SLen Brown 				}
339c98d5d94SLen Brown 			}
340c98d5d94SLen Brown 		}
34140f5cfe7SPrarit Bhargava 	}
342d15cf7c1SLen Brown 	return 0;
343d15cf7c1SLen Brown }
344d15cf7c1SLen Brown 
34588c3281fSLen Brown int cpu_migrate(int cpu)
34688c3281fSLen Brown {
347c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
348c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
349c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
35088c3281fSLen Brown 		return -1;
35188c3281fSLen Brown 	else
35288c3281fSLen Brown 		return 0;
35388c3281fSLen Brown }
35436229897SLen Brown int get_msr_fd(int cpu)
355103a8feaSLen Brown {
356103a8feaSLen Brown 	char pathname[32];
357103a8feaSLen Brown 	int fd;
358103a8feaSLen Brown 
35936229897SLen Brown 	fd = fd_percpu[cpu];
36036229897SLen Brown 
36136229897SLen Brown 	if (fd)
36236229897SLen Brown 		return fd;
36336229897SLen Brown 
364103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
365103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
36615aaa346SLen Brown 	if (fd < 0)
36798481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
368103a8feaSLen Brown 
36936229897SLen Brown 	fd_percpu[cpu] = fd;
37036229897SLen Brown 
37136229897SLen Brown 	return fd;
37236229897SLen Brown }
37336229897SLen Brown 
37436229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
37536229897SLen Brown {
37636229897SLen Brown 	ssize_t retval;
37736229897SLen Brown 
37836229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
37915aaa346SLen Brown 
38098481e79SLen Brown 	if (retval != sizeof *msr)
381cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
38215aaa346SLen Brown 
38315aaa346SLen Brown 	return 0;
384103a8feaSLen Brown }
385103a8feaSLen Brown 
386fc04cc67SLen Brown /*
387bdd5ae3aSLen Brown  * This list matches the column headers, except
388bdd5ae3aSLen Brown  * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
389bdd5ae3aSLen Brown  * 2. Core and CPU are moved to the end, we can't have strings that contain them
390bdd5ae3aSLen Brown  *    matching on them for --show and --hide.
391fc04cc67SLen Brown  */
392812db3f7SLen Brown struct msr_counter bic[] = {
3933f44a5c6SLen Brown 	{ 0x0, "usec" },
3943f44a5c6SLen Brown 	{ 0x0, "Time_Of_Day_Seconds" },
395812db3f7SLen Brown 	{ 0x0, "Package" },
396bdd5ae3aSLen Brown 	{ 0x0, "Node" },
397812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
398bdd5ae3aSLen Brown 	{ 0x0, "Busy%" },
399812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
400812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
401812db3f7SLen Brown 	{ 0x0, "IRQ" },
402495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
403bdd5ae3aSLen Brown 	{ 0x0, "sysfs" },
404812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
405812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
406812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
407812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
408812db3f7SLen Brown 	{ 0x0, "ThreadC" },
409812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
410812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
411812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
412812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
413812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
414812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
415812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
416812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
417812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
4180f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
4190f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
4204bd1f8f2SLen Brown 	{ 0x0, "Pk%pc10" },
421be0e54c4SLen Brown 	{ 0x0, "CPU%LPI" },
422be0e54c4SLen Brown 	{ 0x0, "SYS%LPI" },
423812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
424812db3f7SLen Brown 	{ 0x0, "CorWatt" },
425812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
426812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
427812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
428812db3f7SLen Brown 	{ 0x0, "PKG_%" },
429812db3f7SLen Brown 	{ 0x0, "RAM_%" },
430812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
431812db3f7SLen Brown 	{ 0x0, "Cor_J" },
432812db3f7SLen Brown 	{ 0x0, "GFX_J" },
433812db3f7SLen Brown 	{ 0x0, "RAM_J" },
4340539ba11SLen Brown 	{ 0x0, "Mod%c6" },
435a99d8730SLen Brown 	{ 0x0, "Totl%C0" },
436a99d8730SLen Brown 	{ 0x0, "Any%C0" },
437a99d8730SLen Brown 	{ 0x0, "GFX%C0" },
438a99d8730SLen Brown 	{ 0x0, "CPUGFX%" },
439bdd5ae3aSLen Brown 	{ 0x0, "Core" },
440bdd5ae3aSLen Brown 	{ 0x0, "CPU" },
4414c2122d4SLen Brown 	{ 0x0, "APIC" },
4424c2122d4SLen Brown 	{ 0x0, "X2APIC" },
443812db3f7SLen Brown };
444812db3f7SLen Brown 
445812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
4463f44a5c6SLen Brown #define	BIC_USEC	(1ULL << 0)
4473f44a5c6SLen Brown #define	BIC_TOD		(1ULL << 1)
4483f44a5c6SLen Brown #define	BIC_Package	(1ULL << 2)
449bdd5ae3aSLen Brown #define	BIC_Node	(1ULL << 3)
450bdd5ae3aSLen Brown #define	BIC_Avg_MHz	(1ULL << 4)
451bdd5ae3aSLen Brown #define	BIC_Busy	(1ULL << 5)
452bdd5ae3aSLen Brown #define	BIC_Bzy_MHz	(1ULL << 6)
453bdd5ae3aSLen Brown #define	BIC_TSC_MHz	(1ULL << 7)
454bdd5ae3aSLen Brown #define	BIC_IRQ		(1ULL << 8)
455bdd5ae3aSLen Brown #define	BIC_SMI		(1ULL << 9)
456bdd5ae3aSLen Brown #define	BIC_sysfs	(1ULL << 10)
457bdd5ae3aSLen Brown #define	BIC_CPU_c1	(1ULL << 11)
458bdd5ae3aSLen Brown #define	BIC_CPU_c3	(1ULL << 12)
459bdd5ae3aSLen Brown #define	BIC_CPU_c6	(1ULL << 13)
460bdd5ae3aSLen Brown #define	BIC_CPU_c7	(1ULL << 14)
461bdd5ae3aSLen Brown #define	BIC_ThreadC	(1ULL << 15)
462bdd5ae3aSLen Brown #define	BIC_CoreTmp	(1ULL << 16)
463bdd5ae3aSLen Brown #define	BIC_CoreCnt	(1ULL << 17)
464bdd5ae3aSLen Brown #define	BIC_PkgTmp	(1ULL << 18)
465bdd5ae3aSLen Brown #define	BIC_GFX_rc6	(1ULL << 19)
466bdd5ae3aSLen Brown #define	BIC_GFXMHz	(1ULL << 20)
467bdd5ae3aSLen Brown #define	BIC_Pkgpc2	(1ULL << 21)
468bdd5ae3aSLen Brown #define	BIC_Pkgpc3	(1ULL << 22)
469bdd5ae3aSLen Brown #define	BIC_Pkgpc6	(1ULL << 23)
470bdd5ae3aSLen Brown #define	BIC_Pkgpc7	(1ULL << 24)
471bdd5ae3aSLen Brown #define	BIC_Pkgpc8	(1ULL << 25)
472bdd5ae3aSLen Brown #define	BIC_Pkgpc9	(1ULL << 26)
473bdd5ae3aSLen Brown #define	BIC_Pkgpc10	(1ULL << 27)
474bdd5ae3aSLen Brown #define BIC_CPU_LPI	(1ULL << 28)
475bdd5ae3aSLen Brown #define BIC_SYS_LPI	(1ULL << 29)
476bdd5ae3aSLen Brown #define	BIC_PkgWatt	(1ULL << 30)
477bdd5ae3aSLen Brown #define	BIC_CorWatt	(1ULL << 31)
478bdd5ae3aSLen Brown #define	BIC_GFXWatt	(1ULL << 32)
479bdd5ae3aSLen Brown #define	BIC_PkgCnt	(1ULL << 33)
480bdd5ae3aSLen Brown #define	BIC_RAMWatt	(1ULL << 34)
481bdd5ae3aSLen Brown #define	BIC_PKG__	(1ULL << 35)
482bdd5ae3aSLen Brown #define	BIC_RAM__	(1ULL << 36)
483bdd5ae3aSLen Brown #define	BIC_Pkg_J	(1ULL << 37)
484bdd5ae3aSLen Brown #define	BIC_Cor_J	(1ULL << 38)
485bdd5ae3aSLen Brown #define	BIC_GFX_J	(1ULL << 39)
486bdd5ae3aSLen Brown #define	BIC_RAM_J	(1ULL << 40)
487bdd5ae3aSLen Brown #define	BIC_Mod_c6	(1ULL << 41)
488bdd5ae3aSLen Brown #define	BIC_Totl_c0	(1ULL << 42)
489bdd5ae3aSLen Brown #define	BIC_Any_c0	(1ULL << 43)
490bdd5ae3aSLen Brown #define	BIC_GFX_c0	(1ULL << 44)
491bdd5ae3aSLen Brown #define	BIC_CPUGFX	(1ULL << 45)
492bdd5ae3aSLen Brown #define	BIC_Core	(1ULL << 46)
493bdd5ae3aSLen Brown #define	BIC_CPU		(1ULL << 47)
4944c2122d4SLen Brown #define	BIC_APIC	(1ULL << 48)
4954c2122d4SLen Brown #define	BIC_X2APIC	(1ULL << 49)
496812db3f7SLen Brown 
4974c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT	(BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
4983f44a5c6SLen Brown 
4993f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
5004c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
501812db3f7SLen Brown 
502812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
5033f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
504812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
5050f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
506812db3f7SLen Brown 
5073f44a5c6SLen Brown 
508dd778a5eSLen Brown #define MAX_DEFERRED 16
509dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
510dd778a5eSLen Brown int deferred_skip_index;
511dd778a5eSLen Brown 
512dd778a5eSLen Brown /*
513dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
514dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
515dd778a5eSLen Brown  */
516dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
517dd778a5eSLen Brown 
518dd778a5eSLen Brown void help(void)
519dd778a5eSLen Brown {
520dd778a5eSLen Brown 	fprintf(outf,
521dd778a5eSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
522dd778a5eSLen Brown 	"\n"
523dd778a5eSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
524dd778a5eSLen Brown 	"when COMMAND completes.\n"
525dd778a5eSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
526dd778a5eSLen Brown 	"to print statistics, until interrupted.\n"
527cc481650SNathan Ciobanu 	"  -a, --add	add a counter\n"
528dd778a5eSLen Brown 	"		  eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
529cc481650SNathan Ciobanu 	"  -c, --cpu	cpu-set	limit output to summary plus cpu-set:\n"
530dd778a5eSLen Brown 	"		  {core | package | j,k,l..m,n-p }\n"
5319ce80578SNathan Ciobanu 	"  -d, --debug	displays usec, Time_Of_Day_Seconds and more debugging\n"
5329ce80578SNathan Ciobanu 	"  -D, --Dump	displays the raw counter values\n"
5339ce80578SNathan Ciobanu 	"  -e, --enable	[all | column]\n"
5349ce80578SNathan Ciobanu 	"		shows all or the specified disabled column\n"
5359ce80578SNathan Ciobanu 	"  -H, --hide [column|column,column,...]\n"
5369ce80578SNathan Ciobanu 	"		hide the specified column(s)\n"
537cc481650SNathan Ciobanu 	"  -i, --interval sec.subsec\n"
538cc481650SNathan Ciobanu 	"		Override default 5-second measurement interval\n"
5399ce80578SNathan Ciobanu 	"  -J, --Joules	displays energy in Joules instead of Watts\n"
540cc481650SNathan Ciobanu 	"  -l, --list	list column headers only\n"
541cc481650SNathan Ciobanu 	"  -n, --num_iterations num\n"
542cc481650SNathan Ciobanu 	"		number of the measurement iterations\n"
543cc481650SNathan Ciobanu 	"  -o, --out file\n"
544cc481650SNathan Ciobanu 	"		create or truncate \"file\" for all output\n"
545cc481650SNathan Ciobanu 	"  -q, --quiet	skip decoding system configuration header\n"
5469ce80578SNathan Ciobanu 	"  -s, --show [column|column,column,...]\n"
5479ce80578SNathan Ciobanu 	"		show only the specified column(s)\n"
5489ce80578SNathan Ciobanu 	"  -S, --Summary\n"
5499ce80578SNathan Ciobanu 	"		limits output to 1-line system summary per interval\n"
5509ce80578SNathan Ciobanu 	"  -T, --TCC temperature\n"
5519ce80578SNathan Ciobanu 	"		sets the Thermal Control Circuit temperature in\n"
5529ce80578SNathan Ciobanu 	"		  degrees Celsius\n"
553cc481650SNathan Ciobanu 	"  -h, --help	print this help message\n"
554cc481650SNathan Ciobanu 	"  -v, --version	print version information\n"
555dd778a5eSLen Brown 	"\n"
556dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
557dd778a5eSLen Brown }
558dd778a5eSLen Brown 
559812db3f7SLen Brown /*
560812db3f7SLen Brown  * bic_lookup
561812db3f7SLen Brown  * for all the strings in comma separate name_list,
562812db3f7SLen Brown  * set the approprate bit in return value.
563812db3f7SLen Brown  */
564dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
565812db3f7SLen Brown {
566812db3f7SLen Brown 	int i;
567812db3f7SLen Brown 	unsigned long long retval = 0;
568812db3f7SLen Brown 
569812db3f7SLen Brown 	while (name_list) {
570812db3f7SLen Brown 		char *comma;
571812db3f7SLen Brown 
572812db3f7SLen Brown 		comma = strchr(name_list, ',');
573812db3f7SLen Brown 
574812db3f7SLen Brown 		if (comma)
575812db3f7SLen Brown 			*comma = '\0';
576812db3f7SLen Brown 
5773f44a5c6SLen Brown 		if (!strcmp(name_list, "all"))
5783f44a5c6SLen Brown 			return ~0;
5793f44a5c6SLen Brown 
580812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
581812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
582812db3f7SLen Brown 				retval |= (1ULL << i);
583812db3f7SLen Brown 				break;
584812db3f7SLen Brown 			}
585812db3f7SLen Brown 		}
586812db3f7SLen Brown 		if (i == MAX_BIC) {
587dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
588812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
589812db3f7SLen Brown 				exit(-1);
590812db3f7SLen Brown 			}
591dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
592dd778a5eSLen Brown 			if (debug)
593dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
594dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
595dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
596dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
597dd778a5eSLen Brown 				help();
598dd778a5eSLen Brown 				exit(1);
599dd778a5eSLen Brown 			}
600dd778a5eSLen Brown 		}
601812db3f7SLen Brown 
602812db3f7SLen Brown 		name_list = comma;
603812db3f7SLen Brown 		if (name_list)
604812db3f7SLen Brown 			name_list++;
605812db3f7SLen Brown 
606812db3f7SLen Brown 	}
607812db3f7SLen Brown 	return retval;
608812db3f7SLen Brown }
609fc04cc67SLen Brown 
610dd778a5eSLen Brown 
611c8ade361SLen Brown void print_header(char *delim)
612103a8feaSLen Brown {
613388e9c81SLen Brown 	struct msr_counter *mp;
6146168c2e0SLen Brown 	int printed = 0;
615388e9c81SLen Brown 
6163f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
6173f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
6183f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
6193f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
620812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
6216168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
62201235041SPrarit Bhargava 	if (DO_BIC(BIC_Node))
62301235041SPrarit Bhargava 		outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
624812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
6256168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
626812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
6276168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
6284c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
6294c2122d4SLen Brown 		outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
6304c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
6314c2122d4SLen Brown 		outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
632812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
6336168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
634812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
6356168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
636812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
6376168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
638812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
6396168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
6401cc21f7bSLen Brown 
6410de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
6420de6c0dfSLen Brown 		if (sums_need_wide_columns)
6436168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
6440de6c0dfSLen Brown 		else
6456168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
6460de6c0dfSLen Brown 	}
6470de6c0dfSLen Brown 
648812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
6496168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
6501cc21f7bSLen Brown 
651388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
652dd778a5eSLen Brown 
653388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
654388e9c81SLen Brown 			if (mp->width == 64)
655dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
656388e9c81SLen Brown 			else
657dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
658388e9c81SLen Brown 		} else {
6590de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
660dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
6610de6c0dfSLen Brown 			else
662dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
663388e9c81SLen Brown 		}
664388e9c81SLen Brown 	}
665388e9c81SLen Brown 
66641618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
6676168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
668997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
6696168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
670812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
6716168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
672812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
6736168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
674678a3bd1SLen Brown 
6750539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
6766168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
677678a3bd1SLen Brown 
678812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
6796168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
680388e9c81SLen Brown 
681388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
682388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
683388e9c81SLen Brown 			if (mp->width == 64)
684c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
685388e9c81SLen Brown 			else
686c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
687388e9c81SLen Brown 		} else {
6880de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6890de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6900de6c0dfSLen Brown 			else
691c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
692388e9c81SLen Brown 		}
693388e9c81SLen Brown 	}
694388e9c81SLen Brown 
695812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
6966168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
697889facbeSLen Brown 
698812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
6996168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
700fdf676e5SLen Brown 
701812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
7026168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
70327d47356SLen Brown 
704a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
7056168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
706a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
7076168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
708a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
7096168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
710a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
7116168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
7120b2bb692SLen Brown 
7130f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
7146168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
7150f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
7166168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
7170f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
7186168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
7190f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
7206168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
7210f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
7226168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
7230f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
7246168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
7250f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
7266168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
727be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
728be0e54c4SLen Brown 		outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
729be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
730be0e54c4SLen Brown 		outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
731103a8feaSLen Brown 
7325c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
733812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
7346168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
735812db3f7SLen Brown 		if (DO_BIC(BIC_CorWatt))
7366168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
737812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
7386168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
739812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
7406168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
741812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7426168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
743812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7446168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
745d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
746812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
7476168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
748812db3f7SLen Brown 		if (DO_BIC(BIC_Cor_J))
7496168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
750812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
7516168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
752812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
7536168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
754812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7556168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
756812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7576168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
7585c56be9aSDirk Brandewie 	}
759388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
760388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
761388e9c81SLen Brown 			if (mp->width == 64)
762c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
763388e9c81SLen Brown 			else
764c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
765388e9c81SLen Brown 		} else {
7660de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7670de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7680de6c0dfSLen Brown 			else
769c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
770388e9c81SLen Brown 		}
771388e9c81SLen Brown 	}
772388e9c81SLen Brown 
773c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
774103a8feaSLen Brown }
775103a8feaSLen Brown 
776c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
777c98d5d94SLen Brown 	struct pkg_data *p)
778103a8feaSLen Brown {
779388e9c81SLen Brown 	int i;
780388e9c81SLen Brown 	struct msr_counter *mp;
781388e9c81SLen Brown 
7823b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
783c98d5d94SLen Brown 
784c98d5d94SLen Brown 	if (t) {
7853b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
7863b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
7873b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
7883b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
7893b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
7903b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
7916886fee4SLen Brown 
792812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
7930de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
794812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
795218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
796388e9c81SLen Brown 
797388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
798388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
799388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
800388e9c81SLen Brown 		}
801103a8feaSLen Brown 	}
802103a8feaSLen Brown 
803c98d5d94SLen Brown 	if (c) {
8043b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
8053b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
8063b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
8073b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
8083b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
809388e9c81SLen Brown 
810388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
811388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
812388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
813388e9c81SLen Brown 		}
8140539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
815c98d5d94SLen Brown 	}
816103a8feaSLen Brown 
817c98d5d94SLen Brown 	if (p) {
8183b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
8190b2bb692SLen Brown 
8200b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
8210b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
8220b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
8230b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
8240b2bb692SLen Brown 
8253b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
8260f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
8273b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
8280f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
8293b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
8300f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
8313b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
8323b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
8333b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
8343b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
835be0e54c4SLen Brown 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
836be0e54c4SLen Brown 		outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
837be0e54c4SLen Brown 		outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
8383b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
8393b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
8403b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
8413b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
8423b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
8433b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
8443b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
8453b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
8463b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
847388e9c81SLen Brown 
848388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
849388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
850388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
851388e9c81SLen Brown 		}
852c98d5d94SLen Brown 	}
8533b4d5c7fSAndy Shevchenko 
8543b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
8553b4d5c7fSAndy Shevchenko 
856c98d5d94SLen Brown 	return 0;
857103a8feaSLen Brown }
858103a8feaSLen Brown 
859e23da037SLen Brown /*
860e23da037SLen Brown  * column formatting convention & formats
861e23da037SLen Brown  */
862c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
863c98d5d94SLen Brown 	struct pkg_data *p)
864103a8feaSLen Brown {
865008d396eSLen Brown 	double interval_float, tsc;
866fc04cc67SLen Brown 	char *fmt8;
867388e9c81SLen Brown 	int i;
868388e9c81SLen Brown 	struct msr_counter *mp;
8696168c2e0SLen Brown 	char *delim = "\t";
8706168c2e0SLen Brown 	int printed = 0;
871103a8feaSLen Brown 
872c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
873c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
874c98d5d94SLen Brown 		return 0;
875c98d5d94SLen Brown 
876c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
877c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
878c98d5d94SLen Brown 		return 0;
879c98d5d94SLen Brown 
8801ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
8811ef7d21aSLen Brown 	if ((t != &average.threads) &&
8821ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
8831ef7d21aSLen Brown 		return 0;
8841ef7d21aSLen Brown 
8853f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
886f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
887f4fdf2b4SLen Brown 		struct timeval tv;
888f4fdf2b4SLen Brown 
889f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
890f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
891f4fdf2b4SLen Brown 	}
892f4fdf2b4SLen Brown 
8933f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
8943f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
8953f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
8963f44a5c6SLen Brown 
897103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
898103a8feaSLen Brown 
899008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
900008d396eSLen Brown 
901c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
902c98d5d94SLen Brown 	if (t == &average.threads) {
903812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
9046168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
90501235041SPrarit Bhargava 		if (DO_BIC(BIC_Node))
90601235041SPrarit Bhargava 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
907812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
9086168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
909812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9106168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9114c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9124c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9134c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9144c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
915103a8feaSLen Brown 	} else {
916812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
917c98d5d94SLen Brown 			if (p)
9186168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
919c98d5d94SLen Brown 			else
9206168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
921c98d5d94SLen Brown 		}
92201235041SPrarit Bhargava 		if (DO_BIC(BIC_Node)) {
92301235041SPrarit Bhargava 			if (t)
92401235041SPrarit Bhargava 				outp += sprintf(outp, "%s%d",
92501235041SPrarit Bhargava 						(printed++ ? delim : ""),
92601235041SPrarit Bhargava 					      cpus[t->cpu_id].physical_node_id);
92701235041SPrarit Bhargava 			else
92801235041SPrarit Bhargava 				outp += sprintf(outp, "%s-",
92901235041SPrarit Bhargava 						(printed++ ? delim : ""));
93001235041SPrarit Bhargava 		}
931812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
932c98d5d94SLen Brown 			if (c)
9336168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
934c98d5d94SLen Brown 			else
9356168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
936c98d5d94SLen Brown 		}
937812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9386168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
9394c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9404c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
9414c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9424c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
943103a8feaSLen Brown 	}
944fc04cc67SLen Brown 
945812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
9466168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
947fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
948fc04cc67SLen Brown 
949812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
9506168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
951103a8feaSLen Brown 
952812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
95321ed5574SLen Brown 		if (has_base_hz)
9546168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
95521ed5574SLen Brown 		else
9566168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
957008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
95821ed5574SLen Brown 	}
959103a8feaSLen Brown 
960812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
9616168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
962103a8feaSLen Brown 
963562a2d37SLen Brown 	/* IRQ */
9640de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
9650de6c0dfSLen Brown 		if (sums_need_wide_columns)
9666168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
9670de6c0dfSLen Brown 		else
9686168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
9690de6c0dfSLen Brown 	}
970562a2d37SLen Brown 
9711cc21f7bSLen Brown 	/* SMI */
972812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
9736168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
9741cc21f7bSLen Brown 
975678a3bd1SLen Brown 	/* Added counters */
976678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
977678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
978678a3bd1SLen Brown 			if (mp->width == 32)
9795f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
980678a3bd1SLen Brown 			else
9816168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
982678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
9830de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9846168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
9850de6c0dfSLen Brown 			else
9866168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
987678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
98841618e63SLen Brown 			if (mp->type == COUNTER_USEC)
9896168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
99041618e63SLen Brown 			else
9916168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
992678a3bd1SLen Brown 		}
993678a3bd1SLen Brown 	}
994678a3bd1SLen Brown 
99541618e63SLen Brown 	/* C1 */
99641618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
9976168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
99841618e63SLen Brown 
99941618e63SLen Brown 
1000c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
1001c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1002c98d5d94SLen Brown 		goto done;
1003c98d5d94SLen Brown 
1004997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
10056168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
1006812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
10076168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
1008812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
10096168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
1010c98d5d94SLen Brown 
10110539ba11SLen Brown 	/* Mod%c6 */
10120539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
10136168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
10140539ba11SLen Brown 
1015812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
10166168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1017889facbeSLen Brown 
1018388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1019388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1020388e9c81SLen Brown 			if (mp->width == 32)
10215f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1022388e9c81SLen Brown 			else
10236168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1024388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10250de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10266168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
10270de6c0dfSLen Brown 			else
10286168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1029388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10306168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1031388e9c81SLen Brown 		}
1032388e9c81SLen Brown 	}
1033388e9c81SLen Brown 
1034c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
1035c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1036c98d5d94SLen Brown 		goto done;
1037c98d5d94SLen Brown 
10380b2bb692SLen Brown 	/* PkgTmp */
1039812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
10406168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1041889facbeSLen Brown 
1042fdf676e5SLen Brown 	/* GFXrc6 */
1043812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
1044ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
10456168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
10469185e988SLen Brown 		} else {
10476168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
10489185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
10499185e988SLen Brown 		}
10509185e988SLen Brown 	}
1051fdf676e5SLen Brown 
105227d47356SLen Brown 	/* GFXMHz */
1053812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
10546168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
105527d47356SLen Brown 
10560b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1057a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
10586168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1059a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
10606168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1061a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
10626168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1063a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
10646168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
10650b2bb692SLen Brown 
10660f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
10676168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
10680f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
10696168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
10700f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
10716168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
10720f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
10736168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
10740f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
10756168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
10760f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
10776168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
10780f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
10796168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1080889facbeSLen Brown 
1081be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1082be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1083be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1084be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1085be0e54c4SLen Brown 
1086889facbeSLen Brown 	/*
1087889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
1088889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
1089889facbeSLen Brown  	 */
1090fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
10916168c2e0SLen Brown 		fmt8 = "%s%.2f";
1092fc04cc67SLen Brown 	else
1093fc04cc67SLen Brown 		fmt8 = "%6.0f**";
1094889facbeSLen Brown 
1095812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
10966168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1097812db3f7SLen Brown 	if (DO_BIC(BIC_CorWatt))
10986168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1099812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
11006168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1101812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
11026168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1103812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
11046168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1105812db3f7SLen Brown 	if (DO_BIC(BIC_Cor_J))
11066168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1107812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
11086168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1109812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
11106168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1111812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
11126168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1113812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
11146168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1115812db3f7SLen Brown 
1116388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1117388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1118388e9c81SLen Brown 			if (mp->width == 32)
11195f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1120388e9c81SLen Brown 			else
11216168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1122388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
11230de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
11246168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
11250de6c0dfSLen Brown 			else
11266168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1127388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
11286168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1129388e9c81SLen Brown 		}
1130388e9c81SLen Brown 	}
1131388e9c81SLen Brown 
1132c98d5d94SLen Brown done:
113394d6ab4bSLen Brown 	if (*(outp - 1) != '\n')
1134c98d5d94SLen Brown 		outp += sprintf(outp, "\n");
1135c98d5d94SLen Brown 
1136c98d5d94SLen Brown 	return 0;
1137103a8feaSLen Brown }
1138103a8feaSLen Brown 
1139b7d8c148SLen Brown void flush_output_stdout(void)
1140103a8feaSLen Brown {
1141b7d8c148SLen Brown 	FILE *filep;
1142b7d8c148SLen Brown 
1143b7d8c148SLen Brown 	if (outf == stderr)
1144b7d8c148SLen Brown 		filep = stdout;
1145b7d8c148SLen Brown 	else
1146b7d8c148SLen Brown 		filep = outf;
1147b7d8c148SLen Brown 
1148b7d8c148SLen Brown 	fputs(output_buffer, filep);
1149b7d8c148SLen Brown 	fflush(filep);
1150b7d8c148SLen Brown 
1151c98d5d94SLen Brown 	outp = output_buffer;
1152c98d5d94SLen Brown }
1153b7d8c148SLen Brown void flush_output_stderr(void)
1154c98d5d94SLen Brown {
1155b7d8c148SLen Brown 	fputs(output_buffer, outf);
1156b7d8c148SLen Brown 	fflush(outf);
1157c98d5d94SLen Brown 	outp = output_buffer;
1158c98d5d94SLen Brown }
1159c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1160c98d5d94SLen Brown {
1161e23da037SLen Brown 	static int printed;
1162103a8feaSLen Brown 
1163e23da037SLen Brown 	if (!printed || !summary_only)
1164c8ade361SLen Brown 		print_header("\t");
1165103a8feaSLen Brown 
1166*9d83601aSLen Brown 	format_counters(&average.threads, &average.cores, &average.packages);
1167103a8feaSLen Brown 
1168e23da037SLen Brown 	printed = 1;
1169e23da037SLen Brown 
1170e23da037SLen Brown 	if (summary_only)
1171e23da037SLen Brown 		return;
1172e23da037SLen Brown 
1173c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1174103a8feaSLen Brown }
1175103a8feaSLen Brown 
1176889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1177889facbeSLen Brown 	if (new > old) {			\
1178889facbeSLen Brown 		old = new - old;		\
1179889facbeSLen Brown 	} else {				\
1180889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1181889facbeSLen Brown 	}
1182889facbeSLen Brown 
1183ba3dec99SLen Brown int
1184c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1185103a8feaSLen Brown {
1186388e9c81SLen Brown 	int i;
1187388e9c81SLen Brown 	struct msr_counter *mp;
11880b2bb692SLen Brown 
1189a99d8730SLen Brown 
1190a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
11910b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1192a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
11930b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1194a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
11950b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1196a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
11970b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1198a99d8730SLen Brown 
1199c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
12000f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1201c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
12020f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1203c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
12040f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1205c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1206ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1207ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1208ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1209be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1210be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1211889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1212889facbeSLen Brown 
12139185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
12149185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
12159185e988SLen Brown 		old->gfx_rc6_ms = -1;
12169185e988SLen Brown 	else
1217fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
12189185e988SLen Brown 
121927d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
122027d47356SLen Brown 
1221889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1222889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1223889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1224889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1225889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1226889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1227ba3dec99SLen Brown 
1228388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1229388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1230388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1231388e9c81SLen Brown 		else
1232388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1233388e9c81SLen Brown 	}
1234388e9c81SLen Brown 
1235ba3dec99SLen Brown 	return 0;
1236103a8feaSLen Brown }
1237103a8feaSLen Brown 
1238c98d5d94SLen Brown void
1239c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1240c98d5d94SLen Brown {
1241388e9c81SLen Brown 	int i;
1242388e9c81SLen Brown 	struct msr_counter *mp;
1243388e9c81SLen Brown 
1244c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1245c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1246c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1247889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
12480539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1249388e9c81SLen Brown 
1250388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1251388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1252388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1253388e9c81SLen Brown 		else
1254388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1255388e9c81SLen Brown 	}
1256103a8feaSLen Brown }
1257c98d5d94SLen Brown 
1258c3ae331dSLen Brown /*
1259c3ae331dSLen Brown  * old = new - old
1260c3ae331dSLen Brown  */
1261ba3dec99SLen Brown int
1262c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1263c98d5d94SLen Brown 	struct core_data *core_delta)
1264c98d5d94SLen Brown {
1265388e9c81SLen Brown 	int i;
1266388e9c81SLen Brown 	struct msr_counter *mp;
1267388e9c81SLen Brown 
12684c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
12694c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
12704c2122d4SLen Brown 		new->apic_id = old->apic_id;
12714c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
12724c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
12734c2122d4SLen Brown 
12743f44a5c6SLen Brown 	/*
12753f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
12763f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
12773f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
12783f44a5c6SLen Brown 	 */
12793f44a5c6SLen Brown 
12803f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
12813f44a5c6SLen Brown 	old->tv_end = new->tv_end;
12823f44a5c6SLen Brown 
1283c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1284c98d5d94SLen Brown 
1285103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1286b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1287b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1288b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1289b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1290103a8feaSLen Brown 
1291c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1292c98d5d94SLen Brown 
1293812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1294c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1295c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1296c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1297c98d5d94SLen Brown 		} else {
1298ba3dec99SLen Brown 			return -1;
1299103a8feaSLen Brown 		}
1300a729617cSLen Brown 	}
1301103a8feaSLen Brown 
1302c98d5d94SLen Brown 
1303144b44b1SLen Brown 	if (use_c1_residency_msr) {
1304144b44b1SLen Brown 		/*
1305144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1306144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1307144b44b1SLen Brown 		 */
1308144b44b1SLen Brown 	} else {
1309103a8feaSLen Brown 		/*
1310c3ae331dSLen Brown 		 * As counter collection is not atomic,
1311c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1312103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1313103a8feaSLen Brown 		 */
131495149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1315c98d5d94SLen Brown 			old->c1 = 0;
1316c98d5d94SLen Brown 		else {
1317c98d5d94SLen Brown 			/* normal case, derive c1 */
1318008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1319c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1320c98d5d94SLen Brown 		}
1321144b44b1SLen Brown 	}
1322c3ae331dSLen Brown 
1323c98d5d94SLen Brown 	if (old->mperf == 0) {
1324b7d8c148SLen Brown 		if (debug > 1)
1325b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1326c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1327c98d5d94SLen Brown 	}
1328103a8feaSLen Brown 
1329812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1330562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1331562a2d37SLen Brown 
1332812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
13331ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1334ba3dec99SLen Brown 
1335388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1336388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1337388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1338388e9c81SLen Brown 		else
1339388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1340388e9c81SLen Brown 	}
1341ba3dec99SLen Brown 	return 0;
1342103a8feaSLen Brown }
1343c98d5d94SLen Brown 
1344c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1345c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1346c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1347c98d5d94SLen Brown {
1348ba3dec99SLen Brown 	int retval = 0;
1349ba3dec99SLen Brown 
1350c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1351c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1352c98d5d94SLen Brown 		delta_core(c, c2);
1353c98d5d94SLen Brown 
1354c98d5d94SLen Brown 	/* always calculate thread delta */
1355ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1356ba3dec99SLen Brown 	if (retval)
1357ba3dec99SLen Brown 		return retval;
1358c98d5d94SLen Brown 
1359c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1360c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1361ba3dec99SLen Brown 		retval = delta_package(p, p2);
1362c98d5d94SLen Brown 
1363ba3dec99SLen Brown 	return retval;
1364103a8feaSLen Brown }
1365103a8feaSLen Brown 
1366c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1367103a8feaSLen Brown {
1368388e9c81SLen Brown 	int i;
1369388e9c81SLen Brown 	struct msr_counter  *mp;
1370388e9c81SLen Brown 
13713f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
13723f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
13733f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
13743f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
13753f44a5c6SLen Brown 
1376c98d5d94SLen Brown 	t->tsc = 0;
1377c98d5d94SLen Brown 	t->aperf = 0;
1378c98d5d94SLen Brown 	t->mperf = 0;
1379c98d5d94SLen Brown 	t->c1 = 0;
1380103a8feaSLen Brown 
1381562a2d37SLen Brown 	t->irq_count = 0;
1382562a2d37SLen Brown 	t->smi_count = 0;
1383562a2d37SLen Brown 
1384c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1385c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1386c98d5d94SLen Brown 
1387c98d5d94SLen Brown 	c->c3 = 0;
1388c98d5d94SLen Brown 	c->c6 = 0;
1389c98d5d94SLen Brown 	c->c7 = 0;
13900539ba11SLen Brown 	c->mc6_us = 0;
1391889facbeSLen Brown 	c->core_temp_c = 0;
1392c98d5d94SLen Brown 
13930b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
13940b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
13950b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
13960b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
13970b2bb692SLen Brown 
1398c98d5d94SLen Brown 	p->pc2 = 0;
13990f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1400c98d5d94SLen Brown 		p->pc3 = 0;
14010f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1402c98d5d94SLen Brown 		p->pc6 = 0;
14030f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1404c98d5d94SLen Brown 		p->pc7 = 0;
1405ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1406ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1407ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1408be0e54c4SLen Brown 	p->cpu_lpi = 0;
1409be0e54c4SLen Brown 	p->sys_lpi = 0;
1410889facbeSLen Brown 
1411889facbeSLen Brown 	p->energy_pkg = 0;
1412889facbeSLen Brown 	p->energy_dram = 0;
1413889facbeSLen Brown 	p->energy_cores = 0;
1414889facbeSLen Brown 	p->energy_gfx = 0;
1415889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1416889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1417889facbeSLen Brown 	p->pkg_temp_c = 0;
141827d47356SLen Brown 
1419fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
142027d47356SLen Brown 	p->gfx_mhz = 0;
1421388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1422388e9c81SLen Brown 		t->counter[i] = 0;
1423388e9c81SLen Brown 
1424388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1425388e9c81SLen Brown 		c->counter[i] = 0;
1426388e9c81SLen Brown 
1427388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1428388e9c81SLen Brown 		p->counter[i] = 0;
1429103a8feaSLen Brown }
1430c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1431c98d5d94SLen Brown 	struct pkg_data *p)
1432103a8feaSLen Brown {
1433388e9c81SLen Brown 	int i;
1434388e9c81SLen Brown 	struct msr_counter *mp;
1435388e9c81SLen Brown 
14364c2122d4SLen Brown 	/* copy un-changing apic_id's */
14374c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
14384c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
14394c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
14404c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
14414c2122d4SLen Brown 
14423f44a5c6SLen Brown 	/* remember first tv_begin */
14433f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
14443f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
14453f44a5c6SLen Brown 
14463f44a5c6SLen Brown 	/* remember last tv_end */
14473f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
14483f44a5c6SLen Brown 
1449c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1450c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1451c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1452c98d5d94SLen Brown 	average.threads.c1 += t->c1;
145315aaa346SLen Brown 
1454562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1455562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1456562a2d37SLen Brown 
1457388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1458388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1459388e9c81SLen Brown 			continue;
1460388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1461388e9c81SLen Brown 	}
1462388e9c81SLen Brown 
1463c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1464c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1465c98d5d94SLen Brown 		return 0;
1466c98d5d94SLen Brown 
1467c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1468c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1469c98d5d94SLen Brown 	average.cores.c7 += c->c7;
14700539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1471c98d5d94SLen Brown 
1472889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1473889facbeSLen Brown 
1474388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1475388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1476388e9c81SLen Brown 			continue;
1477388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1478388e9c81SLen Brown 	}
1479388e9c81SLen Brown 
1480c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1481c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1482c98d5d94SLen Brown 		return 0;
1483c98d5d94SLen Brown 
1484a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
14850b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1486a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
14870b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1488a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
14890b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1490a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
14910b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
14920b2bb692SLen Brown 
1493c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
14940f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1495c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
14960f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1497c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
14980f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1499c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1500ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1501ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1502ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1503c98d5d94SLen Brown 
1504be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1505be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1506be0e54c4SLen Brown 
1507889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1508889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1509889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1510889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1511889facbeSLen Brown 
1512fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
151327d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
151427d47356SLen Brown 
1515889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1516889facbeSLen Brown 
1517889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1518889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1519388e9c81SLen Brown 
1520388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1521388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1522388e9c81SLen Brown 			continue;
1523388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1524388e9c81SLen Brown 	}
1525c98d5d94SLen Brown 	return 0;
1526c98d5d94SLen Brown }
1527c98d5d94SLen Brown /*
1528c98d5d94SLen Brown  * sum the counters for all cpus in the system
1529c98d5d94SLen Brown  * compute the weighted average
1530c98d5d94SLen Brown  */
1531c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1532c98d5d94SLen Brown 	struct pkg_data *p)
1533c98d5d94SLen Brown {
1534388e9c81SLen Brown 	int i;
1535388e9c81SLen Brown 	struct msr_counter *mp;
1536388e9c81SLen Brown 
1537c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1538c98d5d94SLen Brown 
1539c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1540c98d5d94SLen Brown 
1541c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1542c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1543c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1544c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1545c98d5d94SLen Brown 
15460de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
15470de6c0dfSLen Brown 		sums_need_wide_columns = 1;
15480de6c0dfSLen Brown 
1549c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1550c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1551c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
15520539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1553c98d5d94SLen Brown 
1554a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
15550b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1556a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
15570b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1558a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
15590b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1560a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
15610b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
15620b2bb692SLen Brown 
1563c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
15640f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1565c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
15660f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1567c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
15680f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1569c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1570ca58710fSKristen Carlson Accardi 
1571ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1572ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1573ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1574388e9c81SLen Brown 
1575388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1576388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1577388e9c81SLen Brown 			continue;
15780de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
15790de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
15800de6c0dfSLen Brown 				sums_need_wide_columns = 1;
158141618e63SLen Brown 			continue;
15820de6c0dfSLen Brown 		}
1583388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1584388e9c81SLen Brown 	}
1585388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1586388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1587388e9c81SLen Brown 			continue;
15880de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
15890de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
15900de6c0dfSLen Brown 				sums_need_wide_columns = 1;
15910de6c0dfSLen Brown 		}
1592388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1593388e9c81SLen Brown 	}
1594388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1595388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1596388e9c81SLen Brown 			continue;
15970de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
15980de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
15990de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16000de6c0dfSLen Brown 		}
1601388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1602388e9c81SLen Brown 	}
1603c98d5d94SLen Brown }
1604c98d5d94SLen Brown 
1605c98d5d94SLen Brown static unsigned long long rdtsc(void)
1606c98d5d94SLen Brown {
1607c98d5d94SLen Brown 	unsigned int low, high;
1608c98d5d94SLen Brown 
1609c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1610c98d5d94SLen Brown 
1611c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1612c98d5d94SLen Brown }
1613c98d5d94SLen Brown 
1614c98d5d94SLen Brown /*
1615495c7654SLen Brown  * Open a file, and exit on failure
1616495c7654SLen Brown  */
1617495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1618495c7654SLen Brown {
1619495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1620495c7654SLen Brown 
1621495c7654SLen Brown 	if (!filep)
1622495c7654SLen Brown 		err(1, "%s: open failed", path);
1623495c7654SLen Brown 	return filep;
1624495c7654SLen Brown }
1625495c7654SLen Brown /*
1626495c7654SLen Brown  * snapshot_sysfs_counter()
1627495c7654SLen Brown  *
1628495c7654SLen Brown  * return snapshot of given counter
1629495c7654SLen Brown  */
1630495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1631495c7654SLen Brown {
1632495c7654SLen Brown 	FILE *fp;
1633495c7654SLen Brown 	int retval;
1634495c7654SLen Brown 	unsigned long long counter;
1635495c7654SLen Brown 
1636495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1637495c7654SLen Brown 
1638495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1639495c7654SLen Brown 	if (retval != 1)
1640495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1641495c7654SLen Brown 
1642495c7654SLen Brown 	fclose(fp);
1643495c7654SLen Brown 
1644495c7654SLen Brown 	return counter;
1645495c7654SLen Brown }
1646495c7654SLen Brown 
1647495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1648495c7654SLen Brown {
1649495c7654SLen Brown 	if (mp->msr_num != 0) {
1650495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1651495c7654SLen Brown 			return -1;
1652495c7654SLen Brown 	} else {
165346c27978SLen Brown 		char path[128 + PATH_BYTES];
165441618e63SLen Brown 
165541618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
165641618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
165741618e63SLen Brown 				 cpu, mp->path);
165841618e63SLen Brown 
165941618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
166041618e63SLen Brown 		} else {
1661495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1662495c7654SLen Brown 		}
166341618e63SLen Brown 	}
1664495c7654SLen Brown 
1665495c7654SLen Brown 	return 0;
1666495c7654SLen Brown }
1667495c7654SLen Brown 
16684c2122d4SLen Brown void get_apic_id(struct thread_data *t)
16694c2122d4SLen Brown {
16704c2122d4SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level;
16714c2122d4SLen Brown 
16724c2122d4SLen Brown 	eax = ebx = ecx = edx = 0;
16734c2122d4SLen Brown 
16744c2122d4SLen Brown 	if (!genuine_intel)
16754c2122d4SLen Brown 		return;
16764c2122d4SLen Brown 
16774c2122d4SLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
16784c2122d4SLen Brown 
16794c2122d4SLen Brown 	__cpuid(1, eax, ebx, ecx, edx);
16804c2122d4SLen Brown 	t->apic_id = (ebx >> 24) & 0xf;
16814c2122d4SLen Brown 
16824c2122d4SLen Brown 	if (max_level < 0xb)
16834c2122d4SLen Brown 		return;
16844c2122d4SLen Brown 
16854c2122d4SLen Brown 	if (!DO_BIC(BIC_X2APIC))
16864c2122d4SLen Brown 		return;
16874c2122d4SLen Brown 
16884c2122d4SLen Brown 	ecx = 0;
16894c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
16904c2122d4SLen Brown 	t->x2apic_id = edx;
16914c2122d4SLen Brown 
16924c2122d4SLen Brown 	if (debug && (t->apic_id != t->x2apic_id))
16934c2122d4SLen Brown 		fprintf(stderr, "cpu%d: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id);
16944c2122d4SLen Brown }
16954c2122d4SLen Brown 
1696495c7654SLen Brown /*
1697c98d5d94SLen Brown  * get_counters(...)
1698c98d5d94SLen Brown  * migrate to cpu
1699c98d5d94SLen Brown  * acquire and record local counters for that cpu
1700c98d5d94SLen Brown  */
1701c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1702c98d5d94SLen Brown {
1703c98d5d94SLen Brown 	int cpu = t->cpu_id;
1704889facbeSLen Brown 	unsigned long long msr;
17050102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1706388e9c81SLen Brown 	struct msr_counter *mp;
1707388e9c81SLen Brown 	int i;
1708c98d5d94SLen Brown 
1709f4fdf2b4SLen Brown 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1710f4fdf2b4SLen Brown 
1711e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1712b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
171315aaa346SLen Brown 		return -1;
1714e52966c0SLen Brown 	}
171515aaa346SLen Brown 
17164c2122d4SLen Brown 	if (first_counter_read)
17174c2122d4SLen Brown 		get_apic_id(t);
17180102b067SLen Brown retry:
1719c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
172015aaa346SLen Brown 
1721812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
17220102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
17230102b067SLen Brown 
17240102b067SLen Brown 		/*
17250102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
17260102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
17270102b067SLen Brown 		 *
17280102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
17290102b067SLen Brown 		 * individual system call, so delays may occur
17300102b067SLen Brown 		 * between them.  If the time to read them
17310102b067SLen Brown 		 * varies by a large amount, we re-read them.
17320102b067SLen Brown 		 */
17330102b067SLen Brown 
17340102b067SLen Brown 		/*
17350102b067SLen Brown 		 * This initial dummy APERF read has been seen to
17360102b067SLen Brown 		 * reduce jitter in the subsequent reads.
17370102b067SLen Brown 		 */
17380102b067SLen Brown 
17399c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1740c98d5d94SLen Brown 			return -3;
17410102b067SLen Brown 
17420102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
17430102b067SLen Brown 
17440102b067SLen Brown 		tsc_before = t->tsc;
17450102b067SLen Brown 
17460102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
17470102b067SLen Brown 			return -3;
17480102b067SLen Brown 
17490102b067SLen Brown 		tsc_between = rdtsc();
17500102b067SLen Brown 
17519c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1752c98d5d94SLen Brown 			return -4;
17530102b067SLen Brown 
17540102b067SLen Brown 		tsc_after = rdtsc();
17550102b067SLen Brown 
17560102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
17570102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
17580102b067SLen Brown 
17590102b067SLen Brown 		/*
17600102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
17610102b067SLen Brown 		 * differ by more than 2x, then try again.
17620102b067SLen Brown 		 */
17630102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
17640102b067SLen Brown 			aperf_mperf_retry_count++;
17650102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
17660102b067SLen Brown 				goto retry;
17670102b067SLen Brown 			else
17680102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
17690102b067SLen Brown 					cpu, aperf_time, mperf_time);
17700102b067SLen Brown 		}
17710102b067SLen Brown 		aperf_mperf_retry_count = 0;
17720102b067SLen Brown 
1773b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1774b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
177588c3281fSLen Brown 	}
177688c3281fSLen Brown 
1777812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1778562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1779812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
17801ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
17811ed51011SLen Brown 			return -5;
17821ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
17831ed51011SLen Brown 	}
17840539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1785144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1786144b44b1SLen Brown 			return -6;
1787144b44b1SLen Brown 	}
1788144b44b1SLen Brown 
1789388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1790495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1791388e9c81SLen Brown 			return -10;
1792388e9c81SLen Brown 	}
1793388e9c81SLen Brown 
1794c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1795c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1796f4fdf2b4SLen Brown 		goto done;
1797c98d5d94SLen Brown 
1798997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
1799c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1800c98d5d94SLen Brown 			return -6;
1801144b44b1SLen Brown 	}
1802144b44b1SLen Brown 
1803812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1804c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1805c98d5d94SLen Brown 			return -7;
1806fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1807fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1808fb5d4327SDasaratharaman Chandramouli 			return -7;
1809103a8feaSLen Brown 	}
181015aaa346SLen Brown 
1811812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1812c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1813c98d5d94SLen Brown 			return -8;
1814c98d5d94SLen Brown 
18150539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
18160539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
18170539ba11SLen Brown 			return -8;
18180539ba11SLen Brown 
1819812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1820889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1821889facbeSLen Brown 			return -9;
1822889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1823889facbeSLen Brown 	}
1824889facbeSLen Brown 
1825388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1826495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1827388e9c81SLen Brown 			return -10;
1828388e9c81SLen Brown 	}
1829889facbeSLen Brown 
1830c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1831c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1832f4fdf2b4SLen Brown 		goto done;
183315aaa346SLen Brown 
1834a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
18350b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
18360b2bb692SLen Brown 			return -10;
1837a99d8730SLen Brown 	}
1838a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
18390b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
18400b2bb692SLen Brown 			return -11;
1841a99d8730SLen Brown 	}
1842a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
18430b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
18440b2bb692SLen Brown 			return -12;
1845a99d8730SLen Brown 	}
1846a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
18470b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
18480b2bb692SLen Brown 			return -13;
18490b2bb692SLen Brown 	}
18500f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1851c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1852c98d5d94SLen Brown 			return -9;
18530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
18540539ba11SLen Brown 		if (do_slm_cstates) {
18550539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
18560539ba11SLen Brown 				return -10;
18570539ba11SLen Brown 		} else {
1858c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1859c98d5d94SLen Brown 				return -10;
18600539ba11SLen Brown 		}
18610539ba11SLen Brown 	}
18620539ba11SLen Brown 
18630f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1864c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1865c98d5d94SLen Brown 			return -11;
18660f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1867c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1868c98d5d94SLen Brown 			return -12;
18690f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1870ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1871ca58710fSKristen Carlson Accardi 			return -13;
18720f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1873ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1874ca58710fSKristen Carlson Accardi 			return -13;
18750f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1876ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1877ca58710fSKristen Carlson Accardi 			return -13;
18780f47c08dSLen Brown 
1879be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1880be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1881be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1882be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
1883be0e54c4SLen Brown 
1884889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1885889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1886889facbeSLen Brown 			return -13;
1887889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1888889facbeSLen Brown 	}
18899148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1890889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1891889facbeSLen Brown 			return -14;
1892889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1893889facbeSLen Brown 	}
1894889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1895889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1896889facbeSLen Brown 			return -15;
1897889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1898889facbeSLen Brown 	}
1899889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1900889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1901889facbeSLen Brown 			return -16;
1902889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1903889facbeSLen Brown 	}
1904889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1905889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1906889facbeSLen Brown 			return -16;
1907889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1908889facbeSLen Brown 	}
1909889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1910889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1911889facbeSLen Brown 			return -16;
1912889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1913889facbeSLen Brown 	}
1914812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1915889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1916889facbeSLen Brown 			return -17;
1917889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1918889facbeSLen Brown 	}
1919fdf676e5SLen Brown 
1920812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1921fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1922fdf676e5SLen Brown 
1923812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
192427d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
192527d47356SLen Brown 
1926388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1927495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
1928388e9c81SLen Brown 			return -10;
1929388e9c81SLen Brown 	}
1930f4fdf2b4SLen Brown done:
1931f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
1932388e9c81SLen Brown 
193315aaa346SLen Brown 	return 0;
1934103a8feaSLen Brown }
1935103a8feaSLen Brown 
1936ee7e38e3SLen Brown /*
1937ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1938ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1939ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1940ee7e38e3SLen Brown  */
1941ee7e38e3SLen Brown 
1942ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1943ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1944ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1945ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1946ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1947ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1948ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1949ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1950ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1951ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1952ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1953ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
19540b2bb692SLen Brown #define PCL__8 12 /* PC8 */
19550b2bb692SLen Brown #define PCL__9 13 /* PC9 */
19560b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1957ee7e38e3SLen Brown 
1958ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1959ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
19600b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1961ee7e38e3SLen Brown 
1962e9257f5fSLen 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};
1963e9257f5fSLen 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};
1964e9257f5fSLen 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};
19650539ba11SLen 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};
1966f2642888SLen 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};
1967e9257f5fSLen 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};
1968e4085d54SLen 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};
19692085e124SArtem 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};
1970ee7e38e3SLen Brown 
1971a2b7b749SLen Brown 
1972a2b7b749SLen Brown static void
1973a2b7b749SLen Brown calculate_tsc_tweak()
1974a2b7b749SLen Brown {
1975a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1976a2b7b749SLen Brown }
1977a2b7b749SLen Brown 
1978fcd17211SLen Brown static void
1979fcd17211SLen Brown dump_nhm_platform_info(void)
1980103a8feaSLen Brown {
1981103a8feaSLen Brown 	unsigned long long msr;
1982103a8feaSLen Brown 	unsigned int ratio;
1983103a8feaSLen Brown 
1984ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1985103a8feaSLen Brown 
1986b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
19876574a5d5SLen Brown 
1988103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1989710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
1990103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1991103a8feaSLen Brown 
1992103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1993710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1994103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1995103a8feaSLen Brown 
19967ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1997b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1998bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
199967920418SLen Brown 
2000fcd17211SLen Brown 	return;
2001fcd17211SLen Brown }
2002fcd17211SLen Brown 
2003fcd17211SLen Brown static void
2004fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
2005fcd17211SLen Brown {
2006fcd17211SLen Brown 	unsigned long long msr;
2007fcd17211SLen Brown 	unsigned int ratio;
2008fcd17211SLen Brown 
20097ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2010fcd17211SLen Brown 
2011b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2012fcd17211SLen Brown 
2013fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
2014fcd17211SLen Brown 	if (ratio)
2015710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2016fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2017fcd17211SLen Brown 
2018fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2019fcd17211SLen Brown 	if (ratio)
2020710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2021fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2022fcd17211SLen Brown 	return;
2023fcd17211SLen Brown }
2024fcd17211SLen Brown 
2025fcd17211SLen Brown static void
2026fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
2027fcd17211SLen Brown {
2028fcd17211SLen Brown 	unsigned long long msr;
2029fcd17211SLen Brown 	unsigned int ratio;
20306574a5d5SLen Brown 
20317ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
20326574a5d5SLen Brown 
2033b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
20346574a5d5SLen Brown 
20356574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
20366574a5d5SLen Brown 	if (ratio)
2037710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
20386574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20396574a5d5SLen Brown 
20406574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
20416574a5d5SLen Brown 	if (ratio)
2042710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
20436574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20446574a5d5SLen Brown 
20456574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
20466574a5d5SLen Brown 	if (ratio)
2047710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
20486574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20496574a5d5SLen Brown 
20506574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
20516574a5d5SLen Brown 	if (ratio)
2052710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
20536574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20546574a5d5SLen Brown 
20556574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
20566574a5d5SLen Brown 	if (ratio)
2057710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
20586574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20596574a5d5SLen Brown 
20606574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
20616574a5d5SLen Brown 	if (ratio)
2062710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
20636574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20646574a5d5SLen Brown 
20656574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
20666574a5d5SLen Brown 	if (ratio)
2067710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
20686574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20696574a5d5SLen Brown 
20706574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
20716574a5d5SLen Brown 	if (ratio)
2072710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
20736574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
2074103a8feaSLen Brown 	return;
2075fcd17211SLen Brown }
207631e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
207731e07522SLen Brown {
207831e07522SLen Brown 
207931e07522SLen Brown 	if (!genuine_intel)
208031e07522SLen Brown 		return 0;
208131e07522SLen Brown 
208231e07522SLen Brown 	switch (model) {
208331e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
208431e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
208531e07522SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
208631e07522SLen Brown 		return 1;
208731e07522SLen Brown 	}
208831e07522SLen Brown 	return 0;
208931e07522SLen Brown }
2090fcd17211SLen Brown 
2091fcd17211SLen Brown static void
209231e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
2093fcd17211SLen Brown {
209431e07522SLen Brown 	unsigned long long msr, core_counts;
209531e07522SLen Brown 	unsigned int ratio, group_size;
2096103a8feaSLen Brown 
20977ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2098b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
20996574a5d5SLen Brown 
210031e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
210131e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
210231e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
210331e07522SLen Brown 	} else {
210431e07522SLen Brown 		core_counts = 0x0807060504030201;
210531e07522SLen Brown 	}
210631e07522SLen Brown 
21076574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
210831e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
21096574a5d5SLen Brown 	if (ratio)
211031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
211131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21126574a5d5SLen Brown 
21136574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
211431e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
21156574a5d5SLen Brown 	if (ratio)
211631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
211731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21186574a5d5SLen Brown 
21196574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
212031e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
21216574a5d5SLen Brown 	if (ratio)
212231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
212331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21246574a5d5SLen Brown 
21256574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
212631e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
21276574a5d5SLen Brown 	if (ratio)
212831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
212931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21306574a5d5SLen Brown 
2131103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
213231e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
2133103a8feaSLen Brown 	if (ratio)
213431e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
213531e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2136103a8feaSLen Brown 
2137103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
213831e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
2139103a8feaSLen Brown 	if (ratio)
214031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
214131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2142103a8feaSLen Brown 
2143103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
214431e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
2145103a8feaSLen Brown 	if (ratio)
214631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
214731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2148103a8feaSLen Brown 
2149103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
215031e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
2151103a8feaSLen Brown 	if (ratio)
215231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
215331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2154fcd17211SLen Brown 	return;
2155fcd17211SLen Brown }
21563a9a941dSLen Brown 
2157fcd17211SLen Brown static void
21580f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
21590f7887c4SLen Brown {
21600f7887c4SLen Brown 	unsigned long long msr;
21610f7887c4SLen Brown 	unsigned int ratio;
21620f7887c4SLen Brown 
21630f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
21640f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
21650f7887c4SLen Brown 
21660f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
21670f7887c4SLen Brown 	if (ratio)
21680f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
21690f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21700f7887c4SLen Brown 
21710f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
21720f7887c4SLen Brown 	if (ratio)
21730f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
21740f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21750f7887c4SLen Brown 
21760f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
21770f7887c4SLen Brown 	if (ratio)
21780f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
21790f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21800f7887c4SLen Brown 
21810f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
21820f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
21830f7887c4SLen Brown 
21840f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
21850f7887c4SLen Brown 	if (ratio)
21860f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
21870f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21880f7887c4SLen Brown 
21890f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
21900f7887c4SLen Brown 	if (ratio)
21910f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
21920f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21930f7887c4SLen Brown 
21940f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
21950f7887c4SLen Brown 	if (ratio)
21960f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
21970f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21980f7887c4SLen Brown 
21990f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22000f7887c4SLen Brown 	if (ratio)
22010f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
22020f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22030f7887c4SLen Brown }
22040f7887c4SLen Brown 
22050f7887c4SLen Brown static void
2206fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2207fb5d4327SDasaratharaman Chandramouli {
2208cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2209cbf97abaSHubert Chrzaniuk 
2210fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2211cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2212cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2213cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2214cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2215fb5d4327SDasaratharaman Chandramouli 
2216ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2217fb5d4327SDasaratharaman Chandramouli 
2218b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2219bfae2052SLen Brown 		base_cpu, msr);
2220fb5d4327SDasaratharaman Chandramouli 
2221fb5d4327SDasaratharaman Chandramouli 	/**
2222fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2223cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2224cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2225fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2226fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2227fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2228fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2229fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2230fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2231fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2232fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2233fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2234fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2235fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2236fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2237fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2238fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2239fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2240fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2241fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2242fb5d4327SDasaratharaman Chandramouli 	 */
2243fb5d4327SDasaratharaman Chandramouli 
2244cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2245cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2246cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2247cbf97abaSHubert Chrzaniuk 
2248cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2249fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2250cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2251fb5d4327SDasaratharaman Chandramouli 
2252cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2253cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2254cbf97abaSHubert Chrzaniuk 		b_nr++;
2255fb5d4327SDasaratharaman Chandramouli 	}
2256cbf97abaSHubert Chrzaniuk 
2257cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2258cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2259b7d8c148SLen Brown 			fprintf(outf,
2260710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2261cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2262fb5d4327SDasaratharaman Chandramouli }
2263fb5d4327SDasaratharaman Chandramouli 
2264fb5d4327SDasaratharaman Chandramouli static void
2265fcd17211SLen Brown dump_nhm_cst_cfg(void)
2266fcd17211SLen Brown {
2267fcd17211SLen Brown 	unsigned long long msr;
2268fcd17211SLen Brown 
22691df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2270fcd17211SLen Brown 
22711df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2272fcd17211SLen Brown 
22733e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2274fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2275fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2276fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2277fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2278fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
22796c34f160SLen Brown 		(unsigned int)msr & 0xF,
2280fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2281ac980e13SArtem Bityutskiy 
2282ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2283ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
2284ac980e13SArtem Bityutskiy 		fprintf(outf, ", automatic c-state conversion=%s",
2285ac980e13SArtem Bityutskiy 			(msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2286ac980e13SArtem Bityutskiy 	}
2287ac980e13SArtem Bityutskiy 
2288ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2289ac980e13SArtem Bityutskiy 
2290fcd17211SLen Brown 	return;
2291103a8feaSLen Brown }
2292103a8feaSLen Brown 
22936fb3143bSLen Brown static void
22946fb3143bSLen Brown dump_config_tdp(void)
22956fb3143bSLen Brown {
22966fb3143bSLen Brown 	unsigned long long msr;
22976fb3143bSLen Brown 
22986fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2299b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2300685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
23016fb3143bSLen Brown 
23026fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2303b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
23046fb3143bSLen Brown 	if (msr) {
2305685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2306685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2307685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2308685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
23096fb3143bSLen Brown 	}
2310b7d8c148SLen Brown 	fprintf(outf, ")\n");
23116fb3143bSLen Brown 
23126fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2313b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
23146fb3143bSLen Brown 	if (msr) {
2315685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2316685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2317685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2318685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
23196fb3143bSLen Brown 	}
2320b7d8c148SLen Brown 	fprintf(outf, ")\n");
23216fb3143bSLen Brown 
23226fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2323b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
23246fb3143bSLen Brown 	if ((msr) & 0x3)
2325b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2326b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2327b7d8c148SLen Brown 	fprintf(outf, ")\n");
23286fb3143bSLen Brown 
23296fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2330b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2331685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2332b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2333b7d8c148SLen Brown 	fprintf(outf, ")\n");
23346fb3143bSLen Brown }
23355a63426eSLen Brown 
23365a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
23375a63426eSLen Brown 
23385a63426eSLen Brown void print_irtl(void)
23395a63426eSLen Brown {
23405a63426eSLen Brown 	unsigned long long msr;
23415a63426eSLen Brown 
23425a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
23435a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
23445a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23455a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23465a63426eSLen Brown 
23475a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
23485a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
23495a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23505a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23515a63426eSLen Brown 
23525a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
23535a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
23545a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23555a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23565a63426eSLen Brown 
23575a63426eSLen Brown 	if (!do_irtl_hsw)
23585a63426eSLen Brown 		return;
23595a63426eSLen Brown 
23605a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
23615a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
23625a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23635a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23645a63426eSLen Brown 
23655a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
23665a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
23675a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23685a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23695a63426eSLen Brown 
23705a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
23715a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
23725a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23735a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23745a63426eSLen Brown 
23755a63426eSLen Brown }
237636229897SLen Brown void free_fd_percpu(void)
237736229897SLen Brown {
237836229897SLen Brown 	int i;
237936229897SLen Brown 
238001a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
238136229897SLen Brown 		if (fd_percpu[i] != 0)
238236229897SLen Brown 			close(fd_percpu[i]);
238336229897SLen Brown 	}
238436229897SLen Brown 
238536229897SLen Brown 	free(fd_percpu);
23866fb3143bSLen Brown }
23876fb3143bSLen Brown 
2388c98d5d94SLen Brown void free_all_buffers(void)
2389103a8feaSLen Brown {
23900e2d8f05SLen Brown 	int i;
23910e2d8f05SLen Brown 
2392c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2393c98d5d94SLen Brown 	cpu_present_set = NULL;
239436229897SLen Brown 	cpu_present_setsize = 0;
2395103a8feaSLen Brown 
2396c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2397c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2398c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2399103a8feaSLen Brown 
2400c98d5d94SLen Brown 	free(thread_even);
2401c98d5d94SLen Brown 	free(core_even);
2402c98d5d94SLen Brown 	free(package_even);
2403103a8feaSLen Brown 
2404c98d5d94SLen Brown 	thread_even = NULL;
2405c98d5d94SLen Brown 	core_even = NULL;
2406c98d5d94SLen Brown 	package_even = NULL;
2407103a8feaSLen Brown 
2408c98d5d94SLen Brown 	free(thread_odd);
2409c98d5d94SLen Brown 	free(core_odd);
2410c98d5d94SLen Brown 	free(package_odd);
2411103a8feaSLen Brown 
2412c98d5d94SLen Brown 	thread_odd = NULL;
2413c98d5d94SLen Brown 	core_odd = NULL;
2414c98d5d94SLen Brown 	package_odd = NULL;
2415103a8feaSLen Brown 
2416c98d5d94SLen Brown 	free(output_buffer);
2417c98d5d94SLen Brown 	output_buffer = NULL;
2418c98d5d94SLen Brown 	outp = NULL;
241936229897SLen Brown 
242036229897SLen Brown 	free_fd_percpu();
2421562a2d37SLen Brown 
2422562a2d37SLen Brown 	free(irq_column_2_cpu);
2423562a2d37SLen Brown 	free(irqs_per_cpu);
24240e2d8f05SLen Brown 
24250e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
24260e2d8f05SLen Brown 		if (cpus[i].put_ids)
24270e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
24280e2d8f05SLen Brown 	}
24290e2d8f05SLen Brown 	free(cpus);
2430103a8feaSLen Brown }
2431103a8feaSLen Brown 
243257a42a34SJosh Triplett 
243357a42a34SJosh Triplett /*
243495aebc44SJosh Triplett  * Parse a file containing a single int.
243595aebc44SJosh Triplett  */
243695aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
243795aebc44SJosh Triplett {
243895aebc44SJosh Triplett 	va_list args;
243995aebc44SJosh Triplett 	char path[PATH_MAX];
244095aebc44SJosh Triplett 	FILE *filep;
244195aebc44SJosh Triplett 	int value;
244295aebc44SJosh Triplett 
244395aebc44SJosh Triplett 	va_start(args, fmt);
244495aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
244595aebc44SJosh Triplett 	va_end(args);
244657a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2447b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2448b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
244995aebc44SJosh Triplett 	fclose(filep);
245095aebc44SJosh Triplett 	return value;
245195aebc44SJosh Triplett }
245295aebc44SJosh Triplett 
245395aebc44SJosh Triplett /*
2454c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2455c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2456c98d5d94SLen Brown  */
2457c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2458c98d5d94SLen Brown {
245995aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2460103a8feaSLen Brown }
2461103a8feaSLen Brown 
2462103a8feaSLen Brown int get_physical_package_id(int cpu)
2463103a8feaSLen Brown {
246495aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2465103a8feaSLen Brown }
2466103a8feaSLen Brown 
2467103a8feaSLen Brown int get_core_id(int cpu)
2468103a8feaSLen Brown {
246995aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2470103a8feaSLen Brown }
2471103a8feaSLen Brown 
2472ef605741SPrarit Bhargava void set_node_data(void)
2473ef605741SPrarit Bhargava {
2474ef605741SPrarit Bhargava 	char path[80];
2475ef605741SPrarit Bhargava 	FILE *filep;
2476ef605741SPrarit Bhargava 	int pkg, node, cpu;
2477ef605741SPrarit Bhargava 
2478ef605741SPrarit Bhargava 	struct pkg_node_info {
2479ef605741SPrarit Bhargava 		int count;
2480ef605741SPrarit Bhargava 		int min;
2481ef605741SPrarit Bhargava 	} *pni;
2482ef605741SPrarit Bhargava 
2483ef605741SPrarit Bhargava 	pni = calloc(topo.num_packages, sizeof(struct pkg_node_info));
2484ef605741SPrarit Bhargava 	if (!pni)
2485ef605741SPrarit Bhargava 		err(1, "calloc pkg_node_count");
2486ef605741SPrarit Bhargava 
2487ef605741SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++)
2488ef605741SPrarit Bhargava 		pni[pkg].min = topo.num_cpus;
2489ef605741SPrarit Bhargava 
2490ef605741SPrarit Bhargava 	for (node = 0; node <= topo.max_node_num; node++) {
2491ef605741SPrarit Bhargava 		/* find the "first" cpu in the node */
2492ef605741SPrarit Bhargava 		sprintf(path, "/sys/bus/node/devices/node%d/cpulist", node);
2493ef605741SPrarit Bhargava 		filep = fopen(path, "r");
2494ef605741SPrarit Bhargava 		if (!filep)
2495ef605741SPrarit Bhargava 			continue;
2496ef605741SPrarit Bhargava 		fscanf(filep, "%d", &cpu);
2497ef605741SPrarit Bhargava 		fclose(filep);
2498ef605741SPrarit Bhargava 
2499ef605741SPrarit Bhargava 		pkg = cpus[cpu].physical_package_id;
2500ef605741SPrarit Bhargava 		pni[pkg].count++;
2501ef605741SPrarit Bhargava 
2502ef605741SPrarit Bhargava 		if (node < pni[pkg].min)
2503ef605741SPrarit Bhargava 			pni[pkg].min = node;
2504ef605741SPrarit Bhargava 	}
2505ef605741SPrarit Bhargava 
2506ef605741SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++)
250770a9c6e8SPrarit Bhargava 		if (pni[pkg].count > topo.nodes_per_pkg)
250870a9c6e8SPrarit Bhargava 			topo.nodes_per_pkg = pni[0].count;
2509ef605741SPrarit Bhargava 
251042dd4520SNathan Ciobanu 	/* Fake 1 node per pkg for machines that don't
251142dd4520SNathan Ciobanu 	 * expose nodes and thus avoid -nan results
251242dd4520SNathan Ciobanu 	 */
251342dd4520SNathan Ciobanu 	if (topo.nodes_per_pkg == 0)
251442dd4520SNathan Ciobanu 		topo.nodes_per_pkg = 1;
251542dd4520SNathan Ciobanu 
2516ef605741SPrarit Bhargava 	for (cpu = 0; cpu < topo.num_cpus; cpu++) {
2517ef605741SPrarit Bhargava 		pkg = cpus[cpu].physical_package_id;
2518ef605741SPrarit Bhargava 		node = cpus[cpu].physical_node_id;
2519ef605741SPrarit Bhargava 		cpus[cpu].logical_node_id = node - pni[pkg].min;
2520ef605741SPrarit Bhargava 	}
2521ef605741SPrarit Bhargava 	free(pni);
2522ef605741SPrarit Bhargava 
2523ef605741SPrarit Bhargava }
2524ef605741SPrarit Bhargava 
2525ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2526c98d5d94SLen Brown {
2527c98d5d94SLen Brown 	char path[80];
2528c98d5d94SLen Brown 	FILE *filep;
25290e2d8f05SLen Brown 	int i;
25300e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2531c98d5d94SLen Brown 
25320e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
25330e2d8f05SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
25340e2d8f05SLen Brown 			cpu, i);
25350e2d8f05SLen Brown 		filep = fopen(path, "r");
25360e2d8f05SLen Brown 		if (!filep)
25370e2d8f05SLen Brown 			continue;
25380e2d8f05SLen Brown 		fclose(filep);
25390e2d8f05SLen Brown 		return i;
25400e2d8f05SLen Brown 	}
25410e2d8f05SLen Brown 	return -1;
2542e275b388SDasaratharaman Chandramouli }
2543c98d5d94SLen Brown 
25440e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
25450e2d8f05SLen Brown {
25460e2d8f05SLen Brown 	char path[80], character;
25470e2d8f05SLen Brown 	FILE *filep;
25480e2d8f05SLen Brown 	unsigned long map;
25498cb48b32SPrarit Bhargava 	int so, shift, sib_core;
25500e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
25510e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
25520e2d8f05SLen Brown 	size_t size;
25538cb48b32SPrarit Bhargava 	int thread_id = 0;
25540e2d8f05SLen Brown 
25550e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
25568cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
25578cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
25580e2d8f05SLen Brown 	if (!thiscpu->put_ids)
25590e2d8f05SLen Brown 		return -1;
25600e2d8f05SLen Brown 
25610e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
25620e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
25630e2d8f05SLen Brown 
25640e2d8f05SLen Brown 	sprintf(path,
25650e2d8f05SLen Brown 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
25660e2d8f05SLen Brown 	filep = fopen_or_die(path, "r");
25670e2d8f05SLen Brown 	do {
25680e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
25690e2d8f05SLen Brown 		fscanf(filep, "%lx%c", &map, &character);
25700e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
25710e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
25728cb48b32SPrarit Bhargava 				so = shift + offset;
25738cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
25748cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
25758cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
25768cb48b32SPrarit Bhargava 					if ((so != cpu) &&
25778cb48b32SPrarit Bhargava 					    (cpus[so].thread_id < 0))
25788cb48b32SPrarit Bhargava 						cpus[so].thread_id =
25798cb48b32SPrarit Bhargava 								    thread_id++;
25808cb48b32SPrarit Bhargava 				}
25810e2d8f05SLen Brown 			}
25820e2d8f05SLen Brown 		}
25830e2d8f05SLen Brown 	} while (!strncmp(&character, ",", 1));
2584c98d5d94SLen Brown 	fclose(filep);
25850e2d8f05SLen Brown 
25860e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2587c98d5d94SLen Brown }
2588c98d5d94SLen Brown 
2589c98d5d94SLen Brown /*
2590c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2591c98d5d94SLen Brown  * skip non-present cpus
2592103a8feaSLen Brown  */
2593103a8feaSLen Brown 
2594c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2595c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2596c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2597c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2598c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2599c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2600c98d5d94SLen Brown {
260140f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2602c98d5d94SLen Brown 
2603c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
260440f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
260540f5cfe7SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node;
260640f5cfe7SPrarit Bhargava 			     ++core_no) {
2607c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
260870a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
2609c98d5d94SLen Brown 					struct thread_data *t, *t2;
2610c98d5d94SLen Brown 					struct core_data *c, *c2;
2611c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2612c98d5d94SLen Brown 
261340f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
261440f5cfe7SPrarit Bhargava 						       core_no, node_no,
261540f5cfe7SPrarit Bhargava 						       pkg_no);
2616c98d5d94SLen Brown 
2617c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
2618c98d5d94SLen Brown 						continue;
2619c98d5d94SLen Brown 
262040f5cfe7SPrarit Bhargava 					t2 = GET_THREAD(thread_base2, thread_no,
262140f5cfe7SPrarit Bhargava 							core_no, node_no,
262240f5cfe7SPrarit Bhargava 							pkg_no);
2623c98d5d94SLen Brown 
262440f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
262540f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
262640f5cfe7SPrarit Bhargava 					c2 = GET_CORE(core_base2, core_no,
262740f5cfe7SPrarit Bhargava 						      node_no,
262840f5cfe7SPrarit Bhargava 						      pkg_no);
2629c98d5d94SLen Brown 
2630c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
2631c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
2632c98d5d94SLen Brown 
2633c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
2634c98d5d94SLen Brown 					if (retval)
2635c98d5d94SLen Brown 						return retval;
2636c98d5d94SLen Brown 				}
2637c98d5d94SLen Brown 			}
2638c98d5d94SLen Brown 		}
263940f5cfe7SPrarit Bhargava 	}
2640c98d5d94SLen Brown 	return 0;
2641c98d5d94SLen Brown }
2642c98d5d94SLen Brown 
2643c98d5d94SLen Brown /*
2644c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2645c98d5d94SLen Brown  * return max_cpu number
2646c98d5d94SLen Brown  */
2647c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2648103a8feaSLen Brown {
2649103a8feaSLen Brown 	FILE *fp;
2650c98d5d94SLen Brown 	int cpu_num;
2651103a8feaSLen Brown 	int retval;
2652103a8feaSLen Brown 
265357a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2654103a8feaSLen Brown 
2655103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2656b2c95d90SJosh Triplett 	if (retval != 0)
2657b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2658103a8feaSLen Brown 
2659c98d5d94SLen Brown 	while (1) {
2660c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2661103a8feaSLen Brown 		if (retval != 1)
2662103a8feaSLen Brown 			break;
2663103a8feaSLen Brown 
2664c98d5d94SLen Brown 		retval = func(cpu_num);
2665c98d5d94SLen Brown 		if (retval) {
2666c98d5d94SLen Brown 			fclose(fp);
2667c98d5d94SLen Brown 			return(retval);
2668c98d5d94SLen Brown 		}
2669103a8feaSLen Brown 	}
2670103a8feaSLen Brown 	fclose(fp);
2671c98d5d94SLen Brown 	return 0;
2672103a8feaSLen Brown }
2673103a8feaSLen Brown 
2674103a8feaSLen Brown void re_initialize(void)
2675103a8feaSLen Brown {
2676c98d5d94SLen Brown 	free_all_buffers();
2677c98d5d94SLen Brown 	setup_all_buffers();
2678c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2679103a8feaSLen Brown }
2680103a8feaSLen Brown 
2681843c5791SPrarit Bhargava void set_max_cpu_num(void)
2682843c5791SPrarit Bhargava {
2683843c5791SPrarit Bhargava 	FILE *filep;
2684843c5791SPrarit Bhargava 	unsigned long dummy;
2685843c5791SPrarit Bhargava 
2686843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
2687843c5791SPrarit Bhargava 	filep = fopen_or_die(
2688843c5791SPrarit Bhargava 			"/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2689843c5791SPrarit Bhargava 			"r");
2690843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
26910e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
2692843c5791SPrarit Bhargava 	fclose(filep);
2693843c5791SPrarit Bhargava 	topo.max_cpu_num--; /* 0 based */
2694843c5791SPrarit Bhargava }
2695c98d5d94SLen Brown 
2696103a8feaSLen Brown /*
2697c98d5d94SLen Brown  * count_cpus()
2698c98d5d94SLen Brown  * remember the last one seen, it will be the max
2699103a8feaSLen Brown  */
2700c98d5d94SLen Brown int count_cpus(int cpu)
2701103a8feaSLen Brown {
2702843c5791SPrarit Bhargava 	topo.num_cpus++;
2703c98d5d94SLen Brown 	return 0;
2704103a8feaSLen Brown }
2705c98d5d94SLen Brown int mark_cpu_present(int cpu)
2706c98d5d94SLen Brown {
2707c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
270815aaa346SLen Brown 	return 0;
2709103a8feaSLen Brown }
2710103a8feaSLen Brown 
27118cb48b32SPrarit Bhargava int init_thread_id(int cpu)
27128cb48b32SPrarit Bhargava {
27138cb48b32SPrarit Bhargava 	cpus[cpu].thread_id = -1;
27148cb48b32SPrarit Bhargava 	return 0;
27158cb48b32SPrarit Bhargava }
27168cb48b32SPrarit Bhargava 
2717562a2d37SLen Brown /*
2718562a2d37SLen Brown  * snapshot_proc_interrupts()
2719562a2d37SLen Brown  *
2720562a2d37SLen Brown  * read and record summary of /proc/interrupts
2721562a2d37SLen Brown  *
2722562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2723562a2d37SLen Brown  */
2724562a2d37SLen Brown int snapshot_proc_interrupts(void)
2725562a2d37SLen Brown {
2726562a2d37SLen Brown 	static FILE *fp;
2727562a2d37SLen Brown 	int column, retval;
2728562a2d37SLen Brown 
2729562a2d37SLen Brown 	if (fp == NULL)
2730562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2731562a2d37SLen Brown 	else
2732562a2d37SLen Brown 		rewind(fp);
2733562a2d37SLen Brown 
2734562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2735562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2736562a2d37SLen Brown 		int cpu_number;
2737562a2d37SLen Brown 
2738562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2739562a2d37SLen Brown 		if (retval != 1)
2740562a2d37SLen Brown 			break;
2741562a2d37SLen Brown 
2742562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2743562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2744562a2d37SLen Brown 			return 1;
2745562a2d37SLen Brown 		}
2746562a2d37SLen Brown 
2747562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2748562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2749562a2d37SLen Brown 	}
2750562a2d37SLen Brown 
2751562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2752562a2d37SLen Brown 	while (1) {
2753562a2d37SLen Brown 		int column;
2754562a2d37SLen Brown 		char buf[64];
2755562a2d37SLen Brown 
2756562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2757562a2d37SLen Brown 		if (retval != 1)
2758562a2d37SLen Brown 			break;
2759562a2d37SLen Brown 
2760562a2d37SLen Brown 		/* read the count per cpu */
2761562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2762562a2d37SLen Brown 
2763562a2d37SLen Brown 			int cpu_number, irq_count;
2764562a2d37SLen Brown 
2765562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2766562a2d37SLen Brown 			if (retval != 1)
2767562a2d37SLen Brown 				break;
2768562a2d37SLen Brown 
2769562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2770562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2771562a2d37SLen Brown 
2772562a2d37SLen Brown 		}
2773562a2d37SLen Brown 
2774562a2d37SLen Brown 		while (getc(fp) != '\n')
2775562a2d37SLen Brown 			;	/* flush interrupt description */
2776562a2d37SLen Brown 
2777562a2d37SLen Brown 	}
2778562a2d37SLen Brown 	return 0;
2779562a2d37SLen Brown }
278027d47356SLen Brown /*
2781fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2782fdf676e5SLen Brown  *
2783fdf676e5SLen Brown  * record snapshot of
2784fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2785fdf676e5SLen Brown  *
2786fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2787fdf676e5SLen Brown  */
2788fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2789fdf676e5SLen Brown {
2790fdf676e5SLen Brown 	FILE *fp;
2791fdf676e5SLen Brown 	int retval;
2792fdf676e5SLen Brown 
2793fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2794fdf676e5SLen Brown 
2795fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2796fdf676e5SLen Brown 	if (retval != 1)
2797fdf676e5SLen Brown 		err(1, "GFX rc6");
2798fdf676e5SLen Brown 
2799fdf676e5SLen Brown 	fclose(fp);
2800fdf676e5SLen Brown 
2801fdf676e5SLen Brown 	return 0;
2802fdf676e5SLen Brown }
2803fdf676e5SLen Brown /*
280427d47356SLen Brown  * snapshot_gfx_mhz()
280527d47356SLen Brown  *
280627d47356SLen Brown  * record snapshot of
280727d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
280827d47356SLen Brown  *
280927d47356SLen Brown  * return 1 if config change requires a restart, else return 0
281027d47356SLen Brown  */
281127d47356SLen Brown int snapshot_gfx_mhz(void)
281227d47356SLen Brown {
281327d47356SLen Brown 	static FILE *fp;
281427d47356SLen Brown 	int retval;
281527d47356SLen Brown 
281627d47356SLen Brown 	if (fp == NULL)
281727d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
281822048c54SLen Brown 	else {
281927d47356SLen Brown 		rewind(fp);
282022048c54SLen Brown 		fflush(fp);
282122048c54SLen Brown 	}
282227d47356SLen Brown 
282327d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
282427d47356SLen Brown 	if (retval != 1)
282527d47356SLen Brown 		err(1, "GFX MHz");
282627d47356SLen Brown 
282727d47356SLen Brown 	return 0;
282827d47356SLen Brown }
2829562a2d37SLen Brown 
2830562a2d37SLen Brown /*
2831be0e54c4SLen Brown  * snapshot_cpu_lpi()
2832be0e54c4SLen Brown  *
2833be0e54c4SLen Brown  * record snapshot of
2834be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2835be0e54c4SLen Brown  *
2836be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2837be0e54c4SLen Brown  */
2838be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
2839be0e54c4SLen Brown {
2840be0e54c4SLen Brown 	FILE *fp;
2841be0e54c4SLen Brown 	int retval;
2842be0e54c4SLen Brown 
2843be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2844be0e54c4SLen Brown 
2845be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2846be0e54c4SLen Brown 	if (retval != 1)
2847be0e54c4SLen Brown 		err(1, "CPU LPI");
2848be0e54c4SLen Brown 
2849be0e54c4SLen Brown 	fclose(fp);
2850be0e54c4SLen Brown 
2851be0e54c4SLen Brown 	return 0;
2852be0e54c4SLen Brown }
2853be0e54c4SLen Brown /*
2854be0e54c4SLen Brown  * snapshot_sys_lpi()
2855be0e54c4SLen Brown  *
2856be0e54c4SLen Brown  * record snapshot of
2857be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2858be0e54c4SLen Brown  *
2859be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2860be0e54c4SLen Brown  */
2861be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
2862be0e54c4SLen Brown {
2863be0e54c4SLen Brown 	FILE *fp;
2864be0e54c4SLen Brown 	int retval;
2865be0e54c4SLen Brown 
2866be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2867be0e54c4SLen Brown 
2868be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2869be0e54c4SLen Brown 	if (retval != 1)
2870be0e54c4SLen Brown 		err(1, "SYS LPI");
2871be0e54c4SLen Brown 
2872be0e54c4SLen Brown 	fclose(fp);
2873be0e54c4SLen Brown 
2874be0e54c4SLen Brown 	return 0;
2875be0e54c4SLen Brown }
2876be0e54c4SLen Brown /*
2877562a2d37SLen Brown  * snapshot /proc and /sys files
2878562a2d37SLen Brown  *
2879562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2880562a2d37SLen Brown  */
2881562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2882562a2d37SLen Brown {
2883218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2884562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2885562a2d37SLen Brown 			return 1;
2886562a2d37SLen Brown 
2887812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2888fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2889fdf676e5SLen Brown 
2890812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
289127d47356SLen Brown 		snapshot_gfx_mhz();
289227d47356SLen Brown 
2893be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2894be0e54c4SLen Brown 		snapshot_cpu_lpi_us();
2895be0e54c4SLen Brown 
2896be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2897be0e54c4SLen Brown 		snapshot_sys_lpi_us();
2898be0e54c4SLen Brown 
2899562a2d37SLen Brown 	return 0;
2900562a2d37SLen Brown }
2901562a2d37SLen Brown 
29028aa2ed0bSLen Brown int exit_requested;
29038aa2ed0bSLen Brown 
29048aa2ed0bSLen Brown static void signal_handler (int signal)
29058aa2ed0bSLen Brown {
29068aa2ed0bSLen Brown 	switch (signal) {
29078aa2ed0bSLen Brown 	case SIGINT:
29088aa2ed0bSLen Brown 		exit_requested = 1;
29098aa2ed0bSLen Brown 		if (debug)
29108aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
29118aa2ed0bSLen Brown 		break;
291207211960SLen Brown 	case SIGUSR1:
291307211960SLen Brown 		if (debug > 1)
291407211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
291507211960SLen Brown 		break;
29168aa2ed0bSLen Brown 	}
2917b9ad8ee0SLen Brown 	/* make sure this manually-invoked interval is at least 1ms long */
2918b9ad8ee0SLen Brown 	nanosleep(&one_msec, NULL);
29198aa2ed0bSLen Brown }
29208aa2ed0bSLen Brown 
29218aa2ed0bSLen Brown void setup_signal_handler(void)
29228aa2ed0bSLen Brown {
29238aa2ed0bSLen Brown 	struct sigaction sa;
29248aa2ed0bSLen Brown 
29258aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
29268aa2ed0bSLen Brown 
29278aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
29288aa2ed0bSLen Brown 
29298aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
29308aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
293107211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
293207211960SLen Brown 		err(1, "sigaction SIGUSR1");
29338aa2ed0bSLen Brown }
2934b9ad8ee0SLen Brown 
293547936f94SArtem Bityutskiy void do_sleep(void)
2936b9ad8ee0SLen Brown {
2937b9ad8ee0SLen Brown 	struct timeval select_timeout;
2938b9ad8ee0SLen Brown 	fd_set readfds;
2939b9ad8ee0SLen Brown 	int retval;
2940b9ad8ee0SLen Brown 
2941b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
2942b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
2943b9ad8ee0SLen Brown 
294447936f94SArtem Bityutskiy 	if (!isatty(fileno(stdin))) {
294547936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
294647936f94SArtem Bityutskiy 		return;
294747936f94SArtem Bityutskiy 	}
2948b9ad8ee0SLen Brown 
294947936f94SArtem Bityutskiy 	select_timeout = interval_tv;
2950b9ad8ee0SLen Brown 	retval = select(1, &readfds, NULL, NULL, &select_timeout);
2951b9ad8ee0SLen Brown 
2952b9ad8ee0SLen Brown 	if (retval == 1) {
2953b9ad8ee0SLen Brown 		switch (getc(stdin)) {
2954b9ad8ee0SLen Brown 		case 'q':
2955b9ad8ee0SLen Brown 			exit_requested = 1;
2956b9ad8ee0SLen Brown 			break;
2957b9ad8ee0SLen Brown 		}
2958b9ad8ee0SLen Brown 		/* make sure this manually-invoked interval is at least 1ms long */
2959b9ad8ee0SLen Brown 		nanosleep(&one_msec, NULL);
2960b9ad8ee0SLen Brown 	}
2961b9ad8ee0SLen Brown }
296247936f94SArtem Bityutskiy 
29634c2122d4SLen Brown 
2964103a8feaSLen Brown void turbostat_loop()
2965103a8feaSLen Brown {
2966c98d5d94SLen Brown 	int retval;
2967e52966c0SLen Brown 	int restarted = 0;
2968023fe0acSChen Yu 	int done_iters = 0;
2969c98d5d94SLen Brown 
29708aa2ed0bSLen Brown 	setup_signal_handler();
29718aa2ed0bSLen Brown 
2972103a8feaSLen Brown restart:
2973e52966c0SLen Brown 	restarted++;
2974e52966c0SLen Brown 
2975562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2976c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
29774c2122d4SLen Brown 	first_counter_read = 0;
2978d91bb17cSLen Brown 	if (retval < -1) {
2979d91bb17cSLen Brown 		exit(retval);
2980d91bb17cSLen Brown 	} else if (retval == -1) {
2981e52966c0SLen Brown 		if (restarted > 1) {
2982e52966c0SLen Brown 			exit(retval);
2983e52966c0SLen Brown 		}
2984c98d5d94SLen Brown 		re_initialize();
2985c98d5d94SLen Brown 		goto restart;
2986c98d5d94SLen Brown 	}
2987e52966c0SLen Brown 	restarted = 0;
2988023fe0acSChen Yu 	done_iters = 0;
2989103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2990103a8feaSLen Brown 
2991103a8feaSLen Brown 	while (1) {
2992c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2993103a8feaSLen Brown 			re_initialize();
2994103a8feaSLen Brown 			goto restart;
2995103a8feaSLen Brown 		}
2996b9ad8ee0SLen Brown 		do_sleep();
2997562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2998562a2d37SLen Brown 			goto restart;
2999c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
3000d91bb17cSLen Brown 		if (retval < -1) {
3001d91bb17cSLen Brown 			exit(retval);
3002d91bb17cSLen Brown 		} else if (retval == -1) {
300315aaa346SLen Brown 			re_initialize();
300415aaa346SLen Brown 			goto restart;
300515aaa346SLen Brown 		}
3006103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
3007103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
3008ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3009ba3dec99SLen Brown 			re_initialize();
3010ba3dec99SLen Brown 			goto restart;
3011ba3dec99SLen Brown 		}
3012c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3013c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3014b7d8c148SLen Brown 		flush_output_stdout();
30158aa2ed0bSLen Brown 		if (exit_requested)
30168aa2ed0bSLen Brown 			break;
3017023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3018023fe0acSChen Yu 			break;
3019b9ad8ee0SLen Brown 		do_sleep();
3020562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3021562a2d37SLen Brown 			goto restart;
3022c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3023d91bb17cSLen Brown 		if (retval < -1) {
3024d91bb17cSLen Brown 			exit(retval);
3025d91bb17cSLen Brown 		} else if (retval == -1) {
3026103a8feaSLen Brown 			re_initialize();
3027103a8feaSLen Brown 			goto restart;
3028103a8feaSLen Brown 		}
3029103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3030103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3031ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3032ba3dec99SLen Brown 			re_initialize();
3033ba3dec99SLen Brown 			goto restart;
3034ba3dec99SLen Brown 		}
3035c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3036c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3037b7d8c148SLen Brown 		flush_output_stdout();
30388aa2ed0bSLen Brown 		if (exit_requested)
30398aa2ed0bSLen Brown 			break;
3040023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3041023fe0acSChen Yu 			break;
3042103a8feaSLen Brown 	}
3043103a8feaSLen Brown }
3044103a8feaSLen Brown 
3045103a8feaSLen Brown void check_dev_msr()
3046103a8feaSLen Brown {
3047103a8feaSLen Brown 	struct stat sb;
30487ce7d5deSPrarit Bhargava 	char pathname[32];
3049103a8feaSLen Brown 
30507ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
30517ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3052a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3053d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3054103a8feaSLen Brown }
3055103a8feaSLen Brown 
305698481e79SLen Brown void check_permissions()
3057103a8feaSLen Brown {
305898481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
305998481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
306098481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
306198481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
306298481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
306398481e79SLen Brown 	int do_exit = 0;
30647ce7d5deSPrarit Bhargava 	char pathname[32];
306598481e79SLen Brown 
306698481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
306798481e79SLen Brown 	cap_header->pid = getpid();
306898481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
306998481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
307098481e79SLen Brown 		err(-6, "capget(2) failed");
307198481e79SLen Brown 
307298481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
307398481e79SLen Brown 		do_exit++;
307498481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
307598481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
307698481e79SLen Brown 	}
307798481e79SLen Brown 
307898481e79SLen Brown 	/* test file permissions */
30797ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
30807ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
308198481e79SLen Brown 		do_exit++;
308298481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
308398481e79SLen Brown 	}
308498481e79SLen Brown 
308598481e79SLen Brown 	/* if all else fails, thell them to be root */
308698481e79SLen Brown 	if (do_exit)
3087b2c95d90SJosh Triplett 		if (getuid() != 0)
3088d7899447SLen Brown 			warnx("... or simply run as root");
308998481e79SLen Brown 
309098481e79SLen Brown 	if (do_exit)
309198481e79SLen Brown 		exit(-6);
3092103a8feaSLen Brown }
3093103a8feaSLen Brown 
3094d7899447SLen Brown /*
3095d7899447SLen Brown  * NHM adds support for additional MSRs:
3096d7899447SLen Brown  *
3097d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3098d7899447SLen Brown  *
3099ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
31001df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3101d7899447SLen Brown  *
3102cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3103cf4cbe53SLen Brown  *
3104d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3105d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3106d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3107d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3108d7899447SLen Brown  *
3109ee7e38e3SLen Brown  * Side effect:
31101df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
311133148d67SLen Brown  * sets has_misc_feature_control
3112d7899447SLen Brown  */
3113ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3114103a8feaSLen Brown {
3115ee7e38e3SLen Brown 	unsigned long long msr;
311621ed5574SLen Brown 	unsigned int base_ratio;
3117ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3118ee7e38e3SLen Brown 
3119103a8feaSLen Brown 	if (!genuine_intel)
3120103a8feaSLen Brown 		return 0;
3121103a8feaSLen Brown 
3122103a8feaSLen Brown 	if (family != 6)
3123103a8feaSLen Brown 		return 0;
3124103a8feaSLen Brown 
312521ed5574SLen Brown 	bclk = discover_bclk(family, model);
312621ed5574SLen Brown 
3127103a8feaSLen Brown 	switch (model) {
3128869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3129869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3130103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
3131869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
3132869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
3133869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3134869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
3135ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3136ee7e38e3SLen Brown 		break;
3137869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3138869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3139869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3140869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3141ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
314233148d67SLen Brown 		has_misc_feature_control = 1;
3143ee7e38e3SLen Brown 		break;
3144869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3145869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3146869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3147869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3148869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3149869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3150869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3151869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3152869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3153869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3154869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3155869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3156997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3157ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
315833148d67SLen Brown 		has_misc_feature_control = 1;
3159ee7e38e3SLen Brown 		break;
3160d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3161d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
316233148d67SLen Brown 		has_misc_feature_control = 1;
3163d8ebb442SLen Brown 		break;
3164869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3165cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3166869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3167ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3168ee7e38e3SLen Brown 		break;
3169869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3170ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3171cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3172ee7e38e3SLen Brown 		break;
3173869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3174005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3175ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3176ee7e38e3SLen Brown 		break;
3177869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3178ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3179869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3180e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
3181e4085d54SLen Brown 		break;
3182103a8feaSLen Brown 	default:
3183103a8feaSLen Brown 		return 0;
3184103a8feaSLen Brown 	}
31851df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3186e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3187ee7e38e3SLen Brown 
3188ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
318921ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
319021ed5574SLen Brown 
319121ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
319221ed5574SLen Brown 	has_base_hz = 1;
3193ee7e38e3SLen Brown 	return 1;
3194103a8feaSLen Brown }
31950f7887c4SLen Brown /*
3196495c7654SLen Brown  * SLV client has support for unique MSRs:
31970f7887c4SLen Brown  *
31980f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
31990f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
32000f7887c4SLen Brown  */
32010f7887c4SLen Brown 
32020f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
32030f7887c4SLen Brown {
32040f7887c4SLen Brown 	if (!genuine_intel)
32050f7887c4SLen Brown 		return 0;
32060f7887c4SLen Brown 
32070f7887c4SLen Brown 	switch (model) {
32080f7887c4SLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
32090f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MERRIFIELD:
32100f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MOOREFIELD:
32110f7887c4SLen Brown 		return 1;
32120f7887c4SLen Brown 	}
32130f7887c4SLen Brown 	return 0;
32140f7887c4SLen Brown }
32157170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
32167170a374SLen Brown {
32177170a374SLen Brown 
32187170a374SLen Brown 	if (!genuine_intel)
32197170a374SLen Brown 		return 0;
32207170a374SLen Brown 
32217170a374SLen Brown 	switch (model) {
32227170a374SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
32237170a374SLen Brown 		return 1;
32247170a374SLen Brown 	}
32257170a374SLen Brown 	return 0;
32267170a374SLen Brown }
3227ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3228ade0ebacSLen Brown {
3229ade0ebacSLen Brown 
3230ade0ebacSLen Brown 	if (!genuine_intel)
3231ade0ebacSLen Brown 		return 0;
3232ade0ebacSLen Brown 
3233ade0ebacSLen Brown 	switch (model) {
3234ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3235ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:
3236ade0ebacSLen Brown 		return 1;
3237ade0ebacSLen Brown 	}
3238ade0ebacSLen Brown 	return 0;
3239ade0ebacSLen Brown }
324034c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
324134c76197SLen Brown {
324234c76197SLen Brown 
324334c76197SLen Brown 	if (!genuine_intel)
324434c76197SLen Brown 		return 0;
324534c76197SLen Brown 
324634c76197SLen Brown 	switch (model) {
324734c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
324834c76197SLen Brown 		return 1;
324934c76197SLen Brown 	}
325034c76197SLen Brown 	return 0;
325134c76197SLen Brown }
32520f7887c4SLen Brown 
325331e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3254d7899447SLen Brown {
32550f7887c4SLen Brown 	if (has_slv_msrs(family, model))
32560f7887c4SLen Brown 		return 0;
32570f7887c4SLen Brown 
3258d7899447SLen Brown 	switch (model) {
3259d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
3260869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3261869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
3262869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3263005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3264d7899447SLen Brown 		return 0;
3265d7899447SLen Brown 	default:
3266d7899447SLen Brown 		return 1;
3267d7899447SLen Brown 	}
3268d7899447SLen Brown }
32690f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
32700f7887c4SLen Brown {
32710f7887c4SLen Brown 	if (has_slv_msrs(family, model))
32720f7887c4SLen Brown 		return 1;
32730f7887c4SLen Brown 
32740f7887c4SLen Brown 	return 0;
32750f7887c4SLen Brown }
32766574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
32776574a5d5SLen Brown {
32786574a5d5SLen Brown 	if (!genuine_intel)
32796574a5d5SLen Brown 		return 0;
32806574a5d5SLen Brown 
32816574a5d5SLen Brown 	if (family != 6)
32826574a5d5SLen Brown 		return 0;
32836574a5d5SLen Brown 
32846574a5d5SLen Brown 	switch (model) {
3285869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3286869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
32876574a5d5SLen Brown 		return 1;
32886574a5d5SLen Brown 	default:
32896574a5d5SLen Brown 		return 0;
32906574a5d5SLen Brown 	}
32916574a5d5SLen Brown }
3292fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3293fcd17211SLen Brown {
3294fcd17211SLen Brown 	if (!genuine_intel)
3295fcd17211SLen Brown 		return 0;
3296fcd17211SLen Brown 
3297fcd17211SLen Brown 	if (family != 6)
3298fcd17211SLen Brown 		return 0;
3299fcd17211SLen Brown 
3300fcd17211SLen Brown 	switch (model) {
3301869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3302fcd17211SLen Brown 		return 1;
3303fcd17211SLen Brown 	default:
3304fcd17211SLen Brown 		return 0;
3305fcd17211SLen Brown 	}
3306fcd17211SLen Brown }
3307fcd17211SLen Brown 
3308fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3309fb5d4327SDasaratharaman Chandramouli {
3310fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3311fb5d4327SDasaratharaman Chandramouli 		return 0;
3312fb5d4327SDasaratharaman Chandramouli 
3313fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3314fb5d4327SDasaratharaman Chandramouli 		return 0;
3315fb5d4327SDasaratharaman Chandramouli 
3316fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3317869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3318005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3319fb5d4327SDasaratharaman Chandramouli 		return 1;
3320fb5d4327SDasaratharaman Chandramouli 	default:
3321fb5d4327SDasaratharaman Chandramouli 		return 0;
3322fb5d4327SDasaratharaman Chandramouli 	}
3323fb5d4327SDasaratharaman Chandramouli }
332431e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
332531e07522SLen Brown {
332631e07522SLen Brown 	if (!genuine_intel)
332731e07522SLen Brown 		return 0;
332831e07522SLen Brown 
332931e07522SLen Brown 	if (family != 6)
333031e07522SLen Brown 		return 0;
333131e07522SLen Brown 
333231e07522SLen Brown 	switch (model) {
333331e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
333431e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
333531e07522SLen Brown 		return 1;
333631e07522SLen Brown 	default:
333731e07522SLen Brown 		return 0;
333831e07522SLen Brown 	}
333931e07522SLen Brown }
33406fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
33416fb3143bSLen Brown {
33426fb3143bSLen Brown 	if (!genuine_intel)
33436fb3143bSLen Brown 		return 0;
33446fb3143bSLen Brown 
33456fb3143bSLen Brown 	if (family != 6)
33466fb3143bSLen Brown 		return 0;
33476fb3143bSLen Brown 
33486fb3143bSLen Brown 	switch (model) {
3349869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3350869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3351869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3352869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3353869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3354869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3355869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3356869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3357869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3358869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3359869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3360869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3361869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3362997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3363869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
33646fb3143bSLen Brown 
3365869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3366005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
33676fb3143bSLen Brown 		return 1;
33686fb3143bSLen Brown 	default:
33696fb3143bSLen Brown 		return 0;
33706fb3143bSLen Brown 	}
33716fb3143bSLen Brown }
33726fb3143bSLen Brown 
3373fcd17211SLen Brown static void
33741b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3375fcd17211SLen Brown {
3376fcd17211SLen Brown 	if (!do_nhm_platform_info)
3377fcd17211SLen Brown 		return;
3378fcd17211SLen Brown 
3379fcd17211SLen Brown 	dump_nhm_platform_info();
3380fcd17211SLen Brown 
3381fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
3382fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
3383fcd17211SLen Brown 
3384fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
3385fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
3386fcd17211SLen Brown 
338731e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
338831e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
3389fcd17211SLen Brown 
33900f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
33910f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
33920f7887c4SLen Brown 
3393fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
3394fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
3395fb5d4327SDasaratharaman Chandramouli 
33966fb3143bSLen Brown 	if (has_config_tdp(family, model))
33976fb3143bSLen Brown 		dump_config_tdp();
33986fb3143bSLen Brown 
3399fcd17211SLen Brown 	dump_nhm_cst_cfg();
3400fcd17211SLen Brown }
3401fcd17211SLen Brown 
340241618e63SLen Brown static void
340341618e63SLen Brown dump_sysfs_cstate_config(void)
340441618e63SLen Brown {
340541618e63SLen Brown 	char path[64];
340641618e63SLen Brown 	char name_buf[16];
340741618e63SLen Brown 	char desc[64];
340841618e63SLen Brown 	FILE *input;
340941618e63SLen Brown 	int state;
341041618e63SLen Brown 	char *sp;
341141618e63SLen Brown 
341241618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
341341618e63SLen Brown 		return;
341441618e63SLen Brown 
341541618e63SLen Brown 	for (state = 0; state < 10; ++state) {
341641618e63SLen Brown 
341741618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
341841618e63SLen Brown 			base_cpu, state);
341941618e63SLen Brown 		input = fopen(path, "r");
342041618e63SLen Brown 		if (input == NULL)
342141618e63SLen Brown 			continue;
342241618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
342341618e63SLen Brown 
342441618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
342541618e63SLen Brown 		sp = strchr(name_buf, '-');
342641618e63SLen Brown 		if (!sp)
342741618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
342841618e63SLen Brown 		*sp = '\0';
342941618e63SLen Brown 
343041618e63SLen Brown 		fclose(input);
343141618e63SLen Brown 
343241618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
343341618e63SLen Brown 			base_cpu, state);
343441618e63SLen Brown 		input = fopen(path, "r");
343541618e63SLen Brown 		if (input == NULL)
343641618e63SLen Brown 			continue;
343741618e63SLen Brown 		fgets(desc, sizeof(desc), input);
343841618e63SLen Brown 
343941618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
344041618e63SLen Brown 		fclose(input);
344141618e63SLen Brown 	}
344241618e63SLen Brown }
34437293fccdSLen Brown static void
34447293fccdSLen Brown dump_sysfs_pstate_config(void)
34457293fccdSLen Brown {
34467293fccdSLen Brown 	char path[64];
34477293fccdSLen Brown 	char driver_buf[64];
34487293fccdSLen Brown 	char governor_buf[64];
34497293fccdSLen Brown 	FILE *input;
34507293fccdSLen Brown 	int turbo;
34517293fccdSLen Brown 
34527293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
34537293fccdSLen Brown 			base_cpu);
34547293fccdSLen Brown 	input = fopen(path, "r");
34557293fccdSLen Brown 	if (input == NULL) {
34567293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
34577293fccdSLen Brown 		return;
34587293fccdSLen Brown 	}
34597293fccdSLen Brown 	fgets(driver_buf, sizeof(driver_buf), input);
34607293fccdSLen Brown 	fclose(input);
34617293fccdSLen Brown 
34627293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
34637293fccdSLen Brown 			base_cpu);
34647293fccdSLen Brown 	input = fopen(path, "r");
34657293fccdSLen Brown 	if (input == NULL) {
34667293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
34677293fccdSLen Brown 		return;
34687293fccdSLen Brown 	}
34697293fccdSLen Brown 	fgets(governor_buf, sizeof(governor_buf), input);
34707293fccdSLen Brown 	fclose(input);
34717293fccdSLen Brown 
34727293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
34737293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
34747293fccdSLen Brown 
34757293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
34767293fccdSLen Brown 	input = fopen(path, "r");
34777293fccdSLen Brown 	if (input != NULL) {
34787293fccdSLen Brown 		fscanf(input, "%d", &turbo);
34797293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
34807293fccdSLen Brown 		fclose(input);
34817293fccdSLen Brown 	}
34827293fccdSLen Brown 
34837293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
34847293fccdSLen Brown 	input = fopen(path, "r");
34857293fccdSLen Brown 	if (input != NULL) {
34867293fccdSLen Brown 		fscanf(input, "%d", &turbo);
34877293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
34887293fccdSLen Brown 		fclose(input);
34897293fccdSLen Brown 	}
34907293fccdSLen Brown }
349141618e63SLen Brown 
34926574a5d5SLen Brown 
3493889facbeSLen Brown /*
3494889facbeSLen Brown  * print_epb()
3495889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3496889facbeSLen Brown  */
3497889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3498889facbeSLen Brown {
3499889facbeSLen Brown 	unsigned long long msr;
3500889facbeSLen Brown 	char *epb_string;
3501889facbeSLen Brown 	int cpu;
3502889facbeSLen Brown 
3503889facbeSLen Brown 	if (!has_epb)
3504889facbeSLen Brown 		return 0;
3505889facbeSLen Brown 
3506889facbeSLen Brown 	cpu = t->cpu_id;
3507889facbeSLen Brown 
3508889facbeSLen Brown 	/* EPB is per-package */
3509889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3510889facbeSLen Brown 		return 0;
3511889facbeSLen Brown 
3512889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3513b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3514889facbeSLen Brown 		return -1;
3515889facbeSLen Brown 	}
3516889facbeSLen Brown 
3517889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3518889facbeSLen Brown 		return 0;
3519889facbeSLen Brown 
3520e9be7dd6SLen Brown 	switch (msr & 0xF) {
3521889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3522889facbeSLen Brown 		epb_string = "performance";
3523889facbeSLen Brown 		break;
3524889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3525889facbeSLen Brown 		epb_string = "balanced";
3526889facbeSLen Brown 		break;
3527889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3528889facbeSLen Brown 		epb_string = "powersave";
3529889facbeSLen Brown 		break;
3530889facbeSLen Brown 	default:
3531889facbeSLen Brown 		epb_string = "custom";
3532889facbeSLen Brown 		break;
3533889facbeSLen Brown 	}
3534b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3535889facbeSLen Brown 
3536889facbeSLen Brown 	return 0;
3537889facbeSLen Brown }
35387f5c258eSLen Brown /*
35397f5c258eSLen Brown  * print_hwp()
35407f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
35417f5c258eSLen Brown  */
35427f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
35437f5c258eSLen Brown {
35447f5c258eSLen Brown 	unsigned long long msr;
35457f5c258eSLen Brown 	int cpu;
35467f5c258eSLen Brown 
35477f5c258eSLen Brown 	if (!has_hwp)
35487f5c258eSLen Brown 		return 0;
35497f5c258eSLen Brown 
35507f5c258eSLen Brown 	cpu = t->cpu_id;
35517f5c258eSLen Brown 
35527f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
35537f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
35547f5c258eSLen Brown 		return 0;
35557f5c258eSLen Brown 
35567f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3557b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
35587f5c258eSLen Brown 		return -1;
35597f5c258eSLen Brown 	}
35607f5c258eSLen Brown 
35617f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
35627f5c258eSLen Brown 		return 0;
35637f5c258eSLen Brown 
3564b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
35657f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
35667f5c258eSLen Brown 
35677f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
35687f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
35697f5c258eSLen Brown 		return 0;
35707f5c258eSLen Brown 
35717f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
35727f5c258eSLen Brown 		return 0;
35737f5c258eSLen Brown 
3574b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
35756dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
35767f5c258eSLen Brown 			cpu, msr,
35777f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
35787f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
35797f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
35807f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
35817f5c258eSLen Brown 
35827f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
35837f5c258eSLen Brown 		return 0;
35847f5c258eSLen Brown 
3585b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
35866dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
35877f5c258eSLen Brown 			cpu, msr,
35887f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
35897f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
35907f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
35917f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
35927f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
35937f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
35947f5c258eSLen Brown 
35957f5c258eSLen Brown 	if (has_hwp_pkg) {
35967f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
35977f5c258eSLen Brown 			return 0;
35987f5c258eSLen Brown 
3599b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
36006dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
36017f5c258eSLen Brown 			cpu, msr,
36027f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
36037f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
36047f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
36057f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
36067f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
36077f5c258eSLen Brown 	}
36087f5c258eSLen Brown 	if (has_hwp_notify) {
36097f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
36107f5c258eSLen Brown 			return 0;
36117f5c258eSLen Brown 
3612b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
36137f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
36147f5c258eSLen Brown 			cpu, msr,
36157f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
36167f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
36177f5c258eSLen Brown 	}
36187f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
36197f5c258eSLen Brown 		return 0;
36207f5c258eSLen Brown 
3621b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
36227f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
36237f5c258eSLen Brown 			cpu, msr,
36247f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
36257f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3626889facbeSLen Brown 
3627889facbeSLen Brown 	return 0;
3628889facbeSLen Brown }
3629889facbeSLen Brown 
36303a9a941dSLen Brown /*
36313a9a941dSLen Brown  * print_perf_limit()
36323a9a941dSLen Brown  */
36333a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
36343a9a941dSLen Brown {
36353a9a941dSLen Brown 	unsigned long long msr;
36363a9a941dSLen Brown 	int cpu;
36373a9a941dSLen Brown 
36383a9a941dSLen Brown 	cpu = t->cpu_id;
36393a9a941dSLen Brown 
36403a9a941dSLen Brown 	/* per-package */
36413a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36423a9a941dSLen Brown 		return 0;
36433a9a941dSLen Brown 
36443a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3645b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
36463a9a941dSLen Brown 		return -1;
36473a9a941dSLen Brown 	}
36483a9a941dSLen Brown 
36493a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
36503a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3651b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3652b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3653e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
36543a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3655e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3656e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3657e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3658e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3659e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3660e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3661e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3662e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3663e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3664e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3665e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3666e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3667b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3668e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
36693a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3670e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3671e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3672e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3673e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3674e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3675e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3676e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3677e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3678e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3679e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3680e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3681e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
36823a9a941dSLen Brown 
36833a9a941dSLen Brown 	}
36843a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
36853a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3686b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3687b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
36883a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
36893a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
36903a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
36913a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
36923a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
36933a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
36943a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
36953a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3696b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
36973a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
36983a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
36993a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
37003a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
37013a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37023a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
37033a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37043a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37053a9a941dSLen Brown 	}
37063a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
37073a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3708b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3709b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
37103a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
37113a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
37123a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
37133a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
37143a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
37153a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3716b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
37173a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
37183a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
37193a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
37203a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37213a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37223a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37233a9a941dSLen Brown 	}
37243a9a941dSLen Brown 	return 0;
37253a9a941dSLen Brown }
37263a9a941dSLen Brown 
3727889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3728889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3729889facbeSLen Brown 
37301b69317dSColin Ian King double get_tdp(unsigned int model)
3731144b44b1SLen Brown {
3732144b44b1SLen Brown 	unsigned long long msr;
3733144b44b1SLen Brown 
3734144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
37357ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3736144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3737144b44b1SLen Brown 
3738144b44b1SLen Brown 	switch (model) {
3739869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
3740869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:
3741144b44b1SLen Brown 		return 30.0;
3742144b44b1SLen Brown 	default:
3743144b44b1SLen Brown 		return 135.0;
3744144b44b1SLen Brown 	}
3745144b44b1SLen Brown }
3746144b44b1SLen Brown 
374740ee8e3bSAndrey Semin /*
374840ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
374940ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
375040ee8e3bSAndrey Semin  */
375140ee8e3bSAndrey Semin static double
375240ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
375340ee8e3bSAndrey Semin {
375440ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
375540ee8e3bSAndrey Semin 
375640ee8e3bSAndrey Semin 	switch (model) {
3757869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3758869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3759869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3760869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3761005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
376240ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
376340ee8e3bSAndrey Semin 	default:
376440ee8e3bSAndrey Semin 		return (rapl_energy_units);
376540ee8e3bSAndrey Semin 	}
376640ee8e3bSAndrey Semin }
376740ee8e3bSAndrey Semin 
3768144b44b1SLen Brown 
3769889facbeSLen Brown /*
3770889facbeSLen Brown  * rapl_probe()
3771889facbeSLen Brown  *
3772144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3773889facbeSLen Brown  */
3774889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
3775889facbeSLen Brown {
3776889facbeSLen Brown 	unsigned long long msr;
3777144b44b1SLen Brown 	unsigned int time_unit;
3778889facbeSLen Brown 	double tdp;
3779889facbeSLen Brown 
3780889facbeSLen Brown 	if (!genuine_intel)
3781889facbeSLen Brown 		return;
3782889facbeSLen Brown 
3783889facbeSLen Brown 	if (family != 6)
3784889facbeSLen Brown 		return;
3785889facbeSLen Brown 
3786889facbeSLen Brown 	switch (model) {
3787869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3788869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3789869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3790869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3791869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3792869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3793869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3794144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3795812db3f7SLen Brown 		if (rapl_joules) {
3796812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3797812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3798812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3799812db3f7SLen Brown 		} else {
3800812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3801812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3802812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3803812db3f7SLen Brown 		}
3804889facbeSLen Brown 		break;
3805869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3806ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3807e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3808812db3f7SLen Brown 		if (rapl_joules)
3809812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3810812db3f7SLen Brown 		else
3811812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3812e4085d54SLen Brown 		break;
3813869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3814869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3815869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3816869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3817997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
381881824921SLen 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;
3819812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3820812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3821812db3f7SLen Brown 		if (rapl_joules) {
3822812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3823812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3824812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
382581824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3826812db3f7SLen Brown 		} else {
3827812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3828812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3829812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
383081824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3831812db3f7SLen Brown 		}
38320b2bb692SLen Brown 		break;
3833869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3834869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3835869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3836869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3837869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3838005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
38390b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3840812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3841812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3842812db3f7SLen Brown 		if (rapl_joules) {
3843812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3844812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3845812db3f7SLen Brown 		} else {
3846812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3847812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3848812db3f7SLen Brown 		}
3849e6f9bb3cSLen Brown 		break;
3850869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3851869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
38520b2bb692SLen 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;
3853812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3854812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3855812db3f7SLen Brown 		if (rapl_joules) {
3856812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3857812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3858812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3859812db3f7SLen Brown 		} else {
3860812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3861812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3862812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3863812db3f7SLen Brown 		}
3864144b44b1SLen Brown 		break;
3865869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3866869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3867144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3868812db3f7SLen Brown 		if (rapl_joules) {
3869812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3870812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3871812db3f7SLen Brown 		} else {
3872812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3873812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3874812db3f7SLen Brown 		}
3875889facbeSLen Brown 		break;
3876869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
38770f644909SJacob 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;
3878812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3879812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3880812db3f7SLen Brown 		if (rapl_joules) {
3881812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3882812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3883812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3884812db3f7SLen Brown 		} else {
3885812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3886812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3887812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3888812db3f7SLen Brown 		}
38890f644909SJacob Pan 		break;
3890889facbeSLen Brown 	default:
3891889facbeSLen Brown 		return;
3892889facbeSLen Brown 	}
3893889facbeSLen Brown 
3894889facbeSLen Brown 	/* units on package 0, verify later other packages match */
38957ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3896889facbeSLen Brown 		return;
3897889facbeSLen Brown 
3898889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3899869ce69eSLen Brown 	if (model == INTEL_FAM6_ATOM_SILVERMONT1)
3900144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3901144b44b1SLen Brown 	else
3902889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3903889facbeSLen Brown 
390440ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
390540ee8e3bSAndrey Semin 
3906144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3907144b44b1SLen Brown 	if (time_unit == 0)
3908144b44b1SLen Brown 		time_unit = 0xA;
3909889facbeSLen Brown 
3910144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3911144b44b1SLen Brown 
3912144b44b1SLen Brown 	tdp = get_tdp(model);
3913889facbeSLen Brown 
3914889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
391596e47158SLen Brown 	if (!quiet)
3916b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3917889facbeSLen Brown 
3918889facbeSLen Brown 	return;
3919889facbeSLen Brown }
3920889facbeSLen Brown 
39211b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
39223a9a941dSLen Brown {
39233a9a941dSLen Brown 	if (!genuine_intel)
39243a9a941dSLen Brown 		return;
39253a9a941dSLen Brown 
39263a9a941dSLen Brown 	if (family != 6)
39273a9a941dSLen Brown 		return;
39283a9a941dSLen Brown 
39293a9a941dSLen Brown 	switch (model) {
3930869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3931869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3932869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
39333a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
3934869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
39353a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
39363a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
39373a9a941dSLen Brown 	default:
39383a9a941dSLen Brown 		return;
39393a9a941dSLen Brown 	}
39403a9a941dSLen Brown }
39413a9a941dSLen Brown 
3942ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3943ac980e13SArtem Bityutskiy {
3944ac980e13SArtem Bityutskiy 	if (is_skx(family, model) || is_bdx(family, model))
3945ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
3946ac980e13SArtem Bityutskiy }
3947ac980e13SArtem Bityutskiy 
3948889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3949889facbeSLen Brown {
3950889facbeSLen Brown 	unsigned long long msr;
3951f4896fa5SLen Brown 	unsigned int dts, dts2;
3952889facbeSLen Brown 	int cpu;
3953889facbeSLen Brown 
3954889facbeSLen Brown 	if (!(do_dts || do_ptm))
3955889facbeSLen Brown 		return 0;
3956889facbeSLen Brown 
3957889facbeSLen Brown 	cpu = t->cpu_id;
3958889facbeSLen Brown 
3959889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
3960889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3961889facbeSLen Brown 		return 0;
3962889facbeSLen Brown 
3963889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3964b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3965889facbeSLen Brown 		return -1;
3966889facbeSLen Brown 	}
3967889facbeSLen Brown 
3968889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3969889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3970889facbeSLen Brown 			return 0;
3971889facbeSLen Brown 
3972889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3973b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3974889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
3975889facbeSLen Brown 
3976889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3977889facbeSLen Brown 			return 0;
3978889facbeSLen Brown 
3979889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3980889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3981b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3982889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3983889facbeSLen Brown 	}
3984889facbeSLen Brown 
3985889facbeSLen Brown 
3986f4896fa5SLen Brown 	if (do_dts && debug) {
3987889facbeSLen Brown 		unsigned int resolution;
3988889facbeSLen Brown 
3989889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3990889facbeSLen Brown 			return 0;
3991889facbeSLen Brown 
3992889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3993889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
3994b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3995889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
3996889facbeSLen Brown 
3997889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3998889facbeSLen Brown 			return 0;
3999889facbeSLen Brown 
4000889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4001889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4002b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4003889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4004889facbeSLen Brown 	}
4005889facbeSLen Brown 
4006889facbeSLen Brown 	return 0;
4007889facbeSLen Brown }
4008889facbeSLen Brown 
4009889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4010889facbeSLen Brown {
4011b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4012889facbeSLen Brown 		cpu, label,
4013889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
4014889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
4015889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4016889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
4017889facbeSLen Brown 
4018889facbeSLen Brown 	return;
4019889facbeSLen Brown }
4020889facbeSLen Brown 
4021889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4022889facbeSLen Brown {
4023889facbeSLen Brown 	unsigned long long msr;
4024889facbeSLen Brown 	int cpu;
4025889facbeSLen Brown 
4026889facbeSLen Brown 	if (!do_rapl)
4027889facbeSLen Brown 		return 0;
4028889facbeSLen Brown 
4029889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4030889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4031889facbeSLen Brown 		return 0;
4032889facbeSLen Brown 
4033889facbeSLen Brown 	cpu = t->cpu_id;
4034889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4035b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4036889facbeSLen Brown 		return -1;
4037889facbeSLen Brown 	}
4038889facbeSLen Brown 
4039889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4040889facbeSLen Brown 		return -1;
4041889facbeSLen Brown 
404296e47158SLen Brown 	fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
4043144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
404496e47158SLen Brown 
4045144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4046144b44b1SLen Brown 
4047889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4048889facbeSLen Brown                 	return -5;
4049889facbeSLen Brown 
4050889facbeSLen Brown 
4051b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4052889facbeSLen Brown 			cpu, msr,
4053889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4054889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4055889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4056889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4057889facbeSLen Brown 
4058144b44b1SLen Brown 	}
4059144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4060144b44b1SLen Brown 
4061889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4062889facbeSLen Brown 			return -9;
4063889facbeSLen Brown 
4064b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4065889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4066889facbeSLen Brown 
4067889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
4068b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4069889facbeSLen Brown 			cpu,
4070889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4071889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4072889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4073889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4074889facbeSLen Brown 	}
4075889facbeSLen Brown 
40760b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4077889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4078889facbeSLen Brown                 	return -6;
4079889facbeSLen Brown 
4080b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4081889facbeSLen Brown 			cpu, msr,
4082889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4083889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4084889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4085889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
40860b2bb692SLen Brown 	}
40870b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4088889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4089889facbeSLen Brown 			return -9;
4090b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4091889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4092889facbeSLen Brown 
4093889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
4094889facbeSLen Brown 	}
4095144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
4096889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4097889facbeSLen Brown 			return -7;
4098889facbeSLen Brown 
4099b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4100144b44b1SLen Brown 	}
41019148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4102889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4103889facbeSLen Brown 			return -9;
4104b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4105889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4106889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
4107889facbeSLen Brown 	}
4108889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
4109889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4110889facbeSLen Brown 			return -8;
4111889facbeSLen Brown 
4112b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4113889facbeSLen Brown 
4114889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4115889facbeSLen Brown 			return -9;
4116b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4117889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4118889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
4119889facbeSLen Brown 	}
4120889facbeSLen Brown 	return 0;
4121889facbeSLen Brown }
4122889facbeSLen Brown 
4123d7899447SLen Brown /*
4124d7899447SLen Brown  * SNB adds support for additional MSRs:
4125d7899447SLen Brown  *
4126d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
4127d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
4128d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
4129d7899447SLen Brown  */
4130103a8feaSLen Brown 
4131d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
4132103a8feaSLen Brown {
4133103a8feaSLen Brown 	if (!genuine_intel)
4134103a8feaSLen Brown 		return 0;
4135103a8feaSLen Brown 
4136103a8feaSLen Brown 	switch (model) {
4137869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4138869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4139869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
4140869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
4141869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
4142869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
4143869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
4144869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
4145869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
4146869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
4147869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4148869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
4149869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4150869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4151869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4152869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4153997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4154869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
4155869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4156ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
41575bbac26eSXiaolong Wang 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
4158103a8feaSLen Brown 		return 1;
4159103a8feaSLen Brown 	}
4160103a8feaSLen Brown 	return 0;
4161103a8feaSLen Brown }
4162103a8feaSLen Brown 
4163d7899447SLen Brown /*
4164d7899447SLen Brown  * HSW adds support for additional MSRs:
4165d7899447SLen Brown  *
4166d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
4167d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
4168d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
41695a63426eSLen Brown  *
41705a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
41715a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
41725a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
41735a63426eSLen Brown  *
4174d7899447SLen Brown  */
4175d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
4176ca58710fSKristen Carlson Accardi {
4177ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
4178ca58710fSKristen Carlson Accardi 		return 0;
4179ca58710fSKristen Carlson Accardi 
4180ca58710fSKristen Carlson Accardi 	switch (model) {
4181869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
4182869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
4183869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4184869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4185869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4186869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4187997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4188869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4189ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
4190ca58710fSKristen Carlson Accardi 		return 1;
4191ca58710fSKristen Carlson Accardi 	}
4192ca58710fSKristen Carlson Accardi 	return 0;
4193ca58710fSKristen Carlson Accardi }
4194ca58710fSKristen Carlson Accardi 
41950b2bb692SLen Brown /*
41960b2bb692SLen Brown  * SKL adds support for additional MSRS:
41970b2bb692SLen Brown  *
41980b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
41990b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
42000b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
42010b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
42020b2bb692SLen Brown  */
42030b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
42040b2bb692SLen Brown {
42050b2bb692SLen Brown 	if (!genuine_intel)
42060b2bb692SLen Brown 		return 0;
42070b2bb692SLen Brown 
42080b2bb692SLen Brown 	switch (model) {
4209869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4210869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4211869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4212869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4213997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
42140b2bb692SLen Brown 		return 1;
42150b2bb692SLen Brown 	}
42160b2bb692SLen Brown 	return 0;
42170b2bb692SLen Brown }
42180b2bb692SLen Brown 
4219144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
4220144b44b1SLen Brown {
4221144b44b1SLen Brown 	if (!genuine_intel)
4222144b44b1SLen Brown 		return 0;
4223144b44b1SLen Brown 	switch (model) {
4224869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
4225869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
4226144b44b1SLen Brown 		return 1;
4227144b44b1SLen Brown 	}
4228144b44b1SLen Brown 	return 0;
4229144b44b1SLen Brown }
4230144b44b1SLen Brown 
4231fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
4232fb5d4327SDasaratharaman Chandramouli {
4233fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
4234fb5d4327SDasaratharaman Chandramouli 		return 0;
4235fb5d4327SDasaratharaman Chandramouli 	switch (model) {
4236869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
4237005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
4238fb5d4327SDasaratharaman Chandramouli 		return 1;
4239fb5d4327SDasaratharaman Chandramouli 	}
4240fb5d4327SDasaratharaman Chandramouli 	return 0;
4241fb5d4327SDasaratharaman Chandramouli }
4242fb5d4327SDasaratharaman Chandramouli 
4243997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
4244997e5395SSrinivas Pandruvada {
4245997e5395SSrinivas Pandruvada 	if (!genuine_intel)
4246997e5395SSrinivas Pandruvada 		return 0;
4247997e5395SSrinivas Pandruvada 
4248997e5395SSrinivas Pandruvada 	switch (model) {
4249997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4250997e5395SSrinivas Pandruvada 		return 1;
4251997e5395SSrinivas Pandruvada 	}
4252997e5395SSrinivas Pandruvada 
4253997e5395SSrinivas Pandruvada 	return 0;
4254997e5395SSrinivas Pandruvada }
4255997e5395SSrinivas Pandruvada 
4256b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4257b2b34dfeSHubert Chrzaniuk {
4258b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
4259b2b34dfeSHubert Chrzaniuk 		return 1024;
4260b2b34dfeSHubert Chrzaniuk 	return 1;
4261b2b34dfeSHubert Chrzaniuk }
4262b2b34dfeSHubert Chrzaniuk 
4263144b44b1SLen Brown #define SLM_BCLK_FREQS 5
4264144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4265144b44b1SLen Brown 
4266144b44b1SLen Brown double slm_bclk(void)
4267144b44b1SLen Brown {
4268144b44b1SLen Brown 	unsigned long long msr = 3;
4269144b44b1SLen Brown 	unsigned int i;
4270144b44b1SLen Brown 	double freq;
4271144b44b1SLen Brown 
42727ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4273b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
4274144b44b1SLen Brown 
4275144b44b1SLen Brown 	i = msr & 0xf;
4276144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
4277b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
42780a91e551SColin Ian King 		i = 3;
4279144b44b1SLen Brown 	}
4280144b44b1SLen Brown 	freq = slm_freq_table[i];
4281144b44b1SLen Brown 
428296e47158SLen Brown 	if (!quiet)
4283b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4284144b44b1SLen Brown 
4285144b44b1SLen Brown 	return freq;
4286144b44b1SLen Brown }
4287144b44b1SLen Brown 
4288103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
4289103a8feaSLen Brown {
4290121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
4291103a8feaSLen Brown 		return 100.00;
4292144b44b1SLen Brown 	else if (is_slm(family, model))
4293144b44b1SLen Brown 		return slm_bclk();
4294103a8feaSLen Brown 	else
4295103a8feaSLen Brown 		return 133.33;
4296103a8feaSLen Brown }
4297103a8feaSLen Brown 
4298889facbeSLen Brown /*
4299889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4300889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
4301889facbeSLen Brown  * This is usually equal to tjMax.
4302889facbeSLen Brown  *
4303889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
4304889facbeSLen Brown  * but also allow cmdline over-ride with -T.
4305889facbeSLen Brown  *
4306889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
4307889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
4308889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4309889facbeSLen Brown  */
4310889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4311889facbeSLen Brown {
4312889facbeSLen Brown 	unsigned long long msr;
4313889facbeSLen Brown 	unsigned int target_c_local;
4314889facbeSLen Brown 	int cpu;
4315889facbeSLen Brown 
4316889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
4317889facbeSLen Brown 	if (!(do_dts || do_ptm))
4318889facbeSLen Brown 		return 0;
4319889facbeSLen Brown 
4320889facbeSLen Brown 	/* this is a per-package concept */
4321889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4322889facbeSLen Brown 		return 0;
4323889facbeSLen Brown 
4324889facbeSLen Brown 	cpu = t->cpu_id;
4325889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4326b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4327889facbeSLen Brown 		return -1;
4328889facbeSLen Brown 	}
4329889facbeSLen Brown 
4330889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
4331889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
4332b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4333889facbeSLen Brown 			cpu, tcc_activation_temp);
4334889facbeSLen Brown 		return 0;
4335889facbeSLen Brown 	}
4336889facbeSLen Brown 
4337889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
4338d7899447SLen Brown 	if (!do_nhm_platform_info)
4339889facbeSLen Brown 		goto guess;
4340889facbeSLen Brown 
43417ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4342889facbeSLen Brown 		goto guess;
4343889facbeSLen Brown 
43443482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
4345889facbeSLen Brown 
434696e47158SLen Brown 	if (!quiet)
4347b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4348889facbeSLen Brown 			cpu, msr, target_c_local);
4349889facbeSLen Brown 
43503482124aSJean Delvare 	if (!target_c_local)
4351889facbeSLen Brown 		goto guess;
4352889facbeSLen Brown 
4353889facbeSLen Brown 	tcc_activation_temp = target_c_local;
4354889facbeSLen Brown 
4355889facbeSLen Brown 	return 0;
4356889facbeSLen Brown 
4357889facbeSLen Brown guess:
4358889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
4359b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4360889facbeSLen Brown 		cpu, tcc_activation_temp);
4361889facbeSLen Brown 
4362889facbeSLen Brown 	return 0;
4363889facbeSLen Brown }
436469807a63SLen Brown 
4365aa8d8cc7SLen Brown void decode_feature_control_msr(void)
4366aa8d8cc7SLen Brown {
4367aa8d8cc7SLen Brown 	unsigned long long msr;
4368aa8d8cc7SLen Brown 
4369aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4370aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4371aa8d8cc7SLen Brown 			base_cpu, msr,
4372aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4373aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
4374aa8d8cc7SLen Brown }
4375aa8d8cc7SLen Brown 
437669807a63SLen Brown void decode_misc_enable_msr(void)
437769807a63SLen Brown {
437869807a63SLen Brown 	unsigned long long msr;
437969807a63SLen Brown 
4380f26b1519SLen Brown 	if (!genuine_intel)
4381f26b1519SLen Brown 		return;
4382f26b1519SLen Brown 
438369807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4384e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
438569807a63SLen Brown 			base_cpu, msr,
4386e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4387e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4388fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4389e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4390e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
439169807a63SLen Brown }
439269807a63SLen Brown 
439333148d67SLen Brown void decode_misc_feature_control(void)
439433148d67SLen Brown {
439533148d67SLen Brown 	unsigned long long msr;
439633148d67SLen Brown 
439733148d67SLen Brown 	if (!has_misc_feature_control)
439833148d67SLen Brown 		return;
439933148d67SLen Brown 
440033148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
440133148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
440233148d67SLen Brown 			base_cpu, msr,
440333148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
440433148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
440533148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
440633148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
440733148d67SLen Brown }
4408f0057310SLen Brown /*
4409f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
4410f0057310SLen Brown  *
4411f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
4412f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
4413f0057310SLen Brown  * bit[1] less so...
4414f0057310SLen Brown  */
4415f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
4416f0057310SLen Brown {
4417f0057310SLen Brown 	unsigned long long msr;
4418f0057310SLen Brown 
4419f0057310SLen Brown 	if (!do_nhm_platform_info)
4420f0057310SLen Brown 		return;
4421f0057310SLen Brown 
4422cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
4423cf4cbe53SLen Brown 		return;
4424cf4cbe53SLen Brown 
4425f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4426ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4427f0057310SLen Brown 			base_cpu, msr,
4428f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
4429ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
4430ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
4431f0057310SLen Brown }
443271616c8eSLen Brown /*
443371616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
443471616c8eSLen Brown  *
443571616c8eSLen Brown  * This MSRs are present on Silvermont processors,
443671616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
443771616c8eSLen Brown  */
443871616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
443971616c8eSLen Brown {
444071616c8eSLen Brown 	unsigned long long msr;
444171616c8eSLen Brown 
444271616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
444371616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
444471616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
444571616c8eSLen Brown 
444671616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
444771616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
444871616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
444971616c8eSLen Brown }
44507f5c258eSLen Brown 
4451fcd17211SLen Brown void process_cpuid()
4452103a8feaSLen Brown {
445361a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
4454103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
4455b3a34e93SLen Brown 	unsigned int has_turbo;
4456103a8feaSLen Brown 
4457103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4458103a8feaSLen Brown 
44595aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4460103a8feaSLen Brown 
4461103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4462103a8feaSLen Brown 		genuine_intel = 1;
4463103a8feaSLen Brown 
446496e47158SLen Brown 	if (!quiet)
4465b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4466103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4467103a8feaSLen Brown 
44685aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4469103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4470103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4471103a8feaSLen Brown 	stepping = fms & 0xf;
4472103a8feaSLen Brown 	if (family == 6 || family == 0xf)
4473103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
4474103a8feaSLen Brown 
447596e47158SLen Brown 	if (!quiet) {
4476b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
4477103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
4478d9d226ffSLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
447969807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
448069807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
4481aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
448269807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
448369807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
448469807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
448569807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
448669807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
4487d9d226ffSLen Brown 			edx & (1 << 28) ? "HT" : "-",
448869807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
448969807a63SLen Brown 	}
4490103a8feaSLen Brown 
4491b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
4492b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
4493103a8feaSLen Brown 
4494103a8feaSLen Brown 	/*
4495103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4496103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4497103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4498103a8feaSLen Brown 	 */
4499103a8feaSLen Brown 	ebx = ecx = edx = 0;
45005aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4501103a8feaSLen Brown 
450261a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4503103a8feaSLen Brown 
4504103a8feaSLen Brown 		/*
4505103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4506103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4507103a8feaSLen Brown 		 */
45085aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
45098209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4510d7899447SLen Brown 	}
4511103a8feaSLen Brown 
4512103a8feaSLen Brown 	/*
4513103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4514103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4515103a8feaSLen Brown 	 */
4516103a8feaSLen Brown 
45175aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
45188209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4519812db3f7SLen Brown 	if (has_aperf) {
4520812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4521812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4522812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4523812db3f7SLen Brown 	}
4524889facbeSLen Brown 	do_dts = eax & (1 << 0);
4525812db3f7SLen Brown 	if (do_dts)
4526812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4527b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4528889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4529812db3f7SLen Brown 	if (do_ptm)
4530812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
45317f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
45327f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
45337f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
45347f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
45357f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4536889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4537889facbeSLen Brown 
453896e47158SLen Brown 	if (!quiet)
4539b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
45407f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
45417f5c258eSLen Brown 			has_aperf ? "" : "No-",
4542b3a34e93SLen Brown 			has_turbo ? "" : "No-",
45437f5c258eSLen Brown 			do_dts ? "" : "No-",
45447f5c258eSLen Brown 			do_ptm ? "" : "No-",
45457f5c258eSLen Brown 			has_hwp ? "" : "No-",
45467f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
45477f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
45487f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
45497f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
45507f5c258eSLen Brown 			has_epb ? "" : "No-");
4551103a8feaSLen Brown 
455296e47158SLen Brown 	if (!quiet)
455369807a63SLen Brown 		decode_misc_enable_msr();
455469807a63SLen Brown 
455533148d67SLen Brown 
455696e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4557aa8d8cc7SLen Brown 		int has_sgx;
4558aa8d8cc7SLen Brown 
4559aa8d8cc7SLen Brown 		ecx = 0;
4560aa8d8cc7SLen Brown 
4561aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4562aa8d8cc7SLen Brown 
4563aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4564aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4565aa8d8cc7SLen Brown 
4566aa8d8cc7SLen Brown 		if (has_sgx)
4567aa8d8cc7SLen Brown 			decode_feature_control_msr();
4568aa8d8cc7SLen Brown 	}
4569aa8d8cc7SLen Brown 
457061a87ba7SLen Brown 	if (max_level >= 0x15) {
45718a5bdf41SLen Brown 		unsigned int eax_crystal;
45728a5bdf41SLen Brown 		unsigned int ebx_tsc;
45738a5bdf41SLen Brown 
45748a5bdf41SLen Brown 		/*
45758a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
45768a5bdf41SLen Brown 		 */
45778a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
45785aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
45798a5bdf41SLen Brown 
45808a5bdf41SLen Brown 		if (ebx_tsc != 0) {
45818a5bdf41SLen Brown 
458296e47158SLen Brown 			if (!quiet && (ebx != 0))
4583b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
45848a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
45858a5bdf41SLen Brown 
45868a5bdf41SLen Brown 			if (crystal_hz == 0)
45878a5bdf41SLen Brown 				switch(model) {
4588869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4589869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4590869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4591869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4592e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4593e8efbc80SLen Brown 					break;
45947268d407SLen Brown 				case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
4595ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4596ec53e594SLen Brown 					break;
4597869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4598ac01ac13SLen Brown 				case INTEL_FAM6_ATOM_GEMINI_LAKE:
4599e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
46008a5bdf41SLen Brown 					break;
46018a5bdf41SLen Brown 				default:
46028a5bdf41SLen Brown 					crystal_hz = 0;
46038a5bdf41SLen Brown 			}
46048a5bdf41SLen Brown 
46058a5bdf41SLen Brown 			if (crystal_hz) {
46068a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
460796e47158SLen Brown 				if (!quiet)
4608b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
46098a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
46108a5bdf41SLen Brown 			}
46118a5bdf41SLen Brown 		}
46128a5bdf41SLen Brown 	}
461361a87ba7SLen Brown 	if (max_level >= 0x16) {
461461a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
461561a87ba7SLen Brown 
461661a87ba7SLen Brown 		/*
461761a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
461861a87ba7SLen Brown 		 */
461961a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
462061a87ba7SLen Brown 
46215aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
462296e47158SLen Brown 		if (!quiet)
4623b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
462461a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
462561a87ba7SLen Brown 	}
46268a5bdf41SLen Brown 
4627b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4628b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4629b2b34dfeSHubert Chrzaniuk 
4630812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4631812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4632812db3f7SLen Brown 
4633812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4634812db3f7SLen Brown 		do_nhm_platform_info = 1;
4635812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4636812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4637812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4638812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4639812db3f7SLen Brown 	}
4640d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4641812db3f7SLen Brown 
4642812db3f7SLen Brown 	if (do_snb_cstates)
4643812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4644812db3f7SLen Brown 
46455a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
46460f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
46470f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
46480f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
46490f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
46500f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
46510f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
46520f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
46530f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
46540539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
46550f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
46560f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
46570f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
46580f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
46590539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
46600539ba11SLen Brown 		use_c1_residency_msr = 1;
46610539ba11SLen Brown 	}
46627170a374SLen Brown 	if (is_dnv(family, model)) {
46637170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
46647170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
46657170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
46667170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
46677170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
46687170a374SLen Brown 		use_c1_residency_msr = 1;
46697170a374SLen Brown 	}
467034c76197SLen Brown 	if (is_skx(family, model)) {
467134c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
467234c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
467334c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
467434c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
467534c76197SLen Brown 	}
4676ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4677ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4678ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4679ade0ebacSLen Brown 	}
46800f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
46810f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
46820f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
46830f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
46840f47c08dSLen Brown 	}
46855a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
4686a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4687a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4688a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4689a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4690a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4691a99d8730SLen Brown 	}
4692144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4693fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4694997e5395SSrinivas Pandruvada 	do_cnl_cstates = is_cnl(family, model);
4695103a8feaSLen Brown 
469696e47158SLen Brown 	if (!quiet)
4697f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4698f0057310SLen Brown 
469996e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
470071616c8eSLen Brown 		decode_c6_demotion_policy_msr();
470171616c8eSLen Brown 
4702889facbeSLen Brown 	rapl_probe(family, model);
47033a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4704ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
4705889facbeSLen Brown 
470696e47158SLen Brown 	if (!quiet)
47071b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4708fcd17211SLen Brown 
470941618e63SLen Brown 	if (!quiet)
471041618e63SLen Brown 		dump_sysfs_cstate_config();
47117293fccdSLen Brown 	if (!quiet)
47127293fccdSLen Brown 		dump_sysfs_pstate_config();
471341618e63SLen Brown 
4714a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4715a2b7b749SLen Brown 		calculate_tsc_tweak();
4716a2b7b749SLen Brown 
4717812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4718812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4719fdf676e5SLen Brown 
4720812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4721812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
472227d47356SLen Brown 
4723be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4724be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
4725be0e54c4SLen Brown 	else
4726be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
4727be0e54c4SLen Brown 
4728be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4729be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
4730be0e54c4SLen Brown 	else
4731be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
4732be0e54c4SLen Brown 
473396e47158SLen Brown 	if (!quiet)
473433148d67SLen Brown 		decode_misc_feature_control();
473533148d67SLen Brown 
4736889facbeSLen Brown 	return;
4737103a8feaSLen Brown }
4738103a8feaSLen Brown 
4739103a8feaSLen Brown /*
4740103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4741103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4742103a8feaSLen Brown  */
4743103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4744103a8feaSLen Brown {
4745103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4746103a8feaSLen Brown 		return 1;
4747103a8feaSLen Brown 	else
4748103a8feaSLen Brown 		return 0;
4749103a8feaSLen Brown }
4750103a8feaSLen Brown 
4751103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4752103a8feaSLen Brown {
4753103a8feaSLen Brown 	return 0;
4754103a8feaSLen Brown }
4755103a8feaSLen Brown 
4756c98d5d94SLen Brown void topology_probe()
4757c98d5d94SLen Brown {
4758c98d5d94SLen Brown 	int i;
4759c98d5d94SLen Brown 	int max_core_id = 0;
4760c98d5d94SLen Brown 	int max_package_id = 0;
4761c98d5d94SLen Brown 	int max_siblings = 0;
4762c98d5d94SLen Brown 
4763c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4764843c5791SPrarit Bhargava 	set_max_cpu_num();
4765c98d5d94SLen Brown 	topo.num_cpus = 0;
4766c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4767c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4768812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4769c98d5d94SLen Brown 
4770d8af6f5fSLen Brown 	if (debug > 1)
4771b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4772c98d5d94SLen Brown 
4773c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4774b2c95d90SJosh Triplett 	if (cpus == NULL)
4775b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4776c98d5d94SLen Brown 
4777c98d5d94SLen Brown 	/*
4778c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4779c98d5d94SLen Brown 	 */
4780c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4781b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4782b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4783c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4784c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4785c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4786c98d5d94SLen Brown 
4787c98d5d94SLen Brown 	/*
47881ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
47891ef7d21aSLen Brown 	 */
47901ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
47911ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
47921ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
47931ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
47941ef7d21aSLen Brown 	}
47951ef7d21aSLen Brown 
47961ef7d21aSLen Brown 	/*
4797c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4798c98d5d94SLen Brown 	 */
4799c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4800b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4801b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4802c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4803c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4804c98d5d94SLen Brown 
48058cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
4806c98d5d94SLen Brown 
4807c98d5d94SLen Brown 	/*
4808c98d5d94SLen Brown 	 * For online cpus
4809c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4810c98d5d94SLen Brown 	 */
4811c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4812c98d5d94SLen Brown 		int siblings;
4813c98d5d94SLen Brown 
4814c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4815d8af6f5fSLen Brown 			if (debug > 1)
4816b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4817c98d5d94SLen Brown 			continue;
4818c98d5d94SLen Brown 		}
4819c98d5d94SLen Brown 
48200e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
48210e2d8f05SLen Brown 
48220e2d8f05SLen Brown 		/* get package information */
4823c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4824c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4825c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4826c98d5d94SLen Brown 
48270e2d8f05SLen Brown 		/* get numa node information */
4828ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4829ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
4830ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
48310e2d8f05SLen Brown 
48320e2d8f05SLen Brown 		/* get core information */
48330e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
48340e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
48350e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
48360e2d8f05SLen Brown 
48370e2d8f05SLen Brown 		/* get thread information */
48380e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
4839c98d5d94SLen Brown 		if (siblings > max_siblings)
4840c98d5d94SLen Brown 			max_siblings = siblings;
48418cb48b32SPrarit Bhargava 		if (cpus[i].thread_id != -1)
48428cb48b32SPrarit Bhargava 			topo.num_cores++;
48430e2d8f05SLen Brown 
4844d8af6f5fSLen Brown 		if (debug > 1)
48458cb48b32SPrarit Bhargava 			fprintf(outf,
48468cb48b32SPrarit Bhargava 				"cpu %d pkg %d node %d core %d thread %d\n",
48470e2d8f05SLen Brown 				i, cpus[i].physical_package_id,
4848ef605741SPrarit Bhargava 				cpus[i].physical_node_id,
48498cb48b32SPrarit Bhargava 				cpus[i].physical_core_id,
48508cb48b32SPrarit Bhargava 				cpus[i].thread_id);
4851c98d5d94SLen Brown 	}
4852ef605741SPrarit Bhargava 
485370a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
4854d8af6f5fSLen Brown 	if (debug > 1)
4855b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
485670a9c6e8SPrarit Bhargava 			max_core_id, topo.cores_per_node);
485770a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
4858812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
4859c98d5d94SLen Brown 
4860c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
4861d8af6f5fSLen Brown 	if (debug > 1)
4862b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4863c98d5d94SLen Brown 			max_package_id, topo.num_packages);
48647da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
4865812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
4866c98d5d94SLen Brown 
4867ef605741SPrarit Bhargava 	set_node_data();
4868ef605741SPrarit Bhargava 	if (debug > 1)
486970a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
487001235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
487101235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
4872ef605741SPrarit Bhargava 
487370a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
4874d8af6f5fSLen Brown 	if (debug > 1)
4875b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
4876c98d5d94SLen Brown }
4877c98d5d94SLen Brown 
4878c98d5d94SLen Brown void
487940f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c,
488040f5cfe7SPrarit Bhargava 		  struct pkg_data **p)
4881c98d5d94SLen Brown {
4882c98d5d94SLen Brown 	int i;
488340f5cfe7SPrarit Bhargava 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
488440f5cfe7SPrarit Bhargava 			topo.num_packages;
488540f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
4886c98d5d94SLen Brown 
488740f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
4888c98d5d94SLen Brown 	if (*t == NULL)
4889c98d5d94SLen Brown 		goto error;
4890c98d5d94SLen Brown 
489140f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
4892c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
4893c98d5d94SLen Brown 
489440f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
4895c98d5d94SLen Brown 	if (*c == NULL)
4896c98d5d94SLen Brown 		goto error;
4897c98d5d94SLen Brown 
489840f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
4899c98d5d94SLen Brown 		(*c)[i].core_id = -1;
4900c98d5d94SLen Brown 
4901678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
4902c98d5d94SLen Brown 	if (*p == NULL)
4903c98d5d94SLen Brown 		goto error;
4904c98d5d94SLen Brown 
4905c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
4906c98d5d94SLen Brown 		(*p)[i].package_id = i;
4907c98d5d94SLen Brown 
4908c98d5d94SLen Brown 	return;
4909c98d5d94SLen Brown error:
4910b2c95d90SJosh Triplett 	err(1, "calloc counters");
4911c98d5d94SLen Brown }
4912c98d5d94SLen Brown /*
4913c98d5d94SLen Brown  * init_counter()
4914c98d5d94SLen Brown  *
4915c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4916c98d5d94SLen Brown  */
4917c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
49188cb48b32SPrarit Bhargava 	struct pkg_data *pkg_base, int cpu_id)
4919c98d5d94SLen Brown {
49208cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
492140f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
49228cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
49238cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
4924c98d5d94SLen Brown 	struct thread_data *t;
4925c98d5d94SLen Brown 	struct core_data *c;
4926c98d5d94SLen Brown 	struct pkg_data *p;
4927c98d5d94SLen Brown 
492842dd4520SNathan Ciobanu 
492942dd4520SNathan Ciobanu 	/* Workaround for systems where physical_node_id==-1
493042dd4520SNathan Ciobanu 	 * and logical_node_id==(-1 - topo.num_cpus)
493142dd4520SNathan Ciobanu 	 */
493242dd4520SNathan Ciobanu 	if (node_id < 0)
493342dd4520SNathan Ciobanu 		node_id = 0;
493442dd4520SNathan Ciobanu 
493540f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
493640f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
49378cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
4938c98d5d94SLen Brown 
4939c98d5d94SLen Brown 	t->cpu_id = cpu_id;
49408cb48b32SPrarit Bhargava 	if (thread_id == 0) {
4941c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4942c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
4943c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4944c98d5d94SLen Brown 	}
4945c98d5d94SLen Brown 
49468cb48b32SPrarit Bhargava 	c->core_id = core_id;
49478cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
4948c98d5d94SLen Brown }
4949c98d5d94SLen Brown 
4950c98d5d94SLen Brown 
4951c98d5d94SLen Brown int initialize_counters(int cpu_id)
4952c98d5d94SLen Brown {
49538cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
49548cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
4955c98d5d94SLen Brown 	return 0;
4956c98d5d94SLen Brown }
4957c98d5d94SLen Brown 
4958c98d5d94SLen Brown void allocate_output_buffer()
4959c98d5d94SLen Brown {
49603b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4961c98d5d94SLen Brown 	outp = output_buffer;
4962b2c95d90SJosh Triplett 	if (outp == NULL)
4963b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
4964c98d5d94SLen Brown }
496536229897SLen Brown void allocate_fd_percpu(void)
496636229897SLen Brown {
496701a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
496836229897SLen Brown 	if (fd_percpu == NULL)
496936229897SLen Brown 		err(-1, "calloc fd_percpu");
497036229897SLen Brown }
4971562a2d37SLen Brown void allocate_irq_buffers(void)
4972562a2d37SLen Brown {
4973562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4974562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
4975562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
4976c98d5d94SLen Brown 
497701a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4978562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
497901a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
4980562a2d37SLen Brown }
4981c98d5d94SLen Brown void setup_all_buffers(void)
4982c98d5d94SLen Brown {
4983c98d5d94SLen Brown 	topology_probe();
4984562a2d37SLen Brown 	allocate_irq_buffers();
498536229897SLen Brown 	allocate_fd_percpu();
4986c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
4987c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
4988c98d5d94SLen Brown 	allocate_output_buffer();
4989c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
4990c98d5d94SLen Brown }
49913b4d5c7fSAndy Shevchenko 
49927ce7d5deSPrarit Bhargava void set_base_cpu(void)
49937ce7d5deSPrarit Bhargava {
49947ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
49957ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
49967ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
49977ce7d5deSPrarit Bhargava 
49987ce7d5deSPrarit Bhargava 	if (debug > 1)
4999b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
50007ce7d5deSPrarit Bhargava }
50017ce7d5deSPrarit Bhargava 
5002103a8feaSLen Brown void turbostat_init()
5003103a8feaSLen Brown {
50047ce7d5deSPrarit Bhargava 	setup_all_buffers();
50057ce7d5deSPrarit Bhargava 	set_base_cpu();
5006103a8feaSLen Brown 	check_dev_msr();
500798481e79SLen Brown 	check_permissions();
5008fcd17211SLen Brown 	process_cpuid();
5009103a8feaSLen Brown 
5010103a8feaSLen Brown 
501196e47158SLen Brown 	if (!quiet)
50127f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
50137f5c258eSLen Brown 
501496e47158SLen Brown 	if (!quiet)
5015889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
5016889facbeSLen Brown 
501796e47158SLen Brown 	if (!quiet)
50183a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
50193a9a941dSLen Brown 
502096e47158SLen Brown 	if (!quiet)
5021889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
5022889facbeSLen Brown 
5023889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
5024889facbeSLen Brown 
502596e47158SLen Brown 	if (!quiet)
5026889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
50275a63426eSLen Brown 
502896e47158SLen Brown 	if (!quiet && do_irtl_snb)
50295a63426eSLen Brown 		print_irtl();
5030103a8feaSLen Brown }
5031103a8feaSLen Brown 
5032103a8feaSLen Brown int fork_it(char **argv)
5033103a8feaSLen Brown {
5034103a8feaSLen Brown 	pid_t child_pid;
5035d91bb17cSLen Brown 	int status;
5036d15cf7c1SLen Brown 
5037218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5038d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
50394c2122d4SLen Brown 	first_counter_read = 0;
5040d91bb17cSLen Brown 	if (status)
5041d91bb17cSLen Brown 		exit(status);
5042d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
5043d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5044103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
5045103a8feaSLen Brown 
5046103a8feaSLen Brown 	child_pid = fork();
5047103a8feaSLen Brown 	if (!child_pid) {
5048103a8feaSLen Brown 		/* child */
5049103a8feaSLen Brown 		execvp(argv[0], argv);
50500815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
5051103a8feaSLen Brown 	} else {
5052103a8feaSLen Brown 
5053103a8feaSLen Brown 		/* parent */
5054b2c95d90SJosh Triplett 		if (child_pid == -1)
5055b2c95d90SJosh Triplett 			err(1, "fork");
5056103a8feaSLen Brown 
5057103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
5058103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
5059b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
5060b2c95d90SJosh Triplett 			err(status, "waitpid");
5061103a8feaSLen Brown 	}
5062c98d5d94SLen Brown 	/*
5063c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
5064c98d5d94SLen Brown 	 * because re-starting is problematic when forking
5065c98d5d94SLen Brown 	 */
5066218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5067c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
5068103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
5069103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
5070ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5071ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
5072ba3dec99SLen Brown 	else {
5073c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
5074c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
5075ba3dec99SLen Brown 	}
5076103a8feaSLen Brown 
5077b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5078b7d8c148SLen Brown 
5079b7d8c148SLen Brown 	flush_output_stderr();
5080103a8feaSLen Brown 
5081d91bb17cSLen Brown 	return status;
5082103a8feaSLen Brown }
5083103a8feaSLen Brown 
50843b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
50853b4d5c7fSAndy Shevchenko {
50863b4d5c7fSAndy Shevchenko 	int status;
50873b4d5c7fSAndy Shevchenko 
5088218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
50893b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
50903b4d5c7fSAndy Shevchenko 	if (status)
50913b4d5c7fSAndy Shevchenko 		return status;
50923b4d5c7fSAndy Shevchenko 
50933b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
50943b4d5c7fSAndy Shevchenko 	if (status)
50953b4d5c7fSAndy Shevchenko 		return status;
50963b4d5c7fSAndy Shevchenko 
5097b7d8c148SLen Brown 	flush_output_stdout();
50983b4d5c7fSAndy Shevchenko 
50993b4d5c7fSAndy Shevchenko 	return status;
51003b4d5c7fSAndy Shevchenko }
51013b4d5c7fSAndy Shevchenko 
5102d8af6f5fSLen Brown void print_version() {
510373780cd8SLen Brown 	fprintf(outf, "turbostat version 18.06.20"
5104d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
5105d8af6f5fSLen Brown }
5106d8af6f5fSLen Brown 
5107495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
5108495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
510941618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
5110388e9c81SLen Brown {
5111388e9c81SLen Brown 	struct msr_counter *msrp;
5112388e9c81SLen Brown 
5113388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
5114388e9c81SLen Brown 	if (msrp == NULL) {
5115388e9c81SLen Brown 		perror("calloc");
5116388e9c81SLen Brown 		exit(1);
5117388e9c81SLen Brown 	}
5118388e9c81SLen Brown 
5119388e9c81SLen Brown 	msrp->msr_num = msr_num;
5120388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
5121495c7654SLen Brown 	if (path)
5122495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
5123388e9c81SLen Brown 	msrp->width = width;
5124388e9c81SLen Brown 	msrp->type = type;
5125388e9c81SLen Brown 	msrp->format = format;
512641618e63SLen Brown 	msrp->flags = flags;
5127388e9c81SLen Brown 
5128388e9c81SLen Brown 	switch (scope) {
5129388e9c81SLen Brown 
5130388e9c81SLen Brown 	case SCOPE_CPU:
5131388e9c81SLen Brown 		msrp->next = sys.tp;
5132388e9c81SLen Brown 		sys.tp = msrp;
5133678a3bd1SLen Brown 		sys.added_thread_counters++;
51340748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5135678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
5136678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5137678a3bd1SLen Brown 			exit(-1);
5138678a3bd1SLen Brown 		}
5139388e9c81SLen Brown 		break;
5140388e9c81SLen Brown 
5141388e9c81SLen Brown 	case SCOPE_CORE:
5142388e9c81SLen Brown 		msrp->next = sys.cp;
5143388e9c81SLen Brown 		sys.cp = msrp;
5144678a3bd1SLen Brown 		sys.added_core_counters++;
5145678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5146678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
5147678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5148678a3bd1SLen Brown 			exit(-1);
5149678a3bd1SLen Brown 		}
5150388e9c81SLen Brown 		break;
5151388e9c81SLen Brown 
5152388e9c81SLen Brown 	case SCOPE_PACKAGE:
5153388e9c81SLen Brown 		msrp->next = sys.pp;
5154388e9c81SLen Brown 		sys.pp = msrp;
5155678a3bd1SLen Brown 		sys.added_package_counters++;
5156678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5157678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
5158678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5159678a3bd1SLen Brown 			exit(-1);
5160678a3bd1SLen Brown 		}
5161388e9c81SLen Brown 		break;
5162388e9c81SLen Brown 	}
5163388e9c81SLen Brown 
5164388e9c81SLen Brown 	return 0;
5165388e9c81SLen Brown }
5166388e9c81SLen Brown 
5167388e9c81SLen Brown void parse_add_command(char *add_command)
5168388e9c81SLen Brown {
5169388e9c81SLen Brown 	int msr_num = 0;
5170495c7654SLen Brown 	char *path = NULL;
51710f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
5172388e9c81SLen Brown 	int width = 64;
5173388e9c81SLen Brown 	int fail = 0;
5174388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
5175388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
5176388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
5177388e9c81SLen Brown 
5178388e9c81SLen Brown 	while (add_command) {
5179388e9c81SLen Brown 
5180388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5181388e9c81SLen Brown 			goto next;
5182388e9c81SLen Brown 
5183388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
5184388e9c81SLen Brown 			goto next;
5185388e9c81SLen Brown 
5186495c7654SLen Brown 		if (*add_command == '/') {
5187495c7654SLen Brown 			path = add_command;
5188495c7654SLen Brown 			goto next;
5189495c7654SLen Brown 		}
5190495c7654SLen Brown 
5191388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
5192388e9c81SLen Brown 			if ((width == 32) || (width == 64))
5193388e9c81SLen Brown 				goto next;
5194388e9c81SLen Brown 			width = 64;
5195388e9c81SLen Brown 		}
5196388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5197388e9c81SLen Brown 			scope = SCOPE_CPU;
5198388e9c81SLen Brown 			goto next;
5199388e9c81SLen Brown 		}
5200388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
5201388e9c81SLen Brown 			scope = SCOPE_CORE;
5202388e9c81SLen Brown 			goto next;
5203388e9c81SLen Brown 		}
5204388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
5205388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
5206388e9c81SLen Brown 			goto next;
5207388e9c81SLen Brown 		}
5208388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5209388e9c81SLen Brown 			type = COUNTER_CYCLES;
5210388e9c81SLen Brown 			goto next;
5211388e9c81SLen Brown 		}
5212388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5213388e9c81SLen Brown 			type = COUNTER_SECONDS;
5214388e9c81SLen Brown 			goto next;
5215388e9c81SLen Brown 		}
521641618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
521741618e63SLen Brown 			type = COUNTER_USEC;
521841618e63SLen Brown 			goto next;
521941618e63SLen Brown 		}
5220388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
5221388e9c81SLen Brown 			format = FORMAT_RAW;
5222388e9c81SLen Brown 			goto next;
5223388e9c81SLen Brown 		}
5224388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
5225388e9c81SLen Brown 			format = FORMAT_DELTA;
5226388e9c81SLen Brown 			goto next;
5227388e9c81SLen Brown 		}
5228388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
5229388e9c81SLen Brown 			format = FORMAT_PERCENT;
5230388e9c81SLen Brown 			goto next;
5231388e9c81SLen Brown 		}
5232388e9c81SLen Brown 
5233388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
5234388e9c81SLen Brown 			char *eos;
5235388e9c81SLen Brown 
5236388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
5237388e9c81SLen Brown 			if (eos)
5238388e9c81SLen Brown 				*eos = '\0';
5239388e9c81SLen Brown 			goto next;
5240388e9c81SLen Brown 		}
5241388e9c81SLen Brown 
5242388e9c81SLen Brown next:
5243388e9c81SLen Brown 		add_command = strchr(add_command, ',');
5244495c7654SLen Brown 		if (add_command) {
5245495c7654SLen Brown 			*add_command = '\0';
5246388e9c81SLen Brown 			add_command++;
5247495c7654SLen Brown 		}
5248388e9c81SLen Brown 
5249388e9c81SLen Brown 	}
5250495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
5251495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
5252388e9c81SLen Brown 		fail++;
5253388e9c81SLen Brown 	}
5254388e9c81SLen Brown 
5255388e9c81SLen Brown 	/* generate default column header */
5256388e9c81SLen Brown 	if (*name_buffer == '\0') {
5257388e9c81SLen Brown 		if (width == 32)
52585f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5259388e9c81SLen Brown 		else
52605f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5261388e9c81SLen Brown 	}
5262388e9c81SLen Brown 
526341618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
5264388e9c81SLen Brown 		fail++;
5265388e9c81SLen Brown 
5266388e9c81SLen Brown 	if (fail) {
5267388e9c81SLen Brown 		help();
5268388e9c81SLen Brown 		exit(1);
5269388e9c81SLen Brown 	}
5270388e9c81SLen Brown }
527141618e63SLen Brown 
5272dd778a5eSLen Brown int is_deferred_skip(char *name)
5273dd778a5eSLen Brown {
5274dd778a5eSLen Brown 	int i;
5275dd778a5eSLen Brown 
5276dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
5277dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
5278dd778a5eSLen Brown 			return 1;
5279dd778a5eSLen Brown 	return 0;
5280dd778a5eSLen Brown }
5281dd778a5eSLen Brown 
528241618e63SLen Brown void probe_sysfs(void)
528341618e63SLen Brown {
528441618e63SLen Brown 	char path[64];
528541618e63SLen Brown 	char name_buf[16];
528641618e63SLen Brown 	FILE *input;
528741618e63SLen Brown 	int state;
528841618e63SLen Brown 	char *sp;
528941618e63SLen Brown 
529041618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
529141618e63SLen Brown 		return;
529241618e63SLen Brown 
52930748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
529441618e63SLen Brown 
529541618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
529641618e63SLen Brown 			base_cpu, state);
529741618e63SLen Brown 		input = fopen(path, "r");
529841618e63SLen Brown 		if (input == NULL)
529941618e63SLen Brown 			continue;
530041618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
530141618e63SLen Brown 
530241618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
530341618e63SLen Brown 		sp = strchr(name_buf, '-');
530441618e63SLen Brown 		if (!sp)
530541618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
530641618e63SLen Brown 		*sp = '%';
530741618e63SLen Brown 		*(sp + 1) = '\0';
530841618e63SLen Brown 
530941618e63SLen Brown 		fclose(input);
531041618e63SLen Brown 
531141618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
531241618e63SLen Brown 
5313dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5314dd778a5eSLen Brown 			continue;
5315dd778a5eSLen Brown 
531641618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
531741618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
531841618e63SLen Brown 	}
531941618e63SLen Brown 
53200748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
532141618e63SLen Brown 
532241618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
532341618e63SLen Brown 			base_cpu, state);
532441618e63SLen Brown 		input = fopen(path, "r");
532541618e63SLen Brown 		if (input == NULL)
532641618e63SLen Brown 			continue;
532741618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
532841618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
532941618e63SLen Brown 		sp = strchr(name_buf, '-');
533041618e63SLen Brown 		if (!sp)
533141618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
533241618e63SLen Brown 		*sp = '\0';
533341618e63SLen Brown 		fclose(input);
533441618e63SLen Brown 
533541618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
533641618e63SLen Brown 
5337dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5338dd778a5eSLen Brown 			continue;
5339dd778a5eSLen Brown 
534041618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
534141618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
534241618e63SLen Brown 	}
534341618e63SLen Brown 
534441618e63SLen Brown }
534541618e63SLen Brown 
53461ef7d21aSLen Brown 
53471ef7d21aSLen Brown /*
53481ef7d21aSLen Brown  * parse cpuset with following syntax
53491ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
53501ef7d21aSLen Brown  */
53511ef7d21aSLen Brown void parse_cpu_command(char *optarg)
53521ef7d21aSLen Brown {
53531ef7d21aSLen Brown 	unsigned int start, end;
53541ef7d21aSLen Brown 	char *next;
53551ef7d21aSLen Brown 
53564e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
53574e4e1e7cSLen Brown 		if (cpu_subset)
53584e4e1e7cSLen Brown 			goto error;
53594e4e1e7cSLen Brown 		show_core_only++;
53604e4e1e7cSLen Brown 		return;
53614e4e1e7cSLen Brown 	}
53624e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
53634e4e1e7cSLen Brown 		if (cpu_subset)
53644e4e1e7cSLen Brown 			goto error;
53654e4e1e7cSLen Brown 		show_pkg_only++;
53664e4e1e7cSLen Brown 		return;
53674e4e1e7cSLen Brown 	}
53684e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
53694e4e1e7cSLen Brown 		goto error;
53704e4e1e7cSLen Brown 
53711ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
53721ef7d21aSLen Brown 	if (cpu_subset == NULL)
53731ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
53741ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
53751ef7d21aSLen Brown 
53761ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
53771ef7d21aSLen Brown 
53781ef7d21aSLen Brown 	next = optarg;
53791ef7d21aSLen Brown 
53801ef7d21aSLen Brown 	while (next && *next) {
53811ef7d21aSLen Brown 
53821ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
53831ef7d21aSLen Brown 			goto error;
53841ef7d21aSLen Brown 
53851ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
53861ef7d21aSLen Brown 
53871ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
53881ef7d21aSLen Brown 			goto error;
53891ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
53901ef7d21aSLen Brown 
53911ef7d21aSLen Brown 		if (*next == '\0')
53921ef7d21aSLen Brown 			break;
53931ef7d21aSLen Brown 
53941ef7d21aSLen Brown 		if (*next == ',') {
53951ef7d21aSLen Brown 			next += 1;
53961ef7d21aSLen Brown 			continue;
53971ef7d21aSLen Brown 		}
53981ef7d21aSLen Brown 
53991ef7d21aSLen Brown 		if (*next == '-') {
54001ef7d21aSLen Brown 			next += 1;	/* start range */
54011ef7d21aSLen Brown 		} else if (*next == '.') {
54021ef7d21aSLen Brown 			next += 1;
54031ef7d21aSLen Brown 			if (*next == '.')
54041ef7d21aSLen Brown 				next += 1;	/* start range */
54051ef7d21aSLen Brown 			else
54061ef7d21aSLen Brown 				goto error;
54071ef7d21aSLen Brown 		}
54081ef7d21aSLen Brown 
54091ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
54101ef7d21aSLen Brown 		if (end <= start)
54111ef7d21aSLen Brown 			goto error;
54121ef7d21aSLen Brown 
54131ef7d21aSLen Brown 		while (++start <= end) {
54141ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
54151ef7d21aSLen Brown 				goto error;
54161ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
54171ef7d21aSLen Brown 		}
54181ef7d21aSLen Brown 
54191ef7d21aSLen Brown 		if (*next == ',')
54201ef7d21aSLen Brown 			next += 1;
54211ef7d21aSLen Brown 		else if (*next != '\0')
54221ef7d21aSLen Brown 			goto error;
54231ef7d21aSLen Brown 	}
54241ef7d21aSLen Brown 
54251ef7d21aSLen Brown 	return;
54261ef7d21aSLen Brown 
54271ef7d21aSLen Brown error:
54284e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
54294e4e1e7cSLen Brown 	help();
54301ef7d21aSLen Brown 	exit(-1);
54311ef7d21aSLen Brown }
54321ef7d21aSLen Brown 
5433812db3f7SLen Brown 
5434103a8feaSLen Brown void cmdline(int argc, char **argv)
5435103a8feaSLen Brown {
5436103a8feaSLen Brown 	int opt;
5437d8af6f5fSLen Brown 	int option_index = 0;
5438d8af6f5fSLen Brown 	static struct option long_options[] = {
5439388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
54401ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5441d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
544296e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
54433f44a5c6SLen Brown 		{"enable",	required_argument,	0, 'e'},
5444d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5445023fe0acSChen Yu 		{"num_iterations",	required_argument,	0, 'n'},
5446d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5447812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5448d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5449c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5450b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
545196e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5452812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5453d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5454d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5455d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5456d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5457d8af6f5fSLen Brown 	};
5458103a8feaSLen Brown 
5459103a8feaSLen Brown 	progname = argv[0];
5460103a8feaSLen Brown 
5461023fe0acSChen Yu 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
5462d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5463103a8feaSLen Brown 		switch (opt) {
5464388e9c81SLen Brown 		case 'a':
5465388e9c81SLen Brown 			parse_add_command(optarg);
5466388e9c81SLen Brown 			break;
54671ef7d21aSLen Brown 		case 'c':
54681ef7d21aSLen Brown 			parse_cpu_command(optarg);
54691ef7d21aSLen Brown 			break;
5470d8af6f5fSLen Brown 		case 'D':
5471d8af6f5fSLen Brown 			dump_only++;
54728e180f3cSLen Brown 			break;
54733f44a5c6SLen Brown 		case 'e':
54743f44a5c6SLen Brown 			/* --enable specified counter */
54754c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
54763f44a5c6SLen Brown 			break;
5477d8af6f5fSLen Brown 		case 'd':
5478d8af6f5fSLen Brown 			debug++;
54793f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
54802f32edf1SLen Brown 			break;
5481812db3f7SLen Brown 		case 'H':
54823f44a5c6SLen Brown 			/*
54833f44a5c6SLen Brown 			 * --hide: do not show those specified
54843f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
54853f44a5c6SLen Brown 			 */
54863f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5487812db3f7SLen Brown 			break;
5488d8af6f5fSLen Brown 		case 'h':
5489d8af6f5fSLen Brown 		default:
5490d8af6f5fSLen Brown 			help();
5491d8af6f5fSLen Brown 			exit(1);
5492d8af6f5fSLen Brown 		case 'i':
54932a0609c0SLen Brown 			{
54942a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
54952a0609c0SLen Brown 
54962a0609c0SLen Brown 				if (interval < 0.001) {
5497b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
54982a0609c0SLen Brown 						interval);
54992a0609c0SLen Brown 					exit(2);
55002a0609c0SLen Brown 				}
55012a0609c0SLen Brown 
550247936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
5503b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
550447936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
55052a0609c0SLen Brown 			}
5506889facbeSLen Brown 			break;
55075c56be9aSDirk Brandewie 		case 'J':
55085c56be9aSDirk Brandewie 			rapl_joules++;
55095c56be9aSDirk Brandewie 			break;
5510c8ade361SLen Brown 		case 'l':
55113f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5512c8ade361SLen Brown 			list_header_only++;
5513c8ade361SLen Brown 			quiet++;
5514c8ade361SLen Brown 			break;
5515b7d8c148SLen Brown 		case 'o':
5516b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5517b7d8c148SLen Brown 			break;
551896e47158SLen Brown 		case 'q':
551996e47158SLen Brown 			quiet = 1;
552096e47158SLen Brown 			break;
5521023fe0acSChen Yu 		case 'n':
5522023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
5523023fe0acSChen Yu 
5524023fe0acSChen Yu 			if (num_iterations <= 0) {
5525023fe0acSChen Yu 				fprintf(outf, "iterations %d should be positive number\n",
5526023fe0acSChen Yu 					num_iterations);
5527023fe0acSChen Yu 				exit(2);
5528023fe0acSChen Yu 			}
5529023fe0acSChen Yu 			break;
5530812db3f7SLen Brown 		case 's':
55313f44a5c6SLen Brown 			/*
55323f44a5c6SLen Brown 			 * --show: show only those specified
55333f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
55343f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
55353f44a5c6SLen Brown 			 */
55363f44a5c6SLen Brown 			if (shown == 0)
55373f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
55383f44a5c6SLen Brown 			else
55393f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
55403f44a5c6SLen Brown 			shown = 1;
5541812db3f7SLen Brown 			break;
5542d8af6f5fSLen Brown 		case 'S':
5543d8af6f5fSLen Brown 			summary_only++;
5544d8af6f5fSLen Brown 			break;
5545d8af6f5fSLen Brown 		case 'T':
5546d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5547d8af6f5fSLen Brown 			break;
5548d8af6f5fSLen Brown 		case 'v':
5549d8af6f5fSLen Brown 			print_version();
5550d8af6f5fSLen Brown 			exit(0);
5551d8af6f5fSLen Brown 			break;
5552103a8feaSLen Brown 		}
5553103a8feaSLen Brown 	}
5554103a8feaSLen Brown }
5555103a8feaSLen Brown 
5556103a8feaSLen Brown int main(int argc, char **argv)
5557103a8feaSLen Brown {
5558b7d8c148SLen Brown 	outf = stderr;
5559103a8feaSLen Brown 	cmdline(argc, argv);
5560103a8feaSLen Brown 
556196e47158SLen Brown 	if (!quiet)
5562d8af6f5fSLen Brown 		print_version();
5563103a8feaSLen Brown 
556441618e63SLen Brown 	probe_sysfs();
556541618e63SLen Brown 
5566103a8feaSLen Brown 	turbostat_init();
5567103a8feaSLen Brown 
55683b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
55693b4d5c7fSAndy Shevchenko 	if (dump_only)
55703b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
55713b4d5c7fSAndy Shevchenko 
5572c8ade361SLen Brown 	/* list header and exit */
5573c8ade361SLen Brown 	if (list_header_only) {
5574c8ade361SLen Brown 		print_header(",");
5575c8ade361SLen Brown 		flush_output_stdout();
5576c8ade361SLen Brown 		return 0;
5577c8ade361SLen Brown 	}
5578c8ade361SLen Brown 
5579103a8feaSLen Brown 	/*
5580103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5581103a8feaSLen Brown 	 */
5582103a8feaSLen Brown 	if (argc - optind)
5583103a8feaSLen Brown 		return fork_it(argv + optind);
5584103a8feaSLen Brown 	else
5585103a8feaSLen Brown 		turbostat_loop();
5586103a8feaSLen Brown 
5587103a8feaSLen Brown 	return 0;
5588103a8feaSLen Brown }
5589