xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 4c2122d42116ebaa1665ad0fbef2c558fdc0e3c6)
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;
112*4c2122d4SLen 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;
174*4c2122d4SLen Brown 	unsigned int apic_id;
175*4c2122d4SLen 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" },
441*4c2122d4SLen Brown 	{ 0x0, "APIC" },
442*4c2122d4SLen 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)
494*4c2122d4SLen Brown #define	BIC_APIC	(1ULL << 48)
495*4c2122d4SLen Brown #define	BIC_X2APIC	(1ULL << 49)
496812db3f7SLen Brown 
497*4c2122d4SLen 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);
500*4c2122d4SLen 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"
527dd778a5eSLen Brown 	"--add		add a counter\n"
528dd778a5eSLen Brown 	"		eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
529dd778a5eSLen Brown 	"--cpu	cpu-set	limit output to summary plus cpu-set:\n"
530dd778a5eSLen Brown 	"		{core | package | j,k,l..m,n-p }\n"
531dd778a5eSLen Brown 	"--quiet	skip decoding system configuration header\n"
532b9ad8ee0SLen Brown 	"--interval sec.subsec	Override default 5-second measurement interval\n"
533dd778a5eSLen Brown 	"--help		print this help message\n"
534dd778a5eSLen Brown 	"--list		list column headers only\n"
535023fe0acSChen Yu 	"--num_iterations num   number of the measurement iterations\n"
536dd778a5eSLen Brown 	"--out file	create or truncate \"file\" for all output\n"
537dd778a5eSLen Brown 	"--version	print version information\n"
538dd778a5eSLen Brown 	"\n"
539dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
540dd778a5eSLen Brown }
541dd778a5eSLen Brown 
542812db3f7SLen Brown /*
543812db3f7SLen Brown  * bic_lookup
544812db3f7SLen Brown  * for all the strings in comma separate name_list,
545812db3f7SLen Brown  * set the approprate bit in return value.
546812db3f7SLen Brown  */
547dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
548812db3f7SLen Brown {
549812db3f7SLen Brown 	int i;
550812db3f7SLen Brown 	unsigned long long retval = 0;
551812db3f7SLen Brown 
552812db3f7SLen Brown 	while (name_list) {
553812db3f7SLen Brown 		char *comma;
554812db3f7SLen Brown 
555812db3f7SLen Brown 		comma = strchr(name_list, ',');
556812db3f7SLen Brown 
557812db3f7SLen Brown 		if (comma)
558812db3f7SLen Brown 			*comma = '\0';
559812db3f7SLen Brown 
5603f44a5c6SLen Brown 		if (!strcmp(name_list, "all"))
5613f44a5c6SLen Brown 			return ~0;
5623f44a5c6SLen Brown 
563812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
564812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
565812db3f7SLen Brown 				retval |= (1ULL << i);
566812db3f7SLen Brown 				break;
567812db3f7SLen Brown 			}
568812db3f7SLen Brown 		}
569812db3f7SLen Brown 		if (i == MAX_BIC) {
570dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
571812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
572812db3f7SLen Brown 				exit(-1);
573812db3f7SLen Brown 			}
574dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
575dd778a5eSLen Brown 			if (debug)
576dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
577dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
578dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
579dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
580dd778a5eSLen Brown 				help();
581dd778a5eSLen Brown 				exit(1);
582dd778a5eSLen Brown 			}
583dd778a5eSLen Brown 		}
584812db3f7SLen Brown 
585812db3f7SLen Brown 		name_list = comma;
586812db3f7SLen Brown 		if (name_list)
587812db3f7SLen Brown 			name_list++;
588812db3f7SLen Brown 
589812db3f7SLen Brown 	}
590812db3f7SLen Brown 	return retval;
591812db3f7SLen Brown }
592fc04cc67SLen Brown 
593dd778a5eSLen Brown 
594c8ade361SLen Brown void print_header(char *delim)
595103a8feaSLen Brown {
596388e9c81SLen Brown 	struct msr_counter *mp;
5976168c2e0SLen Brown 	int printed = 0;
598388e9c81SLen Brown 
5993f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
6003f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
6013f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
6023f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
603812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
6046168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
60501235041SPrarit Bhargava 	if (DO_BIC(BIC_Node))
60601235041SPrarit Bhargava 		outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
607812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
6086168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
609812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
6106168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
611*4c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
612*4c2122d4SLen Brown 		outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
613*4c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
614*4c2122d4SLen Brown 		outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
615812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
6166168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
617812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
6186168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
619812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
6206168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
621812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
6226168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
6231cc21f7bSLen Brown 
6240de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
6250de6c0dfSLen Brown 		if (sums_need_wide_columns)
6266168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
6270de6c0dfSLen Brown 		else
6286168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
6290de6c0dfSLen Brown 	}
6300de6c0dfSLen Brown 
631812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
6326168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
6331cc21f7bSLen Brown 
634388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
635dd778a5eSLen Brown 
636388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
637388e9c81SLen Brown 			if (mp->width == 64)
638dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
639388e9c81SLen Brown 			else
640dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
641388e9c81SLen Brown 		} else {
6420de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
643dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
6440de6c0dfSLen Brown 			else
645dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
646388e9c81SLen Brown 		}
647388e9c81SLen Brown 	}
648388e9c81SLen Brown 
64941618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
6506168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
651997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
6526168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
653812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
6546168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
655812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
6566168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
657678a3bd1SLen Brown 
6580539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
6596168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
660678a3bd1SLen Brown 
661812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
6626168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
663388e9c81SLen Brown 
664388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
665388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
666388e9c81SLen Brown 			if (mp->width == 64)
667c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
668388e9c81SLen Brown 			else
669c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
670388e9c81SLen Brown 		} else {
6710de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6720de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6730de6c0dfSLen Brown 			else
674c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
675388e9c81SLen Brown 		}
676388e9c81SLen Brown 	}
677388e9c81SLen Brown 
678812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
6796168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
680889facbeSLen Brown 
681812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
6826168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
683fdf676e5SLen Brown 
684812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
6856168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
68627d47356SLen Brown 
687a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
6886168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
689a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
6906168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
691a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
6926168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
693a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
6946168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
6950b2bb692SLen Brown 
6960f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
6976168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
6980f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
6996168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
7000f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
7016168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
7020f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
7036168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
7040f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
7056168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
7060f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
7076168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
7080f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
7096168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
710be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
711be0e54c4SLen Brown 		outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
712be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
713be0e54c4SLen Brown 		outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
714103a8feaSLen Brown 
7155c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
716812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
7176168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
718812db3f7SLen Brown 		if (DO_BIC(BIC_CorWatt))
7196168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
720812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
7216168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
722812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
7236168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
724812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7256168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
726812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7276168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
728d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
729812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
7306168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
731812db3f7SLen Brown 		if (DO_BIC(BIC_Cor_J))
7326168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
733812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
7346168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
735812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
7366168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
737812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7386168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
739812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7406168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
7415c56be9aSDirk Brandewie 	}
742388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
743388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
744388e9c81SLen Brown 			if (mp->width == 64)
745c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
746388e9c81SLen Brown 			else
747c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
748388e9c81SLen Brown 		} else {
7490de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7500de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7510de6c0dfSLen Brown 			else
752c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
753388e9c81SLen Brown 		}
754388e9c81SLen Brown 	}
755388e9c81SLen Brown 
756c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
757103a8feaSLen Brown }
758103a8feaSLen Brown 
759c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
760c98d5d94SLen Brown 	struct pkg_data *p)
761103a8feaSLen Brown {
762388e9c81SLen Brown 	int i;
763388e9c81SLen Brown 	struct msr_counter *mp;
764388e9c81SLen Brown 
7653b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
766c98d5d94SLen Brown 
767c98d5d94SLen Brown 	if (t) {
7683b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
7693b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
7703b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
7713b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
7723b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
7733b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
7746886fee4SLen Brown 
775812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
7760de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
777812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
778218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
779388e9c81SLen Brown 
780388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
781388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
782388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
783388e9c81SLen Brown 		}
784103a8feaSLen Brown 	}
785103a8feaSLen Brown 
786c98d5d94SLen Brown 	if (c) {
7873b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
7883b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
7893b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
7903b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
7913b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
792388e9c81SLen Brown 
793388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
794388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
795388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
796388e9c81SLen Brown 		}
7970539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
798c98d5d94SLen Brown 	}
799103a8feaSLen Brown 
800c98d5d94SLen Brown 	if (p) {
8013b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
8020b2bb692SLen Brown 
8030b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
8040b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
8050b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
8060b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
8070b2bb692SLen Brown 
8083b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
8090f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
8103b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
8110f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
8123b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
8130f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
8143b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
8153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
8163b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
8173b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
818be0e54c4SLen Brown 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
819be0e54c4SLen Brown 		outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
820be0e54c4SLen Brown 		outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
8213b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
8223b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
8233b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
8243b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
8253b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
8263b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
8273b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
8283b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
8293b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
830388e9c81SLen Brown 
831388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
832388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
833388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
834388e9c81SLen Brown 		}
835c98d5d94SLen Brown 	}
8363b4d5c7fSAndy Shevchenko 
8373b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
8383b4d5c7fSAndy Shevchenko 
839c98d5d94SLen Brown 	return 0;
840103a8feaSLen Brown }
841103a8feaSLen Brown 
842e23da037SLen Brown /*
843e23da037SLen Brown  * column formatting convention & formats
844e23da037SLen Brown  */
845c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
846c98d5d94SLen Brown 	struct pkg_data *p)
847103a8feaSLen Brown {
848008d396eSLen Brown 	double interval_float, tsc;
849fc04cc67SLen Brown 	char *fmt8;
850388e9c81SLen Brown 	int i;
851388e9c81SLen Brown 	struct msr_counter *mp;
8526168c2e0SLen Brown 	char *delim = "\t";
8536168c2e0SLen Brown 	int printed = 0;
854103a8feaSLen Brown 
855c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
856c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
857c98d5d94SLen Brown 		return 0;
858c98d5d94SLen Brown 
859c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
860c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
861c98d5d94SLen Brown 		return 0;
862c98d5d94SLen Brown 
8631ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
8641ef7d21aSLen Brown 	if ((t != &average.threads) &&
8651ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
8661ef7d21aSLen Brown 		return 0;
8671ef7d21aSLen Brown 
8683f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
869f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
870f4fdf2b4SLen Brown 		struct timeval tv;
871f4fdf2b4SLen Brown 
872f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
873f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
874f4fdf2b4SLen Brown 	}
875f4fdf2b4SLen Brown 
8763f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
8773f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
8783f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
8793f44a5c6SLen Brown 
880103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
881103a8feaSLen Brown 
882008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
883008d396eSLen Brown 
884c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
885c98d5d94SLen Brown 	if (t == &average.threads) {
886812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
8876168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
88801235041SPrarit Bhargava 		if (DO_BIC(BIC_Node))
88901235041SPrarit Bhargava 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
890812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
8916168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
892812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
8936168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
894*4c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
895*4c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
896*4c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
897*4c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
898103a8feaSLen Brown 	} else {
899812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
900c98d5d94SLen Brown 			if (p)
9016168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
902c98d5d94SLen Brown 			else
9036168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
904c98d5d94SLen Brown 		}
90501235041SPrarit Bhargava 		if (DO_BIC(BIC_Node)) {
90601235041SPrarit Bhargava 			if (t)
90701235041SPrarit Bhargava 				outp += sprintf(outp, "%s%d",
90801235041SPrarit Bhargava 						(printed++ ? delim : ""),
90901235041SPrarit Bhargava 					      cpus[t->cpu_id].physical_node_id);
91001235041SPrarit Bhargava 			else
91101235041SPrarit Bhargava 				outp += sprintf(outp, "%s-",
91201235041SPrarit Bhargava 						(printed++ ? delim : ""));
91301235041SPrarit Bhargava 		}
914812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
915c98d5d94SLen Brown 			if (c)
9166168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
917c98d5d94SLen Brown 			else
9186168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
919c98d5d94SLen Brown 		}
920812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9216168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
922*4c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
923*4c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
924*4c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
925*4c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
926103a8feaSLen Brown 	}
927fc04cc67SLen Brown 
928812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
9296168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
930fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
931fc04cc67SLen Brown 
932812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
9336168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
934103a8feaSLen Brown 
935812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
93621ed5574SLen Brown 		if (has_base_hz)
9376168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
93821ed5574SLen Brown 		else
9396168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
940008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
94121ed5574SLen Brown 	}
942103a8feaSLen Brown 
943812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
9446168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
945103a8feaSLen Brown 
946562a2d37SLen Brown 	/* IRQ */
9470de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
9480de6c0dfSLen Brown 		if (sums_need_wide_columns)
9496168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
9500de6c0dfSLen Brown 		else
9516168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
9520de6c0dfSLen Brown 	}
953562a2d37SLen Brown 
9541cc21f7bSLen Brown 	/* SMI */
955812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
9566168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
9571cc21f7bSLen Brown 
958678a3bd1SLen Brown 	/* Added counters */
959678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
960678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
961678a3bd1SLen Brown 			if (mp->width == 32)
9625f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
963678a3bd1SLen Brown 			else
9646168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
965678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
9660de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9676168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
9680de6c0dfSLen Brown 			else
9696168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
970678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
97141618e63SLen Brown 			if (mp->type == COUNTER_USEC)
9726168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
97341618e63SLen Brown 			else
9746168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
975678a3bd1SLen Brown 		}
976678a3bd1SLen Brown 	}
977678a3bd1SLen Brown 
97841618e63SLen Brown 	/* C1 */
97941618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
9806168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
98141618e63SLen Brown 
98241618e63SLen Brown 
983c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
984c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
985c98d5d94SLen Brown 		goto done;
986c98d5d94SLen Brown 
987997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
9886168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
989812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
9906168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
991812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
9926168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
993c98d5d94SLen Brown 
9940539ba11SLen Brown 	/* Mod%c6 */
9950539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
9966168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
9970539ba11SLen Brown 
998812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
9996168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1000889facbeSLen Brown 
1001388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1002388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1003388e9c81SLen Brown 			if (mp->width == 32)
10045f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1005388e9c81SLen Brown 			else
10066168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1007388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10080de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10096168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
10100de6c0dfSLen Brown 			else
10116168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1012388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10136168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1014388e9c81SLen Brown 		}
1015388e9c81SLen Brown 	}
1016388e9c81SLen Brown 
1017c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
1018c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1019c98d5d94SLen Brown 		goto done;
1020c98d5d94SLen Brown 
10210b2bb692SLen Brown 	/* PkgTmp */
1022812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
10236168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1024889facbeSLen Brown 
1025fdf676e5SLen Brown 	/* GFXrc6 */
1026812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
1027ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
10286168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
10299185e988SLen Brown 		} else {
10306168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
10319185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
10329185e988SLen Brown 		}
10339185e988SLen Brown 	}
1034fdf676e5SLen Brown 
103527d47356SLen Brown 	/* GFXMHz */
1036812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
10376168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
103827d47356SLen Brown 
10390b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1040a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
10416168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1042a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
10436168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1044a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
10456168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1046a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
10476168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
10480b2bb692SLen Brown 
10490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
10506168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
10510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
10526168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
10530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
10546168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
10550f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
10566168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
10570f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
10586168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
10590f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
10606168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
10610f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
10626168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1063889facbeSLen Brown 
1064be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1065be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1066be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1067be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1068be0e54c4SLen Brown 
1069889facbeSLen Brown 	/*
1070889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
1071889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
1072889facbeSLen Brown  	 */
1073fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
10746168c2e0SLen Brown 		fmt8 = "%s%.2f";
1075fc04cc67SLen Brown 	else
1076fc04cc67SLen Brown 		fmt8 = "%6.0f**";
1077889facbeSLen Brown 
1078812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
10796168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1080812db3f7SLen Brown 	if (DO_BIC(BIC_CorWatt))
10816168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1082812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
10836168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1084812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
10856168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1086812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
10876168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1088812db3f7SLen Brown 	if (DO_BIC(BIC_Cor_J))
10896168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1090812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
10916168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1092812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
10936168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1094812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
10956168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1096812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
10976168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1098812db3f7SLen Brown 
1099388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1100388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1101388e9c81SLen Brown 			if (mp->width == 32)
11025f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1103388e9c81SLen Brown 			else
11046168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1105388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
11060de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
11076168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
11080de6c0dfSLen Brown 			else
11096168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1110388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
11116168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1112388e9c81SLen Brown 		}
1113388e9c81SLen Brown 	}
1114388e9c81SLen Brown 
1115c98d5d94SLen Brown done:
111694d6ab4bSLen Brown 	if (*(outp - 1) != '\n')
1117c98d5d94SLen Brown 		outp += sprintf(outp, "\n");
1118c98d5d94SLen Brown 
1119c98d5d94SLen Brown 	return 0;
1120103a8feaSLen Brown }
1121103a8feaSLen Brown 
1122b7d8c148SLen Brown void flush_output_stdout(void)
1123103a8feaSLen Brown {
1124b7d8c148SLen Brown 	FILE *filep;
1125b7d8c148SLen Brown 
1126b7d8c148SLen Brown 	if (outf == stderr)
1127b7d8c148SLen Brown 		filep = stdout;
1128b7d8c148SLen Brown 	else
1129b7d8c148SLen Brown 		filep = outf;
1130b7d8c148SLen Brown 
1131b7d8c148SLen Brown 	fputs(output_buffer, filep);
1132b7d8c148SLen Brown 	fflush(filep);
1133b7d8c148SLen Brown 
1134c98d5d94SLen Brown 	outp = output_buffer;
1135c98d5d94SLen Brown }
1136b7d8c148SLen Brown void flush_output_stderr(void)
1137c98d5d94SLen Brown {
1138b7d8c148SLen Brown 	fputs(output_buffer, outf);
1139b7d8c148SLen Brown 	fflush(outf);
1140c98d5d94SLen Brown 	outp = output_buffer;
1141c98d5d94SLen Brown }
1142c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1143c98d5d94SLen Brown {
1144e23da037SLen Brown 	static int printed;
1145103a8feaSLen Brown 
1146e23da037SLen Brown 	if (!printed || !summary_only)
1147c8ade361SLen Brown 		print_header("\t");
1148103a8feaSLen Brown 
1149c98d5d94SLen Brown 	if (topo.num_cpus > 1)
1150c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
1151c98d5d94SLen Brown 			&average.packages);
1152103a8feaSLen Brown 
1153e23da037SLen Brown 	printed = 1;
1154e23da037SLen Brown 
1155e23da037SLen Brown 	if (summary_only)
1156e23da037SLen Brown 		return;
1157e23da037SLen Brown 
1158c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1159103a8feaSLen Brown }
1160103a8feaSLen Brown 
1161889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1162889facbeSLen Brown 	if (new > old) {			\
1163889facbeSLen Brown 		old = new - old;		\
1164889facbeSLen Brown 	} else {				\
1165889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1166889facbeSLen Brown 	}
1167889facbeSLen Brown 
1168ba3dec99SLen Brown int
1169c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1170103a8feaSLen Brown {
1171388e9c81SLen Brown 	int i;
1172388e9c81SLen Brown 	struct msr_counter *mp;
11730b2bb692SLen Brown 
1174a99d8730SLen Brown 
1175a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
11760b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1177a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
11780b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1179a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
11800b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1181a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
11820b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1183a99d8730SLen Brown 
1184c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
11850f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1186c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
11870f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1188c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
11890f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1190c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1191ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1192ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1193ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1194be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1195be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1196889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1197889facbeSLen Brown 
11989185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
11999185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
12009185e988SLen Brown 		old->gfx_rc6_ms = -1;
12019185e988SLen Brown 	else
1202fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
12039185e988SLen Brown 
120427d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
120527d47356SLen Brown 
1206889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1207889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1208889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1209889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1210889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1211889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1212ba3dec99SLen Brown 
1213388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1214388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1215388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1216388e9c81SLen Brown 		else
1217388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1218388e9c81SLen Brown 	}
1219388e9c81SLen Brown 
1220ba3dec99SLen Brown 	return 0;
1221103a8feaSLen Brown }
1222103a8feaSLen Brown 
1223c98d5d94SLen Brown void
1224c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1225c98d5d94SLen Brown {
1226388e9c81SLen Brown 	int i;
1227388e9c81SLen Brown 	struct msr_counter *mp;
1228388e9c81SLen Brown 
1229c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1230c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1231c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1232889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
12330539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1234388e9c81SLen Brown 
1235388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1236388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1237388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1238388e9c81SLen Brown 		else
1239388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1240388e9c81SLen Brown 	}
1241103a8feaSLen Brown }
1242c98d5d94SLen Brown 
1243c3ae331dSLen Brown /*
1244c3ae331dSLen Brown  * old = new - old
1245c3ae331dSLen Brown  */
1246ba3dec99SLen Brown int
1247c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1248c98d5d94SLen Brown 	struct core_data *core_delta)
1249c98d5d94SLen Brown {
1250388e9c81SLen Brown 	int i;
1251388e9c81SLen Brown 	struct msr_counter *mp;
1252388e9c81SLen Brown 
1253*4c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
1254*4c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
1255*4c2122d4SLen Brown 		new->apic_id = old->apic_id;
1256*4c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
1257*4c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
1258*4c2122d4SLen Brown 
12593f44a5c6SLen Brown 	/*
12603f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
12613f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
12623f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
12633f44a5c6SLen Brown 	 */
12643f44a5c6SLen Brown 
12653f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
12663f44a5c6SLen Brown 	old->tv_end = new->tv_end;
12673f44a5c6SLen Brown 
1268c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1269c98d5d94SLen Brown 
1270103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1271b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1272b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1273b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1274b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1275103a8feaSLen Brown 
1276c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1277c98d5d94SLen Brown 
1278812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1279c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1280c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1281c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1282c98d5d94SLen Brown 		} else {
1283ba3dec99SLen Brown 			return -1;
1284103a8feaSLen Brown 		}
1285a729617cSLen Brown 	}
1286103a8feaSLen Brown 
1287c98d5d94SLen Brown 
1288144b44b1SLen Brown 	if (use_c1_residency_msr) {
1289144b44b1SLen Brown 		/*
1290144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1291144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1292144b44b1SLen Brown 		 */
1293144b44b1SLen Brown 	} else {
1294103a8feaSLen Brown 		/*
1295c3ae331dSLen Brown 		 * As counter collection is not atomic,
1296c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1297103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1298103a8feaSLen Brown 		 */
129995149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1300c98d5d94SLen Brown 			old->c1 = 0;
1301c98d5d94SLen Brown 		else {
1302c98d5d94SLen Brown 			/* normal case, derive c1 */
1303008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1304c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1305c98d5d94SLen Brown 		}
1306144b44b1SLen Brown 	}
1307c3ae331dSLen Brown 
1308c98d5d94SLen Brown 	if (old->mperf == 0) {
1309b7d8c148SLen Brown 		if (debug > 1)
1310b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1311c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1312c98d5d94SLen Brown 	}
1313103a8feaSLen Brown 
1314812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1315562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1316562a2d37SLen Brown 
1317812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
13181ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1319ba3dec99SLen Brown 
1320388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1321388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1322388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1323388e9c81SLen Brown 		else
1324388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1325388e9c81SLen Brown 	}
1326ba3dec99SLen Brown 	return 0;
1327103a8feaSLen Brown }
1328c98d5d94SLen Brown 
1329c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1330c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1331c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1332c98d5d94SLen Brown {
1333ba3dec99SLen Brown 	int retval = 0;
1334ba3dec99SLen Brown 
1335c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1336c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1337c98d5d94SLen Brown 		delta_core(c, c2);
1338c98d5d94SLen Brown 
1339c98d5d94SLen Brown 	/* always calculate thread delta */
1340ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1341ba3dec99SLen Brown 	if (retval)
1342ba3dec99SLen Brown 		return retval;
1343c98d5d94SLen Brown 
1344c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1345c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1346ba3dec99SLen Brown 		retval = delta_package(p, p2);
1347c98d5d94SLen Brown 
1348ba3dec99SLen Brown 	return retval;
1349103a8feaSLen Brown }
1350103a8feaSLen Brown 
1351c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1352103a8feaSLen Brown {
1353388e9c81SLen Brown 	int i;
1354388e9c81SLen Brown 	struct msr_counter  *mp;
1355388e9c81SLen Brown 
13563f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
13573f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
13583f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
13593f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
13603f44a5c6SLen Brown 
1361c98d5d94SLen Brown 	t->tsc = 0;
1362c98d5d94SLen Brown 	t->aperf = 0;
1363c98d5d94SLen Brown 	t->mperf = 0;
1364c98d5d94SLen Brown 	t->c1 = 0;
1365103a8feaSLen Brown 
1366562a2d37SLen Brown 	t->irq_count = 0;
1367562a2d37SLen Brown 	t->smi_count = 0;
1368562a2d37SLen Brown 
1369c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1370c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1371c98d5d94SLen Brown 
1372c98d5d94SLen Brown 	c->c3 = 0;
1373c98d5d94SLen Brown 	c->c6 = 0;
1374c98d5d94SLen Brown 	c->c7 = 0;
13750539ba11SLen Brown 	c->mc6_us = 0;
1376889facbeSLen Brown 	c->core_temp_c = 0;
1377c98d5d94SLen Brown 
13780b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
13790b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
13800b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
13810b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
13820b2bb692SLen Brown 
1383c98d5d94SLen Brown 	p->pc2 = 0;
13840f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1385c98d5d94SLen Brown 		p->pc3 = 0;
13860f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1387c98d5d94SLen Brown 		p->pc6 = 0;
13880f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1389c98d5d94SLen Brown 		p->pc7 = 0;
1390ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1391ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1392ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1393be0e54c4SLen Brown 	p->cpu_lpi = 0;
1394be0e54c4SLen Brown 	p->sys_lpi = 0;
1395889facbeSLen Brown 
1396889facbeSLen Brown 	p->energy_pkg = 0;
1397889facbeSLen Brown 	p->energy_dram = 0;
1398889facbeSLen Brown 	p->energy_cores = 0;
1399889facbeSLen Brown 	p->energy_gfx = 0;
1400889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1401889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1402889facbeSLen Brown 	p->pkg_temp_c = 0;
140327d47356SLen Brown 
1404fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
140527d47356SLen Brown 	p->gfx_mhz = 0;
1406388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1407388e9c81SLen Brown 		t->counter[i] = 0;
1408388e9c81SLen Brown 
1409388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1410388e9c81SLen Brown 		c->counter[i] = 0;
1411388e9c81SLen Brown 
1412388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1413388e9c81SLen Brown 		p->counter[i] = 0;
1414103a8feaSLen Brown }
1415c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1416c98d5d94SLen Brown 	struct pkg_data *p)
1417103a8feaSLen Brown {
1418388e9c81SLen Brown 	int i;
1419388e9c81SLen Brown 	struct msr_counter *mp;
1420388e9c81SLen Brown 
1421*4c2122d4SLen Brown 	/* copy un-changing apic_id's */
1422*4c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
1423*4c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
1424*4c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
1425*4c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
1426*4c2122d4SLen Brown 
14273f44a5c6SLen Brown 	/* remember first tv_begin */
14283f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
14293f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
14303f44a5c6SLen Brown 
14313f44a5c6SLen Brown 	/* remember last tv_end */
14323f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
14333f44a5c6SLen Brown 
1434c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1435c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1436c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1437c98d5d94SLen Brown 	average.threads.c1 += t->c1;
143815aaa346SLen Brown 
1439562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1440562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1441562a2d37SLen Brown 
1442388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1443388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1444388e9c81SLen Brown 			continue;
1445388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1446388e9c81SLen Brown 	}
1447388e9c81SLen Brown 
1448c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1449c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1450c98d5d94SLen Brown 		return 0;
1451c98d5d94SLen Brown 
1452c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1453c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1454c98d5d94SLen Brown 	average.cores.c7 += c->c7;
14550539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1456c98d5d94SLen Brown 
1457889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1458889facbeSLen Brown 
1459388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1460388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1461388e9c81SLen Brown 			continue;
1462388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1463388e9c81SLen Brown 	}
1464388e9c81SLen Brown 
1465c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1466c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1467c98d5d94SLen Brown 		return 0;
1468c98d5d94SLen Brown 
1469a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
14700b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1471a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
14720b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1473a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
14740b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1475a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
14760b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
14770b2bb692SLen Brown 
1478c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
14790f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1480c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
14810f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1482c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
14830f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1484c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1485ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1486ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1487ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1488c98d5d94SLen Brown 
1489be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1490be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1491be0e54c4SLen Brown 
1492889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1493889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1494889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1495889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1496889facbeSLen Brown 
1497fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
149827d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
149927d47356SLen Brown 
1500889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1501889facbeSLen Brown 
1502889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1503889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1504388e9c81SLen Brown 
1505388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1506388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1507388e9c81SLen Brown 			continue;
1508388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1509388e9c81SLen Brown 	}
1510c98d5d94SLen Brown 	return 0;
1511c98d5d94SLen Brown }
1512c98d5d94SLen Brown /*
1513c98d5d94SLen Brown  * sum the counters for all cpus in the system
1514c98d5d94SLen Brown  * compute the weighted average
1515c98d5d94SLen Brown  */
1516c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1517c98d5d94SLen Brown 	struct pkg_data *p)
1518c98d5d94SLen Brown {
1519388e9c81SLen Brown 	int i;
1520388e9c81SLen Brown 	struct msr_counter *mp;
1521388e9c81SLen Brown 
1522c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1523c98d5d94SLen Brown 
1524c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1525c98d5d94SLen Brown 
1526c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1527c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1528c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1529c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1530c98d5d94SLen Brown 
15310de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
15320de6c0dfSLen Brown 		sums_need_wide_columns = 1;
15330de6c0dfSLen Brown 
1534c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1535c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1536c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
15370539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1538c98d5d94SLen Brown 
1539a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
15400b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1541a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
15420b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1543a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
15440b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1545a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
15460b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
15470b2bb692SLen Brown 
1548c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
15490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1550c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
15510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1552c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
15530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1554c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1555ca58710fSKristen Carlson Accardi 
1556ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1557ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1558ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1559388e9c81SLen Brown 
1560388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1561388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1562388e9c81SLen Brown 			continue;
15630de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
15640de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
15650de6c0dfSLen Brown 				sums_need_wide_columns = 1;
156641618e63SLen Brown 			continue;
15670de6c0dfSLen Brown 		}
1568388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1569388e9c81SLen Brown 	}
1570388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1571388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1572388e9c81SLen Brown 			continue;
15730de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
15740de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
15750de6c0dfSLen Brown 				sums_need_wide_columns = 1;
15760de6c0dfSLen Brown 		}
1577388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1578388e9c81SLen Brown 	}
1579388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1580388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1581388e9c81SLen Brown 			continue;
15820de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
15830de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
15840de6c0dfSLen Brown 				sums_need_wide_columns = 1;
15850de6c0dfSLen Brown 		}
1586388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1587388e9c81SLen Brown 	}
1588c98d5d94SLen Brown }
1589c98d5d94SLen Brown 
1590c98d5d94SLen Brown static unsigned long long rdtsc(void)
1591c98d5d94SLen Brown {
1592c98d5d94SLen Brown 	unsigned int low, high;
1593c98d5d94SLen Brown 
1594c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1595c98d5d94SLen Brown 
1596c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1597c98d5d94SLen Brown }
1598c98d5d94SLen Brown 
1599c98d5d94SLen Brown /*
1600495c7654SLen Brown  * Open a file, and exit on failure
1601495c7654SLen Brown  */
1602495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1603495c7654SLen Brown {
1604495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1605495c7654SLen Brown 
1606495c7654SLen Brown 	if (!filep)
1607495c7654SLen Brown 		err(1, "%s: open failed", path);
1608495c7654SLen Brown 	return filep;
1609495c7654SLen Brown }
1610495c7654SLen Brown /*
1611495c7654SLen Brown  * snapshot_sysfs_counter()
1612495c7654SLen Brown  *
1613495c7654SLen Brown  * return snapshot of given counter
1614495c7654SLen Brown  */
1615495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1616495c7654SLen Brown {
1617495c7654SLen Brown 	FILE *fp;
1618495c7654SLen Brown 	int retval;
1619495c7654SLen Brown 	unsigned long long counter;
1620495c7654SLen Brown 
1621495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1622495c7654SLen Brown 
1623495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1624495c7654SLen Brown 	if (retval != 1)
1625495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1626495c7654SLen Brown 
1627495c7654SLen Brown 	fclose(fp);
1628495c7654SLen Brown 
1629495c7654SLen Brown 	return counter;
1630495c7654SLen Brown }
1631495c7654SLen Brown 
1632495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1633495c7654SLen Brown {
1634495c7654SLen Brown 	if (mp->msr_num != 0) {
1635495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1636495c7654SLen Brown 			return -1;
1637495c7654SLen Brown 	} else {
163846c27978SLen Brown 		char path[128 + PATH_BYTES];
163941618e63SLen Brown 
164041618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
164141618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
164241618e63SLen Brown 				 cpu, mp->path);
164341618e63SLen Brown 
164441618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
164541618e63SLen Brown 		} else {
1646495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1647495c7654SLen Brown 		}
164841618e63SLen Brown 	}
1649495c7654SLen Brown 
1650495c7654SLen Brown 	return 0;
1651495c7654SLen Brown }
1652495c7654SLen Brown 
1653*4c2122d4SLen Brown void get_apic_id(struct thread_data *t)
1654*4c2122d4SLen Brown {
1655*4c2122d4SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level;
1656*4c2122d4SLen Brown 
1657*4c2122d4SLen Brown 	eax = ebx = ecx = edx = 0;
1658*4c2122d4SLen Brown 
1659*4c2122d4SLen Brown 	if (!genuine_intel)
1660*4c2122d4SLen Brown 		return;
1661*4c2122d4SLen Brown 
1662*4c2122d4SLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
1663*4c2122d4SLen Brown 
1664*4c2122d4SLen Brown 	__cpuid(1, eax, ebx, ecx, edx);
1665*4c2122d4SLen Brown 	t->apic_id = (ebx >> 24) & 0xf;
1666*4c2122d4SLen Brown 
1667*4c2122d4SLen Brown 	if (max_level < 0xb)
1668*4c2122d4SLen Brown 		return;
1669*4c2122d4SLen Brown 
1670*4c2122d4SLen Brown 	if (!DO_BIC(BIC_X2APIC))
1671*4c2122d4SLen Brown 		return;
1672*4c2122d4SLen Brown 
1673*4c2122d4SLen Brown 	ecx = 0;
1674*4c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
1675*4c2122d4SLen Brown 	t->x2apic_id = edx;
1676*4c2122d4SLen Brown 
1677*4c2122d4SLen Brown 	if (debug && (t->apic_id != t->x2apic_id))
1678*4c2122d4SLen Brown 		fprintf(stderr, "cpu%d: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id);
1679*4c2122d4SLen Brown }
1680*4c2122d4SLen Brown 
1681495c7654SLen Brown /*
1682c98d5d94SLen Brown  * get_counters(...)
1683c98d5d94SLen Brown  * migrate to cpu
1684c98d5d94SLen Brown  * acquire and record local counters for that cpu
1685c98d5d94SLen Brown  */
1686c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1687c98d5d94SLen Brown {
1688c98d5d94SLen Brown 	int cpu = t->cpu_id;
1689889facbeSLen Brown 	unsigned long long msr;
16900102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1691388e9c81SLen Brown 	struct msr_counter *mp;
1692388e9c81SLen Brown 	int i;
1693c98d5d94SLen Brown 
1694f4fdf2b4SLen Brown 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1695f4fdf2b4SLen Brown 
1696e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1697b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
169815aaa346SLen Brown 		return -1;
1699e52966c0SLen Brown 	}
170015aaa346SLen Brown 
1701*4c2122d4SLen Brown 	if (first_counter_read)
1702*4c2122d4SLen Brown 		get_apic_id(t);
17030102b067SLen Brown retry:
1704c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
170515aaa346SLen Brown 
1706812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
17070102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
17080102b067SLen Brown 
17090102b067SLen Brown 		/*
17100102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
17110102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
17120102b067SLen Brown 		 *
17130102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
17140102b067SLen Brown 		 * individual system call, so delays may occur
17150102b067SLen Brown 		 * between them.  If the time to read them
17160102b067SLen Brown 		 * varies by a large amount, we re-read them.
17170102b067SLen Brown 		 */
17180102b067SLen Brown 
17190102b067SLen Brown 		/*
17200102b067SLen Brown 		 * This initial dummy APERF read has been seen to
17210102b067SLen Brown 		 * reduce jitter in the subsequent reads.
17220102b067SLen Brown 		 */
17230102b067SLen Brown 
17249c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1725c98d5d94SLen Brown 			return -3;
17260102b067SLen Brown 
17270102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
17280102b067SLen Brown 
17290102b067SLen Brown 		tsc_before = t->tsc;
17300102b067SLen Brown 
17310102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
17320102b067SLen Brown 			return -3;
17330102b067SLen Brown 
17340102b067SLen Brown 		tsc_between = rdtsc();
17350102b067SLen Brown 
17369c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1737c98d5d94SLen Brown 			return -4;
17380102b067SLen Brown 
17390102b067SLen Brown 		tsc_after = rdtsc();
17400102b067SLen Brown 
17410102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
17420102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
17430102b067SLen Brown 
17440102b067SLen Brown 		/*
17450102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
17460102b067SLen Brown 		 * differ by more than 2x, then try again.
17470102b067SLen Brown 		 */
17480102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
17490102b067SLen Brown 			aperf_mperf_retry_count++;
17500102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
17510102b067SLen Brown 				goto retry;
17520102b067SLen Brown 			else
17530102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
17540102b067SLen Brown 					cpu, aperf_time, mperf_time);
17550102b067SLen Brown 		}
17560102b067SLen Brown 		aperf_mperf_retry_count = 0;
17570102b067SLen Brown 
1758b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1759b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
176088c3281fSLen Brown 	}
176188c3281fSLen Brown 
1762812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1763562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1764812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
17651ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
17661ed51011SLen Brown 			return -5;
17671ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
17681ed51011SLen Brown 	}
17690539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1770144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1771144b44b1SLen Brown 			return -6;
1772144b44b1SLen Brown 	}
1773144b44b1SLen Brown 
1774388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1775495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1776388e9c81SLen Brown 			return -10;
1777388e9c81SLen Brown 	}
1778388e9c81SLen Brown 
1779c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1780c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1781f4fdf2b4SLen Brown 		goto done;
1782c98d5d94SLen Brown 
1783997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
1784c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1785c98d5d94SLen Brown 			return -6;
1786144b44b1SLen Brown 	}
1787144b44b1SLen Brown 
1788812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1789c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1790c98d5d94SLen Brown 			return -7;
1791fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1792fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1793fb5d4327SDasaratharaman Chandramouli 			return -7;
1794103a8feaSLen Brown 	}
179515aaa346SLen Brown 
1796812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1797c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1798c98d5d94SLen Brown 			return -8;
1799c98d5d94SLen Brown 
18000539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
18010539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
18020539ba11SLen Brown 			return -8;
18030539ba11SLen Brown 
1804812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1805889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1806889facbeSLen Brown 			return -9;
1807889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1808889facbeSLen Brown 	}
1809889facbeSLen Brown 
1810388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1811495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1812388e9c81SLen Brown 			return -10;
1813388e9c81SLen Brown 	}
1814889facbeSLen Brown 
1815c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1816c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1817f4fdf2b4SLen Brown 		goto done;
181815aaa346SLen Brown 
1819a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
18200b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
18210b2bb692SLen Brown 			return -10;
1822a99d8730SLen Brown 	}
1823a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
18240b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
18250b2bb692SLen Brown 			return -11;
1826a99d8730SLen Brown 	}
1827a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
18280b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
18290b2bb692SLen Brown 			return -12;
1830a99d8730SLen Brown 	}
1831a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
18320b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
18330b2bb692SLen Brown 			return -13;
18340b2bb692SLen Brown 	}
18350f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1836c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1837c98d5d94SLen Brown 			return -9;
18380f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
18390539ba11SLen Brown 		if (do_slm_cstates) {
18400539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
18410539ba11SLen Brown 				return -10;
18420539ba11SLen Brown 		} else {
1843c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1844c98d5d94SLen Brown 				return -10;
18450539ba11SLen Brown 		}
18460539ba11SLen Brown 	}
18470539ba11SLen Brown 
18480f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1849c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1850c98d5d94SLen Brown 			return -11;
18510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1852c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1853c98d5d94SLen Brown 			return -12;
18540f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1855ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1856ca58710fSKristen Carlson Accardi 			return -13;
18570f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1858ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1859ca58710fSKristen Carlson Accardi 			return -13;
18600f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1861ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1862ca58710fSKristen Carlson Accardi 			return -13;
18630f47c08dSLen Brown 
1864be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1865be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1866be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1867be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
1868be0e54c4SLen Brown 
1869889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1870889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1871889facbeSLen Brown 			return -13;
1872889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1873889facbeSLen Brown 	}
18749148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1875889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1876889facbeSLen Brown 			return -14;
1877889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1878889facbeSLen Brown 	}
1879889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1880889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1881889facbeSLen Brown 			return -15;
1882889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1883889facbeSLen Brown 	}
1884889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1885889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1886889facbeSLen Brown 			return -16;
1887889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1888889facbeSLen Brown 	}
1889889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1890889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1891889facbeSLen Brown 			return -16;
1892889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1893889facbeSLen Brown 	}
1894889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1895889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1896889facbeSLen Brown 			return -16;
1897889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1898889facbeSLen Brown 	}
1899812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1900889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1901889facbeSLen Brown 			return -17;
1902889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1903889facbeSLen Brown 	}
1904fdf676e5SLen Brown 
1905812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1906fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1907fdf676e5SLen Brown 
1908812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
190927d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
191027d47356SLen Brown 
1911388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1912495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
1913388e9c81SLen Brown 			return -10;
1914388e9c81SLen Brown 	}
1915f4fdf2b4SLen Brown done:
1916f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
1917388e9c81SLen Brown 
191815aaa346SLen Brown 	return 0;
1919103a8feaSLen Brown }
1920103a8feaSLen Brown 
1921ee7e38e3SLen Brown /*
1922ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1923ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1924ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1925ee7e38e3SLen Brown  */
1926ee7e38e3SLen Brown 
1927ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1928ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1929ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1930ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1931ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1932ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1933ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1934ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1935ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1936ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1937ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1938ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
19390b2bb692SLen Brown #define PCL__8 12 /* PC8 */
19400b2bb692SLen Brown #define PCL__9 13 /* PC9 */
19410b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1942ee7e38e3SLen Brown 
1943ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1944ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
19450b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1946ee7e38e3SLen Brown 
1947e9257f5fSLen 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};
1948e9257f5fSLen 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};
1949e9257f5fSLen 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};
19500539ba11SLen 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};
1951f2642888SLen 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};
1952e9257f5fSLen 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};
1953e4085d54SLen 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};
19542085e124SArtem 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};
1955ee7e38e3SLen Brown 
1956a2b7b749SLen Brown 
1957a2b7b749SLen Brown static void
1958a2b7b749SLen Brown calculate_tsc_tweak()
1959a2b7b749SLen Brown {
1960a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1961a2b7b749SLen Brown }
1962a2b7b749SLen Brown 
1963fcd17211SLen Brown static void
1964fcd17211SLen Brown dump_nhm_platform_info(void)
1965103a8feaSLen Brown {
1966103a8feaSLen Brown 	unsigned long long msr;
1967103a8feaSLen Brown 	unsigned int ratio;
1968103a8feaSLen Brown 
1969ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1970103a8feaSLen Brown 
1971b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
19726574a5d5SLen Brown 
1973103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1974710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
1975103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1976103a8feaSLen Brown 
1977103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1978710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1979103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1980103a8feaSLen Brown 
19817ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1982b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1983bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
198467920418SLen Brown 
1985fcd17211SLen Brown 	return;
1986fcd17211SLen Brown }
1987fcd17211SLen Brown 
1988fcd17211SLen Brown static void
1989fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1990fcd17211SLen Brown {
1991fcd17211SLen Brown 	unsigned long long msr;
1992fcd17211SLen Brown 	unsigned int ratio;
1993fcd17211SLen Brown 
19947ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1995fcd17211SLen Brown 
1996b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1997fcd17211SLen Brown 
1998fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1999fcd17211SLen Brown 	if (ratio)
2000710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2001fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2002fcd17211SLen Brown 
2003fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2004fcd17211SLen Brown 	if (ratio)
2005710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2006fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2007fcd17211SLen Brown 	return;
2008fcd17211SLen Brown }
2009fcd17211SLen Brown 
2010fcd17211SLen Brown static void
2011fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
2012fcd17211SLen Brown {
2013fcd17211SLen Brown 	unsigned long long msr;
2014fcd17211SLen Brown 	unsigned int ratio;
20156574a5d5SLen Brown 
20167ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
20176574a5d5SLen Brown 
2018b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
20196574a5d5SLen Brown 
20206574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
20216574a5d5SLen Brown 	if (ratio)
2022710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
20236574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20246574a5d5SLen Brown 
20256574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
20266574a5d5SLen Brown 	if (ratio)
2027710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
20286574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20296574a5d5SLen Brown 
20306574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
20316574a5d5SLen Brown 	if (ratio)
2032710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
20336574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20346574a5d5SLen Brown 
20356574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
20366574a5d5SLen Brown 	if (ratio)
2037710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
20386574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20396574a5d5SLen Brown 
20406574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
20416574a5d5SLen Brown 	if (ratio)
2042710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
20436574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20446574a5d5SLen Brown 
20456574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
20466574a5d5SLen Brown 	if (ratio)
2047710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
20486574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20496574a5d5SLen Brown 
20506574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
20516574a5d5SLen Brown 	if (ratio)
2052710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
20536574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20546574a5d5SLen Brown 
20556574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
20566574a5d5SLen Brown 	if (ratio)
2057710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
20586574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
2059103a8feaSLen Brown 	return;
2060fcd17211SLen Brown }
206131e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
206231e07522SLen Brown {
206331e07522SLen Brown 
206431e07522SLen Brown 	if (!genuine_intel)
206531e07522SLen Brown 		return 0;
206631e07522SLen Brown 
206731e07522SLen Brown 	switch (model) {
206831e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
206931e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
207031e07522SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
207131e07522SLen Brown 		return 1;
207231e07522SLen Brown 	}
207331e07522SLen Brown 	return 0;
207431e07522SLen Brown }
2075fcd17211SLen Brown 
2076fcd17211SLen Brown static void
207731e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
2078fcd17211SLen Brown {
207931e07522SLen Brown 	unsigned long long msr, core_counts;
208031e07522SLen Brown 	unsigned int ratio, group_size;
2081103a8feaSLen Brown 
20827ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2083b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
20846574a5d5SLen Brown 
208531e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
208631e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
208731e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
208831e07522SLen Brown 	} else {
208931e07522SLen Brown 		core_counts = 0x0807060504030201;
209031e07522SLen Brown 	}
209131e07522SLen Brown 
20926574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
209331e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
20946574a5d5SLen Brown 	if (ratio)
209531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
209631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
20976574a5d5SLen Brown 
20986574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
209931e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
21006574a5d5SLen Brown 	if (ratio)
210131e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
210231e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21036574a5d5SLen Brown 
21046574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
210531e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
21066574a5d5SLen Brown 	if (ratio)
210731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
210831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21096574a5d5SLen Brown 
21106574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
211131e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
21126574a5d5SLen Brown 	if (ratio)
211331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
211431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21156574a5d5SLen Brown 
2116103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
211731e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
2118103a8feaSLen Brown 	if (ratio)
211931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
212031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2121103a8feaSLen Brown 
2122103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
212331e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
2124103a8feaSLen Brown 	if (ratio)
212531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
212631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2127103a8feaSLen Brown 
2128103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
212931e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
2130103a8feaSLen Brown 	if (ratio)
213131e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
213231e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2133103a8feaSLen Brown 
2134103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
213531e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
2136103a8feaSLen Brown 	if (ratio)
213731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
213831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2139fcd17211SLen Brown 	return;
2140fcd17211SLen Brown }
21413a9a941dSLen Brown 
2142fcd17211SLen Brown static void
21430f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
21440f7887c4SLen Brown {
21450f7887c4SLen Brown 	unsigned long long msr;
21460f7887c4SLen Brown 	unsigned int ratio;
21470f7887c4SLen Brown 
21480f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
21490f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
21500f7887c4SLen Brown 
21510f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
21520f7887c4SLen Brown 	if (ratio)
21530f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
21540f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21550f7887c4SLen Brown 
21560f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
21570f7887c4SLen Brown 	if (ratio)
21580f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
21590f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21600f7887c4SLen Brown 
21610f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
21620f7887c4SLen Brown 	if (ratio)
21630f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
21640f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21650f7887c4SLen Brown 
21660f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
21670f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
21680f7887c4SLen Brown 
21690f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
21700f7887c4SLen Brown 	if (ratio)
21710f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
21720f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21730f7887c4SLen Brown 
21740f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
21750f7887c4SLen Brown 	if (ratio)
21760f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
21770f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21780f7887c4SLen Brown 
21790f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
21800f7887c4SLen Brown 	if (ratio)
21810f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
21820f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21830f7887c4SLen Brown 
21840f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
21850f7887c4SLen Brown 	if (ratio)
21860f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
21870f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21880f7887c4SLen Brown }
21890f7887c4SLen Brown 
21900f7887c4SLen Brown static void
2191fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2192fb5d4327SDasaratharaman Chandramouli {
2193cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2194cbf97abaSHubert Chrzaniuk 
2195fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2196cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2197cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2198cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2199cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2200fb5d4327SDasaratharaman Chandramouli 
2201ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2202fb5d4327SDasaratharaman Chandramouli 
2203b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2204bfae2052SLen Brown 		base_cpu, msr);
2205fb5d4327SDasaratharaman Chandramouli 
2206fb5d4327SDasaratharaman Chandramouli 	/**
2207fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2208cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2209cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2210fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2211fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2212fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2213fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2214fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2215fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2216fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2217fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2218fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2219fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2220fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2221fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2222fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2223fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2224fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2225fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2226fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2227fb5d4327SDasaratharaman Chandramouli 	 */
2228fb5d4327SDasaratharaman Chandramouli 
2229cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2230cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2231cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2232cbf97abaSHubert Chrzaniuk 
2233cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2234fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2235cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2236fb5d4327SDasaratharaman Chandramouli 
2237cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2238cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2239cbf97abaSHubert Chrzaniuk 		b_nr++;
2240fb5d4327SDasaratharaman Chandramouli 	}
2241cbf97abaSHubert Chrzaniuk 
2242cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2243cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2244b7d8c148SLen Brown 			fprintf(outf,
2245710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2246cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2247fb5d4327SDasaratharaman Chandramouli }
2248fb5d4327SDasaratharaman Chandramouli 
2249fb5d4327SDasaratharaman Chandramouli static void
2250fcd17211SLen Brown dump_nhm_cst_cfg(void)
2251fcd17211SLen Brown {
2252fcd17211SLen Brown 	unsigned long long msr;
2253fcd17211SLen Brown 
22541df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2255fcd17211SLen Brown 
22561df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2257fcd17211SLen Brown 
22583e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2259fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2260fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2261fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2262fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2263fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
22646c34f160SLen Brown 		(unsigned int)msr & 0xF,
2265fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2266ac980e13SArtem Bityutskiy 
2267ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2268ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
2269ac980e13SArtem Bityutskiy 		fprintf(outf, ", automatic c-state conversion=%s",
2270ac980e13SArtem Bityutskiy 			(msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2271ac980e13SArtem Bityutskiy 	}
2272ac980e13SArtem Bityutskiy 
2273ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2274ac980e13SArtem Bityutskiy 
2275fcd17211SLen Brown 	return;
2276103a8feaSLen Brown }
2277103a8feaSLen Brown 
22786fb3143bSLen Brown static void
22796fb3143bSLen Brown dump_config_tdp(void)
22806fb3143bSLen Brown {
22816fb3143bSLen Brown 	unsigned long long msr;
22826fb3143bSLen Brown 
22836fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2284b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2285685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
22866fb3143bSLen Brown 
22876fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2288b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
22896fb3143bSLen Brown 	if (msr) {
2290685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2291685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2292685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2293685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
22946fb3143bSLen Brown 	}
2295b7d8c148SLen Brown 	fprintf(outf, ")\n");
22966fb3143bSLen Brown 
22976fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2298b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
22996fb3143bSLen Brown 	if (msr) {
2300685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2301685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2302685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2303685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
23046fb3143bSLen Brown 	}
2305b7d8c148SLen Brown 	fprintf(outf, ")\n");
23066fb3143bSLen Brown 
23076fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2308b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
23096fb3143bSLen Brown 	if ((msr) & 0x3)
2310b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2311b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2312b7d8c148SLen Brown 	fprintf(outf, ")\n");
23136fb3143bSLen Brown 
23146fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2315b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2316685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2317b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2318b7d8c148SLen Brown 	fprintf(outf, ")\n");
23196fb3143bSLen Brown }
23205a63426eSLen Brown 
23215a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
23225a63426eSLen Brown 
23235a63426eSLen Brown void print_irtl(void)
23245a63426eSLen Brown {
23255a63426eSLen Brown 	unsigned long long msr;
23265a63426eSLen Brown 
23275a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
23285a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
23295a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23305a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23315a63426eSLen Brown 
23325a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
23335a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
23345a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23355a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23365a63426eSLen Brown 
23375a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
23385a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
23395a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23405a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23415a63426eSLen Brown 
23425a63426eSLen Brown 	if (!do_irtl_hsw)
23435a63426eSLen Brown 		return;
23445a63426eSLen Brown 
23455a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
23465a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
23475a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23485a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23495a63426eSLen Brown 
23505a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
23515a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
23525a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23535a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23545a63426eSLen Brown 
23555a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
23565a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
23575a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23585a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23595a63426eSLen Brown 
23605a63426eSLen Brown }
236136229897SLen Brown void free_fd_percpu(void)
236236229897SLen Brown {
236336229897SLen Brown 	int i;
236436229897SLen Brown 
236501a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
236636229897SLen Brown 		if (fd_percpu[i] != 0)
236736229897SLen Brown 			close(fd_percpu[i]);
236836229897SLen Brown 	}
236936229897SLen Brown 
237036229897SLen Brown 	free(fd_percpu);
23716fb3143bSLen Brown }
23726fb3143bSLen Brown 
2373c98d5d94SLen Brown void free_all_buffers(void)
2374103a8feaSLen Brown {
23750e2d8f05SLen Brown 	int i;
23760e2d8f05SLen Brown 
2377c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2378c98d5d94SLen Brown 	cpu_present_set = NULL;
237936229897SLen Brown 	cpu_present_setsize = 0;
2380103a8feaSLen Brown 
2381c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2382c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2383c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2384103a8feaSLen Brown 
2385c98d5d94SLen Brown 	free(thread_even);
2386c98d5d94SLen Brown 	free(core_even);
2387c98d5d94SLen Brown 	free(package_even);
2388103a8feaSLen Brown 
2389c98d5d94SLen Brown 	thread_even = NULL;
2390c98d5d94SLen Brown 	core_even = NULL;
2391c98d5d94SLen Brown 	package_even = NULL;
2392103a8feaSLen Brown 
2393c98d5d94SLen Brown 	free(thread_odd);
2394c98d5d94SLen Brown 	free(core_odd);
2395c98d5d94SLen Brown 	free(package_odd);
2396103a8feaSLen Brown 
2397c98d5d94SLen Brown 	thread_odd = NULL;
2398c98d5d94SLen Brown 	core_odd = NULL;
2399c98d5d94SLen Brown 	package_odd = NULL;
2400103a8feaSLen Brown 
2401c98d5d94SLen Brown 	free(output_buffer);
2402c98d5d94SLen Brown 	output_buffer = NULL;
2403c98d5d94SLen Brown 	outp = NULL;
240436229897SLen Brown 
240536229897SLen Brown 	free_fd_percpu();
2406562a2d37SLen Brown 
2407562a2d37SLen Brown 	free(irq_column_2_cpu);
2408562a2d37SLen Brown 	free(irqs_per_cpu);
24090e2d8f05SLen Brown 
24100e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
24110e2d8f05SLen Brown 		if (cpus[i].put_ids)
24120e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
24130e2d8f05SLen Brown 	}
24140e2d8f05SLen Brown 	free(cpus);
2415103a8feaSLen Brown }
2416103a8feaSLen Brown 
241757a42a34SJosh Triplett 
241857a42a34SJosh Triplett /*
241995aebc44SJosh Triplett  * Parse a file containing a single int.
242095aebc44SJosh Triplett  */
242195aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
242295aebc44SJosh Triplett {
242395aebc44SJosh Triplett 	va_list args;
242495aebc44SJosh Triplett 	char path[PATH_MAX];
242595aebc44SJosh Triplett 	FILE *filep;
242695aebc44SJosh Triplett 	int value;
242795aebc44SJosh Triplett 
242895aebc44SJosh Triplett 	va_start(args, fmt);
242995aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
243095aebc44SJosh Triplett 	va_end(args);
243157a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2432b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2433b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
243495aebc44SJosh Triplett 	fclose(filep);
243595aebc44SJosh Triplett 	return value;
243695aebc44SJosh Triplett }
243795aebc44SJosh Triplett 
243895aebc44SJosh Triplett /*
2439c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2440c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2441c98d5d94SLen Brown  */
2442c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2443c98d5d94SLen Brown {
244495aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2445103a8feaSLen Brown }
2446103a8feaSLen Brown 
2447103a8feaSLen Brown int get_physical_package_id(int cpu)
2448103a8feaSLen Brown {
244995aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2450103a8feaSLen Brown }
2451103a8feaSLen Brown 
2452103a8feaSLen Brown int get_core_id(int cpu)
2453103a8feaSLen Brown {
245495aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2455103a8feaSLen Brown }
2456103a8feaSLen Brown 
2457ef605741SPrarit Bhargava void set_node_data(void)
2458ef605741SPrarit Bhargava {
2459ef605741SPrarit Bhargava 	char path[80];
2460ef605741SPrarit Bhargava 	FILE *filep;
2461ef605741SPrarit Bhargava 	int pkg, node, cpu;
2462ef605741SPrarit Bhargava 
2463ef605741SPrarit Bhargava 	struct pkg_node_info {
2464ef605741SPrarit Bhargava 		int count;
2465ef605741SPrarit Bhargava 		int min;
2466ef605741SPrarit Bhargava 	} *pni;
2467ef605741SPrarit Bhargava 
2468ef605741SPrarit Bhargava 	pni = calloc(topo.num_packages, sizeof(struct pkg_node_info));
2469ef605741SPrarit Bhargava 	if (!pni)
2470ef605741SPrarit Bhargava 		err(1, "calloc pkg_node_count");
2471ef605741SPrarit Bhargava 
2472ef605741SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++)
2473ef605741SPrarit Bhargava 		pni[pkg].min = topo.num_cpus;
2474ef605741SPrarit Bhargava 
2475ef605741SPrarit Bhargava 	for (node = 0; node <= topo.max_node_num; node++) {
2476ef605741SPrarit Bhargava 		/* find the "first" cpu in the node */
2477ef605741SPrarit Bhargava 		sprintf(path, "/sys/bus/node/devices/node%d/cpulist", node);
2478ef605741SPrarit Bhargava 		filep = fopen(path, "r");
2479ef605741SPrarit Bhargava 		if (!filep)
2480ef605741SPrarit Bhargava 			continue;
2481ef605741SPrarit Bhargava 		fscanf(filep, "%d", &cpu);
2482ef605741SPrarit Bhargava 		fclose(filep);
2483ef605741SPrarit Bhargava 
2484ef605741SPrarit Bhargava 		pkg = cpus[cpu].physical_package_id;
2485ef605741SPrarit Bhargava 		pni[pkg].count++;
2486ef605741SPrarit Bhargava 
2487ef605741SPrarit Bhargava 		if (node < pni[pkg].min)
2488ef605741SPrarit Bhargava 			pni[pkg].min = node;
2489ef605741SPrarit Bhargava 	}
2490ef605741SPrarit Bhargava 
2491ef605741SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++)
249270a9c6e8SPrarit Bhargava 		if (pni[pkg].count > topo.nodes_per_pkg)
249370a9c6e8SPrarit Bhargava 			topo.nodes_per_pkg = pni[0].count;
2494ef605741SPrarit Bhargava 
2495ef605741SPrarit Bhargava 	for (cpu = 0; cpu < topo.num_cpus; cpu++) {
2496ef605741SPrarit Bhargava 		pkg = cpus[cpu].physical_package_id;
2497ef605741SPrarit Bhargava 		node = cpus[cpu].physical_node_id;
2498ef605741SPrarit Bhargava 		cpus[cpu].logical_node_id = node - pni[pkg].min;
2499ef605741SPrarit Bhargava 	}
2500ef605741SPrarit Bhargava 	free(pni);
2501ef605741SPrarit Bhargava 
2502ef605741SPrarit Bhargava }
2503ef605741SPrarit Bhargava 
2504ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2505c98d5d94SLen Brown {
2506c98d5d94SLen Brown 	char path[80];
2507c98d5d94SLen Brown 	FILE *filep;
25080e2d8f05SLen Brown 	int i;
25090e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2510c98d5d94SLen Brown 
25110e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
25120e2d8f05SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
25130e2d8f05SLen Brown 			cpu, i);
25140e2d8f05SLen Brown 		filep = fopen(path, "r");
25150e2d8f05SLen Brown 		if (!filep)
25160e2d8f05SLen Brown 			continue;
25170e2d8f05SLen Brown 		fclose(filep);
25180e2d8f05SLen Brown 		return i;
25190e2d8f05SLen Brown 	}
25200e2d8f05SLen Brown 	return -1;
2521e275b388SDasaratharaman Chandramouli }
2522c98d5d94SLen Brown 
25230e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
25240e2d8f05SLen Brown {
25250e2d8f05SLen Brown 	char path[80], character;
25260e2d8f05SLen Brown 	FILE *filep;
25270e2d8f05SLen Brown 	unsigned long map;
25288cb48b32SPrarit Bhargava 	int so, shift, sib_core;
25290e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
25300e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
25310e2d8f05SLen Brown 	size_t size;
25328cb48b32SPrarit Bhargava 	int thread_id = 0;
25330e2d8f05SLen Brown 
25340e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
25358cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
25368cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
25370e2d8f05SLen Brown 	if (!thiscpu->put_ids)
25380e2d8f05SLen Brown 		return -1;
25390e2d8f05SLen Brown 
25400e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
25410e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
25420e2d8f05SLen Brown 
25430e2d8f05SLen Brown 	sprintf(path,
25440e2d8f05SLen Brown 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
25450e2d8f05SLen Brown 	filep = fopen_or_die(path, "r");
25460e2d8f05SLen Brown 	do {
25470e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
25480e2d8f05SLen Brown 		fscanf(filep, "%lx%c", &map, &character);
25490e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
25500e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
25518cb48b32SPrarit Bhargava 				so = shift + offset;
25528cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
25538cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
25548cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
25558cb48b32SPrarit Bhargava 					if ((so != cpu) &&
25568cb48b32SPrarit Bhargava 					    (cpus[so].thread_id < 0))
25578cb48b32SPrarit Bhargava 						cpus[so].thread_id =
25588cb48b32SPrarit Bhargava 								    thread_id++;
25598cb48b32SPrarit Bhargava 				}
25600e2d8f05SLen Brown 			}
25610e2d8f05SLen Brown 		}
25620e2d8f05SLen Brown 	} while (!strncmp(&character, ",", 1));
2563c98d5d94SLen Brown 	fclose(filep);
25640e2d8f05SLen Brown 
25650e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2566c98d5d94SLen Brown }
2567c98d5d94SLen Brown 
2568c98d5d94SLen Brown /*
2569c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2570c98d5d94SLen Brown  * skip non-present cpus
2571103a8feaSLen Brown  */
2572103a8feaSLen Brown 
2573c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2574c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2575c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2576c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2577c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2578c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2579c98d5d94SLen Brown {
258040f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2581c98d5d94SLen Brown 
2582c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
258340f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
258440f5cfe7SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node;
258540f5cfe7SPrarit Bhargava 			     ++core_no) {
2586c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
258770a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
2588c98d5d94SLen Brown 					struct thread_data *t, *t2;
2589c98d5d94SLen Brown 					struct core_data *c, *c2;
2590c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2591c98d5d94SLen Brown 
259240f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
259340f5cfe7SPrarit Bhargava 						       core_no, node_no,
259440f5cfe7SPrarit Bhargava 						       pkg_no);
2595c98d5d94SLen Brown 
2596c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
2597c98d5d94SLen Brown 						continue;
2598c98d5d94SLen Brown 
259940f5cfe7SPrarit Bhargava 					t2 = GET_THREAD(thread_base2, thread_no,
260040f5cfe7SPrarit Bhargava 							core_no, node_no,
260140f5cfe7SPrarit Bhargava 							pkg_no);
2602c98d5d94SLen Brown 
260340f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
260440f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
260540f5cfe7SPrarit Bhargava 					c2 = GET_CORE(core_base2, core_no,
260640f5cfe7SPrarit Bhargava 						      node_no,
260740f5cfe7SPrarit Bhargava 						      pkg_no);
2608c98d5d94SLen Brown 
2609c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
2610c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
2611c98d5d94SLen Brown 
2612c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
2613c98d5d94SLen Brown 					if (retval)
2614c98d5d94SLen Brown 						return retval;
2615c98d5d94SLen Brown 				}
2616c98d5d94SLen Brown 			}
2617c98d5d94SLen Brown 		}
261840f5cfe7SPrarit Bhargava 	}
2619c98d5d94SLen Brown 	return 0;
2620c98d5d94SLen Brown }
2621c98d5d94SLen Brown 
2622c98d5d94SLen Brown /*
2623c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2624c98d5d94SLen Brown  * return max_cpu number
2625c98d5d94SLen Brown  */
2626c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2627103a8feaSLen Brown {
2628103a8feaSLen Brown 	FILE *fp;
2629c98d5d94SLen Brown 	int cpu_num;
2630103a8feaSLen Brown 	int retval;
2631103a8feaSLen Brown 
263257a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2633103a8feaSLen Brown 
2634103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2635b2c95d90SJosh Triplett 	if (retval != 0)
2636b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2637103a8feaSLen Brown 
2638c98d5d94SLen Brown 	while (1) {
2639c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2640103a8feaSLen Brown 		if (retval != 1)
2641103a8feaSLen Brown 			break;
2642103a8feaSLen Brown 
2643c98d5d94SLen Brown 		retval = func(cpu_num);
2644c98d5d94SLen Brown 		if (retval) {
2645c98d5d94SLen Brown 			fclose(fp);
2646c98d5d94SLen Brown 			return(retval);
2647c98d5d94SLen Brown 		}
2648103a8feaSLen Brown 	}
2649103a8feaSLen Brown 	fclose(fp);
2650c98d5d94SLen Brown 	return 0;
2651103a8feaSLen Brown }
2652103a8feaSLen Brown 
2653103a8feaSLen Brown void re_initialize(void)
2654103a8feaSLen Brown {
2655c98d5d94SLen Brown 	free_all_buffers();
2656c98d5d94SLen Brown 	setup_all_buffers();
2657c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2658103a8feaSLen Brown }
2659103a8feaSLen Brown 
2660843c5791SPrarit Bhargava void set_max_cpu_num(void)
2661843c5791SPrarit Bhargava {
2662843c5791SPrarit Bhargava 	FILE *filep;
2663843c5791SPrarit Bhargava 	unsigned long dummy;
2664843c5791SPrarit Bhargava 
2665843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
2666843c5791SPrarit Bhargava 	filep = fopen_or_die(
2667843c5791SPrarit Bhargava 			"/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2668843c5791SPrarit Bhargava 			"r");
2669843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
26700e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
2671843c5791SPrarit Bhargava 	fclose(filep);
2672843c5791SPrarit Bhargava 	topo.max_cpu_num--; /* 0 based */
2673843c5791SPrarit Bhargava }
2674c98d5d94SLen Brown 
2675103a8feaSLen Brown /*
2676c98d5d94SLen Brown  * count_cpus()
2677c98d5d94SLen Brown  * remember the last one seen, it will be the max
2678103a8feaSLen Brown  */
2679c98d5d94SLen Brown int count_cpus(int cpu)
2680103a8feaSLen Brown {
2681843c5791SPrarit Bhargava 	topo.num_cpus++;
2682c98d5d94SLen Brown 	return 0;
2683103a8feaSLen Brown }
2684c98d5d94SLen Brown int mark_cpu_present(int cpu)
2685c98d5d94SLen Brown {
2686c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
268715aaa346SLen Brown 	return 0;
2688103a8feaSLen Brown }
2689103a8feaSLen Brown 
26908cb48b32SPrarit Bhargava int init_thread_id(int cpu)
26918cb48b32SPrarit Bhargava {
26928cb48b32SPrarit Bhargava 	cpus[cpu].thread_id = -1;
26938cb48b32SPrarit Bhargava 	return 0;
26948cb48b32SPrarit Bhargava }
26958cb48b32SPrarit Bhargava 
2696562a2d37SLen Brown /*
2697562a2d37SLen Brown  * snapshot_proc_interrupts()
2698562a2d37SLen Brown  *
2699562a2d37SLen Brown  * read and record summary of /proc/interrupts
2700562a2d37SLen Brown  *
2701562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2702562a2d37SLen Brown  */
2703562a2d37SLen Brown int snapshot_proc_interrupts(void)
2704562a2d37SLen Brown {
2705562a2d37SLen Brown 	static FILE *fp;
2706562a2d37SLen Brown 	int column, retval;
2707562a2d37SLen Brown 
2708562a2d37SLen Brown 	if (fp == NULL)
2709562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2710562a2d37SLen Brown 	else
2711562a2d37SLen Brown 		rewind(fp);
2712562a2d37SLen Brown 
2713562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2714562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2715562a2d37SLen Brown 		int cpu_number;
2716562a2d37SLen Brown 
2717562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2718562a2d37SLen Brown 		if (retval != 1)
2719562a2d37SLen Brown 			break;
2720562a2d37SLen Brown 
2721562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2722562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2723562a2d37SLen Brown 			return 1;
2724562a2d37SLen Brown 		}
2725562a2d37SLen Brown 
2726562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2727562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2728562a2d37SLen Brown 	}
2729562a2d37SLen Brown 
2730562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2731562a2d37SLen Brown 	while (1) {
2732562a2d37SLen Brown 		int column;
2733562a2d37SLen Brown 		char buf[64];
2734562a2d37SLen Brown 
2735562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2736562a2d37SLen Brown 		if (retval != 1)
2737562a2d37SLen Brown 			break;
2738562a2d37SLen Brown 
2739562a2d37SLen Brown 		/* read the count per cpu */
2740562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2741562a2d37SLen Brown 
2742562a2d37SLen Brown 			int cpu_number, irq_count;
2743562a2d37SLen Brown 
2744562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2745562a2d37SLen Brown 			if (retval != 1)
2746562a2d37SLen Brown 				break;
2747562a2d37SLen Brown 
2748562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2749562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2750562a2d37SLen Brown 
2751562a2d37SLen Brown 		}
2752562a2d37SLen Brown 
2753562a2d37SLen Brown 		while (getc(fp) != '\n')
2754562a2d37SLen Brown 			;	/* flush interrupt description */
2755562a2d37SLen Brown 
2756562a2d37SLen Brown 	}
2757562a2d37SLen Brown 	return 0;
2758562a2d37SLen Brown }
275927d47356SLen Brown /*
2760fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2761fdf676e5SLen Brown  *
2762fdf676e5SLen Brown  * record snapshot of
2763fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2764fdf676e5SLen Brown  *
2765fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2766fdf676e5SLen Brown  */
2767fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2768fdf676e5SLen Brown {
2769fdf676e5SLen Brown 	FILE *fp;
2770fdf676e5SLen Brown 	int retval;
2771fdf676e5SLen Brown 
2772fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2773fdf676e5SLen Brown 
2774fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2775fdf676e5SLen Brown 	if (retval != 1)
2776fdf676e5SLen Brown 		err(1, "GFX rc6");
2777fdf676e5SLen Brown 
2778fdf676e5SLen Brown 	fclose(fp);
2779fdf676e5SLen Brown 
2780fdf676e5SLen Brown 	return 0;
2781fdf676e5SLen Brown }
2782fdf676e5SLen Brown /*
278327d47356SLen Brown  * snapshot_gfx_mhz()
278427d47356SLen Brown  *
278527d47356SLen Brown  * record snapshot of
278627d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
278727d47356SLen Brown  *
278827d47356SLen Brown  * return 1 if config change requires a restart, else return 0
278927d47356SLen Brown  */
279027d47356SLen Brown int snapshot_gfx_mhz(void)
279127d47356SLen Brown {
279227d47356SLen Brown 	static FILE *fp;
279327d47356SLen Brown 	int retval;
279427d47356SLen Brown 
279527d47356SLen Brown 	if (fp == NULL)
279627d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
279722048c54SLen Brown 	else {
279827d47356SLen Brown 		rewind(fp);
279922048c54SLen Brown 		fflush(fp);
280022048c54SLen Brown 	}
280127d47356SLen Brown 
280227d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
280327d47356SLen Brown 	if (retval != 1)
280427d47356SLen Brown 		err(1, "GFX MHz");
280527d47356SLen Brown 
280627d47356SLen Brown 	return 0;
280727d47356SLen Brown }
2808562a2d37SLen Brown 
2809562a2d37SLen Brown /*
2810be0e54c4SLen Brown  * snapshot_cpu_lpi()
2811be0e54c4SLen Brown  *
2812be0e54c4SLen Brown  * record snapshot of
2813be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2814be0e54c4SLen Brown  *
2815be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2816be0e54c4SLen Brown  */
2817be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
2818be0e54c4SLen Brown {
2819be0e54c4SLen Brown 	FILE *fp;
2820be0e54c4SLen Brown 	int retval;
2821be0e54c4SLen Brown 
2822be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2823be0e54c4SLen Brown 
2824be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2825be0e54c4SLen Brown 	if (retval != 1)
2826be0e54c4SLen Brown 		err(1, "CPU LPI");
2827be0e54c4SLen Brown 
2828be0e54c4SLen Brown 	fclose(fp);
2829be0e54c4SLen Brown 
2830be0e54c4SLen Brown 	return 0;
2831be0e54c4SLen Brown }
2832be0e54c4SLen Brown /*
2833be0e54c4SLen Brown  * snapshot_sys_lpi()
2834be0e54c4SLen Brown  *
2835be0e54c4SLen Brown  * record snapshot of
2836be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2837be0e54c4SLen Brown  *
2838be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2839be0e54c4SLen Brown  */
2840be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
2841be0e54c4SLen Brown {
2842be0e54c4SLen Brown 	FILE *fp;
2843be0e54c4SLen Brown 	int retval;
2844be0e54c4SLen Brown 
2845be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2846be0e54c4SLen Brown 
2847be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2848be0e54c4SLen Brown 	if (retval != 1)
2849be0e54c4SLen Brown 		err(1, "SYS LPI");
2850be0e54c4SLen Brown 
2851be0e54c4SLen Brown 	fclose(fp);
2852be0e54c4SLen Brown 
2853be0e54c4SLen Brown 	return 0;
2854be0e54c4SLen Brown }
2855be0e54c4SLen Brown /*
2856562a2d37SLen Brown  * snapshot /proc and /sys files
2857562a2d37SLen Brown  *
2858562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2859562a2d37SLen Brown  */
2860562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2861562a2d37SLen Brown {
2862218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2863562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2864562a2d37SLen Brown 			return 1;
2865562a2d37SLen Brown 
2866812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2867fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2868fdf676e5SLen Brown 
2869812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
287027d47356SLen Brown 		snapshot_gfx_mhz();
287127d47356SLen Brown 
2872be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2873be0e54c4SLen Brown 		snapshot_cpu_lpi_us();
2874be0e54c4SLen Brown 
2875be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2876be0e54c4SLen Brown 		snapshot_sys_lpi_us();
2877be0e54c4SLen Brown 
2878562a2d37SLen Brown 	return 0;
2879562a2d37SLen Brown }
2880562a2d37SLen Brown 
28818aa2ed0bSLen Brown int exit_requested;
28828aa2ed0bSLen Brown 
28838aa2ed0bSLen Brown static void signal_handler (int signal)
28848aa2ed0bSLen Brown {
28858aa2ed0bSLen Brown 	switch (signal) {
28868aa2ed0bSLen Brown 	case SIGINT:
28878aa2ed0bSLen Brown 		exit_requested = 1;
28888aa2ed0bSLen Brown 		if (debug)
28898aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
28908aa2ed0bSLen Brown 		break;
289107211960SLen Brown 	case SIGUSR1:
289207211960SLen Brown 		if (debug > 1)
289307211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
289407211960SLen Brown 		break;
28958aa2ed0bSLen Brown 	}
2896b9ad8ee0SLen Brown 	/* make sure this manually-invoked interval is at least 1ms long */
2897b9ad8ee0SLen Brown 	nanosleep(&one_msec, NULL);
28988aa2ed0bSLen Brown }
28998aa2ed0bSLen Brown 
29008aa2ed0bSLen Brown void setup_signal_handler(void)
29018aa2ed0bSLen Brown {
29028aa2ed0bSLen Brown 	struct sigaction sa;
29038aa2ed0bSLen Brown 
29048aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
29058aa2ed0bSLen Brown 
29068aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
29078aa2ed0bSLen Brown 
29088aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
29098aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
291007211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
291107211960SLen Brown 		err(1, "sigaction SIGUSR1");
29128aa2ed0bSLen Brown }
2913b9ad8ee0SLen Brown 
291447936f94SArtem Bityutskiy void do_sleep(void)
2915b9ad8ee0SLen Brown {
2916b9ad8ee0SLen Brown 	struct timeval select_timeout;
2917b9ad8ee0SLen Brown 	fd_set readfds;
2918b9ad8ee0SLen Brown 	int retval;
2919b9ad8ee0SLen Brown 
2920b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
2921b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
2922b9ad8ee0SLen Brown 
292347936f94SArtem Bityutskiy 	if (!isatty(fileno(stdin))) {
292447936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
292547936f94SArtem Bityutskiy 		return;
292647936f94SArtem Bityutskiy 	}
2927b9ad8ee0SLen Brown 
292847936f94SArtem Bityutskiy 	select_timeout = interval_tv;
2929b9ad8ee0SLen Brown 	retval = select(1, &readfds, NULL, NULL, &select_timeout);
2930b9ad8ee0SLen Brown 
2931b9ad8ee0SLen Brown 	if (retval == 1) {
2932b9ad8ee0SLen Brown 		switch (getc(stdin)) {
2933b9ad8ee0SLen Brown 		case 'q':
2934b9ad8ee0SLen Brown 			exit_requested = 1;
2935b9ad8ee0SLen Brown 			break;
2936b9ad8ee0SLen Brown 		}
2937b9ad8ee0SLen Brown 		/* make sure this manually-invoked interval is at least 1ms long */
2938b9ad8ee0SLen Brown 		nanosleep(&one_msec, NULL);
2939b9ad8ee0SLen Brown 	}
2940b9ad8ee0SLen Brown }
294147936f94SArtem Bityutskiy 
2942*4c2122d4SLen Brown 
2943103a8feaSLen Brown void turbostat_loop()
2944103a8feaSLen Brown {
2945c98d5d94SLen Brown 	int retval;
2946e52966c0SLen Brown 	int restarted = 0;
2947023fe0acSChen Yu 	int done_iters = 0;
2948c98d5d94SLen Brown 
29498aa2ed0bSLen Brown 	setup_signal_handler();
29508aa2ed0bSLen Brown 
2951103a8feaSLen Brown restart:
2952e52966c0SLen Brown 	restarted++;
2953e52966c0SLen Brown 
2954562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2955c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2956*4c2122d4SLen Brown 	first_counter_read = 0;
2957d91bb17cSLen Brown 	if (retval < -1) {
2958d91bb17cSLen Brown 		exit(retval);
2959d91bb17cSLen Brown 	} else if (retval == -1) {
2960e52966c0SLen Brown 		if (restarted > 1) {
2961e52966c0SLen Brown 			exit(retval);
2962e52966c0SLen Brown 		}
2963c98d5d94SLen Brown 		re_initialize();
2964c98d5d94SLen Brown 		goto restart;
2965c98d5d94SLen Brown 	}
2966e52966c0SLen Brown 	restarted = 0;
2967023fe0acSChen Yu 	done_iters = 0;
2968103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2969103a8feaSLen Brown 
2970103a8feaSLen Brown 	while (1) {
2971c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2972103a8feaSLen Brown 			re_initialize();
2973103a8feaSLen Brown 			goto restart;
2974103a8feaSLen Brown 		}
2975b9ad8ee0SLen Brown 		do_sleep();
2976562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2977562a2d37SLen Brown 			goto restart;
2978c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
2979d91bb17cSLen Brown 		if (retval < -1) {
2980d91bb17cSLen Brown 			exit(retval);
2981d91bb17cSLen Brown 		} else if (retval == -1) {
298215aaa346SLen Brown 			re_initialize();
298315aaa346SLen Brown 			goto restart;
298415aaa346SLen Brown 		}
2985103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
2986103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
2987ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2988ba3dec99SLen Brown 			re_initialize();
2989ba3dec99SLen Brown 			goto restart;
2990ba3dec99SLen Brown 		}
2991c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
2992c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
2993b7d8c148SLen Brown 		flush_output_stdout();
29948aa2ed0bSLen Brown 		if (exit_requested)
29958aa2ed0bSLen Brown 			break;
2996023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
2997023fe0acSChen Yu 			break;
2998b9ad8ee0SLen Brown 		do_sleep();
2999562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3000562a2d37SLen Brown 			goto restart;
3001c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3002d91bb17cSLen Brown 		if (retval < -1) {
3003d91bb17cSLen Brown 			exit(retval);
3004d91bb17cSLen Brown 		} else if (retval == -1) {
3005103a8feaSLen Brown 			re_initialize();
3006103a8feaSLen Brown 			goto restart;
3007103a8feaSLen Brown 		}
3008103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3009103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3010ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3011ba3dec99SLen Brown 			re_initialize();
3012ba3dec99SLen Brown 			goto restart;
3013ba3dec99SLen Brown 		}
3014c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3015c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3016b7d8c148SLen Brown 		flush_output_stdout();
30178aa2ed0bSLen Brown 		if (exit_requested)
30188aa2ed0bSLen Brown 			break;
3019023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3020023fe0acSChen Yu 			break;
3021103a8feaSLen Brown 	}
3022103a8feaSLen Brown }
3023103a8feaSLen Brown 
3024103a8feaSLen Brown void check_dev_msr()
3025103a8feaSLen Brown {
3026103a8feaSLen Brown 	struct stat sb;
30277ce7d5deSPrarit Bhargava 	char pathname[32];
3028103a8feaSLen Brown 
30297ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
30307ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3031a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3032d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3033103a8feaSLen Brown }
3034103a8feaSLen Brown 
303598481e79SLen Brown void check_permissions()
3036103a8feaSLen Brown {
303798481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
303898481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
303998481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
304098481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
304198481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
304298481e79SLen Brown 	int do_exit = 0;
30437ce7d5deSPrarit Bhargava 	char pathname[32];
304498481e79SLen Brown 
304598481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
304698481e79SLen Brown 	cap_header->pid = getpid();
304798481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
304898481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
304998481e79SLen Brown 		err(-6, "capget(2) failed");
305098481e79SLen Brown 
305198481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
305298481e79SLen Brown 		do_exit++;
305398481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
305498481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
305598481e79SLen Brown 	}
305698481e79SLen Brown 
305798481e79SLen Brown 	/* test file permissions */
30587ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
30597ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
306098481e79SLen Brown 		do_exit++;
306198481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
306298481e79SLen Brown 	}
306398481e79SLen Brown 
306498481e79SLen Brown 	/* if all else fails, thell them to be root */
306598481e79SLen Brown 	if (do_exit)
3066b2c95d90SJosh Triplett 		if (getuid() != 0)
3067d7899447SLen Brown 			warnx("... or simply run as root");
306898481e79SLen Brown 
306998481e79SLen Brown 	if (do_exit)
307098481e79SLen Brown 		exit(-6);
3071103a8feaSLen Brown }
3072103a8feaSLen Brown 
3073d7899447SLen Brown /*
3074d7899447SLen Brown  * NHM adds support for additional MSRs:
3075d7899447SLen Brown  *
3076d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3077d7899447SLen Brown  *
3078ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
30791df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3080d7899447SLen Brown  *
3081cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3082cf4cbe53SLen Brown  *
3083d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3084d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3085d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3086d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3087d7899447SLen Brown  *
3088ee7e38e3SLen Brown  * Side effect:
30891df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
309033148d67SLen Brown  * sets has_misc_feature_control
3091d7899447SLen Brown  */
3092ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3093103a8feaSLen Brown {
3094ee7e38e3SLen Brown 	unsigned long long msr;
309521ed5574SLen Brown 	unsigned int base_ratio;
3096ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3097ee7e38e3SLen Brown 
3098103a8feaSLen Brown 	if (!genuine_intel)
3099103a8feaSLen Brown 		return 0;
3100103a8feaSLen Brown 
3101103a8feaSLen Brown 	if (family != 6)
3102103a8feaSLen Brown 		return 0;
3103103a8feaSLen Brown 
310421ed5574SLen Brown 	bclk = discover_bclk(family, model);
310521ed5574SLen Brown 
3106103a8feaSLen Brown 	switch (model) {
3107869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3108869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3109103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
3110869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
3111869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
3112869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3113869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
3114ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3115ee7e38e3SLen Brown 		break;
3116869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3117869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3118869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3119869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3120ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
312133148d67SLen Brown 		has_misc_feature_control = 1;
3122ee7e38e3SLen Brown 		break;
3123869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3124869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3125869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3126869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3127869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3128869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3129869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3130869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3131869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3132869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3133869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3134869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3135997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3136ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
313733148d67SLen Brown 		has_misc_feature_control = 1;
3138ee7e38e3SLen Brown 		break;
3139d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3140d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
314133148d67SLen Brown 		has_misc_feature_control = 1;
3142d8ebb442SLen Brown 		break;
3143869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3144cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3145869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3146ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3147ee7e38e3SLen Brown 		break;
3148869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3149ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3150cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3151ee7e38e3SLen Brown 		break;
3152869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3153005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3154ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3155ee7e38e3SLen Brown 		break;
3156869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3157ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3158869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3159e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
3160e4085d54SLen Brown 		break;
3161103a8feaSLen Brown 	default:
3162103a8feaSLen Brown 		return 0;
3163103a8feaSLen Brown 	}
31641df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3165e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3166ee7e38e3SLen Brown 
3167ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
316821ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
316921ed5574SLen Brown 
317021ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
317121ed5574SLen Brown 	has_base_hz = 1;
3172ee7e38e3SLen Brown 	return 1;
3173103a8feaSLen Brown }
31740f7887c4SLen Brown /*
3175495c7654SLen Brown  * SLV client has support for unique MSRs:
31760f7887c4SLen Brown  *
31770f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
31780f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
31790f7887c4SLen Brown  */
31800f7887c4SLen Brown 
31810f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
31820f7887c4SLen Brown {
31830f7887c4SLen Brown 	if (!genuine_intel)
31840f7887c4SLen Brown 		return 0;
31850f7887c4SLen Brown 
31860f7887c4SLen Brown 	switch (model) {
31870f7887c4SLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
31880f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MERRIFIELD:
31890f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MOOREFIELD:
31900f7887c4SLen Brown 		return 1;
31910f7887c4SLen Brown 	}
31920f7887c4SLen Brown 	return 0;
31930f7887c4SLen Brown }
31947170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
31957170a374SLen Brown {
31967170a374SLen Brown 
31977170a374SLen Brown 	if (!genuine_intel)
31987170a374SLen Brown 		return 0;
31997170a374SLen Brown 
32007170a374SLen Brown 	switch (model) {
32017170a374SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
32027170a374SLen Brown 		return 1;
32037170a374SLen Brown 	}
32047170a374SLen Brown 	return 0;
32057170a374SLen Brown }
3206ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3207ade0ebacSLen Brown {
3208ade0ebacSLen Brown 
3209ade0ebacSLen Brown 	if (!genuine_intel)
3210ade0ebacSLen Brown 		return 0;
3211ade0ebacSLen Brown 
3212ade0ebacSLen Brown 	switch (model) {
3213ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3214ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:
3215ade0ebacSLen Brown 		return 1;
3216ade0ebacSLen Brown 	}
3217ade0ebacSLen Brown 	return 0;
3218ade0ebacSLen Brown }
321934c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
322034c76197SLen Brown {
322134c76197SLen Brown 
322234c76197SLen Brown 	if (!genuine_intel)
322334c76197SLen Brown 		return 0;
322434c76197SLen Brown 
322534c76197SLen Brown 	switch (model) {
322634c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
322734c76197SLen Brown 		return 1;
322834c76197SLen Brown 	}
322934c76197SLen Brown 	return 0;
323034c76197SLen Brown }
32310f7887c4SLen Brown 
323231e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3233d7899447SLen Brown {
32340f7887c4SLen Brown 	if (has_slv_msrs(family, model))
32350f7887c4SLen Brown 		return 0;
32360f7887c4SLen Brown 
3237d7899447SLen Brown 	switch (model) {
3238d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
3239869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3240869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
3241869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3242005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3243d7899447SLen Brown 		return 0;
3244d7899447SLen Brown 	default:
3245d7899447SLen Brown 		return 1;
3246d7899447SLen Brown 	}
3247d7899447SLen Brown }
32480f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
32490f7887c4SLen Brown {
32500f7887c4SLen Brown 	if (has_slv_msrs(family, model))
32510f7887c4SLen Brown 		return 1;
32520f7887c4SLen Brown 
32530f7887c4SLen Brown 	return 0;
32540f7887c4SLen Brown }
32556574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
32566574a5d5SLen Brown {
32576574a5d5SLen Brown 	if (!genuine_intel)
32586574a5d5SLen Brown 		return 0;
32596574a5d5SLen Brown 
32606574a5d5SLen Brown 	if (family != 6)
32616574a5d5SLen Brown 		return 0;
32626574a5d5SLen Brown 
32636574a5d5SLen Brown 	switch (model) {
3264869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3265869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
32666574a5d5SLen Brown 		return 1;
32676574a5d5SLen Brown 	default:
32686574a5d5SLen Brown 		return 0;
32696574a5d5SLen Brown 	}
32706574a5d5SLen Brown }
3271fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3272fcd17211SLen Brown {
3273fcd17211SLen Brown 	if (!genuine_intel)
3274fcd17211SLen Brown 		return 0;
3275fcd17211SLen Brown 
3276fcd17211SLen Brown 	if (family != 6)
3277fcd17211SLen Brown 		return 0;
3278fcd17211SLen Brown 
3279fcd17211SLen Brown 	switch (model) {
3280869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3281fcd17211SLen Brown 		return 1;
3282fcd17211SLen Brown 	default:
3283fcd17211SLen Brown 		return 0;
3284fcd17211SLen Brown 	}
3285fcd17211SLen Brown }
3286fcd17211SLen Brown 
3287fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3288fb5d4327SDasaratharaman Chandramouli {
3289fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3290fb5d4327SDasaratharaman Chandramouli 		return 0;
3291fb5d4327SDasaratharaman Chandramouli 
3292fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3293fb5d4327SDasaratharaman Chandramouli 		return 0;
3294fb5d4327SDasaratharaman Chandramouli 
3295fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3296869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3297005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3298fb5d4327SDasaratharaman Chandramouli 		return 1;
3299fb5d4327SDasaratharaman Chandramouli 	default:
3300fb5d4327SDasaratharaman Chandramouli 		return 0;
3301fb5d4327SDasaratharaman Chandramouli 	}
3302fb5d4327SDasaratharaman Chandramouli }
330331e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
330431e07522SLen Brown {
330531e07522SLen Brown 	if (!genuine_intel)
330631e07522SLen Brown 		return 0;
330731e07522SLen Brown 
330831e07522SLen Brown 	if (family != 6)
330931e07522SLen Brown 		return 0;
331031e07522SLen Brown 
331131e07522SLen Brown 	switch (model) {
331231e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
331331e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
331431e07522SLen Brown 		return 1;
331531e07522SLen Brown 	default:
331631e07522SLen Brown 		return 0;
331731e07522SLen Brown 	}
331831e07522SLen Brown }
33196fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
33206fb3143bSLen Brown {
33216fb3143bSLen Brown 	if (!genuine_intel)
33226fb3143bSLen Brown 		return 0;
33236fb3143bSLen Brown 
33246fb3143bSLen Brown 	if (family != 6)
33256fb3143bSLen Brown 		return 0;
33266fb3143bSLen Brown 
33276fb3143bSLen Brown 	switch (model) {
3328869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3329869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3330869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3331869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3332869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3333869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3334869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3335869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3336869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3337869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3338869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3339869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3340869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3341997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3342869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
33436fb3143bSLen Brown 
3344869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3345005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
33466fb3143bSLen Brown 		return 1;
33476fb3143bSLen Brown 	default:
33486fb3143bSLen Brown 		return 0;
33496fb3143bSLen Brown 	}
33506fb3143bSLen Brown }
33516fb3143bSLen Brown 
3352fcd17211SLen Brown static void
33531b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3354fcd17211SLen Brown {
3355fcd17211SLen Brown 	if (!do_nhm_platform_info)
3356fcd17211SLen Brown 		return;
3357fcd17211SLen Brown 
3358fcd17211SLen Brown 	dump_nhm_platform_info();
3359fcd17211SLen Brown 
3360fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
3361fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
3362fcd17211SLen Brown 
3363fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
3364fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
3365fcd17211SLen Brown 
336631e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
336731e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
3368fcd17211SLen Brown 
33690f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
33700f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
33710f7887c4SLen Brown 
3372fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
3373fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
3374fb5d4327SDasaratharaman Chandramouli 
33756fb3143bSLen Brown 	if (has_config_tdp(family, model))
33766fb3143bSLen Brown 		dump_config_tdp();
33776fb3143bSLen Brown 
3378fcd17211SLen Brown 	dump_nhm_cst_cfg();
3379fcd17211SLen Brown }
3380fcd17211SLen Brown 
338141618e63SLen Brown static void
338241618e63SLen Brown dump_sysfs_cstate_config(void)
338341618e63SLen Brown {
338441618e63SLen Brown 	char path[64];
338541618e63SLen Brown 	char name_buf[16];
338641618e63SLen Brown 	char desc[64];
338741618e63SLen Brown 	FILE *input;
338841618e63SLen Brown 	int state;
338941618e63SLen Brown 	char *sp;
339041618e63SLen Brown 
339141618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
339241618e63SLen Brown 		return;
339341618e63SLen Brown 
339441618e63SLen Brown 	for (state = 0; state < 10; ++state) {
339541618e63SLen Brown 
339641618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
339741618e63SLen Brown 			base_cpu, state);
339841618e63SLen Brown 		input = fopen(path, "r");
339941618e63SLen Brown 		if (input == NULL)
340041618e63SLen Brown 			continue;
340141618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
340241618e63SLen Brown 
340341618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
340441618e63SLen Brown 		sp = strchr(name_buf, '-');
340541618e63SLen Brown 		if (!sp)
340641618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
340741618e63SLen Brown 		*sp = '\0';
340841618e63SLen Brown 
340941618e63SLen Brown 		fclose(input);
341041618e63SLen Brown 
341141618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
341241618e63SLen Brown 			base_cpu, state);
341341618e63SLen Brown 		input = fopen(path, "r");
341441618e63SLen Brown 		if (input == NULL)
341541618e63SLen Brown 			continue;
341641618e63SLen Brown 		fgets(desc, sizeof(desc), input);
341741618e63SLen Brown 
341841618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
341941618e63SLen Brown 		fclose(input);
342041618e63SLen Brown 	}
342141618e63SLen Brown }
34227293fccdSLen Brown static void
34237293fccdSLen Brown dump_sysfs_pstate_config(void)
34247293fccdSLen Brown {
34257293fccdSLen Brown 	char path[64];
34267293fccdSLen Brown 	char driver_buf[64];
34277293fccdSLen Brown 	char governor_buf[64];
34287293fccdSLen Brown 	FILE *input;
34297293fccdSLen Brown 	int turbo;
34307293fccdSLen Brown 
34317293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
34327293fccdSLen Brown 			base_cpu);
34337293fccdSLen Brown 	input = fopen(path, "r");
34347293fccdSLen Brown 	if (input == NULL) {
34357293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
34367293fccdSLen Brown 		return;
34377293fccdSLen Brown 	}
34387293fccdSLen Brown 	fgets(driver_buf, sizeof(driver_buf), input);
34397293fccdSLen Brown 	fclose(input);
34407293fccdSLen Brown 
34417293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
34427293fccdSLen Brown 			base_cpu);
34437293fccdSLen Brown 	input = fopen(path, "r");
34447293fccdSLen Brown 	if (input == NULL) {
34457293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
34467293fccdSLen Brown 		return;
34477293fccdSLen Brown 	}
34487293fccdSLen Brown 	fgets(governor_buf, sizeof(governor_buf), input);
34497293fccdSLen Brown 	fclose(input);
34507293fccdSLen Brown 
34517293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
34527293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
34537293fccdSLen Brown 
34547293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
34557293fccdSLen Brown 	input = fopen(path, "r");
34567293fccdSLen Brown 	if (input != NULL) {
34577293fccdSLen Brown 		fscanf(input, "%d", &turbo);
34587293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
34597293fccdSLen Brown 		fclose(input);
34607293fccdSLen Brown 	}
34617293fccdSLen Brown 
34627293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
34637293fccdSLen Brown 	input = fopen(path, "r");
34647293fccdSLen Brown 	if (input != NULL) {
34657293fccdSLen Brown 		fscanf(input, "%d", &turbo);
34667293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
34677293fccdSLen Brown 		fclose(input);
34687293fccdSLen Brown 	}
34697293fccdSLen Brown }
347041618e63SLen Brown 
34716574a5d5SLen Brown 
3472889facbeSLen Brown /*
3473889facbeSLen Brown  * print_epb()
3474889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3475889facbeSLen Brown  */
3476889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3477889facbeSLen Brown {
3478889facbeSLen Brown 	unsigned long long msr;
3479889facbeSLen Brown 	char *epb_string;
3480889facbeSLen Brown 	int cpu;
3481889facbeSLen Brown 
3482889facbeSLen Brown 	if (!has_epb)
3483889facbeSLen Brown 		return 0;
3484889facbeSLen Brown 
3485889facbeSLen Brown 	cpu = t->cpu_id;
3486889facbeSLen Brown 
3487889facbeSLen Brown 	/* EPB is per-package */
3488889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3489889facbeSLen Brown 		return 0;
3490889facbeSLen Brown 
3491889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3492b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3493889facbeSLen Brown 		return -1;
3494889facbeSLen Brown 	}
3495889facbeSLen Brown 
3496889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3497889facbeSLen Brown 		return 0;
3498889facbeSLen Brown 
3499e9be7dd6SLen Brown 	switch (msr & 0xF) {
3500889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3501889facbeSLen Brown 		epb_string = "performance";
3502889facbeSLen Brown 		break;
3503889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3504889facbeSLen Brown 		epb_string = "balanced";
3505889facbeSLen Brown 		break;
3506889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3507889facbeSLen Brown 		epb_string = "powersave";
3508889facbeSLen Brown 		break;
3509889facbeSLen Brown 	default:
3510889facbeSLen Brown 		epb_string = "custom";
3511889facbeSLen Brown 		break;
3512889facbeSLen Brown 	}
3513b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3514889facbeSLen Brown 
3515889facbeSLen Brown 	return 0;
3516889facbeSLen Brown }
35177f5c258eSLen Brown /*
35187f5c258eSLen Brown  * print_hwp()
35197f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
35207f5c258eSLen Brown  */
35217f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
35227f5c258eSLen Brown {
35237f5c258eSLen Brown 	unsigned long long msr;
35247f5c258eSLen Brown 	int cpu;
35257f5c258eSLen Brown 
35267f5c258eSLen Brown 	if (!has_hwp)
35277f5c258eSLen Brown 		return 0;
35287f5c258eSLen Brown 
35297f5c258eSLen Brown 	cpu = t->cpu_id;
35307f5c258eSLen Brown 
35317f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
35327f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
35337f5c258eSLen Brown 		return 0;
35347f5c258eSLen Brown 
35357f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3536b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
35377f5c258eSLen Brown 		return -1;
35387f5c258eSLen Brown 	}
35397f5c258eSLen Brown 
35407f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
35417f5c258eSLen Brown 		return 0;
35427f5c258eSLen Brown 
3543b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
35447f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
35457f5c258eSLen Brown 
35467f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
35477f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
35487f5c258eSLen Brown 		return 0;
35497f5c258eSLen Brown 
35507f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
35517f5c258eSLen Brown 		return 0;
35527f5c258eSLen Brown 
3553b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
35546dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
35557f5c258eSLen Brown 			cpu, msr,
35567f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
35577f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
35587f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
35597f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
35607f5c258eSLen Brown 
35617f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
35627f5c258eSLen Brown 		return 0;
35637f5c258eSLen Brown 
3564b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
35656dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
35667f5c258eSLen Brown 			cpu, msr,
35677f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
35687f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
35697f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
35707f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
35717f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
35727f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
35737f5c258eSLen Brown 
35747f5c258eSLen Brown 	if (has_hwp_pkg) {
35757f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
35767f5c258eSLen Brown 			return 0;
35777f5c258eSLen Brown 
3578b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
35796dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
35807f5c258eSLen Brown 			cpu, msr,
35817f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
35827f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
35837f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
35847f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
35857f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
35867f5c258eSLen Brown 	}
35877f5c258eSLen Brown 	if (has_hwp_notify) {
35887f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
35897f5c258eSLen Brown 			return 0;
35907f5c258eSLen Brown 
3591b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
35927f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
35937f5c258eSLen Brown 			cpu, msr,
35947f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
35957f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
35967f5c258eSLen Brown 	}
35977f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
35987f5c258eSLen Brown 		return 0;
35997f5c258eSLen Brown 
3600b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
36017f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
36027f5c258eSLen Brown 			cpu, msr,
36037f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
36047f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3605889facbeSLen Brown 
3606889facbeSLen Brown 	return 0;
3607889facbeSLen Brown }
3608889facbeSLen Brown 
36093a9a941dSLen Brown /*
36103a9a941dSLen Brown  * print_perf_limit()
36113a9a941dSLen Brown  */
36123a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
36133a9a941dSLen Brown {
36143a9a941dSLen Brown 	unsigned long long msr;
36153a9a941dSLen Brown 	int cpu;
36163a9a941dSLen Brown 
36173a9a941dSLen Brown 	cpu = t->cpu_id;
36183a9a941dSLen Brown 
36193a9a941dSLen Brown 	/* per-package */
36203a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36213a9a941dSLen Brown 		return 0;
36223a9a941dSLen Brown 
36233a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3624b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
36253a9a941dSLen Brown 		return -1;
36263a9a941dSLen Brown 	}
36273a9a941dSLen Brown 
36283a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
36293a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3630b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3631b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3632e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
36333a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3634e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3635e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3636e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3637e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3638e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3639e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3640e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3641e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3642e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3643e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3644e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3645e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3646b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3647e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
36483a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3649e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3650e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3651e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3652e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3653e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3654e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3655e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3656e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3657e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3658e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3659e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3660e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
36613a9a941dSLen Brown 
36623a9a941dSLen Brown 	}
36633a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
36643a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3665b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3666b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
36673a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
36683a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
36693a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
36703a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
36713a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
36723a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
36733a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
36743a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3675b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
36763a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
36773a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
36783a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
36793a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
36803a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
36813a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
36823a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
36833a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
36843a9a941dSLen Brown 	}
36853a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
36863a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3687b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3688b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
36893a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
36903a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
36913a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
36923a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
36933a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
36943a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3695b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
36963a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
36973a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
36983a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
36993a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37003a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37013a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37023a9a941dSLen Brown 	}
37033a9a941dSLen Brown 	return 0;
37043a9a941dSLen Brown }
37053a9a941dSLen Brown 
3706889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3707889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3708889facbeSLen Brown 
37091b69317dSColin Ian King double get_tdp(unsigned int model)
3710144b44b1SLen Brown {
3711144b44b1SLen Brown 	unsigned long long msr;
3712144b44b1SLen Brown 
3713144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
37147ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3715144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3716144b44b1SLen Brown 
3717144b44b1SLen Brown 	switch (model) {
3718869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
3719869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:
3720144b44b1SLen Brown 		return 30.0;
3721144b44b1SLen Brown 	default:
3722144b44b1SLen Brown 		return 135.0;
3723144b44b1SLen Brown 	}
3724144b44b1SLen Brown }
3725144b44b1SLen Brown 
372640ee8e3bSAndrey Semin /*
372740ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
372840ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
372940ee8e3bSAndrey Semin  */
373040ee8e3bSAndrey Semin static double
373140ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
373240ee8e3bSAndrey Semin {
373340ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
373440ee8e3bSAndrey Semin 
373540ee8e3bSAndrey Semin 	switch (model) {
3736869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3737869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3738869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3739869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3740005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
374140ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
374240ee8e3bSAndrey Semin 	default:
374340ee8e3bSAndrey Semin 		return (rapl_energy_units);
374440ee8e3bSAndrey Semin 	}
374540ee8e3bSAndrey Semin }
374640ee8e3bSAndrey Semin 
3747144b44b1SLen Brown 
3748889facbeSLen Brown /*
3749889facbeSLen Brown  * rapl_probe()
3750889facbeSLen Brown  *
3751144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3752889facbeSLen Brown  */
3753889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
3754889facbeSLen Brown {
3755889facbeSLen Brown 	unsigned long long msr;
3756144b44b1SLen Brown 	unsigned int time_unit;
3757889facbeSLen Brown 	double tdp;
3758889facbeSLen Brown 
3759889facbeSLen Brown 	if (!genuine_intel)
3760889facbeSLen Brown 		return;
3761889facbeSLen Brown 
3762889facbeSLen Brown 	if (family != 6)
3763889facbeSLen Brown 		return;
3764889facbeSLen Brown 
3765889facbeSLen Brown 	switch (model) {
3766869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3767869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3768869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3769869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3770869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3771869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3772869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3773144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3774812db3f7SLen Brown 		if (rapl_joules) {
3775812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3776812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3777812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3778812db3f7SLen Brown 		} else {
3779812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3780812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3781812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3782812db3f7SLen Brown 		}
3783889facbeSLen Brown 		break;
3784869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3785ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3786e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3787812db3f7SLen Brown 		if (rapl_joules)
3788812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3789812db3f7SLen Brown 		else
3790812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3791e4085d54SLen Brown 		break;
3792869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3793869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3794869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3795869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3796997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
379781824921SLen 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;
3798812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3799812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3800812db3f7SLen Brown 		if (rapl_joules) {
3801812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3802812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3803812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
380481824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3805812db3f7SLen Brown 		} else {
3806812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3807812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3808812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
380981824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3810812db3f7SLen Brown 		}
38110b2bb692SLen Brown 		break;
3812869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3813869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3814869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3815869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3816869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3817005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
38180b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 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_RAM_J);
3824812db3f7SLen Brown 		} else {
3825812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3826812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3827812db3f7SLen Brown 		}
3828e6f9bb3cSLen Brown 		break;
3829869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3830869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
38310b2bb692SLen 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;
3832812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3833812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3834812db3f7SLen Brown 		if (rapl_joules) {
3835812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3836812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3837812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3838812db3f7SLen Brown 		} else {
3839812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3840812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3841812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3842812db3f7SLen Brown 		}
3843144b44b1SLen Brown 		break;
3844869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3845869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3846144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3847812db3f7SLen Brown 		if (rapl_joules) {
3848812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3849812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3850812db3f7SLen Brown 		} else {
3851812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3852812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3853812db3f7SLen Brown 		}
3854889facbeSLen Brown 		break;
3855869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
38560f644909SJacob 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;
3857812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3858812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3859812db3f7SLen Brown 		if (rapl_joules) {
3860812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3861812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3862812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3863812db3f7SLen Brown 		} else {
3864812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3865812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3866812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3867812db3f7SLen Brown 		}
38680f644909SJacob Pan 		break;
3869889facbeSLen Brown 	default:
3870889facbeSLen Brown 		return;
3871889facbeSLen Brown 	}
3872889facbeSLen Brown 
3873889facbeSLen Brown 	/* units on package 0, verify later other packages match */
38747ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3875889facbeSLen Brown 		return;
3876889facbeSLen Brown 
3877889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3878869ce69eSLen Brown 	if (model == INTEL_FAM6_ATOM_SILVERMONT1)
3879144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3880144b44b1SLen Brown 	else
3881889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3882889facbeSLen Brown 
388340ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
388440ee8e3bSAndrey Semin 
3885144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3886144b44b1SLen Brown 	if (time_unit == 0)
3887144b44b1SLen Brown 		time_unit = 0xA;
3888889facbeSLen Brown 
3889144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3890144b44b1SLen Brown 
3891144b44b1SLen Brown 	tdp = get_tdp(model);
3892889facbeSLen Brown 
3893889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
389496e47158SLen Brown 	if (!quiet)
3895b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3896889facbeSLen Brown 
3897889facbeSLen Brown 	return;
3898889facbeSLen Brown }
3899889facbeSLen Brown 
39001b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
39013a9a941dSLen Brown {
39023a9a941dSLen Brown 	if (!genuine_intel)
39033a9a941dSLen Brown 		return;
39043a9a941dSLen Brown 
39053a9a941dSLen Brown 	if (family != 6)
39063a9a941dSLen Brown 		return;
39073a9a941dSLen Brown 
39083a9a941dSLen Brown 	switch (model) {
3909869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3910869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3911869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
39123a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
3913869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
39143a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
39153a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
39163a9a941dSLen Brown 	default:
39173a9a941dSLen Brown 		return;
39183a9a941dSLen Brown 	}
39193a9a941dSLen Brown }
39203a9a941dSLen Brown 
3921ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3922ac980e13SArtem Bityutskiy {
3923ac980e13SArtem Bityutskiy 	if (is_skx(family, model) || is_bdx(family, model))
3924ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
3925ac980e13SArtem Bityutskiy }
3926ac980e13SArtem Bityutskiy 
3927889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3928889facbeSLen Brown {
3929889facbeSLen Brown 	unsigned long long msr;
3930f4896fa5SLen Brown 	unsigned int dts, dts2;
3931889facbeSLen Brown 	int cpu;
3932889facbeSLen Brown 
3933889facbeSLen Brown 	if (!(do_dts || do_ptm))
3934889facbeSLen Brown 		return 0;
3935889facbeSLen Brown 
3936889facbeSLen Brown 	cpu = t->cpu_id;
3937889facbeSLen Brown 
3938889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
3939889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3940889facbeSLen Brown 		return 0;
3941889facbeSLen Brown 
3942889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3943b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3944889facbeSLen Brown 		return -1;
3945889facbeSLen Brown 	}
3946889facbeSLen Brown 
3947889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3948889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3949889facbeSLen Brown 			return 0;
3950889facbeSLen Brown 
3951889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3952b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3953889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
3954889facbeSLen Brown 
3955889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3956889facbeSLen Brown 			return 0;
3957889facbeSLen Brown 
3958889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3959889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3960b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3961889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3962889facbeSLen Brown 	}
3963889facbeSLen Brown 
3964889facbeSLen Brown 
3965f4896fa5SLen Brown 	if (do_dts && debug) {
3966889facbeSLen Brown 		unsigned int resolution;
3967889facbeSLen Brown 
3968889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3969889facbeSLen Brown 			return 0;
3970889facbeSLen Brown 
3971889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3972889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
3973b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3974889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
3975889facbeSLen Brown 
3976889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_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_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 	return 0;
3986889facbeSLen Brown }
3987889facbeSLen Brown 
3988889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3989889facbeSLen Brown {
3990b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
3991889facbeSLen Brown 		cpu, label,
3992889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
3993889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
3994889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3995889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
3996889facbeSLen Brown 
3997889facbeSLen Brown 	return;
3998889facbeSLen Brown }
3999889facbeSLen Brown 
4000889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4001889facbeSLen Brown {
4002889facbeSLen Brown 	unsigned long long msr;
4003889facbeSLen Brown 	int cpu;
4004889facbeSLen Brown 
4005889facbeSLen Brown 	if (!do_rapl)
4006889facbeSLen Brown 		return 0;
4007889facbeSLen Brown 
4008889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4009889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4010889facbeSLen Brown 		return 0;
4011889facbeSLen Brown 
4012889facbeSLen Brown 	cpu = t->cpu_id;
4013889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4014b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4015889facbeSLen Brown 		return -1;
4016889facbeSLen Brown 	}
4017889facbeSLen Brown 
4018889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4019889facbeSLen Brown 		return -1;
4020889facbeSLen Brown 
402196e47158SLen Brown 	fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
4022144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
402396e47158SLen Brown 
4024144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4025144b44b1SLen Brown 
4026889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4027889facbeSLen Brown                 	return -5;
4028889facbeSLen Brown 
4029889facbeSLen Brown 
4030b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4031889facbeSLen Brown 			cpu, msr,
4032889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4033889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4034889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4035889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4036889facbeSLen Brown 
4037144b44b1SLen Brown 	}
4038144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4039144b44b1SLen Brown 
4040889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4041889facbeSLen Brown 			return -9;
4042889facbeSLen Brown 
4043b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4044889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4045889facbeSLen Brown 
4046889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
4047b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4048889facbeSLen Brown 			cpu,
4049889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4050889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4051889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4052889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4053889facbeSLen Brown 	}
4054889facbeSLen Brown 
40550b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4056889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4057889facbeSLen Brown                 	return -6;
4058889facbeSLen Brown 
4059b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4060889facbeSLen Brown 			cpu, msr,
4061889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4062889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4063889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4064889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
40650b2bb692SLen Brown 	}
40660b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4067889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4068889facbeSLen Brown 			return -9;
4069b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4070889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4071889facbeSLen Brown 
4072889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
4073889facbeSLen Brown 	}
4074144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
4075889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4076889facbeSLen Brown 			return -7;
4077889facbeSLen Brown 
4078b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4079144b44b1SLen Brown 	}
40809148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4081889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4082889facbeSLen Brown 			return -9;
4083b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4084889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4085889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
4086889facbeSLen Brown 	}
4087889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
4088889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4089889facbeSLen Brown 			return -8;
4090889facbeSLen Brown 
4091b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4092889facbeSLen Brown 
4093889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4094889facbeSLen Brown 			return -9;
4095b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4096889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4097889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
4098889facbeSLen Brown 	}
4099889facbeSLen Brown 	return 0;
4100889facbeSLen Brown }
4101889facbeSLen Brown 
4102d7899447SLen Brown /*
4103d7899447SLen Brown  * SNB adds support for additional MSRs:
4104d7899447SLen Brown  *
4105d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
4106d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
4107d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
4108d7899447SLen Brown  */
4109103a8feaSLen Brown 
4110d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
4111103a8feaSLen Brown {
4112103a8feaSLen Brown 	if (!genuine_intel)
4113103a8feaSLen Brown 		return 0;
4114103a8feaSLen Brown 
4115103a8feaSLen Brown 	switch (model) {
4116869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4117869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4118869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
4119869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
4120869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
4121869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
4122869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
4123869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
4124869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
4125869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
4126869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4127869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
4128869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4129869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4130869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4131869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4132997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4133869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
4134869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4135ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
41365bbac26eSXiaolong Wang 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
4137103a8feaSLen Brown 		return 1;
4138103a8feaSLen Brown 	}
4139103a8feaSLen Brown 	return 0;
4140103a8feaSLen Brown }
4141103a8feaSLen Brown 
4142d7899447SLen Brown /*
4143d7899447SLen Brown  * HSW adds support for additional MSRs:
4144d7899447SLen Brown  *
4145d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
4146d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
4147d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
41485a63426eSLen Brown  *
41495a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
41505a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
41515a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
41525a63426eSLen Brown  *
4153d7899447SLen Brown  */
4154d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
4155ca58710fSKristen Carlson Accardi {
4156ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
4157ca58710fSKristen Carlson Accardi 		return 0;
4158ca58710fSKristen Carlson Accardi 
4159ca58710fSKristen Carlson Accardi 	switch (model) {
4160869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
4161869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
4162869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4163869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4164869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4165869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4166997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4167869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4168ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
4169ca58710fSKristen Carlson Accardi 		return 1;
4170ca58710fSKristen Carlson Accardi 	}
4171ca58710fSKristen Carlson Accardi 	return 0;
4172ca58710fSKristen Carlson Accardi }
4173ca58710fSKristen Carlson Accardi 
41740b2bb692SLen Brown /*
41750b2bb692SLen Brown  * SKL adds support for additional MSRS:
41760b2bb692SLen Brown  *
41770b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
41780b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
41790b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
41800b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
41810b2bb692SLen Brown  */
41820b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
41830b2bb692SLen Brown {
41840b2bb692SLen Brown 	if (!genuine_intel)
41850b2bb692SLen Brown 		return 0;
41860b2bb692SLen Brown 
41870b2bb692SLen Brown 	switch (model) {
4188869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4189869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4190869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4191869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4192997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
41930b2bb692SLen Brown 		return 1;
41940b2bb692SLen Brown 	}
41950b2bb692SLen Brown 	return 0;
41960b2bb692SLen Brown }
41970b2bb692SLen Brown 
4198144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
4199144b44b1SLen Brown {
4200144b44b1SLen Brown 	if (!genuine_intel)
4201144b44b1SLen Brown 		return 0;
4202144b44b1SLen Brown 	switch (model) {
4203869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
4204869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
4205144b44b1SLen Brown 		return 1;
4206144b44b1SLen Brown 	}
4207144b44b1SLen Brown 	return 0;
4208144b44b1SLen Brown }
4209144b44b1SLen Brown 
4210fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
4211fb5d4327SDasaratharaman Chandramouli {
4212fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
4213fb5d4327SDasaratharaman Chandramouli 		return 0;
4214fb5d4327SDasaratharaman Chandramouli 	switch (model) {
4215869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
4216005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
4217fb5d4327SDasaratharaman Chandramouli 		return 1;
4218fb5d4327SDasaratharaman Chandramouli 	}
4219fb5d4327SDasaratharaman Chandramouli 	return 0;
4220fb5d4327SDasaratharaman Chandramouli }
4221fb5d4327SDasaratharaman Chandramouli 
4222997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
4223997e5395SSrinivas Pandruvada {
4224997e5395SSrinivas Pandruvada 	if (!genuine_intel)
4225997e5395SSrinivas Pandruvada 		return 0;
4226997e5395SSrinivas Pandruvada 
4227997e5395SSrinivas Pandruvada 	switch (model) {
4228997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4229997e5395SSrinivas Pandruvada 		return 1;
4230997e5395SSrinivas Pandruvada 	}
4231997e5395SSrinivas Pandruvada 
4232997e5395SSrinivas Pandruvada 	return 0;
4233997e5395SSrinivas Pandruvada }
4234997e5395SSrinivas Pandruvada 
4235b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4236b2b34dfeSHubert Chrzaniuk {
4237b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
4238b2b34dfeSHubert Chrzaniuk 		return 1024;
4239b2b34dfeSHubert Chrzaniuk 	return 1;
4240b2b34dfeSHubert Chrzaniuk }
4241b2b34dfeSHubert Chrzaniuk 
4242144b44b1SLen Brown #define SLM_BCLK_FREQS 5
4243144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4244144b44b1SLen Brown 
4245144b44b1SLen Brown double slm_bclk(void)
4246144b44b1SLen Brown {
4247144b44b1SLen Brown 	unsigned long long msr = 3;
4248144b44b1SLen Brown 	unsigned int i;
4249144b44b1SLen Brown 	double freq;
4250144b44b1SLen Brown 
42517ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4252b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
4253144b44b1SLen Brown 
4254144b44b1SLen Brown 	i = msr & 0xf;
4255144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
4256b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
42570a91e551SColin Ian King 		i = 3;
4258144b44b1SLen Brown 	}
4259144b44b1SLen Brown 	freq = slm_freq_table[i];
4260144b44b1SLen Brown 
426196e47158SLen Brown 	if (!quiet)
4262b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4263144b44b1SLen Brown 
4264144b44b1SLen Brown 	return freq;
4265144b44b1SLen Brown }
4266144b44b1SLen Brown 
4267103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
4268103a8feaSLen Brown {
4269121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
4270103a8feaSLen Brown 		return 100.00;
4271144b44b1SLen Brown 	else if (is_slm(family, model))
4272144b44b1SLen Brown 		return slm_bclk();
4273103a8feaSLen Brown 	else
4274103a8feaSLen Brown 		return 133.33;
4275103a8feaSLen Brown }
4276103a8feaSLen Brown 
4277889facbeSLen Brown /*
4278889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4279889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
4280889facbeSLen Brown  * This is usually equal to tjMax.
4281889facbeSLen Brown  *
4282889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
4283889facbeSLen Brown  * but also allow cmdline over-ride with -T.
4284889facbeSLen Brown  *
4285889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
4286889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
4287889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4288889facbeSLen Brown  */
4289889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4290889facbeSLen Brown {
4291889facbeSLen Brown 	unsigned long long msr;
4292889facbeSLen Brown 	unsigned int target_c_local;
4293889facbeSLen Brown 	int cpu;
4294889facbeSLen Brown 
4295889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
4296889facbeSLen Brown 	if (!(do_dts || do_ptm))
4297889facbeSLen Brown 		return 0;
4298889facbeSLen Brown 
4299889facbeSLen Brown 	/* this is a per-package concept */
4300889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4301889facbeSLen Brown 		return 0;
4302889facbeSLen Brown 
4303889facbeSLen Brown 	cpu = t->cpu_id;
4304889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4305b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4306889facbeSLen Brown 		return -1;
4307889facbeSLen Brown 	}
4308889facbeSLen Brown 
4309889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
4310889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
4311b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4312889facbeSLen Brown 			cpu, tcc_activation_temp);
4313889facbeSLen Brown 		return 0;
4314889facbeSLen Brown 	}
4315889facbeSLen Brown 
4316889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
4317d7899447SLen Brown 	if (!do_nhm_platform_info)
4318889facbeSLen Brown 		goto guess;
4319889facbeSLen Brown 
43207ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4321889facbeSLen Brown 		goto guess;
4322889facbeSLen Brown 
43233482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
4324889facbeSLen Brown 
432596e47158SLen Brown 	if (!quiet)
4326b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4327889facbeSLen Brown 			cpu, msr, target_c_local);
4328889facbeSLen Brown 
43293482124aSJean Delvare 	if (!target_c_local)
4330889facbeSLen Brown 		goto guess;
4331889facbeSLen Brown 
4332889facbeSLen Brown 	tcc_activation_temp = target_c_local;
4333889facbeSLen Brown 
4334889facbeSLen Brown 	return 0;
4335889facbeSLen Brown 
4336889facbeSLen Brown guess:
4337889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
4338b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4339889facbeSLen Brown 		cpu, tcc_activation_temp);
4340889facbeSLen Brown 
4341889facbeSLen Brown 	return 0;
4342889facbeSLen Brown }
434369807a63SLen Brown 
4344aa8d8cc7SLen Brown void decode_feature_control_msr(void)
4345aa8d8cc7SLen Brown {
4346aa8d8cc7SLen Brown 	unsigned long long msr;
4347aa8d8cc7SLen Brown 
4348aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4349aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4350aa8d8cc7SLen Brown 			base_cpu, msr,
4351aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4352aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
4353aa8d8cc7SLen Brown }
4354aa8d8cc7SLen Brown 
435569807a63SLen Brown void decode_misc_enable_msr(void)
435669807a63SLen Brown {
435769807a63SLen Brown 	unsigned long long msr;
435869807a63SLen Brown 
4359f26b1519SLen Brown 	if (!genuine_intel)
4360f26b1519SLen Brown 		return;
4361f26b1519SLen Brown 
436269807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4363e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
436469807a63SLen Brown 			base_cpu, msr,
4365e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4366e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4367fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4368e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4369e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
437069807a63SLen Brown }
437169807a63SLen Brown 
437233148d67SLen Brown void decode_misc_feature_control(void)
437333148d67SLen Brown {
437433148d67SLen Brown 	unsigned long long msr;
437533148d67SLen Brown 
437633148d67SLen Brown 	if (!has_misc_feature_control)
437733148d67SLen Brown 		return;
437833148d67SLen Brown 
437933148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
438033148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
438133148d67SLen Brown 			base_cpu, msr,
438233148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
438333148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
438433148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
438533148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
438633148d67SLen Brown }
4387f0057310SLen Brown /*
4388f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
4389f0057310SLen Brown  *
4390f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
4391f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
4392f0057310SLen Brown  * bit[1] less so...
4393f0057310SLen Brown  */
4394f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
4395f0057310SLen Brown {
4396f0057310SLen Brown 	unsigned long long msr;
4397f0057310SLen Brown 
4398f0057310SLen Brown 	if (!do_nhm_platform_info)
4399f0057310SLen Brown 		return;
4400f0057310SLen Brown 
4401cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
4402cf4cbe53SLen Brown 		return;
4403cf4cbe53SLen Brown 
4404f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4405ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4406f0057310SLen Brown 			base_cpu, msr,
4407f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
4408ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
4409ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
4410f0057310SLen Brown }
441171616c8eSLen Brown /*
441271616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
441371616c8eSLen Brown  *
441471616c8eSLen Brown  * This MSRs are present on Silvermont processors,
441571616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
441671616c8eSLen Brown  */
441771616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
441871616c8eSLen Brown {
441971616c8eSLen Brown 	unsigned long long msr;
442071616c8eSLen Brown 
442171616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
442271616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
442371616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
442471616c8eSLen Brown 
442571616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
442671616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
442771616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
442871616c8eSLen Brown }
44297f5c258eSLen Brown 
4430fcd17211SLen Brown void process_cpuid()
4431103a8feaSLen Brown {
443261a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
4433103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
4434b3a34e93SLen Brown 	unsigned int has_turbo;
4435103a8feaSLen Brown 
4436103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4437103a8feaSLen Brown 
44385aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4439103a8feaSLen Brown 
4440103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4441103a8feaSLen Brown 		genuine_intel = 1;
4442103a8feaSLen Brown 
444396e47158SLen Brown 	if (!quiet)
4444b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4445103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4446103a8feaSLen Brown 
44475aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4448103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4449103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4450103a8feaSLen Brown 	stepping = fms & 0xf;
4451103a8feaSLen Brown 	if (family == 6 || family == 0xf)
4452103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
4453103a8feaSLen Brown 
445496e47158SLen Brown 	if (!quiet) {
4455b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
4456103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
4457d9d226ffSLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
445869807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
445969807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
4460aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
446169807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
446269807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
446369807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
446469807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
446569807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
4466d9d226ffSLen Brown 			edx & (1 << 28) ? "HT" : "-",
446769807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
446869807a63SLen Brown 	}
4469103a8feaSLen Brown 
4470b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
4471b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
4472103a8feaSLen Brown 
4473103a8feaSLen Brown 	/*
4474103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4475103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4476103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4477103a8feaSLen Brown 	 */
4478103a8feaSLen Brown 	ebx = ecx = edx = 0;
44795aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4480103a8feaSLen Brown 
448161a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4482103a8feaSLen Brown 
4483103a8feaSLen Brown 		/*
4484103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4485103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4486103a8feaSLen Brown 		 */
44875aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
44888209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4489d7899447SLen Brown 	}
4490103a8feaSLen Brown 
4491103a8feaSLen Brown 	/*
4492103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4493103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4494103a8feaSLen Brown 	 */
4495103a8feaSLen Brown 
44965aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
44978209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4498812db3f7SLen Brown 	if (has_aperf) {
4499812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4500812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4501812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4502812db3f7SLen Brown 	}
4503889facbeSLen Brown 	do_dts = eax & (1 << 0);
4504812db3f7SLen Brown 	if (do_dts)
4505812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4506b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4507889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4508812db3f7SLen Brown 	if (do_ptm)
4509812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
45107f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
45117f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
45127f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
45137f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
45147f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4515889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4516889facbeSLen Brown 
451796e47158SLen Brown 	if (!quiet)
4518b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
45197f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
45207f5c258eSLen Brown 			has_aperf ? "" : "No-",
4521b3a34e93SLen Brown 			has_turbo ? "" : "No-",
45227f5c258eSLen Brown 			do_dts ? "" : "No-",
45237f5c258eSLen Brown 			do_ptm ? "" : "No-",
45247f5c258eSLen Brown 			has_hwp ? "" : "No-",
45257f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
45267f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
45277f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
45287f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
45297f5c258eSLen Brown 			has_epb ? "" : "No-");
4530103a8feaSLen Brown 
453196e47158SLen Brown 	if (!quiet)
453269807a63SLen Brown 		decode_misc_enable_msr();
453369807a63SLen Brown 
453433148d67SLen Brown 
453596e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4536aa8d8cc7SLen Brown 		int has_sgx;
4537aa8d8cc7SLen Brown 
4538aa8d8cc7SLen Brown 		ecx = 0;
4539aa8d8cc7SLen Brown 
4540aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4541aa8d8cc7SLen Brown 
4542aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4543aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4544aa8d8cc7SLen Brown 
4545aa8d8cc7SLen Brown 		if (has_sgx)
4546aa8d8cc7SLen Brown 			decode_feature_control_msr();
4547aa8d8cc7SLen Brown 	}
4548aa8d8cc7SLen Brown 
454961a87ba7SLen Brown 	if (max_level >= 0x15) {
45508a5bdf41SLen Brown 		unsigned int eax_crystal;
45518a5bdf41SLen Brown 		unsigned int ebx_tsc;
45528a5bdf41SLen Brown 
45538a5bdf41SLen Brown 		/*
45548a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
45558a5bdf41SLen Brown 		 */
45568a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
45575aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
45588a5bdf41SLen Brown 
45598a5bdf41SLen Brown 		if (ebx_tsc != 0) {
45608a5bdf41SLen Brown 
456196e47158SLen Brown 			if (!quiet && (ebx != 0))
4562b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
45638a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
45648a5bdf41SLen Brown 
45658a5bdf41SLen Brown 			if (crystal_hz == 0)
45668a5bdf41SLen Brown 				switch(model) {
4567869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4568869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4569869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4570869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4571e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4572e8efbc80SLen Brown 					break;
45737268d407SLen Brown 				case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
4574ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4575ec53e594SLen Brown 					break;
4576869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4577ac01ac13SLen Brown 				case INTEL_FAM6_ATOM_GEMINI_LAKE:
4578e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
45798a5bdf41SLen Brown 					break;
45808a5bdf41SLen Brown 				default:
45818a5bdf41SLen Brown 					crystal_hz = 0;
45828a5bdf41SLen Brown 			}
45838a5bdf41SLen Brown 
45848a5bdf41SLen Brown 			if (crystal_hz) {
45858a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
458696e47158SLen Brown 				if (!quiet)
4587b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
45888a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
45898a5bdf41SLen Brown 			}
45908a5bdf41SLen Brown 		}
45918a5bdf41SLen Brown 	}
459261a87ba7SLen Brown 	if (max_level >= 0x16) {
459361a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
459461a87ba7SLen Brown 
459561a87ba7SLen Brown 		/*
459661a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
459761a87ba7SLen Brown 		 */
459861a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
459961a87ba7SLen Brown 
46005aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
460196e47158SLen Brown 		if (!quiet)
4602b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
460361a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
460461a87ba7SLen Brown 	}
46058a5bdf41SLen Brown 
4606b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4607b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4608b2b34dfeSHubert Chrzaniuk 
4609812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4610812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4611812db3f7SLen Brown 
4612812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4613812db3f7SLen Brown 		do_nhm_platform_info = 1;
4614812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4615812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4616812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4617812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4618812db3f7SLen Brown 	}
4619d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4620812db3f7SLen Brown 
4621812db3f7SLen Brown 	if (do_snb_cstates)
4622812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4623812db3f7SLen Brown 
46245a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
46250f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
46260f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
46270f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
46280f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
46290f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
46300f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
46310f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
46320f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
46330539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
46340f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
46350f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
46360f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
46370f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
46380539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
46390539ba11SLen Brown 		use_c1_residency_msr = 1;
46400539ba11SLen Brown 	}
46417170a374SLen Brown 	if (is_dnv(family, model)) {
46427170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
46437170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
46447170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
46457170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
46467170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
46477170a374SLen Brown 		use_c1_residency_msr = 1;
46487170a374SLen Brown 	}
464934c76197SLen Brown 	if (is_skx(family, model)) {
465034c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
465134c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
465234c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
465334c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
465434c76197SLen Brown 	}
4655ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4656ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4657ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4658ade0ebacSLen Brown 	}
46590f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
46600f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
46610f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
46620f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
46630f47c08dSLen Brown 	}
46645a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
4665a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4666a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4667a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4668a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4669a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4670a99d8730SLen Brown 	}
4671144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4672fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4673997e5395SSrinivas Pandruvada 	do_cnl_cstates = is_cnl(family, model);
4674103a8feaSLen Brown 
467596e47158SLen Brown 	if (!quiet)
4676f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4677f0057310SLen Brown 
467896e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
467971616c8eSLen Brown 		decode_c6_demotion_policy_msr();
468071616c8eSLen Brown 
4681889facbeSLen Brown 	rapl_probe(family, model);
46823a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4683ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
4684889facbeSLen Brown 
468596e47158SLen Brown 	if (!quiet)
46861b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4687fcd17211SLen Brown 
468841618e63SLen Brown 	if (!quiet)
468941618e63SLen Brown 		dump_sysfs_cstate_config();
46907293fccdSLen Brown 	if (!quiet)
46917293fccdSLen Brown 		dump_sysfs_pstate_config();
469241618e63SLen Brown 
4693a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4694a2b7b749SLen Brown 		calculate_tsc_tweak();
4695a2b7b749SLen Brown 
4696812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4697812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4698fdf676e5SLen Brown 
4699812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4700812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
470127d47356SLen Brown 
4702be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4703be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
4704be0e54c4SLen Brown 	else
4705be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
4706be0e54c4SLen Brown 
4707be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4708be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
4709be0e54c4SLen Brown 	else
4710be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
4711be0e54c4SLen Brown 
471296e47158SLen Brown 	if (!quiet)
471333148d67SLen Brown 		decode_misc_feature_control();
471433148d67SLen Brown 
4715889facbeSLen Brown 	return;
4716103a8feaSLen Brown }
4717103a8feaSLen Brown 
4718103a8feaSLen Brown /*
4719103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4720103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4721103a8feaSLen Brown  */
4722103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4723103a8feaSLen Brown {
4724103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4725103a8feaSLen Brown 		return 1;
4726103a8feaSLen Brown 	else
4727103a8feaSLen Brown 		return 0;
4728103a8feaSLen Brown }
4729103a8feaSLen Brown 
4730103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4731103a8feaSLen Brown {
4732103a8feaSLen Brown 	return 0;
4733103a8feaSLen Brown }
4734103a8feaSLen Brown 
4735c98d5d94SLen Brown void topology_probe()
4736c98d5d94SLen Brown {
4737c98d5d94SLen Brown 	int i;
4738c98d5d94SLen Brown 	int max_core_id = 0;
4739c98d5d94SLen Brown 	int max_package_id = 0;
4740c98d5d94SLen Brown 	int max_siblings = 0;
4741c98d5d94SLen Brown 
4742c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4743843c5791SPrarit Bhargava 	set_max_cpu_num();
4744c98d5d94SLen Brown 	topo.num_cpus = 0;
4745c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4746c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4747812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4748c98d5d94SLen Brown 
4749d8af6f5fSLen Brown 	if (debug > 1)
4750b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4751c98d5d94SLen Brown 
4752c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4753b2c95d90SJosh Triplett 	if (cpus == NULL)
4754b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4755c98d5d94SLen Brown 
4756c98d5d94SLen Brown 	/*
4757c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4758c98d5d94SLen Brown 	 */
4759c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4760b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4761b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4762c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4763c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4764c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4765c98d5d94SLen Brown 
4766c98d5d94SLen Brown 	/*
47671ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
47681ef7d21aSLen Brown 	 */
47691ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
47701ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
47711ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
47721ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
47731ef7d21aSLen Brown 	}
47741ef7d21aSLen Brown 
47751ef7d21aSLen Brown 	/*
4776c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4777c98d5d94SLen Brown 	 */
4778c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4779b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4780b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4781c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4782c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4783c98d5d94SLen Brown 
47848cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
4785c98d5d94SLen Brown 
4786c98d5d94SLen Brown 	/*
4787c98d5d94SLen Brown 	 * For online cpus
4788c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4789c98d5d94SLen Brown 	 */
4790c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4791c98d5d94SLen Brown 		int siblings;
4792c98d5d94SLen Brown 
4793c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4794d8af6f5fSLen Brown 			if (debug > 1)
4795b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4796c98d5d94SLen Brown 			continue;
4797c98d5d94SLen Brown 		}
4798c98d5d94SLen Brown 
47990e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
48000e2d8f05SLen Brown 
48010e2d8f05SLen Brown 		/* get package information */
4802c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4803c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4804c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4805c98d5d94SLen Brown 
48060e2d8f05SLen Brown 		/* get numa node information */
4807ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4808ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
4809ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
48100e2d8f05SLen Brown 
48110e2d8f05SLen Brown 		/* get core information */
48120e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
48130e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
48140e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
48150e2d8f05SLen Brown 
48160e2d8f05SLen Brown 		/* get thread information */
48170e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
4818c98d5d94SLen Brown 		if (siblings > max_siblings)
4819c98d5d94SLen Brown 			max_siblings = siblings;
48208cb48b32SPrarit Bhargava 		if (cpus[i].thread_id != -1)
48218cb48b32SPrarit Bhargava 			topo.num_cores++;
48220e2d8f05SLen Brown 
4823d8af6f5fSLen Brown 		if (debug > 1)
48248cb48b32SPrarit Bhargava 			fprintf(outf,
48258cb48b32SPrarit Bhargava 				"cpu %d pkg %d node %d core %d thread %d\n",
48260e2d8f05SLen Brown 				i, cpus[i].physical_package_id,
4827ef605741SPrarit Bhargava 				cpus[i].physical_node_id,
48288cb48b32SPrarit Bhargava 				cpus[i].physical_core_id,
48298cb48b32SPrarit Bhargava 				cpus[i].thread_id);
4830c98d5d94SLen Brown 	}
4831ef605741SPrarit Bhargava 
483270a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
4833d8af6f5fSLen Brown 	if (debug > 1)
4834b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
483570a9c6e8SPrarit Bhargava 			max_core_id, topo.cores_per_node);
483670a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
4837812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
4838c98d5d94SLen Brown 
4839c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
4840d8af6f5fSLen Brown 	if (debug > 1)
4841b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4842c98d5d94SLen Brown 			max_package_id, topo.num_packages);
48437da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
4844812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
4845c98d5d94SLen Brown 
4846ef605741SPrarit Bhargava 	set_node_data();
4847ef605741SPrarit Bhargava 	if (debug > 1)
484870a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
484901235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
485001235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
4851ef605741SPrarit Bhargava 
485270a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
4853d8af6f5fSLen Brown 	if (debug > 1)
4854b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
4855c98d5d94SLen Brown }
4856c98d5d94SLen Brown 
4857c98d5d94SLen Brown void
485840f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c,
485940f5cfe7SPrarit Bhargava 		  struct pkg_data **p)
4860c98d5d94SLen Brown {
4861c98d5d94SLen Brown 	int i;
486240f5cfe7SPrarit Bhargava 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
486340f5cfe7SPrarit Bhargava 			topo.num_packages;
486440f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
4865c98d5d94SLen Brown 
486640f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
4867c98d5d94SLen Brown 	if (*t == NULL)
4868c98d5d94SLen Brown 		goto error;
4869c98d5d94SLen Brown 
487040f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
4871c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
4872c98d5d94SLen Brown 
487340f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
4874c98d5d94SLen Brown 	if (*c == NULL)
4875c98d5d94SLen Brown 		goto error;
4876c98d5d94SLen Brown 
487740f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
4878c98d5d94SLen Brown 		(*c)[i].core_id = -1;
4879c98d5d94SLen Brown 
4880678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
4881c98d5d94SLen Brown 	if (*p == NULL)
4882c98d5d94SLen Brown 		goto error;
4883c98d5d94SLen Brown 
4884c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
4885c98d5d94SLen Brown 		(*p)[i].package_id = i;
4886c98d5d94SLen Brown 
4887c98d5d94SLen Brown 	return;
4888c98d5d94SLen Brown error:
4889b2c95d90SJosh Triplett 	err(1, "calloc counters");
4890c98d5d94SLen Brown }
4891c98d5d94SLen Brown /*
4892c98d5d94SLen Brown  * init_counter()
4893c98d5d94SLen Brown  *
4894c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4895c98d5d94SLen Brown  */
4896c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
48978cb48b32SPrarit Bhargava 	struct pkg_data *pkg_base, int cpu_id)
4898c98d5d94SLen Brown {
48998cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
490040f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
49018cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
49028cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
4903c98d5d94SLen Brown 	struct thread_data *t;
4904c98d5d94SLen Brown 	struct core_data *c;
4905c98d5d94SLen Brown 	struct pkg_data *p;
4906c98d5d94SLen Brown 
490740f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
490840f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
49098cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
4910c98d5d94SLen Brown 
4911c98d5d94SLen Brown 	t->cpu_id = cpu_id;
49128cb48b32SPrarit Bhargava 	if (thread_id == 0) {
4913c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4914c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
4915c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4916c98d5d94SLen Brown 	}
4917c98d5d94SLen Brown 
49188cb48b32SPrarit Bhargava 	c->core_id = core_id;
49198cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
4920c98d5d94SLen Brown }
4921c98d5d94SLen Brown 
4922c98d5d94SLen Brown 
4923c98d5d94SLen Brown int initialize_counters(int cpu_id)
4924c98d5d94SLen Brown {
49258cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
49268cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
4927c98d5d94SLen Brown 	return 0;
4928c98d5d94SLen Brown }
4929c98d5d94SLen Brown 
4930c98d5d94SLen Brown void allocate_output_buffer()
4931c98d5d94SLen Brown {
49323b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4933c98d5d94SLen Brown 	outp = output_buffer;
4934b2c95d90SJosh Triplett 	if (outp == NULL)
4935b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
4936c98d5d94SLen Brown }
493736229897SLen Brown void allocate_fd_percpu(void)
493836229897SLen Brown {
493901a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
494036229897SLen Brown 	if (fd_percpu == NULL)
494136229897SLen Brown 		err(-1, "calloc fd_percpu");
494236229897SLen Brown }
4943562a2d37SLen Brown void allocate_irq_buffers(void)
4944562a2d37SLen Brown {
4945562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4946562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
4947562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
4948c98d5d94SLen Brown 
494901a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4950562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
495101a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
4952562a2d37SLen Brown }
4953c98d5d94SLen Brown void setup_all_buffers(void)
4954c98d5d94SLen Brown {
4955c98d5d94SLen Brown 	topology_probe();
4956562a2d37SLen Brown 	allocate_irq_buffers();
495736229897SLen Brown 	allocate_fd_percpu();
4958c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
4959c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
4960c98d5d94SLen Brown 	allocate_output_buffer();
4961c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
4962c98d5d94SLen Brown }
49633b4d5c7fSAndy Shevchenko 
49647ce7d5deSPrarit Bhargava void set_base_cpu(void)
49657ce7d5deSPrarit Bhargava {
49667ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
49677ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
49687ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
49697ce7d5deSPrarit Bhargava 
49707ce7d5deSPrarit Bhargava 	if (debug > 1)
4971b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
49727ce7d5deSPrarit Bhargava }
49737ce7d5deSPrarit Bhargava 
4974103a8feaSLen Brown void turbostat_init()
4975103a8feaSLen Brown {
49767ce7d5deSPrarit Bhargava 	setup_all_buffers();
49777ce7d5deSPrarit Bhargava 	set_base_cpu();
4978103a8feaSLen Brown 	check_dev_msr();
497998481e79SLen Brown 	check_permissions();
4980fcd17211SLen Brown 	process_cpuid();
4981103a8feaSLen Brown 
4982103a8feaSLen Brown 
498396e47158SLen Brown 	if (!quiet)
49847f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
49857f5c258eSLen Brown 
498696e47158SLen Brown 	if (!quiet)
4987889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
4988889facbeSLen Brown 
498996e47158SLen Brown 	if (!quiet)
49903a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
49913a9a941dSLen Brown 
499296e47158SLen Brown 	if (!quiet)
4993889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
4994889facbeSLen Brown 
4995889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
4996889facbeSLen Brown 
499796e47158SLen Brown 	if (!quiet)
4998889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
49995a63426eSLen Brown 
500096e47158SLen Brown 	if (!quiet && do_irtl_snb)
50015a63426eSLen Brown 		print_irtl();
5002103a8feaSLen Brown }
5003103a8feaSLen Brown 
5004103a8feaSLen Brown int fork_it(char **argv)
5005103a8feaSLen Brown {
5006103a8feaSLen Brown 	pid_t child_pid;
5007d91bb17cSLen Brown 	int status;
5008d15cf7c1SLen Brown 
5009218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5010d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
5011*4c2122d4SLen Brown 	first_counter_read = 0;
5012d91bb17cSLen Brown 	if (status)
5013d91bb17cSLen Brown 		exit(status);
5014d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
5015d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5016103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
5017103a8feaSLen Brown 
5018103a8feaSLen Brown 	child_pid = fork();
5019103a8feaSLen Brown 	if (!child_pid) {
5020103a8feaSLen Brown 		/* child */
5021103a8feaSLen Brown 		execvp(argv[0], argv);
50220815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
5023103a8feaSLen Brown 	} else {
5024103a8feaSLen Brown 
5025103a8feaSLen Brown 		/* parent */
5026b2c95d90SJosh Triplett 		if (child_pid == -1)
5027b2c95d90SJosh Triplett 			err(1, "fork");
5028103a8feaSLen Brown 
5029103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
5030103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
5031b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
5032b2c95d90SJosh Triplett 			err(status, "waitpid");
5033103a8feaSLen Brown 	}
5034c98d5d94SLen Brown 	/*
5035c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
5036c98d5d94SLen Brown 	 * because re-starting is problematic when forking
5037c98d5d94SLen Brown 	 */
5038218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5039c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
5040103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
5041103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
5042ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5043ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
5044ba3dec99SLen Brown 	else {
5045c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
5046c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
5047ba3dec99SLen Brown 	}
5048103a8feaSLen Brown 
5049b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5050b7d8c148SLen Brown 
5051b7d8c148SLen Brown 	flush_output_stderr();
5052103a8feaSLen Brown 
5053d91bb17cSLen Brown 	return status;
5054103a8feaSLen Brown }
5055103a8feaSLen Brown 
50563b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
50573b4d5c7fSAndy Shevchenko {
50583b4d5c7fSAndy Shevchenko 	int status;
50593b4d5c7fSAndy Shevchenko 
5060218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
50613b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
50623b4d5c7fSAndy Shevchenko 	if (status)
50633b4d5c7fSAndy Shevchenko 		return status;
50643b4d5c7fSAndy Shevchenko 
50653b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
50663b4d5c7fSAndy Shevchenko 	if (status)
50673b4d5c7fSAndy Shevchenko 		return status;
50683b4d5c7fSAndy Shevchenko 
5069b7d8c148SLen Brown 	flush_output_stdout();
50703b4d5c7fSAndy Shevchenko 
50713b4d5c7fSAndy Shevchenko 	return status;
50723b4d5c7fSAndy Shevchenko }
50733b4d5c7fSAndy Shevchenko 
5074d8af6f5fSLen Brown void print_version() {
5075201d4f50SLen Brown 	fprintf(outf, "turbostat version 18.06.01"
5076d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
5077d8af6f5fSLen Brown }
5078d8af6f5fSLen Brown 
5079495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
5080495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
508141618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
5082388e9c81SLen Brown {
5083388e9c81SLen Brown 	struct msr_counter *msrp;
5084388e9c81SLen Brown 
5085388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
5086388e9c81SLen Brown 	if (msrp == NULL) {
5087388e9c81SLen Brown 		perror("calloc");
5088388e9c81SLen Brown 		exit(1);
5089388e9c81SLen Brown 	}
5090388e9c81SLen Brown 
5091388e9c81SLen Brown 	msrp->msr_num = msr_num;
5092388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
5093495c7654SLen Brown 	if (path)
5094495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
5095388e9c81SLen Brown 	msrp->width = width;
5096388e9c81SLen Brown 	msrp->type = type;
5097388e9c81SLen Brown 	msrp->format = format;
509841618e63SLen Brown 	msrp->flags = flags;
5099388e9c81SLen Brown 
5100388e9c81SLen Brown 	switch (scope) {
5101388e9c81SLen Brown 
5102388e9c81SLen Brown 	case SCOPE_CPU:
5103388e9c81SLen Brown 		msrp->next = sys.tp;
5104388e9c81SLen Brown 		sys.tp = msrp;
5105678a3bd1SLen Brown 		sys.added_thread_counters++;
51060748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5107678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
5108678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5109678a3bd1SLen Brown 			exit(-1);
5110678a3bd1SLen Brown 		}
5111388e9c81SLen Brown 		break;
5112388e9c81SLen Brown 
5113388e9c81SLen Brown 	case SCOPE_CORE:
5114388e9c81SLen Brown 		msrp->next = sys.cp;
5115388e9c81SLen Brown 		sys.cp = msrp;
5116678a3bd1SLen Brown 		sys.added_core_counters++;
5117678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5118678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
5119678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5120678a3bd1SLen Brown 			exit(-1);
5121678a3bd1SLen Brown 		}
5122388e9c81SLen Brown 		break;
5123388e9c81SLen Brown 
5124388e9c81SLen Brown 	case SCOPE_PACKAGE:
5125388e9c81SLen Brown 		msrp->next = sys.pp;
5126388e9c81SLen Brown 		sys.pp = msrp;
5127678a3bd1SLen Brown 		sys.added_package_counters++;
5128678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5129678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
5130678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5131678a3bd1SLen Brown 			exit(-1);
5132678a3bd1SLen Brown 		}
5133388e9c81SLen Brown 		break;
5134388e9c81SLen Brown 	}
5135388e9c81SLen Brown 
5136388e9c81SLen Brown 	return 0;
5137388e9c81SLen Brown }
5138388e9c81SLen Brown 
5139388e9c81SLen Brown void parse_add_command(char *add_command)
5140388e9c81SLen Brown {
5141388e9c81SLen Brown 	int msr_num = 0;
5142495c7654SLen Brown 	char *path = NULL;
51430f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
5144388e9c81SLen Brown 	int width = 64;
5145388e9c81SLen Brown 	int fail = 0;
5146388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
5147388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
5148388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
5149388e9c81SLen Brown 
5150388e9c81SLen Brown 	while (add_command) {
5151388e9c81SLen Brown 
5152388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5153388e9c81SLen Brown 			goto next;
5154388e9c81SLen Brown 
5155388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
5156388e9c81SLen Brown 			goto next;
5157388e9c81SLen Brown 
5158495c7654SLen Brown 		if (*add_command == '/') {
5159495c7654SLen Brown 			path = add_command;
5160495c7654SLen Brown 			goto next;
5161495c7654SLen Brown 		}
5162495c7654SLen Brown 
5163388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
5164388e9c81SLen Brown 			if ((width == 32) || (width == 64))
5165388e9c81SLen Brown 				goto next;
5166388e9c81SLen Brown 			width = 64;
5167388e9c81SLen Brown 		}
5168388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5169388e9c81SLen Brown 			scope = SCOPE_CPU;
5170388e9c81SLen Brown 			goto next;
5171388e9c81SLen Brown 		}
5172388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
5173388e9c81SLen Brown 			scope = SCOPE_CORE;
5174388e9c81SLen Brown 			goto next;
5175388e9c81SLen Brown 		}
5176388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
5177388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
5178388e9c81SLen Brown 			goto next;
5179388e9c81SLen Brown 		}
5180388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5181388e9c81SLen Brown 			type = COUNTER_CYCLES;
5182388e9c81SLen Brown 			goto next;
5183388e9c81SLen Brown 		}
5184388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5185388e9c81SLen Brown 			type = COUNTER_SECONDS;
5186388e9c81SLen Brown 			goto next;
5187388e9c81SLen Brown 		}
518841618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
518941618e63SLen Brown 			type = COUNTER_USEC;
519041618e63SLen Brown 			goto next;
519141618e63SLen Brown 		}
5192388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
5193388e9c81SLen Brown 			format = FORMAT_RAW;
5194388e9c81SLen Brown 			goto next;
5195388e9c81SLen Brown 		}
5196388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
5197388e9c81SLen Brown 			format = FORMAT_DELTA;
5198388e9c81SLen Brown 			goto next;
5199388e9c81SLen Brown 		}
5200388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
5201388e9c81SLen Brown 			format = FORMAT_PERCENT;
5202388e9c81SLen Brown 			goto next;
5203388e9c81SLen Brown 		}
5204388e9c81SLen Brown 
5205388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
5206388e9c81SLen Brown 			char *eos;
5207388e9c81SLen Brown 
5208388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
5209388e9c81SLen Brown 			if (eos)
5210388e9c81SLen Brown 				*eos = '\0';
5211388e9c81SLen Brown 			goto next;
5212388e9c81SLen Brown 		}
5213388e9c81SLen Brown 
5214388e9c81SLen Brown next:
5215388e9c81SLen Brown 		add_command = strchr(add_command, ',');
5216495c7654SLen Brown 		if (add_command) {
5217495c7654SLen Brown 			*add_command = '\0';
5218388e9c81SLen Brown 			add_command++;
5219495c7654SLen Brown 		}
5220388e9c81SLen Brown 
5221388e9c81SLen Brown 	}
5222495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
5223495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
5224388e9c81SLen Brown 		fail++;
5225388e9c81SLen Brown 	}
5226388e9c81SLen Brown 
5227388e9c81SLen Brown 	/* generate default column header */
5228388e9c81SLen Brown 	if (*name_buffer == '\0') {
5229388e9c81SLen Brown 		if (width == 32)
52305f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5231388e9c81SLen Brown 		else
52325f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5233388e9c81SLen Brown 	}
5234388e9c81SLen Brown 
523541618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
5236388e9c81SLen Brown 		fail++;
5237388e9c81SLen Brown 
5238388e9c81SLen Brown 	if (fail) {
5239388e9c81SLen Brown 		help();
5240388e9c81SLen Brown 		exit(1);
5241388e9c81SLen Brown 	}
5242388e9c81SLen Brown }
524341618e63SLen Brown 
5244dd778a5eSLen Brown int is_deferred_skip(char *name)
5245dd778a5eSLen Brown {
5246dd778a5eSLen Brown 	int i;
5247dd778a5eSLen Brown 
5248dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
5249dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
5250dd778a5eSLen Brown 			return 1;
5251dd778a5eSLen Brown 	return 0;
5252dd778a5eSLen Brown }
5253dd778a5eSLen Brown 
525441618e63SLen Brown void probe_sysfs(void)
525541618e63SLen Brown {
525641618e63SLen Brown 	char path[64];
525741618e63SLen Brown 	char name_buf[16];
525841618e63SLen Brown 	FILE *input;
525941618e63SLen Brown 	int state;
526041618e63SLen Brown 	char *sp;
526141618e63SLen Brown 
526241618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
526341618e63SLen Brown 		return;
526441618e63SLen Brown 
52650748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
526641618e63SLen Brown 
526741618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
526841618e63SLen Brown 			base_cpu, state);
526941618e63SLen Brown 		input = fopen(path, "r");
527041618e63SLen Brown 		if (input == NULL)
527141618e63SLen Brown 			continue;
527241618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
527341618e63SLen Brown 
527441618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
527541618e63SLen Brown 		sp = strchr(name_buf, '-');
527641618e63SLen Brown 		if (!sp)
527741618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
527841618e63SLen Brown 		*sp = '%';
527941618e63SLen Brown 		*(sp + 1) = '\0';
528041618e63SLen Brown 
528141618e63SLen Brown 		fclose(input);
528241618e63SLen Brown 
528341618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
528441618e63SLen Brown 
5285dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5286dd778a5eSLen Brown 			continue;
5287dd778a5eSLen Brown 
528841618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
528941618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
529041618e63SLen Brown 	}
529141618e63SLen Brown 
52920748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
529341618e63SLen Brown 
529441618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
529541618e63SLen Brown 			base_cpu, state);
529641618e63SLen Brown 		input = fopen(path, "r");
529741618e63SLen Brown 		if (input == NULL)
529841618e63SLen Brown 			continue;
529941618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
530041618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
530141618e63SLen Brown 		sp = strchr(name_buf, '-');
530241618e63SLen Brown 		if (!sp)
530341618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
530441618e63SLen Brown 		*sp = '\0';
530541618e63SLen Brown 		fclose(input);
530641618e63SLen Brown 
530741618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
530841618e63SLen Brown 
5309dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5310dd778a5eSLen Brown 			continue;
5311dd778a5eSLen Brown 
531241618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
531341618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
531441618e63SLen Brown 	}
531541618e63SLen Brown 
531641618e63SLen Brown }
531741618e63SLen Brown 
53181ef7d21aSLen Brown 
53191ef7d21aSLen Brown /*
53201ef7d21aSLen Brown  * parse cpuset with following syntax
53211ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
53221ef7d21aSLen Brown  */
53231ef7d21aSLen Brown void parse_cpu_command(char *optarg)
53241ef7d21aSLen Brown {
53251ef7d21aSLen Brown 	unsigned int start, end;
53261ef7d21aSLen Brown 	char *next;
53271ef7d21aSLen Brown 
53284e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
53294e4e1e7cSLen Brown 		if (cpu_subset)
53304e4e1e7cSLen Brown 			goto error;
53314e4e1e7cSLen Brown 		show_core_only++;
53324e4e1e7cSLen Brown 		return;
53334e4e1e7cSLen Brown 	}
53344e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
53354e4e1e7cSLen Brown 		if (cpu_subset)
53364e4e1e7cSLen Brown 			goto error;
53374e4e1e7cSLen Brown 		show_pkg_only++;
53384e4e1e7cSLen Brown 		return;
53394e4e1e7cSLen Brown 	}
53404e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
53414e4e1e7cSLen Brown 		goto error;
53424e4e1e7cSLen Brown 
53431ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
53441ef7d21aSLen Brown 	if (cpu_subset == NULL)
53451ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
53461ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
53471ef7d21aSLen Brown 
53481ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
53491ef7d21aSLen Brown 
53501ef7d21aSLen Brown 	next = optarg;
53511ef7d21aSLen Brown 
53521ef7d21aSLen Brown 	while (next && *next) {
53531ef7d21aSLen Brown 
53541ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
53551ef7d21aSLen Brown 			goto error;
53561ef7d21aSLen Brown 
53571ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
53581ef7d21aSLen Brown 
53591ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
53601ef7d21aSLen Brown 			goto error;
53611ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
53621ef7d21aSLen Brown 
53631ef7d21aSLen Brown 		if (*next == '\0')
53641ef7d21aSLen Brown 			break;
53651ef7d21aSLen Brown 
53661ef7d21aSLen Brown 		if (*next == ',') {
53671ef7d21aSLen Brown 			next += 1;
53681ef7d21aSLen Brown 			continue;
53691ef7d21aSLen Brown 		}
53701ef7d21aSLen Brown 
53711ef7d21aSLen Brown 		if (*next == '-') {
53721ef7d21aSLen Brown 			next += 1;	/* start range */
53731ef7d21aSLen Brown 		} else if (*next == '.') {
53741ef7d21aSLen Brown 			next += 1;
53751ef7d21aSLen Brown 			if (*next == '.')
53761ef7d21aSLen Brown 				next += 1;	/* start range */
53771ef7d21aSLen Brown 			else
53781ef7d21aSLen Brown 				goto error;
53791ef7d21aSLen Brown 		}
53801ef7d21aSLen Brown 
53811ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
53821ef7d21aSLen Brown 		if (end <= start)
53831ef7d21aSLen Brown 			goto error;
53841ef7d21aSLen Brown 
53851ef7d21aSLen Brown 		while (++start <= end) {
53861ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
53871ef7d21aSLen Brown 				goto error;
53881ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
53891ef7d21aSLen Brown 		}
53901ef7d21aSLen Brown 
53911ef7d21aSLen Brown 		if (*next == ',')
53921ef7d21aSLen Brown 			next += 1;
53931ef7d21aSLen Brown 		else if (*next != '\0')
53941ef7d21aSLen Brown 			goto error;
53951ef7d21aSLen Brown 	}
53961ef7d21aSLen Brown 
53971ef7d21aSLen Brown 	return;
53981ef7d21aSLen Brown 
53991ef7d21aSLen Brown error:
54004e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
54014e4e1e7cSLen Brown 	help();
54021ef7d21aSLen Brown 	exit(-1);
54031ef7d21aSLen Brown }
54041ef7d21aSLen Brown 
5405812db3f7SLen Brown 
5406103a8feaSLen Brown void cmdline(int argc, char **argv)
5407103a8feaSLen Brown {
5408103a8feaSLen Brown 	int opt;
5409d8af6f5fSLen Brown 	int option_index = 0;
5410d8af6f5fSLen Brown 	static struct option long_options[] = {
5411388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
54121ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5413d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
541496e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
54153f44a5c6SLen Brown 		{"enable",	required_argument,	0, 'e'},
5416d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5417023fe0acSChen Yu 		{"num_iterations",	required_argument,	0, 'n'},
5418d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5419812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5420d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5421c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5422b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
542396e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5424812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5425d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5426d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5427d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5428d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5429d8af6f5fSLen Brown 	};
5430103a8feaSLen Brown 
5431103a8feaSLen Brown 	progname = argv[0];
5432103a8feaSLen Brown 
5433023fe0acSChen Yu 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
5434d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5435103a8feaSLen Brown 		switch (opt) {
5436388e9c81SLen Brown 		case 'a':
5437388e9c81SLen Brown 			parse_add_command(optarg);
5438388e9c81SLen Brown 			break;
54391ef7d21aSLen Brown 		case 'c':
54401ef7d21aSLen Brown 			parse_cpu_command(optarg);
54411ef7d21aSLen Brown 			break;
5442d8af6f5fSLen Brown 		case 'D':
5443d8af6f5fSLen Brown 			dump_only++;
54448e180f3cSLen Brown 			break;
54453f44a5c6SLen Brown 		case 'e':
54463f44a5c6SLen Brown 			/* --enable specified counter */
5447*4c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
54483f44a5c6SLen Brown 			break;
5449d8af6f5fSLen Brown 		case 'd':
5450d8af6f5fSLen Brown 			debug++;
54513f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
54522f32edf1SLen Brown 			break;
5453812db3f7SLen Brown 		case 'H':
54543f44a5c6SLen Brown 			/*
54553f44a5c6SLen Brown 			 * --hide: do not show those specified
54563f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
54573f44a5c6SLen Brown 			 */
54583f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5459812db3f7SLen Brown 			break;
5460d8af6f5fSLen Brown 		case 'h':
5461d8af6f5fSLen Brown 		default:
5462d8af6f5fSLen Brown 			help();
5463d8af6f5fSLen Brown 			exit(1);
5464d8af6f5fSLen Brown 		case 'i':
54652a0609c0SLen Brown 			{
54662a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
54672a0609c0SLen Brown 
54682a0609c0SLen Brown 				if (interval < 0.001) {
5469b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
54702a0609c0SLen Brown 						interval);
54712a0609c0SLen Brown 					exit(2);
54722a0609c0SLen Brown 				}
54732a0609c0SLen Brown 
547447936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
5475b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
547647936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
54772a0609c0SLen Brown 			}
5478889facbeSLen Brown 			break;
54795c56be9aSDirk Brandewie 		case 'J':
54805c56be9aSDirk Brandewie 			rapl_joules++;
54815c56be9aSDirk Brandewie 			break;
5482c8ade361SLen Brown 		case 'l':
54833f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5484c8ade361SLen Brown 			list_header_only++;
5485c8ade361SLen Brown 			quiet++;
5486c8ade361SLen Brown 			break;
5487b7d8c148SLen Brown 		case 'o':
5488b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5489b7d8c148SLen Brown 			break;
549096e47158SLen Brown 		case 'q':
549196e47158SLen Brown 			quiet = 1;
549296e47158SLen Brown 			break;
5493023fe0acSChen Yu 		case 'n':
5494023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
5495023fe0acSChen Yu 
5496023fe0acSChen Yu 			if (num_iterations <= 0) {
5497023fe0acSChen Yu 				fprintf(outf, "iterations %d should be positive number\n",
5498023fe0acSChen Yu 					num_iterations);
5499023fe0acSChen Yu 				exit(2);
5500023fe0acSChen Yu 			}
5501023fe0acSChen Yu 			break;
5502812db3f7SLen Brown 		case 's':
55033f44a5c6SLen Brown 			/*
55043f44a5c6SLen Brown 			 * --show: show only those specified
55053f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
55063f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
55073f44a5c6SLen Brown 			 */
55083f44a5c6SLen Brown 			if (shown == 0)
55093f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
55103f44a5c6SLen Brown 			else
55113f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
55123f44a5c6SLen Brown 			shown = 1;
5513812db3f7SLen Brown 			break;
5514d8af6f5fSLen Brown 		case 'S':
5515d8af6f5fSLen Brown 			summary_only++;
5516d8af6f5fSLen Brown 			break;
5517d8af6f5fSLen Brown 		case 'T':
5518d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5519d8af6f5fSLen Brown 			break;
5520d8af6f5fSLen Brown 		case 'v':
5521d8af6f5fSLen Brown 			print_version();
5522d8af6f5fSLen Brown 			exit(0);
5523d8af6f5fSLen Brown 			break;
5524103a8feaSLen Brown 		}
5525103a8feaSLen Brown 	}
5526103a8feaSLen Brown }
5527103a8feaSLen Brown 
5528103a8feaSLen Brown int main(int argc, char **argv)
5529103a8feaSLen Brown {
5530b7d8c148SLen Brown 	outf = stderr;
5531103a8feaSLen Brown 	cmdline(argc, argv);
5532103a8feaSLen Brown 
553396e47158SLen Brown 	if (!quiet)
5534d8af6f5fSLen Brown 		print_version();
5535103a8feaSLen Brown 
553641618e63SLen Brown 	probe_sysfs();
553741618e63SLen Brown 
5538103a8feaSLen Brown 	turbostat_init();
5539103a8feaSLen Brown 
55403b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
55413b4d5c7fSAndy Shevchenko 	if (dump_only)
55423b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
55433b4d5c7fSAndy Shevchenko 
5544c8ade361SLen Brown 	/* list header and exit */
5545c8ade361SLen Brown 	if (list_header_only) {
5546c8ade361SLen Brown 		print_header(",");
5547c8ade361SLen Brown 		flush_output_stdout();
5548c8ade361SLen Brown 		return 0;
5549c8ade361SLen Brown 	}
5550c8ade361SLen Brown 
5551103a8feaSLen Brown 	/*
5552103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5553103a8feaSLen Brown 	 */
5554103a8feaSLen Brown 	if (argc - optind)
5555103a8feaSLen Brown 		return fork_it(argv + optind);
5556103a8feaSLen Brown 	else
5557103a8feaSLen Brown 		turbostat_loop();
5558103a8feaSLen Brown 
5559103a8feaSLen Brown 	return 0;
5560103a8feaSLen Brown }
5561