xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision f26b151977447be3b86f92c91e1caedc9b5eb8bf)
1103a8feaSLen Brown /*
2103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
3103a8feaSLen Brown  * on modern Intel turbo-capable processors.
4103a8feaSLen Brown  *
5144b44b1SLen Brown  * Copyright (c) 2013 Intel Corporation.
6103a8feaSLen Brown  * Len Brown <len.brown@intel.com>
7103a8feaSLen Brown  *
8103a8feaSLen Brown  * This program is free software; you can redistribute it and/or modify it
9103a8feaSLen Brown  * under the terms and conditions of the GNU General Public License,
10103a8feaSLen Brown  * version 2, as published by the Free Software Foundation.
11103a8feaSLen Brown  *
12103a8feaSLen Brown  * This program is distributed in the hope it will be useful, but WITHOUT
13103a8feaSLen Brown  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14103a8feaSLen Brown  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15103a8feaSLen Brown  * more details.
16103a8feaSLen Brown  *
17103a8feaSLen Brown  * You should have received a copy of the GNU General Public License along with
18103a8feaSLen Brown  * this program; if not, write to the Free Software Foundation, Inc.,
19103a8feaSLen Brown  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20103a8feaSLen Brown  */
21103a8feaSLen Brown 
2288c3281fSLen Brown #define _GNU_SOURCE
23b731f311SJosh Triplett #include MSRHEADER
24869ce69eSLen Brown #include INTEL_FAMILY_HEADER
2595aebc44SJosh Triplett #include <stdarg.h>
26103a8feaSLen Brown #include <stdio.h>
27b2c95d90SJosh Triplett #include <err.h>
28103a8feaSLen Brown #include <unistd.h>
29103a8feaSLen Brown #include <sys/types.h>
30103a8feaSLen Brown #include <sys/wait.h>
31103a8feaSLen Brown #include <sys/stat.h>
32103a8feaSLen Brown #include <sys/resource.h>
33103a8feaSLen Brown #include <fcntl.h>
34103a8feaSLen Brown #include <signal.h>
35103a8feaSLen Brown #include <sys/time.h>
36103a8feaSLen Brown #include <stdlib.h>
37d8af6f5fSLen Brown #include <getopt.h>
38103a8feaSLen Brown #include <dirent.h>
39103a8feaSLen Brown #include <string.h>
40103a8feaSLen Brown #include <ctype.h>
4188c3281fSLen Brown #include <sched.h>
422a0609c0SLen Brown #include <time.h>
432b92865eSJosh Triplett #include <cpuid.h>
4498481e79SLen Brown #include <linux/capability.h>
4598481e79SLen Brown #include <errno.h>
46103a8feaSLen Brown 
47103a8feaSLen Brown char *proc_stat = "/proc/stat";
48b7d8c148SLen Brown FILE *outf;
4936229897SLen Brown int *fd_percpu;
502a0609c0SLen Brown struct timespec interval_ts = {5, 0};
51d8af6f5fSLen Brown unsigned int debug;
5296e47158SLen Brown unsigned int quiet;
530de6c0dfSLen Brown unsigned int sums_need_wide_columns;
54d8af6f5fSLen Brown unsigned int rapl_joules;
55d8af6f5fSLen Brown unsigned int summary_only;
56c8ade361SLen Brown unsigned int list_header_only;
57d8af6f5fSLen Brown unsigned int dump_only;
58103a8feaSLen Brown unsigned int do_snb_cstates;
59fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
60144b44b1SLen Brown unsigned int do_slm_cstates;
61144b44b1SLen Brown unsigned int use_c1_residency_msr;
62103a8feaSLen Brown unsigned int has_aperf;
63889facbeSLen Brown unsigned int has_epb;
645a63426eSLen Brown unsigned int do_irtl_snb;
655a63426eSLen Brown unsigned int do_irtl_hsw;
66fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
67103a8feaSLen Brown unsigned int genuine_intel;
68103a8feaSLen Brown unsigned int has_invariant_tsc;
69d7899447SLen Brown unsigned int do_nhm_platform_info;
70cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
71b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
72103a8feaSLen Brown double bclk;
73a2b7b749SLen Brown double base_hz;
7421ed5574SLen Brown unsigned int has_base_hz;
75a2b7b749SLen Brown double tsc_tweak = 1.0;
76c98d5d94SLen Brown unsigned int show_pkg_only;
77c98d5d94SLen Brown unsigned int show_core_only;
78c98d5d94SLen Brown char *output_buffer, *outp;
79889facbeSLen Brown unsigned int do_rapl;
80889facbeSLen Brown unsigned int do_dts;
81889facbeSLen Brown unsigned int do_ptm;
82fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
8327d47356SLen Brown unsigned int gfx_cur_mhz;
84889facbeSLen Brown unsigned int tcc_activation_temp;
85889facbeSLen Brown unsigned int tcc_activation_temp_override;
8640ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
8740ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
88889facbeSLen Brown double rapl_joule_counter_range;
893a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
903a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
913a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
928a5bdf41SLen Brown unsigned int crystal_hz;
938a5bdf41SLen Brown unsigned long long tsc_hz;
947ce7d5deSPrarit Bhargava int base_cpu;
95c91fc851SLen Brown int do_migrate;
9621ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
977f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
987f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
997f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
1007f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1017f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1027f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
10333148d67SLen Brown unsigned int has_misc_feature_control;
104889facbeSLen Brown 
105889facbeSLen Brown #define RAPL_PKG		(1 << 0)
106e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
107e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
108e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
109e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
110e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
111e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
112e6f9bb3cSLen Brown 
113889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
114e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
115e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
116e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
117e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1180b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1190b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
120e6f9bb3cSLen Brown 
1219148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
122e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1230b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
124e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
125e6f9bb3cSLen Brown 
1260b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
127e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
128e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
129e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1309148494cSJacob Pan 
1319148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1329148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1339148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
134889facbeSLen Brown #define	TJMAX_DEFAULT	100
135889facbeSLen Brown 
136889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
137103a8feaSLen Brown 
138388e9c81SLen Brown /*
139388e9c81SLen Brown  * buffer size used by sscanf() for added column names
140388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
141388e9c81SLen Brown  */
142388e9c81SLen Brown #define	NAME_BYTES 20
143495c7654SLen Brown #define PATH_BYTES 128
144388e9c81SLen Brown 
145103a8feaSLen Brown int backwards_count;
146103a8feaSLen Brown char *progname;
147103a8feaSLen Brown 
1481ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
1491ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
1501ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
151678a3bd1SLen Brown #define MAX_ADDED_COUNTERS 16
152c98d5d94SLen Brown 
153c98d5d94SLen Brown struct thread_data {
154f4fdf2b4SLen Brown 	struct timeval tv_begin;
155f4fdf2b4SLen Brown 	struct timeval tv_end;
156c98d5d94SLen Brown 	unsigned long long tsc;
157c98d5d94SLen Brown 	unsigned long long aperf;
158c98d5d94SLen Brown 	unsigned long long mperf;
159144b44b1SLen Brown 	unsigned long long c1;
1600de6c0dfSLen Brown 	unsigned long long  irq_count;
1611ed51011SLen Brown 	unsigned int smi_count;
162c98d5d94SLen Brown 	unsigned int cpu_id;
163c98d5d94SLen Brown 	unsigned int flags;
164c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
165c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
166678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
167c98d5d94SLen Brown } *thread_even, *thread_odd;
168c98d5d94SLen Brown 
169c98d5d94SLen Brown struct core_data {
170c98d5d94SLen Brown 	unsigned long long c3;
171c98d5d94SLen Brown 	unsigned long long c6;
172c98d5d94SLen Brown 	unsigned long long c7;
1730539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
174889facbeSLen Brown 	unsigned int core_temp_c;
175c98d5d94SLen Brown 	unsigned int core_id;
176678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
177c98d5d94SLen Brown } *core_even, *core_odd;
178c98d5d94SLen Brown 
179c98d5d94SLen Brown struct pkg_data {
180c98d5d94SLen Brown 	unsigned long long pc2;
181c98d5d94SLen Brown 	unsigned long long pc3;
182c98d5d94SLen Brown 	unsigned long long pc6;
183c98d5d94SLen Brown 	unsigned long long pc7;
184ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
185ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
186ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1870b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1880b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1890b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1900b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
1919185e988SLen Brown 	long long gfx_rc6_ms;
19227d47356SLen Brown 	unsigned int gfx_mhz;
193c98d5d94SLen Brown 	unsigned int package_id;
194889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
195889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
196889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
197889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
198889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
199889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
200889facbeSLen Brown 	unsigned int pkg_temp_c;
201678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
202c98d5d94SLen Brown } *package_even, *package_odd;
203c98d5d94SLen Brown 
204c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
205c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
206c98d5d94SLen Brown 
207c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
208c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
209c98d5d94SLen Brown 		topo.num_threads_per_core + \
210c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
211c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
212c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
213c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
214c98d5d94SLen Brown 
215388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
21641618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
217388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
218388e9c81SLen Brown 
219388e9c81SLen Brown struct msr_counter {
220388e9c81SLen Brown 	unsigned int msr_num;
221388e9c81SLen Brown 	char name[NAME_BYTES];
222495c7654SLen Brown 	char path[PATH_BYTES];
223388e9c81SLen Brown 	unsigned int width;
224388e9c81SLen Brown 	enum counter_type type;
225388e9c81SLen Brown 	enum counter_format format;
226388e9c81SLen Brown 	struct msr_counter *next;
227812db3f7SLen Brown 	unsigned int flags;
228812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
229812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
23041618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
231388e9c81SLen Brown };
232388e9c81SLen Brown 
233388e9c81SLen Brown struct sys_counters {
234678a3bd1SLen Brown 	unsigned int added_thread_counters;
235678a3bd1SLen Brown 	unsigned int added_core_counters;
236678a3bd1SLen Brown 	unsigned int added_package_counters;
237388e9c81SLen Brown 	struct msr_counter *tp;
238388e9c81SLen Brown 	struct msr_counter *cp;
239388e9c81SLen Brown 	struct msr_counter *pp;
240388e9c81SLen Brown } sys;
241388e9c81SLen Brown 
242c98d5d94SLen Brown struct system_summary {
243c98d5d94SLen Brown 	struct thread_data threads;
244c98d5d94SLen Brown 	struct core_data cores;
245c98d5d94SLen Brown 	struct pkg_data packages;
246388e9c81SLen Brown } average;
247c98d5d94SLen Brown 
248c98d5d94SLen Brown 
249c98d5d94SLen Brown struct topo_params {
250c98d5d94SLen Brown 	int num_packages;
251103a8feaSLen Brown 	int num_cpus;
252c98d5d94SLen Brown 	int num_cores;
253c98d5d94SLen Brown 	int max_cpu_num;
254c98d5d94SLen Brown 	int num_cores_per_pkg;
255c98d5d94SLen Brown 	int num_threads_per_core;
256c98d5d94SLen Brown } topo;
257103a8feaSLen Brown 
258c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
259103a8feaSLen Brown 
260562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
261562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
262562a2d37SLen Brown 
263c98d5d94SLen Brown void setup_all_buffers(void);
264103a8feaSLen Brown 
265c98d5d94SLen Brown int cpu_is_not_present(int cpu)
266d15cf7c1SLen Brown {
267c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
268c98d5d94SLen Brown }
269c98d5d94SLen Brown /*
270c98d5d94SLen Brown  * run func(thread, core, package) in topology order
271c98d5d94SLen Brown  * skip non-present cpus
272c98d5d94SLen Brown  */
273c98d5d94SLen Brown 
274c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
275c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
276c98d5d94SLen Brown {
277c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
278c98d5d94SLen Brown 
279c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
280c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
281c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
282c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
283c98d5d94SLen Brown 				struct thread_data *t;
284c98d5d94SLen Brown 				struct core_data *c;
285c98d5d94SLen Brown 				struct pkg_data *p;
286c98d5d94SLen Brown 
287c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
288c98d5d94SLen Brown 
289c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
290c98d5d94SLen Brown 					continue;
291c98d5d94SLen Brown 
292c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
293c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
294c98d5d94SLen Brown 
295c98d5d94SLen Brown 				retval = func(t, c, p);
296c98d5d94SLen Brown 				if (retval)
297c98d5d94SLen Brown 					return retval;
298c98d5d94SLen Brown 			}
299c98d5d94SLen Brown 		}
300c98d5d94SLen Brown 	}
301d15cf7c1SLen Brown 	return 0;
302d15cf7c1SLen Brown }
303d15cf7c1SLen Brown 
30488c3281fSLen Brown int cpu_migrate(int cpu)
30588c3281fSLen Brown {
306c91fc851SLen Brown 	if (!do_migrate)
307c91fc851SLen Brown 		return 0;
308c91fc851SLen Brown 
309c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
310c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
311c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
31288c3281fSLen Brown 		return -1;
31388c3281fSLen Brown 	else
31488c3281fSLen Brown 		return 0;
31588c3281fSLen Brown }
31636229897SLen Brown int get_msr_fd(int cpu)
317103a8feaSLen Brown {
318103a8feaSLen Brown 	char pathname[32];
319103a8feaSLen Brown 	int fd;
320103a8feaSLen Brown 
32136229897SLen Brown 	fd = fd_percpu[cpu];
32236229897SLen Brown 
32336229897SLen Brown 	if (fd)
32436229897SLen Brown 		return fd;
32536229897SLen Brown 
326103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
327103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
32815aaa346SLen Brown 	if (fd < 0)
32998481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
330103a8feaSLen Brown 
33136229897SLen Brown 	fd_percpu[cpu] = fd;
33236229897SLen Brown 
33336229897SLen Brown 	return fd;
33436229897SLen Brown }
33536229897SLen Brown 
33636229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
33736229897SLen Brown {
33836229897SLen Brown 	ssize_t retval;
33936229897SLen Brown 
34036229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
34115aaa346SLen Brown 
34298481e79SLen Brown 	if (retval != sizeof *msr)
343cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
34415aaa346SLen Brown 
34515aaa346SLen Brown 	return 0;
346103a8feaSLen Brown }
347103a8feaSLen Brown 
348fc04cc67SLen Brown /*
349812db3f7SLen Brown  * Each string in this array is compared in --show and --hide cmdline.
350812db3f7SLen Brown  * Thus, strings that are proper sub-sets must follow their more specific peers.
351fc04cc67SLen Brown  */
352812db3f7SLen Brown struct msr_counter bic[] = {
353812db3f7SLen Brown 	{ 0x0, "Package" },
354812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
355812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
356812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
357812db3f7SLen Brown 	{ 0x0, "IRQ" },
358495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
359812db3f7SLen Brown 	{ 0x0, "Busy%" },
360812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
361812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
362812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
363812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
364812db3f7SLen Brown 	{ 0x0, "ThreadC" },
365812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
366812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
367812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
368812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
369812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
370812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
371812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
372812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
373812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
3740f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
3750f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
3760f47c08dSLen Brown 	{ 0x0, "Pkg%pc10" },
377812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
378812db3f7SLen Brown 	{ 0x0, "CorWatt" },
379812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
380812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
381812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
382812db3f7SLen Brown 	{ 0x0, "PKG_%" },
383812db3f7SLen Brown 	{ 0x0, "RAM_%" },
384812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
385812db3f7SLen Brown 	{ 0x0, "Cor_J" },
386812db3f7SLen Brown 	{ 0x0, "GFX_J" },
387812db3f7SLen Brown 	{ 0x0, "RAM_J" },
388812db3f7SLen Brown 	{ 0x0, "Core" },
389812db3f7SLen Brown 	{ 0x0, "CPU" },
3900539ba11SLen Brown 	{ 0x0, "Mod%c6" },
39141618e63SLen Brown 	{ 0x0, "sysfs" },
392a99d8730SLen Brown 	{ 0x0, "Totl%C0" },
393a99d8730SLen Brown 	{ 0x0, "Any%C0" },
394a99d8730SLen Brown 	{ 0x0, "GFX%C0" },
395a99d8730SLen Brown 	{ 0x0, "CPUGFX%" },
396812db3f7SLen Brown };
397812db3f7SLen Brown 
398a99d8730SLen Brown 
399a99d8730SLen Brown 
400812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
401812db3f7SLen Brown #define	BIC_Package	(1ULL << 0)
402812db3f7SLen Brown #define	BIC_Avg_MHz	(1ULL << 1)
403812db3f7SLen Brown #define	BIC_Bzy_MHz	(1ULL << 2)
404812db3f7SLen Brown #define	BIC_TSC_MHz	(1ULL << 3)
405812db3f7SLen Brown #define	BIC_IRQ		(1ULL << 4)
406812db3f7SLen Brown #define	BIC_SMI		(1ULL << 5)
407812db3f7SLen Brown #define	BIC_Busy	(1ULL << 6)
408812db3f7SLen Brown #define	BIC_CPU_c1	(1ULL << 7)
409812db3f7SLen Brown #define	BIC_CPU_c3	(1ULL << 8)
410812db3f7SLen Brown #define	BIC_CPU_c6	(1ULL << 9)
411812db3f7SLen Brown #define	BIC_CPU_c7	(1ULL << 10)
412812db3f7SLen Brown #define	BIC_ThreadC	(1ULL << 11)
413812db3f7SLen Brown #define	BIC_CoreTmp	(1ULL << 12)
414812db3f7SLen Brown #define	BIC_CoreCnt	(1ULL << 13)
415812db3f7SLen Brown #define	BIC_PkgTmp	(1ULL << 14)
416812db3f7SLen Brown #define	BIC_GFX_rc6	(1ULL << 15)
417812db3f7SLen Brown #define	BIC_GFXMHz	(1ULL << 16)
418812db3f7SLen Brown #define	BIC_Pkgpc2	(1ULL << 17)
419812db3f7SLen Brown #define	BIC_Pkgpc3	(1ULL << 18)
420812db3f7SLen Brown #define	BIC_Pkgpc6	(1ULL << 19)
421812db3f7SLen Brown #define	BIC_Pkgpc7	(1ULL << 20)
4220f47c08dSLen Brown #define	BIC_Pkgpc8	(1ULL << 21)
4230f47c08dSLen Brown #define	BIC_Pkgpc9	(1ULL << 22)
4240f47c08dSLen Brown #define	BIC_Pkgpc10	(1ULL << 23)
4250f47c08dSLen Brown #define	BIC_PkgWatt	(1ULL << 24)
4260f47c08dSLen Brown #define	BIC_CorWatt	(1ULL << 25)
4270f47c08dSLen Brown #define	BIC_GFXWatt	(1ULL << 26)
4280f47c08dSLen Brown #define	BIC_PkgCnt	(1ULL << 27)
4290f47c08dSLen Brown #define	BIC_RAMWatt	(1ULL << 28)
4300f47c08dSLen Brown #define	BIC_PKG__	(1ULL << 29)
4310f47c08dSLen Brown #define	BIC_RAM__	(1ULL << 30)
4320f47c08dSLen Brown #define	BIC_Pkg_J	(1ULL << 31)
4330f47c08dSLen Brown #define	BIC_Cor_J	(1ULL << 32)
4340f47c08dSLen Brown #define	BIC_GFX_J	(1ULL << 33)
4350f47c08dSLen Brown #define	BIC_RAM_J	(1ULL << 34)
4360f47c08dSLen Brown #define	BIC_Core	(1ULL << 35)
4370f47c08dSLen Brown #define	BIC_CPU		(1ULL << 36)
4380f47c08dSLen Brown #define	BIC_Mod_c6	(1ULL << 37)
43941618e63SLen Brown #define	BIC_sysfs	(1ULL << 38)
440a99d8730SLen Brown #define	BIC_Totl_c0	(1ULL << 39)
441a99d8730SLen Brown #define	BIC_Any_c0	(1ULL << 40)
442a99d8730SLen Brown #define	BIC_GFX_c0	(1ULL << 41)
443a99d8730SLen Brown #define	BIC_CPUGFX	(1ULL << 42)
444812db3f7SLen Brown 
445812db3f7SLen Brown unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
44641618e63SLen Brown unsigned long long bic_present = BIC_sysfs;
447812db3f7SLen Brown 
448812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
449812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
4500f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
451812db3f7SLen Brown 
452dd778a5eSLen Brown #define MAX_DEFERRED 16
453dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
454dd778a5eSLen Brown int deferred_skip_index;
455dd778a5eSLen Brown 
456dd778a5eSLen Brown /*
457dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
458dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
459dd778a5eSLen Brown  */
460dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
461dd778a5eSLen Brown 
462dd778a5eSLen Brown void help(void)
463dd778a5eSLen Brown {
464dd778a5eSLen Brown 	fprintf(outf,
465dd778a5eSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
466dd778a5eSLen Brown 	"\n"
467dd778a5eSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
468dd778a5eSLen Brown 	"when COMMAND completes.\n"
469dd778a5eSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
470dd778a5eSLen Brown 	"to print statistics, until interrupted.\n"
471dd778a5eSLen Brown 	"--add		add a counter\n"
472dd778a5eSLen Brown 	"		eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
473dd778a5eSLen Brown 	"--cpu	cpu-set	limit output to summary plus cpu-set:\n"
474dd778a5eSLen Brown 	"		{core | package | j,k,l..m,n-p }\n"
475dd778a5eSLen Brown 	"--quiet	skip decoding system configuration header\n"
476dd778a5eSLen Brown 	"--interval sec	Override default 5-second measurement interval\n"
477dd778a5eSLen Brown 	"--help		print this help message\n"
478dd778a5eSLen Brown 	"--list		list column headers only\n"
479dd778a5eSLen Brown 	"--out file	create or truncate \"file\" for all output\n"
480dd778a5eSLen Brown 	"--version	print version information\n"
481dd778a5eSLen Brown 	"\n"
482dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
483dd778a5eSLen Brown }
484dd778a5eSLen Brown 
485812db3f7SLen Brown /*
486812db3f7SLen Brown  * bic_lookup
487812db3f7SLen Brown  * for all the strings in comma separate name_list,
488812db3f7SLen Brown  * set the approprate bit in return value.
489812db3f7SLen Brown  */
490dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
491812db3f7SLen Brown {
492812db3f7SLen Brown 	int i;
493812db3f7SLen Brown 	unsigned long long retval = 0;
494812db3f7SLen Brown 
495812db3f7SLen Brown 	while (name_list) {
496812db3f7SLen Brown 		char *comma;
497812db3f7SLen Brown 
498812db3f7SLen Brown 		comma = strchr(name_list, ',');
499812db3f7SLen Brown 
500812db3f7SLen Brown 		if (comma)
501812db3f7SLen Brown 			*comma = '\0';
502812db3f7SLen Brown 
503812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
504812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
505812db3f7SLen Brown 				retval |= (1ULL << i);
506812db3f7SLen Brown 				break;
507812db3f7SLen Brown 			}
508812db3f7SLen Brown 		}
509812db3f7SLen Brown 		if (i == MAX_BIC) {
510dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
511812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
512812db3f7SLen Brown 				exit(-1);
513812db3f7SLen Brown 			}
514dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
515dd778a5eSLen Brown 			if (debug)
516dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
517dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
518dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
519dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
520dd778a5eSLen Brown 				help();
521dd778a5eSLen Brown 				exit(1);
522dd778a5eSLen Brown 			}
523dd778a5eSLen Brown 		}
524812db3f7SLen Brown 
525812db3f7SLen Brown 		name_list = comma;
526812db3f7SLen Brown 		if (name_list)
527812db3f7SLen Brown 			name_list++;
528812db3f7SLen Brown 
529812db3f7SLen Brown 	}
530812db3f7SLen Brown 	return retval;
531812db3f7SLen Brown }
532fc04cc67SLen Brown 
533dd778a5eSLen Brown 
534c8ade361SLen Brown void print_header(char *delim)
535103a8feaSLen Brown {
536388e9c81SLen Brown 	struct msr_counter *mp;
5376168c2e0SLen Brown 	int printed = 0;
538388e9c81SLen Brown 
539f4fdf2b4SLen Brown 	if (debug)
540f4fdf2b4SLen Brown 		outp += sprintf(outp, "usec %s", delim);
541812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
5426168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
543812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
5446168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
545812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
5466168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
547812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
5486168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
549812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
5506168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
551812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
5526168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
553812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
5546168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
5551cc21f7bSLen Brown 
5560de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
5570de6c0dfSLen Brown 		if (sums_need_wide_columns)
5586168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
5590de6c0dfSLen Brown 		else
5606168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
5610de6c0dfSLen Brown 	}
5620de6c0dfSLen Brown 
563812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
5646168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
5651cc21f7bSLen Brown 
566388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
567dd778a5eSLen Brown 
568388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
569388e9c81SLen Brown 			if (mp->width == 64)
570dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
571388e9c81SLen Brown 			else
572dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
573388e9c81SLen Brown 		} else {
5740de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
575dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
5760de6c0dfSLen Brown 			else
577dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
578388e9c81SLen Brown 		}
579388e9c81SLen Brown 	}
580388e9c81SLen Brown 
58141618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
5826168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
583812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
5846168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
585812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
5866168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
587812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
5886168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
589678a3bd1SLen Brown 
5900539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
5916168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
592678a3bd1SLen Brown 
593812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
5946168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
595388e9c81SLen Brown 
596388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
597388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
598388e9c81SLen Brown 			if (mp->width == 64)
599c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
600388e9c81SLen Brown 			else
601c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
602388e9c81SLen Brown 		} else {
6030de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6040de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6050de6c0dfSLen Brown 			else
606c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
607388e9c81SLen Brown 		}
608388e9c81SLen Brown 	}
609388e9c81SLen Brown 
610812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
6116168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
612889facbeSLen Brown 
613812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
6146168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
615fdf676e5SLen Brown 
616812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
6176168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
61827d47356SLen Brown 
619a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
6206168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
621a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
6226168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
623a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
6246168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
625a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
6266168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
6270b2bb692SLen Brown 
6280f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
6296168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
6300f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
6316168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
6320f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
6336168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
6340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
6356168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
6360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
6376168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
6380f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
6396168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
6400f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
6416168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
642103a8feaSLen Brown 
6435c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
644812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
6456168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
646812db3f7SLen Brown 		if (DO_BIC(BIC_CorWatt))
6476168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
648812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
6496168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
650812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
6516168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
652812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
6536168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
654812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
6556168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
656d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
657812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
6586168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
659812db3f7SLen Brown 		if (DO_BIC(BIC_Cor_J))
6606168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
661812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
6626168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
663812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
6646168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
665812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
6666168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
667812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
6686168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
6695c56be9aSDirk Brandewie 	}
670388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
671388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
672388e9c81SLen Brown 			if (mp->width == 64)
673c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
674388e9c81SLen Brown 			else
675c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
676388e9c81SLen Brown 		} else {
6770de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6780de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6790de6c0dfSLen Brown 			else
680c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
681388e9c81SLen Brown 		}
682388e9c81SLen Brown 	}
683388e9c81SLen Brown 
684c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
685103a8feaSLen Brown }
686103a8feaSLen Brown 
687c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
688c98d5d94SLen Brown 	struct pkg_data *p)
689103a8feaSLen Brown {
690388e9c81SLen Brown 	int i;
691388e9c81SLen Brown 	struct msr_counter *mp;
692388e9c81SLen Brown 
6933b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
694c98d5d94SLen Brown 
695c98d5d94SLen Brown 	if (t) {
6963b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
6973b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
6983b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
6993b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
7003b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
7013b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
7026886fee4SLen Brown 
703812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
7040de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
705812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
706218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
707388e9c81SLen Brown 
708388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
709388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
710388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
711388e9c81SLen Brown 		}
712103a8feaSLen Brown 	}
713103a8feaSLen Brown 
714c98d5d94SLen Brown 	if (c) {
7153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
7163b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
7173b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
7183b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
7193b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
720388e9c81SLen Brown 
721388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
722388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
723388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
724388e9c81SLen Brown 		}
7250539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
726c98d5d94SLen Brown 	}
727103a8feaSLen Brown 
728c98d5d94SLen Brown 	if (p) {
7293b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
7300b2bb692SLen Brown 
7310b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
7320b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
7330b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
7340b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
7350b2bb692SLen Brown 
7363b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
7370f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
7383b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
7390f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
7403b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
7410f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
7423b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
7433b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
7443b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
7453b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
7463b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
7473b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
7483b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
7493b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
7503b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
7513b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
7523b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
7533b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
7543b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
755388e9c81SLen Brown 
756388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
757388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
758388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
759388e9c81SLen Brown 		}
760c98d5d94SLen Brown 	}
7613b4d5c7fSAndy Shevchenko 
7623b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
7633b4d5c7fSAndy Shevchenko 
764c98d5d94SLen Brown 	return 0;
765103a8feaSLen Brown }
766103a8feaSLen Brown 
767e23da037SLen Brown /*
768e23da037SLen Brown  * column formatting convention & formats
769e23da037SLen Brown  */
770c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
771c98d5d94SLen Brown 	struct pkg_data *p)
772103a8feaSLen Brown {
773008d396eSLen Brown 	double interval_float, tsc;
774fc04cc67SLen Brown 	char *fmt8;
775388e9c81SLen Brown 	int i;
776388e9c81SLen Brown 	struct msr_counter *mp;
7776168c2e0SLen Brown 	char *delim = "\t";
7786168c2e0SLen Brown 	int printed = 0;
779103a8feaSLen Brown 
780c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
781c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
782c98d5d94SLen Brown 		return 0;
783c98d5d94SLen Brown 
784c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
785c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
786c98d5d94SLen Brown 		return 0;
787c98d5d94SLen Brown 
7881ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
7891ef7d21aSLen Brown 	if ((t != &average.threads) &&
7901ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
7911ef7d21aSLen Brown 		return 0;
7921ef7d21aSLen Brown 
793f4fdf2b4SLen Brown 	if (debug) {
794f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
795f4fdf2b4SLen Brown 		struct timeval tv;
796f4fdf2b4SLen Brown 
797f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
798f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
799f4fdf2b4SLen Brown 	}
800f4fdf2b4SLen Brown 
801103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
802103a8feaSLen Brown 
803008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
804008d396eSLen Brown 
805c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
806c98d5d94SLen Brown 	if (t == &average.threads) {
807812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
8086168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
809812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
8106168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
811812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
8126168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
813103a8feaSLen Brown 	} else {
814812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
815c98d5d94SLen Brown 			if (p)
8166168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
817c98d5d94SLen Brown 			else
8186168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
819c98d5d94SLen Brown 		}
820812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
821c98d5d94SLen Brown 			if (c)
8226168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
823c98d5d94SLen Brown 			else
8246168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
825c98d5d94SLen Brown 		}
826812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
8276168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
828103a8feaSLen Brown 	}
829fc04cc67SLen Brown 
830812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
8316168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
832fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
833fc04cc67SLen Brown 
834812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
8356168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
836103a8feaSLen Brown 
837812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
83821ed5574SLen Brown 		if (has_base_hz)
8396168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
84021ed5574SLen Brown 		else
8416168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
842008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
84321ed5574SLen Brown 	}
844103a8feaSLen Brown 
845812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
8466168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
847103a8feaSLen Brown 
848562a2d37SLen Brown 	/* IRQ */
8490de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
8500de6c0dfSLen Brown 		if (sums_need_wide_columns)
8516168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
8520de6c0dfSLen Brown 		else
8536168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
8540de6c0dfSLen Brown 	}
855562a2d37SLen Brown 
8561cc21f7bSLen Brown 	/* SMI */
857812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
8586168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
8591cc21f7bSLen Brown 
860678a3bd1SLen Brown 	/* Added counters */
861678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
862678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
863678a3bd1SLen Brown 			if (mp->width == 32)
8645f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
865678a3bd1SLen Brown 			else
8666168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
867678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
8680de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
8696168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
8700de6c0dfSLen Brown 			else
8716168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
872678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
87341618e63SLen Brown 			if (mp->type == COUNTER_USEC)
8746168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
87541618e63SLen Brown 			else
8766168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
877678a3bd1SLen Brown 		}
878678a3bd1SLen Brown 	}
879678a3bd1SLen Brown 
88041618e63SLen Brown 	/* C1 */
88141618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
8826168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
88341618e63SLen Brown 
88441618e63SLen Brown 
885c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
886c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
887c98d5d94SLen Brown 		goto done;
888c98d5d94SLen Brown 
889812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
8906168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
891812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
8926168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
893812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
8946168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
895c98d5d94SLen Brown 
8960539ba11SLen Brown 	/* Mod%c6 */
8970539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
8986168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
8990539ba11SLen Brown 
900812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
9016168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
902889facbeSLen Brown 
903388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
904388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
905388e9c81SLen Brown 			if (mp->width == 32)
9065f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
907388e9c81SLen Brown 			else
9086168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
909388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
9100de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9116168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
9120de6c0dfSLen Brown 			else
9136168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
914388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
9156168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
916388e9c81SLen Brown 		}
917388e9c81SLen Brown 	}
918388e9c81SLen Brown 
919c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
920c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
921c98d5d94SLen Brown 		goto done;
922c98d5d94SLen Brown 
9230b2bb692SLen Brown 	/* PkgTmp */
924812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
9256168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
926889facbeSLen Brown 
927fdf676e5SLen Brown 	/* GFXrc6 */
928812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
929ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
9306168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
9319185e988SLen Brown 		} else {
9326168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
9339185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
9349185e988SLen Brown 		}
9359185e988SLen Brown 	}
936fdf676e5SLen Brown 
93727d47356SLen Brown 	/* GFXMHz */
938812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
9396168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
94027d47356SLen Brown 
9410b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
942a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
9436168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
944a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
9456168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
946a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
9476168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
948a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
9496168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
9500b2bb692SLen Brown 
9510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
9526168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
9530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
9546168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
9550f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
9566168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
9570f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
9586168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
9590f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
9606168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
9610f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
9626168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
9630f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
9646168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
965889facbeSLen Brown 
966889facbeSLen Brown 	/*
967889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
968889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
969889facbeSLen Brown  	 */
970fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
9716168c2e0SLen Brown 		fmt8 = "%s%.2f";
972fc04cc67SLen Brown 	else
973fc04cc67SLen Brown 		fmt8 = "%6.0f**";
974889facbeSLen Brown 
975812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
9766168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
977812db3f7SLen Brown 	if (DO_BIC(BIC_CorWatt))
9786168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
979812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
9806168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
981812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
9826168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
983812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
9846168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
985812db3f7SLen Brown 	if (DO_BIC(BIC_Cor_J))
9866168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
987812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
9886168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
989812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
9906168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
991812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
9926168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
993812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
9946168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
995812db3f7SLen Brown 
996388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
997388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
998388e9c81SLen Brown 			if (mp->width == 32)
9995f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1000388e9c81SLen Brown 			else
10016168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1002388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10030de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10046168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
10050de6c0dfSLen Brown 			else
10066168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1007388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10086168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1009388e9c81SLen Brown 		}
1010388e9c81SLen Brown 	}
1011388e9c81SLen Brown 
1012c98d5d94SLen Brown done:
1013c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
1014c98d5d94SLen Brown 
1015c98d5d94SLen Brown 	return 0;
1016103a8feaSLen Brown }
1017103a8feaSLen Brown 
1018b7d8c148SLen Brown void flush_output_stdout(void)
1019103a8feaSLen Brown {
1020b7d8c148SLen Brown 	FILE *filep;
1021b7d8c148SLen Brown 
1022b7d8c148SLen Brown 	if (outf == stderr)
1023b7d8c148SLen Brown 		filep = stdout;
1024b7d8c148SLen Brown 	else
1025b7d8c148SLen Brown 		filep = outf;
1026b7d8c148SLen Brown 
1027b7d8c148SLen Brown 	fputs(output_buffer, filep);
1028b7d8c148SLen Brown 	fflush(filep);
1029b7d8c148SLen Brown 
1030c98d5d94SLen Brown 	outp = output_buffer;
1031c98d5d94SLen Brown }
1032b7d8c148SLen Brown void flush_output_stderr(void)
1033c98d5d94SLen Brown {
1034b7d8c148SLen Brown 	fputs(output_buffer, outf);
1035b7d8c148SLen Brown 	fflush(outf);
1036c98d5d94SLen Brown 	outp = output_buffer;
1037c98d5d94SLen Brown }
1038c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1039c98d5d94SLen Brown {
1040e23da037SLen Brown 	static int printed;
1041103a8feaSLen Brown 
1042e23da037SLen Brown 	if (!printed || !summary_only)
1043c8ade361SLen Brown 		print_header("\t");
1044103a8feaSLen Brown 
1045c98d5d94SLen Brown 	if (topo.num_cpus > 1)
1046c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
1047c98d5d94SLen Brown 			&average.packages);
1048103a8feaSLen Brown 
1049e23da037SLen Brown 	printed = 1;
1050e23da037SLen Brown 
1051e23da037SLen Brown 	if (summary_only)
1052e23da037SLen Brown 		return;
1053e23da037SLen Brown 
1054c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1055103a8feaSLen Brown }
1056103a8feaSLen Brown 
1057889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1058889facbeSLen Brown 	if (new > old) {			\
1059889facbeSLen Brown 		old = new - old;		\
1060889facbeSLen Brown 	} else {				\
1061889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1062889facbeSLen Brown 	}
1063889facbeSLen Brown 
1064ba3dec99SLen Brown int
1065c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1066103a8feaSLen Brown {
1067388e9c81SLen Brown 	int i;
1068388e9c81SLen Brown 	struct msr_counter *mp;
10690b2bb692SLen Brown 
1070a99d8730SLen Brown 
1071a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
10720b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1073a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
10740b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1075a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
10760b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1077a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
10780b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1079a99d8730SLen Brown 
1080c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
10810f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1082c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
10830f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1084c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
10850f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1086c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1087ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1088ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1089ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1090889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1091889facbeSLen Brown 
10929185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
10939185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
10949185e988SLen Brown 		old->gfx_rc6_ms = -1;
10959185e988SLen Brown 	else
1096fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
10979185e988SLen Brown 
109827d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
109927d47356SLen Brown 
1100889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1101889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1102889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1103889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1104889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1105889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1106ba3dec99SLen Brown 
1107388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1108388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1109388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1110388e9c81SLen Brown 		else
1111388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1112388e9c81SLen Brown 	}
1113388e9c81SLen Brown 
1114ba3dec99SLen Brown 	return 0;
1115103a8feaSLen Brown }
1116103a8feaSLen Brown 
1117c98d5d94SLen Brown void
1118c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1119c98d5d94SLen Brown {
1120388e9c81SLen Brown 	int i;
1121388e9c81SLen Brown 	struct msr_counter *mp;
1122388e9c81SLen Brown 
1123c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1124c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1125c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1126889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
11270539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1128388e9c81SLen Brown 
1129388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1130388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1131388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1132388e9c81SLen Brown 		else
1133388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1134388e9c81SLen Brown 	}
1135103a8feaSLen Brown }
1136c98d5d94SLen Brown 
1137c3ae331dSLen Brown /*
1138c3ae331dSLen Brown  * old = new - old
1139c3ae331dSLen Brown  */
1140ba3dec99SLen Brown int
1141c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1142c98d5d94SLen Brown 	struct core_data *core_delta)
1143c98d5d94SLen Brown {
1144388e9c81SLen Brown 	int i;
1145388e9c81SLen Brown 	struct msr_counter *mp;
1146388e9c81SLen Brown 
1147c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1148c98d5d94SLen Brown 
1149103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1150b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1151b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1152b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1153b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1154103a8feaSLen Brown 
1155c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1156c98d5d94SLen Brown 
1157812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1158c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1159c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1160c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1161c98d5d94SLen Brown 		} else {
1162ba3dec99SLen Brown 			return -1;
1163103a8feaSLen Brown 		}
1164a729617cSLen Brown 	}
1165103a8feaSLen Brown 
1166c98d5d94SLen Brown 
1167144b44b1SLen Brown 	if (use_c1_residency_msr) {
1168144b44b1SLen Brown 		/*
1169144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1170144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1171144b44b1SLen Brown 		 */
1172144b44b1SLen Brown 	} else {
1173103a8feaSLen Brown 		/*
1174c3ae331dSLen Brown 		 * As counter collection is not atomic,
1175c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1176103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1177103a8feaSLen Brown 		 */
117895149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1179c98d5d94SLen Brown 			old->c1 = 0;
1180c98d5d94SLen Brown 		else {
1181c98d5d94SLen Brown 			/* normal case, derive c1 */
1182008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1183c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1184c98d5d94SLen Brown 		}
1185144b44b1SLen Brown 	}
1186c3ae331dSLen Brown 
1187c98d5d94SLen Brown 	if (old->mperf == 0) {
1188b7d8c148SLen Brown 		if (debug > 1)
1189b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1190c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1191c98d5d94SLen Brown 	}
1192103a8feaSLen Brown 
1193812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1194562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1195562a2d37SLen Brown 
1196812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
11971ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1198ba3dec99SLen Brown 
1199388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1200388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1201388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1202388e9c81SLen Brown 		else
1203388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1204388e9c81SLen Brown 	}
1205ba3dec99SLen Brown 	return 0;
1206103a8feaSLen Brown }
1207c98d5d94SLen Brown 
1208c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1209c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1210c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1211c98d5d94SLen Brown {
1212ba3dec99SLen Brown 	int retval = 0;
1213ba3dec99SLen Brown 
1214c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1215c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1216c98d5d94SLen Brown 		delta_core(c, c2);
1217c98d5d94SLen Brown 
1218c98d5d94SLen Brown 	/* always calculate thread delta */
1219ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1220ba3dec99SLen Brown 	if (retval)
1221ba3dec99SLen Brown 		return retval;
1222c98d5d94SLen Brown 
1223c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1224c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1225ba3dec99SLen Brown 		retval = delta_package(p, p2);
1226c98d5d94SLen Brown 
1227ba3dec99SLen Brown 	return retval;
1228103a8feaSLen Brown }
1229103a8feaSLen Brown 
1230c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1231103a8feaSLen Brown {
1232388e9c81SLen Brown 	int i;
1233388e9c81SLen Brown 	struct msr_counter  *mp;
1234388e9c81SLen Brown 
1235c98d5d94SLen Brown 	t->tsc = 0;
1236c98d5d94SLen Brown 	t->aperf = 0;
1237c98d5d94SLen Brown 	t->mperf = 0;
1238c98d5d94SLen Brown 	t->c1 = 0;
1239103a8feaSLen Brown 
1240562a2d37SLen Brown 	t->irq_count = 0;
1241562a2d37SLen Brown 	t->smi_count = 0;
1242562a2d37SLen Brown 
1243c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1244c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1245c98d5d94SLen Brown 
1246c98d5d94SLen Brown 	c->c3 = 0;
1247c98d5d94SLen Brown 	c->c6 = 0;
1248c98d5d94SLen Brown 	c->c7 = 0;
12490539ba11SLen Brown 	c->mc6_us = 0;
1250889facbeSLen Brown 	c->core_temp_c = 0;
1251c98d5d94SLen Brown 
12520b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
12530b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
12540b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
12550b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
12560b2bb692SLen Brown 
1257c98d5d94SLen Brown 	p->pc2 = 0;
12580f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1259c98d5d94SLen Brown 		p->pc3 = 0;
12600f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1261c98d5d94SLen Brown 		p->pc6 = 0;
12620f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1263c98d5d94SLen Brown 		p->pc7 = 0;
1264ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1265ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1266ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1267889facbeSLen Brown 
1268889facbeSLen Brown 	p->energy_pkg = 0;
1269889facbeSLen Brown 	p->energy_dram = 0;
1270889facbeSLen Brown 	p->energy_cores = 0;
1271889facbeSLen Brown 	p->energy_gfx = 0;
1272889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1273889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1274889facbeSLen Brown 	p->pkg_temp_c = 0;
127527d47356SLen Brown 
1276fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
127727d47356SLen Brown 	p->gfx_mhz = 0;
1278388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1279388e9c81SLen Brown 		t->counter[i] = 0;
1280388e9c81SLen Brown 
1281388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1282388e9c81SLen Brown 		c->counter[i] = 0;
1283388e9c81SLen Brown 
1284388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1285388e9c81SLen Brown 		p->counter[i] = 0;
1286103a8feaSLen Brown }
1287c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1288c98d5d94SLen Brown 	struct pkg_data *p)
1289103a8feaSLen Brown {
1290388e9c81SLen Brown 	int i;
1291388e9c81SLen Brown 	struct msr_counter *mp;
1292388e9c81SLen Brown 
1293c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1294c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1295c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1296c98d5d94SLen Brown 	average.threads.c1 += t->c1;
129715aaa346SLen Brown 
1298562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1299562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1300562a2d37SLen Brown 
1301388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1302388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1303388e9c81SLen Brown 			continue;
1304388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1305388e9c81SLen Brown 	}
1306388e9c81SLen Brown 
1307c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1308c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1309c98d5d94SLen Brown 		return 0;
1310c98d5d94SLen Brown 
1311c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1312c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1313c98d5d94SLen Brown 	average.cores.c7 += c->c7;
13140539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1315c98d5d94SLen Brown 
1316889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1317889facbeSLen Brown 
1318388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1319388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1320388e9c81SLen Brown 			continue;
1321388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1322388e9c81SLen Brown 	}
1323388e9c81SLen Brown 
1324c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1325c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1326c98d5d94SLen Brown 		return 0;
1327c98d5d94SLen Brown 
1328a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
13290b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1330a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
13310b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1332a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
13330b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1334a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
13350b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
13360b2bb692SLen Brown 
1337c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
13380f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1339c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
13400f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1341c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
13420f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1343c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1344ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1345ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1346ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1347c98d5d94SLen Brown 
1348889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1349889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1350889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1351889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1352889facbeSLen Brown 
1353fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
135427d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
135527d47356SLen Brown 
1356889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1357889facbeSLen Brown 
1358889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1359889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1360388e9c81SLen Brown 
1361388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1362388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1363388e9c81SLen Brown 			continue;
1364388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1365388e9c81SLen Brown 	}
1366c98d5d94SLen Brown 	return 0;
1367c98d5d94SLen Brown }
1368c98d5d94SLen Brown /*
1369c98d5d94SLen Brown  * sum the counters for all cpus in the system
1370c98d5d94SLen Brown  * compute the weighted average
1371c98d5d94SLen Brown  */
1372c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1373c98d5d94SLen Brown 	struct pkg_data *p)
1374c98d5d94SLen Brown {
1375388e9c81SLen Brown 	int i;
1376388e9c81SLen Brown 	struct msr_counter *mp;
1377388e9c81SLen Brown 
1378c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1379c98d5d94SLen Brown 
1380c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1381c98d5d94SLen Brown 
1382c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1383c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1384c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1385c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1386c98d5d94SLen Brown 
13870de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
13880de6c0dfSLen Brown 		sums_need_wide_columns = 1;
13890de6c0dfSLen Brown 
1390c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1391c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1392c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
13930539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1394c98d5d94SLen Brown 
1395a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
13960b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1397a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
13980b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1399a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
14000b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1401a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
14020b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
14030b2bb692SLen Brown 
1404c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
14050f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1406c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
14070f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1408c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
14090f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1410c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1411ca58710fSKristen Carlson Accardi 
1412ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1413ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1414ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1415388e9c81SLen Brown 
1416388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1417388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1418388e9c81SLen Brown 			continue;
14190de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14200de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
14210de6c0dfSLen Brown 				sums_need_wide_columns = 1;
142241618e63SLen Brown 			continue;
14230de6c0dfSLen Brown 		}
1424388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1425388e9c81SLen Brown 	}
1426388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1427388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1428388e9c81SLen Brown 			continue;
14290de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14300de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
14310de6c0dfSLen Brown 				sums_need_wide_columns = 1;
14320de6c0dfSLen Brown 		}
1433388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1434388e9c81SLen Brown 	}
1435388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1436388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1437388e9c81SLen Brown 			continue;
14380de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14390de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
14400de6c0dfSLen Brown 				sums_need_wide_columns = 1;
14410de6c0dfSLen Brown 		}
1442388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1443388e9c81SLen Brown 	}
1444c98d5d94SLen Brown }
1445c98d5d94SLen Brown 
1446c98d5d94SLen Brown static unsigned long long rdtsc(void)
1447c98d5d94SLen Brown {
1448c98d5d94SLen Brown 	unsigned int low, high;
1449c98d5d94SLen Brown 
1450c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1451c98d5d94SLen Brown 
1452c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1453c98d5d94SLen Brown }
1454c98d5d94SLen Brown 
1455c98d5d94SLen Brown /*
1456495c7654SLen Brown  * Open a file, and exit on failure
1457495c7654SLen Brown  */
1458495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1459495c7654SLen Brown {
1460495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1461495c7654SLen Brown 
1462495c7654SLen Brown 	if (!filep)
1463495c7654SLen Brown 		err(1, "%s: open failed", path);
1464495c7654SLen Brown 	return filep;
1465495c7654SLen Brown }
1466495c7654SLen Brown /*
1467495c7654SLen Brown  * snapshot_sysfs_counter()
1468495c7654SLen Brown  *
1469495c7654SLen Brown  * return snapshot of given counter
1470495c7654SLen Brown  */
1471495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1472495c7654SLen Brown {
1473495c7654SLen Brown 	FILE *fp;
1474495c7654SLen Brown 	int retval;
1475495c7654SLen Brown 	unsigned long long counter;
1476495c7654SLen Brown 
1477495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1478495c7654SLen Brown 
1479495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1480495c7654SLen Brown 	if (retval != 1)
1481495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1482495c7654SLen Brown 
1483495c7654SLen Brown 	fclose(fp);
1484495c7654SLen Brown 
1485495c7654SLen Brown 	return counter;
1486495c7654SLen Brown }
1487495c7654SLen Brown 
1488495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1489495c7654SLen Brown {
1490495c7654SLen Brown 	if (mp->msr_num != 0) {
1491495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1492495c7654SLen Brown 			return -1;
1493495c7654SLen Brown 	} else {
149441618e63SLen Brown 		char path[128];
149541618e63SLen Brown 
149641618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
149741618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
149841618e63SLen Brown 				 cpu, mp->path);
149941618e63SLen Brown 
150041618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
150141618e63SLen Brown 		} else {
1502495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1503495c7654SLen Brown 		}
150441618e63SLen Brown 	}
1505495c7654SLen Brown 
1506495c7654SLen Brown 	return 0;
1507495c7654SLen Brown }
1508495c7654SLen Brown 
1509495c7654SLen Brown /*
1510c98d5d94SLen Brown  * get_counters(...)
1511c98d5d94SLen Brown  * migrate to cpu
1512c98d5d94SLen Brown  * acquire and record local counters for that cpu
1513c98d5d94SLen Brown  */
1514c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1515c98d5d94SLen Brown {
1516c98d5d94SLen Brown 	int cpu = t->cpu_id;
1517889facbeSLen Brown 	unsigned long long msr;
15180102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1519388e9c81SLen Brown 	struct msr_counter *mp;
1520388e9c81SLen Brown 	int i;
1521c98d5d94SLen Brown 
1522f4fdf2b4SLen Brown 
1523f4fdf2b4SLen Brown 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1524f4fdf2b4SLen Brown 
1525e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1526b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
152715aaa346SLen Brown 		return -1;
1528e52966c0SLen Brown 	}
152915aaa346SLen Brown 
15300102b067SLen Brown retry:
1531c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
153215aaa346SLen Brown 
1533812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
15340102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
15350102b067SLen Brown 
15360102b067SLen Brown 		/*
15370102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
15380102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
15390102b067SLen Brown 		 *
15400102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
15410102b067SLen Brown 		 * individual system call, so delays may occur
15420102b067SLen Brown 		 * between them.  If the time to read them
15430102b067SLen Brown 		 * varies by a large amount, we re-read them.
15440102b067SLen Brown 		 */
15450102b067SLen Brown 
15460102b067SLen Brown 		/*
15470102b067SLen Brown 		 * This initial dummy APERF read has been seen to
15480102b067SLen Brown 		 * reduce jitter in the subsequent reads.
15490102b067SLen Brown 		 */
15500102b067SLen Brown 
15519c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1552c98d5d94SLen Brown 			return -3;
15530102b067SLen Brown 
15540102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
15550102b067SLen Brown 
15560102b067SLen Brown 		tsc_before = t->tsc;
15570102b067SLen Brown 
15580102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
15590102b067SLen Brown 			return -3;
15600102b067SLen Brown 
15610102b067SLen Brown 		tsc_between = rdtsc();
15620102b067SLen Brown 
15639c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1564c98d5d94SLen Brown 			return -4;
15650102b067SLen Brown 
15660102b067SLen Brown 		tsc_after = rdtsc();
15670102b067SLen Brown 
15680102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
15690102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
15700102b067SLen Brown 
15710102b067SLen Brown 		/*
15720102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
15730102b067SLen Brown 		 * differ by more than 2x, then try again.
15740102b067SLen Brown 		 */
15750102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
15760102b067SLen Brown 			aperf_mperf_retry_count++;
15770102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
15780102b067SLen Brown 				goto retry;
15790102b067SLen Brown 			else
15800102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
15810102b067SLen Brown 					cpu, aperf_time, mperf_time);
15820102b067SLen Brown 		}
15830102b067SLen Brown 		aperf_mperf_retry_count = 0;
15840102b067SLen Brown 
1585b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1586b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
158788c3281fSLen Brown 	}
158888c3281fSLen Brown 
1589812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1590562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1591812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
15921ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
15931ed51011SLen Brown 			return -5;
15941ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
15951ed51011SLen Brown 	}
15960539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1597144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1598144b44b1SLen Brown 			return -6;
1599144b44b1SLen Brown 	}
1600144b44b1SLen Brown 
1601388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1602495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1603388e9c81SLen Brown 			return -10;
1604388e9c81SLen Brown 	}
1605388e9c81SLen Brown 
1606c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1607c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1608f4fdf2b4SLen Brown 		goto done;
1609c98d5d94SLen Brown 
1610812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
1611c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1612c98d5d94SLen Brown 			return -6;
1613144b44b1SLen Brown 	}
1614144b44b1SLen Brown 
1615812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1616c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1617c98d5d94SLen Brown 			return -7;
1618fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1619fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1620fb5d4327SDasaratharaman Chandramouli 			return -7;
1621103a8feaSLen Brown 	}
162215aaa346SLen Brown 
1623812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1624c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1625c98d5d94SLen Brown 			return -8;
1626c98d5d94SLen Brown 
16270539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
16280539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
16290539ba11SLen Brown 			return -8;
16300539ba11SLen Brown 
1631812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1632889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1633889facbeSLen Brown 			return -9;
1634889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1635889facbeSLen Brown 	}
1636889facbeSLen Brown 
1637388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1638495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1639388e9c81SLen Brown 			return -10;
1640388e9c81SLen Brown 	}
1641889facbeSLen Brown 
1642c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1643c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1644f4fdf2b4SLen Brown 		goto done;
164515aaa346SLen Brown 
1646a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
16470b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
16480b2bb692SLen Brown 			return -10;
1649a99d8730SLen Brown 	}
1650a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
16510b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
16520b2bb692SLen Brown 			return -11;
1653a99d8730SLen Brown 	}
1654a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
16550b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
16560b2bb692SLen Brown 			return -12;
1657a99d8730SLen Brown 	}
1658a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
16590b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
16600b2bb692SLen Brown 			return -13;
16610b2bb692SLen Brown 	}
16620f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1663c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1664c98d5d94SLen Brown 			return -9;
16650f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
16660539ba11SLen Brown 		if (do_slm_cstates) {
16670539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
16680539ba11SLen Brown 				return -10;
16690539ba11SLen Brown 		} else {
1670c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1671c98d5d94SLen Brown 				return -10;
16720539ba11SLen Brown 		}
16730539ba11SLen Brown 	}
16740539ba11SLen Brown 
16750f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1676c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1677c98d5d94SLen Brown 			return -11;
16780f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1679c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1680c98d5d94SLen Brown 			return -12;
16810f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1682ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1683ca58710fSKristen Carlson Accardi 			return -13;
16840f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1685ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1686ca58710fSKristen Carlson Accardi 			return -13;
16870f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1688ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1689ca58710fSKristen Carlson Accardi 			return -13;
16900f47c08dSLen Brown 
1691889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1692889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1693889facbeSLen Brown 			return -13;
1694889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1695889facbeSLen Brown 	}
16969148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1697889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1698889facbeSLen Brown 			return -14;
1699889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1700889facbeSLen Brown 	}
1701889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1702889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1703889facbeSLen Brown 			return -15;
1704889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1705889facbeSLen Brown 	}
1706889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1707889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1708889facbeSLen Brown 			return -16;
1709889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1710889facbeSLen Brown 	}
1711889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1712889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1713889facbeSLen Brown 			return -16;
1714889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1715889facbeSLen Brown 	}
1716889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1717889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1718889facbeSLen Brown 			return -16;
1719889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1720889facbeSLen Brown 	}
1721812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1722889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1723889facbeSLen Brown 			return -17;
1724889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1725889facbeSLen Brown 	}
1726fdf676e5SLen Brown 
1727812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1728fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1729fdf676e5SLen Brown 
1730812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
173127d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
173227d47356SLen Brown 
1733388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1734495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
1735388e9c81SLen Brown 			return -10;
1736388e9c81SLen Brown 	}
1737f4fdf2b4SLen Brown done:
1738f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
1739388e9c81SLen Brown 
174015aaa346SLen Brown 	return 0;
1741103a8feaSLen Brown }
1742103a8feaSLen Brown 
1743ee7e38e3SLen Brown /*
1744ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1745ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1746ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1747ee7e38e3SLen Brown  */
1748ee7e38e3SLen Brown 
1749ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1750ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1751ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1752ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1753ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1754ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1755ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1756ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1757ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1758ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1759ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1760ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
17610b2bb692SLen Brown #define PCL__8 12 /* PC8 */
17620b2bb692SLen Brown #define PCL__9 13 /* PC9 */
17630b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1764ee7e38e3SLen Brown 
1765ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1766ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
17670b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1768ee7e38e3SLen Brown 
1769e9257f5fSLen 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};
1770e9257f5fSLen 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};
1771e9257f5fSLen 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};
17720539ba11SLen 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};
1773f2642888SLen 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};
1774e9257f5fSLen 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};
1775e4085d54SLen Brown int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1776d8ebb442SLen Brown int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1777ee7e38e3SLen Brown 
1778a2b7b749SLen Brown 
1779a2b7b749SLen Brown static void
1780a2b7b749SLen Brown calculate_tsc_tweak()
1781a2b7b749SLen Brown {
1782a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1783a2b7b749SLen Brown }
1784a2b7b749SLen Brown 
1785fcd17211SLen Brown static void
1786fcd17211SLen Brown dump_nhm_platform_info(void)
1787103a8feaSLen Brown {
1788103a8feaSLen Brown 	unsigned long long msr;
1789103a8feaSLen Brown 	unsigned int ratio;
1790103a8feaSLen Brown 
1791ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1792103a8feaSLen Brown 
1793b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
17946574a5d5SLen Brown 
1795103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1796710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
1797103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1798103a8feaSLen Brown 
1799103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1800710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1801103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1802103a8feaSLen Brown 
18037ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1804b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1805bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
180667920418SLen Brown 
1807fcd17211SLen Brown 	return;
1808fcd17211SLen Brown }
1809fcd17211SLen Brown 
1810fcd17211SLen Brown static void
1811fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1812fcd17211SLen Brown {
1813fcd17211SLen Brown 	unsigned long long msr;
1814fcd17211SLen Brown 	unsigned int ratio;
1815fcd17211SLen Brown 
18167ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1817fcd17211SLen Brown 
1818b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1819fcd17211SLen Brown 
1820fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1821fcd17211SLen Brown 	if (ratio)
1822710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
1823fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1824fcd17211SLen Brown 
1825fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1826fcd17211SLen Brown 	if (ratio)
1827710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
1828fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1829fcd17211SLen Brown 	return;
1830fcd17211SLen Brown }
1831fcd17211SLen Brown 
1832fcd17211SLen Brown static void
1833fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1834fcd17211SLen Brown {
1835fcd17211SLen Brown 	unsigned long long msr;
1836fcd17211SLen Brown 	unsigned int ratio;
18376574a5d5SLen Brown 
18387ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
18396574a5d5SLen Brown 
1840b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
18416574a5d5SLen Brown 
18426574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
18436574a5d5SLen Brown 	if (ratio)
1844710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
18456574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18466574a5d5SLen Brown 
18476574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
18486574a5d5SLen Brown 	if (ratio)
1849710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
18506574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18516574a5d5SLen Brown 
18526574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
18536574a5d5SLen Brown 	if (ratio)
1854710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
18556574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18566574a5d5SLen Brown 
18576574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
18586574a5d5SLen Brown 	if (ratio)
1859710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
18606574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18616574a5d5SLen Brown 
18626574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
18636574a5d5SLen Brown 	if (ratio)
1864710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
18656574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18666574a5d5SLen Brown 
18676574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
18686574a5d5SLen Brown 	if (ratio)
1869710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
18706574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18716574a5d5SLen Brown 
18726574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
18736574a5d5SLen Brown 	if (ratio)
1874710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
18756574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18766574a5d5SLen Brown 
18776574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
18786574a5d5SLen Brown 	if (ratio)
1879710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
18806574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1881103a8feaSLen Brown 	return;
1882fcd17211SLen Brown }
188331e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
188431e07522SLen Brown {
188531e07522SLen Brown 
188631e07522SLen Brown 	if (!genuine_intel)
188731e07522SLen Brown 		return 0;
188831e07522SLen Brown 
188931e07522SLen Brown 	switch (model) {
189031e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
189131e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
189231e07522SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
189331e07522SLen Brown 		return 1;
189431e07522SLen Brown 	}
189531e07522SLen Brown 	return 0;
189631e07522SLen Brown }
1897fcd17211SLen Brown 
1898fcd17211SLen Brown static void
189931e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
1900fcd17211SLen Brown {
190131e07522SLen Brown 	unsigned long long msr, core_counts;
190231e07522SLen Brown 	unsigned int ratio, group_size;
1903103a8feaSLen Brown 
19047ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1905b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
19066574a5d5SLen Brown 
190731e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
190831e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
190931e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
191031e07522SLen Brown 	} else {
191131e07522SLen Brown 		core_counts = 0x0807060504030201;
191231e07522SLen Brown 	}
191331e07522SLen Brown 
19146574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
191531e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
19166574a5d5SLen Brown 	if (ratio)
191731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
191831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19196574a5d5SLen Brown 
19206574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
192131e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
19226574a5d5SLen Brown 	if (ratio)
192331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
192431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19256574a5d5SLen Brown 
19266574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
192731e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
19286574a5d5SLen Brown 	if (ratio)
192931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
193031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19316574a5d5SLen Brown 
19326574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
193331e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
19346574a5d5SLen Brown 	if (ratio)
193531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
193631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19376574a5d5SLen Brown 
1938103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
193931e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
1940103a8feaSLen Brown 	if (ratio)
194131e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
194231e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1943103a8feaSLen Brown 
1944103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
194531e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
1946103a8feaSLen Brown 	if (ratio)
194731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
194831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1949103a8feaSLen Brown 
1950103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
195131e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
1952103a8feaSLen Brown 	if (ratio)
195331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
195431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1955103a8feaSLen Brown 
1956103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
195731e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
1958103a8feaSLen Brown 	if (ratio)
195931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
196031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1961fcd17211SLen Brown 	return;
1962fcd17211SLen Brown }
19633a9a941dSLen Brown 
1964fcd17211SLen Brown static void
19650f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
19660f7887c4SLen Brown {
19670f7887c4SLen Brown 	unsigned long long msr;
19680f7887c4SLen Brown 	unsigned int ratio;
19690f7887c4SLen Brown 
19700f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
19710f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
19720f7887c4SLen Brown 
19730f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
19740f7887c4SLen Brown 	if (ratio)
19750f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
19760f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19770f7887c4SLen Brown 
19780f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
19790f7887c4SLen Brown 	if (ratio)
19800f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
19810f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19820f7887c4SLen Brown 
19830f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
19840f7887c4SLen Brown 	if (ratio)
19850f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
19860f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19870f7887c4SLen Brown 
19880f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
19890f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
19900f7887c4SLen Brown 
19910f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
19920f7887c4SLen Brown 	if (ratio)
19930f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
19940f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19950f7887c4SLen Brown 
19960f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
19970f7887c4SLen Brown 	if (ratio)
19980f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
19990f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20000f7887c4SLen Brown 
20010f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
20020f7887c4SLen Brown 	if (ratio)
20030f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
20040f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20050f7887c4SLen Brown 
20060f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
20070f7887c4SLen Brown 	if (ratio)
20080f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
20090f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20100f7887c4SLen Brown }
20110f7887c4SLen Brown 
20120f7887c4SLen Brown static void
2013fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2014fb5d4327SDasaratharaman Chandramouli {
2015cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2016cbf97abaSHubert Chrzaniuk 
2017fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2018cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2019cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2020cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2021cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2022fb5d4327SDasaratharaman Chandramouli 
2023ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2024fb5d4327SDasaratharaman Chandramouli 
2025b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2026bfae2052SLen Brown 		base_cpu, msr);
2027fb5d4327SDasaratharaman Chandramouli 
2028fb5d4327SDasaratharaman Chandramouli 	/**
2029fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2030cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2031cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2032fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2033fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2034fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2035fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2036fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2037fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2038fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2039fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2040fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2041fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2042fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2043fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2044fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2045fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2046fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2047fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2048fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2049fb5d4327SDasaratharaman Chandramouli 	 */
2050fb5d4327SDasaratharaman Chandramouli 
2051cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2052cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2053cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2054cbf97abaSHubert Chrzaniuk 
2055cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2056fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2057cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2058fb5d4327SDasaratharaman Chandramouli 
2059cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2060cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2061cbf97abaSHubert Chrzaniuk 		b_nr++;
2062fb5d4327SDasaratharaman Chandramouli 	}
2063cbf97abaSHubert Chrzaniuk 
2064cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2065cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2066b7d8c148SLen Brown 			fprintf(outf,
2067710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2068cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2069fb5d4327SDasaratharaman Chandramouli }
2070fb5d4327SDasaratharaman Chandramouli 
2071fb5d4327SDasaratharaman Chandramouli static void
2072fcd17211SLen Brown dump_nhm_cst_cfg(void)
2073fcd17211SLen Brown {
2074fcd17211SLen Brown 	unsigned long long msr;
2075fcd17211SLen Brown 
20761df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2077fcd17211SLen Brown 
2078fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
2079fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
2080fcd17211SLen Brown 
20811df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2082fcd17211SLen Brown 
2083b7d8c148SLen Brown 	fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
2084fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2085fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2086fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2087fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2088fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
20896c34f160SLen Brown 		(unsigned int)msr & 0xF,
2090fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2091fcd17211SLen Brown 	return;
2092103a8feaSLen Brown }
2093103a8feaSLen Brown 
20946fb3143bSLen Brown static void
20956fb3143bSLen Brown dump_config_tdp(void)
20966fb3143bSLen Brown {
20976fb3143bSLen Brown 	unsigned long long msr;
20986fb3143bSLen Brown 
20996fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2100b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2101685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
21026fb3143bSLen Brown 
21036fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2104b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
21056fb3143bSLen Brown 	if (msr) {
2106685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2107685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2108685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2109685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
21106fb3143bSLen Brown 	}
2111b7d8c148SLen Brown 	fprintf(outf, ")\n");
21126fb3143bSLen Brown 
21136fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2114b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
21156fb3143bSLen Brown 	if (msr) {
2116685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2117685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2118685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2119685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
21206fb3143bSLen Brown 	}
2121b7d8c148SLen Brown 	fprintf(outf, ")\n");
21226fb3143bSLen Brown 
21236fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2124b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
21256fb3143bSLen Brown 	if ((msr) & 0x3)
2126b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2127b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2128b7d8c148SLen Brown 	fprintf(outf, ")\n");
21296fb3143bSLen Brown 
21306fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2131b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2132685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2133b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2134b7d8c148SLen Brown 	fprintf(outf, ")\n");
21356fb3143bSLen Brown }
21365a63426eSLen Brown 
21375a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
21385a63426eSLen Brown 
21395a63426eSLen Brown void print_irtl(void)
21405a63426eSLen Brown {
21415a63426eSLen Brown 	unsigned long long msr;
21425a63426eSLen Brown 
21435a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
21445a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
21455a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21465a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21475a63426eSLen Brown 
21485a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
21495a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
21505a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21515a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21525a63426eSLen Brown 
21535a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
21545a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
21555a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21565a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21575a63426eSLen Brown 
21585a63426eSLen Brown 	if (!do_irtl_hsw)
21595a63426eSLen Brown 		return;
21605a63426eSLen Brown 
21615a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
21625a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
21635a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21645a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21655a63426eSLen Brown 
21665a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
21675a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
21685a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21695a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21705a63426eSLen Brown 
21715a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
21725a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
21735a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21745a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21755a63426eSLen Brown 
21765a63426eSLen Brown }
217736229897SLen Brown void free_fd_percpu(void)
217836229897SLen Brown {
217936229897SLen Brown 	int i;
218036229897SLen Brown 
218101a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
218236229897SLen Brown 		if (fd_percpu[i] != 0)
218336229897SLen Brown 			close(fd_percpu[i]);
218436229897SLen Brown 	}
218536229897SLen Brown 
218636229897SLen Brown 	free(fd_percpu);
21876fb3143bSLen Brown }
21886fb3143bSLen Brown 
2189c98d5d94SLen Brown void free_all_buffers(void)
2190103a8feaSLen Brown {
2191c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2192c98d5d94SLen Brown 	cpu_present_set = NULL;
219336229897SLen Brown 	cpu_present_setsize = 0;
2194103a8feaSLen Brown 
2195c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2196c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2197c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2198103a8feaSLen Brown 
2199c98d5d94SLen Brown 	free(thread_even);
2200c98d5d94SLen Brown 	free(core_even);
2201c98d5d94SLen Brown 	free(package_even);
2202103a8feaSLen Brown 
2203c98d5d94SLen Brown 	thread_even = NULL;
2204c98d5d94SLen Brown 	core_even = NULL;
2205c98d5d94SLen Brown 	package_even = NULL;
2206103a8feaSLen Brown 
2207c98d5d94SLen Brown 	free(thread_odd);
2208c98d5d94SLen Brown 	free(core_odd);
2209c98d5d94SLen Brown 	free(package_odd);
2210103a8feaSLen Brown 
2211c98d5d94SLen Brown 	thread_odd = NULL;
2212c98d5d94SLen Brown 	core_odd = NULL;
2213c98d5d94SLen Brown 	package_odd = NULL;
2214103a8feaSLen Brown 
2215c98d5d94SLen Brown 	free(output_buffer);
2216c98d5d94SLen Brown 	output_buffer = NULL;
2217c98d5d94SLen Brown 	outp = NULL;
221836229897SLen Brown 
221936229897SLen Brown 	free_fd_percpu();
2220562a2d37SLen Brown 
2221562a2d37SLen Brown 	free(irq_column_2_cpu);
2222562a2d37SLen Brown 	free(irqs_per_cpu);
2223103a8feaSLen Brown }
2224103a8feaSLen Brown 
222557a42a34SJosh Triplett 
222657a42a34SJosh Triplett /*
222795aebc44SJosh Triplett  * Parse a file containing a single int.
222895aebc44SJosh Triplett  */
222995aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
223095aebc44SJosh Triplett {
223195aebc44SJosh Triplett 	va_list args;
223295aebc44SJosh Triplett 	char path[PATH_MAX];
223395aebc44SJosh Triplett 	FILE *filep;
223495aebc44SJosh Triplett 	int value;
223595aebc44SJosh Triplett 
223695aebc44SJosh Triplett 	va_start(args, fmt);
223795aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
223895aebc44SJosh Triplett 	va_end(args);
223957a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2240b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2241b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
224295aebc44SJosh Triplett 	fclose(filep);
224395aebc44SJosh Triplett 	return value;
224495aebc44SJosh Triplett }
224595aebc44SJosh Triplett 
224695aebc44SJosh Triplett /*
2247e275b388SDasaratharaman Chandramouli  * get_cpu_position_in_core(cpu)
2248e275b388SDasaratharaman Chandramouli  * return the position of the CPU among its HT siblings in the core
2249e275b388SDasaratharaman Chandramouli  * return -1 if the sibling is not in list
2250103a8feaSLen Brown  */
2251e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu)
2252103a8feaSLen Brown {
2253e275b388SDasaratharaman Chandramouli 	char path[64];
2254e275b388SDasaratharaman Chandramouli 	FILE *filep;
2255e275b388SDasaratharaman Chandramouli 	int this_cpu;
2256e275b388SDasaratharaman Chandramouli 	char character;
2257e275b388SDasaratharaman Chandramouli 	int i;
2258e275b388SDasaratharaman Chandramouli 
2259e275b388SDasaratharaman Chandramouli 	sprintf(path,
2260e275b388SDasaratharaman Chandramouli 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2261e275b388SDasaratharaman Chandramouli 		cpu);
2262e275b388SDasaratharaman Chandramouli 	filep = fopen(path, "r");
2263e275b388SDasaratharaman Chandramouli 	if (filep == NULL) {
2264e275b388SDasaratharaman Chandramouli 		perror(path);
2265e275b388SDasaratharaman Chandramouli 		exit(1);
2266e275b388SDasaratharaman Chandramouli 	}
2267e275b388SDasaratharaman Chandramouli 
2268e275b388SDasaratharaman Chandramouli 	for (i = 0; i < topo.num_threads_per_core; i++) {
2269e275b388SDasaratharaman Chandramouli 		fscanf(filep, "%d", &this_cpu);
2270e275b388SDasaratharaman Chandramouli 		if (this_cpu == cpu) {
2271e275b388SDasaratharaman Chandramouli 			fclose(filep);
2272e275b388SDasaratharaman Chandramouli 			return i;
2273e275b388SDasaratharaman Chandramouli 		}
2274e275b388SDasaratharaman Chandramouli 
2275e275b388SDasaratharaman Chandramouli 		/* Account for no separator after last thread*/
2276e275b388SDasaratharaman Chandramouli 		if (i != (topo.num_threads_per_core - 1))
2277e275b388SDasaratharaman Chandramouli 			fscanf(filep, "%c", &character);
2278e275b388SDasaratharaman Chandramouli 	}
2279e275b388SDasaratharaman Chandramouli 
2280e275b388SDasaratharaman Chandramouli 	fclose(filep);
2281e275b388SDasaratharaman Chandramouli 	return -1;
2282c98d5d94SLen Brown }
2283103a8feaSLen Brown 
2284c98d5d94SLen Brown /*
2285c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2286c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2287c98d5d94SLen Brown  */
2288c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2289c98d5d94SLen Brown {
229095aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2291103a8feaSLen Brown }
2292103a8feaSLen Brown 
2293103a8feaSLen Brown int get_physical_package_id(int cpu)
2294103a8feaSLen Brown {
229595aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2296103a8feaSLen Brown }
2297103a8feaSLen Brown 
2298103a8feaSLen Brown int get_core_id(int cpu)
2299103a8feaSLen Brown {
230095aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2301103a8feaSLen Brown }
2302103a8feaSLen Brown 
2303c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
2304c98d5d94SLen Brown {
2305c98d5d94SLen Brown 	char path[80];
2306c98d5d94SLen Brown 	FILE *filep;
2307e275b388SDasaratharaman Chandramouli 	int sib1;
2308e275b388SDasaratharaman Chandramouli 	int matches = 0;
2309c98d5d94SLen Brown 	char character;
2310e275b388SDasaratharaman Chandramouli 	char str[100];
2311e275b388SDasaratharaman Chandramouli 	char *ch;
2312c98d5d94SLen Brown 
2313c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
231457a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2315e275b388SDasaratharaman Chandramouli 
2316103a8feaSLen Brown 	/*
2317c98d5d94SLen Brown 	 * file format:
2318e275b388SDasaratharaman Chandramouli 	 * A ',' separated or '-' separated set of numbers
2319e275b388SDasaratharaman Chandramouli 	 * (eg 1-2 or 1,3,4,5)
2320c98d5d94SLen Brown 	 */
2321e275b388SDasaratharaman Chandramouli 	fscanf(filep, "%d%c\n", &sib1, &character);
2322e275b388SDasaratharaman Chandramouli 	fseek(filep, 0, SEEK_SET);
2323e275b388SDasaratharaman Chandramouli 	fgets(str, 100, filep);
2324e275b388SDasaratharaman Chandramouli 	ch = strchr(str, character);
2325e275b388SDasaratharaman Chandramouli 	while (ch != NULL) {
2326e275b388SDasaratharaman Chandramouli 		matches++;
2327e275b388SDasaratharaman Chandramouli 		ch = strchr(ch+1, character);
2328e275b388SDasaratharaman Chandramouli 	}
2329c98d5d94SLen Brown 
2330c98d5d94SLen Brown 	fclose(filep);
2331e275b388SDasaratharaman Chandramouli 	return matches+1;
2332c98d5d94SLen Brown }
2333c98d5d94SLen Brown 
2334c98d5d94SLen Brown /*
2335c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2336c98d5d94SLen Brown  * skip non-present cpus
2337103a8feaSLen Brown  */
2338103a8feaSLen Brown 
2339c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2340c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2341c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2342c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2343c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2344c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2345c98d5d94SLen Brown {
2346c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
2347c98d5d94SLen Brown 
2348c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2349c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2350c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
2351c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
2352c98d5d94SLen Brown 				struct thread_data *t, *t2;
2353c98d5d94SLen Brown 				struct core_data *c, *c2;
2354c98d5d94SLen Brown 				struct pkg_data *p, *p2;
2355c98d5d94SLen Brown 
2356c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2357c98d5d94SLen Brown 
2358c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
2359c98d5d94SLen Brown 					continue;
2360c98d5d94SLen Brown 
2361c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2362c98d5d94SLen Brown 
2363c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
2364c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
2365c98d5d94SLen Brown 
2366c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
2367c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
2368c98d5d94SLen Brown 
2369c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
2370c98d5d94SLen Brown 				if (retval)
2371c98d5d94SLen Brown 					return retval;
2372c98d5d94SLen Brown 			}
2373c98d5d94SLen Brown 		}
2374c98d5d94SLen Brown 	}
2375c98d5d94SLen Brown 	return 0;
2376c98d5d94SLen Brown }
2377c98d5d94SLen Brown 
2378c98d5d94SLen Brown /*
2379c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2380c98d5d94SLen Brown  * return max_cpu number
2381c98d5d94SLen Brown  */
2382c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2383103a8feaSLen Brown {
2384103a8feaSLen Brown 	FILE *fp;
2385c98d5d94SLen Brown 	int cpu_num;
2386103a8feaSLen Brown 	int retval;
2387103a8feaSLen Brown 
238857a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2389103a8feaSLen Brown 
2390103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2391b2c95d90SJosh Triplett 	if (retval != 0)
2392b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2393103a8feaSLen Brown 
2394c98d5d94SLen Brown 	while (1) {
2395c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2396103a8feaSLen Brown 		if (retval != 1)
2397103a8feaSLen Brown 			break;
2398103a8feaSLen Brown 
2399c98d5d94SLen Brown 		retval = func(cpu_num);
2400c98d5d94SLen Brown 		if (retval) {
2401c98d5d94SLen Brown 			fclose(fp);
2402c98d5d94SLen Brown 			return(retval);
2403c98d5d94SLen Brown 		}
2404103a8feaSLen Brown 	}
2405103a8feaSLen Brown 	fclose(fp);
2406c98d5d94SLen Brown 	return 0;
2407103a8feaSLen Brown }
2408103a8feaSLen Brown 
2409103a8feaSLen Brown void re_initialize(void)
2410103a8feaSLen Brown {
2411c98d5d94SLen Brown 	free_all_buffers();
2412c98d5d94SLen Brown 	setup_all_buffers();
2413c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2414103a8feaSLen Brown }
2415103a8feaSLen Brown 
2416c98d5d94SLen Brown 
2417103a8feaSLen Brown /*
2418c98d5d94SLen Brown  * count_cpus()
2419c98d5d94SLen Brown  * remember the last one seen, it will be the max
2420103a8feaSLen Brown  */
2421c98d5d94SLen Brown int count_cpus(int cpu)
2422103a8feaSLen Brown {
2423c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
2424c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
2425103a8feaSLen Brown 
2426c98d5d94SLen Brown 	topo.num_cpus += 1;
2427c98d5d94SLen Brown 	return 0;
2428103a8feaSLen Brown }
2429c98d5d94SLen Brown int mark_cpu_present(int cpu)
2430c98d5d94SLen Brown {
2431c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
243215aaa346SLen Brown 	return 0;
2433103a8feaSLen Brown }
2434103a8feaSLen Brown 
2435562a2d37SLen Brown /*
2436562a2d37SLen Brown  * snapshot_proc_interrupts()
2437562a2d37SLen Brown  *
2438562a2d37SLen Brown  * read and record summary of /proc/interrupts
2439562a2d37SLen Brown  *
2440562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2441562a2d37SLen Brown  */
2442562a2d37SLen Brown int snapshot_proc_interrupts(void)
2443562a2d37SLen Brown {
2444562a2d37SLen Brown 	static FILE *fp;
2445562a2d37SLen Brown 	int column, retval;
2446562a2d37SLen Brown 
2447562a2d37SLen Brown 	if (fp == NULL)
2448562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2449562a2d37SLen Brown 	else
2450562a2d37SLen Brown 		rewind(fp);
2451562a2d37SLen Brown 
2452562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2453562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2454562a2d37SLen Brown 		int cpu_number;
2455562a2d37SLen Brown 
2456562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2457562a2d37SLen Brown 		if (retval != 1)
2458562a2d37SLen Brown 			break;
2459562a2d37SLen Brown 
2460562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2461562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2462562a2d37SLen Brown 			return 1;
2463562a2d37SLen Brown 		}
2464562a2d37SLen Brown 
2465562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2466562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2467562a2d37SLen Brown 	}
2468562a2d37SLen Brown 
2469562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2470562a2d37SLen Brown 	while (1) {
2471562a2d37SLen Brown 		int column;
2472562a2d37SLen Brown 		char buf[64];
2473562a2d37SLen Brown 
2474562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2475562a2d37SLen Brown 		if (retval != 1)
2476562a2d37SLen Brown 			break;
2477562a2d37SLen Brown 
2478562a2d37SLen Brown 		/* read the count per cpu */
2479562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2480562a2d37SLen Brown 
2481562a2d37SLen Brown 			int cpu_number, irq_count;
2482562a2d37SLen Brown 
2483562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2484562a2d37SLen Brown 			if (retval != 1)
2485562a2d37SLen Brown 				break;
2486562a2d37SLen Brown 
2487562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2488562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2489562a2d37SLen Brown 
2490562a2d37SLen Brown 		}
2491562a2d37SLen Brown 
2492562a2d37SLen Brown 		while (getc(fp) != '\n')
2493562a2d37SLen Brown 			;	/* flush interrupt description */
2494562a2d37SLen Brown 
2495562a2d37SLen Brown 	}
2496562a2d37SLen Brown 	return 0;
2497562a2d37SLen Brown }
249827d47356SLen Brown /*
2499fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2500fdf676e5SLen Brown  *
2501fdf676e5SLen Brown  * record snapshot of
2502fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2503fdf676e5SLen Brown  *
2504fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2505fdf676e5SLen Brown  */
2506fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2507fdf676e5SLen Brown {
2508fdf676e5SLen Brown 	FILE *fp;
2509fdf676e5SLen Brown 	int retval;
2510fdf676e5SLen Brown 
2511fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2512fdf676e5SLen Brown 
2513fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2514fdf676e5SLen Brown 	if (retval != 1)
2515fdf676e5SLen Brown 		err(1, "GFX rc6");
2516fdf676e5SLen Brown 
2517fdf676e5SLen Brown 	fclose(fp);
2518fdf676e5SLen Brown 
2519fdf676e5SLen Brown 	return 0;
2520fdf676e5SLen Brown }
2521fdf676e5SLen Brown /*
252227d47356SLen Brown  * snapshot_gfx_mhz()
252327d47356SLen Brown  *
252427d47356SLen Brown  * record snapshot of
252527d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
252627d47356SLen Brown  *
252727d47356SLen Brown  * return 1 if config change requires a restart, else return 0
252827d47356SLen Brown  */
252927d47356SLen Brown int snapshot_gfx_mhz(void)
253027d47356SLen Brown {
253127d47356SLen Brown 	static FILE *fp;
253227d47356SLen Brown 	int retval;
253327d47356SLen Brown 
253427d47356SLen Brown 	if (fp == NULL)
253527d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
253622048c54SLen Brown 	else {
253727d47356SLen Brown 		rewind(fp);
253822048c54SLen Brown 		fflush(fp);
253922048c54SLen Brown 	}
254027d47356SLen Brown 
254127d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
254227d47356SLen Brown 	if (retval != 1)
254327d47356SLen Brown 		err(1, "GFX MHz");
254427d47356SLen Brown 
254527d47356SLen Brown 	return 0;
254627d47356SLen Brown }
2547562a2d37SLen Brown 
2548562a2d37SLen Brown /*
2549562a2d37SLen Brown  * snapshot /proc and /sys files
2550562a2d37SLen Brown  *
2551562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2552562a2d37SLen Brown  */
2553562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2554562a2d37SLen Brown {
2555218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2556562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2557562a2d37SLen Brown 			return 1;
2558562a2d37SLen Brown 
2559812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2560fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2561fdf676e5SLen Brown 
2562812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
256327d47356SLen Brown 		snapshot_gfx_mhz();
256427d47356SLen Brown 
2565562a2d37SLen Brown 	return 0;
2566562a2d37SLen Brown }
2567562a2d37SLen Brown 
2568103a8feaSLen Brown void turbostat_loop()
2569103a8feaSLen Brown {
2570c98d5d94SLen Brown 	int retval;
2571e52966c0SLen Brown 	int restarted = 0;
2572c98d5d94SLen Brown 
2573103a8feaSLen Brown restart:
2574e52966c0SLen Brown 	restarted++;
2575e52966c0SLen Brown 
2576562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2577c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2578d91bb17cSLen Brown 	if (retval < -1) {
2579d91bb17cSLen Brown 		exit(retval);
2580d91bb17cSLen Brown 	} else if (retval == -1) {
2581e52966c0SLen Brown 		if (restarted > 1) {
2582e52966c0SLen Brown 			exit(retval);
2583e52966c0SLen Brown 		}
2584c98d5d94SLen Brown 		re_initialize();
2585c98d5d94SLen Brown 		goto restart;
2586c98d5d94SLen Brown 	}
2587e52966c0SLen Brown 	restarted = 0;
2588103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2589103a8feaSLen Brown 
2590103a8feaSLen Brown 	while (1) {
2591c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2592103a8feaSLen Brown 			re_initialize();
2593103a8feaSLen Brown 			goto restart;
2594103a8feaSLen Brown 		}
25952a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2596562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2597562a2d37SLen Brown 			goto restart;
2598c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
2599d91bb17cSLen Brown 		if (retval < -1) {
2600d91bb17cSLen Brown 			exit(retval);
2601d91bb17cSLen Brown 		} else if (retval == -1) {
260215aaa346SLen Brown 			re_initialize();
260315aaa346SLen Brown 			goto restart;
260415aaa346SLen Brown 		}
2605103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
2606103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
2607ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2608ba3dec99SLen Brown 			re_initialize();
2609ba3dec99SLen Brown 			goto restart;
2610ba3dec99SLen Brown 		}
2611c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
2612c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
2613b7d8c148SLen Brown 		flush_output_stdout();
26142a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2615562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2616562a2d37SLen Brown 			goto restart;
2617c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2618d91bb17cSLen Brown 		if (retval < -1) {
2619d91bb17cSLen Brown 			exit(retval);
2620d91bb17cSLen Brown 		} else if (retval == -1) {
2621103a8feaSLen Brown 			re_initialize();
2622103a8feaSLen Brown 			goto restart;
2623103a8feaSLen Brown 		}
2624103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
2625103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
2626ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2627ba3dec99SLen Brown 			re_initialize();
2628ba3dec99SLen Brown 			goto restart;
2629ba3dec99SLen Brown 		}
2630c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
2631c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
2632b7d8c148SLen Brown 		flush_output_stdout();
2633103a8feaSLen Brown 	}
2634103a8feaSLen Brown }
2635103a8feaSLen Brown 
2636103a8feaSLen Brown void check_dev_msr()
2637103a8feaSLen Brown {
2638103a8feaSLen Brown 	struct stat sb;
26397ce7d5deSPrarit Bhargava 	char pathname[32];
2640103a8feaSLen Brown 
26417ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
26427ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
2643a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2644d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2645103a8feaSLen Brown }
2646103a8feaSLen Brown 
264798481e79SLen Brown void check_permissions()
2648103a8feaSLen Brown {
264998481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
265098481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
265198481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
265298481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
265398481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
265498481e79SLen Brown 	int do_exit = 0;
26557ce7d5deSPrarit Bhargava 	char pathname[32];
265698481e79SLen Brown 
265798481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
265898481e79SLen Brown 	cap_header->pid = getpid();
265998481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
266098481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
266198481e79SLen Brown 		err(-6, "capget(2) failed");
266298481e79SLen Brown 
266398481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
266498481e79SLen Brown 		do_exit++;
266598481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
266698481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
266798481e79SLen Brown 	}
266898481e79SLen Brown 
266998481e79SLen Brown 	/* test file permissions */
26707ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
26717ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
267298481e79SLen Brown 		do_exit++;
267398481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
267498481e79SLen Brown 	}
267598481e79SLen Brown 
267698481e79SLen Brown 	/* if all else fails, thell them to be root */
267798481e79SLen Brown 	if (do_exit)
2678b2c95d90SJosh Triplett 		if (getuid() != 0)
2679d7899447SLen Brown 			warnx("... or simply run as root");
268098481e79SLen Brown 
268198481e79SLen Brown 	if (do_exit)
268298481e79SLen Brown 		exit(-6);
2683103a8feaSLen Brown }
2684103a8feaSLen Brown 
2685d7899447SLen Brown /*
2686d7899447SLen Brown  * NHM adds support for additional MSRs:
2687d7899447SLen Brown  *
2688d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
2689d7899447SLen Brown  *
2690ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
26911df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
2692d7899447SLen Brown  *
2693cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
2694cf4cbe53SLen Brown  *
2695d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
2696d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
2697d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
2698d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
2699d7899447SLen Brown  *
2700ee7e38e3SLen Brown  * Side effect:
27011df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
270233148d67SLen Brown  * sets has_misc_feature_control
2703d7899447SLen Brown  */
2704ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
2705103a8feaSLen Brown {
2706ee7e38e3SLen Brown 	unsigned long long msr;
270721ed5574SLen Brown 	unsigned int base_ratio;
2708ee7e38e3SLen Brown 	int *pkg_cstate_limits;
2709ee7e38e3SLen Brown 
2710103a8feaSLen Brown 	if (!genuine_intel)
2711103a8feaSLen Brown 		return 0;
2712103a8feaSLen Brown 
2713103a8feaSLen Brown 	if (family != 6)
2714103a8feaSLen Brown 		return 0;
2715103a8feaSLen Brown 
271621ed5574SLen Brown 	bclk = discover_bclk(family, model);
271721ed5574SLen Brown 
2718103a8feaSLen Brown 	switch (model) {
2719869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2720869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2721103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
2722869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
2723869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
2724869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2725869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2726ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
2727ee7e38e3SLen Brown 		break;
2728869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
2729869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
2730869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2731869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2732ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
273333148d67SLen Brown 		has_misc_feature_control = 1;
2734ee7e38e3SLen Brown 		break;
2735869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2736869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2737869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2738869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2739869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2740869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2741869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2742869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2743869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2744869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2745869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2746869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2747ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
274833148d67SLen Brown 		has_misc_feature_control = 1;
2749ee7e38e3SLen Brown 		break;
2750d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
2751d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
275233148d67SLen Brown 		has_misc_feature_control = 1;
2753d8ebb442SLen Brown 		break;
2754869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
2755cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2756869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
2757ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
2758ee7e38e3SLen Brown 		break;
2759869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
2760ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
2761cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2762ee7e38e3SLen Brown 		break;
2763869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
2764005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2765ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
2766ee7e38e3SLen Brown 		break;
2767869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
2768ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
2769869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
2770e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
2771e4085d54SLen Brown 		break;
2772103a8feaSLen Brown 	default:
2773103a8feaSLen Brown 		return 0;
2774103a8feaSLen Brown 	}
27751df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2776e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2777ee7e38e3SLen Brown 
2778ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
277921ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
278021ed5574SLen Brown 
278121ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
278221ed5574SLen Brown 	has_base_hz = 1;
2783ee7e38e3SLen Brown 	return 1;
2784103a8feaSLen Brown }
27850f7887c4SLen Brown /*
2786495c7654SLen Brown  * SLV client has support for unique MSRs:
27870f7887c4SLen Brown  *
27880f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
27890f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
27900f7887c4SLen Brown  */
27910f7887c4SLen Brown 
27920f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
27930f7887c4SLen Brown {
27940f7887c4SLen Brown 	if (!genuine_intel)
27950f7887c4SLen Brown 		return 0;
27960f7887c4SLen Brown 
27970f7887c4SLen Brown 	switch (model) {
27980f7887c4SLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
27990f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MERRIFIELD:
28000f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MOOREFIELD:
28010f7887c4SLen Brown 		return 1;
28020f7887c4SLen Brown 	}
28030f7887c4SLen Brown 	return 0;
28040f7887c4SLen Brown }
28057170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
28067170a374SLen Brown {
28077170a374SLen Brown 
28087170a374SLen Brown 	if (!genuine_intel)
28097170a374SLen Brown 		return 0;
28107170a374SLen Brown 
28117170a374SLen Brown 	switch (model) {
28127170a374SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
28137170a374SLen Brown 		return 1;
28147170a374SLen Brown 	}
28157170a374SLen Brown 	return 0;
28167170a374SLen Brown }
2817ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
2818ade0ebacSLen Brown {
2819ade0ebacSLen Brown 
2820ade0ebacSLen Brown 	if (!genuine_intel)
2821ade0ebacSLen Brown 		return 0;
2822ade0ebacSLen Brown 
2823ade0ebacSLen Brown 	switch (model) {
2824ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
2825ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:
2826ade0ebacSLen Brown 		return 1;
2827ade0ebacSLen Brown 	}
2828ade0ebacSLen Brown 	return 0;
2829ade0ebacSLen Brown }
283034c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
283134c76197SLen Brown {
283234c76197SLen Brown 
283334c76197SLen Brown 	if (!genuine_intel)
283434c76197SLen Brown 		return 0;
283534c76197SLen Brown 
283634c76197SLen Brown 	switch (model) {
283734c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
283834c76197SLen Brown 		return 1;
283934c76197SLen Brown 	}
284034c76197SLen Brown 	return 0;
284134c76197SLen Brown }
28420f7887c4SLen Brown 
284331e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
2844d7899447SLen Brown {
28450f7887c4SLen Brown 	if (has_slv_msrs(family, model))
28460f7887c4SLen Brown 		return 0;
28470f7887c4SLen Brown 
2848d7899447SLen Brown 	switch (model) {
2849d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
2850869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2851869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2852869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
2853005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2854d7899447SLen Brown 		return 0;
2855d7899447SLen Brown 	default:
2856d7899447SLen Brown 		return 1;
2857d7899447SLen Brown 	}
2858d7899447SLen Brown }
28590f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
28600f7887c4SLen Brown {
28610f7887c4SLen Brown 	if (has_slv_msrs(family, model))
28620f7887c4SLen Brown 		return 1;
28630f7887c4SLen Brown 
28640f7887c4SLen Brown 	return 0;
28650f7887c4SLen Brown }
28666574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
28676574a5d5SLen Brown {
28686574a5d5SLen Brown 	if (!genuine_intel)
28696574a5d5SLen Brown 		return 0;
28706574a5d5SLen Brown 
28716574a5d5SLen Brown 	if (family != 6)
28726574a5d5SLen Brown 		return 0;
28736574a5d5SLen Brown 
28746574a5d5SLen Brown 	switch (model) {
2875869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2876869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
28776574a5d5SLen Brown 		return 1;
28786574a5d5SLen Brown 	default:
28796574a5d5SLen Brown 		return 0;
28806574a5d5SLen Brown 	}
28816574a5d5SLen Brown }
2882fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2883fcd17211SLen Brown {
2884fcd17211SLen Brown 	if (!genuine_intel)
2885fcd17211SLen Brown 		return 0;
2886fcd17211SLen Brown 
2887fcd17211SLen Brown 	if (family != 6)
2888fcd17211SLen Brown 		return 0;
2889fcd17211SLen Brown 
2890fcd17211SLen Brown 	switch (model) {
2891869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
2892fcd17211SLen Brown 		return 1;
2893fcd17211SLen Brown 	default:
2894fcd17211SLen Brown 		return 0;
2895fcd17211SLen Brown 	}
2896fcd17211SLen Brown }
2897fcd17211SLen Brown 
2898fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2899fb5d4327SDasaratharaman Chandramouli {
2900fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
2901fb5d4327SDasaratharaman Chandramouli 		return 0;
2902fb5d4327SDasaratharaman Chandramouli 
2903fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
2904fb5d4327SDasaratharaman Chandramouli 		return 0;
2905fb5d4327SDasaratharaman Chandramouli 
2906fb5d4327SDasaratharaman Chandramouli 	switch (model) {
2907869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2908005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2909fb5d4327SDasaratharaman Chandramouli 		return 1;
2910fb5d4327SDasaratharaman Chandramouli 	default:
2911fb5d4327SDasaratharaman Chandramouli 		return 0;
2912fb5d4327SDasaratharaman Chandramouli 	}
2913fb5d4327SDasaratharaman Chandramouli }
291431e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
291531e07522SLen Brown {
291631e07522SLen Brown 	if (!genuine_intel)
291731e07522SLen Brown 		return 0;
291831e07522SLen Brown 
291931e07522SLen Brown 	if (family != 6)
292031e07522SLen Brown 		return 0;
292131e07522SLen Brown 
292231e07522SLen Brown 	switch (model) {
292331e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
292431e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
292531e07522SLen Brown 		return 1;
292631e07522SLen Brown 	default:
292731e07522SLen Brown 		return 0;
292831e07522SLen Brown 	}
292931e07522SLen Brown }
29306fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
29316fb3143bSLen Brown {
29326fb3143bSLen Brown 	if (!genuine_intel)
29336fb3143bSLen Brown 		return 0;
29346fb3143bSLen Brown 
29356fb3143bSLen Brown 	if (family != 6)
29366fb3143bSLen Brown 		return 0;
29376fb3143bSLen Brown 
29386fb3143bSLen Brown 	switch (model) {
2939869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2940869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2941869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2942869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2943869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2944869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2945869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2946869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2947869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2948869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2949869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2950869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2951869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2952869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
29536fb3143bSLen Brown 
2954869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2955005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
29566fb3143bSLen Brown 		return 1;
29576fb3143bSLen Brown 	default:
29586fb3143bSLen Brown 		return 0;
29596fb3143bSLen Brown 	}
29606fb3143bSLen Brown }
29616fb3143bSLen Brown 
2962fcd17211SLen Brown static void
29631b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
2964fcd17211SLen Brown {
2965fcd17211SLen Brown 	if (!do_nhm_platform_info)
2966fcd17211SLen Brown 		return;
2967fcd17211SLen Brown 
2968fcd17211SLen Brown 	dump_nhm_platform_info();
2969fcd17211SLen Brown 
2970fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
2971fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
2972fcd17211SLen Brown 
2973fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
2974fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
2975fcd17211SLen Brown 
297631e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
297731e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
2978fcd17211SLen Brown 
29790f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
29800f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
29810f7887c4SLen Brown 
2982fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
2983fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
2984fb5d4327SDasaratharaman Chandramouli 
29856fb3143bSLen Brown 	if (has_config_tdp(family, model))
29866fb3143bSLen Brown 		dump_config_tdp();
29876fb3143bSLen Brown 
2988fcd17211SLen Brown 	dump_nhm_cst_cfg();
2989fcd17211SLen Brown }
2990fcd17211SLen Brown 
299141618e63SLen Brown static void
299241618e63SLen Brown dump_sysfs_cstate_config(void)
299341618e63SLen Brown {
299441618e63SLen Brown 	char path[64];
299541618e63SLen Brown 	char name_buf[16];
299641618e63SLen Brown 	char desc[64];
299741618e63SLen Brown 	FILE *input;
299841618e63SLen Brown 	int state;
299941618e63SLen Brown 	char *sp;
300041618e63SLen Brown 
300141618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
300241618e63SLen Brown 		return;
300341618e63SLen Brown 
300441618e63SLen Brown 	for (state = 0; state < 10; ++state) {
300541618e63SLen Brown 
300641618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
300741618e63SLen Brown 			base_cpu, state);
300841618e63SLen Brown 		input = fopen(path, "r");
300941618e63SLen Brown 		if (input == NULL)
301041618e63SLen Brown 			continue;
301141618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
301241618e63SLen Brown 
301341618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
301441618e63SLen Brown 		sp = strchr(name_buf, '-');
301541618e63SLen Brown 		if (!sp)
301641618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
301741618e63SLen Brown 		*sp = '\0';
301841618e63SLen Brown 
301941618e63SLen Brown 		fclose(input);
302041618e63SLen Brown 
302141618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
302241618e63SLen Brown 			base_cpu, state);
302341618e63SLen Brown 		input = fopen(path, "r");
302441618e63SLen Brown 		if (input == NULL)
302541618e63SLen Brown 			continue;
302641618e63SLen Brown 		fgets(desc, sizeof(desc), input);
302741618e63SLen Brown 
302841618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
302941618e63SLen Brown 		fclose(input);
303041618e63SLen Brown 	}
303141618e63SLen Brown }
30327293fccdSLen Brown static void
30337293fccdSLen Brown dump_sysfs_pstate_config(void)
30347293fccdSLen Brown {
30357293fccdSLen Brown 	char path[64];
30367293fccdSLen Brown 	char driver_buf[64];
30377293fccdSLen Brown 	char governor_buf[64];
30387293fccdSLen Brown 	FILE *input;
30397293fccdSLen Brown 	int turbo;
30407293fccdSLen Brown 
30417293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
30427293fccdSLen Brown 			base_cpu);
30437293fccdSLen Brown 	input = fopen(path, "r");
30447293fccdSLen Brown 	if (input == NULL) {
30457293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
30467293fccdSLen Brown 		return;
30477293fccdSLen Brown 	}
30487293fccdSLen Brown 	fgets(driver_buf, sizeof(driver_buf), input);
30497293fccdSLen Brown 	fclose(input);
30507293fccdSLen Brown 
30517293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
30527293fccdSLen Brown 			base_cpu);
30537293fccdSLen Brown 	input = fopen(path, "r");
30547293fccdSLen Brown 	if (input == NULL) {
30557293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
30567293fccdSLen Brown 		return;
30577293fccdSLen Brown 	}
30587293fccdSLen Brown 	fgets(governor_buf, sizeof(governor_buf), input);
30597293fccdSLen Brown 	fclose(input);
30607293fccdSLen Brown 
30617293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
30627293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
30637293fccdSLen Brown 
30647293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
30657293fccdSLen Brown 	input = fopen(path, "r");
30667293fccdSLen Brown 	if (input != NULL) {
30677293fccdSLen Brown 		fscanf(input, "%d", &turbo);
30687293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
30697293fccdSLen Brown 		fclose(input);
30707293fccdSLen Brown 	}
30717293fccdSLen Brown 
30727293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
30737293fccdSLen Brown 	input = fopen(path, "r");
30747293fccdSLen Brown 	if (input != NULL) {
30757293fccdSLen Brown 		fscanf(input, "%d", &turbo);
30767293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
30777293fccdSLen Brown 		fclose(input);
30787293fccdSLen Brown 	}
30797293fccdSLen Brown }
308041618e63SLen Brown 
30816574a5d5SLen Brown 
3082889facbeSLen Brown /*
3083889facbeSLen Brown  * print_epb()
3084889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3085889facbeSLen Brown  */
3086889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3087889facbeSLen Brown {
3088889facbeSLen Brown 	unsigned long long msr;
3089889facbeSLen Brown 	char *epb_string;
3090889facbeSLen Brown 	int cpu;
3091889facbeSLen Brown 
3092889facbeSLen Brown 	if (!has_epb)
3093889facbeSLen Brown 		return 0;
3094889facbeSLen Brown 
3095889facbeSLen Brown 	cpu = t->cpu_id;
3096889facbeSLen Brown 
3097889facbeSLen Brown 	/* EPB is per-package */
3098889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3099889facbeSLen Brown 		return 0;
3100889facbeSLen Brown 
3101889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3102b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3103889facbeSLen Brown 		return -1;
3104889facbeSLen Brown 	}
3105889facbeSLen Brown 
3106889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3107889facbeSLen Brown 		return 0;
3108889facbeSLen Brown 
3109e9be7dd6SLen Brown 	switch (msr & 0xF) {
3110889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3111889facbeSLen Brown 		epb_string = "performance";
3112889facbeSLen Brown 		break;
3113889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3114889facbeSLen Brown 		epb_string = "balanced";
3115889facbeSLen Brown 		break;
3116889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3117889facbeSLen Brown 		epb_string = "powersave";
3118889facbeSLen Brown 		break;
3119889facbeSLen Brown 	default:
3120889facbeSLen Brown 		epb_string = "custom";
3121889facbeSLen Brown 		break;
3122889facbeSLen Brown 	}
3123b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3124889facbeSLen Brown 
3125889facbeSLen Brown 	return 0;
3126889facbeSLen Brown }
31277f5c258eSLen Brown /*
31287f5c258eSLen Brown  * print_hwp()
31297f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
31307f5c258eSLen Brown  */
31317f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
31327f5c258eSLen Brown {
31337f5c258eSLen Brown 	unsigned long long msr;
31347f5c258eSLen Brown 	int cpu;
31357f5c258eSLen Brown 
31367f5c258eSLen Brown 	if (!has_hwp)
31377f5c258eSLen Brown 		return 0;
31387f5c258eSLen Brown 
31397f5c258eSLen Brown 	cpu = t->cpu_id;
31407f5c258eSLen Brown 
31417f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
31427f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
31437f5c258eSLen Brown 		return 0;
31447f5c258eSLen Brown 
31457f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3146b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
31477f5c258eSLen Brown 		return -1;
31487f5c258eSLen Brown 	}
31497f5c258eSLen Brown 
31507f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
31517f5c258eSLen Brown 		return 0;
31527f5c258eSLen Brown 
3153b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
31547f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
31557f5c258eSLen Brown 
31567f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
31577f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
31587f5c258eSLen Brown 		return 0;
31597f5c258eSLen Brown 
31607f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
31617f5c258eSLen Brown 		return 0;
31627f5c258eSLen Brown 
3163b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
31646dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
31657f5c258eSLen Brown 			cpu, msr,
31667f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
31677f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
31687f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
31697f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
31707f5c258eSLen Brown 
31717f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
31727f5c258eSLen Brown 		return 0;
31737f5c258eSLen Brown 
3174b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
31756dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
31767f5c258eSLen Brown 			cpu, msr,
31777f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
31787f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
31797f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
31807f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
31817f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
31827f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
31837f5c258eSLen Brown 
31847f5c258eSLen Brown 	if (has_hwp_pkg) {
31857f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
31867f5c258eSLen Brown 			return 0;
31877f5c258eSLen Brown 
3188b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
31896dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
31907f5c258eSLen Brown 			cpu, msr,
31917f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
31927f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
31937f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
31947f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
31957f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
31967f5c258eSLen Brown 	}
31977f5c258eSLen Brown 	if (has_hwp_notify) {
31987f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
31997f5c258eSLen Brown 			return 0;
32007f5c258eSLen Brown 
3201b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
32027f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
32037f5c258eSLen Brown 			cpu, msr,
32047f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
32057f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
32067f5c258eSLen Brown 	}
32077f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
32087f5c258eSLen Brown 		return 0;
32097f5c258eSLen Brown 
3210b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
32117f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
32127f5c258eSLen Brown 			cpu, msr,
32137f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
32147f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3215889facbeSLen Brown 
3216889facbeSLen Brown 	return 0;
3217889facbeSLen Brown }
3218889facbeSLen Brown 
32193a9a941dSLen Brown /*
32203a9a941dSLen Brown  * print_perf_limit()
32213a9a941dSLen Brown  */
32223a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
32233a9a941dSLen Brown {
32243a9a941dSLen Brown 	unsigned long long msr;
32253a9a941dSLen Brown 	int cpu;
32263a9a941dSLen Brown 
32273a9a941dSLen Brown 	cpu = t->cpu_id;
32283a9a941dSLen Brown 
32293a9a941dSLen Brown 	/* per-package */
32303a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
32313a9a941dSLen Brown 		return 0;
32323a9a941dSLen Brown 
32333a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3234b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
32353a9a941dSLen Brown 		return -1;
32363a9a941dSLen Brown 	}
32373a9a941dSLen Brown 
32383a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
32393a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3240b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3241b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3242e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
32433a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3244e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3245e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3246e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3247e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3248e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3249e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3250e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3251e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3252e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3253e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3254e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3255e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3256b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3257e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
32583a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3259e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3260e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3261e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3262e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3263e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3264e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3265e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3266e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3267e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3268e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3269e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3270e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
32713a9a941dSLen Brown 
32723a9a941dSLen Brown 	}
32733a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
32743a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3275b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3276b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
32773a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
32783a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
32793a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
32803a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
32813a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
32823a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
32833a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
32843a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3285b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
32863a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
32873a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
32883a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
32893a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
32903a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
32913a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
32923a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
32933a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
32943a9a941dSLen Brown 	}
32953a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
32963a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3297b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3298b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
32993a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
33003a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
33013a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
33023a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
33033a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
33043a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3305b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
33063a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
33073a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
33083a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
33093a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
33103a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
33113a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
33123a9a941dSLen Brown 	}
33133a9a941dSLen Brown 	return 0;
33143a9a941dSLen Brown }
33153a9a941dSLen Brown 
3316889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3317889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3318889facbeSLen Brown 
33191b69317dSColin Ian King double get_tdp(unsigned int model)
3320144b44b1SLen Brown {
3321144b44b1SLen Brown 	unsigned long long msr;
3322144b44b1SLen Brown 
3323144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
33247ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3325144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3326144b44b1SLen Brown 
3327144b44b1SLen Brown 	switch (model) {
3328869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
3329869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:
3330144b44b1SLen Brown 		return 30.0;
3331144b44b1SLen Brown 	default:
3332144b44b1SLen Brown 		return 135.0;
3333144b44b1SLen Brown 	}
3334144b44b1SLen Brown }
3335144b44b1SLen Brown 
333640ee8e3bSAndrey Semin /*
333740ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
333840ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
333940ee8e3bSAndrey Semin  */
334040ee8e3bSAndrey Semin static double
334140ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
334240ee8e3bSAndrey Semin {
334340ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
334440ee8e3bSAndrey Semin 
334540ee8e3bSAndrey Semin 	switch (model) {
3346869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3347869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3348869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3349869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3350005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
335140ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
335240ee8e3bSAndrey Semin 	default:
335340ee8e3bSAndrey Semin 		return (rapl_energy_units);
335440ee8e3bSAndrey Semin 	}
335540ee8e3bSAndrey Semin }
335640ee8e3bSAndrey Semin 
3357144b44b1SLen Brown 
3358889facbeSLen Brown /*
3359889facbeSLen Brown  * rapl_probe()
3360889facbeSLen Brown  *
3361144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3362889facbeSLen Brown  */
3363889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
3364889facbeSLen Brown {
3365889facbeSLen Brown 	unsigned long long msr;
3366144b44b1SLen Brown 	unsigned int time_unit;
3367889facbeSLen Brown 	double tdp;
3368889facbeSLen Brown 
3369889facbeSLen Brown 	if (!genuine_intel)
3370889facbeSLen Brown 		return;
3371889facbeSLen Brown 
3372889facbeSLen Brown 	if (family != 6)
3373889facbeSLen Brown 		return;
3374889facbeSLen Brown 
3375889facbeSLen Brown 	switch (model) {
3376869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3377869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3378869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3379869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3380869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3381869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3382869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3383144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3384812db3f7SLen Brown 		if (rapl_joules) {
3385812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3386812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3387812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3388812db3f7SLen Brown 		} else {
3389812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3390812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3391812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3392812db3f7SLen Brown 		}
3393889facbeSLen Brown 		break;
3394869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3395ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3396e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3397812db3f7SLen Brown 		if (rapl_joules)
3398812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3399812db3f7SLen Brown 		else
3400812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3401e4085d54SLen Brown 		break;
3402869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3403869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3404869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3405869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
340681824921SLen 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;
3407812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3408812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3409812db3f7SLen Brown 		if (rapl_joules) {
3410812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3411812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3412812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
341381824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3414812db3f7SLen Brown 		} else {
3415812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3416812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3417812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
341881824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3419812db3f7SLen Brown 		}
34200b2bb692SLen Brown 		break;
3421869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3422869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3423869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3424869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3425869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3426005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
34270b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3428812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3429812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3430812db3f7SLen Brown 		if (rapl_joules) {
3431812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3432812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3433812db3f7SLen Brown 		} else {
3434812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3435812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3436812db3f7SLen Brown 		}
3437e6f9bb3cSLen Brown 		break;
3438869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3439869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
34400b2bb692SLen 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;
3441812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3442812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3443812db3f7SLen Brown 		if (rapl_joules) {
3444812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3445812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3446812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3447812db3f7SLen Brown 		} else {
3448812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3449812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3450812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3451812db3f7SLen Brown 		}
3452144b44b1SLen Brown 		break;
3453869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3454869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3455144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3456812db3f7SLen Brown 		if (rapl_joules) {
3457812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3458812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3459812db3f7SLen Brown 		} else {
3460812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3461812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3462812db3f7SLen Brown 		}
3463889facbeSLen Brown 		break;
3464869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
34650f644909SJacob 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;
3466812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3467812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3468812db3f7SLen Brown 		if (rapl_joules) {
3469812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3470812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3471812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3472812db3f7SLen Brown 		} else {
3473812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3474812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3475812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3476812db3f7SLen Brown 		}
34770f644909SJacob Pan 		break;
3478889facbeSLen Brown 	default:
3479889facbeSLen Brown 		return;
3480889facbeSLen Brown 	}
3481889facbeSLen Brown 
3482889facbeSLen Brown 	/* units on package 0, verify later other packages match */
34837ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3484889facbeSLen Brown 		return;
3485889facbeSLen Brown 
3486889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3487869ce69eSLen Brown 	if (model == INTEL_FAM6_ATOM_SILVERMONT1)
3488144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3489144b44b1SLen Brown 	else
3490889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3491889facbeSLen Brown 
349240ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
349340ee8e3bSAndrey Semin 
3494144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3495144b44b1SLen Brown 	if (time_unit == 0)
3496144b44b1SLen Brown 		time_unit = 0xA;
3497889facbeSLen Brown 
3498144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3499144b44b1SLen Brown 
3500144b44b1SLen Brown 	tdp = get_tdp(model);
3501889facbeSLen Brown 
3502889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
350396e47158SLen Brown 	if (!quiet)
3504b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3505889facbeSLen Brown 
3506889facbeSLen Brown 	return;
3507889facbeSLen Brown }
3508889facbeSLen Brown 
35091b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
35103a9a941dSLen Brown {
35113a9a941dSLen Brown 	if (!genuine_intel)
35123a9a941dSLen Brown 		return;
35133a9a941dSLen Brown 
35143a9a941dSLen Brown 	if (family != 6)
35153a9a941dSLen Brown 		return;
35163a9a941dSLen Brown 
35173a9a941dSLen Brown 	switch (model) {
3518869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3519869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3520869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
35213a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
3522869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
35233a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
35243a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
35253a9a941dSLen Brown 	default:
35263a9a941dSLen Brown 		return;
35273a9a941dSLen Brown 	}
35283a9a941dSLen Brown }
35293a9a941dSLen Brown 
3530889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3531889facbeSLen Brown {
3532889facbeSLen Brown 	unsigned long long msr;
3533f4896fa5SLen Brown 	unsigned int dts, dts2;
3534889facbeSLen Brown 	int cpu;
3535889facbeSLen Brown 
3536889facbeSLen Brown 	if (!(do_dts || do_ptm))
3537889facbeSLen Brown 		return 0;
3538889facbeSLen Brown 
3539889facbeSLen Brown 	cpu = t->cpu_id;
3540889facbeSLen Brown 
3541889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
3542889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3543889facbeSLen Brown 		return 0;
3544889facbeSLen Brown 
3545889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3546b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3547889facbeSLen Brown 		return -1;
3548889facbeSLen Brown 	}
3549889facbeSLen Brown 
3550889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3551889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3552889facbeSLen Brown 			return 0;
3553889facbeSLen Brown 
3554889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3555b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3556889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
3557889facbeSLen Brown 
3558889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3559889facbeSLen Brown 			return 0;
3560889facbeSLen Brown 
3561889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3562889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3563b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3564889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3565889facbeSLen Brown 	}
3566889facbeSLen Brown 
3567889facbeSLen Brown 
3568f4896fa5SLen Brown 	if (do_dts && debug) {
3569889facbeSLen Brown 		unsigned int resolution;
3570889facbeSLen Brown 
3571889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3572889facbeSLen Brown 			return 0;
3573889facbeSLen Brown 
3574889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3575889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
3576b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3577889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
3578889facbeSLen Brown 
3579889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3580889facbeSLen Brown 			return 0;
3581889facbeSLen Brown 
3582889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3583889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3584b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3585889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3586889facbeSLen Brown 	}
3587889facbeSLen Brown 
3588889facbeSLen Brown 	return 0;
3589889facbeSLen Brown }
3590889facbeSLen Brown 
3591889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3592889facbeSLen Brown {
3593b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
3594889facbeSLen Brown 		cpu, label,
3595889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
3596889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
3597889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3598889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
3599889facbeSLen Brown 
3600889facbeSLen Brown 	return;
3601889facbeSLen Brown }
3602889facbeSLen Brown 
3603889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3604889facbeSLen Brown {
3605889facbeSLen Brown 	unsigned long long msr;
3606889facbeSLen Brown 	int cpu;
3607889facbeSLen Brown 
3608889facbeSLen Brown 	if (!do_rapl)
3609889facbeSLen Brown 		return 0;
3610889facbeSLen Brown 
3611889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
3612889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3613889facbeSLen Brown 		return 0;
3614889facbeSLen Brown 
3615889facbeSLen Brown 	cpu = t->cpu_id;
3616889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3617b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3618889facbeSLen Brown 		return -1;
3619889facbeSLen Brown 	}
3620889facbeSLen Brown 
3621889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3622889facbeSLen Brown 		return -1;
3623889facbeSLen Brown 
362496e47158SLen Brown 	fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3625144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
362696e47158SLen Brown 
3627144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
3628144b44b1SLen Brown 
3629889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3630889facbeSLen Brown                 	return -5;
3631889facbeSLen Brown 
3632889facbeSLen Brown 
3633b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3634889facbeSLen Brown 			cpu, msr,
3635889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3636889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3637889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3638889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3639889facbeSLen Brown 
3640144b44b1SLen Brown 	}
3641144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
3642144b44b1SLen Brown 
3643889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3644889facbeSLen Brown 			return -9;
3645889facbeSLen Brown 
3646b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
3647889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
3648889facbeSLen Brown 
3649889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
3650b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
3651889facbeSLen Brown 			cpu,
3652889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
3653889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
3654889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3655889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
3656889facbeSLen Brown 	}
3657889facbeSLen Brown 
36580b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
3659889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3660889facbeSLen Brown                 	return -6;
3661889facbeSLen Brown 
3662b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3663889facbeSLen Brown 			cpu, msr,
3664889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3665889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3666889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3667889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
36680b2bb692SLen Brown 	}
36690b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
3670889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3671889facbeSLen Brown 			return -9;
3672b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
3673889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3674889facbeSLen Brown 
3675889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
3676889facbeSLen Brown 	}
3677144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
3678889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3679889facbeSLen Brown 			return -7;
3680889facbeSLen Brown 
3681b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
3682144b44b1SLen Brown 	}
36839148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
3684889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3685889facbeSLen Brown 			return -9;
3686b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3687889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3688889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
3689889facbeSLen Brown 	}
3690889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
3691889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3692889facbeSLen Brown 			return -8;
3693889facbeSLen Brown 
3694b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
3695889facbeSLen Brown 
3696889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3697889facbeSLen Brown 			return -9;
3698b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3699889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3700889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
3701889facbeSLen Brown 	}
3702889facbeSLen Brown 	return 0;
3703889facbeSLen Brown }
3704889facbeSLen Brown 
3705d7899447SLen Brown /*
3706d7899447SLen Brown  * SNB adds support for additional MSRs:
3707d7899447SLen Brown  *
3708d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
3709d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
3710d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
3711d7899447SLen Brown  */
3712103a8feaSLen Brown 
3713d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
3714103a8feaSLen Brown {
3715103a8feaSLen Brown 	if (!genuine_intel)
3716103a8feaSLen Brown 		return 0;
3717103a8feaSLen Brown 
3718103a8feaSLen Brown 	switch (model) {
3719869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3720869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3721869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3722869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3723869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3724869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
3725869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3726869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3727869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3728869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3729869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3730869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3731869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3732869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3733869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3734869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3735869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3736869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3737ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
37385bbac26eSXiaolong Wang 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3739103a8feaSLen Brown 		return 1;
3740103a8feaSLen Brown 	}
3741103a8feaSLen Brown 	return 0;
3742103a8feaSLen Brown }
3743103a8feaSLen Brown 
3744d7899447SLen Brown /*
3745d7899447SLen Brown  * HSW adds support for additional MSRs:
3746d7899447SLen Brown  *
3747d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
3748d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
3749d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
37505a63426eSLen Brown  *
37515a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
37525a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
37535a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
37545a63426eSLen Brown  *
3755d7899447SLen Brown  */
3756d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
3757ca58710fSKristen Carlson Accardi {
3758ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
3759ca58710fSKristen Carlson Accardi 		return 0;
3760ca58710fSKristen Carlson Accardi 
3761ca58710fSKristen Carlson Accardi 	switch (model) {
3762869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3763869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3764869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3765869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3766869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3767869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3768869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3769ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3770ca58710fSKristen Carlson Accardi 		return 1;
3771ca58710fSKristen Carlson Accardi 	}
3772ca58710fSKristen Carlson Accardi 	return 0;
3773ca58710fSKristen Carlson Accardi }
3774ca58710fSKristen Carlson Accardi 
37750b2bb692SLen Brown /*
37760b2bb692SLen Brown  * SKL adds support for additional MSRS:
37770b2bb692SLen Brown  *
37780b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
37790b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
37800b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
37810b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
37820b2bb692SLen Brown  */
37830b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
37840b2bb692SLen Brown {
37850b2bb692SLen Brown 	if (!genuine_intel)
37860b2bb692SLen Brown 		return 0;
37870b2bb692SLen Brown 
37880b2bb692SLen Brown 	switch (model) {
3789869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3790869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3791869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3792869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
37930b2bb692SLen Brown 		return 1;
37940b2bb692SLen Brown 	}
37950b2bb692SLen Brown 	return 0;
37960b2bb692SLen Brown }
37970b2bb692SLen Brown 
3798144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
3799144b44b1SLen Brown {
3800144b44b1SLen Brown 	if (!genuine_intel)
3801144b44b1SLen Brown 		return 0;
3802144b44b1SLen Brown 	switch (model) {
3803869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3804869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3805144b44b1SLen Brown 		return 1;
3806144b44b1SLen Brown 	}
3807144b44b1SLen Brown 	return 0;
3808144b44b1SLen Brown }
3809144b44b1SLen Brown 
3810fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
3811fb5d4327SDasaratharaman Chandramouli {
3812fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3813fb5d4327SDasaratharaman Chandramouli 		return 0;
3814fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3815869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3816005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3817fb5d4327SDasaratharaman Chandramouli 		return 1;
3818fb5d4327SDasaratharaman Chandramouli 	}
3819fb5d4327SDasaratharaman Chandramouli 	return 0;
3820fb5d4327SDasaratharaman Chandramouli }
3821fb5d4327SDasaratharaman Chandramouli 
3822b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3823b2b34dfeSHubert Chrzaniuk {
3824b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
3825b2b34dfeSHubert Chrzaniuk 		return 1024;
3826b2b34dfeSHubert Chrzaniuk 	return 1;
3827b2b34dfeSHubert Chrzaniuk }
3828b2b34dfeSHubert Chrzaniuk 
3829144b44b1SLen Brown #define SLM_BCLK_FREQS 5
3830144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3831144b44b1SLen Brown 
3832144b44b1SLen Brown double slm_bclk(void)
3833144b44b1SLen Brown {
3834144b44b1SLen Brown 	unsigned long long msr = 3;
3835144b44b1SLen Brown 	unsigned int i;
3836144b44b1SLen Brown 	double freq;
3837144b44b1SLen Brown 
38387ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3839b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
3840144b44b1SLen Brown 
3841144b44b1SLen Brown 	i = msr & 0xf;
3842144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
3843b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
38440a91e551SColin Ian King 		i = 3;
3845144b44b1SLen Brown 	}
3846144b44b1SLen Brown 	freq = slm_freq_table[i];
3847144b44b1SLen Brown 
384896e47158SLen Brown 	if (!quiet)
3849b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3850144b44b1SLen Brown 
3851144b44b1SLen Brown 	return freq;
3852144b44b1SLen Brown }
3853144b44b1SLen Brown 
3854103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
3855103a8feaSLen Brown {
3856121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
3857103a8feaSLen Brown 		return 100.00;
3858144b44b1SLen Brown 	else if (is_slm(family, model))
3859144b44b1SLen Brown 		return slm_bclk();
3860103a8feaSLen Brown 	else
3861103a8feaSLen Brown 		return 133.33;
3862103a8feaSLen Brown }
3863103a8feaSLen Brown 
3864889facbeSLen Brown /*
3865889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3866889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
3867889facbeSLen Brown  * This is usually equal to tjMax.
3868889facbeSLen Brown  *
3869889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
3870889facbeSLen Brown  * but also allow cmdline over-ride with -T.
3871889facbeSLen Brown  *
3872889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
3873889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
3874889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3875889facbeSLen Brown  */
3876889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3877889facbeSLen Brown {
3878889facbeSLen Brown 	unsigned long long msr;
3879889facbeSLen Brown 	unsigned int target_c_local;
3880889facbeSLen Brown 	int cpu;
3881889facbeSLen Brown 
3882889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
3883889facbeSLen Brown 	if (!(do_dts || do_ptm))
3884889facbeSLen Brown 		return 0;
3885889facbeSLen Brown 
3886889facbeSLen Brown 	/* this is a per-package concept */
3887889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3888889facbeSLen Brown 		return 0;
3889889facbeSLen Brown 
3890889facbeSLen Brown 	cpu = t->cpu_id;
3891889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3892b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3893889facbeSLen Brown 		return -1;
3894889facbeSLen Brown 	}
3895889facbeSLen Brown 
3896889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
3897889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
3898b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
3899889facbeSLen Brown 			cpu, tcc_activation_temp);
3900889facbeSLen Brown 		return 0;
3901889facbeSLen Brown 	}
3902889facbeSLen Brown 
3903889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
3904d7899447SLen Brown 	if (!do_nhm_platform_info)
3905889facbeSLen Brown 		goto guess;
3906889facbeSLen Brown 
39077ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
3908889facbeSLen Brown 		goto guess;
3909889facbeSLen Brown 
39103482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
3911889facbeSLen Brown 
391296e47158SLen Brown 	if (!quiet)
3913b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3914889facbeSLen Brown 			cpu, msr, target_c_local);
3915889facbeSLen Brown 
39163482124aSJean Delvare 	if (!target_c_local)
3917889facbeSLen Brown 		goto guess;
3918889facbeSLen Brown 
3919889facbeSLen Brown 	tcc_activation_temp = target_c_local;
3920889facbeSLen Brown 
3921889facbeSLen Brown 	return 0;
3922889facbeSLen Brown 
3923889facbeSLen Brown guess:
3924889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
3925b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3926889facbeSLen Brown 		cpu, tcc_activation_temp);
3927889facbeSLen Brown 
3928889facbeSLen Brown 	return 0;
3929889facbeSLen Brown }
393069807a63SLen Brown 
3931aa8d8cc7SLen Brown void decode_feature_control_msr(void)
3932aa8d8cc7SLen Brown {
3933aa8d8cc7SLen Brown 	unsigned long long msr;
3934aa8d8cc7SLen Brown 
3935aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3936aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3937aa8d8cc7SLen Brown 			base_cpu, msr,
3938aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3939aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
3940aa8d8cc7SLen Brown }
3941aa8d8cc7SLen Brown 
394269807a63SLen Brown void decode_misc_enable_msr(void)
394369807a63SLen Brown {
394469807a63SLen Brown 	unsigned long long msr;
394569807a63SLen Brown 
3946*f26b1519SLen Brown 	if (!genuine_intel)
3947*f26b1519SLen Brown 		return;
3948*f26b1519SLen Brown 
394969807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
3950e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
395169807a63SLen Brown 			base_cpu, msr,
3952e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3953e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3954e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3955e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3956e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
395769807a63SLen Brown }
395869807a63SLen Brown 
395933148d67SLen Brown void decode_misc_feature_control(void)
396033148d67SLen Brown {
396133148d67SLen Brown 	unsigned long long msr;
396233148d67SLen Brown 
396333148d67SLen Brown 	if (!has_misc_feature_control)
396433148d67SLen Brown 		return;
396533148d67SLen Brown 
396633148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
396733148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
396833148d67SLen Brown 			base_cpu, msr,
396933148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
397033148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
397133148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
397233148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
397333148d67SLen Brown }
3974f0057310SLen Brown /*
3975f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
3976f0057310SLen Brown  *
3977f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
3978f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
3979f0057310SLen Brown  * bit[1] less so...
3980f0057310SLen Brown  */
3981f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
3982f0057310SLen Brown {
3983f0057310SLen Brown 	unsigned long long msr;
3984f0057310SLen Brown 
3985f0057310SLen Brown 	if (!do_nhm_platform_info)
3986f0057310SLen Brown 		return;
3987f0057310SLen Brown 
3988cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
3989cf4cbe53SLen Brown 		return;
3990cf4cbe53SLen Brown 
3991f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
3992ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
3993f0057310SLen Brown 			base_cpu, msr,
3994f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
3995ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
3996ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
3997f0057310SLen Brown }
399871616c8eSLen Brown /*
399971616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
400071616c8eSLen Brown  *
400171616c8eSLen Brown  * This MSRs are present on Silvermont processors,
400271616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
400371616c8eSLen Brown  */
400471616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
400571616c8eSLen Brown {
400671616c8eSLen Brown 	unsigned long long msr;
400771616c8eSLen Brown 
400871616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
400971616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
401071616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
401171616c8eSLen Brown 
401271616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
401371616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
401471616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
401571616c8eSLen Brown }
40167f5c258eSLen Brown 
4017fcd17211SLen Brown void process_cpuid()
4018103a8feaSLen Brown {
401961a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
4020103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
4021b3a34e93SLen Brown 	unsigned int has_turbo;
4022103a8feaSLen Brown 
4023103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4024103a8feaSLen Brown 
40255aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4026103a8feaSLen Brown 
4027103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4028103a8feaSLen Brown 		genuine_intel = 1;
4029103a8feaSLen Brown 
403096e47158SLen Brown 	if (!quiet)
4031b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4032103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4033103a8feaSLen Brown 
40345aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4035103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4036103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4037103a8feaSLen Brown 	stepping = fms & 0xf;
4038103a8feaSLen Brown 	if (family == 6 || family == 0xf)
4039103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
4040103a8feaSLen Brown 
404196e47158SLen Brown 	if (!quiet) {
4042b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
4043103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
4044aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
404569807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
404669807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
4047aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
404869807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
404969807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
405069807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
405169807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
405269807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
405369807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
405469807a63SLen Brown 	}
4055103a8feaSLen Brown 
4056b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
4057b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
4058103a8feaSLen Brown 
4059103a8feaSLen Brown 	/*
4060103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4061103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4062103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4063103a8feaSLen Brown 	 */
4064103a8feaSLen Brown 	ebx = ecx = edx = 0;
40655aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4066103a8feaSLen Brown 
406761a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4068103a8feaSLen Brown 
4069103a8feaSLen Brown 		/*
4070103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4071103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4072103a8feaSLen Brown 		 */
40735aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
40748209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4075d7899447SLen Brown 	}
4076103a8feaSLen Brown 
4077103a8feaSLen Brown 	/*
4078103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4079103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4080103a8feaSLen Brown 	 */
4081103a8feaSLen Brown 
40825aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
40838209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4084812db3f7SLen Brown 	if (has_aperf) {
4085812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4086812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4087812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4088812db3f7SLen Brown 	}
4089889facbeSLen Brown 	do_dts = eax & (1 << 0);
4090812db3f7SLen Brown 	if (do_dts)
4091812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4092b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4093889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4094812db3f7SLen Brown 	if (do_ptm)
4095812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
40967f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
40977f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
40987f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
40997f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
41007f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4101889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4102889facbeSLen Brown 
410396e47158SLen Brown 	if (!quiet)
4104b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
41057f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
41067f5c258eSLen Brown 			has_aperf ? "" : "No-",
4107b3a34e93SLen Brown 			has_turbo ? "" : "No-",
41087f5c258eSLen Brown 			do_dts ? "" : "No-",
41097f5c258eSLen Brown 			do_ptm ? "" : "No-",
41107f5c258eSLen Brown 			has_hwp ? "" : "No-",
41117f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
41127f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
41137f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
41147f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
41157f5c258eSLen Brown 			has_epb ? "" : "No-");
4116103a8feaSLen Brown 
411796e47158SLen Brown 	if (!quiet)
411869807a63SLen Brown 		decode_misc_enable_msr();
411969807a63SLen Brown 
412033148d67SLen Brown 
412196e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4122aa8d8cc7SLen Brown 		int has_sgx;
4123aa8d8cc7SLen Brown 
4124aa8d8cc7SLen Brown 		ecx = 0;
4125aa8d8cc7SLen Brown 
4126aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4127aa8d8cc7SLen Brown 
4128aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4129aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4130aa8d8cc7SLen Brown 
4131aa8d8cc7SLen Brown 		if (has_sgx)
4132aa8d8cc7SLen Brown 			decode_feature_control_msr();
4133aa8d8cc7SLen Brown 	}
4134aa8d8cc7SLen Brown 
413561a87ba7SLen Brown 	if (max_level >= 0x15) {
41368a5bdf41SLen Brown 		unsigned int eax_crystal;
41378a5bdf41SLen Brown 		unsigned int ebx_tsc;
41388a5bdf41SLen Brown 
41398a5bdf41SLen Brown 		/*
41408a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
41418a5bdf41SLen Brown 		 */
41428a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
41435aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
41448a5bdf41SLen Brown 
41458a5bdf41SLen Brown 		if (ebx_tsc != 0) {
41468a5bdf41SLen Brown 
414796e47158SLen Brown 			if (!quiet && (ebx != 0))
4148b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
41498a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
41508a5bdf41SLen Brown 
41518a5bdf41SLen Brown 			if (crystal_hz == 0)
41528a5bdf41SLen Brown 				switch(model) {
4153869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4154869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4155869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4156869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4157e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4158e8efbc80SLen Brown 					break;
4159869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_X:	/* SKX */
41607268d407SLen Brown 				case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
4161ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4162ec53e594SLen Brown 					break;
4163869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4164ac01ac13SLen Brown 				case INTEL_FAM6_ATOM_GEMINI_LAKE:
4165e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
41668a5bdf41SLen Brown 					break;
41678a5bdf41SLen Brown 				default:
41688a5bdf41SLen Brown 					crystal_hz = 0;
41698a5bdf41SLen Brown 			}
41708a5bdf41SLen Brown 
41718a5bdf41SLen Brown 			if (crystal_hz) {
41728a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
417396e47158SLen Brown 				if (!quiet)
4174b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
41758a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
41768a5bdf41SLen Brown 			}
41778a5bdf41SLen Brown 		}
41788a5bdf41SLen Brown 	}
417961a87ba7SLen Brown 	if (max_level >= 0x16) {
418061a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
418161a87ba7SLen Brown 
418261a87ba7SLen Brown 		/*
418361a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
418461a87ba7SLen Brown 		 */
418561a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
418661a87ba7SLen Brown 
41875aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
418896e47158SLen Brown 		if (!quiet)
4189b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
419061a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
419161a87ba7SLen Brown 	}
41928a5bdf41SLen Brown 
4193b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4194b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4195b2b34dfeSHubert Chrzaniuk 
4196812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4197812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4198812db3f7SLen Brown 
4199812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4200812db3f7SLen Brown 		do_nhm_platform_info = 1;
4201812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4202812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4203812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4204812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4205812db3f7SLen Brown 	}
4206d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4207812db3f7SLen Brown 
4208812db3f7SLen Brown 	if (do_snb_cstates)
4209812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4210812db3f7SLen Brown 
42115a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
42120f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
42130f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
42140f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
42150f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
42160f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
42170f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
42180f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
42190f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
42200539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
42210f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
42220f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
42230f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
42240f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
42250539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
42260539ba11SLen Brown 		use_c1_residency_msr = 1;
42270539ba11SLen Brown 	}
42287170a374SLen Brown 	if (is_dnv(family, model)) {
42297170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
42307170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
42317170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
42327170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
42337170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
42347170a374SLen Brown 		use_c1_residency_msr = 1;
42357170a374SLen Brown 	}
423634c76197SLen Brown 	if (is_skx(family, model)) {
423734c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
423834c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
423934c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
424034c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
424134c76197SLen Brown 	}
4242ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4243ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4244ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4245ade0ebacSLen Brown 	}
42460f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
42470f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
42480f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
42490f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
42500f47c08dSLen Brown 	}
42515a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
4252a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4253a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4254a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4255a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4256a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4257a99d8730SLen Brown 	}
4258144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4259fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4260103a8feaSLen Brown 
426196e47158SLen Brown 	if (!quiet)
4262f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4263f0057310SLen Brown 
426496e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
426571616c8eSLen Brown 		decode_c6_demotion_policy_msr();
426671616c8eSLen Brown 
4267889facbeSLen Brown 	rapl_probe(family, model);
42683a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4269889facbeSLen Brown 
427096e47158SLen Brown 	if (!quiet)
42711b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4272fcd17211SLen Brown 
427341618e63SLen Brown 	if (!quiet)
427441618e63SLen Brown 		dump_sysfs_cstate_config();
42757293fccdSLen Brown 	if (!quiet)
42767293fccdSLen Brown 		dump_sysfs_pstate_config();
427741618e63SLen Brown 
4278a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4279a2b7b749SLen Brown 		calculate_tsc_tweak();
4280a2b7b749SLen Brown 
4281812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4282812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4283fdf676e5SLen Brown 
4284812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4285812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
428627d47356SLen Brown 
428796e47158SLen Brown 	if (!quiet)
428833148d67SLen Brown 		decode_misc_feature_control();
428933148d67SLen Brown 
4290889facbeSLen Brown 	return;
4291103a8feaSLen Brown }
4292103a8feaSLen Brown 
4293103a8feaSLen Brown 
4294103a8feaSLen Brown /*
4295103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4296103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4297103a8feaSLen Brown  */
4298103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4299103a8feaSLen Brown {
4300103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4301103a8feaSLen Brown 		return 1;
4302103a8feaSLen Brown 	else
4303103a8feaSLen Brown 		return 0;
4304103a8feaSLen Brown }
4305103a8feaSLen Brown 
4306103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4307103a8feaSLen Brown {
4308103a8feaSLen Brown 	return 0;
4309103a8feaSLen Brown }
4310103a8feaSLen Brown 
4311c98d5d94SLen Brown void topology_probe()
4312c98d5d94SLen Brown {
4313c98d5d94SLen Brown 	int i;
4314c98d5d94SLen Brown 	int max_core_id = 0;
4315c98d5d94SLen Brown 	int max_package_id = 0;
4316c98d5d94SLen Brown 	int max_siblings = 0;
4317c98d5d94SLen Brown 	struct cpu_topology {
4318c98d5d94SLen Brown 		int core_id;
4319c98d5d94SLen Brown 		int physical_package_id;
4320c98d5d94SLen Brown 	} *cpus;
4321c98d5d94SLen Brown 
4322c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4323c98d5d94SLen Brown 	topo.num_cpus = 0;
4324c98d5d94SLen Brown 	topo.max_cpu_num = 0;
4325c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4326c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4327812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4328c98d5d94SLen Brown 
4329d8af6f5fSLen Brown 	if (debug > 1)
4330b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4331c98d5d94SLen Brown 
4332c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4333b2c95d90SJosh Triplett 	if (cpus == NULL)
4334b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4335c98d5d94SLen Brown 
4336c98d5d94SLen Brown 	/*
4337c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4338c98d5d94SLen Brown 	 */
4339c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4340b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4341b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4342c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4343c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4344c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4345c98d5d94SLen Brown 
4346c98d5d94SLen Brown 	/*
43471ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
43481ef7d21aSLen Brown 	 */
43491ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
43501ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
43511ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
43521ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
43531ef7d21aSLen Brown 	}
43541ef7d21aSLen Brown 
43551ef7d21aSLen Brown 	/*
4356c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4357c98d5d94SLen Brown 	 */
4358c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4359b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4360b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4361c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4362c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4363c98d5d94SLen Brown 
4364c98d5d94SLen Brown 
4365c98d5d94SLen Brown 	/*
4366c98d5d94SLen Brown 	 * For online cpus
4367c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4368c98d5d94SLen Brown 	 */
4369c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4370c98d5d94SLen Brown 		int siblings;
4371c98d5d94SLen Brown 
4372c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4373d8af6f5fSLen Brown 			if (debug > 1)
4374b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4375c98d5d94SLen Brown 			continue;
4376c98d5d94SLen Brown 		}
4377c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
4378c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
4379c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
4380c98d5d94SLen Brown 
4381c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4382c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4383c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4384c98d5d94SLen Brown 
4385c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
4386c98d5d94SLen Brown 		if (siblings > max_siblings)
4387c98d5d94SLen Brown 			max_siblings = siblings;
4388d8af6f5fSLen Brown 		if (debug > 1)
4389b7d8c148SLen Brown 			fprintf(outf, "cpu %d pkg %d core %d\n",
4390c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
4391c98d5d94SLen Brown 	}
4392c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
4393d8af6f5fSLen Brown 	if (debug > 1)
4394b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
4395c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
43960f47c08dSLen Brown 	if (!summary_only && topo.num_cores_per_pkg > 1)
4397812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
4398c98d5d94SLen Brown 
4399c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
4400d8af6f5fSLen Brown 	if (debug > 1)
4401b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4402c98d5d94SLen Brown 			max_package_id, topo.num_packages);
44037da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
4404812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
4405c98d5d94SLen Brown 
4406c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
4407d8af6f5fSLen Brown 	if (debug > 1)
4408b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
4409c98d5d94SLen Brown 
4410c98d5d94SLen Brown 	free(cpus);
4411c98d5d94SLen Brown }
4412c98d5d94SLen Brown 
4413c98d5d94SLen Brown void
4414c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4415c98d5d94SLen Brown {
4416c98d5d94SLen Brown 	int i;
4417c98d5d94SLen Brown 
4418c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
4419678a3bd1SLen Brown 		topo.num_packages, sizeof(struct thread_data));
4420c98d5d94SLen Brown 	if (*t == NULL)
4421c98d5d94SLen Brown 		goto error;
4422c98d5d94SLen Brown 
4423c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
4424c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
4425c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
4426c98d5d94SLen Brown 
4427c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
4428678a3bd1SLen Brown 		sizeof(struct core_data));
4429c98d5d94SLen Brown 	if (*c == NULL)
4430c98d5d94SLen Brown 		goto error;
4431c98d5d94SLen Brown 
4432c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4433c98d5d94SLen Brown 		(*c)[i].core_id = -1;
4434c98d5d94SLen Brown 
4435678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
4436c98d5d94SLen Brown 	if (*p == NULL)
4437c98d5d94SLen Brown 		goto error;
4438c98d5d94SLen Brown 
4439c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
4440c98d5d94SLen Brown 		(*p)[i].package_id = i;
4441c98d5d94SLen Brown 
4442c98d5d94SLen Brown 	return;
4443c98d5d94SLen Brown error:
4444b2c95d90SJosh Triplett 	err(1, "calloc counters");
4445c98d5d94SLen Brown }
4446c98d5d94SLen Brown /*
4447c98d5d94SLen Brown  * init_counter()
4448c98d5d94SLen Brown  *
4449c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
4450c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4451c98d5d94SLen Brown  *
4452c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
4453c98d5d94SLen Brown  */
4454c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4455c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
4456c98d5d94SLen Brown 	int pkg_num, int cpu_id)
4457c98d5d94SLen Brown {
4458c98d5d94SLen Brown 	struct thread_data *t;
4459c98d5d94SLen Brown 	struct core_data *c;
4460c98d5d94SLen Brown 	struct pkg_data *p;
4461c98d5d94SLen Brown 
4462c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4463c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
4464c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
4465c98d5d94SLen Brown 
4466c98d5d94SLen Brown 	t->cpu_id = cpu_id;
4467c98d5d94SLen Brown 	if (thread_num == 0) {
4468c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4469c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
4470c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4471c98d5d94SLen Brown 	}
4472c98d5d94SLen Brown 
4473c98d5d94SLen Brown 	c->core_id = core_num;
4474c98d5d94SLen Brown 	p->package_id = pkg_num;
4475c98d5d94SLen Brown }
4476c98d5d94SLen Brown 
4477c98d5d94SLen Brown 
4478c98d5d94SLen Brown int initialize_counters(int cpu_id)
4479c98d5d94SLen Brown {
4480c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
4481c98d5d94SLen Brown 
4482c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
4483c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
4484e275b388SDasaratharaman Chandramouli 	my_thread_id = get_cpu_position_in_core(cpu_id);
4485e275b388SDasaratharaman Chandramouli 	if (!my_thread_id)
4486c98d5d94SLen Brown 		topo.num_cores++;
4487c98d5d94SLen Brown 
4488c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4489c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4490c98d5d94SLen Brown 	return 0;
4491c98d5d94SLen Brown }
4492c98d5d94SLen Brown 
4493c98d5d94SLen Brown void allocate_output_buffer()
4494c98d5d94SLen Brown {
44953b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4496c98d5d94SLen Brown 	outp = output_buffer;
4497b2c95d90SJosh Triplett 	if (outp == NULL)
4498b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
4499c98d5d94SLen Brown }
450036229897SLen Brown void allocate_fd_percpu(void)
450136229897SLen Brown {
450201a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
450336229897SLen Brown 	if (fd_percpu == NULL)
450436229897SLen Brown 		err(-1, "calloc fd_percpu");
450536229897SLen Brown }
4506562a2d37SLen Brown void allocate_irq_buffers(void)
4507562a2d37SLen Brown {
4508562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4509562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
4510562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
4511c98d5d94SLen Brown 
451201a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4513562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
451401a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
4515562a2d37SLen Brown }
4516c98d5d94SLen Brown void setup_all_buffers(void)
4517c98d5d94SLen Brown {
4518c98d5d94SLen Brown 	topology_probe();
4519562a2d37SLen Brown 	allocate_irq_buffers();
452036229897SLen Brown 	allocate_fd_percpu();
4521c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
4522c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
4523c98d5d94SLen Brown 	allocate_output_buffer();
4524c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
4525c98d5d94SLen Brown }
45263b4d5c7fSAndy Shevchenko 
45277ce7d5deSPrarit Bhargava void set_base_cpu(void)
45287ce7d5deSPrarit Bhargava {
45297ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
45307ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
45317ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
45327ce7d5deSPrarit Bhargava 
45337ce7d5deSPrarit Bhargava 	if (debug > 1)
4534b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
45357ce7d5deSPrarit Bhargava }
45367ce7d5deSPrarit Bhargava 
4537103a8feaSLen Brown void turbostat_init()
4538103a8feaSLen Brown {
45397ce7d5deSPrarit Bhargava 	setup_all_buffers();
45407ce7d5deSPrarit Bhargava 	set_base_cpu();
4541103a8feaSLen Brown 	check_dev_msr();
454298481e79SLen Brown 	check_permissions();
4543fcd17211SLen Brown 	process_cpuid();
4544103a8feaSLen Brown 
4545103a8feaSLen Brown 
454696e47158SLen Brown 	if (!quiet)
45477f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
45487f5c258eSLen Brown 
454996e47158SLen Brown 	if (!quiet)
4550889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
4551889facbeSLen Brown 
455296e47158SLen Brown 	if (!quiet)
45533a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
45543a9a941dSLen Brown 
455596e47158SLen Brown 	if (!quiet)
4556889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
4557889facbeSLen Brown 
4558889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
4559889facbeSLen Brown 
456096e47158SLen Brown 	if (!quiet)
4561889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
45625a63426eSLen Brown 
456396e47158SLen Brown 	if (!quiet && do_irtl_snb)
45645a63426eSLen Brown 		print_irtl();
4565103a8feaSLen Brown }
4566103a8feaSLen Brown 
4567103a8feaSLen Brown int fork_it(char **argv)
4568103a8feaSLen Brown {
4569103a8feaSLen Brown 	pid_t child_pid;
4570d91bb17cSLen Brown 	int status;
4571d15cf7c1SLen Brown 
4572218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
4573d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
4574d91bb17cSLen Brown 	if (status)
4575d91bb17cSLen Brown 		exit(status);
4576d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
4577d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
4578103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
4579103a8feaSLen Brown 
4580103a8feaSLen Brown 	child_pid = fork();
4581103a8feaSLen Brown 	if (!child_pid) {
4582103a8feaSLen Brown 		/* child */
4583103a8feaSLen Brown 		execvp(argv[0], argv);
45840815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
4585103a8feaSLen Brown 	} else {
4586103a8feaSLen Brown 
4587103a8feaSLen Brown 		/* parent */
4588b2c95d90SJosh Triplett 		if (child_pid == -1)
4589b2c95d90SJosh Triplett 			err(1, "fork");
4590103a8feaSLen Brown 
4591103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
4592103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
4593b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
4594b2c95d90SJosh Triplett 			err(status, "waitpid");
4595103a8feaSLen Brown 	}
4596c98d5d94SLen Brown 	/*
4597c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
4598c98d5d94SLen Brown 	 * because re-starting is problematic when forking
4599c98d5d94SLen Brown 	 */
4600218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
4601c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
4602103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
4603103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
4604ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4605ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
4606ba3dec99SLen Brown 	else {
4607c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
4608c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
4609ba3dec99SLen Brown 	}
4610103a8feaSLen Brown 
4611b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4612b7d8c148SLen Brown 
4613b7d8c148SLen Brown 	flush_output_stderr();
4614103a8feaSLen Brown 
4615d91bb17cSLen Brown 	return status;
4616103a8feaSLen Brown }
4617103a8feaSLen Brown 
46183b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
46193b4d5c7fSAndy Shevchenko {
46203b4d5c7fSAndy Shevchenko 	int status;
46213b4d5c7fSAndy Shevchenko 
4622218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
46233b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
46243b4d5c7fSAndy Shevchenko 	if (status)
46253b4d5c7fSAndy Shevchenko 		return status;
46263b4d5c7fSAndy Shevchenko 
46273b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
46283b4d5c7fSAndy Shevchenko 	if (status)
46293b4d5c7fSAndy Shevchenko 		return status;
46303b4d5c7fSAndy Shevchenko 
4631b7d8c148SLen Brown 	flush_output_stdout();
46323b4d5c7fSAndy Shevchenko 
46333b4d5c7fSAndy Shevchenko 	return status;
46343b4d5c7fSAndy Shevchenko }
46353b4d5c7fSAndy Shevchenko 
4636d8af6f5fSLen Brown void print_version() {
46375f9bf02aSLen Brown 	fprintf(outf, "turbostat version 17.04.12"
4638d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
4639d8af6f5fSLen Brown }
4640d8af6f5fSLen Brown 
4641495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
4642495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
464341618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
4644388e9c81SLen Brown {
4645388e9c81SLen Brown 	struct msr_counter *msrp;
4646388e9c81SLen Brown 
4647388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
4648388e9c81SLen Brown 	if (msrp == NULL) {
4649388e9c81SLen Brown 		perror("calloc");
4650388e9c81SLen Brown 		exit(1);
4651388e9c81SLen Brown 	}
4652388e9c81SLen Brown 
4653388e9c81SLen Brown 	msrp->msr_num = msr_num;
4654388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
4655495c7654SLen Brown 	if (path)
4656495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
4657388e9c81SLen Brown 	msrp->width = width;
4658388e9c81SLen Brown 	msrp->type = type;
4659388e9c81SLen Brown 	msrp->format = format;
466041618e63SLen Brown 	msrp->flags = flags;
4661388e9c81SLen Brown 
4662388e9c81SLen Brown 	switch (scope) {
4663388e9c81SLen Brown 
4664388e9c81SLen Brown 	case SCOPE_CPU:
4665388e9c81SLen Brown 		msrp->next = sys.tp;
4666388e9c81SLen Brown 		sys.tp = msrp;
4667678a3bd1SLen Brown 		sys.added_thread_counters++;
4668678a3bd1SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4669678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
4670678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4671678a3bd1SLen Brown 			exit(-1);
4672678a3bd1SLen Brown 		}
4673388e9c81SLen Brown 		break;
4674388e9c81SLen Brown 
4675388e9c81SLen Brown 	case SCOPE_CORE:
4676388e9c81SLen Brown 		msrp->next = sys.cp;
4677388e9c81SLen Brown 		sys.cp = msrp;
4678678a3bd1SLen Brown 		sys.added_core_counters++;
4679678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4680678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
4681678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4682678a3bd1SLen Brown 			exit(-1);
4683678a3bd1SLen Brown 		}
4684388e9c81SLen Brown 		break;
4685388e9c81SLen Brown 
4686388e9c81SLen Brown 	case SCOPE_PACKAGE:
4687388e9c81SLen Brown 		msrp->next = sys.pp;
4688388e9c81SLen Brown 		sys.pp = msrp;
4689678a3bd1SLen Brown 		sys.added_package_counters++;
4690678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4691678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
4692678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4693678a3bd1SLen Brown 			exit(-1);
4694678a3bd1SLen Brown 		}
4695388e9c81SLen Brown 		break;
4696388e9c81SLen Brown 	}
4697388e9c81SLen Brown 
4698388e9c81SLen Brown 	return 0;
4699388e9c81SLen Brown }
4700388e9c81SLen Brown 
4701388e9c81SLen Brown void parse_add_command(char *add_command)
4702388e9c81SLen Brown {
4703388e9c81SLen Brown 	int msr_num = 0;
4704495c7654SLen Brown 	char *path = NULL;
47050f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
4706388e9c81SLen Brown 	int width = 64;
4707388e9c81SLen Brown 	int fail = 0;
4708388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
4709388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
4710388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
4711388e9c81SLen Brown 
4712388e9c81SLen Brown 	while (add_command) {
4713388e9c81SLen Brown 
4714388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4715388e9c81SLen Brown 			goto next;
4716388e9c81SLen Brown 
4717388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
4718388e9c81SLen Brown 			goto next;
4719388e9c81SLen Brown 
4720495c7654SLen Brown 		if (*add_command == '/') {
4721495c7654SLen Brown 			path = add_command;
4722495c7654SLen Brown 			goto next;
4723495c7654SLen Brown 		}
4724495c7654SLen Brown 
4725388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
4726388e9c81SLen Brown 			if ((width == 32) || (width == 64))
4727388e9c81SLen Brown 				goto next;
4728388e9c81SLen Brown 			width = 64;
4729388e9c81SLen Brown 		}
4730388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4731388e9c81SLen Brown 			scope = SCOPE_CPU;
4732388e9c81SLen Brown 			goto next;
4733388e9c81SLen Brown 		}
4734388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
4735388e9c81SLen Brown 			scope = SCOPE_CORE;
4736388e9c81SLen Brown 			goto next;
4737388e9c81SLen Brown 		}
4738388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
4739388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
4740388e9c81SLen Brown 			goto next;
4741388e9c81SLen Brown 		}
4742388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4743388e9c81SLen Brown 			type = COUNTER_CYCLES;
4744388e9c81SLen Brown 			goto next;
4745388e9c81SLen Brown 		}
4746388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4747388e9c81SLen Brown 			type = COUNTER_SECONDS;
4748388e9c81SLen Brown 			goto next;
4749388e9c81SLen Brown 		}
475041618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
475141618e63SLen Brown 			type = COUNTER_USEC;
475241618e63SLen Brown 			goto next;
475341618e63SLen Brown 		}
4754388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
4755388e9c81SLen Brown 			format = FORMAT_RAW;
4756388e9c81SLen Brown 			goto next;
4757388e9c81SLen Brown 		}
4758388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
4759388e9c81SLen Brown 			format = FORMAT_DELTA;
4760388e9c81SLen Brown 			goto next;
4761388e9c81SLen Brown 		}
4762388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
4763388e9c81SLen Brown 			format = FORMAT_PERCENT;
4764388e9c81SLen Brown 			goto next;
4765388e9c81SLen Brown 		}
4766388e9c81SLen Brown 
4767388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
4768388e9c81SLen Brown 			char *eos;
4769388e9c81SLen Brown 
4770388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
4771388e9c81SLen Brown 			if (eos)
4772388e9c81SLen Brown 				*eos = '\0';
4773388e9c81SLen Brown 			goto next;
4774388e9c81SLen Brown 		}
4775388e9c81SLen Brown 
4776388e9c81SLen Brown next:
4777388e9c81SLen Brown 		add_command = strchr(add_command, ',');
4778495c7654SLen Brown 		if (add_command) {
4779495c7654SLen Brown 			*add_command = '\0';
4780388e9c81SLen Brown 			add_command++;
4781495c7654SLen Brown 		}
4782388e9c81SLen Brown 
4783388e9c81SLen Brown 	}
4784495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
4785495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
4786388e9c81SLen Brown 		fail++;
4787388e9c81SLen Brown 	}
4788388e9c81SLen Brown 
4789388e9c81SLen Brown 	/* generate default column header */
4790388e9c81SLen Brown 	if (*name_buffer == '\0') {
4791388e9c81SLen Brown 		if (width == 32)
47925f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4793388e9c81SLen Brown 		else
47945f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4795388e9c81SLen Brown 	}
4796388e9c81SLen Brown 
479741618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
4798388e9c81SLen Brown 		fail++;
4799388e9c81SLen Brown 
4800388e9c81SLen Brown 	if (fail) {
4801388e9c81SLen Brown 		help();
4802388e9c81SLen Brown 		exit(1);
4803388e9c81SLen Brown 	}
4804388e9c81SLen Brown }
480541618e63SLen Brown 
4806dd778a5eSLen Brown int is_deferred_skip(char *name)
4807dd778a5eSLen Brown {
4808dd778a5eSLen Brown 	int i;
4809dd778a5eSLen Brown 
4810dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
4811dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
4812dd778a5eSLen Brown 			return 1;
4813dd778a5eSLen Brown 	return 0;
4814dd778a5eSLen Brown }
4815dd778a5eSLen Brown 
481641618e63SLen Brown void probe_sysfs(void)
481741618e63SLen Brown {
481841618e63SLen Brown 	char path[64];
481941618e63SLen Brown 	char name_buf[16];
482041618e63SLen Brown 	FILE *input;
482141618e63SLen Brown 	int state;
482241618e63SLen Brown 	char *sp;
482341618e63SLen Brown 
482441618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
482541618e63SLen Brown 		return;
482641618e63SLen Brown 
482741618e63SLen Brown 	for (state = 10; state > 0; --state) {
482841618e63SLen Brown 
482941618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
483041618e63SLen Brown 			base_cpu, state);
483141618e63SLen Brown 		input = fopen(path, "r");
483241618e63SLen Brown 		if (input == NULL)
483341618e63SLen Brown 			continue;
483441618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
483541618e63SLen Brown 
483641618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
483741618e63SLen Brown 		sp = strchr(name_buf, '-');
483841618e63SLen Brown 		if (!sp)
483941618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
484041618e63SLen Brown 		*sp = '%';
484141618e63SLen Brown 		*(sp + 1) = '\0';
484241618e63SLen Brown 
484341618e63SLen Brown 		fclose(input);
484441618e63SLen Brown 
484541618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
484641618e63SLen Brown 
4847dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
4848dd778a5eSLen Brown 			continue;
4849dd778a5eSLen Brown 
485041618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
485141618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
485241618e63SLen Brown 	}
485341618e63SLen Brown 
485441618e63SLen Brown 	for (state = 10; state > 0; --state) {
485541618e63SLen Brown 
485641618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
485741618e63SLen Brown 			base_cpu, state);
485841618e63SLen Brown 		input = fopen(path, "r");
485941618e63SLen Brown 		if (input == NULL)
486041618e63SLen Brown 			continue;
486141618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
486241618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
486341618e63SLen Brown 		sp = strchr(name_buf, '-');
486441618e63SLen Brown 		if (!sp)
486541618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
486641618e63SLen Brown 		*sp = '\0';
486741618e63SLen Brown 		fclose(input);
486841618e63SLen Brown 
486941618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
487041618e63SLen Brown 
4871dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
4872dd778a5eSLen Brown 			continue;
4873dd778a5eSLen Brown 
487441618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
487541618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
487641618e63SLen Brown 	}
487741618e63SLen Brown 
487841618e63SLen Brown }
487941618e63SLen Brown 
48801ef7d21aSLen Brown 
48811ef7d21aSLen Brown /*
48821ef7d21aSLen Brown  * parse cpuset with following syntax
48831ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
48841ef7d21aSLen Brown  */
48851ef7d21aSLen Brown void parse_cpu_command(char *optarg)
48861ef7d21aSLen Brown {
48871ef7d21aSLen Brown 	unsigned int start, end;
48881ef7d21aSLen Brown 	char *next;
48891ef7d21aSLen Brown 
48904e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
48914e4e1e7cSLen Brown 		if (cpu_subset)
48924e4e1e7cSLen Brown 			goto error;
48934e4e1e7cSLen Brown 		show_core_only++;
48944e4e1e7cSLen Brown 		return;
48954e4e1e7cSLen Brown 	}
48964e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
48974e4e1e7cSLen Brown 		if (cpu_subset)
48984e4e1e7cSLen Brown 			goto error;
48994e4e1e7cSLen Brown 		show_pkg_only++;
49004e4e1e7cSLen Brown 		return;
49014e4e1e7cSLen Brown 	}
49024e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
49034e4e1e7cSLen Brown 		goto error;
49044e4e1e7cSLen Brown 
49051ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
49061ef7d21aSLen Brown 	if (cpu_subset == NULL)
49071ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
49081ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
49091ef7d21aSLen Brown 
49101ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
49111ef7d21aSLen Brown 
49121ef7d21aSLen Brown 	next = optarg;
49131ef7d21aSLen Brown 
49141ef7d21aSLen Brown 	while (next && *next) {
49151ef7d21aSLen Brown 
49161ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
49171ef7d21aSLen Brown 			goto error;
49181ef7d21aSLen Brown 
49191ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
49201ef7d21aSLen Brown 
49211ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
49221ef7d21aSLen Brown 			goto error;
49231ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
49241ef7d21aSLen Brown 
49251ef7d21aSLen Brown 		if (*next == '\0')
49261ef7d21aSLen Brown 			break;
49271ef7d21aSLen Brown 
49281ef7d21aSLen Brown 		if (*next == ',') {
49291ef7d21aSLen Brown 			next += 1;
49301ef7d21aSLen Brown 			continue;
49311ef7d21aSLen Brown 		}
49321ef7d21aSLen Brown 
49331ef7d21aSLen Brown 		if (*next == '-') {
49341ef7d21aSLen Brown 			next += 1;	/* start range */
49351ef7d21aSLen Brown 		} else if (*next == '.') {
49361ef7d21aSLen Brown 			next += 1;
49371ef7d21aSLen Brown 			if (*next == '.')
49381ef7d21aSLen Brown 				next += 1;	/* start range */
49391ef7d21aSLen Brown 			else
49401ef7d21aSLen Brown 				goto error;
49411ef7d21aSLen Brown 		}
49421ef7d21aSLen Brown 
49431ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
49441ef7d21aSLen Brown 		if (end <= start)
49451ef7d21aSLen Brown 			goto error;
49461ef7d21aSLen Brown 
49471ef7d21aSLen Brown 		while (++start <= end) {
49481ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
49491ef7d21aSLen Brown 				goto error;
49501ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
49511ef7d21aSLen Brown 		}
49521ef7d21aSLen Brown 
49531ef7d21aSLen Brown 		if (*next == ',')
49541ef7d21aSLen Brown 			next += 1;
49551ef7d21aSLen Brown 		else if (*next != '\0')
49561ef7d21aSLen Brown 			goto error;
49571ef7d21aSLen Brown 	}
49581ef7d21aSLen Brown 
49591ef7d21aSLen Brown 	return;
49601ef7d21aSLen Brown 
49611ef7d21aSLen Brown error:
49624e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
49634e4e1e7cSLen Brown 	help();
49641ef7d21aSLen Brown 	exit(-1);
49651ef7d21aSLen Brown }
49661ef7d21aSLen Brown 
4967812db3f7SLen Brown int shown;
4968812db3f7SLen Brown /*
4969812db3f7SLen Brown  * parse_show_hide() - process cmdline to set default counter action
4970812db3f7SLen Brown  */
4971812db3f7SLen Brown void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4972812db3f7SLen Brown {
4973812db3f7SLen Brown 	/*
4974812db3f7SLen Brown 	 * --show: show only those specified
4975812db3f7SLen Brown 	 *  The 1st invocation will clear and replace the enabled mask
4976812db3f7SLen Brown 	 *  subsequent invocations can add to it.
4977812db3f7SLen Brown 	 */
4978812db3f7SLen Brown 	if (new_mode == SHOW_LIST) {
4979812db3f7SLen Brown 		if (shown == 0)
4980dd778a5eSLen Brown 			bic_enabled = bic_lookup(optarg, new_mode);
4981812db3f7SLen Brown 		else
4982dd778a5eSLen Brown 			bic_enabled |= bic_lookup(optarg, new_mode);
4983812db3f7SLen Brown 		shown = 1;
4984812db3f7SLen Brown 
4985812db3f7SLen Brown 		return;
4986812db3f7SLen Brown 	}
4987812db3f7SLen Brown 
4988812db3f7SLen Brown 	/*
4989812db3f7SLen Brown 	 * --hide: do not show those specified
4990812db3f7SLen Brown 	 *  multiple invocations simply clear more bits in enabled mask
4991812db3f7SLen Brown 	 */
4992dd778a5eSLen Brown 	bic_enabled &= ~bic_lookup(optarg, new_mode);
499341618e63SLen Brown 
4994812db3f7SLen Brown }
4995812db3f7SLen Brown 
4996103a8feaSLen Brown void cmdline(int argc, char **argv)
4997103a8feaSLen Brown {
4998103a8feaSLen Brown 	int opt;
4999d8af6f5fSLen Brown 	int option_index = 0;
5000d8af6f5fSLen Brown 	static struct option long_options[] = {
5001388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
50021ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5003d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
500496e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
5005d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5006d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5007812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5008d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5009c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5010c91fc851SLen Brown 		{"migrate",	no_argument,		0, 'm'},
5011b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
501296e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5013812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5014d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5015d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5016d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5017d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5018d8af6f5fSLen Brown 	};
5019103a8feaSLen Brown 
5020103a8feaSLen Brown 	progname = argv[0];
5021103a8feaSLen Brown 
5022c91fc851SLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:Jmo:qST:v",
5023d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5024103a8feaSLen Brown 		switch (opt) {
5025388e9c81SLen Brown 		case 'a':
5026388e9c81SLen Brown 			parse_add_command(optarg);
5027388e9c81SLen Brown 			break;
50281ef7d21aSLen Brown 		case 'c':
50291ef7d21aSLen Brown 			parse_cpu_command(optarg);
50301ef7d21aSLen Brown 			break;
5031d8af6f5fSLen Brown 		case 'D':
5032d8af6f5fSLen Brown 			dump_only++;
50338e180f3cSLen Brown 			break;
5034d8af6f5fSLen Brown 		case 'd':
5035d8af6f5fSLen Brown 			debug++;
50362f32edf1SLen Brown 			break;
5037812db3f7SLen Brown 		case 'H':
5038812db3f7SLen Brown 			parse_show_hide(optarg, HIDE_LIST);
5039812db3f7SLen Brown 			break;
5040d8af6f5fSLen Brown 		case 'h':
5041d8af6f5fSLen Brown 		default:
5042d8af6f5fSLen Brown 			help();
5043d8af6f5fSLen Brown 			exit(1);
5044d8af6f5fSLen Brown 		case 'i':
50452a0609c0SLen Brown 			{
50462a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
50472a0609c0SLen Brown 
50482a0609c0SLen Brown 				if (interval < 0.001) {
5049b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
50502a0609c0SLen Brown 						interval);
50512a0609c0SLen Brown 					exit(2);
50522a0609c0SLen Brown 				}
50532a0609c0SLen Brown 
50542a0609c0SLen Brown 				interval_ts.tv_sec = interval;
50552a0609c0SLen Brown 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
50562a0609c0SLen Brown 			}
5057889facbeSLen Brown 			break;
50585c56be9aSDirk Brandewie 		case 'J':
50595c56be9aSDirk Brandewie 			rapl_joules++;
50605c56be9aSDirk Brandewie 			break;
5061c8ade361SLen Brown 		case 'l':
5062c8ade361SLen Brown 			list_header_only++;
5063c8ade361SLen Brown 			quiet++;
5064c8ade361SLen Brown 			break;
5065c91fc851SLen Brown 		case 'm':
5066c91fc851SLen Brown 			do_migrate = 1;
5067c91fc851SLen Brown 			break;
5068b7d8c148SLen Brown 		case 'o':
5069b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5070b7d8c148SLen Brown 			break;
507196e47158SLen Brown 		case 'q':
507296e47158SLen Brown 			quiet = 1;
507396e47158SLen Brown 			break;
5074812db3f7SLen Brown 		case 's':
5075812db3f7SLen Brown 			parse_show_hide(optarg, SHOW_LIST);
5076812db3f7SLen Brown 			break;
5077d8af6f5fSLen Brown 		case 'S':
5078d8af6f5fSLen Brown 			summary_only++;
5079d8af6f5fSLen Brown 			break;
5080d8af6f5fSLen Brown 		case 'T':
5081d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5082d8af6f5fSLen Brown 			break;
5083d8af6f5fSLen Brown 		case 'v':
5084d8af6f5fSLen Brown 			print_version();
5085d8af6f5fSLen Brown 			exit(0);
5086d8af6f5fSLen Brown 			break;
5087103a8feaSLen Brown 		}
5088103a8feaSLen Brown 	}
5089103a8feaSLen Brown }
5090103a8feaSLen Brown 
5091103a8feaSLen Brown int main(int argc, char **argv)
5092103a8feaSLen Brown {
5093b7d8c148SLen Brown 	outf = stderr;
5094b7d8c148SLen Brown 
5095103a8feaSLen Brown 	cmdline(argc, argv);
5096103a8feaSLen Brown 
509796e47158SLen Brown 	if (!quiet)
5098d8af6f5fSLen Brown 		print_version();
5099103a8feaSLen Brown 
510041618e63SLen Brown 	probe_sysfs();
510141618e63SLen Brown 
5102103a8feaSLen Brown 	turbostat_init();
5103103a8feaSLen Brown 
51043b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
51053b4d5c7fSAndy Shevchenko 	if (dump_only)
51063b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
51073b4d5c7fSAndy Shevchenko 
5108c8ade361SLen Brown 	/* list header and exit */
5109c8ade361SLen Brown 	if (list_header_only) {
5110c8ade361SLen Brown 		print_header(",");
5111c8ade361SLen Brown 		flush_output_stdout();
5112c8ade361SLen Brown 		return 0;
5113c8ade361SLen Brown 	}
5114c8ade361SLen Brown 
5115103a8feaSLen Brown 	/*
5116103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5117103a8feaSLen Brown 	 */
5118103a8feaSLen Brown 	if (argc - optind)
5119103a8feaSLen Brown 		return fork_it(argv + optind);
5120103a8feaSLen Brown 	else
5121103a8feaSLen Brown 		turbostat_loop();
5122103a8feaSLen Brown 
5123103a8feaSLen Brown 	return 0;
5124103a8feaSLen Brown }
5125