xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 3316f99a9f1b68c578c57e76792bd19da1c7d423)
1103a8feaSLen Brown /*
2103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
334041551SLen Brown  * on modern Intel and AMD 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>
479392bd98SCalvin Walton #include <math.h>
48103a8feaSLen Brown 
49103a8feaSLen Brown char *proc_stat = "/proc/stat";
50b7d8c148SLen Brown FILE *outf;
5136229897SLen Brown int *fd_percpu;
52b9ad8ee0SLen Brown struct timeval interval_tv = {5, 0};
5347936f94SArtem Bityutskiy struct timespec interval_ts = {5, 0};
54b9ad8ee0SLen Brown struct timespec one_msec = {0, 1000000};
55023fe0acSChen Yu unsigned int num_iterations;
56d8af6f5fSLen Brown unsigned int debug;
5796e47158SLen Brown unsigned int quiet;
583f44a5c6SLen Brown unsigned int shown;
590de6c0dfSLen Brown unsigned int sums_need_wide_columns;
60d8af6f5fSLen Brown unsigned int rapl_joules;
61d8af6f5fSLen Brown unsigned int summary_only;
62c8ade361SLen Brown unsigned int list_header_only;
63d8af6f5fSLen Brown unsigned int dump_only;
64103a8feaSLen Brown unsigned int do_snb_cstates;
65fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
66144b44b1SLen Brown unsigned int do_slm_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;
7434041551SLen Brown unsigned int authentic_amd;
7534041551SLen Brown unsigned int max_level, max_extended_level;
76103a8feaSLen Brown unsigned int has_invariant_tsc;
77d7899447SLen Brown unsigned int do_nhm_platform_info;
78cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
79b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
80103a8feaSLen Brown double bclk;
81a2b7b749SLen Brown double base_hz;
8221ed5574SLen Brown unsigned int has_base_hz;
83a2b7b749SLen Brown double tsc_tweak = 1.0;
84c98d5d94SLen Brown unsigned int show_pkg_only;
85c98d5d94SLen Brown unsigned int show_core_only;
86c98d5d94SLen Brown char *output_buffer, *outp;
87889facbeSLen Brown unsigned int do_rapl;
88889facbeSLen Brown unsigned int do_dts;
89889facbeSLen Brown unsigned int do_ptm;
90fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
91be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us;
92be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us;
9327d47356SLen Brown unsigned int gfx_cur_mhz;
94889facbeSLen Brown unsigned int tcc_activation_temp;
95889facbeSLen Brown unsigned int tcc_activation_temp_override;
9640ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
9740ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
98889facbeSLen Brown double rapl_joule_counter_range;
993a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
100ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
1013a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
1023a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
1038a5bdf41SLen Brown unsigned int crystal_hz;
1048a5bdf41SLen Brown unsigned long long tsc_hz;
1057ce7d5deSPrarit Bhargava int base_cpu;
10621ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
1077f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
1087f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
1097f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
1107f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1117f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1127f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
11333148d67SLen Brown unsigned int has_misc_feature_control;
1144c2122d4SLen Brown unsigned int first_counter_read = 1;
115889facbeSLen Brown 
116889facbeSLen Brown #define RAPL_PKG		(1 << 0)
117e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
118e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
119e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
120e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
121e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
122e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
123e6f9bb3cSLen Brown 
124889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
125e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
126e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
127e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
128e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1290b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1300b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
131e6f9bb3cSLen Brown 
1329148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
133e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1340b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
135e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
136e6f9bb3cSLen Brown 
1370b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
138e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
139e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
140e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1419148494cSJacob Pan 
1429148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1439148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1449392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY	(1 << 10)
1459392bd98SCalvin Walton 					/* Indicates cores energy collection is per-core,
1469392bd98SCalvin Walton 					 * not per-package. */
1479392bd98SCalvin Walton #define RAPL_AMD_F17H		(1 << 11)
1489392bd98SCalvin Walton 					/* 0xc0010299 MSR_RAPL_PWR_UNIT */
1499392bd98SCalvin Walton 					/* 0xc001029a MSR_CORE_ENERGY_STAT */
1509392bd98SCalvin Walton 					/* 0xc001029b MSR_PKG_ENERGY_STAT */
1519148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
152889facbeSLen Brown #define	TJMAX_DEFAULT	100
153889facbeSLen Brown 
1549392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */
1559392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT	0xc0010299
1569392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT	0xc001029a
1579392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT	0xc001029b
1589392bd98SCalvin Walton 
159889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
160103a8feaSLen Brown 
161388e9c81SLen Brown /*
162388e9c81SLen Brown  * buffer size used by sscanf() for added column names
163388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
164388e9c81SLen Brown  */
165388e9c81SLen Brown #define	NAME_BYTES 20
166495c7654SLen Brown #define PATH_BYTES 128
167388e9c81SLen Brown 
168103a8feaSLen Brown int backwards_count;
169103a8feaSLen Brown char *progname;
170103a8feaSLen Brown 
1711ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
1721ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
1731ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
1740748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8
1750748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24
1760e2d8f05SLen Brown #define BITMASK_SIZE 32
177c98d5d94SLen Brown 
178c98d5d94SLen Brown struct thread_data {
179f4fdf2b4SLen Brown 	struct timeval tv_begin;
180f4fdf2b4SLen Brown 	struct timeval tv_end;
181c98d5d94SLen Brown 	unsigned long long tsc;
182c98d5d94SLen Brown 	unsigned long long aperf;
183c98d5d94SLen Brown 	unsigned long long mperf;
184144b44b1SLen Brown 	unsigned long long c1;
1850de6c0dfSLen Brown 	unsigned long long  irq_count;
1861ed51011SLen Brown 	unsigned int smi_count;
187c98d5d94SLen Brown 	unsigned int cpu_id;
1884c2122d4SLen Brown 	unsigned int apic_id;
1894c2122d4SLen Brown 	unsigned int x2apic_id;
190c98d5d94SLen Brown 	unsigned int flags;
191c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
192c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
1930748eaf0SLen Brown 	unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
194c98d5d94SLen Brown } *thread_even, *thread_odd;
195c98d5d94SLen Brown 
196c98d5d94SLen Brown struct core_data {
197c98d5d94SLen Brown 	unsigned long long c3;
198c98d5d94SLen Brown 	unsigned long long c6;
199c98d5d94SLen Brown 	unsigned long long c7;
2000539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
201889facbeSLen Brown 	unsigned int core_temp_c;
2029392bd98SCalvin Walton 	unsigned int core_energy;	/* MSR_CORE_ENERGY_STAT */
203c98d5d94SLen Brown 	unsigned int core_id;
204678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
205c98d5d94SLen Brown } *core_even, *core_odd;
206c98d5d94SLen Brown 
207c98d5d94SLen Brown struct pkg_data {
208c98d5d94SLen Brown 	unsigned long long pc2;
209c98d5d94SLen Brown 	unsigned long long pc3;
210c98d5d94SLen Brown 	unsigned long long pc6;
211c98d5d94SLen Brown 	unsigned long long pc7;
212ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
213ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
214ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
215be0e54c4SLen Brown 	unsigned long long cpu_lpi;
216be0e54c4SLen Brown 	unsigned long long sys_lpi;
2170b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
2180b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
2190b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
2200b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
2219185e988SLen Brown 	long long gfx_rc6_ms;
22227d47356SLen Brown 	unsigned int gfx_mhz;
223c98d5d94SLen Brown 	unsigned int package_id;
224889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
225889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
226889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
227889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
228889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
229889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
230889facbeSLen Brown 	unsigned int pkg_temp_c;
231678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
232c98d5d94SLen Brown } *package_even, *package_odd;
233c98d5d94SLen Brown 
234c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
235c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
236c98d5d94SLen Brown 
23740f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no)	      \
23840f5cfe7SPrarit Bhargava 	((thread_base) +						      \
23940f5cfe7SPrarit Bhargava 	 ((pkg_no) *							      \
24040f5cfe7SPrarit Bhargava 	  topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
24140f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node * topo.threads_per_core) +	      \
24240f5cfe7SPrarit Bhargava 	 ((core_no) * topo.threads_per_core) +				      \
24340f5cfe7SPrarit Bhargava 	 (thread_no))
24440f5cfe7SPrarit Bhargava 
24540f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no)			\
24640f5cfe7SPrarit Bhargava 	((core_base) +							\
24740f5cfe7SPrarit Bhargava 	 ((pkg_no) *  topo.nodes_per_pkg * topo.cores_per_node) +	\
24840f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node) +				\
24940f5cfe7SPrarit Bhargava 	 (core_no))
25040f5cfe7SPrarit Bhargava 
25140f5cfe7SPrarit Bhargava 
252c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
253c98d5d94SLen Brown 
254388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
25541618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
256388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
257388e9c81SLen Brown 
258388e9c81SLen Brown struct msr_counter {
259388e9c81SLen Brown 	unsigned int msr_num;
260388e9c81SLen Brown 	char name[NAME_BYTES];
261495c7654SLen Brown 	char path[PATH_BYTES];
262388e9c81SLen Brown 	unsigned int width;
263388e9c81SLen Brown 	enum counter_type type;
264388e9c81SLen Brown 	enum counter_format format;
265388e9c81SLen Brown 	struct msr_counter *next;
266812db3f7SLen Brown 	unsigned int flags;
267812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
268812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
26941618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
270388e9c81SLen Brown };
271388e9c81SLen Brown 
272388e9c81SLen Brown struct sys_counters {
273678a3bd1SLen Brown 	unsigned int added_thread_counters;
274678a3bd1SLen Brown 	unsigned int added_core_counters;
275678a3bd1SLen Brown 	unsigned int added_package_counters;
276388e9c81SLen Brown 	struct msr_counter *tp;
277388e9c81SLen Brown 	struct msr_counter *cp;
278388e9c81SLen Brown 	struct msr_counter *pp;
279388e9c81SLen Brown } sys;
280388e9c81SLen Brown 
281c98d5d94SLen Brown struct system_summary {
282c98d5d94SLen Brown 	struct thread_data threads;
283c98d5d94SLen Brown 	struct core_data cores;
284c98d5d94SLen Brown 	struct pkg_data packages;
285388e9c81SLen Brown } average;
286c98d5d94SLen Brown 
2870e2d8f05SLen Brown struct cpu_topology {
2880e2d8f05SLen Brown 	int physical_package_id;
2896de68fe1SLen Brown 	int die_id;
2900e2d8f05SLen Brown 	int logical_cpu_id;
291ef605741SPrarit Bhargava 	int physical_node_id;
292ef605741SPrarit Bhargava 	int logical_node_id;	/* 0-based count within the package */
2930e2d8f05SLen Brown 	int physical_core_id;
2948cb48b32SPrarit Bhargava 	int thread_id;
2950e2d8f05SLen Brown 	cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
2960e2d8f05SLen Brown } *cpus;
297c98d5d94SLen Brown 
298c98d5d94SLen Brown struct topo_params {
299c98d5d94SLen Brown 	int num_packages;
3006de68fe1SLen Brown 	int num_die;
301103a8feaSLen Brown 	int num_cpus;
302c98d5d94SLen Brown 	int num_cores;
303c98d5d94SLen Brown 	int max_cpu_num;
304ef605741SPrarit Bhargava 	int max_node_num;
30570a9c6e8SPrarit Bhargava 	int nodes_per_pkg;
30670a9c6e8SPrarit Bhargava 	int cores_per_node;
30770a9c6e8SPrarit Bhargava 	int threads_per_core;
308c98d5d94SLen Brown } topo;
309103a8feaSLen Brown 
310c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
311103a8feaSLen Brown 
312562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
313562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
314562a2d37SLen Brown 
315c98d5d94SLen Brown void setup_all_buffers(void);
316103a8feaSLen Brown 
317c98d5d94SLen Brown int cpu_is_not_present(int cpu)
318d15cf7c1SLen Brown {
319c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
320c98d5d94SLen Brown }
321c98d5d94SLen Brown /*
322c98d5d94SLen Brown  * run func(thread, core, package) in topology order
323c98d5d94SLen Brown  * skip non-present cpus
324c98d5d94SLen Brown  */
325c98d5d94SLen Brown 
326c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
327c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
328c98d5d94SLen Brown {
32940f5cfe7SPrarit Bhargava 	int retval, pkg_no, core_no, thread_no, node_no;
330c98d5d94SLen Brown 
331c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
332df2f677dSLen Brown 		for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
33370a9c6e8SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
334c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
33570a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
336c98d5d94SLen Brown 					struct thread_data *t;
337c98d5d94SLen Brown 					struct core_data *c;
338c98d5d94SLen Brown 					struct pkg_data *p;
339c98d5d94SLen Brown 
34040f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
34140f5cfe7SPrarit Bhargava 						       core_no, node_no,
34240f5cfe7SPrarit Bhargava 						       pkg_no);
343c98d5d94SLen Brown 
344c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
345c98d5d94SLen Brown 						continue;
346c98d5d94SLen Brown 
34740f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
34840f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
349c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
350c98d5d94SLen Brown 
351c98d5d94SLen Brown 					retval = func(t, c, p);
352c98d5d94SLen Brown 					if (retval)
353c98d5d94SLen Brown 						return retval;
354c98d5d94SLen Brown 				}
355c98d5d94SLen Brown 			}
356c98d5d94SLen Brown 		}
35740f5cfe7SPrarit Bhargava 	}
358d15cf7c1SLen Brown 	return 0;
359d15cf7c1SLen Brown }
360d15cf7c1SLen Brown 
36188c3281fSLen Brown int cpu_migrate(int cpu)
36288c3281fSLen Brown {
363c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
364c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
365c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
36688c3281fSLen Brown 		return -1;
36788c3281fSLen Brown 	else
36888c3281fSLen Brown 		return 0;
36988c3281fSLen Brown }
37036229897SLen Brown int get_msr_fd(int cpu)
371103a8feaSLen Brown {
372103a8feaSLen Brown 	char pathname[32];
373103a8feaSLen Brown 	int fd;
374103a8feaSLen Brown 
37536229897SLen Brown 	fd = fd_percpu[cpu];
37636229897SLen Brown 
37736229897SLen Brown 	if (fd)
37836229897SLen Brown 		return fd;
37936229897SLen Brown 
380103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
381103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
38215aaa346SLen Brown 	if (fd < 0)
38398481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
384103a8feaSLen Brown 
38536229897SLen Brown 	fd_percpu[cpu] = fd;
38636229897SLen Brown 
38736229897SLen Brown 	return fd;
38836229897SLen Brown }
38936229897SLen Brown 
39036229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
39136229897SLen Brown {
39236229897SLen Brown 	ssize_t retval;
39336229897SLen Brown 
39436229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
39515aaa346SLen Brown 
39698481e79SLen Brown 	if (retval != sizeof *msr)
397cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
39815aaa346SLen Brown 
39915aaa346SLen Brown 	return 0;
400103a8feaSLen Brown }
401103a8feaSLen Brown 
402fc04cc67SLen Brown /*
403bdd5ae3aSLen Brown  * This list matches the column headers, except
404bdd5ae3aSLen Brown  * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
405bdd5ae3aSLen Brown  * 2. Core and CPU are moved to the end, we can't have strings that contain them
406bdd5ae3aSLen Brown  *    matching on them for --show and --hide.
407fc04cc67SLen Brown  */
408812db3f7SLen Brown struct msr_counter bic[] = {
4093f44a5c6SLen Brown 	{ 0x0, "usec" },
4103f44a5c6SLen Brown 	{ 0x0, "Time_Of_Day_Seconds" },
411812db3f7SLen Brown 	{ 0x0, "Package" },
412bdd5ae3aSLen Brown 	{ 0x0, "Node" },
413812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
414bdd5ae3aSLen Brown 	{ 0x0, "Busy%" },
415812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
416812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
417812db3f7SLen Brown 	{ 0x0, "IRQ" },
418495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
419bdd5ae3aSLen Brown 	{ 0x0, "sysfs" },
420812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
421812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
422812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
423812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
424812db3f7SLen Brown 	{ 0x0, "ThreadC" },
425812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
426812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
427812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
428812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
429812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
430812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
431812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
432812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
433812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
4340f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
4350f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
4364bd1f8f2SLen Brown 	{ 0x0, "Pk%pc10" },
437be0e54c4SLen Brown 	{ 0x0, "CPU%LPI" },
438be0e54c4SLen Brown 	{ 0x0, "SYS%LPI" },
439812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
440812db3f7SLen Brown 	{ 0x0, "CorWatt" },
441812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
442812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
443812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
444812db3f7SLen Brown 	{ 0x0, "PKG_%" },
445812db3f7SLen Brown 	{ 0x0, "RAM_%" },
446812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
447812db3f7SLen Brown 	{ 0x0, "Cor_J" },
448812db3f7SLen Brown 	{ 0x0, "GFX_J" },
449812db3f7SLen Brown 	{ 0x0, "RAM_J" },
4500539ba11SLen Brown 	{ 0x0, "Mod%c6" },
451a99d8730SLen Brown 	{ 0x0, "Totl%C0" },
452a99d8730SLen Brown 	{ 0x0, "Any%C0" },
453a99d8730SLen Brown 	{ 0x0, "GFX%C0" },
454a99d8730SLen Brown 	{ 0x0, "CPUGFX%" },
455bdd5ae3aSLen Brown 	{ 0x0, "Core" },
456bdd5ae3aSLen Brown 	{ 0x0, "CPU" },
4574c2122d4SLen Brown 	{ 0x0, "APIC" },
4584c2122d4SLen Brown 	{ 0x0, "X2APIC" },
4596de68fe1SLen Brown 	{ 0x0, "Die" },
460812db3f7SLen Brown };
461812db3f7SLen Brown 
462812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
4633f44a5c6SLen Brown #define	BIC_USEC	(1ULL << 0)
4643f44a5c6SLen Brown #define	BIC_TOD		(1ULL << 1)
4653f44a5c6SLen Brown #define	BIC_Package	(1ULL << 2)
466bdd5ae3aSLen Brown #define	BIC_Node	(1ULL << 3)
467bdd5ae3aSLen Brown #define	BIC_Avg_MHz	(1ULL << 4)
468bdd5ae3aSLen Brown #define	BIC_Busy	(1ULL << 5)
469bdd5ae3aSLen Brown #define	BIC_Bzy_MHz	(1ULL << 6)
470bdd5ae3aSLen Brown #define	BIC_TSC_MHz	(1ULL << 7)
471bdd5ae3aSLen Brown #define	BIC_IRQ		(1ULL << 8)
472bdd5ae3aSLen Brown #define	BIC_SMI		(1ULL << 9)
473bdd5ae3aSLen Brown #define	BIC_sysfs	(1ULL << 10)
474bdd5ae3aSLen Brown #define	BIC_CPU_c1	(1ULL << 11)
475bdd5ae3aSLen Brown #define	BIC_CPU_c3	(1ULL << 12)
476bdd5ae3aSLen Brown #define	BIC_CPU_c6	(1ULL << 13)
477bdd5ae3aSLen Brown #define	BIC_CPU_c7	(1ULL << 14)
478bdd5ae3aSLen Brown #define	BIC_ThreadC	(1ULL << 15)
479bdd5ae3aSLen Brown #define	BIC_CoreTmp	(1ULL << 16)
480bdd5ae3aSLen Brown #define	BIC_CoreCnt	(1ULL << 17)
481bdd5ae3aSLen Brown #define	BIC_PkgTmp	(1ULL << 18)
482bdd5ae3aSLen Brown #define	BIC_GFX_rc6	(1ULL << 19)
483bdd5ae3aSLen Brown #define	BIC_GFXMHz	(1ULL << 20)
484bdd5ae3aSLen Brown #define	BIC_Pkgpc2	(1ULL << 21)
485bdd5ae3aSLen Brown #define	BIC_Pkgpc3	(1ULL << 22)
486bdd5ae3aSLen Brown #define	BIC_Pkgpc6	(1ULL << 23)
487bdd5ae3aSLen Brown #define	BIC_Pkgpc7	(1ULL << 24)
488bdd5ae3aSLen Brown #define	BIC_Pkgpc8	(1ULL << 25)
489bdd5ae3aSLen Brown #define	BIC_Pkgpc9	(1ULL << 26)
490bdd5ae3aSLen Brown #define	BIC_Pkgpc10	(1ULL << 27)
491bdd5ae3aSLen Brown #define BIC_CPU_LPI	(1ULL << 28)
492bdd5ae3aSLen Brown #define BIC_SYS_LPI	(1ULL << 29)
493bdd5ae3aSLen Brown #define	BIC_PkgWatt	(1ULL << 30)
494bdd5ae3aSLen Brown #define	BIC_CorWatt	(1ULL << 31)
495bdd5ae3aSLen Brown #define	BIC_GFXWatt	(1ULL << 32)
496bdd5ae3aSLen Brown #define	BIC_PkgCnt	(1ULL << 33)
497bdd5ae3aSLen Brown #define	BIC_RAMWatt	(1ULL << 34)
498bdd5ae3aSLen Brown #define	BIC_PKG__	(1ULL << 35)
499bdd5ae3aSLen Brown #define	BIC_RAM__	(1ULL << 36)
500bdd5ae3aSLen Brown #define	BIC_Pkg_J	(1ULL << 37)
501bdd5ae3aSLen Brown #define	BIC_Cor_J	(1ULL << 38)
502bdd5ae3aSLen Brown #define	BIC_GFX_J	(1ULL << 39)
503bdd5ae3aSLen Brown #define	BIC_RAM_J	(1ULL << 40)
504bdd5ae3aSLen Brown #define	BIC_Mod_c6	(1ULL << 41)
505bdd5ae3aSLen Brown #define	BIC_Totl_c0	(1ULL << 42)
506bdd5ae3aSLen Brown #define	BIC_Any_c0	(1ULL << 43)
507bdd5ae3aSLen Brown #define	BIC_GFX_c0	(1ULL << 44)
508bdd5ae3aSLen Brown #define	BIC_CPUGFX	(1ULL << 45)
509bdd5ae3aSLen Brown #define	BIC_Core	(1ULL << 46)
510bdd5ae3aSLen Brown #define	BIC_CPU		(1ULL << 47)
5114c2122d4SLen Brown #define	BIC_APIC	(1ULL << 48)
5124c2122d4SLen Brown #define	BIC_X2APIC	(1ULL << 49)
5136de68fe1SLen Brown #define	BIC_Die		(1ULL << 50)
514812db3f7SLen Brown 
5154c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT	(BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
5163f44a5c6SLen Brown 
5173f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
5184c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
519812db3f7SLen Brown 
520812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
5213f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
522812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
5230f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
524812db3f7SLen Brown 
5253f44a5c6SLen Brown 
526dd778a5eSLen Brown #define MAX_DEFERRED 16
527dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
528dd778a5eSLen Brown int deferred_skip_index;
529dd778a5eSLen Brown 
530dd778a5eSLen Brown /*
531dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
532dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
533dd778a5eSLen Brown  */
534dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
535dd778a5eSLen Brown 
536dd778a5eSLen Brown void help(void)
537dd778a5eSLen Brown {
538dd778a5eSLen Brown 	fprintf(outf,
539dd778a5eSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
540dd778a5eSLen Brown 	"\n"
541dd778a5eSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
542dd778a5eSLen Brown 	"when COMMAND completes.\n"
543dd778a5eSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
544dd778a5eSLen Brown 	"to print statistics, until interrupted.\n"
545cc481650SNathan Ciobanu 	"  -a, --add	add a counter\n"
546dd778a5eSLen Brown 	"		  eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
547cc481650SNathan Ciobanu 	"  -c, --cpu	cpu-set	limit output to summary plus cpu-set:\n"
548dd778a5eSLen Brown 	"		  {core | package | j,k,l..m,n-p }\n"
5499ce80578SNathan Ciobanu 	"  -d, --debug	displays usec, Time_Of_Day_Seconds and more debugging\n"
5509ce80578SNathan Ciobanu 	"  -D, --Dump	displays the raw counter values\n"
5519ce80578SNathan Ciobanu 	"  -e, --enable	[all | column]\n"
5529ce80578SNathan Ciobanu 	"		shows all or the specified disabled column\n"
5539ce80578SNathan Ciobanu 	"  -H, --hide [column|column,column,...]\n"
5549ce80578SNathan Ciobanu 	"		hide the specified column(s)\n"
555cc481650SNathan Ciobanu 	"  -i, --interval sec.subsec\n"
556cc481650SNathan Ciobanu 	"		Override default 5-second measurement interval\n"
5579ce80578SNathan Ciobanu 	"  -J, --Joules	displays energy in Joules instead of Watts\n"
558cc481650SNathan Ciobanu 	"  -l, --list	list column headers only\n"
559cc481650SNathan Ciobanu 	"  -n, --num_iterations num\n"
560cc481650SNathan Ciobanu 	"		number of the measurement iterations\n"
561cc481650SNathan Ciobanu 	"  -o, --out file\n"
562cc481650SNathan Ciobanu 	"		create or truncate \"file\" for all output\n"
563cc481650SNathan Ciobanu 	"  -q, --quiet	skip decoding system configuration header\n"
5649ce80578SNathan Ciobanu 	"  -s, --show [column|column,column,...]\n"
5659ce80578SNathan Ciobanu 	"		show only the specified column(s)\n"
5669ce80578SNathan Ciobanu 	"  -S, --Summary\n"
5679ce80578SNathan Ciobanu 	"		limits output to 1-line system summary per interval\n"
5689ce80578SNathan Ciobanu 	"  -T, --TCC temperature\n"
5699ce80578SNathan Ciobanu 	"		sets the Thermal Control Circuit temperature in\n"
5709ce80578SNathan Ciobanu 	"		  degrees Celsius\n"
571cc481650SNathan Ciobanu 	"  -h, --help	print this help message\n"
572cc481650SNathan Ciobanu 	"  -v, --version	print version information\n"
573dd778a5eSLen Brown 	"\n"
574dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
575dd778a5eSLen Brown }
576dd778a5eSLen Brown 
577812db3f7SLen Brown /*
578812db3f7SLen Brown  * bic_lookup
579812db3f7SLen Brown  * for all the strings in comma separate name_list,
580812db3f7SLen Brown  * set the approprate bit in return value.
581812db3f7SLen Brown  */
582dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
583812db3f7SLen Brown {
584812db3f7SLen Brown 	int i;
585812db3f7SLen Brown 	unsigned long long retval = 0;
586812db3f7SLen Brown 
587812db3f7SLen Brown 	while (name_list) {
588812db3f7SLen Brown 		char *comma;
589812db3f7SLen Brown 
590812db3f7SLen Brown 		comma = strchr(name_list, ',');
591812db3f7SLen Brown 
592812db3f7SLen Brown 		if (comma)
593812db3f7SLen Brown 			*comma = '\0';
594812db3f7SLen Brown 
5953f44a5c6SLen Brown 		if (!strcmp(name_list, "all"))
5963f44a5c6SLen Brown 			return ~0;
5973f44a5c6SLen Brown 
598812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
599812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
600812db3f7SLen Brown 				retval |= (1ULL << i);
601812db3f7SLen Brown 				break;
602812db3f7SLen Brown 			}
603812db3f7SLen Brown 		}
604812db3f7SLen Brown 		if (i == MAX_BIC) {
605dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
606812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
607812db3f7SLen Brown 				exit(-1);
608812db3f7SLen Brown 			}
609dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
610dd778a5eSLen Brown 			if (debug)
611dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
612dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
613dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
614dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
615dd778a5eSLen Brown 				help();
616dd778a5eSLen Brown 				exit(1);
617dd778a5eSLen Brown 			}
618dd778a5eSLen Brown 		}
619812db3f7SLen Brown 
620812db3f7SLen Brown 		name_list = comma;
621812db3f7SLen Brown 		if (name_list)
622812db3f7SLen Brown 			name_list++;
623812db3f7SLen Brown 
624812db3f7SLen Brown 	}
625812db3f7SLen Brown 	return retval;
626812db3f7SLen Brown }
627fc04cc67SLen Brown 
628dd778a5eSLen Brown 
629c8ade361SLen Brown void print_header(char *delim)
630103a8feaSLen Brown {
631388e9c81SLen Brown 	struct msr_counter *mp;
6326168c2e0SLen Brown 	int printed = 0;
633388e9c81SLen Brown 
6343f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
6353f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
6363f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
6373f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
638812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
6396168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
6406de68fe1SLen Brown 	if (DO_BIC(BIC_Die))
6416de68fe1SLen Brown 		outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
64201235041SPrarit Bhargava 	if (DO_BIC(BIC_Node))
64301235041SPrarit Bhargava 		outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
644812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
6456168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
646812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
6476168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
6484c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
6494c2122d4SLen Brown 		outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
6504c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
6514c2122d4SLen Brown 		outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
652812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
6536168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
654812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
6556168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
656812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
6576168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
658812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
6596168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
6601cc21f7bSLen Brown 
6610de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
6620de6c0dfSLen Brown 		if (sums_need_wide_columns)
6636168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
6640de6c0dfSLen Brown 		else
6656168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
6660de6c0dfSLen Brown 	}
6670de6c0dfSLen Brown 
668812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
6696168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
6701cc21f7bSLen Brown 
671388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
672dd778a5eSLen Brown 
673388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
674388e9c81SLen Brown 			if (mp->width == 64)
675dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
676388e9c81SLen Brown 			else
677dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
678388e9c81SLen Brown 		} else {
6790de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
680dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
6810de6c0dfSLen Brown 			else
682dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
683388e9c81SLen Brown 		}
684388e9c81SLen Brown 	}
685388e9c81SLen Brown 
68641618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
6876168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
688562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
6896168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
690812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
6916168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
692812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
6936168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
694678a3bd1SLen Brown 
6950539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
6966168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
697678a3bd1SLen Brown 
698812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
6996168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
700388e9c81SLen Brown 
7019392bd98SCalvin Walton 	if (do_rapl && !rapl_joules) {
7029392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
7039392bd98SCalvin Walton 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
7049392bd98SCalvin Walton 	} else if (do_rapl && rapl_joules) {
7059392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
7069392bd98SCalvin Walton 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
7079392bd98SCalvin Walton 	}
7089392bd98SCalvin Walton 
709388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
710388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
711388e9c81SLen Brown 			if (mp->width == 64)
712c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
713388e9c81SLen Brown 			else
714c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
715388e9c81SLen Brown 		} else {
7160de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7170de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7180de6c0dfSLen Brown 			else
719c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
720388e9c81SLen Brown 		}
721388e9c81SLen Brown 	}
722388e9c81SLen Brown 
723812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
7246168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
725889facbeSLen Brown 
726812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
7276168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
728fdf676e5SLen Brown 
729812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
7306168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
73127d47356SLen Brown 
732a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
7336168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
734a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
7356168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
736a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
7376168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
738a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
7396168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
7400b2bb692SLen Brown 
7410f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
7426168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
7430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
7446168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
7450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
7466168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
7470f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
7486168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
7490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
7506168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
7510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
7526168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
7530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
7546168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
755be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
756be0e54c4SLen Brown 		outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
757be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
758be0e54c4SLen Brown 		outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
759103a8feaSLen Brown 
7605c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
761812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
7626168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
7639392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
7646168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
765812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
7666168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
767812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
7686168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
769812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7706168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
771812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7726168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
773d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
774812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
7756168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
7769392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
7776168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
778812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
7796168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
780812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
7816168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
782812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7836168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
784812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7856168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
7865c56be9aSDirk Brandewie 	}
787388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
788388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
789388e9c81SLen Brown 			if (mp->width == 64)
790c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
791388e9c81SLen Brown 			else
792c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
793388e9c81SLen Brown 		} else {
7940de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7950de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7960de6c0dfSLen Brown 			else
797c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
798388e9c81SLen Brown 		}
799388e9c81SLen Brown 	}
800388e9c81SLen Brown 
801c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
802103a8feaSLen Brown }
803103a8feaSLen Brown 
804c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
805c98d5d94SLen Brown 	struct pkg_data *p)
806103a8feaSLen Brown {
807388e9c81SLen Brown 	int i;
808388e9c81SLen Brown 	struct msr_counter *mp;
809388e9c81SLen Brown 
8103b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
811c98d5d94SLen Brown 
812c98d5d94SLen Brown 	if (t) {
8133b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
8143b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
8153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
8163b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
8173b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
8183b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
8196886fee4SLen Brown 
820812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
8210de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
822812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
823218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
824388e9c81SLen Brown 
825388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
826388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
827388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
828388e9c81SLen Brown 		}
829103a8feaSLen Brown 	}
830103a8feaSLen Brown 
831c98d5d94SLen Brown 	if (c) {
8323b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
8333b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
8343b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
8353b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
8363b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
8379392bd98SCalvin Walton 		outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
838388e9c81SLen Brown 
839388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
840388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
841388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
842388e9c81SLen Brown 		}
8430539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
844c98d5d94SLen Brown 	}
845103a8feaSLen Brown 
846c98d5d94SLen Brown 	if (p) {
8473b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
8480b2bb692SLen Brown 
8490b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
8500b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
8510b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
8520b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
8530b2bb692SLen Brown 
8543b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
8550f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
8563b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
8570f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
8583b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
8590f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
8603b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
8613b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
8623b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
8633b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
864be0e54c4SLen Brown 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
865be0e54c4SLen Brown 		outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
866be0e54c4SLen Brown 		outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
8673b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
8683b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
8693b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
8703b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
8713b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
8723b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
8733b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
8743b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
8753b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
876388e9c81SLen Brown 
877388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
878388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
879388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
880388e9c81SLen Brown 		}
881c98d5d94SLen Brown 	}
8823b4d5c7fSAndy Shevchenko 
8833b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
8843b4d5c7fSAndy Shevchenko 
885c98d5d94SLen Brown 	return 0;
886103a8feaSLen Brown }
887103a8feaSLen Brown 
888e23da037SLen Brown /*
889e23da037SLen Brown  * column formatting convention & formats
890e23da037SLen Brown  */
891c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
892c98d5d94SLen Brown 	struct pkg_data *p)
893103a8feaSLen Brown {
894008d396eSLen Brown 	double interval_float, tsc;
895fc04cc67SLen Brown 	char *fmt8;
896388e9c81SLen Brown 	int i;
897388e9c81SLen Brown 	struct msr_counter *mp;
8986168c2e0SLen Brown 	char *delim = "\t";
8996168c2e0SLen Brown 	int printed = 0;
900103a8feaSLen Brown 
901c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
902c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
903c98d5d94SLen Brown 		return 0;
904c98d5d94SLen Brown 
905c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
906c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
907c98d5d94SLen Brown 		return 0;
908c98d5d94SLen Brown 
9091ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
9101ef7d21aSLen Brown 	if ((t != &average.threads) &&
9111ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
9121ef7d21aSLen Brown 		return 0;
9131ef7d21aSLen Brown 
9143f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
915f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
916f4fdf2b4SLen Brown 		struct timeval tv;
917f4fdf2b4SLen Brown 
918f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
919f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
920f4fdf2b4SLen Brown 	}
921f4fdf2b4SLen Brown 
9223f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
9233f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
9243f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
9253f44a5c6SLen Brown 
926103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
927103a8feaSLen Brown 
928008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
929008d396eSLen Brown 
930c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
931c98d5d94SLen Brown 	if (t == &average.threads) {
932812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
9336168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9346de68fe1SLen Brown 		if (DO_BIC(BIC_Die))
9356de68fe1SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
93601235041SPrarit Bhargava 		if (DO_BIC(BIC_Node))
93701235041SPrarit Bhargava 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
938812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
9396168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
940812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9416168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9424c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9434c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9444c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9454c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
946103a8feaSLen Brown 	} else {
947812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
948c98d5d94SLen Brown 			if (p)
9496168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
950c98d5d94SLen Brown 			else
9516168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
952c98d5d94SLen Brown 		}
9536de68fe1SLen Brown 		if (DO_BIC(BIC_Die)) {
9546de68fe1SLen Brown 			if (c)
9556de68fe1SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
9566de68fe1SLen Brown 			else
9576de68fe1SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9586de68fe1SLen Brown 		}
95901235041SPrarit Bhargava 		if (DO_BIC(BIC_Node)) {
96001235041SPrarit Bhargava 			if (t)
96101235041SPrarit Bhargava 				outp += sprintf(outp, "%s%d",
96201235041SPrarit Bhargava 						(printed++ ? delim : ""),
96301235041SPrarit Bhargava 					      cpus[t->cpu_id].physical_node_id);
96401235041SPrarit Bhargava 			else
96501235041SPrarit Bhargava 				outp += sprintf(outp, "%s-",
96601235041SPrarit Bhargava 						(printed++ ? delim : ""));
96701235041SPrarit Bhargava 		}
968812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
969c98d5d94SLen Brown 			if (c)
9706168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
971c98d5d94SLen Brown 			else
9726168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
973c98d5d94SLen Brown 		}
974812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9756168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
9764c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9774c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
9784c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9794c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
980103a8feaSLen Brown 	}
981fc04cc67SLen Brown 
982812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
9836168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
984fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
985fc04cc67SLen Brown 
986812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
9876168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
988103a8feaSLen Brown 
989812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
99021ed5574SLen Brown 		if (has_base_hz)
9916168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
99221ed5574SLen Brown 		else
9936168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
994008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
99521ed5574SLen Brown 	}
996103a8feaSLen Brown 
997812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
9986168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
999103a8feaSLen Brown 
1000562a2d37SLen Brown 	/* IRQ */
10010de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
10020de6c0dfSLen Brown 		if (sums_need_wide_columns)
10036168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
10040de6c0dfSLen Brown 		else
10056168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
10060de6c0dfSLen Brown 	}
1007562a2d37SLen Brown 
10081cc21f7bSLen Brown 	/* SMI */
1009812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
10106168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
10111cc21f7bSLen Brown 
1012678a3bd1SLen Brown 	/* Added counters */
1013678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1014678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
1015678a3bd1SLen Brown 			if (mp->width == 32)
10165f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
1017678a3bd1SLen Brown 			else
10186168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1019678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10200de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10216168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
10220de6c0dfSLen Brown 			else
10236168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1024678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
102541618e63SLen Brown 			if (mp->type == COUNTER_USEC)
10266168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
102741618e63SLen Brown 			else
10286168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
1029678a3bd1SLen Brown 		}
1030678a3bd1SLen Brown 	}
1031678a3bd1SLen Brown 
103241618e63SLen Brown 	/* C1 */
103341618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
10346168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
103541618e63SLen Brown 
103641618e63SLen Brown 
1037c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
1038c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1039c98d5d94SLen Brown 		goto done;
1040c98d5d94SLen Brown 
1041562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
10426168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
1043812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
10446168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
1045812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
10466168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
1047c98d5d94SLen Brown 
10480539ba11SLen Brown 	/* Mod%c6 */
10490539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
10506168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
10510539ba11SLen Brown 
1052812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
10536168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1054889facbeSLen Brown 
1055388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1056388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1057388e9c81SLen Brown 			if (mp->width == 32)
10585f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1059388e9c81SLen Brown 			else
10606168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1061388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10620de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10636168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
10640de6c0dfSLen Brown 			else
10656168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1066388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10676168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1068388e9c81SLen Brown 		}
1069388e9c81SLen Brown 	}
1070388e9c81SLen Brown 
10719392bd98SCalvin Walton 	/*
10729392bd98SCalvin Walton 	 * If measurement interval exceeds minimum RAPL Joule Counter range,
10739392bd98SCalvin Walton 	 * indicate that results are suspect by printing "**" in fraction place.
10749392bd98SCalvin Walton 	 */
10759392bd98SCalvin Walton 	if (interval_float < rapl_joule_counter_range)
10769392bd98SCalvin Walton 		fmt8 = "%s%.2f";
10779392bd98SCalvin Walton 	else
10789392bd98SCalvin Walton 		fmt8 = "%6.0f**";
10799392bd98SCalvin Walton 
10809392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
10819392bd98SCalvin Walton 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
10829392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
10839392bd98SCalvin Walton 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
10849392bd98SCalvin Walton 
1085c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
1086c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1087c98d5d94SLen Brown 		goto done;
1088c98d5d94SLen Brown 
10890b2bb692SLen Brown 	/* PkgTmp */
1090812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
10916168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1092889facbeSLen Brown 
1093fdf676e5SLen Brown 	/* GFXrc6 */
1094812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
1095ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
10966168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
10979185e988SLen Brown 		} else {
10986168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
10999185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
11009185e988SLen Brown 		}
11019185e988SLen Brown 	}
1102fdf676e5SLen Brown 
110327d47356SLen Brown 	/* GFXMHz */
1104812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
11056168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
110627d47356SLen Brown 
11070b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1108a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
11096168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1110a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
11116168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1112a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
11136168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1114a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
11156168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
11160b2bb692SLen Brown 
11170f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
11186168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
11190f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
11206168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
11210f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
11226168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
11230f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
11246168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
11250f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
11266168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
11270f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
11286168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
11290f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
11306168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1131889facbeSLen Brown 
1132be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1133be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1134be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1135be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1136be0e54c4SLen Brown 
1137812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
11386168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
11399392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
11406168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1141812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
11426168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1143812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
11446168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1145812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
11466168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
11479392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
11486168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1149812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
11506168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1151812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
11526168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1153812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
11546168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1155812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
11566168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1157812db3f7SLen Brown 
1158388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1159388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1160388e9c81SLen Brown 			if (mp->width == 32)
11615f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1162388e9c81SLen Brown 			else
11636168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1164388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
11650de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
11666168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
11670de6c0dfSLen Brown 			else
11686168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1169388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
11706168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1171388e9c81SLen Brown 		}
1172388e9c81SLen Brown 	}
1173388e9c81SLen Brown 
1174c98d5d94SLen Brown done:
117594d6ab4bSLen Brown 	if (*(outp - 1) != '\n')
1176c98d5d94SLen Brown 		outp += sprintf(outp, "\n");
1177c98d5d94SLen Brown 
1178c98d5d94SLen Brown 	return 0;
1179103a8feaSLen Brown }
1180103a8feaSLen Brown 
1181b7d8c148SLen Brown void flush_output_stdout(void)
1182103a8feaSLen Brown {
1183b7d8c148SLen Brown 	FILE *filep;
1184b7d8c148SLen Brown 
1185b7d8c148SLen Brown 	if (outf == stderr)
1186b7d8c148SLen Brown 		filep = stdout;
1187b7d8c148SLen Brown 	else
1188b7d8c148SLen Brown 		filep = outf;
1189b7d8c148SLen Brown 
1190b7d8c148SLen Brown 	fputs(output_buffer, filep);
1191b7d8c148SLen Brown 	fflush(filep);
1192b7d8c148SLen Brown 
1193c98d5d94SLen Brown 	outp = output_buffer;
1194c98d5d94SLen Brown }
1195b7d8c148SLen Brown void flush_output_stderr(void)
1196c98d5d94SLen Brown {
1197b7d8c148SLen Brown 	fputs(output_buffer, outf);
1198b7d8c148SLen Brown 	fflush(outf);
1199c98d5d94SLen Brown 	outp = output_buffer;
1200c98d5d94SLen Brown }
1201c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1202c98d5d94SLen Brown {
1203e23da037SLen Brown 	static int printed;
1204103a8feaSLen Brown 
1205e23da037SLen Brown 	if (!printed || !summary_only)
1206c8ade361SLen Brown 		print_header("\t");
1207103a8feaSLen Brown 
12089d83601aSLen Brown 	format_counters(&average.threads, &average.cores, &average.packages);
1209103a8feaSLen Brown 
1210e23da037SLen Brown 	printed = 1;
1211e23da037SLen Brown 
1212e23da037SLen Brown 	if (summary_only)
1213e23da037SLen Brown 		return;
1214e23da037SLen Brown 
1215c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1216103a8feaSLen Brown }
1217103a8feaSLen Brown 
1218889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1219889facbeSLen Brown 	if (new > old) {			\
1220889facbeSLen Brown 		old = new - old;		\
1221889facbeSLen Brown 	} else {				\
1222889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1223889facbeSLen Brown 	}
1224889facbeSLen Brown 
1225ba3dec99SLen Brown int
1226c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1227103a8feaSLen Brown {
1228388e9c81SLen Brown 	int i;
1229388e9c81SLen Brown 	struct msr_counter *mp;
12300b2bb692SLen Brown 
1231a99d8730SLen Brown 
1232a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
12330b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1234a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
12350b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1236a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
12370b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1238a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
12390b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1240a99d8730SLen Brown 
1241c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
12420f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1243c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
12440f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1245c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
12460f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1247c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1248ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1249ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1250ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1251be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1252be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1253889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1254889facbeSLen Brown 
12559185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
12569185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
12579185e988SLen Brown 		old->gfx_rc6_ms = -1;
12589185e988SLen Brown 	else
1259fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
12609185e988SLen Brown 
126127d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
126227d47356SLen Brown 
1263889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1264889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1265889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1266889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1267889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1268889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1269ba3dec99SLen Brown 
1270388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1271388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1272388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1273388e9c81SLen Brown 		else
1274388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1275388e9c81SLen Brown 	}
1276388e9c81SLen Brown 
1277ba3dec99SLen Brown 	return 0;
1278103a8feaSLen Brown }
1279103a8feaSLen Brown 
1280c98d5d94SLen Brown void
1281c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1282c98d5d94SLen Brown {
1283388e9c81SLen Brown 	int i;
1284388e9c81SLen Brown 	struct msr_counter *mp;
1285388e9c81SLen Brown 
1286c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1287c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1288c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1289889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
12900539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1291388e9c81SLen Brown 
12929392bd98SCalvin Walton 	DELTA_WRAP32(new->core_energy, old->core_energy);
12939392bd98SCalvin Walton 
1294388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1295388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1296388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1297388e9c81SLen Brown 		else
1298388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1299388e9c81SLen Brown 	}
1300103a8feaSLen Brown }
1301c98d5d94SLen Brown 
1302c3ae331dSLen Brown /*
1303c3ae331dSLen Brown  * old = new - old
1304c3ae331dSLen Brown  */
1305ba3dec99SLen Brown int
1306c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1307c98d5d94SLen Brown 	struct core_data *core_delta)
1308c98d5d94SLen Brown {
1309388e9c81SLen Brown 	int i;
1310388e9c81SLen Brown 	struct msr_counter *mp;
1311388e9c81SLen Brown 
13124c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
13134c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
13144c2122d4SLen Brown 		new->apic_id = old->apic_id;
13154c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
13164c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
13174c2122d4SLen Brown 
13183f44a5c6SLen Brown 	/*
13193f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
13203f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
13213f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
13223f44a5c6SLen Brown 	 */
13233f44a5c6SLen Brown 
13243f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
13253f44a5c6SLen Brown 	old->tv_end = new->tv_end;
13263f44a5c6SLen Brown 
1327c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1328c98d5d94SLen Brown 
1329103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1330b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1331b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1332b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1333b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1334103a8feaSLen Brown 
1335c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1336c98d5d94SLen Brown 
1337812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1338c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1339c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1340c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1341c98d5d94SLen Brown 		} else {
1342ba3dec99SLen Brown 			return -1;
1343103a8feaSLen Brown 		}
1344a729617cSLen Brown 	}
1345103a8feaSLen Brown 
1346c98d5d94SLen Brown 
1347144b44b1SLen Brown 	if (use_c1_residency_msr) {
1348144b44b1SLen Brown 		/*
1349144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1350144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1351144b44b1SLen Brown 		 */
1352144b44b1SLen Brown 	} else {
1353103a8feaSLen Brown 		/*
1354c3ae331dSLen Brown 		 * As counter collection is not atomic,
1355c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1356103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1357103a8feaSLen Brown 		 */
135895149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1359c98d5d94SLen Brown 			old->c1 = 0;
1360c98d5d94SLen Brown 		else {
1361c98d5d94SLen Brown 			/* normal case, derive c1 */
1362008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1363c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1364c98d5d94SLen Brown 		}
1365144b44b1SLen Brown 	}
1366c3ae331dSLen Brown 
1367c98d5d94SLen Brown 	if (old->mperf == 0) {
1368b7d8c148SLen Brown 		if (debug > 1)
1369b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1370c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1371c98d5d94SLen Brown 	}
1372103a8feaSLen Brown 
1373812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1374562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1375562a2d37SLen Brown 
1376812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
13771ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1378ba3dec99SLen Brown 
1379388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1380388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1381388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1382388e9c81SLen Brown 		else
1383388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1384388e9c81SLen Brown 	}
1385ba3dec99SLen Brown 	return 0;
1386103a8feaSLen Brown }
1387c98d5d94SLen Brown 
1388c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1389c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1390c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1391c98d5d94SLen Brown {
1392ba3dec99SLen Brown 	int retval = 0;
1393ba3dec99SLen Brown 
1394c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1395c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1396c98d5d94SLen Brown 		delta_core(c, c2);
1397c98d5d94SLen Brown 
1398c98d5d94SLen Brown 	/* always calculate thread delta */
1399ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1400ba3dec99SLen Brown 	if (retval)
1401ba3dec99SLen Brown 		return retval;
1402c98d5d94SLen Brown 
1403c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1404c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1405ba3dec99SLen Brown 		retval = delta_package(p, p2);
1406c98d5d94SLen Brown 
1407ba3dec99SLen Brown 	return retval;
1408103a8feaSLen Brown }
1409103a8feaSLen Brown 
1410c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1411103a8feaSLen Brown {
1412388e9c81SLen Brown 	int i;
1413388e9c81SLen Brown 	struct msr_counter  *mp;
1414388e9c81SLen Brown 
14153f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
14163f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
14173f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
14183f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
14193f44a5c6SLen Brown 
1420c98d5d94SLen Brown 	t->tsc = 0;
1421c98d5d94SLen Brown 	t->aperf = 0;
1422c98d5d94SLen Brown 	t->mperf = 0;
1423c98d5d94SLen Brown 	t->c1 = 0;
1424103a8feaSLen Brown 
1425562a2d37SLen Brown 	t->irq_count = 0;
1426562a2d37SLen Brown 	t->smi_count = 0;
1427562a2d37SLen Brown 
1428c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1429c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1430c98d5d94SLen Brown 
1431c98d5d94SLen Brown 	c->c3 = 0;
1432c98d5d94SLen Brown 	c->c6 = 0;
1433c98d5d94SLen Brown 	c->c7 = 0;
14340539ba11SLen Brown 	c->mc6_us = 0;
1435889facbeSLen Brown 	c->core_temp_c = 0;
14369392bd98SCalvin Walton 	c->core_energy = 0;
1437c98d5d94SLen Brown 
14380b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
14390b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
14400b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
14410b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
14420b2bb692SLen Brown 
1443c98d5d94SLen Brown 	p->pc2 = 0;
14440f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1445c98d5d94SLen Brown 		p->pc3 = 0;
14460f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1447c98d5d94SLen Brown 		p->pc6 = 0;
14480f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1449c98d5d94SLen Brown 		p->pc7 = 0;
1450ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1451ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1452ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1453be0e54c4SLen Brown 	p->cpu_lpi = 0;
1454be0e54c4SLen Brown 	p->sys_lpi = 0;
1455889facbeSLen Brown 
1456889facbeSLen Brown 	p->energy_pkg = 0;
1457889facbeSLen Brown 	p->energy_dram = 0;
1458889facbeSLen Brown 	p->energy_cores = 0;
1459889facbeSLen Brown 	p->energy_gfx = 0;
1460889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1461889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1462889facbeSLen Brown 	p->pkg_temp_c = 0;
146327d47356SLen Brown 
1464fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
146527d47356SLen Brown 	p->gfx_mhz = 0;
1466388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1467388e9c81SLen Brown 		t->counter[i] = 0;
1468388e9c81SLen Brown 
1469388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1470388e9c81SLen Brown 		c->counter[i] = 0;
1471388e9c81SLen Brown 
1472388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1473388e9c81SLen Brown 		p->counter[i] = 0;
1474103a8feaSLen Brown }
1475c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1476c98d5d94SLen Brown 	struct pkg_data *p)
1477103a8feaSLen Brown {
1478388e9c81SLen Brown 	int i;
1479388e9c81SLen Brown 	struct msr_counter *mp;
1480388e9c81SLen Brown 
14814c2122d4SLen Brown 	/* copy un-changing apic_id's */
14824c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
14834c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
14844c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
14854c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
14864c2122d4SLen Brown 
14873f44a5c6SLen Brown 	/* remember first tv_begin */
14883f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
14893f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
14903f44a5c6SLen Brown 
14913f44a5c6SLen Brown 	/* remember last tv_end */
14923f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
14933f44a5c6SLen Brown 
1494c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1495c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1496c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1497c98d5d94SLen Brown 	average.threads.c1 += t->c1;
149815aaa346SLen Brown 
1499562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1500562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1501562a2d37SLen Brown 
1502388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1503388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1504388e9c81SLen Brown 			continue;
1505388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1506388e9c81SLen Brown 	}
1507388e9c81SLen Brown 
1508c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1509c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1510c98d5d94SLen Brown 		return 0;
1511c98d5d94SLen Brown 
1512c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1513c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1514c98d5d94SLen Brown 	average.cores.c7 += c->c7;
15150539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1516c98d5d94SLen Brown 
1517889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1518889facbeSLen Brown 
15199392bd98SCalvin Walton 	average.cores.core_energy += c->core_energy;
15209392bd98SCalvin Walton 
1521388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1522388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1523388e9c81SLen Brown 			continue;
1524388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1525388e9c81SLen Brown 	}
1526388e9c81SLen Brown 
1527c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1528c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1529c98d5d94SLen Brown 		return 0;
1530c98d5d94SLen Brown 
1531a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
15320b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1533a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
15340b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1535a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
15360b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1537a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
15380b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
15390b2bb692SLen Brown 
1540c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
15410f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1542c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
15430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1544c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
15450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1546c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1547ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1548ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1549ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1550c98d5d94SLen Brown 
1551be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1552be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1553be0e54c4SLen Brown 
1554889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1555889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1556889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1557889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1558889facbeSLen Brown 
1559fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
156027d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
156127d47356SLen Brown 
1562889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1563889facbeSLen Brown 
1564889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1565889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1566388e9c81SLen Brown 
1567388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1568388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1569388e9c81SLen Brown 			continue;
1570388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1571388e9c81SLen Brown 	}
1572c98d5d94SLen Brown 	return 0;
1573c98d5d94SLen Brown }
1574c98d5d94SLen Brown /*
1575c98d5d94SLen Brown  * sum the counters for all cpus in the system
1576c98d5d94SLen Brown  * compute the weighted average
1577c98d5d94SLen Brown  */
1578c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1579c98d5d94SLen Brown 	struct pkg_data *p)
1580c98d5d94SLen Brown {
1581388e9c81SLen Brown 	int i;
1582388e9c81SLen Brown 	struct msr_counter *mp;
1583388e9c81SLen Brown 
1584c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1585c98d5d94SLen Brown 
1586c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1587c98d5d94SLen Brown 
1588c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1589c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1590c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1591c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1592c98d5d94SLen Brown 
15930de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
15940de6c0dfSLen Brown 		sums_need_wide_columns = 1;
15950de6c0dfSLen Brown 
1596c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1597c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1598c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
15990539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1600c98d5d94SLen Brown 
1601a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
16020b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1603a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
16040b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1605a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
16060b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1607a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
16080b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
16090b2bb692SLen Brown 
1610c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
16110f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1612c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
16130f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1614c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
16150f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1616c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1617ca58710fSKristen Carlson Accardi 
1618ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1619ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1620ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1621388e9c81SLen Brown 
1622388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1623388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1624388e9c81SLen Brown 			continue;
16250de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16260de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
16270de6c0dfSLen Brown 				sums_need_wide_columns = 1;
162841618e63SLen Brown 			continue;
16290de6c0dfSLen Brown 		}
1630388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1631388e9c81SLen Brown 	}
1632388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1633388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1634388e9c81SLen Brown 			continue;
16350de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16360de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
16370de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16380de6c0dfSLen Brown 		}
1639388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1640388e9c81SLen Brown 	}
1641388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1642388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1643388e9c81SLen Brown 			continue;
16440de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16450de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
16460de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16470de6c0dfSLen Brown 		}
1648388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1649388e9c81SLen Brown 	}
1650c98d5d94SLen Brown }
1651c98d5d94SLen Brown 
1652c98d5d94SLen Brown static unsigned long long rdtsc(void)
1653c98d5d94SLen Brown {
1654c98d5d94SLen Brown 	unsigned int low, high;
1655c98d5d94SLen Brown 
1656c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1657c98d5d94SLen Brown 
1658c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1659c98d5d94SLen Brown }
1660c98d5d94SLen Brown 
1661c98d5d94SLen Brown /*
1662495c7654SLen Brown  * Open a file, and exit on failure
1663495c7654SLen Brown  */
1664495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1665495c7654SLen Brown {
1666495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1667495c7654SLen Brown 
1668495c7654SLen Brown 	if (!filep)
1669495c7654SLen Brown 		err(1, "%s: open failed", path);
1670495c7654SLen Brown 	return filep;
1671495c7654SLen Brown }
1672495c7654SLen Brown /*
1673495c7654SLen Brown  * snapshot_sysfs_counter()
1674495c7654SLen Brown  *
1675495c7654SLen Brown  * return snapshot of given counter
1676495c7654SLen Brown  */
1677495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1678495c7654SLen Brown {
1679495c7654SLen Brown 	FILE *fp;
1680495c7654SLen Brown 	int retval;
1681495c7654SLen Brown 	unsigned long long counter;
1682495c7654SLen Brown 
1683495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1684495c7654SLen Brown 
1685495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1686495c7654SLen Brown 	if (retval != 1)
1687495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1688495c7654SLen Brown 
1689495c7654SLen Brown 	fclose(fp);
1690495c7654SLen Brown 
1691495c7654SLen Brown 	return counter;
1692495c7654SLen Brown }
1693495c7654SLen Brown 
1694495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1695495c7654SLen Brown {
1696495c7654SLen Brown 	if (mp->msr_num != 0) {
1697495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1698495c7654SLen Brown 			return -1;
1699495c7654SLen Brown 	} else {
170046c27978SLen Brown 		char path[128 + PATH_BYTES];
170141618e63SLen Brown 
170241618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
170341618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
170441618e63SLen Brown 				 cpu, mp->path);
170541618e63SLen Brown 
170641618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
170741618e63SLen Brown 		} else {
1708495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1709495c7654SLen Brown 		}
171041618e63SLen Brown 	}
1711495c7654SLen Brown 
1712495c7654SLen Brown 	return 0;
1713495c7654SLen Brown }
1714495c7654SLen Brown 
17154c2122d4SLen Brown void get_apic_id(struct thread_data *t)
17164c2122d4SLen Brown {
171734041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
171834041551SLen Brown 
171934041551SLen Brown 	if (DO_BIC(BIC_APIC)) {
172034041551SLen Brown 		eax = ebx = ecx = edx = 0;
172134041551SLen Brown 		__cpuid(1, eax, ebx, ecx, edx);
172234041551SLen Brown 
172334041551SLen Brown 		t->apic_id = (ebx >> 24) & 0xff;
172434041551SLen Brown 	}
172534041551SLen Brown 
172634041551SLen Brown 	if (!DO_BIC(BIC_X2APIC))
172734041551SLen Brown 		return;
172834041551SLen Brown 
172934041551SLen Brown 	if (authentic_amd) {
173034041551SLen Brown 		unsigned int topology_extensions;
173134041551SLen Brown 
173234041551SLen Brown 		if (max_extended_level < 0x8000001e)
173334041551SLen Brown 			return;
17344c2122d4SLen Brown 
17354c2122d4SLen Brown 		eax = ebx = ecx = edx = 0;
173634041551SLen Brown 		__cpuid(0x80000001, eax, ebx, ecx, edx);
173734041551SLen Brown 			topology_extensions = ecx & (1 << 22);
173834041551SLen Brown 
173934041551SLen Brown 		if (topology_extensions == 0)
174034041551SLen Brown 			return;
174134041551SLen Brown 
174234041551SLen Brown 		eax = ebx = ecx = edx = 0;
174334041551SLen Brown 		__cpuid(0x8000001e, eax, ebx, ecx, edx);
174434041551SLen Brown 
174534041551SLen Brown 		t->x2apic_id = eax;
174634041551SLen Brown 		return;
174734041551SLen Brown 	}
17484c2122d4SLen Brown 
17494c2122d4SLen Brown 	if (!genuine_intel)
17504c2122d4SLen Brown 		return;
17514c2122d4SLen Brown 
17524c2122d4SLen Brown 	if (max_level < 0xb)
17534c2122d4SLen Brown 		return;
17544c2122d4SLen Brown 
17554c2122d4SLen Brown 	ecx = 0;
17564c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
17574c2122d4SLen Brown 	t->x2apic_id = edx;
17584c2122d4SLen Brown 
175934041551SLen Brown 	if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
176034041551SLen Brown 		fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n",
176134041551SLen Brown 				t->cpu_id, t->apic_id, t->x2apic_id);
17624c2122d4SLen Brown }
17634c2122d4SLen Brown 
1764495c7654SLen Brown /*
1765c98d5d94SLen Brown  * get_counters(...)
1766c98d5d94SLen Brown  * migrate to cpu
1767c98d5d94SLen Brown  * acquire and record local counters for that cpu
1768c98d5d94SLen Brown  */
1769c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1770c98d5d94SLen Brown {
1771c98d5d94SLen Brown 	int cpu = t->cpu_id;
1772889facbeSLen Brown 	unsigned long long msr;
17730102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1774388e9c81SLen Brown 	struct msr_counter *mp;
1775388e9c81SLen Brown 	int i;
1776c98d5d94SLen Brown 
1777f4fdf2b4SLen Brown 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1778f4fdf2b4SLen Brown 
1779e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1780b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
178115aaa346SLen Brown 		return -1;
1782e52966c0SLen Brown 	}
178315aaa346SLen Brown 
17844c2122d4SLen Brown 	if (first_counter_read)
17854c2122d4SLen Brown 		get_apic_id(t);
17860102b067SLen Brown retry:
1787c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
178815aaa346SLen Brown 
1789812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
17900102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
17910102b067SLen Brown 
17920102b067SLen Brown 		/*
17930102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
17940102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
17950102b067SLen Brown 		 *
17960102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
17970102b067SLen Brown 		 * individual system call, so delays may occur
17980102b067SLen Brown 		 * between them.  If the time to read them
17990102b067SLen Brown 		 * varies by a large amount, we re-read them.
18000102b067SLen Brown 		 */
18010102b067SLen Brown 
18020102b067SLen Brown 		/*
18030102b067SLen Brown 		 * This initial dummy APERF read has been seen to
18040102b067SLen Brown 		 * reduce jitter in the subsequent reads.
18050102b067SLen Brown 		 */
18060102b067SLen Brown 
18079c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1808c98d5d94SLen Brown 			return -3;
18090102b067SLen Brown 
18100102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
18110102b067SLen Brown 
18120102b067SLen Brown 		tsc_before = t->tsc;
18130102b067SLen Brown 
18140102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
18150102b067SLen Brown 			return -3;
18160102b067SLen Brown 
18170102b067SLen Brown 		tsc_between = rdtsc();
18180102b067SLen Brown 
18199c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1820c98d5d94SLen Brown 			return -4;
18210102b067SLen Brown 
18220102b067SLen Brown 		tsc_after = rdtsc();
18230102b067SLen Brown 
18240102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
18250102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
18260102b067SLen Brown 
18270102b067SLen Brown 		/*
18280102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
18290102b067SLen Brown 		 * differ by more than 2x, then try again.
18300102b067SLen Brown 		 */
18310102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
18320102b067SLen Brown 			aperf_mperf_retry_count++;
18330102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
18340102b067SLen Brown 				goto retry;
18350102b067SLen Brown 			else
18360102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
18370102b067SLen Brown 					cpu, aperf_time, mperf_time);
18380102b067SLen Brown 		}
18390102b067SLen Brown 		aperf_mperf_retry_count = 0;
18400102b067SLen Brown 
1841b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1842b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
184388c3281fSLen Brown 	}
184488c3281fSLen Brown 
1845812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1846562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1847812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
18481ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
18491ed51011SLen Brown 			return -5;
18501ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
18511ed51011SLen Brown 	}
18520539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1853144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1854144b44b1SLen Brown 			return -6;
1855144b44b1SLen Brown 	}
1856144b44b1SLen Brown 
1857388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1858495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1859388e9c81SLen Brown 			return -10;
1860388e9c81SLen Brown 	}
1861388e9c81SLen Brown 
1862c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1863c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1864f4fdf2b4SLen Brown 		goto done;
1865c98d5d94SLen Brown 
1866562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3)) {
1867c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1868c98d5d94SLen Brown 			return -6;
1869144b44b1SLen Brown 	}
1870144b44b1SLen Brown 
1871812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1872c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1873c98d5d94SLen Brown 			return -7;
1874fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1875fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1876fb5d4327SDasaratharaman Chandramouli 			return -7;
1877103a8feaSLen Brown 	}
187815aaa346SLen Brown 
1879812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1880c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1881c98d5d94SLen Brown 			return -8;
1882c98d5d94SLen Brown 
18830539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
18840539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
18850539ba11SLen Brown 			return -8;
18860539ba11SLen Brown 
1887812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1888889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1889889facbeSLen Brown 			return -9;
1890889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1891889facbeSLen Brown 	}
1892889facbeSLen Brown 
18939392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
18949392bd98SCalvin Walton 		if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
18959392bd98SCalvin Walton 			return -14;
18969392bd98SCalvin Walton 		c->core_energy = msr & 0xFFFFFFFF;
18979392bd98SCalvin Walton 	}
18989392bd98SCalvin Walton 
1899388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1900495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1901388e9c81SLen Brown 			return -10;
1902388e9c81SLen Brown 	}
1903889facbeSLen Brown 
1904c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1905c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1906f4fdf2b4SLen Brown 		goto done;
190715aaa346SLen Brown 
1908a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
19090b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
19100b2bb692SLen Brown 			return -10;
1911a99d8730SLen Brown 	}
1912a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
19130b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
19140b2bb692SLen Brown 			return -11;
1915a99d8730SLen Brown 	}
1916a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
19170b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
19180b2bb692SLen Brown 			return -12;
1919a99d8730SLen Brown 	}
1920a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
19210b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
19220b2bb692SLen Brown 			return -13;
19230b2bb692SLen Brown 	}
19240f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1925c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1926c98d5d94SLen Brown 			return -9;
19270f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
19280539ba11SLen Brown 		if (do_slm_cstates) {
19290539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
19300539ba11SLen Brown 				return -10;
19310539ba11SLen Brown 		} else {
1932c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1933c98d5d94SLen Brown 				return -10;
19340539ba11SLen Brown 		}
19350539ba11SLen Brown 	}
19360539ba11SLen Brown 
19370f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1938c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1939c98d5d94SLen Brown 			return -11;
19400f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1941c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1942c98d5d94SLen Brown 			return -12;
19430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1944ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1945ca58710fSKristen Carlson Accardi 			return -13;
19460f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1947ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1948ca58710fSKristen Carlson Accardi 			return -13;
19490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1950ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1951ca58710fSKristen Carlson Accardi 			return -13;
19520f47c08dSLen Brown 
1953be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1954be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1955be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1956be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
1957be0e54c4SLen Brown 
1958889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1959889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1960889facbeSLen Brown 			return -13;
1961889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1962889facbeSLen Brown 	}
19639148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1964889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1965889facbeSLen Brown 			return -14;
1966889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1967889facbeSLen Brown 	}
1968889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1969889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1970889facbeSLen Brown 			return -15;
1971889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1972889facbeSLen Brown 	}
1973889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1974889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1975889facbeSLen Brown 			return -16;
1976889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1977889facbeSLen Brown 	}
1978889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1979889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1980889facbeSLen Brown 			return -16;
1981889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1982889facbeSLen Brown 	}
1983889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1984889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1985889facbeSLen Brown 			return -16;
1986889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1987889facbeSLen Brown 	}
1988*3316f99aSCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
1989*3316f99aSCalvin Walton 		if (get_msr(cpu, MSR_PKG_ENERGY_STAT, &msr))
1990*3316f99aSCalvin Walton 			return -13;
1991*3316f99aSCalvin Walton 		p->energy_pkg = msr & 0xFFFFFFFF;
1992*3316f99aSCalvin Walton 	}
1993812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1994889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1995889facbeSLen Brown 			return -17;
1996889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1997889facbeSLen Brown 	}
1998fdf676e5SLen Brown 
1999812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2000fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
2001fdf676e5SLen Brown 
2002812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
200327d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
200427d47356SLen Brown 
2005388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2006495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
2007388e9c81SLen Brown 			return -10;
2008388e9c81SLen Brown 	}
2009f4fdf2b4SLen Brown done:
2010f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
2011388e9c81SLen Brown 
201215aaa346SLen Brown 	return 0;
2013103a8feaSLen Brown }
2014103a8feaSLen Brown 
2015ee7e38e3SLen Brown /*
2016ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2017ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
2018ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2019ee7e38e3SLen Brown  */
2020ee7e38e3SLen Brown 
2021ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
2022ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
2023ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
2024ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
2025ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
2026ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
2027ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
2028ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
2029ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
2030ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
2031ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
2032ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
20330b2bb692SLen Brown #define PCL__8 12 /* PC8 */
20340b2bb692SLen Brown #define PCL__9 13 /* PC9 */
2035445640a5SLen Brown #define PCL_10 14 /* PC10 */
2036445640a5SLen Brown #define PCLUNL 15 /* Unlimited */
2037ee7e38e3SLen Brown 
2038ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
2039ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
2040445640a5SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"};
2041ee7e38e3SLen Brown 
2042e9257f5fSLen 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};
2043e9257f5fSLen 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};
2044e9257f5fSLen 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};
20450539ba11SLen 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};
2046f2642888SLen 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};
2047e9257f5fSLen 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};
2048445640a5SLen Brown int glm_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
20492085e124SArtem 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};
2050ee7e38e3SLen Brown 
2051a2b7b749SLen Brown 
2052a2b7b749SLen Brown static void
2053a2b7b749SLen Brown calculate_tsc_tweak()
2054a2b7b749SLen Brown {
2055a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
2056a2b7b749SLen Brown }
2057a2b7b749SLen Brown 
2058fcd17211SLen Brown static void
2059fcd17211SLen Brown dump_nhm_platform_info(void)
2060103a8feaSLen Brown {
2061103a8feaSLen Brown 	unsigned long long msr;
2062103a8feaSLen Brown 	unsigned int ratio;
2063103a8feaSLen Brown 
2064ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2065103a8feaSLen Brown 
2066b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
20676574a5d5SLen Brown 
2068103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
2069710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
2070103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2071103a8feaSLen Brown 
2072103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
2073710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2074103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2075103a8feaSLen Brown 
20767ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2077b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2078bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
207967920418SLen Brown 
2080fcd17211SLen Brown 	return;
2081fcd17211SLen Brown }
2082fcd17211SLen Brown 
2083fcd17211SLen Brown static void
2084fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
2085fcd17211SLen Brown {
2086fcd17211SLen Brown 	unsigned long long msr;
2087fcd17211SLen Brown 	unsigned int ratio;
2088fcd17211SLen Brown 
20897ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2090fcd17211SLen Brown 
2091b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2092fcd17211SLen Brown 
2093fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
2094fcd17211SLen Brown 	if (ratio)
2095710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2096fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2097fcd17211SLen Brown 
2098fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2099fcd17211SLen Brown 	if (ratio)
2100710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2101fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2102fcd17211SLen Brown 	return;
2103fcd17211SLen Brown }
2104fcd17211SLen Brown 
2105fcd17211SLen Brown static void
2106fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
2107fcd17211SLen Brown {
2108fcd17211SLen Brown 	unsigned long long msr;
2109fcd17211SLen Brown 	unsigned int ratio;
21106574a5d5SLen Brown 
21117ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
21126574a5d5SLen Brown 
2113b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
21146574a5d5SLen Brown 
21156574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
21166574a5d5SLen Brown 	if (ratio)
2117710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
21186574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21196574a5d5SLen Brown 
21206574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
21216574a5d5SLen Brown 	if (ratio)
2122710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
21236574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21246574a5d5SLen Brown 
21256574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
21266574a5d5SLen Brown 	if (ratio)
2127710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
21286574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21296574a5d5SLen Brown 
21306574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
21316574a5d5SLen Brown 	if (ratio)
2132710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
21336574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21346574a5d5SLen Brown 
21356574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
21366574a5d5SLen Brown 	if (ratio)
2137710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
21386574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21396574a5d5SLen Brown 
21406574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
21416574a5d5SLen Brown 	if (ratio)
2142710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
21436574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21446574a5d5SLen Brown 
21456574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
21466574a5d5SLen Brown 	if (ratio)
2147710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
21486574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21496574a5d5SLen Brown 
21506574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
21516574a5d5SLen Brown 	if (ratio)
2152710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
21536574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
2154103a8feaSLen Brown 	return;
2155fcd17211SLen Brown }
215631e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
215731e07522SLen Brown {
215831e07522SLen Brown 
215931e07522SLen Brown 	if (!genuine_intel)
216031e07522SLen Brown 		return 0;
216131e07522SLen Brown 
216231e07522SLen Brown 	switch (model) {
216331e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
216431e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
2165f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:
216631e07522SLen Brown 		return 1;
216731e07522SLen Brown 	}
216831e07522SLen Brown 	return 0;
216931e07522SLen Brown }
2170fcd17211SLen Brown 
2171fcd17211SLen Brown static void
217231e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
2173fcd17211SLen Brown {
217431e07522SLen Brown 	unsigned long long msr, core_counts;
217531e07522SLen Brown 	unsigned int ratio, group_size;
2176103a8feaSLen Brown 
21777ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2178b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
21796574a5d5SLen Brown 
218031e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
218131e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
218231e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
218331e07522SLen Brown 	} else {
218431e07522SLen Brown 		core_counts = 0x0807060504030201;
218531e07522SLen Brown 	}
218631e07522SLen Brown 
21876574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
218831e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
21896574a5d5SLen Brown 	if (ratio)
219031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
219131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21926574a5d5SLen Brown 
21936574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
219431e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
21956574a5d5SLen Brown 	if (ratio)
219631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
219731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21986574a5d5SLen Brown 
21996574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
220031e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
22016574a5d5SLen Brown 	if (ratio)
220231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
220331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22046574a5d5SLen Brown 
22056574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
220631e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
22076574a5d5SLen Brown 	if (ratio)
220831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
220931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22106574a5d5SLen Brown 
2211103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
221231e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
2213103a8feaSLen Brown 	if (ratio)
221431e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
221531e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2216103a8feaSLen Brown 
2217103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
221831e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
2219103a8feaSLen Brown 	if (ratio)
222031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
222131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2222103a8feaSLen Brown 
2223103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
222431e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
2225103a8feaSLen Brown 	if (ratio)
222631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
222731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2228103a8feaSLen Brown 
2229103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
223031e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
2231103a8feaSLen Brown 	if (ratio)
223231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
223331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2234fcd17211SLen Brown 	return;
2235fcd17211SLen Brown }
22363a9a941dSLen Brown 
2237fcd17211SLen Brown static void
22380f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
22390f7887c4SLen Brown {
22400f7887c4SLen Brown 	unsigned long long msr;
22410f7887c4SLen Brown 	unsigned int ratio;
22420f7887c4SLen Brown 
22430f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
22440f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22450f7887c4SLen Brown 
22460f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22470f7887c4SLen Brown 	if (ratio)
22480f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
22490f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22500f7887c4SLen Brown 
22510f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22520f7887c4SLen Brown 	if (ratio)
22530f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
22540f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22550f7887c4SLen Brown 
22560f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22570f7887c4SLen Brown 	if (ratio)
22580f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
22590f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22600f7887c4SLen Brown 
22610f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
22620f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22630f7887c4SLen Brown 
22640f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
22650f7887c4SLen Brown 	if (ratio)
22660f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
22670f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22680f7887c4SLen Brown 
22690f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22700f7887c4SLen Brown 	if (ratio)
22710f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
22720f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22730f7887c4SLen Brown 
22740f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22750f7887c4SLen Brown 	if (ratio)
22760f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
22770f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22780f7887c4SLen Brown 
22790f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22800f7887c4SLen Brown 	if (ratio)
22810f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
22820f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22830f7887c4SLen Brown }
22840f7887c4SLen Brown 
22850f7887c4SLen Brown static void
2286fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2287fb5d4327SDasaratharaman Chandramouli {
2288cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2289cbf97abaSHubert Chrzaniuk 
2290fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2291cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2292cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2293cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2294cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2295fb5d4327SDasaratharaman Chandramouli 
2296ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2297fb5d4327SDasaratharaman Chandramouli 
2298b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2299bfae2052SLen Brown 		base_cpu, msr);
2300fb5d4327SDasaratharaman Chandramouli 
2301fb5d4327SDasaratharaman Chandramouli 	/**
2302fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2303cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2304cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2305fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2306fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2307fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2308fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2309fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2310fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2311fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2312fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2313fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2314fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2315fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2316fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2317fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2318fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2319fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2320fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2321fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2322fb5d4327SDasaratharaman Chandramouli 	 */
2323fb5d4327SDasaratharaman Chandramouli 
2324cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2325cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2326cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2327cbf97abaSHubert Chrzaniuk 
2328cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2329fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2330cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2331fb5d4327SDasaratharaman Chandramouli 
2332cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2333cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2334cbf97abaSHubert Chrzaniuk 		b_nr++;
2335fb5d4327SDasaratharaman Chandramouli 	}
2336cbf97abaSHubert Chrzaniuk 
2337cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2338cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2339b7d8c148SLen Brown 			fprintf(outf,
2340710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2341cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2342fb5d4327SDasaratharaman Chandramouli }
2343fb5d4327SDasaratharaman Chandramouli 
2344fb5d4327SDasaratharaman Chandramouli static void
2345fcd17211SLen Brown dump_nhm_cst_cfg(void)
2346fcd17211SLen Brown {
2347fcd17211SLen Brown 	unsigned long long msr;
2348fcd17211SLen Brown 
23491df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2350fcd17211SLen Brown 
23511df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2352fcd17211SLen Brown 
23533e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2354fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2355fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2356fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2357fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2358fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
23596c34f160SLen Brown 		(unsigned int)msr & 0xF,
2360fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2361ac980e13SArtem Bityutskiy 
2362ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2363ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
2364ac980e13SArtem Bityutskiy 		fprintf(outf, ", automatic c-state conversion=%s",
2365ac980e13SArtem Bityutskiy 			(msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2366ac980e13SArtem Bityutskiy 	}
2367ac980e13SArtem Bityutskiy 
2368ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2369ac980e13SArtem Bityutskiy 
2370fcd17211SLen Brown 	return;
2371103a8feaSLen Brown }
2372103a8feaSLen Brown 
23736fb3143bSLen Brown static void
23746fb3143bSLen Brown dump_config_tdp(void)
23756fb3143bSLen Brown {
23766fb3143bSLen Brown 	unsigned long long msr;
23776fb3143bSLen Brown 
23786fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2379b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2380685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
23816fb3143bSLen Brown 
23826fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2383b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
23846fb3143bSLen Brown 	if (msr) {
2385685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2386685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2387685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2388685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
23896fb3143bSLen Brown 	}
2390b7d8c148SLen Brown 	fprintf(outf, ")\n");
23916fb3143bSLen Brown 
23926fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2393b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
23946fb3143bSLen Brown 	if (msr) {
2395685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2396685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2397685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2398685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
23996fb3143bSLen Brown 	}
2400b7d8c148SLen Brown 	fprintf(outf, ")\n");
24016fb3143bSLen Brown 
24026fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2403b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
24046fb3143bSLen Brown 	if ((msr) & 0x3)
2405b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2406b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2407b7d8c148SLen Brown 	fprintf(outf, ")\n");
24086fb3143bSLen Brown 
24096fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2410b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2411685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2412b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2413b7d8c148SLen Brown 	fprintf(outf, ")\n");
24146fb3143bSLen Brown }
24155a63426eSLen Brown 
24165a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
24175a63426eSLen Brown 
24185a63426eSLen Brown void print_irtl(void)
24195a63426eSLen Brown {
24205a63426eSLen Brown 	unsigned long long msr;
24215a63426eSLen Brown 
24225a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
24235a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
24245a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24255a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24265a63426eSLen Brown 
24275a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
24285a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
24295a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24305a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24315a63426eSLen Brown 
24325a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
24335a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
24345a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24355a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24365a63426eSLen Brown 
24375a63426eSLen Brown 	if (!do_irtl_hsw)
24385a63426eSLen Brown 		return;
24395a63426eSLen Brown 
24405a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
24415a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
24425a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24435a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24445a63426eSLen Brown 
24455a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
24465a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
24475a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24485a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24495a63426eSLen Brown 
24505a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
24515a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
24525a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24535a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24545a63426eSLen Brown 
24555a63426eSLen Brown }
245636229897SLen Brown void free_fd_percpu(void)
245736229897SLen Brown {
245836229897SLen Brown 	int i;
245936229897SLen Brown 
246001a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
246136229897SLen Brown 		if (fd_percpu[i] != 0)
246236229897SLen Brown 			close(fd_percpu[i]);
246336229897SLen Brown 	}
246436229897SLen Brown 
246536229897SLen Brown 	free(fd_percpu);
24666fb3143bSLen Brown }
24676fb3143bSLen Brown 
2468c98d5d94SLen Brown void free_all_buffers(void)
2469103a8feaSLen Brown {
24700e2d8f05SLen Brown 	int i;
24710e2d8f05SLen Brown 
2472c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2473c98d5d94SLen Brown 	cpu_present_set = NULL;
247436229897SLen Brown 	cpu_present_setsize = 0;
2475103a8feaSLen Brown 
2476c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2477c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2478c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2479103a8feaSLen Brown 
2480c98d5d94SLen Brown 	free(thread_even);
2481c98d5d94SLen Brown 	free(core_even);
2482c98d5d94SLen Brown 	free(package_even);
2483103a8feaSLen Brown 
2484c98d5d94SLen Brown 	thread_even = NULL;
2485c98d5d94SLen Brown 	core_even = NULL;
2486c98d5d94SLen Brown 	package_even = NULL;
2487103a8feaSLen Brown 
2488c98d5d94SLen Brown 	free(thread_odd);
2489c98d5d94SLen Brown 	free(core_odd);
2490c98d5d94SLen Brown 	free(package_odd);
2491103a8feaSLen Brown 
2492c98d5d94SLen Brown 	thread_odd = NULL;
2493c98d5d94SLen Brown 	core_odd = NULL;
2494c98d5d94SLen Brown 	package_odd = NULL;
2495103a8feaSLen Brown 
2496c98d5d94SLen Brown 	free(output_buffer);
2497c98d5d94SLen Brown 	output_buffer = NULL;
2498c98d5d94SLen Brown 	outp = NULL;
249936229897SLen Brown 
250036229897SLen Brown 	free_fd_percpu();
2501562a2d37SLen Brown 
2502562a2d37SLen Brown 	free(irq_column_2_cpu);
2503562a2d37SLen Brown 	free(irqs_per_cpu);
25040e2d8f05SLen Brown 
25050e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
25060e2d8f05SLen Brown 		if (cpus[i].put_ids)
25070e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
25080e2d8f05SLen Brown 	}
25090e2d8f05SLen Brown 	free(cpus);
2510103a8feaSLen Brown }
2511103a8feaSLen Brown 
251257a42a34SJosh Triplett 
251357a42a34SJosh Triplett /*
251495aebc44SJosh Triplett  * Parse a file containing a single int.
25156de68fe1SLen Brown  * Return 0 if file can not be opened
25166de68fe1SLen Brown  * Exit if file can be opened, but can not be parsed
251795aebc44SJosh Triplett  */
251895aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
251995aebc44SJosh Triplett {
252095aebc44SJosh Triplett 	va_list args;
252195aebc44SJosh Triplett 	char path[PATH_MAX];
252295aebc44SJosh Triplett 	FILE *filep;
252395aebc44SJosh Triplett 	int value;
252495aebc44SJosh Triplett 
252595aebc44SJosh Triplett 	va_start(args, fmt);
252695aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
252795aebc44SJosh Triplett 	va_end(args);
25286de68fe1SLen Brown 	filep = fopen(path, "r");
25296de68fe1SLen Brown 	if (!filep)
25306de68fe1SLen Brown 		return 0;
2531b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2532b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
253395aebc44SJosh Triplett 	fclose(filep);
253495aebc44SJosh Triplett 	return value;
253595aebc44SJosh Triplett }
253695aebc44SJosh Triplett 
253795aebc44SJosh Triplett /*
2538c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2539c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2540c98d5d94SLen Brown  */
2541c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2542c98d5d94SLen Brown {
254395aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2544103a8feaSLen Brown }
2545103a8feaSLen Brown 
2546103a8feaSLen Brown int get_physical_package_id(int cpu)
2547103a8feaSLen Brown {
254895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2549103a8feaSLen Brown }
2550103a8feaSLen Brown 
25516de68fe1SLen Brown int get_die_id(int cpu)
25526de68fe1SLen Brown {
25536de68fe1SLen Brown 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
25546de68fe1SLen Brown }
25556de68fe1SLen Brown 
2556103a8feaSLen Brown int get_core_id(int cpu)
2557103a8feaSLen Brown {
255895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2559103a8feaSLen Brown }
2560103a8feaSLen Brown 
2561ef605741SPrarit Bhargava void set_node_data(void)
2562ef605741SPrarit Bhargava {
25632ffbb224SPrarit Bhargava 	int pkg, node, lnode, cpu, cpux;
25642ffbb224SPrarit Bhargava 	int cpu_count;
2565ef605741SPrarit Bhargava 
25662ffbb224SPrarit Bhargava 	/* initialize logical_node_id */
25672ffbb224SPrarit Bhargava 	for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
25682ffbb224SPrarit Bhargava 		cpus[cpu].logical_node_id = -1;
2569ef605741SPrarit Bhargava 
25702ffbb224SPrarit Bhargava 	cpu_count = 0;
25712ffbb224SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++) {
25722ffbb224SPrarit Bhargava 		lnode = 0;
25732ffbb224SPrarit Bhargava 		for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
25742ffbb224SPrarit Bhargava 			if (cpus[cpu].physical_package_id != pkg)
2575ef605741SPrarit Bhargava 				continue;
25762ffbb224SPrarit Bhargava 			/* find a cpu with an unset logical_node_id */
25772ffbb224SPrarit Bhargava 			if (cpus[cpu].logical_node_id != -1)
25782ffbb224SPrarit Bhargava 				continue;
25792ffbb224SPrarit Bhargava 			cpus[cpu].logical_node_id = lnode;
2580ef605741SPrarit Bhargava 			node = cpus[cpu].physical_node_id;
25812ffbb224SPrarit Bhargava 			cpu_count++;
25822ffbb224SPrarit Bhargava 			/*
25832ffbb224SPrarit Bhargava 			 * find all matching cpus on this pkg and set
25842ffbb224SPrarit Bhargava 			 * the logical_node_id
25852ffbb224SPrarit Bhargava 			 */
25862ffbb224SPrarit Bhargava 			for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
25872ffbb224SPrarit Bhargava 				if ((cpus[cpux].physical_package_id == pkg) &&
25882ffbb224SPrarit Bhargava 				   (cpus[cpux].physical_node_id == node)) {
25892ffbb224SPrarit Bhargava 					cpus[cpux].logical_node_id = lnode;
25902ffbb224SPrarit Bhargava 					cpu_count++;
2591ef605741SPrarit Bhargava 				}
25922ffbb224SPrarit Bhargava 			}
25932ffbb224SPrarit Bhargava 			lnode++;
25942ffbb224SPrarit Bhargava 			if (lnode > topo.nodes_per_pkg)
25952ffbb224SPrarit Bhargava 				topo.nodes_per_pkg = lnode;
25962ffbb224SPrarit Bhargava 		}
25972ffbb224SPrarit Bhargava 		if (cpu_count >= topo.max_cpu_num)
25982ffbb224SPrarit Bhargava 			break;
25992ffbb224SPrarit Bhargava 	}
2600ef605741SPrarit Bhargava }
2601ef605741SPrarit Bhargava 
2602ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2603c98d5d94SLen Brown {
2604c98d5d94SLen Brown 	char path[80];
2605c98d5d94SLen Brown 	FILE *filep;
26060e2d8f05SLen Brown 	int i;
26070e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2608c98d5d94SLen Brown 
26090e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
26100e2d8f05SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
26110e2d8f05SLen Brown 			cpu, i);
26120e2d8f05SLen Brown 		filep = fopen(path, "r");
26130e2d8f05SLen Brown 		if (!filep)
26140e2d8f05SLen Brown 			continue;
26150e2d8f05SLen Brown 		fclose(filep);
26160e2d8f05SLen Brown 		return i;
26170e2d8f05SLen Brown 	}
26180e2d8f05SLen Brown 	return -1;
2619e275b388SDasaratharaman Chandramouli }
2620c98d5d94SLen Brown 
26210e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
26220e2d8f05SLen Brown {
26230e2d8f05SLen Brown 	char path[80], character;
26240e2d8f05SLen Brown 	FILE *filep;
26250e2d8f05SLen Brown 	unsigned long map;
26268cb48b32SPrarit Bhargava 	int so, shift, sib_core;
26270e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
26280e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
26290e2d8f05SLen Brown 	size_t size;
26308cb48b32SPrarit Bhargava 	int thread_id = 0;
26310e2d8f05SLen Brown 
26320e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
26338cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
26348cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
26350e2d8f05SLen Brown 	if (!thiscpu->put_ids)
26360e2d8f05SLen Brown 		return -1;
26370e2d8f05SLen Brown 
26380e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
26390e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
26400e2d8f05SLen Brown 
26410e2d8f05SLen Brown 	sprintf(path,
26420e2d8f05SLen Brown 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
26430e2d8f05SLen Brown 	filep = fopen_or_die(path, "r");
26440e2d8f05SLen Brown 	do {
26450e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
26460e2d8f05SLen Brown 		fscanf(filep, "%lx%c", &map, &character);
26470e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
26480e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
26498cb48b32SPrarit Bhargava 				so = shift + offset;
26508cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
26518cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
26528cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
26538cb48b32SPrarit Bhargava 					if ((so != cpu) &&
26548cb48b32SPrarit Bhargava 					    (cpus[so].thread_id < 0))
26558cb48b32SPrarit Bhargava 						cpus[so].thread_id =
26568cb48b32SPrarit Bhargava 								    thread_id++;
26578cb48b32SPrarit Bhargava 				}
26580e2d8f05SLen Brown 			}
26590e2d8f05SLen Brown 		}
26600e2d8f05SLen Brown 	} while (!strncmp(&character, ",", 1));
2661c98d5d94SLen Brown 	fclose(filep);
26620e2d8f05SLen Brown 
26630e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2664c98d5d94SLen Brown }
2665c98d5d94SLen Brown 
2666c98d5d94SLen Brown /*
2667c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2668c98d5d94SLen Brown  * skip non-present cpus
2669103a8feaSLen Brown  */
2670103a8feaSLen Brown 
2671c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2672c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2673c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2674c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2675c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2676c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2677c98d5d94SLen Brown {
267840f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2679c98d5d94SLen Brown 
2680c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
268140f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
268240f5cfe7SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node;
268340f5cfe7SPrarit Bhargava 			     ++core_no) {
2684c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
268570a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
2686c98d5d94SLen Brown 					struct thread_data *t, *t2;
2687c98d5d94SLen Brown 					struct core_data *c, *c2;
2688c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2689c98d5d94SLen Brown 
269040f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
269140f5cfe7SPrarit Bhargava 						       core_no, node_no,
269240f5cfe7SPrarit Bhargava 						       pkg_no);
2693c98d5d94SLen Brown 
2694c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
2695c98d5d94SLen Brown 						continue;
2696c98d5d94SLen Brown 
269740f5cfe7SPrarit Bhargava 					t2 = GET_THREAD(thread_base2, thread_no,
269840f5cfe7SPrarit Bhargava 							core_no, node_no,
269940f5cfe7SPrarit Bhargava 							pkg_no);
2700c98d5d94SLen Brown 
270140f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
270240f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
270340f5cfe7SPrarit Bhargava 					c2 = GET_CORE(core_base2, core_no,
270440f5cfe7SPrarit Bhargava 						      node_no,
270540f5cfe7SPrarit Bhargava 						      pkg_no);
2706c98d5d94SLen Brown 
2707c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
2708c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
2709c98d5d94SLen Brown 
2710c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
2711c98d5d94SLen Brown 					if (retval)
2712c98d5d94SLen Brown 						return retval;
2713c98d5d94SLen Brown 				}
2714c98d5d94SLen Brown 			}
2715c98d5d94SLen Brown 		}
271640f5cfe7SPrarit Bhargava 	}
2717c98d5d94SLen Brown 	return 0;
2718c98d5d94SLen Brown }
2719c98d5d94SLen Brown 
2720c98d5d94SLen Brown /*
2721c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2722c98d5d94SLen Brown  * return max_cpu number
2723c98d5d94SLen Brown  */
2724c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2725103a8feaSLen Brown {
2726103a8feaSLen Brown 	FILE *fp;
2727c98d5d94SLen Brown 	int cpu_num;
2728103a8feaSLen Brown 	int retval;
2729103a8feaSLen Brown 
273057a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2731103a8feaSLen Brown 
2732103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2733b2c95d90SJosh Triplett 	if (retval != 0)
2734b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2735103a8feaSLen Brown 
2736c98d5d94SLen Brown 	while (1) {
2737c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2738103a8feaSLen Brown 		if (retval != 1)
2739103a8feaSLen Brown 			break;
2740103a8feaSLen Brown 
2741c98d5d94SLen Brown 		retval = func(cpu_num);
2742c98d5d94SLen Brown 		if (retval) {
2743c98d5d94SLen Brown 			fclose(fp);
2744c98d5d94SLen Brown 			return(retval);
2745c98d5d94SLen Brown 		}
2746103a8feaSLen Brown 	}
2747103a8feaSLen Brown 	fclose(fp);
2748c98d5d94SLen Brown 	return 0;
2749103a8feaSLen Brown }
2750103a8feaSLen Brown 
2751103a8feaSLen Brown void re_initialize(void)
2752103a8feaSLen Brown {
2753c98d5d94SLen Brown 	free_all_buffers();
2754c98d5d94SLen Brown 	setup_all_buffers();
2755c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2756103a8feaSLen Brown }
2757103a8feaSLen Brown 
2758843c5791SPrarit Bhargava void set_max_cpu_num(void)
2759843c5791SPrarit Bhargava {
2760843c5791SPrarit Bhargava 	FILE *filep;
2761843c5791SPrarit Bhargava 	unsigned long dummy;
2762843c5791SPrarit Bhargava 
2763843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
2764843c5791SPrarit Bhargava 	filep = fopen_or_die(
2765843c5791SPrarit Bhargava 			"/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2766843c5791SPrarit Bhargava 			"r");
2767843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
27680e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
2769843c5791SPrarit Bhargava 	fclose(filep);
2770843c5791SPrarit Bhargava 	topo.max_cpu_num--; /* 0 based */
2771843c5791SPrarit Bhargava }
2772c98d5d94SLen Brown 
2773103a8feaSLen Brown /*
2774c98d5d94SLen Brown  * count_cpus()
2775c98d5d94SLen Brown  * remember the last one seen, it will be the max
2776103a8feaSLen Brown  */
2777c98d5d94SLen Brown int count_cpus(int cpu)
2778103a8feaSLen Brown {
2779843c5791SPrarit Bhargava 	topo.num_cpus++;
2780c98d5d94SLen Brown 	return 0;
2781103a8feaSLen Brown }
2782c98d5d94SLen Brown int mark_cpu_present(int cpu)
2783c98d5d94SLen Brown {
2784c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
278515aaa346SLen Brown 	return 0;
2786103a8feaSLen Brown }
2787103a8feaSLen Brown 
27888cb48b32SPrarit Bhargava int init_thread_id(int cpu)
27898cb48b32SPrarit Bhargava {
27908cb48b32SPrarit Bhargava 	cpus[cpu].thread_id = -1;
27918cb48b32SPrarit Bhargava 	return 0;
27928cb48b32SPrarit Bhargava }
27938cb48b32SPrarit Bhargava 
2794562a2d37SLen Brown /*
2795562a2d37SLen Brown  * snapshot_proc_interrupts()
2796562a2d37SLen Brown  *
2797562a2d37SLen Brown  * read and record summary of /proc/interrupts
2798562a2d37SLen Brown  *
2799562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2800562a2d37SLen Brown  */
2801562a2d37SLen Brown int snapshot_proc_interrupts(void)
2802562a2d37SLen Brown {
2803562a2d37SLen Brown 	static FILE *fp;
2804562a2d37SLen Brown 	int column, retval;
2805562a2d37SLen Brown 
2806562a2d37SLen Brown 	if (fp == NULL)
2807562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2808562a2d37SLen Brown 	else
2809562a2d37SLen Brown 		rewind(fp);
2810562a2d37SLen Brown 
2811562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2812562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2813562a2d37SLen Brown 		int cpu_number;
2814562a2d37SLen Brown 
2815562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2816562a2d37SLen Brown 		if (retval != 1)
2817562a2d37SLen Brown 			break;
2818562a2d37SLen Brown 
2819562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2820562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2821562a2d37SLen Brown 			return 1;
2822562a2d37SLen Brown 		}
2823562a2d37SLen Brown 
2824562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2825562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2826562a2d37SLen Brown 	}
2827562a2d37SLen Brown 
2828562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2829562a2d37SLen Brown 	while (1) {
2830562a2d37SLen Brown 		int column;
2831562a2d37SLen Brown 		char buf[64];
2832562a2d37SLen Brown 
2833562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2834562a2d37SLen Brown 		if (retval != 1)
2835562a2d37SLen Brown 			break;
2836562a2d37SLen Brown 
2837562a2d37SLen Brown 		/* read the count per cpu */
2838562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2839562a2d37SLen Brown 
2840562a2d37SLen Brown 			int cpu_number, irq_count;
2841562a2d37SLen Brown 
2842562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2843562a2d37SLen Brown 			if (retval != 1)
2844562a2d37SLen Brown 				break;
2845562a2d37SLen Brown 
2846562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2847562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2848562a2d37SLen Brown 
2849562a2d37SLen Brown 		}
2850562a2d37SLen Brown 
2851562a2d37SLen Brown 		while (getc(fp) != '\n')
2852562a2d37SLen Brown 			;	/* flush interrupt description */
2853562a2d37SLen Brown 
2854562a2d37SLen Brown 	}
2855562a2d37SLen Brown 	return 0;
2856562a2d37SLen Brown }
285727d47356SLen Brown /*
2858fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2859fdf676e5SLen Brown  *
2860fdf676e5SLen Brown  * record snapshot of
2861fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2862fdf676e5SLen Brown  *
2863fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2864fdf676e5SLen Brown  */
2865fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2866fdf676e5SLen Brown {
2867fdf676e5SLen Brown 	FILE *fp;
2868fdf676e5SLen Brown 	int retval;
2869fdf676e5SLen Brown 
2870fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2871fdf676e5SLen Brown 
2872fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2873fdf676e5SLen Brown 	if (retval != 1)
2874fdf676e5SLen Brown 		err(1, "GFX rc6");
2875fdf676e5SLen Brown 
2876fdf676e5SLen Brown 	fclose(fp);
2877fdf676e5SLen Brown 
2878fdf676e5SLen Brown 	return 0;
2879fdf676e5SLen Brown }
2880fdf676e5SLen Brown /*
288127d47356SLen Brown  * snapshot_gfx_mhz()
288227d47356SLen Brown  *
288327d47356SLen Brown  * record snapshot of
288427d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
288527d47356SLen Brown  *
288627d47356SLen Brown  * return 1 if config change requires a restart, else return 0
288727d47356SLen Brown  */
288827d47356SLen Brown int snapshot_gfx_mhz(void)
288927d47356SLen Brown {
289027d47356SLen Brown 	static FILE *fp;
289127d47356SLen Brown 	int retval;
289227d47356SLen Brown 
289327d47356SLen Brown 	if (fp == NULL)
289427d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
289522048c54SLen Brown 	else {
289627d47356SLen Brown 		rewind(fp);
289722048c54SLen Brown 		fflush(fp);
289822048c54SLen Brown 	}
289927d47356SLen Brown 
290027d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
290127d47356SLen Brown 	if (retval != 1)
290227d47356SLen Brown 		err(1, "GFX MHz");
290327d47356SLen Brown 
290427d47356SLen Brown 	return 0;
290527d47356SLen Brown }
2906562a2d37SLen Brown 
2907562a2d37SLen Brown /*
2908be0e54c4SLen Brown  * snapshot_cpu_lpi()
2909be0e54c4SLen Brown  *
2910be0e54c4SLen Brown  * record snapshot of
2911be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2912be0e54c4SLen Brown  *
2913be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2914be0e54c4SLen Brown  */
2915be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
2916be0e54c4SLen Brown {
2917be0e54c4SLen Brown 	FILE *fp;
2918be0e54c4SLen Brown 	int retval;
2919be0e54c4SLen Brown 
2920be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2921be0e54c4SLen Brown 
2922be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2923be0e54c4SLen Brown 	if (retval != 1)
2924be0e54c4SLen Brown 		err(1, "CPU LPI");
2925be0e54c4SLen Brown 
2926be0e54c4SLen Brown 	fclose(fp);
2927be0e54c4SLen Brown 
2928be0e54c4SLen Brown 	return 0;
2929be0e54c4SLen Brown }
2930be0e54c4SLen Brown /*
2931be0e54c4SLen Brown  * snapshot_sys_lpi()
2932be0e54c4SLen Brown  *
2933be0e54c4SLen Brown  * record snapshot of
2934be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2935be0e54c4SLen Brown  *
2936be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2937be0e54c4SLen Brown  */
2938be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
2939be0e54c4SLen Brown {
2940be0e54c4SLen Brown 	FILE *fp;
2941be0e54c4SLen Brown 	int retval;
2942be0e54c4SLen Brown 
2943be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2944be0e54c4SLen Brown 
2945be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2946be0e54c4SLen Brown 	if (retval != 1)
2947be0e54c4SLen Brown 		err(1, "SYS LPI");
2948be0e54c4SLen Brown 
2949be0e54c4SLen Brown 	fclose(fp);
2950be0e54c4SLen Brown 
2951be0e54c4SLen Brown 	return 0;
2952be0e54c4SLen Brown }
2953be0e54c4SLen Brown /*
2954562a2d37SLen Brown  * snapshot /proc and /sys files
2955562a2d37SLen Brown  *
2956562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2957562a2d37SLen Brown  */
2958562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2959562a2d37SLen Brown {
2960218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2961562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2962562a2d37SLen Brown 			return 1;
2963562a2d37SLen Brown 
2964812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2965fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2966fdf676e5SLen Brown 
2967812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
296827d47356SLen Brown 		snapshot_gfx_mhz();
296927d47356SLen Brown 
2970be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2971be0e54c4SLen Brown 		snapshot_cpu_lpi_us();
2972be0e54c4SLen Brown 
2973be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2974be0e54c4SLen Brown 		snapshot_sys_lpi_us();
2975be0e54c4SLen Brown 
2976562a2d37SLen Brown 	return 0;
2977562a2d37SLen Brown }
2978562a2d37SLen Brown 
29798aa2ed0bSLen Brown int exit_requested;
29808aa2ed0bSLen Brown 
29818aa2ed0bSLen Brown static void signal_handler (int signal)
29828aa2ed0bSLen Brown {
29838aa2ed0bSLen Brown 	switch (signal) {
29848aa2ed0bSLen Brown 	case SIGINT:
29858aa2ed0bSLen Brown 		exit_requested = 1;
29868aa2ed0bSLen Brown 		if (debug)
29878aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
29888aa2ed0bSLen Brown 		break;
298907211960SLen Brown 	case SIGUSR1:
299007211960SLen Brown 		if (debug > 1)
299107211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
299207211960SLen Brown 		break;
29938aa2ed0bSLen Brown 	}
2994b9ad8ee0SLen Brown 	/* make sure this manually-invoked interval is at least 1ms long */
2995b9ad8ee0SLen Brown 	nanosleep(&one_msec, NULL);
29968aa2ed0bSLen Brown }
29978aa2ed0bSLen Brown 
29988aa2ed0bSLen Brown void setup_signal_handler(void)
29998aa2ed0bSLen Brown {
30008aa2ed0bSLen Brown 	struct sigaction sa;
30018aa2ed0bSLen Brown 
30028aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
30038aa2ed0bSLen Brown 
30048aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
30058aa2ed0bSLen Brown 
30068aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
30078aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
300807211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
300907211960SLen Brown 		err(1, "sigaction SIGUSR1");
30108aa2ed0bSLen Brown }
3011b9ad8ee0SLen Brown 
301247936f94SArtem Bityutskiy void do_sleep(void)
3013b9ad8ee0SLen Brown {
3014b9ad8ee0SLen Brown 	struct timeval select_timeout;
3015b9ad8ee0SLen Brown 	fd_set readfds;
3016b9ad8ee0SLen Brown 	int retval;
3017b9ad8ee0SLen Brown 
3018b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
3019b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
3020b9ad8ee0SLen Brown 
302147936f94SArtem Bityutskiy 	if (!isatty(fileno(stdin))) {
302247936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
302347936f94SArtem Bityutskiy 		return;
302447936f94SArtem Bityutskiy 	}
3025b9ad8ee0SLen Brown 
302647936f94SArtem Bityutskiy 	select_timeout = interval_tv;
3027b9ad8ee0SLen Brown 	retval = select(1, &readfds, NULL, NULL, &select_timeout);
3028b9ad8ee0SLen Brown 
3029b9ad8ee0SLen Brown 	if (retval == 1) {
3030b9ad8ee0SLen Brown 		switch (getc(stdin)) {
3031b9ad8ee0SLen Brown 		case 'q':
3032b9ad8ee0SLen Brown 			exit_requested = 1;
3033b9ad8ee0SLen Brown 			break;
3034b9ad8ee0SLen Brown 		}
3035b9ad8ee0SLen Brown 		/* make sure this manually-invoked interval is at least 1ms long */
3036b9ad8ee0SLen Brown 		nanosleep(&one_msec, NULL);
3037b9ad8ee0SLen Brown 	}
3038b9ad8ee0SLen Brown }
303947936f94SArtem Bityutskiy 
30404c2122d4SLen Brown 
3041103a8feaSLen Brown void turbostat_loop()
3042103a8feaSLen Brown {
3043c98d5d94SLen Brown 	int retval;
3044e52966c0SLen Brown 	int restarted = 0;
3045023fe0acSChen Yu 	int done_iters = 0;
3046c98d5d94SLen Brown 
30478aa2ed0bSLen Brown 	setup_signal_handler();
30488aa2ed0bSLen Brown 
3049103a8feaSLen Brown restart:
3050e52966c0SLen Brown 	restarted++;
3051e52966c0SLen Brown 
3052562a2d37SLen Brown 	snapshot_proc_sysfs_files();
3053c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
30544c2122d4SLen Brown 	first_counter_read = 0;
3055d91bb17cSLen Brown 	if (retval < -1) {
3056d91bb17cSLen Brown 		exit(retval);
3057d91bb17cSLen Brown 	} else if (retval == -1) {
3058e52966c0SLen Brown 		if (restarted > 1) {
3059e52966c0SLen Brown 			exit(retval);
3060e52966c0SLen Brown 		}
3061c98d5d94SLen Brown 		re_initialize();
3062c98d5d94SLen Brown 		goto restart;
3063c98d5d94SLen Brown 	}
3064e52966c0SLen Brown 	restarted = 0;
3065023fe0acSChen Yu 	done_iters = 0;
3066103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3067103a8feaSLen Brown 
3068103a8feaSLen Brown 	while (1) {
3069c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
3070103a8feaSLen Brown 			re_initialize();
3071103a8feaSLen Brown 			goto restart;
3072103a8feaSLen Brown 		}
3073b9ad8ee0SLen Brown 		do_sleep();
3074562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3075562a2d37SLen Brown 			goto restart;
3076c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
3077d91bb17cSLen Brown 		if (retval < -1) {
3078d91bb17cSLen Brown 			exit(retval);
3079d91bb17cSLen Brown 		} else if (retval == -1) {
308015aaa346SLen Brown 			re_initialize();
308115aaa346SLen Brown 			goto restart;
308215aaa346SLen Brown 		}
3083103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
3084103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
3085ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3086ba3dec99SLen Brown 			re_initialize();
3087ba3dec99SLen Brown 			goto restart;
3088ba3dec99SLen Brown 		}
3089c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3090c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3091b7d8c148SLen Brown 		flush_output_stdout();
30928aa2ed0bSLen Brown 		if (exit_requested)
30938aa2ed0bSLen Brown 			break;
3094023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3095023fe0acSChen Yu 			break;
3096b9ad8ee0SLen Brown 		do_sleep();
3097562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3098562a2d37SLen Brown 			goto restart;
3099c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3100d91bb17cSLen Brown 		if (retval < -1) {
3101d91bb17cSLen Brown 			exit(retval);
3102d91bb17cSLen Brown 		} else if (retval == -1) {
3103103a8feaSLen Brown 			re_initialize();
3104103a8feaSLen Brown 			goto restart;
3105103a8feaSLen Brown 		}
3106103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3107103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3108ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3109ba3dec99SLen Brown 			re_initialize();
3110ba3dec99SLen Brown 			goto restart;
3111ba3dec99SLen Brown 		}
3112c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3113c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3114b7d8c148SLen Brown 		flush_output_stdout();
31158aa2ed0bSLen Brown 		if (exit_requested)
31168aa2ed0bSLen Brown 			break;
3117023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3118023fe0acSChen Yu 			break;
3119103a8feaSLen Brown 	}
3120103a8feaSLen Brown }
3121103a8feaSLen Brown 
3122103a8feaSLen Brown void check_dev_msr()
3123103a8feaSLen Brown {
3124103a8feaSLen Brown 	struct stat sb;
31257ce7d5deSPrarit Bhargava 	char pathname[32];
3126103a8feaSLen Brown 
31277ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
31287ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3129a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3130d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3131103a8feaSLen Brown }
3132103a8feaSLen Brown 
313398481e79SLen Brown void check_permissions()
3134103a8feaSLen Brown {
313598481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
313698481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
313798481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
313898481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
313998481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
314098481e79SLen Brown 	int do_exit = 0;
31417ce7d5deSPrarit Bhargava 	char pathname[32];
314298481e79SLen Brown 
314398481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
314498481e79SLen Brown 	cap_header->pid = getpid();
314598481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
314698481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
314798481e79SLen Brown 		err(-6, "capget(2) failed");
314898481e79SLen Brown 
314998481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
315098481e79SLen Brown 		do_exit++;
315198481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
315298481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
315398481e79SLen Brown 	}
315498481e79SLen Brown 
315598481e79SLen Brown 	/* test file permissions */
31567ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
31577ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
315898481e79SLen Brown 		do_exit++;
315998481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
316098481e79SLen Brown 	}
316198481e79SLen Brown 
316298481e79SLen Brown 	/* if all else fails, thell them to be root */
316398481e79SLen Brown 	if (do_exit)
3164b2c95d90SJosh Triplett 		if (getuid() != 0)
3165d7899447SLen Brown 			warnx("... or simply run as root");
316698481e79SLen Brown 
316798481e79SLen Brown 	if (do_exit)
316898481e79SLen Brown 		exit(-6);
3169103a8feaSLen Brown }
3170103a8feaSLen Brown 
3171d7899447SLen Brown /*
3172d7899447SLen Brown  * NHM adds support for additional MSRs:
3173d7899447SLen Brown  *
3174d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3175d7899447SLen Brown  *
3176ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
31771df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3178d7899447SLen Brown  *
3179cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3180cf4cbe53SLen Brown  *
3181d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3182d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3183d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3184d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3185d7899447SLen Brown  *
3186ee7e38e3SLen Brown  * Side effect:
31871df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
318833148d67SLen Brown  * sets has_misc_feature_control
3189d7899447SLen Brown  */
3190ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3191103a8feaSLen Brown {
3192ee7e38e3SLen Brown 	unsigned long long msr;
319321ed5574SLen Brown 	unsigned int base_ratio;
3194ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3195ee7e38e3SLen Brown 
3196103a8feaSLen Brown 	if (!genuine_intel)
3197103a8feaSLen Brown 		return 0;
3198103a8feaSLen Brown 
3199103a8feaSLen Brown 	if (family != 6)
3200103a8feaSLen Brown 		return 0;
3201103a8feaSLen Brown 
320221ed5574SLen Brown 	bclk = discover_bclk(family, model);
320321ed5574SLen Brown 
3204103a8feaSLen Brown 	switch (model) {
3205869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3206869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3207ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3208ee7e38e3SLen Brown 		break;
3209869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3210869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3211869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3212869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3213ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
321433148d67SLen Brown 		has_misc_feature_control = 1;
3215ee7e38e3SLen Brown 		break;
3216869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3217869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3218869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3219869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3220869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3221869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3222869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3223997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3224ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
322533148d67SLen Brown 		has_misc_feature_control = 1;
3226ee7e38e3SLen Brown 		break;
3227d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3228d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
322933148d67SLen Brown 		has_misc_feature_control = 1;
3230d8ebb442SLen Brown 		break;
3231f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3232cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3233f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
3234ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3235ee7e38e3SLen Brown 		break;
3236869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3237ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3238cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3239ee7e38e3SLen Brown 		break;
3240869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3241ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3242ee7e38e3SLen Brown 		break;
3243869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3244f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3245f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
3246445640a5SLen Brown 		pkg_cstate_limits = glm_pkg_cstate_limits;
3247e4085d54SLen Brown 		break;
3248103a8feaSLen Brown 	default:
3249103a8feaSLen Brown 		return 0;
3250103a8feaSLen Brown 	}
32511df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3252e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3253ee7e38e3SLen Brown 
3254ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
325521ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
325621ed5574SLen Brown 
325721ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
325821ed5574SLen Brown 	has_base_hz = 1;
3259ee7e38e3SLen Brown 	return 1;
3260103a8feaSLen Brown }
32610f7887c4SLen Brown /*
3262495c7654SLen Brown  * SLV client has support for unique MSRs:
32630f7887c4SLen Brown  *
32640f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
32650f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
32660f7887c4SLen Brown  */
32670f7887c4SLen Brown 
32680f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
32690f7887c4SLen Brown {
32700f7887c4SLen Brown 	if (!genuine_intel)
32710f7887c4SLen Brown 		return 0;
32720f7887c4SLen Brown 
32730f7887c4SLen Brown 	switch (model) {
3274f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3275f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_MID:
3276f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_AIRMONT_MID:
32770f7887c4SLen Brown 		return 1;
32780f7887c4SLen Brown 	}
32790f7887c4SLen Brown 	return 0;
32800f7887c4SLen Brown }
32817170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
32827170a374SLen Brown {
32837170a374SLen Brown 
32847170a374SLen Brown 	if (!genuine_intel)
32857170a374SLen Brown 		return 0;
32867170a374SLen Brown 
32877170a374SLen Brown 	switch (model) {
3288f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:
32897170a374SLen Brown 		return 1;
32907170a374SLen Brown 	}
32917170a374SLen Brown 	return 0;
32927170a374SLen Brown }
3293ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3294ade0ebacSLen Brown {
3295ade0ebacSLen Brown 
3296ade0ebacSLen Brown 	if (!genuine_intel)
3297ade0ebacSLen Brown 		return 0;
3298ade0ebacSLen Brown 
3299ade0ebacSLen Brown 	switch (model) {
3300ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3301ade0ebacSLen Brown 		return 1;
3302ade0ebacSLen Brown 	}
3303ade0ebacSLen Brown 	return 0;
3304ade0ebacSLen Brown }
330534c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
330634c76197SLen Brown {
330734c76197SLen Brown 
330834c76197SLen Brown 	if (!genuine_intel)
330934c76197SLen Brown 		return 0;
331034c76197SLen Brown 
331134c76197SLen Brown 	switch (model) {
331234c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
331334c76197SLen Brown 		return 1;
331434c76197SLen Brown 	}
331534c76197SLen Brown 	return 0;
331634c76197SLen Brown }
33170f7887c4SLen Brown 
331831e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3319d7899447SLen Brown {
33200f7887c4SLen Brown 	if (has_slv_msrs(family, model))
33210f7887c4SLen Brown 		return 0;
33220f7887c4SLen Brown 
3323d7899447SLen Brown 	switch (model) {
3324d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
3325869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3326869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3327d7899447SLen Brown 		return 0;
3328d7899447SLen Brown 	default:
3329d7899447SLen Brown 		return 1;
3330d7899447SLen Brown 	}
3331d7899447SLen Brown }
33320f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
33330f7887c4SLen Brown {
33340f7887c4SLen Brown 	if (has_slv_msrs(family, model))
33350f7887c4SLen Brown 		return 1;
33360f7887c4SLen Brown 
33370f7887c4SLen Brown 	return 0;
33380f7887c4SLen Brown }
33396574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
33406574a5d5SLen Brown {
33416574a5d5SLen Brown 	if (!genuine_intel)
33426574a5d5SLen Brown 		return 0;
33436574a5d5SLen Brown 
33446574a5d5SLen Brown 	if (family != 6)
33456574a5d5SLen Brown 		return 0;
33466574a5d5SLen Brown 
33476574a5d5SLen Brown 	switch (model) {
3348869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3349869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
33506574a5d5SLen Brown 		return 1;
33516574a5d5SLen Brown 	default:
33526574a5d5SLen Brown 		return 0;
33536574a5d5SLen Brown 	}
33546574a5d5SLen Brown }
3355fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3356fcd17211SLen Brown {
3357fcd17211SLen Brown 	if (!genuine_intel)
3358fcd17211SLen Brown 		return 0;
3359fcd17211SLen Brown 
3360fcd17211SLen Brown 	if (family != 6)
3361fcd17211SLen Brown 		return 0;
3362fcd17211SLen Brown 
3363fcd17211SLen Brown 	switch (model) {
3364869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3365fcd17211SLen Brown 		return 1;
3366fcd17211SLen Brown 	default:
3367fcd17211SLen Brown 		return 0;
3368fcd17211SLen Brown 	}
3369fcd17211SLen Brown }
3370fcd17211SLen Brown 
3371fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3372fb5d4327SDasaratharaman Chandramouli {
3373fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3374fb5d4327SDasaratharaman Chandramouli 		return 0;
3375fb5d4327SDasaratharaman Chandramouli 
3376fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3377fb5d4327SDasaratharaman Chandramouli 		return 0;
3378fb5d4327SDasaratharaman Chandramouli 
3379fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3380869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3381fb5d4327SDasaratharaman Chandramouli 		return 1;
3382fb5d4327SDasaratharaman Chandramouli 	default:
3383fb5d4327SDasaratharaman Chandramouli 		return 0;
3384fb5d4327SDasaratharaman Chandramouli 	}
3385fb5d4327SDasaratharaman Chandramouli }
338631e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
338731e07522SLen Brown {
338831e07522SLen Brown 	if (!genuine_intel)
338931e07522SLen Brown 		return 0;
339031e07522SLen Brown 
339131e07522SLen Brown 	if (family != 6)
339231e07522SLen Brown 		return 0;
339331e07522SLen Brown 
339431e07522SLen Brown 	switch (model) {
339531e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
339631e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
339731e07522SLen Brown 		return 1;
339831e07522SLen Brown 	default:
339931e07522SLen Brown 		return 0;
340031e07522SLen Brown 	}
340131e07522SLen Brown }
34026fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
34036fb3143bSLen Brown {
34046fb3143bSLen Brown 	if (!genuine_intel)
34056fb3143bSLen Brown 		return 0;
34066fb3143bSLen Brown 
34076fb3143bSLen Brown 	if (family != 6)
34086fb3143bSLen Brown 		return 0;
34096fb3143bSLen Brown 
34106fb3143bSLen Brown 	switch (model) {
3411869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3412869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3413869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3414869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3415869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3416869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3417869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3418869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3419997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3420869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
34216fb3143bSLen Brown 
3422869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
34236fb3143bSLen Brown 		return 1;
34246fb3143bSLen Brown 	default:
34256fb3143bSLen Brown 		return 0;
34266fb3143bSLen Brown 	}
34276fb3143bSLen Brown }
34286fb3143bSLen Brown 
3429fcd17211SLen Brown static void
34301b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3431fcd17211SLen Brown {
3432fcd17211SLen Brown 	if (!do_nhm_platform_info)
3433fcd17211SLen Brown 		return;
3434fcd17211SLen Brown 
3435fcd17211SLen Brown 	dump_nhm_platform_info();
3436fcd17211SLen Brown 
3437fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
3438fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
3439fcd17211SLen Brown 
3440fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
3441fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
3442fcd17211SLen Brown 
344331e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
344431e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
3445fcd17211SLen Brown 
34460f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
34470f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
34480f7887c4SLen Brown 
3449fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
3450fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
3451fb5d4327SDasaratharaman Chandramouli 
34526fb3143bSLen Brown 	if (has_config_tdp(family, model))
34536fb3143bSLen Brown 		dump_config_tdp();
34546fb3143bSLen Brown 
3455fcd17211SLen Brown 	dump_nhm_cst_cfg();
3456fcd17211SLen Brown }
3457fcd17211SLen Brown 
345841618e63SLen Brown static void
345941618e63SLen Brown dump_sysfs_cstate_config(void)
346041618e63SLen Brown {
346141618e63SLen Brown 	char path[64];
346241618e63SLen Brown 	char name_buf[16];
346341618e63SLen Brown 	char desc[64];
346441618e63SLen Brown 	FILE *input;
346541618e63SLen Brown 	int state;
346641618e63SLen Brown 	char *sp;
346741618e63SLen Brown 
346841618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
346941618e63SLen Brown 		return;
347041618e63SLen Brown 
347141618e63SLen Brown 	for (state = 0; state < 10; ++state) {
347241618e63SLen Brown 
347341618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
347441618e63SLen Brown 			base_cpu, state);
347541618e63SLen Brown 		input = fopen(path, "r");
347641618e63SLen Brown 		if (input == NULL)
347741618e63SLen Brown 			continue;
347841618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
347941618e63SLen Brown 
348041618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
348141618e63SLen Brown 		sp = strchr(name_buf, '-');
348241618e63SLen Brown 		if (!sp)
348341618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
348441618e63SLen Brown 		*sp = '\0';
348541618e63SLen Brown 
348641618e63SLen Brown 		fclose(input);
348741618e63SLen Brown 
348841618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
348941618e63SLen Brown 			base_cpu, state);
349041618e63SLen Brown 		input = fopen(path, "r");
349141618e63SLen Brown 		if (input == NULL)
349241618e63SLen Brown 			continue;
349341618e63SLen Brown 		fgets(desc, sizeof(desc), input);
349441618e63SLen Brown 
349541618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
349641618e63SLen Brown 		fclose(input);
349741618e63SLen Brown 	}
349841618e63SLen Brown }
34997293fccdSLen Brown static void
35007293fccdSLen Brown dump_sysfs_pstate_config(void)
35017293fccdSLen Brown {
35027293fccdSLen Brown 	char path[64];
35037293fccdSLen Brown 	char driver_buf[64];
35047293fccdSLen Brown 	char governor_buf[64];
35057293fccdSLen Brown 	FILE *input;
35067293fccdSLen Brown 	int turbo;
35077293fccdSLen Brown 
35087293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
35097293fccdSLen Brown 			base_cpu);
35107293fccdSLen Brown 	input = fopen(path, "r");
35117293fccdSLen Brown 	if (input == NULL) {
35120a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
35137293fccdSLen Brown 		return;
35147293fccdSLen Brown 	}
35157293fccdSLen Brown 	fgets(driver_buf, sizeof(driver_buf), input);
35167293fccdSLen Brown 	fclose(input);
35177293fccdSLen Brown 
35187293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
35197293fccdSLen Brown 			base_cpu);
35207293fccdSLen Brown 	input = fopen(path, "r");
35217293fccdSLen Brown 	if (input == NULL) {
35220a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
35237293fccdSLen Brown 		return;
35247293fccdSLen Brown 	}
35257293fccdSLen Brown 	fgets(governor_buf, sizeof(governor_buf), input);
35267293fccdSLen Brown 	fclose(input);
35277293fccdSLen Brown 
35287293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
35297293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
35307293fccdSLen Brown 
35317293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
35327293fccdSLen Brown 	input = fopen(path, "r");
35337293fccdSLen Brown 	if (input != NULL) {
35347293fccdSLen Brown 		fscanf(input, "%d", &turbo);
35357293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
35367293fccdSLen Brown 		fclose(input);
35377293fccdSLen Brown 	}
35387293fccdSLen Brown 
35397293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
35407293fccdSLen Brown 	input = fopen(path, "r");
35417293fccdSLen Brown 	if (input != NULL) {
35427293fccdSLen Brown 		fscanf(input, "%d", &turbo);
35437293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
35447293fccdSLen Brown 		fclose(input);
35457293fccdSLen Brown 	}
35467293fccdSLen Brown }
354741618e63SLen Brown 
35486574a5d5SLen Brown 
3549889facbeSLen Brown /*
3550889facbeSLen Brown  * print_epb()
3551889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3552889facbeSLen Brown  */
3553889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3554889facbeSLen Brown {
3555889facbeSLen Brown 	unsigned long long msr;
3556889facbeSLen Brown 	char *epb_string;
3557889facbeSLen Brown 	int cpu;
3558889facbeSLen Brown 
3559889facbeSLen Brown 	if (!has_epb)
3560889facbeSLen Brown 		return 0;
3561889facbeSLen Brown 
3562889facbeSLen Brown 	cpu = t->cpu_id;
3563889facbeSLen Brown 
3564889facbeSLen Brown 	/* EPB is per-package */
3565889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3566889facbeSLen Brown 		return 0;
3567889facbeSLen Brown 
3568889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3569b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3570889facbeSLen Brown 		return -1;
3571889facbeSLen Brown 	}
3572889facbeSLen Brown 
3573889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3574889facbeSLen Brown 		return 0;
3575889facbeSLen Brown 
3576e9be7dd6SLen Brown 	switch (msr & 0xF) {
3577889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3578889facbeSLen Brown 		epb_string = "performance";
3579889facbeSLen Brown 		break;
3580889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3581889facbeSLen Brown 		epb_string = "balanced";
3582889facbeSLen Brown 		break;
3583889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3584889facbeSLen Brown 		epb_string = "powersave";
3585889facbeSLen Brown 		break;
3586889facbeSLen Brown 	default:
3587889facbeSLen Brown 		epb_string = "custom";
3588889facbeSLen Brown 		break;
3589889facbeSLen Brown 	}
3590b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3591889facbeSLen Brown 
3592889facbeSLen Brown 	return 0;
3593889facbeSLen Brown }
35947f5c258eSLen Brown /*
35957f5c258eSLen Brown  * print_hwp()
35967f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
35977f5c258eSLen Brown  */
35987f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
35997f5c258eSLen Brown {
36007f5c258eSLen Brown 	unsigned long long msr;
36017f5c258eSLen Brown 	int cpu;
36027f5c258eSLen Brown 
36037f5c258eSLen Brown 	if (!has_hwp)
36047f5c258eSLen Brown 		return 0;
36057f5c258eSLen Brown 
36067f5c258eSLen Brown 	cpu = t->cpu_id;
36077f5c258eSLen Brown 
36087f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
36097f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36107f5c258eSLen Brown 		return 0;
36117f5c258eSLen Brown 
36127f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3613b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
36147f5c258eSLen Brown 		return -1;
36157f5c258eSLen Brown 	}
36167f5c258eSLen Brown 
36177f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
36187f5c258eSLen Brown 		return 0;
36197f5c258eSLen Brown 
3620b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
36217f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
36227f5c258eSLen Brown 
36237f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
36247f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
36257f5c258eSLen Brown 		return 0;
36267f5c258eSLen Brown 
36277f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
36287f5c258eSLen Brown 		return 0;
36297f5c258eSLen Brown 
3630b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
36316dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
36327f5c258eSLen Brown 			cpu, msr,
36337f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
36347f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
36357f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
36367f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
36377f5c258eSLen Brown 
36387f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
36397f5c258eSLen Brown 		return 0;
36407f5c258eSLen Brown 
3641b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
36426dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
36437f5c258eSLen Brown 			cpu, msr,
36447f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
36457f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
36467f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
36477f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
36487f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
36497f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
36507f5c258eSLen Brown 
36517f5c258eSLen Brown 	if (has_hwp_pkg) {
36527f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
36537f5c258eSLen Brown 			return 0;
36547f5c258eSLen Brown 
3655b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
36566dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
36577f5c258eSLen Brown 			cpu, msr,
36587f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
36597f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
36607f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
36617f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
36627f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
36637f5c258eSLen Brown 	}
36647f5c258eSLen Brown 	if (has_hwp_notify) {
36657f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
36667f5c258eSLen Brown 			return 0;
36677f5c258eSLen Brown 
3668b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
36697f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
36707f5c258eSLen Brown 			cpu, msr,
36717f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
36727f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
36737f5c258eSLen Brown 	}
36747f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
36757f5c258eSLen Brown 		return 0;
36767f5c258eSLen Brown 
3677b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
36787f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
36797f5c258eSLen Brown 			cpu, msr,
36807f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
36817f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3682889facbeSLen Brown 
3683889facbeSLen Brown 	return 0;
3684889facbeSLen Brown }
3685889facbeSLen Brown 
36863a9a941dSLen Brown /*
36873a9a941dSLen Brown  * print_perf_limit()
36883a9a941dSLen Brown  */
36893a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
36903a9a941dSLen Brown {
36913a9a941dSLen Brown 	unsigned long long msr;
36923a9a941dSLen Brown 	int cpu;
36933a9a941dSLen Brown 
36943a9a941dSLen Brown 	cpu = t->cpu_id;
36953a9a941dSLen Brown 
36963a9a941dSLen Brown 	/* per-package */
36973a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36983a9a941dSLen Brown 		return 0;
36993a9a941dSLen Brown 
37003a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3701b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
37023a9a941dSLen Brown 		return -1;
37033a9a941dSLen Brown 	}
37043a9a941dSLen Brown 
37053a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
37063a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3707b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3708b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3709e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
37103a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3711e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3712e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3713e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3714e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3715e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3716e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3717e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3718e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3719e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3720e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3721e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3722e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3723b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3724e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
37253a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3726e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3727e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3728e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3729e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3730e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3731e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3732e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3733e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3734e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3735e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3736e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3737e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
37383a9a941dSLen Brown 
37393a9a941dSLen Brown 	}
37403a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
37413a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3742b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3743b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
37443a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
37453a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
37463a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
37473a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
37483a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
37493a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
37503a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
37513a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3752b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
37533a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
37543a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
37553a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
37563a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
37573a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37583a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
37593a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37603a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37613a9a941dSLen Brown 	}
37623a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
37633a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3764b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3765b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
37663a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
37673a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
37683a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
37693a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
37703a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
37713a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3772b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
37733a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
37743a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
37753a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
37763a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37773a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37783a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37793a9a941dSLen Brown 	}
37803a9a941dSLen Brown 	return 0;
37813a9a941dSLen Brown }
37823a9a941dSLen Brown 
3783889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3784889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3785889facbeSLen Brown 
37869392bd98SCalvin Walton double get_tdp_intel(unsigned int model)
3787144b44b1SLen Brown {
3788144b44b1SLen Brown 	unsigned long long msr;
3789144b44b1SLen Brown 
3790144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
37917ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3792144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3793144b44b1SLen Brown 
3794144b44b1SLen Brown 	switch (model) {
3795f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3796f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:
3797144b44b1SLen Brown 		return 30.0;
3798144b44b1SLen Brown 	default:
3799144b44b1SLen Brown 		return 135.0;
3800144b44b1SLen Brown 	}
3801144b44b1SLen Brown }
3802144b44b1SLen Brown 
38039392bd98SCalvin Walton double get_tdp_amd(unsigned int family)
38049392bd98SCalvin Walton {
38059392bd98SCalvin Walton 	switch (family) {
38069392bd98SCalvin Walton 	case 0x17:
38079392bd98SCalvin Walton 	default:
38089392bd98SCalvin Walton 		/* This is the max stock TDP of HEDT/Server Fam17h chips */
38099392bd98SCalvin Walton 		return 250.0;
38109392bd98SCalvin Walton 	}
38119392bd98SCalvin Walton }
38129392bd98SCalvin Walton 
381340ee8e3bSAndrey Semin /*
381440ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
381540ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
381640ee8e3bSAndrey Semin  */
381740ee8e3bSAndrey Semin static double
381840ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
381940ee8e3bSAndrey Semin {
382040ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
382140ee8e3bSAndrey Semin 
382240ee8e3bSAndrey Semin 	switch (model) {
3823869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3824869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3825869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
382640ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
382740ee8e3bSAndrey Semin 	default:
382840ee8e3bSAndrey Semin 		return (rapl_energy_units);
382940ee8e3bSAndrey Semin 	}
383040ee8e3bSAndrey Semin }
383140ee8e3bSAndrey Semin 
38329392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model)
3833889facbeSLen Brown {
3834889facbeSLen Brown 	unsigned long long msr;
3835144b44b1SLen Brown 	unsigned int time_unit;
3836889facbeSLen Brown 	double tdp;
3837889facbeSLen Brown 
3838889facbeSLen Brown 	if (family != 6)
3839889facbeSLen Brown 		return;
3840889facbeSLen Brown 
3841889facbeSLen Brown 	switch (model) {
3842869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3843869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3844869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3845869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3846869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3847869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3848144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3849812db3f7SLen Brown 		if (rapl_joules) {
3850812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3851812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3852812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3853812db3f7SLen Brown 		} else {
3854812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3855812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3856812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3857812db3f7SLen Brown 		}
3858889facbeSLen Brown 		break;
3859869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3860f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3861e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3862812db3f7SLen Brown 		if (rapl_joules)
3863812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3864812db3f7SLen Brown 		else
3865812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3866e4085d54SLen Brown 		break;
3867869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3868997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
386981824921SLen 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;
3870812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3871812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3872812db3f7SLen Brown 		if (rapl_joules) {
3873812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3874812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3875812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
387681824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3877812db3f7SLen Brown 		} else {
3878812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3879812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3880812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
388181824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3882812db3f7SLen Brown 		}
38830b2bb692SLen Brown 		break;
3884869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3885869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3886869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3887869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
38880b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3889812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3890812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3891812db3f7SLen Brown 		if (rapl_joules) {
3892812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3893812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3894812db3f7SLen Brown 		} else {
3895812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3896812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3897812db3f7SLen Brown 		}
3898e6f9bb3cSLen Brown 		break;
3899869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3900869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
39010b2bb692SLen 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;
3902812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3903812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3904812db3f7SLen Brown 		if (rapl_joules) {
3905812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3906812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3907812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3908812db3f7SLen Brown 		} else {
3909812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3910812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3911812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3912812db3f7SLen Brown 		}
3913144b44b1SLen Brown 		break;
3914f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3915f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
3916144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3917812db3f7SLen Brown 		if (rapl_joules) {
3918812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3919812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3920812db3f7SLen Brown 		} else {
3921812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3922812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3923812db3f7SLen Brown 		}
3924889facbeSLen Brown 		break;
3925f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
39260f644909SJacob 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;
3927812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3928812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3929812db3f7SLen Brown 		if (rapl_joules) {
3930812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3931812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3932812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3933812db3f7SLen Brown 		} else {
3934812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3935812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3936812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3937812db3f7SLen Brown 		}
39380f644909SJacob Pan 		break;
3939889facbeSLen Brown 	default:
3940889facbeSLen Brown 		return;
3941889facbeSLen Brown 	}
3942889facbeSLen Brown 
3943889facbeSLen Brown 	/* units on package 0, verify later other packages match */
39447ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3945889facbeSLen Brown 		return;
3946889facbeSLen Brown 
3947889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3948f2c4db1bSPeter Zijlstra 	if (model == INTEL_FAM6_ATOM_SILVERMONT)
3949144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3950144b44b1SLen Brown 	else
3951889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3952889facbeSLen Brown 
395340ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
395440ee8e3bSAndrey Semin 
3955144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3956144b44b1SLen Brown 	if (time_unit == 0)
3957144b44b1SLen Brown 		time_unit = 0xA;
3958889facbeSLen Brown 
3959144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3960144b44b1SLen Brown 
39619392bd98SCalvin Walton 	tdp = get_tdp_intel(model);
3962889facbeSLen Brown 
3963889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
396496e47158SLen Brown 	if (!quiet)
3965b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
39669392bd98SCalvin Walton }
3967889facbeSLen Brown 
39689392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model)
39699392bd98SCalvin Walton {
39709392bd98SCalvin Walton 	unsigned long long msr;
39719392bd98SCalvin Walton 	unsigned int eax, ebx, ecx, edx;
39729392bd98SCalvin Walton 	unsigned int has_rapl = 0;
39739392bd98SCalvin Walton 	double tdp;
39749392bd98SCalvin Walton 
39759392bd98SCalvin Walton 	if (max_extended_level >= 0x80000007) {
39769392bd98SCalvin Walton 		__cpuid(0x80000007, eax, ebx, ecx, edx);
39779392bd98SCalvin Walton 		/* RAPL (Fam 17h) */
39789392bd98SCalvin Walton 		has_rapl = edx & (1 << 14);
39799392bd98SCalvin Walton 	}
39809392bd98SCalvin Walton 
39819392bd98SCalvin Walton 	if (!has_rapl)
3982889facbeSLen Brown 		return;
39839392bd98SCalvin Walton 
39849392bd98SCalvin Walton 	switch (family) {
39859392bd98SCalvin Walton 	case 0x17: /* Zen, Zen+ */
39869392bd98SCalvin Walton 		do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
3987*3316f99aSCalvin Walton 		if (rapl_joules) {
3988*3316f99aSCalvin Walton 			BIC_PRESENT(BIC_Pkg_J);
39899392bd98SCalvin Walton 			BIC_PRESENT(BIC_Cor_J);
3990*3316f99aSCalvin Walton 		} else {
3991*3316f99aSCalvin Walton 			BIC_PRESENT(BIC_PkgWatt);
39929392bd98SCalvin Walton 			BIC_PRESENT(BIC_CorWatt);
3993*3316f99aSCalvin Walton 		}
39949392bd98SCalvin Walton 		break;
39959392bd98SCalvin Walton 	default:
39969392bd98SCalvin Walton 		return;
39979392bd98SCalvin Walton 	}
39989392bd98SCalvin Walton 
39999392bd98SCalvin Walton 	if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
40009392bd98SCalvin Walton 		return;
40019392bd98SCalvin Walton 
40029392bd98SCalvin Walton 	rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
40039392bd98SCalvin Walton 	rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
40049392bd98SCalvin Walton 	rapl_power_units = ldexp(1.0, -(msr & 0xf));
40059392bd98SCalvin Walton 
40069392bd98SCalvin Walton 	tdp = get_tdp_amd(model);
40079392bd98SCalvin Walton 
40089392bd98SCalvin Walton 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
40099392bd98SCalvin Walton 	if (!quiet)
40109392bd98SCalvin Walton 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
40119392bd98SCalvin Walton }
40129392bd98SCalvin Walton 
40139392bd98SCalvin Walton /*
40149392bd98SCalvin Walton  * rapl_probe()
40159392bd98SCalvin Walton  *
40169392bd98SCalvin Walton  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
40179392bd98SCalvin Walton  */
40189392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model)
40199392bd98SCalvin Walton {
40209392bd98SCalvin Walton 	if (genuine_intel)
40219392bd98SCalvin Walton 		rapl_probe_intel(family, model);
40229392bd98SCalvin Walton 	if (authentic_amd)
40239392bd98SCalvin Walton 		rapl_probe_amd(family, model);
4024889facbeSLen Brown }
4025889facbeSLen Brown 
40261b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
40273a9a941dSLen Brown {
40283a9a941dSLen Brown 	if (!genuine_intel)
40293a9a941dSLen Brown 		return;
40303a9a941dSLen Brown 
40313a9a941dSLen Brown 	if (family != 6)
40323a9a941dSLen Brown 		return;
40333a9a941dSLen Brown 
40343a9a941dSLen Brown 	switch (model) {
4035869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
4036869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
40373a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
4038869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
40393a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
40403a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
40413a9a941dSLen Brown 	default:
40423a9a941dSLen Brown 		return;
40433a9a941dSLen Brown 	}
40443a9a941dSLen Brown }
40453a9a941dSLen Brown 
4046ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4047ac980e13SArtem Bityutskiy {
4048ac980e13SArtem Bityutskiy 	if (is_skx(family, model) || is_bdx(family, model))
4049ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
4050ac980e13SArtem Bityutskiy }
4051ac980e13SArtem Bityutskiy 
4052889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4053889facbeSLen Brown {
4054889facbeSLen Brown 	unsigned long long msr;
4055f4896fa5SLen Brown 	unsigned int dts, dts2;
4056889facbeSLen Brown 	int cpu;
4057889facbeSLen Brown 
4058889facbeSLen Brown 	if (!(do_dts || do_ptm))
4059889facbeSLen Brown 		return 0;
4060889facbeSLen Brown 
4061889facbeSLen Brown 	cpu = t->cpu_id;
4062889facbeSLen Brown 
4063889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
4064889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4065889facbeSLen Brown 		return 0;
4066889facbeSLen Brown 
4067889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4068b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4069889facbeSLen Brown 		return -1;
4070889facbeSLen Brown 	}
4071889facbeSLen Brown 
4072889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4073889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4074889facbeSLen Brown 			return 0;
4075889facbeSLen Brown 
4076889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4077b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
4078889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
4079889facbeSLen Brown 
4080889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4081889facbeSLen Brown 			return 0;
4082889facbeSLen Brown 
4083889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4084889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4085b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4086889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4087889facbeSLen Brown 	}
4088889facbeSLen Brown 
4089889facbeSLen Brown 
4090f4896fa5SLen Brown 	if (do_dts && debug) {
4091889facbeSLen Brown 		unsigned int resolution;
4092889facbeSLen Brown 
4093889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4094889facbeSLen Brown 			return 0;
4095889facbeSLen Brown 
4096889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4097889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
4098b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4099889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
4100889facbeSLen Brown 
4101889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4102889facbeSLen Brown 			return 0;
4103889facbeSLen Brown 
4104889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4105889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4106b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4107889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4108889facbeSLen Brown 	}
4109889facbeSLen Brown 
4110889facbeSLen Brown 	return 0;
4111889facbeSLen Brown }
4112889facbeSLen Brown 
4113889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4114889facbeSLen Brown {
4115b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4116889facbeSLen Brown 		cpu, label,
4117889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
4118889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
4119889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4120889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
4121889facbeSLen Brown 
4122889facbeSLen Brown 	return;
4123889facbeSLen Brown }
4124889facbeSLen Brown 
4125889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4126889facbeSLen Brown {
4127889facbeSLen Brown 	unsigned long long msr;
41289392bd98SCalvin Walton 	const char *msr_name;
4129889facbeSLen Brown 	int cpu;
4130889facbeSLen Brown 
4131889facbeSLen Brown 	if (!do_rapl)
4132889facbeSLen Brown 		return 0;
4133889facbeSLen Brown 
4134889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4135889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4136889facbeSLen Brown 		return 0;
4137889facbeSLen Brown 
4138889facbeSLen Brown 	cpu = t->cpu_id;
4139889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4140b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4141889facbeSLen Brown 		return -1;
4142889facbeSLen Brown 	}
4143889facbeSLen Brown 
41449392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
41459392bd98SCalvin Walton 		msr_name = "MSR_RAPL_PWR_UNIT";
41469392bd98SCalvin Walton 		if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
41479392bd98SCalvin Walton 			return -1;
41489392bd98SCalvin Walton 	} else {
41499392bd98SCalvin Walton 		msr_name = "MSR_RAPL_POWER_UNIT";
4150889facbeSLen Brown 		if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4151889facbeSLen Brown 			return -1;
41529392bd98SCalvin Walton 	}
4153889facbeSLen Brown 
41549392bd98SCalvin Walton 	fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4155144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
415696e47158SLen Brown 
4157144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4158144b44b1SLen Brown 
4159889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4160889facbeSLen Brown                 	return -5;
4161889facbeSLen Brown 
4162889facbeSLen Brown 
4163b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4164889facbeSLen Brown 			cpu, msr,
4165889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4166889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4167889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4168889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4169889facbeSLen Brown 
4170144b44b1SLen Brown 	}
4171144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4172144b44b1SLen Brown 
4173889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4174889facbeSLen Brown 			return -9;
4175889facbeSLen Brown 
4176b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4177889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4178889facbeSLen Brown 
4179889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
4180b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4181889facbeSLen Brown 			cpu,
4182889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4183889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4184889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4185889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4186889facbeSLen Brown 	}
4187889facbeSLen Brown 
41880b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4189889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4190889facbeSLen Brown                 	return -6;
4191889facbeSLen Brown 
4192b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4193889facbeSLen Brown 			cpu, msr,
4194889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4195889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4196889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4197889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
41980b2bb692SLen Brown 	}
41990b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4200889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4201889facbeSLen Brown 			return -9;
4202b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4203889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4204889facbeSLen Brown 
4205889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
4206889facbeSLen Brown 	}
4207144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
4208889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4209889facbeSLen Brown 			return -7;
4210889facbeSLen Brown 
4211b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4212144b44b1SLen Brown 	}
42139148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4214889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4215889facbeSLen Brown 			return -9;
4216b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4217889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4218889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
4219889facbeSLen Brown 	}
4220889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
4221889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4222889facbeSLen Brown 			return -8;
4223889facbeSLen Brown 
4224b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4225889facbeSLen Brown 
4226889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4227889facbeSLen Brown 			return -9;
4228b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4229889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4230889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
4231889facbeSLen Brown 	}
4232889facbeSLen Brown 	return 0;
4233889facbeSLen Brown }
4234889facbeSLen Brown 
4235d7899447SLen Brown /*
4236d7899447SLen Brown  * SNB adds support for additional MSRs:
4237d7899447SLen Brown  *
4238d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
4239d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
4240d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
4241d7899447SLen Brown  */
4242103a8feaSLen Brown 
4243d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
4244103a8feaSLen Brown {
4245103a8feaSLen Brown 	if (!genuine_intel)
4246103a8feaSLen Brown 		return 0;
4247103a8feaSLen Brown 
4248103a8feaSLen Brown 	switch (model) {
4249869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4250869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4251869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
4252869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
4253869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
4254869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
4255869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
4256869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
4257869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
4258869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4259869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4260997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4261869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
4262869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4263f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4264f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
4265103a8feaSLen Brown 		return 1;
4266103a8feaSLen Brown 	}
4267103a8feaSLen Brown 	return 0;
4268103a8feaSLen Brown }
4269103a8feaSLen Brown 
4270d7899447SLen Brown /*
4271d7899447SLen Brown  * HSW adds support for additional MSRs:
4272d7899447SLen Brown  *
4273d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
4274d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
4275d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
42765a63426eSLen Brown  *
42775a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
42785a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
42795a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
42805a63426eSLen Brown  *
4281d7899447SLen Brown  */
4282d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
4283ca58710fSKristen Carlson Accardi {
4284ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
4285ca58710fSKristen Carlson Accardi 		return 0;
4286ca58710fSKristen Carlson Accardi 
4287ca58710fSKristen Carlson Accardi 	switch (model) {
4288f5a4c76aSLen Brown 	case INTEL_FAM6_HASWELL_CORE:
4289869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
4290869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4291997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4292869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4293f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4294ca58710fSKristen Carlson Accardi 		return 1;
4295ca58710fSKristen Carlson Accardi 	}
4296ca58710fSKristen Carlson Accardi 	return 0;
4297ca58710fSKristen Carlson Accardi }
4298ca58710fSKristen Carlson Accardi 
42990b2bb692SLen Brown /*
43000b2bb692SLen Brown  * SKL adds support for additional MSRS:
43010b2bb692SLen Brown  *
43020b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
43030b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
43040b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
43050b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
43060b2bb692SLen Brown  */
43070b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
43080b2bb692SLen Brown {
43090b2bb692SLen Brown 	if (!genuine_intel)
43100b2bb692SLen Brown 		return 0;
43110b2bb692SLen Brown 
43120b2bb692SLen Brown 	switch (model) {
4313869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4314997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
43150b2bb692SLen Brown 		return 1;
43160b2bb692SLen Brown 	}
43170b2bb692SLen Brown 	return 0;
43180b2bb692SLen Brown }
43190b2bb692SLen Brown 
4320144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
4321144b44b1SLen Brown {
4322144b44b1SLen Brown 	if (!genuine_intel)
4323144b44b1SLen Brown 		return 0;
4324144b44b1SLen Brown 	switch (model) {
4325f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
4326f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
4327144b44b1SLen Brown 		return 1;
4328144b44b1SLen Brown 	}
4329144b44b1SLen Brown 	return 0;
4330144b44b1SLen Brown }
4331144b44b1SLen Brown 
4332fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
4333fb5d4327SDasaratharaman Chandramouli {
4334fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
4335fb5d4327SDasaratharaman Chandramouli 		return 0;
4336fb5d4327SDasaratharaman Chandramouli 	switch (model) {
4337869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
4338fb5d4327SDasaratharaman Chandramouli 		return 1;
4339fb5d4327SDasaratharaman Chandramouli 	}
4340fb5d4327SDasaratharaman Chandramouli 	return 0;
4341fb5d4327SDasaratharaman Chandramouli }
4342fb5d4327SDasaratharaman Chandramouli 
4343997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
4344997e5395SSrinivas Pandruvada {
4345997e5395SSrinivas Pandruvada 	if (!genuine_intel)
4346997e5395SSrinivas Pandruvada 		return 0;
4347997e5395SSrinivas Pandruvada 
4348997e5395SSrinivas Pandruvada 	switch (model) {
4349997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4350997e5395SSrinivas Pandruvada 		return 1;
4351997e5395SSrinivas Pandruvada 	}
4352997e5395SSrinivas Pandruvada 
4353997e5395SSrinivas Pandruvada 	return 0;
4354997e5395SSrinivas Pandruvada }
4355997e5395SSrinivas Pandruvada 
4356b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4357b2b34dfeSHubert Chrzaniuk {
4358b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
4359b2b34dfeSHubert Chrzaniuk 		return 1024;
4360b2b34dfeSHubert Chrzaniuk 	return 1;
4361b2b34dfeSHubert Chrzaniuk }
4362b2b34dfeSHubert Chrzaniuk 
4363144b44b1SLen Brown #define SLM_BCLK_FREQS 5
4364144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4365144b44b1SLen Brown 
4366144b44b1SLen Brown double slm_bclk(void)
4367144b44b1SLen Brown {
4368144b44b1SLen Brown 	unsigned long long msr = 3;
4369144b44b1SLen Brown 	unsigned int i;
4370144b44b1SLen Brown 	double freq;
4371144b44b1SLen Brown 
43727ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4373b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
4374144b44b1SLen Brown 
4375144b44b1SLen Brown 	i = msr & 0xf;
4376144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
4377b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
43780a91e551SColin Ian King 		i = 3;
4379144b44b1SLen Brown 	}
4380144b44b1SLen Brown 	freq = slm_freq_table[i];
4381144b44b1SLen Brown 
438296e47158SLen Brown 	if (!quiet)
4383b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4384144b44b1SLen Brown 
4385144b44b1SLen Brown 	return freq;
4386144b44b1SLen Brown }
4387144b44b1SLen Brown 
4388103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
4389103a8feaSLen Brown {
4390121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
4391103a8feaSLen Brown 		return 100.00;
4392144b44b1SLen Brown 	else if (is_slm(family, model))
4393144b44b1SLen Brown 		return slm_bclk();
4394103a8feaSLen Brown 	else
4395103a8feaSLen Brown 		return 133.33;
4396103a8feaSLen Brown }
4397103a8feaSLen Brown 
4398889facbeSLen Brown /*
4399889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4400889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
4401889facbeSLen Brown  * This is usually equal to tjMax.
4402889facbeSLen Brown  *
4403889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
4404889facbeSLen Brown  * but also allow cmdline over-ride with -T.
4405889facbeSLen Brown  *
4406889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
4407889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
4408889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4409889facbeSLen Brown  */
4410889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4411889facbeSLen Brown {
4412889facbeSLen Brown 	unsigned long long msr;
4413889facbeSLen Brown 	unsigned int target_c_local;
4414889facbeSLen Brown 	int cpu;
4415889facbeSLen Brown 
4416889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
4417889facbeSLen Brown 	if (!(do_dts || do_ptm))
4418889facbeSLen Brown 		return 0;
4419889facbeSLen Brown 
4420889facbeSLen Brown 	/* this is a per-package concept */
4421889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4422889facbeSLen Brown 		return 0;
4423889facbeSLen Brown 
4424889facbeSLen Brown 	cpu = t->cpu_id;
4425889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4426b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4427889facbeSLen Brown 		return -1;
4428889facbeSLen Brown 	}
4429889facbeSLen Brown 
4430889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
4431889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
4432b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4433889facbeSLen Brown 			cpu, tcc_activation_temp);
4434889facbeSLen Brown 		return 0;
4435889facbeSLen Brown 	}
4436889facbeSLen Brown 
4437889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
4438d7899447SLen Brown 	if (!do_nhm_platform_info)
4439889facbeSLen Brown 		goto guess;
4440889facbeSLen Brown 
44417ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4442889facbeSLen Brown 		goto guess;
4443889facbeSLen Brown 
44443482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
4445889facbeSLen Brown 
444696e47158SLen Brown 	if (!quiet)
4447b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4448889facbeSLen Brown 			cpu, msr, target_c_local);
4449889facbeSLen Brown 
44503482124aSJean Delvare 	if (!target_c_local)
4451889facbeSLen Brown 		goto guess;
4452889facbeSLen Brown 
4453889facbeSLen Brown 	tcc_activation_temp = target_c_local;
4454889facbeSLen Brown 
4455889facbeSLen Brown 	return 0;
4456889facbeSLen Brown 
4457889facbeSLen Brown guess:
4458889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
4459b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4460889facbeSLen Brown 		cpu, tcc_activation_temp);
4461889facbeSLen Brown 
4462889facbeSLen Brown 	return 0;
4463889facbeSLen Brown }
446469807a63SLen Brown 
4465aa8d8cc7SLen Brown void decode_feature_control_msr(void)
4466aa8d8cc7SLen Brown {
4467aa8d8cc7SLen Brown 	unsigned long long msr;
4468aa8d8cc7SLen Brown 
4469aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4470aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4471aa8d8cc7SLen Brown 			base_cpu, msr,
4472aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4473aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
4474aa8d8cc7SLen Brown }
4475aa8d8cc7SLen Brown 
447669807a63SLen Brown void decode_misc_enable_msr(void)
447769807a63SLen Brown {
447869807a63SLen Brown 	unsigned long long msr;
447969807a63SLen Brown 
4480f26b1519SLen Brown 	if (!genuine_intel)
4481f26b1519SLen Brown 		return;
4482f26b1519SLen Brown 
448369807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4484e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
448569807a63SLen Brown 			base_cpu, msr,
4486e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4487e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4488fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4489e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4490e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
449169807a63SLen Brown }
449269807a63SLen Brown 
449333148d67SLen Brown void decode_misc_feature_control(void)
449433148d67SLen Brown {
449533148d67SLen Brown 	unsigned long long msr;
449633148d67SLen Brown 
449733148d67SLen Brown 	if (!has_misc_feature_control)
449833148d67SLen Brown 		return;
449933148d67SLen Brown 
450033148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
450133148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
450233148d67SLen Brown 			base_cpu, msr,
450333148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
450433148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
450533148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
450633148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
450733148d67SLen Brown }
4508f0057310SLen Brown /*
4509f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
4510f0057310SLen Brown  *
4511f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
4512f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
4513f0057310SLen Brown  * bit[1] less so...
4514f0057310SLen Brown  */
4515f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
4516f0057310SLen Brown {
4517f0057310SLen Brown 	unsigned long long msr;
4518f0057310SLen Brown 
4519f0057310SLen Brown 	if (!do_nhm_platform_info)
4520f0057310SLen Brown 		return;
4521f0057310SLen Brown 
4522cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
4523cf4cbe53SLen Brown 		return;
4524cf4cbe53SLen Brown 
4525f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4526ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4527f0057310SLen Brown 			base_cpu, msr,
4528f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
4529ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
4530ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
4531f0057310SLen Brown }
453271616c8eSLen Brown /*
453371616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
453471616c8eSLen Brown  *
453571616c8eSLen Brown  * This MSRs are present on Silvermont processors,
453671616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
453771616c8eSLen Brown  */
453871616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
453971616c8eSLen Brown {
454071616c8eSLen Brown 	unsigned long long msr;
454171616c8eSLen Brown 
454271616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
454371616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
454471616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
454571616c8eSLen Brown 
454671616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
454771616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
454871616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
454971616c8eSLen Brown }
45507f5c258eSLen Brown 
4551f5a4c76aSLen Brown /*
4552f5a4c76aSLen Brown  * When models are the same, for the purpose of turbostat, reuse
4553f5a4c76aSLen Brown  */
4554f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model)
4555f5a4c76aSLen Brown {
4556f5a4c76aSLen Brown 
4557f5a4c76aSLen Brown 	switch(model) {
4558f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
4559f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
4560f5a4c76aSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
4561f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
4562f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
4563f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM;
4564f5a4c76aSLen Brown 
4565f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
4566f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
4567f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM_EX;
4568f5a4c76aSLen Brown 
4569f5a4c76aSLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
4570f5a4c76aSLen Brown 		return INTEL_FAM6_XEON_PHI_KNL;
4571f5a4c76aSLen Brown 
4572f5a4c76aSLen Brown 	case INTEL_FAM6_HASWELL_ULT:
4573f5a4c76aSLen Brown 		return INTEL_FAM6_HASWELL_CORE;
4574f5a4c76aSLen Brown 
4575f5a4c76aSLen Brown 	case INTEL_FAM6_BROADWELL_X:
4576f5a4c76aSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
4577f5a4c76aSLen Brown 		return INTEL_FAM6_BROADWELL_X;
4578f5a4c76aSLen Brown 
4579f5a4c76aSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:
4580f5a4c76aSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:
4581f5a4c76aSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:
4582f5a4c76aSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:
4583f5a4c76aSLen Brown 		return INTEL_FAM6_SKYLAKE_MOBILE;
4584937807d3SLen Brown 
4585937807d3SLen Brown 	case INTEL_FAM6_ICELAKE_MOBILE:
4586937807d3SLen Brown 		return INTEL_FAM6_CANNONLAKE_MOBILE;
4587f5a4c76aSLen Brown 	}
4588f5a4c76aSLen Brown 	return model;
4589f5a4c76aSLen Brown }
4590fcd17211SLen Brown void process_cpuid()
4591103a8feaSLen Brown {
459234041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
459334041551SLen Brown 	unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
4594b3a34e93SLen Brown 	unsigned int has_turbo;
4595103a8feaSLen Brown 
4596103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4597103a8feaSLen Brown 
45985aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4599103a8feaSLen Brown 
460034041551SLen Brown 	if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
4601103a8feaSLen Brown 		genuine_intel = 1;
460234041551SLen Brown 	else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
460334041551SLen Brown 		authentic_amd = 1;
4604103a8feaSLen Brown 
460596e47158SLen Brown 	if (!quiet)
4606b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4607103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4608103a8feaSLen Brown 
46095aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4610103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4611103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4612103a8feaSLen Brown 	stepping = fms & 0xf;
46135aa3d1a2SCalvin Walton 	if (family == 0xf)
46145aa3d1a2SCalvin Walton 		family += (fms >> 20) & 0xff;
46155aa3d1a2SCalvin Walton 	if (family >= 6)
4616103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
461734041551SLen Brown 	ecx_flags = ecx;
461834041551SLen Brown 	edx_flags = edx;
4619103a8feaSLen Brown 
4620103a8feaSLen Brown 	/*
4621103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4622103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4623103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4624103a8feaSLen Brown 	 */
4625103a8feaSLen Brown 	ebx = ecx = edx = 0;
46265aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4627103a8feaSLen Brown 
462834041551SLen Brown 	if (!quiet) {
462934041551SLen Brown 		fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
463034041551SLen Brown 			max_level, max_extended_level, family, model, stepping, family, model, stepping);
463134041551SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
463234041551SLen Brown 			ecx_flags & (1 << 0) ? "SSE3" : "-",
463334041551SLen Brown 			ecx_flags & (1 << 3) ? "MONITOR" : "-",
463434041551SLen Brown 			ecx_flags & (1 << 6) ? "SMX" : "-",
463534041551SLen Brown 			ecx_flags & (1 << 7) ? "EIST" : "-",
463634041551SLen Brown 			ecx_flags & (1 << 8) ? "TM2" : "-",
463734041551SLen Brown 			edx_flags & (1 << 4) ? "TSC" : "-",
463834041551SLen Brown 			edx_flags & (1 << 5) ? "MSR" : "-",
463934041551SLen Brown 			edx_flags & (1 << 22) ? "ACPI-TM" : "-",
464034041551SLen Brown 			edx_flags & (1 << 28) ? "HT" : "-",
464134041551SLen Brown 			edx_flags & (1 << 29) ? "TM" : "-");
464234041551SLen Brown 	}
4643f5a4c76aSLen Brown 	if (genuine_intel)
4644f5a4c76aSLen Brown 		model = intel_model_duplicates(model);
464534041551SLen Brown 
464634041551SLen Brown 	if (!(edx_flags & (1 << 5)))
464734041551SLen Brown 		errx(1, "CPUID: no MSR");
464834041551SLen Brown 
464961a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4650103a8feaSLen Brown 
4651103a8feaSLen Brown 		/*
4652103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4653103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4654103a8feaSLen Brown 		 */
46555aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
46568209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4657d7899447SLen Brown 	}
4658103a8feaSLen Brown 
4659103a8feaSLen Brown 	/*
4660103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4661103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4662103a8feaSLen Brown 	 */
4663103a8feaSLen Brown 
46645aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
46658209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4666812db3f7SLen Brown 	if (has_aperf) {
4667812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4668812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4669812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4670812db3f7SLen Brown 	}
4671889facbeSLen Brown 	do_dts = eax & (1 << 0);
4672812db3f7SLen Brown 	if (do_dts)
4673812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4674b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4675889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4676812db3f7SLen Brown 	if (do_ptm)
4677812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
46787f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
46797f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
46807f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
46817f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
46827f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4683889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4684889facbeSLen Brown 
468596e47158SLen Brown 	if (!quiet)
4686b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
46877f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
46887f5c258eSLen Brown 			has_aperf ? "" : "No-",
4689b3a34e93SLen Brown 			has_turbo ? "" : "No-",
46907f5c258eSLen Brown 			do_dts ? "" : "No-",
46917f5c258eSLen Brown 			do_ptm ? "" : "No-",
46927f5c258eSLen Brown 			has_hwp ? "" : "No-",
46937f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
46947f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
46957f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
46967f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
46977f5c258eSLen Brown 			has_epb ? "" : "No-");
4698103a8feaSLen Brown 
469996e47158SLen Brown 	if (!quiet)
470069807a63SLen Brown 		decode_misc_enable_msr();
470169807a63SLen Brown 
470233148d67SLen Brown 
470396e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4704aa8d8cc7SLen Brown 		int has_sgx;
4705aa8d8cc7SLen Brown 
4706aa8d8cc7SLen Brown 		ecx = 0;
4707aa8d8cc7SLen Brown 
4708aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4709aa8d8cc7SLen Brown 
4710aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4711aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4712aa8d8cc7SLen Brown 
4713aa8d8cc7SLen Brown 		if (has_sgx)
4714aa8d8cc7SLen Brown 			decode_feature_control_msr();
4715aa8d8cc7SLen Brown 	}
4716aa8d8cc7SLen Brown 
471761a87ba7SLen Brown 	if (max_level >= 0x15) {
47188a5bdf41SLen Brown 		unsigned int eax_crystal;
47198a5bdf41SLen Brown 		unsigned int ebx_tsc;
47208a5bdf41SLen Brown 
47218a5bdf41SLen Brown 		/*
47228a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
47238a5bdf41SLen Brown 		 */
47248a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
47255aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
47268a5bdf41SLen Brown 
47278a5bdf41SLen Brown 		if (ebx_tsc != 0) {
47288a5bdf41SLen Brown 
472996e47158SLen Brown 			if (!quiet && (ebx != 0))
4730b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
47318a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
47328a5bdf41SLen Brown 
47338a5bdf41SLen Brown 			if (crystal_hz == 0)
47348a5bdf41SLen Brown 				switch(model) {
4735869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4736e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4737e8efbc80SLen Brown 					break;
4738f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
4739ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4740ec53e594SLen Brown 					break;
4741869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4742f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4743e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
47448a5bdf41SLen Brown 					break;
47458a5bdf41SLen Brown 				default:
47468a5bdf41SLen Brown 					crystal_hz = 0;
47478a5bdf41SLen Brown 			}
47488a5bdf41SLen Brown 
47498a5bdf41SLen Brown 			if (crystal_hz) {
47508a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
475196e47158SLen Brown 				if (!quiet)
4752b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
47538a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
47548a5bdf41SLen Brown 			}
47558a5bdf41SLen Brown 		}
47568a5bdf41SLen Brown 	}
475761a87ba7SLen Brown 	if (max_level >= 0x16) {
475861a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
475961a87ba7SLen Brown 
476061a87ba7SLen Brown 		/*
476161a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
476261a87ba7SLen Brown 		 */
476361a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
476461a87ba7SLen Brown 
47655aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
476696e47158SLen Brown 		if (!quiet)
4767b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
476861a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
476961a87ba7SLen Brown 	}
47708a5bdf41SLen Brown 
4771b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4772b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4773b2b34dfeSHubert Chrzaniuk 
4774812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4775812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4776812db3f7SLen Brown 
4777812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4778812db3f7SLen Brown 		do_nhm_platform_info = 1;
4779812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4780812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4781812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4782812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4783812db3f7SLen Brown 	}
4784d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4785812db3f7SLen Brown 
4786812db3f7SLen Brown 	if (do_snb_cstates)
4787812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4788812db3f7SLen Brown 
47895a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
47900f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
47910f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
47920f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
47930f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
47940f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
47950f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
47960f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
47970f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
47980539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
47990f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
48000f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
48010f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
48020f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
48030539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
48040539ba11SLen Brown 		use_c1_residency_msr = 1;
48050539ba11SLen Brown 	}
48067170a374SLen Brown 	if (is_dnv(family, model)) {
48077170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
48087170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
48097170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
48107170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
48117170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
48127170a374SLen Brown 		use_c1_residency_msr = 1;
48137170a374SLen Brown 	}
481434c76197SLen Brown 	if (is_skx(family, model)) {
481534c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
481634c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
481734c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
481834c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
481934c76197SLen Brown 	}
4820ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4821ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4822ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4823ade0ebacSLen Brown 	}
48240f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
48250f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
48260f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
48270f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
48280f47c08dSLen Brown 	}
48295a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
4830a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4831a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4832a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4833a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4834a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4835a99d8730SLen Brown 	}
4836144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4837fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4838103a8feaSLen Brown 
483931a1f15cSLen Brown 	if (do_slm_cstates || do_knl_cstates || is_cnl(family, model))
4840562855eeSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
4841562855eeSLen Brown 
484296e47158SLen Brown 	if (!quiet)
4843f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4844f0057310SLen Brown 
484596e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
484671616c8eSLen Brown 		decode_c6_demotion_policy_msr();
484771616c8eSLen Brown 
4848889facbeSLen Brown 	rapl_probe(family, model);
48493a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4850ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
4851889facbeSLen Brown 
485296e47158SLen Brown 	if (!quiet)
48531b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4854fcd17211SLen Brown 
485541618e63SLen Brown 	if (!quiet)
485641618e63SLen Brown 		dump_sysfs_cstate_config();
48577293fccdSLen Brown 	if (!quiet)
48587293fccdSLen Brown 		dump_sysfs_pstate_config();
485941618e63SLen Brown 
4860a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4861a2b7b749SLen Brown 		calculate_tsc_tweak();
4862a2b7b749SLen Brown 
4863812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4864812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4865fdf676e5SLen Brown 
4866812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4867812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
486827d47356SLen Brown 
4869be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4870be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
4871be0e54c4SLen Brown 	else
4872be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
4873be0e54c4SLen Brown 
4874be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4875be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
4876be0e54c4SLen Brown 	else
4877be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
4878be0e54c4SLen Brown 
487996e47158SLen Brown 	if (!quiet)
488033148d67SLen Brown 		decode_misc_feature_control();
488133148d67SLen Brown 
4882889facbeSLen Brown 	return;
4883103a8feaSLen Brown }
4884103a8feaSLen Brown 
4885103a8feaSLen Brown /*
4886103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4887103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4888103a8feaSLen Brown  */
4889103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4890103a8feaSLen Brown {
4891103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4892103a8feaSLen Brown 		return 1;
4893103a8feaSLen Brown 	else
4894103a8feaSLen Brown 		return 0;
4895103a8feaSLen Brown }
4896103a8feaSLen Brown 
4897103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4898103a8feaSLen Brown {
4899103a8feaSLen Brown 	return 0;
4900103a8feaSLen Brown }
4901103a8feaSLen Brown 
4902c98d5d94SLen Brown void topology_probe()
4903c98d5d94SLen Brown {
4904c98d5d94SLen Brown 	int i;
4905c98d5d94SLen Brown 	int max_core_id = 0;
4906c98d5d94SLen Brown 	int max_package_id = 0;
49076de68fe1SLen Brown 	int max_die_id = 0;
4908c98d5d94SLen Brown 	int max_siblings = 0;
4909c98d5d94SLen Brown 
4910c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4911843c5791SPrarit Bhargava 	set_max_cpu_num();
4912c98d5d94SLen Brown 	topo.num_cpus = 0;
4913c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4914c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4915812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4916c98d5d94SLen Brown 
4917d8af6f5fSLen Brown 	if (debug > 1)
4918b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4919c98d5d94SLen Brown 
4920c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4921b2c95d90SJosh Triplett 	if (cpus == NULL)
4922b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4923c98d5d94SLen Brown 
4924c98d5d94SLen Brown 	/*
4925c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4926c98d5d94SLen Brown 	 */
4927c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4928b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4929b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4930c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4931c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4932c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4933c98d5d94SLen Brown 
4934c98d5d94SLen Brown 	/*
49351ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
49361ef7d21aSLen Brown 	 */
49371ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
49381ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
49391ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
49401ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
49411ef7d21aSLen Brown 	}
49421ef7d21aSLen Brown 
49431ef7d21aSLen Brown 	/*
4944c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4945c98d5d94SLen Brown 	 */
4946c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4947b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4948b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4949c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4950c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4951c98d5d94SLen Brown 
49528cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
4953c98d5d94SLen Brown 
4954c98d5d94SLen Brown 	/*
4955c98d5d94SLen Brown 	 * For online cpus
4956c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4957c98d5d94SLen Brown 	 */
4958c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4959c98d5d94SLen Brown 		int siblings;
4960c98d5d94SLen Brown 
4961c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4962d8af6f5fSLen Brown 			if (debug > 1)
4963b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4964c98d5d94SLen Brown 			continue;
4965c98d5d94SLen Brown 		}
4966c98d5d94SLen Brown 
49670e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
49680e2d8f05SLen Brown 
49690e2d8f05SLen Brown 		/* get package information */
4970c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4971c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4972c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4973c98d5d94SLen Brown 
49746de68fe1SLen Brown 		/* get die information */
49756de68fe1SLen Brown 		cpus[i].die_id = get_die_id(i);
49766de68fe1SLen Brown 		if (cpus[i].die_id > max_die_id)
49776de68fe1SLen Brown 			max_die_id = cpus[i].die_id;
49786de68fe1SLen Brown 
49790e2d8f05SLen Brown 		/* get numa node information */
4980ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4981ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
4982ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
49830e2d8f05SLen Brown 
49840e2d8f05SLen Brown 		/* get core information */
49850e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
49860e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
49870e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
49880e2d8f05SLen Brown 
49890e2d8f05SLen Brown 		/* get thread information */
49900e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
4991c98d5d94SLen Brown 		if (siblings > max_siblings)
4992c98d5d94SLen Brown 			max_siblings = siblings;
49934f206a0fSArtem Bityutskiy 		if (cpus[i].thread_id == 0)
49948cb48b32SPrarit Bhargava 			topo.num_cores++;
4995c98d5d94SLen Brown 	}
4996ef605741SPrarit Bhargava 
499770a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
4998d8af6f5fSLen Brown 	if (debug > 1)
4999b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
500070a9c6e8SPrarit Bhargava 			max_core_id, topo.cores_per_node);
500170a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
5002812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
5003c98d5d94SLen Brown 
50046de68fe1SLen Brown 	topo.num_die = max_die_id + 1;
50056de68fe1SLen Brown 	if (debug > 1)
50066de68fe1SLen Brown 		fprintf(outf, "max_die_id %d, sizing for %d die\n",
50076de68fe1SLen Brown 				max_die_id, topo.num_die);
50086de68fe1SLen Brown 	if (!summary_only && topo.num_die > 1)
50096de68fe1SLen Brown 		BIC_PRESENT(BIC_Die);
50106de68fe1SLen Brown 
5011c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
5012d8af6f5fSLen Brown 	if (debug > 1)
5013b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
5014c98d5d94SLen Brown 			max_package_id, topo.num_packages);
50157da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
5016812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
5017c98d5d94SLen Brown 
5018ef605741SPrarit Bhargava 	set_node_data();
5019ef605741SPrarit Bhargava 	if (debug > 1)
502070a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
502101235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
502201235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
5023ef605741SPrarit Bhargava 
502470a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
5025d8af6f5fSLen Brown 	if (debug > 1)
5026b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
50272ffbb224SPrarit Bhargava 
50282ffbb224SPrarit Bhargava 	if (debug < 1)
50292ffbb224SPrarit Bhargava 		return;
50302ffbb224SPrarit Bhargava 
50312ffbb224SPrarit Bhargava 	for (i = 0; i <= topo.max_cpu_num; ++i) {
50320ec712e3SLen Brown 		if (cpu_is_not_present(i))
50330ec712e3SLen Brown 			continue;
50342ffbb224SPrarit Bhargava 		fprintf(outf,
50356de68fe1SLen Brown 			"cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
50366de68fe1SLen Brown 			i, cpus[i].physical_package_id, cpus[i].die_id,
50372ffbb224SPrarit Bhargava 			cpus[i].physical_node_id,
50382ffbb224SPrarit Bhargava 			cpus[i].logical_node_id,
50392ffbb224SPrarit Bhargava 			cpus[i].physical_core_id,
50402ffbb224SPrarit Bhargava 			cpus[i].thread_id);
50412ffbb224SPrarit Bhargava 	}
50422ffbb224SPrarit Bhargava 
5043c98d5d94SLen Brown }
5044c98d5d94SLen Brown 
5045c98d5d94SLen Brown void
504640f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c,
504740f5cfe7SPrarit Bhargava 		  struct pkg_data **p)
5048c98d5d94SLen Brown {
5049c98d5d94SLen Brown 	int i;
505040f5cfe7SPrarit Bhargava 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
505140f5cfe7SPrarit Bhargava 			topo.num_packages;
505240f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
5053c98d5d94SLen Brown 
505440f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
5055c98d5d94SLen Brown 	if (*t == NULL)
5056c98d5d94SLen Brown 		goto error;
5057c98d5d94SLen Brown 
505840f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
5059c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
5060c98d5d94SLen Brown 
506140f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
5062c98d5d94SLen Brown 	if (*c == NULL)
5063c98d5d94SLen Brown 		goto error;
5064c98d5d94SLen Brown 
506540f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
5066c98d5d94SLen Brown 		(*c)[i].core_id = -1;
5067c98d5d94SLen Brown 
5068678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
5069c98d5d94SLen Brown 	if (*p == NULL)
5070c98d5d94SLen Brown 		goto error;
5071c98d5d94SLen Brown 
5072c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
5073c98d5d94SLen Brown 		(*p)[i].package_id = i;
5074c98d5d94SLen Brown 
5075c98d5d94SLen Brown 	return;
5076c98d5d94SLen Brown error:
5077b2c95d90SJosh Triplett 	err(1, "calloc counters");
5078c98d5d94SLen Brown }
5079c98d5d94SLen Brown /*
5080c98d5d94SLen Brown  * init_counter()
5081c98d5d94SLen Brown  *
5082c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
5083c98d5d94SLen Brown  */
5084c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
50858cb48b32SPrarit Bhargava 	struct pkg_data *pkg_base, int cpu_id)
5086c98d5d94SLen Brown {
50878cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
508840f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
50898cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
50908cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
5091c98d5d94SLen Brown 	struct thread_data *t;
5092c98d5d94SLen Brown 	struct core_data *c;
5093c98d5d94SLen Brown 	struct pkg_data *p;
5094c98d5d94SLen Brown 
509542dd4520SNathan Ciobanu 
509642dd4520SNathan Ciobanu 	/* Workaround for systems where physical_node_id==-1
509742dd4520SNathan Ciobanu 	 * and logical_node_id==(-1 - topo.num_cpus)
509842dd4520SNathan Ciobanu 	 */
509942dd4520SNathan Ciobanu 	if (node_id < 0)
510042dd4520SNathan Ciobanu 		node_id = 0;
510142dd4520SNathan Ciobanu 
510240f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
510340f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
51048cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
5105c98d5d94SLen Brown 
5106c98d5d94SLen Brown 	t->cpu_id = cpu_id;
51078cb48b32SPrarit Bhargava 	if (thread_id == 0) {
5108c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
5109c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
5110c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
5111c98d5d94SLen Brown 	}
5112c98d5d94SLen Brown 
51138cb48b32SPrarit Bhargava 	c->core_id = core_id;
51148cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
5115c98d5d94SLen Brown }
5116c98d5d94SLen Brown 
5117c98d5d94SLen Brown 
5118c98d5d94SLen Brown int initialize_counters(int cpu_id)
5119c98d5d94SLen Brown {
51208cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
51218cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
5122c98d5d94SLen Brown 	return 0;
5123c98d5d94SLen Brown }
5124c98d5d94SLen Brown 
5125c98d5d94SLen Brown void allocate_output_buffer()
5126c98d5d94SLen Brown {
51273b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
5128c98d5d94SLen Brown 	outp = output_buffer;
5129b2c95d90SJosh Triplett 	if (outp == NULL)
5130b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
5131c98d5d94SLen Brown }
513236229897SLen Brown void allocate_fd_percpu(void)
513336229897SLen Brown {
513401a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
513536229897SLen Brown 	if (fd_percpu == NULL)
513636229897SLen Brown 		err(-1, "calloc fd_percpu");
513736229897SLen Brown }
5138562a2d37SLen Brown void allocate_irq_buffers(void)
5139562a2d37SLen Brown {
5140562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5141562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
5142562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
5143c98d5d94SLen Brown 
514401a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5145562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
514601a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
5147562a2d37SLen Brown }
5148c98d5d94SLen Brown void setup_all_buffers(void)
5149c98d5d94SLen Brown {
5150c98d5d94SLen Brown 	topology_probe();
5151562a2d37SLen Brown 	allocate_irq_buffers();
515236229897SLen Brown 	allocate_fd_percpu();
5153c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
5154c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
5155c98d5d94SLen Brown 	allocate_output_buffer();
5156c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
5157c98d5d94SLen Brown }
51583b4d5c7fSAndy Shevchenko 
51597ce7d5deSPrarit Bhargava void set_base_cpu(void)
51607ce7d5deSPrarit Bhargava {
51617ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
51627ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
51637ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
51647ce7d5deSPrarit Bhargava 
51657ce7d5deSPrarit Bhargava 	if (debug > 1)
5166b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
51677ce7d5deSPrarit Bhargava }
51687ce7d5deSPrarit Bhargava 
5169103a8feaSLen Brown void turbostat_init()
5170103a8feaSLen Brown {
51717ce7d5deSPrarit Bhargava 	setup_all_buffers();
51727ce7d5deSPrarit Bhargava 	set_base_cpu();
5173103a8feaSLen Brown 	check_dev_msr();
517498481e79SLen Brown 	check_permissions();
5175fcd17211SLen Brown 	process_cpuid();
5176103a8feaSLen Brown 
5177103a8feaSLen Brown 
517896e47158SLen Brown 	if (!quiet)
51797f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
51807f5c258eSLen Brown 
518196e47158SLen Brown 	if (!quiet)
5182889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
5183889facbeSLen Brown 
518496e47158SLen Brown 	if (!quiet)
51853a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
51863a9a941dSLen Brown 
518796e47158SLen Brown 	if (!quiet)
5188889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
5189889facbeSLen Brown 
5190889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
5191889facbeSLen Brown 
519296e47158SLen Brown 	if (!quiet)
5193889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
51945a63426eSLen Brown 
519596e47158SLen Brown 	if (!quiet && do_irtl_snb)
51965a63426eSLen Brown 		print_irtl();
5197103a8feaSLen Brown }
5198103a8feaSLen Brown 
5199103a8feaSLen Brown int fork_it(char **argv)
5200103a8feaSLen Brown {
5201103a8feaSLen Brown 	pid_t child_pid;
5202d91bb17cSLen Brown 	int status;
5203d15cf7c1SLen Brown 
5204218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5205d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
52064c2122d4SLen Brown 	first_counter_read = 0;
5207d91bb17cSLen Brown 	if (status)
5208d91bb17cSLen Brown 		exit(status);
5209d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
5210d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5211103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
5212103a8feaSLen Brown 
5213103a8feaSLen Brown 	child_pid = fork();
5214103a8feaSLen Brown 	if (!child_pid) {
5215103a8feaSLen Brown 		/* child */
5216103a8feaSLen Brown 		execvp(argv[0], argv);
52170815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
5218103a8feaSLen Brown 	} else {
5219103a8feaSLen Brown 
5220103a8feaSLen Brown 		/* parent */
5221b2c95d90SJosh Triplett 		if (child_pid == -1)
5222b2c95d90SJosh Triplett 			err(1, "fork");
5223103a8feaSLen Brown 
5224103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
5225103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
5226b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
5227b2c95d90SJosh Triplett 			err(status, "waitpid");
5228103a8feaSLen Brown 	}
5229c98d5d94SLen Brown 	/*
5230c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
5231c98d5d94SLen Brown 	 * because re-starting is problematic when forking
5232c98d5d94SLen Brown 	 */
5233218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5234c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
5235103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
5236103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
5237ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5238ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
5239ba3dec99SLen Brown 	else {
5240c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
5241c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
5242ba3dec99SLen Brown 	}
5243103a8feaSLen Brown 
5244b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5245b7d8c148SLen Brown 
5246b7d8c148SLen Brown 	flush_output_stderr();
5247103a8feaSLen Brown 
5248d91bb17cSLen Brown 	return status;
5249103a8feaSLen Brown }
5250103a8feaSLen Brown 
52513b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
52523b4d5c7fSAndy Shevchenko {
52533b4d5c7fSAndy Shevchenko 	int status;
52543b4d5c7fSAndy Shevchenko 
5255218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
52563b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
52573b4d5c7fSAndy Shevchenko 	if (status)
52583b4d5c7fSAndy Shevchenko 		return status;
52593b4d5c7fSAndy Shevchenko 
52603b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
52613b4d5c7fSAndy Shevchenko 	if (status)
52623b4d5c7fSAndy Shevchenko 		return status;
52633b4d5c7fSAndy Shevchenko 
5264b7d8c148SLen Brown 	flush_output_stdout();
52653b4d5c7fSAndy Shevchenko 
52663b4d5c7fSAndy Shevchenko 	return status;
52673b4d5c7fSAndy Shevchenko }
52683b4d5c7fSAndy Shevchenko 
5269d8af6f5fSLen Brown void print_version() {
5270538c48f2SLen Brown 	fprintf(outf, "turbostat version 18.07.27"
5271d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
5272d8af6f5fSLen Brown }
5273d8af6f5fSLen Brown 
5274495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
5275495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
527641618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
5277388e9c81SLen Brown {
5278388e9c81SLen Brown 	struct msr_counter *msrp;
5279388e9c81SLen Brown 
5280388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
5281388e9c81SLen Brown 	if (msrp == NULL) {
5282388e9c81SLen Brown 		perror("calloc");
5283388e9c81SLen Brown 		exit(1);
5284388e9c81SLen Brown 	}
5285388e9c81SLen Brown 
5286388e9c81SLen Brown 	msrp->msr_num = msr_num;
5287388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
5288495c7654SLen Brown 	if (path)
5289495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
5290388e9c81SLen Brown 	msrp->width = width;
5291388e9c81SLen Brown 	msrp->type = type;
5292388e9c81SLen Brown 	msrp->format = format;
529341618e63SLen Brown 	msrp->flags = flags;
5294388e9c81SLen Brown 
5295388e9c81SLen Brown 	switch (scope) {
5296388e9c81SLen Brown 
5297388e9c81SLen Brown 	case SCOPE_CPU:
5298388e9c81SLen Brown 		msrp->next = sys.tp;
5299388e9c81SLen Brown 		sys.tp = msrp;
5300678a3bd1SLen Brown 		sys.added_thread_counters++;
53010748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5302678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
5303678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5304678a3bd1SLen Brown 			exit(-1);
5305678a3bd1SLen Brown 		}
5306388e9c81SLen Brown 		break;
5307388e9c81SLen Brown 
5308388e9c81SLen Brown 	case SCOPE_CORE:
5309388e9c81SLen Brown 		msrp->next = sys.cp;
5310388e9c81SLen Brown 		sys.cp = msrp;
5311678a3bd1SLen Brown 		sys.added_core_counters++;
5312678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5313678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
5314678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5315678a3bd1SLen Brown 			exit(-1);
5316678a3bd1SLen Brown 		}
5317388e9c81SLen Brown 		break;
5318388e9c81SLen Brown 
5319388e9c81SLen Brown 	case SCOPE_PACKAGE:
5320388e9c81SLen Brown 		msrp->next = sys.pp;
5321388e9c81SLen Brown 		sys.pp = msrp;
5322678a3bd1SLen Brown 		sys.added_package_counters++;
5323678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5324678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
5325678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5326678a3bd1SLen Brown 			exit(-1);
5327678a3bd1SLen Brown 		}
5328388e9c81SLen Brown 		break;
5329388e9c81SLen Brown 	}
5330388e9c81SLen Brown 
5331388e9c81SLen Brown 	return 0;
5332388e9c81SLen Brown }
5333388e9c81SLen Brown 
5334388e9c81SLen Brown void parse_add_command(char *add_command)
5335388e9c81SLen Brown {
5336388e9c81SLen Brown 	int msr_num = 0;
5337495c7654SLen Brown 	char *path = NULL;
53380f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
5339388e9c81SLen Brown 	int width = 64;
5340388e9c81SLen Brown 	int fail = 0;
5341388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
5342388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
5343388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
5344388e9c81SLen Brown 
5345388e9c81SLen Brown 	while (add_command) {
5346388e9c81SLen Brown 
5347388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5348388e9c81SLen Brown 			goto next;
5349388e9c81SLen Brown 
5350388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
5351388e9c81SLen Brown 			goto next;
5352388e9c81SLen Brown 
5353495c7654SLen Brown 		if (*add_command == '/') {
5354495c7654SLen Brown 			path = add_command;
5355495c7654SLen Brown 			goto next;
5356495c7654SLen Brown 		}
5357495c7654SLen Brown 
5358388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
5359388e9c81SLen Brown 			if ((width == 32) || (width == 64))
5360388e9c81SLen Brown 				goto next;
5361388e9c81SLen Brown 			width = 64;
5362388e9c81SLen Brown 		}
5363388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5364388e9c81SLen Brown 			scope = SCOPE_CPU;
5365388e9c81SLen Brown 			goto next;
5366388e9c81SLen Brown 		}
5367388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
5368388e9c81SLen Brown 			scope = SCOPE_CORE;
5369388e9c81SLen Brown 			goto next;
5370388e9c81SLen Brown 		}
5371388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
5372388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
5373388e9c81SLen Brown 			goto next;
5374388e9c81SLen Brown 		}
5375388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5376388e9c81SLen Brown 			type = COUNTER_CYCLES;
5377388e9c81SLen Brown 			goto next;
5378388e9c81SLen Brown 		}
5379388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5380388e9c81SLen Brown 			type = COUNTER_SECONDS;
5381388e9c81SLen Brown 			goto next;
5382388e9c81SLen Brown 		}
538341618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
538441618e63SLen Brown 			type = COUNTER_USEC;
538541618e63SLen Brown 			goto next;
538641618e63SLen Brown 		}
5387388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
5388388e9c81SLen Brown 			format = FORMAT_RAW;
5389388e9c81SLen Brown 			goto next;
5390388e9c81SLen Brown 		}
5391388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
5392388e9c81SLen Brown 			format = FORMAT_DELTA;
5393388e9c81SLen Brown 			goto next;
5394388e9c81SLen Brown 		}
5395388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
5396388e9c81SLen Brown 			format = FORMAT_PERCENT;
5397388e9c81SLen Brown 			goto next;
5398388e9c81SLen Brown 		}
5399388e9c81SLen Brown 
5400388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
5401388e9c81SLen Brown 			char *eos;
5402388e9c81SLen Brown 
5403388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
5404388e9c81SLen Brown 			if (eos)
5405388e9c81SLen Brown 				*eos = '\0';
5406388e9c81SLen Brown 			goto next;
5407388e9c81SLen Brown 		}
5408388e9c81SLen Brown 
5409388e9c81SLen Brown next:
5410388e9c81SLen Brown 		add_command = strchr(add_command, ',');
5411495c7654SLen Brown 		if (add_command) {
5412495c7654SLen Brown 			*add_command = '\0';
5413388e9c81SLen Brown 			add_command++;
5414495c7654SLen Brown 		}
5415388e9c81SLen Brown 
5416388e9c81SLen Brown 	}
5417495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
5418495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
5419388e9c81SLen Brown 		fail++;
5420388e9c81SLen Brown 	}
5421388e9c81SLen Brown 
5422388e9c81SLen Brown 	/* generate default column header */
5423388e9c81SLen Brown 	if (*name_buffer == '\0') {
5424388e9c81SLen Brown 		if (width == 32)
54255f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5426388e9c81SLen Brown 		else
54275f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5428388e9c81SLen Brown 	}
5429388e9c81SLen Brown 
543041618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
5431388e9c81SLen Brown 		fail++;
5432388e9c81SLen Brown 
5433388e9c81SLen Brown 	if (fail) {
5434388e9c81SLen Brown 		help();
5435388e9c81SLen Brown 		exit(1);
5436388e9c81SLen Brown 	}
5437388e9c81SLen Brown }
543841618e63SLen Brown 
5439dd778a5eSLen Brown int is_deferred_skip(char *name)
5440dd778a5eSLen Brown {
5441dd778a5eSLen Brown 	int i;
5442dd778a5eSLen Brown 
5443dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
5444dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
5445dd778a5eSLen Brown 			return 1;
5446dd778a5eSLen Brown 	return 0;
5447dd778a5eSLen Brown }
5448dd778a5eSLen Brown 
544941618e63SLen Brown void probe_sysfs(void)
545041618e63SLen Brown {
545141618e63SLen Brown 	char path[64];
545241618e63SLen Brown 	char name_buf[16];
545341618e63SLen Brown 	FILE *input;
545441618e63SLen Brown 	int state;
545541618e63SLen Brown 	char *sp;
545641618e63SLen Brown 
545741618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
545841618e63SLen Brown 		return;
545941618e63SLen Brown 
54600748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
546141618e63SLen Brown 
546241618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
546341618e63SLen Brown 			base_cpu, state);
546441618e63SLen Brown 		input = fopen(path, "r");
546541618e63SLen Brown 		if (input == NULL)
546641618e63SLen Brown 			continue;
546741618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
546841618e63SLen Brown 
546941618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
547041618e63SLen Brown 		sp = strchr(name_buf, '-');
547141618e63SLen Brown 		if (!sp)
547241618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
547341618e63SLen Brown 		*sp = '%';
547441618e63SLen Brown 		*(sp + 1) = '\0';
547541618e63SLen Brown 
547641618e63SLen Brown 		fclose(input);
547741618e63SLen Brown 
547841618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
547941618e63SLen Brown 
5480dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5481dd778a5eSLen Brown 			continue;
5482dd778a5eSLen Brown 
548341618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
548441618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
548541618e63SLen Brown 	}
548641618e63SLen Brown 
54870748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
548841618e63SLen Brown 
548941618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
549041618e63SLen Brown 			base_cpu, state);
549141618e63SLen Brown 		input = fopen(path, "r");
549241618e63SLen Brown 		if (input == NULL)
549341618e63SLen Brown 			continue;
549441618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
549541618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
549641618e63SLen Brown 		sp = strchr(name_buf, '-');
549741618e63SLen Brown 		if (!sp)
549841618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
549941618e63SLen Brown 		*sp = '\0';
550041618e63SLen Brown 		fclose(input);
550141618e63SLen Brown 
550241618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
550341618e63SLen Brown 
5504dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5505dd778a5eSLen Brown 			continue;
5506dd778a5eSLen Brown 
550741618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
550841618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
550941618e63SLen Brown 	}
551041618e63SLen Brown 
551141618e63SLen Brown }
551241618e63SLen Brown 
55131ef7d21aSLen Brown 
55141ef7d21aSLen Brown /*
55151ef7d21aSLen Brown  * parse cpuset with following syntax
55161ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
55171ef7d21aSLen Brown  */
55181ef7d21aSLen Brown void parse_cpu_command(char *optarg)
55191ef7d21aSLen Brown {
55201ef7d21aSLen Brown 	unsigned int start, end;
55211ef7d21aSLen Brown 	char *next;
55221ef7d21aSLen Brown 
55234e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
55244e4e1e7cSLen Brown 		if (cpu_subset)
55254e4e1e7cSLen Brown 			goto error;
55264e4e1e7cSLen Brown 		show_core_only++;
55274e4e1e7cSLen Brown 		return;
55284e4e1e7cSLen Brown 	}
55294e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
55304e4e1e7cSLen Brown 		if (cpu_subset)
55314e4e1e7cSLen Brown 			goto error;
55324e4e1e7cSLen Brown 		show_pkg_only++;
55334e4e1e7cSLen Brown 		return;
55344e4e1e7cSLen Brown 	}
55354e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
55364e4e1e7cSLen Brown 		goto error;
55374e4e1e7cSLen Brown 
55381ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
55391ef7d21aSLen Brown 	if (cpu_subset == NULL)
55401ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
55411ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
55421ef7d21aSLen Brown 
55431ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
55441ef7d21aSLen Brown 
55451ef7d21aSLen Brown 	next = optarg;
55461ef7d21aSLen Brown 
55471ef7d21aSLen Brown 	while (next && *next) {
55481ef7d21aSLen Brown 
55491ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
55501ef7d21aSLen Brown 			goto error;
55511ef7d21aSLen Brown 
55521ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
55531ef7d21aSLen Brown 
55541ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
55551ef7d21aSLen Brown 			goto error;
55561ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
55571ef7d21aSLen Brown 
55581ef7d21aSLen Brown 		if (*next == '\0')
55591ef7d21aSLen Brown 			break;
55601ef7d21aSLen Brown 
55611ef7d21aSLen Brown 		if (*next == ',') {
55621ef7d21aSLen Brown 			next += 1;
55631ef7d21aSLen Brown 			continue;
55641ef7d21aSLen Brown 		}
55651ef7d21aSLen Brown 
55661ef7d21aSLen Brown 		if (*next == '-') {
55671ef7d21aSLen Brown 			next += 1;	/* start range */
55681ef7d21aSLen Brown 		} else if (*next == '.') {
55691ef7d21aSLen Brown 			next += 1;
55701ef7d21aSLen Brown 			if (*next == '.')
55711ef7d21aSLen Brown 				next += 1;	/* start range */
55721ef7d21aSLen Brown 			else
55731ef7d21aSLen Brown 				goto error;
55741ef7d21aSLen Brown 		}
55751ef7d21aSLen Brown 
55761ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
55771ef7d21aSLen Brown 		if (end <= start)
55781ef7d21aSLen Brown 			goto error;
55791ef7d21aSLen Brown 
55801ef7d21aSLen Brown 		while (++start <= end) {
55811ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
55821ef7d21aSLen Brown 				goto error;
55831ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
55841ef7d21aSLen Brown 		}
55851ef7d21aSLen Brown 
55861ef7d21aSLen Brown 		if (*next == ',')
55871ef7d21aSLen Brown 			next += 1;
55881ef7d21aSLen Brown 		else if (*next != '\0')
55891ef7d21aSLen Brown 			goto error;
55901ef7d21aSLen Brown 	}
55911ef7d21aSLen Brown 
55921ef7d21aSLen Brown 	return;
55931ef7d21aSLen Brown 
55941ef7d21aSLen Brown error:
55954e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
55964e4e1e7cSLen Brown 	help();
55971ef7d21aSLen Brown 	exit(-1);
55981ef7d21aSLen Brown }
55991ef7d21aSLen Brown 
5600812db3f7SLen Brown 
5601103a8feaSLen Brown void cmdline(int argc, char **argv)
5602103a8feaSLen Brown {
5603103a8feaSLen Brown 	int opt;
5604d8af6f5fSLen Brown 	int option_index = 0;
5605d8af6f5fSLen Brown 	static struct option long_options[] = {
5606388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
56071ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5608d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
560996e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
56103f44a5c6SLen Brown 		{"enable",	required_argument,	0, 'e'},
5611d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5612023fe0acSChen Yu 		{"num_iterations",	required_argument,	0, 'n'},
5613d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5614812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5615d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5616c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5617b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
561896e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5619812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5620d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5621d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5622d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5623d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5624d8af6f5fSLen Brown 	};
5625103a8feaSLen Brown 
5626103a8feaSLen Brown 	progname = argv[0];
5627103a8feaSLen Brown 
5628023fe0acSChen Yu 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
5629d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5630103a8feaSLen Brown 		switch (opt) {
5631388e9c81SLen Brown 		case 'a':
5632388e9c81SLen Brown 			parse_add_command(optarg);
5633388e9c81SLen Brown 			break;
56341ef7d21aSLen Brown 		case 'c':
56351ef7d21aSLen Brown 			parse_cpu_command(optarg);
56361ef7d21aSLen Brown 			break;
5637d8af6f5fSLen Brown 		case 'D':
5638d8af6f5fSLen Brown 			dump_only++;
56398e180f3cSLen Brown 			break;
56403f44a5c6SLen Brown 		case 'e':
56413f44a5c6SLen Brown 			/* --enable specified counter */
56424c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
56433f44a5c6SLen Brown 			break;
5644d8af6f5fSLen Brown 		case 'd':
5645d8af6f5fSLen Brown 			debug++;
56463f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
56472f32edf1SLen Brown 			break;
5648812db3f7SLen Brown 		case 'H':
56493f44a5c6SLen Brown 			/*
56503f44a5c6SLen Brown 			 * --hide: do not show those specified
56513f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
56523f44a5c6SLen Brown 			 */
56533f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5654812db3f7SLen Brown 			break;
5655d8af6f5fSLen Brown 		case 'h':
5656d8af6f5fSLen Brown 		default:
5657d8af6f5fSLen Brown 			help();
5658d8af6f5fSLen Brown 			exit(1);
5659d8af6f5fSLen Brown 		case 'i':
56602a0609c0SLen Brown 			{
56612a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
56622a0609c0SLen Brown 
56632a0609c0SLen Brown 				if (interval < 0.001) {
5664b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
56652a0609c0SLen Brown 						interval);
56662a0609c0SLen Brown 					exit(2);
56672a0609c0SLen Brown 				}
56682a0609c0SLen Brown 
566947936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
5670b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
567147936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
56722a0609c0SLen Brown 			}
5673889facbeSLen Brown 			break;
56745c56be9aSDirk Brandewie 		case 'J':
56755c56be9aSDirk Brandewie 			rapl_joules++;
56765c56be9aSDirk Brandewie 			break;
5677c8ade361SLen Brown 		case 'l':
56783f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5679c8ade361SLen Brown 			list_header_only++;
5680c8ade361SLen Brown 			quiet++;
5681c8ade361SLen Brown 			break;
5682b7d8c148SLen Brown 		case 'o':
5683b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5684b7d8c148SLen Brown 			break;
568596e47158SLen Brown 		case 'q':
568696e47158SLen Brown 			quiet = 1;
568796e47158SLen Brown 			break;
5688023fe0acSChen Yu 		case 'n':
5689023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
5690023fe0acSChen Yu 
5691023fe0acSChen Yu 			if (num_iterations <= 0) {
5692023fe0acSChen Yu 				fprintf(outf, "iterations %d should be positive number\n",
5693023fe0acSChen Yu 					num_iterations);
5694023fe0acSChen Yu 				exit(2);
5695023fe0acSChen Yu 			}
5696023fe0acSChen Yu 			break;
5697812db3f7SLen Brown 		case 's':
56983f44a5c6SLen Brown 			/*
56993f44a5c6SLen Brown 			 * --show: show only those specified
57003f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
57013f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
57023f44a5c6SLen Brown 			 */
57033f44a5c6SLen Brown 			if (shown == 0)
57043f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
57053f44a5c6SLen Brown 			else
57063f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
57073f44a5c6SLen Brown 			shown = 1;
5708812db3f7SLen Brown 			break;
5709d8af6f5fSLen Brown 		case 'S':
5710d8af6f5fSLen Brown 			summary_only++;
5711d8af6f5fSLen Brown 			break;
5712d8af6f5fSLen Brown 		case 'T':
5713d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5714d8af6f5fSLen Brown 			break;
5715d8af6f5fSLen Brown 		case 'v':
5716d8af6f5fSLen Brown 			print_version();
5717d8af6f5fSLen Brown 			exit(0);
5718d8af6f5fSLen Brown 			break;
5719103a8feaSLen Brown 		}
5720103a8feaSLen Brown 	}
5721103a8feaSLen Brown }
5722103a8feaSLen Brown 
5723103a8feaSLen Brown int main(int argc, char **argv)
5724103a8feaSLen Brown {
5725b7d8c148SLen Brown 	outf = stderr;
5726103a8feaSLen Brown 	cmdline(argc, argv);
5727103a8feaSLen Brown 
572896e47158SLen Brown 	if (!quiet)
5729d8af6f5fSLen Brown 		print_version();
5730103a8feaSLen Brown 
573141618e63SLen Brown 	probe_sysfs();
573241618e63SLen Brown 
5733103a8feaSLen Brown 	turbostat_init();
5734103a8feaSLen Brown 
57353b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
57363b4d5c7fSAndy Shevchenko 	if (dump_only)
57373b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
57383b4d5c7fSAndy Shevchenko 
5739c8ade361SLen Brown 	/* list header and exit */
5740c8ade361SLen Brown 	if (list_header_only) {
5741c8ade361SLen Brown 		print_header(",");
5742c8ade361SLen Brown 		flush_output_stdout();
5743c8ade361SLen Brown 		return 0;
5744c8ade361SLen Brown 	}
5745c8ade361SLen Brown 
5746103a8feaSLen Brown 	/*
5747103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5748103a8feaSLen Brown 	 */
5749103a8feaSLen Brown 	if (argc - optind)
5750103a8feaSLen Brown 		return fork_it(argv + optind);
5751103a8feaSLen Brown 	else
5752103a8feaSLen Brown 		turbostat_loop();
5753103a8feaSLen Brown 
5754103a8feaSLen Brown 	return 0;
5755103a8feaSLen Brown }
5756