xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision f4fdf2b474606580b95eed95d06c762d4fd3f57b)
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;
9521ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
967f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
977f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
987f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
997f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1007f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1017f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
10233148d67SLen Brown unsigned int has_misc_feature_control;
103889facbeSLen Brown 
104889facbeSLen Brown #define RAPL_PKG		(1 << 0)
105e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
106e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
107e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
108e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
109e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
110e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
111e6f9bb3cSLen Brown 
112889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
113e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
114e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
115e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
116e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1170b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1180b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
119e6f9bb3cSLen Brown 
1209148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
121e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1220b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
123e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
124e6f9bb3cSLen Brown 
1250b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
126e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
127e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
128e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1299148494cSJacob Pan 
1309148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1319148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1329148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
133889facbeSLen Brown #define	TJMAX_DEFAULT	100
134889facbeSLen Brown 
135889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
136103a8feaSLen Brown 
137388e9c81SLen Brown /*
138388e9c81SLen Brown  * buffer size used by sscanf() for added column names
139388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
140388e9c81SLen Brown  */
141388e9c81SLen Brown #define	NAME_BYTES 20
142495c7654SLen Brown #define PATH_BYTES 128
143388e9c81SLen Brown 
144103a8feaSLen Brown int backwards_count;
145103a8feaSLen Brown char *progname;
146103a8feaSLen Brown 
1471ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
1481ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
1491ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
150678a3bd1SLen Brown #define MAX_ADDED_COUNTERS 16
151c98d5d94SLen Brown 
152c98d5d94SLen Brown struct thread_data {
153*f4fdf2b4SLen Brown 	struct timeval tv_begin;
154*f4fdf2b4SLen Brown 	struct timeval tv_end;
155c98d5d94SLen Brown 	unsigned long long tsc;
156c98d5d94SLen Brown 	unsigned long long aperf;
157c98d5d94SLen Brown 	unsigned long long mperf;
158144b44b1SLen Brown 	unsigned long long c1;
1590de6c0dfSLen Brown 	unsigned long long  irq_count;
1601ed51011SLen Brown 	unsigned int smi_count;
161c98d5d94SLen Brown 	unsigned int cpu_id;
162c98d5d94SLen Brown 	unsigned int flags;
163c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
164c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
165678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
166c98d5d94SLen Brown } *thread_even, *thread_odd;
167c98d5d94SLen Brown 
168c98d5d94SLen Brown struct core_data {
169c98d5d94SLen Brown 	unsigned long long c3;
170c98d5d94SLen Brown 	unsigned long long c6;
171c98d5d94SLen Brown 	unsigned long long c7;
1720539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
173889facbeSLen Brown 	unsigned int core_temp_c;
174c98d5d94SLen Brown 	unsigned int core_id;
175678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
176c98d5d94SLen Brown } *core_even, *core_odd;
177c98d5d94SLen Brown 
178c98d5d94SLen Brown struct pkg_data {
179c98d5d94SLen Brown 	unsigned long long pc2;
180c98d5d94SLen Brown 	unsigned long long pc3;
181c98d5d94SLen Brown 	unsigned long long pc6;
182c98d5d94SLen Brown 	unsigned long long pc7;
183ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
184ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
185ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1860b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1870b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1880b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1890b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
1909185e988SLen Brown 	long long gfx_rc6_ms;
19127d47356SLen Brown 	unsigned int gfx_mhz;
192c98d5d94SLen Brown 	unsigned int package_id;
193889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
194889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
195889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
196889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
197889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
198889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
199889facbeSLen Brown 	unsigned int pkg_temp_c;
200678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
201c98d5d94SLen Brown } *package_even, *package_odd;
202c98d5d94SLen Brown 
203c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
204c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
205c98d5d94SLen Brown 
206c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
207c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
208c98d5d94SLen Brown 		topo.num_threads_per_core + \
209c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
210c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
211c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
212c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
213c98d5d94SLen Brown 
214388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
21541618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
216388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
217388e9c81SLen Brown 
218388e9c81SLen Brown struct msr_counter {
219388e9c81SLen Brown 	unsigned int msr_num;
220388e9c81SLen Brown 	char name[NAME_BYTES];
221495c7654SLen Brown 	char path[PATH_BYTES];
222388e9c81SLen Brown 	unsigned int width;
223388e9c81SLen Brown 	enum counter_type type;
224388e9c81SLen Brown 	enum counter_format format;
225388e9c81SLen Brown 	struct msr_counter *next;
226812db3f7SLen Brown 	unsigned int flags;
227812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
228812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
22941618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
230388e9c81SLen Brown };
231388e9c81SLen Brown 
232388e9c81SLen Brown struct sys_counters {
233678a3bd1SLen Brown 	unsigned int added_thread_counters;
234678a3bd1SLen Brown 	unsigned int added_core_counters;
235678a3bd1SLen Brown 	unsigned int added_package_counters;
236388e9c81SLen Brown 	struct msr_counter *tp;
237388e9c81SLen Brown 	struct msr_counter *cp;
238388e9c81SLen Brown 	struct msr_counter *pp;
239388e9c81SLen Brown } sys;
240388e9c81SLen Brown 
241c98d5d94SLen Brown struct system_summary {
242c98d5d94SLen Brown 	struct thread_data threads;
243c98d5d94SLen Brown 	struct core_data cores;
244c98d5d94SLen Brown 	struct pkg_data packages;
245388e9c81SLen Brown } average;
246c98d5d94SLen Brown 
247c98d5d94SLen Brown 
248c98d5d94SLen Brown struct topo_params {
249c98d5d94SLen Brown 	int num_packages;
250103a8feaSLen Brown 	int num_cpus;
251c98d5d94SLen Brown 	int num_cores;
252c98d5d94SLen Brown 	int max_cpu_num;
253c98d5d94SLen Brown 	int num_cores_per_pkg;
254c98d5d94SLen Brown 	int num_threads_per_core;
255c98d5d94SLen Brown } topo;
256103a8feaSLen Brown 
257c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
258103a8feaSLen Brown 
259562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
260562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
261562a2d37SLen Brown 
262c98d5d94SLen Brown void setup_all_buffers(void);
263103a8feaSLen Brown 
264c98d5d94SLen Brown int cpu_is_not_present(int cpu)
265d15cf7c1SLen Brown {
266c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
267c98d5d94SLen Brown }
268c98d5d94SLen Brown /*
269c98d5d94SLen Brown  * run func(thread, core, package) in topology order
270c98d5d94SLen Brown  * skip non-present cpus
271c98d5d94SLen Brown  */
272c98d5d94SLen Brown 
273c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
274c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
275c98d5d94SLen Brown {
276c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
277c98d5d94SLen Brown 
278c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
279c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
280c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
281c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
282c98d5d94SLen Brown 				struct thread_data *t;
283c98d5d94SLen Brown 				struct core_data *c;
284c98d5d94SLen Brown 				struct pkg_data *p;
285c98d5d94SLen Brown 
286c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
287c98d5d94SLen Brown 
288c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
289c98d5d94SLen Brown 					continue;
290c98d5d94SLen Brown 
291c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
292c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
293c98d5d94SLen Brown 
294c98d5d94SLen Brown 				retval = func(t, c, p);
295c98d5d94SLen Brown 				if (retval)
296c98d5d94SLen Brown 					return retval;
297c98d5d94SLen Brown 			}
298c98d5d94SLen Brown 		}
299c98d5d94SLen Brown 	}
300d15cf7c1SLen Brown 	return 0;
301d15cf7c1SLen Brown }
302d15cf7c1SLen Brown 
30388c3281fSLen Brown int cpu_migrate(int cpu)
30488c3281fSLen Brown {
305c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
306c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
307c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
30888c3281fSLen Brown 		return -1;
30988c3281fSLen Brown 	else
31088c3281fSLen Brown 		return 0;
31188c3281fSLen Brown }
31236229897SLen Brown int get_msr_fd(int cpu)
313103a8feaSLen Brown {
314103a8feaSLen Brown 	char pathname[32];
315103a8feaSLen Brown 	int fd;
316103a8feaSLen Brown 
31736229897SLen Brown 	fd = fd_percpu[cpu];
31836229897SLen Brown 
31936229897SLen Brown 	if (fd)
32036229897SLen Brown 		return fd;
32136229897SLen Brown 
322103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
323103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
32415aaa346SLen Brown 	if (fd < 0)
32598481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
326103a8feaSLen Brown 
32736229897SLen Brown 	fd_percpu[cpu] = fd;
32836229897SLen Brown 
32936229897SLen Brown 	return fd;
33036229897SLen Brown }
33136229897SLen Brown 
33236229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
33336229897SLen Brown {
33436229897SLen Brown 	ssize_t retval;
33536229897SLen Brown 
33636229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
33715aaa346SLen Brown 
33898481e79SLen Brown 	if (retval != sizeof *msr)
339cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
34015aaa346SLen Brown 
34115aaa346SLen Brown 	return 0;
342103a8feaSLen Brown }
343103a8feaSLen Brown 
344fc04cc67SLen Brown /*
345812db3f7SLen Brown  * Each string in this array is compared in --show and --hide cmdline.
346812db3f7SLen Brown  * Thus, strings that are proper sub-sets must follow their more specific peers.
347fc04cc67SLen Brown  */
348812db3f7SLen Brown struct msr_counter bic[] = {
349812db3f7SLen Brown 	{ 0x0, "Package" },
350812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
351812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
352812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
353812db3f7SLen Brown 	{ 0x0, "IRQ" },
354495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
355812db3f7SLen Brown 	{ 0x0, "Busy%" },
356812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
357812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
358812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
359812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
360812db3f7SLen Brown 	{ 0x0, "ThreadC" },
361812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
362812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
363812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
364812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
365812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
366812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
367812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
368812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
369812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
3700f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
3710f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
3720f47c08dSLen Brown 	{ 0x0, "Pkg%pc10" },
373812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
374812db3f7SLen Brown 	{ 0x0, "CorWatt" },
375812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
376812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
377812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
378812db3f7SLen Brown 	{ 0x0, "PKG_%" },
379812db3f7SLen Brown 	{ 0x0, "RAM_%" },
380812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
381812db3f7SLen Brown 	{ 0x0, "Cor_J" },
382812db3f7SLen Brown 	{ 0x0, "GFX_J" },
383812db3f7SLen Brown 	{ 0x0, "RAM_J" },
384812db3f7SLen Brown 	{ 0x0, "Core" },
385812db3f7SLen Brown 	{ 0x0, "CPU" },
3860539ba11SLen Brown 	{ 0x0, "Mod%c6" },
38741618e63SLen Brown 	{ 0x0, "sysfs" },
388a99d8730SLen Brown 	{ 0x0, "Totl%C0" },
389a99d8730SLen Brown 	{ 0x0, "Any%C0" },
390a99d8730SLen Brown 	{ 0x0, "GFX%C0" },
391a99d8730SLen Brown 	{ 0x0, "CPUGFX%" },
392812db3f7SLen Brown };
393812db3f7SLen Brown 
394a99d8730SLen Brown 
395a99d8730SLen Brown 
396812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
397812db3f7SLen Brown #define	BIC_Package	(1ULL << 0)
398812db3f7SLen Brown #define	BIC_Avg_MHz	(1ULL << 1)
399812db3f7SLen Brown #define	BIC_Bzy_MHz	(1ULL << 2)
400812db3f7SLen Brown #define	BIC_TSC_MHz	(1ULL << 3)
401812db3f7SLen Brown #define	BIC_IRQ		(1ULL << 4)
402812db3f7SLen Brown #define	BIC_SMI		(1ULL << 5)
403812db3f7SLen Brown #define	BIC_Busy	(1ULL << 6)
404812db3f7SLen Brown #define	BIC_CPU_c1	(1ULL << 7)
405812db3f7SLen Brown #define	BIC_CPU_c3	(1ULL << 8)
406812db3f7SLen Brown #define	BIC_CPU_c6	(1ULL << 9)
407812db3f7SLen Brown #define	BIC_CPU_c7	(1ULL << 10)
408812db3f7SLen Brown #define	BIC_ThreadC	(1ULL << 11)
409812db3f7SLen Brown #define	BIC_CoreTmp	(1ULL << 12)
410812db3f7SLen Brown #define	BIC_CoreCnt	(1ULL << 13)
411812db3f7SLen Brown #define	BIC_PkgTmp	(1ULL << 14)
412812db3f7SLen Brown #define	BIC_GFX_rc6	(1ULL << 15)
413812db3f7SLen Brown #define	BIC_GFXMHz	(1ULL << 16)
414812db3f7SLen Brown #define	BIC_Pkgpc2	(1ULL << 17)
415812db3f7SLen Brown #define	BIC_Pkgpc3	(1ULL << 18)
416812db3f7SLen Brown #define	BIC_Pkgpc6	(1ULL << 19)
417812db3f7SLen Brown #define	BIC_Pkgpc7	(1ULL << 20)
4180f47c08dSLen Brown #define	BIC_Pkgpc8	(1ULL << 21)
4190f47c08dSLen Brown #define	BIC_Pkgpc9	(1ULL << 22)
4200f47c08dSLen Brown #define	BIC_Pkgpc10	(1ULL << 23)
4210f47c08dSLen Brown #define	BIC_PkgWatt	(1ULL << 24)
4220f47c08dSLen Brown #define	BIC_CorWatt	(1ULL << 25)
4230f47c08dSLen Brown #define	BIC_GFXWatt	(1ULL << 26)
4240f47c08dSLen Brown #define	BIC_PkgCnt	(1ULL << 27)
4250f47c08dSLen Brown #define	BIC_RAMWatt	(1ULL << 28)
4260f47c08dSLen Brown #define	BIC_PKG__	(1ULL << 29)
4270f47c08dSLen Brown #define	BIC_RAM__	(1ULL << 30)
4280f47c08dSLen Brown #define	BIC_Pkg_J	(1ULL << 31)
4290f47c08dSLen Brown #define	BIC_Cor_J	(1ULL << 32)
4300f47c08dSLen Brown #define	BIC_GFX_J	(1ULL << 33)
4310f47c08dSLen Brown #define	BIC_RAM_J	(1ULL << 34)
4320f47c08dSLen Brown #define	BIC_Core	(1ULL << 35)
4330f47c08dSLen Brown #define	BIC_CPU		(1ULL << 36)
4340f47c08dSLen Brown #define	BIC_Mod_c6	(1ULL << 37)
43541618e63SLen Brown #define	BIC_sysfs	(1ULL << 38)
436a99d8730SLen Brown #define	BIC_Totl_c0	(1ULL << 39)
437a99d8730SLen Brown #define	BIC_Any_c0	(1ULL << 40)
438a99d8730SLen Brown #define	BIC_GFX_c0	(1ULL << 41)
439a99d8730SLen Brown #define	BIC_CPUGFX	(1ULL << 42)
440812db3f7SLen Brown 
441812db3f7SLen Brown unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
44241618e63SLen Brown unsigned long long bic_present = BIC_sysfs;
443812db3f7SLen Brown 
444812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
445812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
4460f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
447812db3f7SLen Brown 
448dd778a5eSLen Brown #define MAX_DEFERRED 16
449dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
450dd778a5eSLen Brown int deferred_skip_index;
451dd778a5eSLen Brown 
452dd778a5eSLen Brown /*
453dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
454dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
455dd778a5eSLen Brown  */
456dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
457dd778a5eSLen Brown 
458dd778a5eSLen Brown void help(void)
459dd778a5eSLen Brown {
460dd778a5eSLen Brown 	fprintf(outf,
461dd778a5eSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
462dd778a5eSLen Brown 	"\n"
463dd778a5eSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
464dd778a5eSLen Brown 	"when COMMAND completes.\n"
465dd778a5eSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
466dd778a5eSLen Brown 	"to print statistics, until interrupted.\n"
467dd778a5eSLen Brown 	"--add		add a counter\n"
468dd778a5eSLen Brown 	"		eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
469dd778a5eSLen Brown 	"--cpu	cpu-set	limit output to summary plus cpu-set:\n"
470dd778a5eSLen Brown 	"		{core | package | j,k,l..m,n-p }\n"
471dd778a5eSLen Brown 	"--quiet	skip decoding system configuration header\n"
472dd778a5eSLen Brown 	"--interval sec	Override default 5-second measurement interval\n"
473dd778a5eSLen Brown 	"--help		print this help message\n"
474dd778a5eSLen Brown 	"--list		list column headers only\n"
475dd778a5eSLen Brown 	"--out file	create or truncate \"file\" for all output\n"
476dd778a5eSLen Brown 	"--version	print version information\n"
477dd778a5eSLen Brown 	"\n"
478dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
479dd778a5eSLen Brown }
480dd778a5eSLen Brown 
481812db3f7SLen Brown /*
482812db3f7SLen Brown  * bic_lookup
483812db3f7SLen Brown  * for all the strings in comma separate name_list,
484812db3f7SLen Brown  * set the approprate bit in return value.
485812db3f7SLen Brown  */
486dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
487812db3f7SLen Brown {
488812db3f7SLen Brown 	int i;
489812db3f7SLen Brown 	unsigned long long retval = 0;
490812db3f7SLen Brown 
491812db3f7SLen Brown 	while (name_list) {
492812db3f7SLen Brown 		char *comma;
493812db3f7SLen Brown 
494812db3f7SLen Brown 		comma = strchr(name_list, ',');
495812db3f7SLen Brown 
496812db3f7SLen Brown 		if (comma)
497812db3f7SLen Brown 			*comma = '\0';
498812db3f7SLen Brown 
499812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
500812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
501812db3f7SLen Brown 				retval |= (1ULL << i);
502812db3f7SLen Brown 				break;
503812db3f7SLen Brown 			}
504812db3f7SLen Brown 		}
505812db3f7SLen Brown 		if (i == MAX_BIC) {
506dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
507812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
508812db3f7SLen Brown 				exit(-1);
509812db3f7SLen Brown 			}
510dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
511dd778a5eSLen Brown 			if (debug)
512dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
513dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
514dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
515dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
516dd778a5eSLen Brown 				help();
517dd778a5eSLen Brown 				exit(1);
518dd778a5eSLen Brown 			}
519dd778a5eSLen Brown 		}
520812db3f7SLen Brown 
521812db3f7SLen Brown 		name_list = comma;
522812db3f7SLen Brown 		if (name_list)
523812db3f7SLen Brown 			name_list++;
524812db3f7SLen Brown 
525812db3f7SLen Brown 	}
526812db3f7SLen Brown 	return retval;
527812db3f7SLen Brown }
528fc04cc67SLen Brown 
529dd778a5eSLen Brown 
530c8ade361SLen Brown void print_header(char *delim)
531103a8feaSLen Brown {
532388e9c81SLen Brown 	struct msr_counter *mp;
5336168c2e0SLen Brown 	int printed = 0;
534388e9c81SLen Brown 
535*f4fdf2b4SLen Brown 	if (debug)
536*f4fdf2b4SLen Brown 		outp += sprintf(outp, "usec %s", delim);
537812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
5386168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
539812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
5406168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
541812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
5426168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
543812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
5446168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
545812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
5466168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
547812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
5486168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
549812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
5506168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
5511cc21f7bSLen Brown 
5520de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
5530de6c0dfSLen Brown 		if (sums_need_wide_columns)
5546168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
5550de6c0dfSLen Brown 		else
5566168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
5570de6c0dfSLen Brown 	}
5580de6c0dfSLen Brown 
559812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
5606168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
5611cc21f7bSLen Brown 
562388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
563dd778a5eSLen Brown 
564388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
565388e9c81SLen Brown 			if (mp->width == 64)
566dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
567388e9c81SLen Brown 			else
568dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
569388e9c81SLen Brown 		} else {
5700de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
571dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
5720de6c0dfSLen Brown 			else
573dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
574388e9c81SLen Brown 		}
575388e9c81SLen Brown 	}
576388e9c81SLen Brown 
57741618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
5786168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
579812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
5806168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
581812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
5826168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
583812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
5846168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
585678a3bd1SLen Brown 
5860539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
5876168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
588678a3bd1SLen Brown 
589812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
5906168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
591388e9c81SLen Brown 
592388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
593388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
594388e9c81SLen Brown 			if (mp->width == 64)
595c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
596388e9c81SLen Brown 			else
597c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
598388e9c81SLen Brown 		} else {
5990de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6000de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6010de6c0dfSLen Brown 			else
602c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
603388e9c81SLen Brown 		}
604388e9c81SLen Brown 	}
605388e9c81SLen Brown 
606812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
6076168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
608889facbeSLen Brown 
609812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
6106168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
611fdf676e5SLen Brown 
612812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
6136168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
61427d47356SLen Brown 
615a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
6166168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
617a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
6186168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
619a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
6206168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
621a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
6226168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
6230b2bb692SLen Brown 
6240f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
6256168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
6260f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
6276168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
6280f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
6296168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
6300f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
6316168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
6320f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
6336168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
6340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
6356168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
6360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
6376168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
638103a8feaSLen Brown 
6395c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
640812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
6416168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
642812db3f7SLen Brown 		if (DO_BIC(BIC_CorWatt))
6436168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
644812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
6456168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
646812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
6476168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
648812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
6496168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
650812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
6516168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
652d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
653812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
6546168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
655812db3f7SLen Brown 		if (DO_BIC(BIC_Cor_J))
6566168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
657812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
6586168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
659812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
6606168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
661812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
6626168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
663812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
6646168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
6655c56be9aSDirk Brandewie 	}
666388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
667388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
668388e9c81SLen Brown 			if (mp->width == 64)
669c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
670388e9c81SLen Brown 			else
671c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
672388e9c81SLen Brown 		} else {
6730de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6740de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6750de6c0dfSLen Brown 			else
676c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
677388e9c81SLen Brown 		}
678388e9c81SLen Brown 	}
679388e9c81SLen Brown 
680c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
681103a8feaSLen Brown }
682103a8feaSLen Brown 
683c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
684c98d5d94SLen Brown 	struct pkg_data *p)
685103a8feaSLen Brown {
686388e9c81SLen Brown 	int i;
687388e9c81SLen Brown 	struct msr_counter *mp;
688388e9c81SLen Brown 
6893b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
690c98d5d94SLen Brown 
691c98d5d94SLen Brown 	if (t) {
6923b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
6933b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
6943b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
6953b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
6963b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
6973b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
6986886fee4SLen Brown 
699812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
7000de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
701812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
702218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
703388e9c81SLen Brown 
704388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
705388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
706388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
707388e9c81SLen Brown 		}
708103a8feaSLen Brown 	}
709103a8feaSLen Brown 
710c98d5d94SLen Brown 	if (c) {
7113b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
7123b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
7133b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
7143b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
7153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
716388e9c81SLen Brown 
717388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
718388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
719388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
720388e9c81SLen Brown 		}
7210539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
722c98d5d94SLen Brown 	}
723103a8feaSLen Brown 
724c98d5d94SLen Brown 	if (p) {
7253b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
7260b2bb692SLen Brown 
7270b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
7280b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
7290b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
7300b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
7310b2bb692SLen Brown 
7323b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
7330f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
7343b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
7350f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
7363b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
7370f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
7383b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
7393b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
7403b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
7413b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
7423b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
7433b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
7443b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
7453b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
7463b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
7473b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
7483b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
7493b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
7503b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
751388e9c81SLen Brown 
752388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
753388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
754388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
755388e9c81SLen Brown 		}
756c98d5d94SLen Brown 	}
7573b4d5c7fSAndy Shevchenko 
7583b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
7593b4d5c7fSAndy Shevchenko 
760c98d5d94SLen Brown 	return 0;
761103a8feaSLen Brown }
762103a8feaSLen Brown 
763e23da037SLen Brown /*
764e23da037SLen Brown  * column formatting convention & formats
765e23da037SLen Brown  */
766c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
767c98d5d94SLen Brown 	struct pkg_data *p)
768103a8feaSLen Brown {
769008d396eSLen Brown 	double interval_float, tsc;
770fc04cc67SLen Brown 	char *fmt8;
771388e9c81SLen Brown 	int i;
772388e9c81SLen Brown 	struct msr_counter *mp;
7736168c2e0SLen Brown 	char *delim = "\t";
7746168c2e0SLen Brown 	int printed = 0;
775103a8feaSLen Brown 
776c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
777c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
778c98d5d94SLen Brown 		return 0;
779c98d5d94SLen Brown 
780c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
781c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
782c98d5d94SLen Brown 		return 0;
783c98d5d94SLen Brown 
7841ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
7851ef7d21aSLen Brown 	if ((t != &average.threads) &&
7861ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
7871ef7d21aSLen Brown 		return 0;
7881ef7d21aSLen Brown 
789*f4fdf2b4SLen Brown 	if (debug) {
790*f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
791*f4fdf2b4SLen Brown 		struct timeval tv;
792*f4fdf2b4SLen Brown 
793*f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
794*f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
795*f4fdf2b4SLen Brown 	}
796*f4fdf2b4SLen Brown 
797103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
798103a8feaSLen Brown 
799008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
800008d396eSLen Brown 
801c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
802c98d5d94SLen Brown 	if (t == &average.threads) {
803812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
8046168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
805812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
8066168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
807812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
8086168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
809103a8feaSLen Brown 	} else {
810812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
811c98d5d94SLen Brown 			if (p)
8126168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
813c98d5d94SLen Brown 			else
8146168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
815c98d5d94SLen Brown 		}
816812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
817c98d5d94SLen Brown 			if (c)
8186168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
819c98d5d94SLen Brown 			else
8206168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
821c98d5d94SLen Brown 		}
822812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
8236168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
824103a8feaSLen Brown 	}
825fc04cc67SLen Brown 
826812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
8276168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
828fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
829fc04cc67SLen Brown 
830812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
8316168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
832103a8feaSLen Brown 
833812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
83421ed5574SLen Brown 		if (has_base_hz)
8356168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
83621ed5574SLen Brown 		else
8376168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
838008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
83921ed5574SLen Brown 	}
840103a8feaSLen Brown 
841812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
8426168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
843103a8feaSLen Brown 
844562a2d37SLen Brown 	/* IRQ */
8450de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
8460de6c0dfSLen Brown 		if (sums_need_wide_columns)
8476168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
8480de6c0dfSLen Brown 		else
8496168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
8500de6c0dfSLen Brown 	}
851562a2d37SLen Brown 
8521cc21f7bSLen Brown 	/* SMI */
853812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
8546168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
8551cc21f7bSLen Brown 
856678a3bd1SLen Brown 	/* Added counters */
857678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
858678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
859678a3bd1SLen Brown 			if (mp->width == 32)
8605f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
861678a3bd1SLen Brown 			else
8626168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
863678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
8640de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
8656168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
8660de6c0dfSLen Brown 			else
8676168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
868678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
86941618e63SLen Brown 			if (mp->type == COUNTER_USEC)
8706168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
87141618e63SLen Brown 			else
8726168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
873678a3bd1SLen Brown 		}
874678a3bd1SLen Brown 	}
875678a3bd1SLen Brown 
87641618e63SLen Brown 	/* C1 */
87741618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
8786168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
87941618e63SLen Brown 
88041618e63SLen Brown 
881c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
882c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
883c98d5d94SLen Brown 		goto done;
884c98d5d94SLen Brown 
885812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
8866168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
887812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
8886168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
889812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
8906168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
891c98d5d94SLen Brown 
8920539ba11SLen Brown 	/* Mod%c6 */
8930539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
8946168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
8950539ba11SLen Brown 
896812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
8976168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
898889facbeSLen Brown 
899388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
900388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
901388e9c81SLen Brown 			if (mp->width == 32)
9025f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
903388e9c81SLen Brown 			else
9046168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
905388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
9060de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9076168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
9080de6c0dfSLen Brown 			else
9096168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
910388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
9116168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
912388e9c81SLen Brown 		}
913388e9c81SLen Brown 	}
914388e9c81SLen Brown 
915c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
916c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
917c98d5d94SLen Brown 		goto done;
918c98d5d94SLen Brown 
9190b2bb692SLen Brown 	/* PkgTmp */
920812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
9216168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
922889facbeSLen Brown 
923fdf676e5SLen Brown 	/* GFXrc6 */
924812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
925ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
9266168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
9279185e988SLen Brown 		} else {
9286168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
9299185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
9309185e988SLen Brown 		}
9319185e988SLen Brown 	}
932fdf676e5SLen Brown 
93327d47356SLen Brown 	/* GFXMHz */
934812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
9356168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
93627d47356SLen Brown 
9370b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
938a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
9396168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
940a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
9416168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
942a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
9436168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
944a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
9456168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
9460b2bb692SLen Brown 
9470f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
9486168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
9490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
9506168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
9510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
9526168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
9530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
9546168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
9550f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
9566168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
9570f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
9586168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
9590f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
9606168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
961889facbeSLen Brown 
962889facbeSLen Brown 	/*
963889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
964889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
965889facbeSLen Brown  	 */
966fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
9676168c2e0SLen Brown 		fmt8 = "%s%.2f";
968fc04cc67SLen Brown 	else
969fc04cc67SLen Brown 		fmt8 = "%6.0f**";
970889facbeSLen Brown 
971812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
9726168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
973812db3f7SLen Brown 	if (DO_BIC(BIC_CorWatt))
9746168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
975812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
9766168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
977812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
9786168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
979812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
9806168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
981812db3f7SLen Brown 	if (DO_BIC(BIC_Cor_J))
9826168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
983812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
9846168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
985812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
9866168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
987812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
9886168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
989812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
9906168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
991812db3f7SLen Brown 
992388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
993388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
994388e9c81SLen Brown 			if (mp->width == 32)
9955f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
996388e9c81SLen Brown 			else
9976168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
998388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
9990de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10006168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
10010de6c0dfSLen Brown 			else
10026168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1003388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10046168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1005388e9c81SLen Brown 		}
1006388e9c81SLen Brown 	}
1007388e9c81SLen Brown 
1008c98d5d94SLen Brown done:
1009c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
1010c98d5d94SLen Brown 
1011c98d5d94SLen Brown 	return 0;
1012103a8feaSLen Brown }
1013103a8feaSLen Brown 
1014b7d8c148SLen Brown void flush_output_stdout(void)
1015103a8feaSLen Brown {
1016b7d8c148SLen Brown 	FILE *filep;
1017b7d8c148SLen Brown 
1018b7d8c148SLen Brown 	if (outf == stderr)
1019b7d8c148SLen Brown 		filep = stdout;
1020b7d8c148SLen Brown 	else
1021b7d8c148SLen Brown 		filep = outf;
1022b7d8c148SLen Brown 
1023b7d8c148SLen Brown 	fputs(output_buffer, filep);
1024b7d8c148SLen Brown 	fflush(filep);
1025b7d8c148SLen Brown 
1026c98d5d94SLen Brown 	outp = output_buffer;
1027c98d5d94SLen Brown }
1028b7d8c148SLen Brown void flush_output_stderr(void)
1029c98d5d94SLen Brown {
1030b7d8c148SLen Brown 	fputs(output_buffer, outf);
1031b7d8c148SLen Brown 	fflush(outf);
1032c98d5d94SLen Brown 	outp = output_buffer;
1033c98d5d94SLen Brown }
1034c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1035c98d5d94SLen Brown {
1036e23da037SLen Brown 	static int printed;
1037103a8feaSLen Brown 
1038e23da037SLen Brown 	if (!printed || !summary_only)
1039c8ade361SLen Brown 		print_header("\t");
1040103a8feaSLen Brown 
1041c98d5d94SLen Brown 	if (topo.num_cpus > 1)
1042c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
1043c98d5d94SLen Brown 			&average.packages);
1044103a8feaSLen Brown 
1045e23da037SLen Brown 	printed = 1;
1046e23da037SLen Brown 
1047e23da037SLen Brown 	if (summary_only)
1048e23da037SLen Brown 		return;
1049e23da037SLen Brown 
1050c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1051103a8feaSLen Brown }
1052103a8feaSLen Brown 
1053889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1054889facbeSLen Brown 	if (new > old) {			\
1055889facbeSLen Brown 		old = new - old;		\
1056889facbeSLen Brown 	} else {				\
1057889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1058889facbeSLen Brown 	}
1059889facbeSLen Brown 
1060ba3dec99SLen Brown int
1061c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1062103a8feaSLen Brown {
1063388e9c81SLen Brown 	int i;
1064388e9c81SLen Brown 	struct msr_counter *mp;
10650b2bb692SLen Brown 
1066a99d8730SLen Brown 
1067a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
10680b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1069a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
10700b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1071a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
10720b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1073a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
10740b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1075a99d8730SLen Brown 
1076c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
10770f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1078c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
10790f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1080c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
10810f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1082c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1083ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1084ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1085ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1086889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1087889facbeSLen Brown 
10889185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
10899185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
10909185e988SLen Brown 		old->gfx_rc6_ms = -1;
10919185e988SLen Brown 	else
1092fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
10939185e988SLen Brown 
109427d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
109527d47356SLen Brown 
1096889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1097889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1098889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1099889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1100889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1101889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1102ba3dec99SLen Brown 
1103388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1104388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1105388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1106388e9c81SLen Brown 		else
1107388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1108388e9c81SLen Brown 	}
1109388e9c81SLen Brown 
1110ba3dec99SLen Brown 	return 0;
1111103a8feaSLen Brown }
1112103a8feaSLen Brown 
1113c98d5d94SLen Brown void
1114c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1115c98d5d94SLen Brown {
1116388e9c81SLen Brown 	int i;
1117388e9c81SLen Brown 	struct msr_counter *mp;
1118388e9c81SLen Brown 
1119c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1120c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1121c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1122889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
11230539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1124388e9c81SLen Brown 
1125388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1126388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1127388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1128388e9c81SLen Brown 		else
1129388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1130388e9c81SLen Brown 	}
1131103a8feaSLen Brown }
1132c98d5d94SLen Brown 
1133c3ae331dSLen Brown /*
1134c3ae331dSLen Brown  * old = new - old
1135c3ae331dSLen Brown  */
1136ba3dec99SLen Brown int
1137c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1138c98d5d94SLen Brown 	struct core_data *core_delta)
1139c98d5d94SLen Brown {
1140388e9c81SLen Brown 	int i;
1141388e9c81SLen Brown 	struct msr_counter *mp;
1142388e9c81SLen Brown 
1143c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1144c98d5d94SLen Brown 
1145103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1146b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1147b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1148b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1149b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1150103a8feaSLen Brown 
1151c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1152c98d5d94SLen Brown 
1153812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1154c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1155c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1156c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1157c98d5d94SLen Brown 		} else {
1158ba3dec99SLen Brown 			return -1;
1159103a8feaSLen Brown 		}
1160a729617cSLen Brown 	}
1161103a8feaSLen Brown 
1162c98d5d94SLen Brown 
1163144b44b1SLen Brown 	if (use_c1_residency_msr) {
1164144b44b1SLen Brown 		/*
1165144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1166144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1167144b44b1SLen Brown 		 */
1168144b44b1SLen Brown 	} else {
1169103a8feaSLen Brown 		/*
1170c3ae331dSLen Brown 		 * As counter collection is not atomic,
1171c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1172103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1173103a8feaSLen Brown 		 */
117495149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1175c98d5d94SLen Brown 			old->c1 = 0;
1176c98d5d94SLen Brown 		else {
1177c98d5d94SLen Brown 			/* normal case, derive c1 */
1178008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1179c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1180c98d5d94SLen Brown 		}
1181144b44b1SLen Brown 	}
1182c3ae331dSLen Brown 
1183c98d5d94SLen Brown 	if (old->mperf == 0) {
1184b7d8c148SLen Brown 		if (debug > 1)
1185b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1186c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1187c98d5d94SLen Brown 	}
1188103a8feaSLen Brown 
1189812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1190562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1191562a2d37SLen Brown 
1192812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
11931ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1194ba3dec99SLen Brown 
1195388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1196388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1197388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1198388e9c81SLen Brown 		else
1199388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1200388e9c81SLen Brown 	}
1201ba3dec99SLen Brown 	return 0;
1202103a8feaSLen Brown }
1203c98d5d94SLen Brown 
1204c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1205c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1206c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1207c98d5d94SLen Brown {
1208ba3dec99SLen Brown 	int retval = 0;
1209ba3dec99SLen Brown 
1210c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1211c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1212c98d5d94SLen Brown 		delta_core(c, c2);
1213c98d5d94SLen Brown 
1214c98d5d94SLen Brown 	/* always calculate thread delta */
1215ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1216ba3dec99SLen Brown 	if (retval)
1217ba3dec99SLen Brown 		return retval;
1218c98d5d94SLen Brown 
1219c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1220c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1221ba3dec99SLen Brown 		retval = delta_package(p, p2);
1222c98d5d94SLen Brown 
1223ba3dec99SLen Brown 	return retval;
1224103a8feaSLen Brown }
1225103a8feaSLen Brown 
1226c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1227103a8feaSLen Brown {
1228388e9c81SLen Brown 	int i;
1229388e9c81SLen Brown 	struct msr_counter  *mp;
1230388e9c81SLen Brown 
1231c98d5d94SLen Brown 	t->tsc = 0;
1232c98d5d94SLen Brown 	t->aperf = 0;
1233c98d5d94SLen Brown 	t->mperf = 0;
1234c98d5d94SLen Brown 	t->c1 = 0;
1235103a8feaSLen Brown 
1236562a2d37SLen Brown 	t->irq_count = 0;
1237562a2d37SLen Brown 	t->smi_count = 0;
1238562a2d37SLen Brown 
1239c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1240c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1241c98d5d94SLen Brown 
1242c98d5d94SLen Brown 	c->c3 = 0;
1243c98d5d94SLen Brown 	c->c6 = 0;
1244c98d5d94SLen Brown 	c->c7 = 0;
12450539ba11SLen Brown 	c->mc6_us = 0;
1246889facbeSLen Brown 	c->core_temp_c = 0;
1247c98d5d94SLen Brown 
12480b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
12490b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
12500b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
12510b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
12520b2bb692SLen Brown 
1253c98d5d94SLen Brown 	p->pc2 = 0;
12540f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1255c98d5d94SLen Brown 		p->pc3 = 0;
12560f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1257c98d5d94SLen Brown 		p->pc6 = 0;
12580f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1259c98d5d94SLen Brown 		p->pc7 = 0;
1260ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1261ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1262ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1263889facbeSLen Brown 
1264889facbeSLen Brown 	p->energy_pkg = 0;
1265889facbeSLen Brown 	p->energy_dram = 0;
1266889facbeSLen Brown 	p->energy_cores = 0;
1267889facbeSLen Brown 	p->energy_gfx = 0;
1268889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1269889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1270889facbeSLen Brown 	p->pkg_temp_c = 0;
127127d47356SLen Brown 
1272fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
127327d47356SLen Brown 	p->gfx_mhz = 0;
1274388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1275388e9c81SLen Brown 		t->counter[i] = 0;
1276388e9c81SLen Brown 
1277388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1278388e9c81SLen Brown 		c->counter[i] = 0;
1279388e9c81SLen Brown 
1280388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1281388e9c81SLen Brown 		p->counter[i] = 0;
1282103a8feaSLen Brown }
1283c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1284c98d5d94SLen Brown 	struct pkg_data *p)
1285103a8feaSLen Brown {
1286388e9c81SLen Brown 	int i;
1287388e9c81SLen Brown 	struct msr_counter *mp;
1288388e9c81SLen Brown 
1289c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1290c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1291c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1292c98d5d94SLen Brown 	average.threads.c1 += t->c1;
129315aaa346SLen Brown 
1294562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1295562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1296562a2d37SLen Brown 
1297388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1298388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1299388e9c81SLen Brown 			continue;
1300388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1301388e9c81SLen Brown 	}
1302388e9c81SLen Brown 
1303c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1304c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1305c98d5d94SLen Brown 		return 0;
1306c98d5d94SLen Brown 
1307c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1308c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1309c98d5d94SLen Brown 	average.cores.c7 += c->c7;
13100539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1311c98d5d94SLen Brown 
1312889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1313889facbeSLen Brown 
1314388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1315388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1316388e9c81SLen Brown 			continue;
1317388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1318388e9c81SLen Brown 	}
1319388e9c81SLen Brown 
1320c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1321c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1322c98d5d94SLen Brown 		return 0;
1323c98d5d94SLen Brown 
1324a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
13250b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1326a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
13270b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1328a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
13290b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1330a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
13310b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
13320b2bb692SLen Brown 
1333c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
13340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1335c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
13360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1337c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
13380f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1339c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1340ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1341ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1342ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1343c98d5d94SLen Brown 
1344889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1345889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1346889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1347889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1348889facbeSLen Brown 
1349fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
135027d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
135127d47356SLen Brown 
1352889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1353889facbeSLen Brown 
1354889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1355889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1356388e9c81SLen Brown 
1357388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1358388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1359388e9c81SLen Brown 			continue;
1360388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1361388e9c81SLen Brown 	}
1362c98d5d94SLen Brown 	return 0;
1363c98d5d94SLen Brown }
1364c98d5d94SLen Brown /*
1365c98d5d94SLen Brown  * sum the counters for all cpus in the system
1366c98d5d94SLen Brown  * compute the weighted average
1367c98d5d94SLen Brown  */
1368c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1369c98d5d94SLen Brown 	struct pkg_data *p)
1370c98d5d94SLen Brown {
1371388e9c81SLen Brown 	int i;
1372388e9c81SLen Brown 	struct msr_counter *mp;
1373388e9c81SLen Brown 
1374c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1375c98d5d94SLen Brown 
1376c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1377c98d5d94SLen Brown 
1378c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1379c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1380c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1381c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1382c98d5d94SLen Brown 
13830de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
13840de6c0dfSLen Brown 		sums_need_wide_columns = 1;
13850de6c0dfSLen Brown 
1386c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1387c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1388c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
13890539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1390c98d5d94SLen Brown 
1391a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
13920b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1393a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
13940b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1395a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
13960b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1397a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
13980b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
13990b2bb692SLen Brown 
1400c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
14010f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1402c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
14030f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1404c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
14050f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1406c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1407ca58710fSKristen Carlson Accardi 
1408ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1409ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1410ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1411388e9c81SLen Brown 
1412388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1413388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1414388e9c81SLen Brown 			continue;
14150de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14160de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
14170de6c0dfSLen Brown 				sums_need_wide_columns = 1;
141841618e63SLen Brown 			continue;
14190de6c0dfSLen Brown 		}
1420388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1421388e9c81SLen Brown 	}
1422388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1423388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1424388e9c81SLen Brown 			continue;
14250de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14260de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
14270de6c0dfSLen Brown 				sums_need_wide_columns = 1;
14280de6c0dfSLen Brown 		}
1429388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1430388e9c81SLen Brown 	}
1431388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1432388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1433388e9c81SLen Brown 			continue;
14340de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
14350de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
14360de6c0dfSLen Brown 				sums_need_wide_columns = 1;
14370de6c0dfSLen Brown 		}
1438388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1439388e9c81SLen Brown 	}
1440c98d5d94SLen Brown }
1441c98d5d94SLen Brown 
1442c98d5d94SLen Brown static unsigned long long rdtsc(void)
1443c98d5d94SLen Brown {
1444c98d5d94SLen Brown 	unsigned int low, high;
1445c98d5d94SLen Brown 
1446c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1447c98d5d94SLen Brown 
1448c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1449c98d5d94SLen Brown }
1450c98d5d94SLen Brown 
1451c98d5d94SLen Brown /*
1452495c7654SLen Brown  * Open a file, and exit on failure
1453495c7654SLen Brown  */
1454495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1455495c7654SLen Brown {
1456495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1457495c7654SLen Brown 
1458495c7654SLen Brown 	if (!filep)
1459495c7654SLen Brown 		err(1, "%s: open failed", path);
1460495c7654SLen Brown 	return filep;
1461495c7654SLen Brown }
1462495c7654SLen Brown /*
1463495c7654SLen Brown  * snapshot_sysfs_counter()
1464495c7654SLen Brown  *
1465495c7654SLen Brown  * return snapshot of given counter
1466495c7654SLen Brown  */
1467495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1468495c7654SLen Brown {
1469495c7654SLen Brown 	FILE *fp;
1470495c7654SLen Brown 	int retval;
1471495c7654SLen Brown 	unsigned long long counter;
1472495c7654SLen Brown 
1473495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1474495c7654SLen Brown 
1475495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1476495c7654SLen Brown 	if (retval != 1)
1477495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1478495c7654SLen Brown 
1479495c7654SLen Brown 	fclose(fp);
1480495c7654SLen Brown 
1481495c7654SLen Brown 	return counter;
1482495c7654SLen Brown }
1483495c7654SLen Brown 
1484495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1485495c7654SLen Brown {
1486495c7654SLen Brown 	if (mp->msr_num != 0) {
1487495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1488495c7654SLen Brown 			return -1;
1489495c7654SLen Brown 	} else {
149041618e63SLen Brown 		char path[128];
149141618e63SLen Brown 
149241618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
149341618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
149441618e63SLen Brown 				 cpu, mp->path);
149541618e63SLen Brown 
149641618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
149741618e63SLen Brown 		} else {
1498495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1499495c7654SLen Brown 		}
150041618e63SLen Brown 	}
1501495c7654SLen Brown 
1502495c7654SLen Brown 	return 0;
1503495c7654SLen Brown }
1504495c7654SLen Brown 
1505495c7654SLen Brown /*
1506c98d5d94SLen Brown  * get_counters(...)
1507c98d5d94SLen Brown  * migrate to cpu
1508c98d5d94SLen Brown  * acquire and record local counters for that cpu
1509c98d5d94SLen Brown  */
1510c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1511c98d5d94SLen Brown {
1512c98d5d94SLen Brown 	int cpu = t->cpu_id;
1513889facbeSLen Brown 	unsigned long long msr;
15140102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1515388e9c81SLen Brown 	struct msr_counter *mp;
1516388e9c81SLen Brown 	int i;
1517c98d5d94SLen Brown 
1518*f4fdf2b4SLen Brown 
1519*f4fdf2b4SLen Brown 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1520*f4fdf2b4SLen Brown 
1521e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1522b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
152315aaa346SLen Brown 		return -1;
1524e52966c0SLen Brown 	}
152515aaa346SLen Brown 
15260102b067SLen Brown retry:
1527c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
152815aaa346SLen Brown 
1529812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
15300102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
15310102b067SLen Brown 
15320102b067SLen Brown 		/*
15330102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
15340102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
15350102b067SLen Brown 		 *
15360102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
15370102b067SLen Brown 		 * individual system call, so delays may occur
15380102b067SLen Brown 		 * between them.  If the time to read them
15390102b067SLen Brown 		 * varies by a large amount, we re-read them.
15400102b067SLen Brown 		 */
15410102b067SLen Brown 
15420102b067SLen Brown 		/*
15430102b067SLen Brown 		 * This initial dummy APERF read has been seen to
15440102b067SLen Brown 		 * reduce jitter in the subsequent reads.
15450102b067SLen Brown 		 */
15460102b067SLen Brown 
15479c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1548c98d5d94SLen Brown 			return -3;
15490102b067SLen Brown 
15500102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
15510102b067SLen Brown 
15520102b067SLen Brown 		tsc_before = t->tsc;
15530102b067SLen Brown 
15540102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
15550102b067SLen Brown 			return -3;
15560102b067SLen Brown 
15570102b067SLen Brown 		tsc_between = rdtsc();
15580102b067SLen Brown 
15599c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1560c98d5d94SLen Brown 			return -4;
15610102b067SLen Brown 
15620102b067SLen Brown 		tsc_after = rdtsc();
15630102b067SLen Brown 
15640102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
15650102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
15660102b067SLen Brown 
15670102b067SLen Brown 		/*
15680102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
15690102b067SLen Brown 		 * differ by more than 2x, then try again.
15700102b067SLen Brown 		 */
15710102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
15720102b067SLen Brown 			aperf_mperf_retry_count++;
15730102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
15740102b067SLen Brown 				goto retry;
15750102b067SLen Brown 			else
15760102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
15770102b067SLen Brown 					cpu, aperf_time, mperf_time);
15780102b067SLen Brown 		}
15790102b067SLen Brown 		aperf_mperf_retry_count = 0;
15800102b067SLen Brown 
1581b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1582b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
158388c3281fSLen Brown 	}
158488c3281fSLen Brown 
1585812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1586562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1587812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
15881ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
15891ed51011SLen Brown 			return -5;
15901ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
15911ed51011SLen Brown 	}
15920539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1593144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1594144b44b1SLen Brown 			return -6;
1595144b44b1SLen Brown 	}
1596144b44b1SLen Brown 
1597388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1598495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1599388e9c81SLen Brown 			return -10;
1600388e9c81SLen Brown 	}
1601388e9c81SLen Brown 
1602c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1603c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1604*f4fdf2b4SLen Brown 		goto done;
1605c98d5d94SLen Brown 
1606812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
1607c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1608c98d5d94SLen Brown 			return -6;
1609144b44b1SLen Brown 	}
1610144b44b1SLen Brown 
1611812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1612c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1613c98d5d94SLen Brown 			return -7;
1614fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1615fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1616fb5d4327SDasaratharaman Chandramouli 			return -7;
1617103a8feaSLen Brown 	}
161815aaa346SLen Brown 
1619812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1620c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1621c98d5d94SLen Brown 			return -8;
1622c98d5d94SLen Brown 
16230539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
16240539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
16250539ba11SLen Brown 			return -8;
16260539ba11SLen Brown 
1627812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1628889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1629889facbeSLen Brown 			return -9;
1630889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1631889facbeSLen Brown 	}
1632889facbeSLen Brown 
1633388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1634495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1635388e9c81SLen Brown 			return -10;
1636388e9c81SLen Brown 	}
1637889facbeSLen Brown 
1638c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1639c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1640*f4fdf2b4SLen Brown 		goto done;
164115aaa346SLen Brown 
1642a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
16430b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
16440b2bb692SLen Brown 			return -10;
1645a99d8730SLen Brown 	}
1646a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
16470b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
16480b2bb692SLen Brown 			return -11;
1649a99d8730SLen Brown 	}
1650a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
16510b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
16520b2bb692SLen Brown 			return -12;
1653a99d8730SLen Brown 	}
1654a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
16550b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
16560b2bb692SLen Brown 			return -13;
16570b2bb692SLen Brown 	}
16580f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1659c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1660c98d5d94SLen Brown 			return -9;
16610f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
16620539ba11SLen Brown 		if (do_slm_cstates) {
16630539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
16640539ba11SLen Brown 				return -10;
16650539ba11SLen Brown 		} else {
1666c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1667c98d5d94SLen Brown 				return -10;
16680539ba11SLen Brown 		}
16690539ba11SLen Brown 	}
16700539ba11SLen Brown 
16710f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1672c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1673c98d5d94SLen Brown 			return -11;
16740f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1675c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1676c98d5d94SLen Brown 			return -12;
16770f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1678ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1679ca58710fSKristen Carlson Accardi 			return -13;
16800f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1681ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1682ca58710fSKristen Carlson Accardi 			return -13;
16830f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1684ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1685ca58710fSKristen Carlson Accardi 			return -13;
16860f47c08dSLen Brown 
1687889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1688889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1689889facbeSLen Brown 			return -13;
1690889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1691889facbeSLen Brown 	}
16929148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1693889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1694889facbeSLen Brown 			return -14;
1695889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1696889facbeSLen Brown 	}
1697889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1698889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1699889facbeSLen Brown 			return -15;
1700889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1701889facbeSLen Brown 	}
1702889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1703889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1704889facbeSLen Brown 			return -16;
1705889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1706889facbeSLen Brown 	}
1707889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1708889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1709889facbeSLen Brown 			return -16;
1710889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1711889facbeSLen Brown 	}
1712889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1713889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1714889facbeSLen Brown 			return -16;
1715889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1716889facbeSLen Brown 	}
1717812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1718889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1719889facbeSLen Brown 			return -17;
1720889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1721889facbeSLen Brown 	}
1722fdf676e5SLen Brown 
1723812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1724fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1725fdf676e5SLen Brown 
1726812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
172727d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
172827d47356SLen Brown 
1729388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1730495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
1731388e9c81SLen Brown 			return -10;
1732388e9c81SLen Brown 	}
1733*f4fdf2b4SLen Brown done:
1734*f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
1735388e9c81SLen Brown 
173615aaa346SLen Brown 	return 0;
1737103a8feaSLen Brown }
1738103a8feaSLen Brown 
1739ee7e38e3SLen Brown /*
1740ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1741ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1742ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1743ee7e38e3SLen Brown  */
1744ee7e38e3SLen Brown 
1745ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1746ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1747ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1748ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1749ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1750ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1751ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1752ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1753ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1754ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1755ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1756ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
17570b2bb692SLen Brown #define PCL__8 12 /* PC8 */
17580b2bb692SLen Brown #define PCL__9 13 /* PC9 */
17590b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1760ee7e38e3SLen Brown 
1761ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1762ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
17630b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1764ee7e38e3SLen Brown 
1765e9257f5fSLen 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};
1766e9257f5fSLen 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};
1767e9257f5fSLen 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};
17680539ba11SLen 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};
1769f2642888SLen 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};
1770e9257f5fSLen 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};
1771e4085d54SLen 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};
1772d8ebb442SLen 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};
1773ee7e38e3SLen Brown 
1774a2b7b749SLen Brown 
1775a2b7b749SLen Brown static void
1776a2b7b749SLen Brown calculate_tsc_tweak()
1777a2b7b749SLen Brown {
1778a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1779a2b7b749SLen Brown }
1780a2b7b749SLen Brown 
1781fcd17211SLen Brown static void
1782fcd17211SLen Brown dump_nhm_platform_info(void)
1783103a8feaSLen Brown {
1784103a8feaSLen Brown 	unsigned long long msr;
1785103a8feaSLen Brown 	unsigned int ratio;
1786103a8feaSLen Brown 
1787ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1788103a8feaSLen Brown 
1789b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
17906574a5d5SLen Brown 
1791103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1792710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
1793103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1794103a8feaSLen Brown 
1795103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1796710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1797103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1798103a8feaSLen Brown 
17997ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1800b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1801bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
180267920418SLen Brown 
1803fcd17211SLen Brown 	return;
1804fcd17211SLen Brown }
1805fcd17211SLen Brown 
1806fcd17211SLen Brown static void
1807fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1808fcd17211SLen Brown {
1809fcd17211SLen Brown 	unsigned long long msr;
1810fcd17211SLen Brown 	unsigned int ratio;
1811fcd17211SLen Brown 
18127ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1813fcd17211SLen Brown 
1814b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1815fcd17211SLen Brown 
1816fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1817fcd17211SLen Brown 	if (ratio)
1818710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
1819fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1820fcd17211SLen Brown 
1821fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1822fcd17211SLen Brown 	if (ratio)
1823710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
1824fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1825fcd17211SLen Brown 	return;
1826fcd17211SLen Brown }
1827fcd17211SLen Brown 
1828fcd17211SLen Brown static void
1829fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1830fcd17211SLen Brown {
1831fcd17211SLen Brown 	unsigned long long msr;
1832fcd17211SLen Brown 	unsigned int ratio;
18336574a5d5SLen Brown 
18347ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
18356574a5d5SLen Brown 
1836b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
18376574a5d5SLen Brown 
18386574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
18396574a5d5SLen Brown 	if (ratio)
1840710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
18416574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18426574a5d5SLen Brown 
18436574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
18446574a5d5SLen Brown 	if (ratio)
1845710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
18466574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18476574a5d5SLen Brown 
18486574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
18496574a5d5SLen Brown 	if (ratio)
1850710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
18516574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18526574a5d5SLen Brown 
18536574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
18546574a5d5SLen Brown 	if (ratio)
1855710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
18566574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18576574a5d5SLen Brown 
18586574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
18596574a5d5SLen Brown 	if (ratio)
1860710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
18616574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18626574a5d5SLen Brown 
18636574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
18646574a5d5SLen Brown 	if (ratio)
1865710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
18666574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18676574a5d5SLen Brown 
18686574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
18696574a5d5SLen Brown 	if (ratio)
1870710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
18716574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
18726574a5d5SLen Brown 
18736574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
18746574a5d5SLen Brown 	if (ratio)
1875710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
18766574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1877103a8feaSLen Brown 	return;
1878fcd17211SLen Brown }
187931e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
188031e07522SLen Brown {
188131e07522SLen Brown 
188231e07522SLen Brown 	if (!genuine_intel)
188331e07522SLen Brown 		return 0;
188431e07522SLen Brown 
188531e07522SLen Brown 	switch (model) {
188631e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
188731e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
188831e07522SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
188931e07522SLen Brown 		return 1;
189031e07522SLen Brown 	}
189131e07522SLen Brown 	return 0;
189231e07522SLen Brown }
1893fcd17211SLen Brown 
1894fcd17211SLen Brown static void
189531e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
1896fcd17211SLen Brown {
189731e07522SLen Brown 	unsigned long long msr, core_counts;
189831e07522SLen Brown 	unsigned int ratio, group_size;
1899103a8feaSLen Brown 
19007ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1901b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
19026574a5d5SLen Brown 
190331e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
190431e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
190531e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
190631e07522SLen Brown 	} else {
190731e07522SLen Brown 		core_counts = 0x0807060504030201;
190831e07522SLen Brown 	}
190931e07522SLen Brown 
19106574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
191131e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
19126574a5d5SLen Brown 	if (ratio)
191331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
191431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19156574a5d5SLen Brown 
19166574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
191731e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
19186574a5d5SLen Brown 	if (ratio)
191931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
192031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19216574a5d5SLen Brown 
19226574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
192331e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
19246574a5d5SLen Brown 	if (ratio)
192531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
192631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19276574a5d5SLen Brown 
19286574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
192931e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
19306574a5d5SLen Brown 	if (ratio)
193131e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
193231e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
19336574a5d5SLen Brown 
1934103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
193531e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
1936103a8feaSLen Brown 	if (ratio)
193731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
193831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1939103a8feaSLen Brown 
1940103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
194131e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
1942103a8feaSLen Brown 	if (ratio)
194331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
194431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1945103a8feaSLen Brown 
1946103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
194731e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
1948103a8feaSLen Brown 	if (ratio)
194931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
195031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1951103a8feaSLen Brown 
1952103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
195331e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
1954103a8feaSLen Brown 	if (ratio)
195531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
195631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1957fcd17211SLen Brown 	return;
1958fcd17211SLen Brown }
19593a9a941dSLen Brown 
1960fcd17211SLen Brown static void
19610f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
19620f7887c4SLen Brown {
19630f7887c4SLen Brown 	unsigned long long msr;
19640f7887c4SLen Brown 	unsigned int ratio;
19650f7887c4SLen Brown 
19660f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
19670f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
19680f7887c4SLen Brown 
19690f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
19700f7887c4SLen Brown 	if (ratio)
19710f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
19720f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19730f7887c4SLen Brown 
19740f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
19750f7887c4SLen Brown 	if (ratio)
19760f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
19770f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19780f7887c4SLen Brown 
19790f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
19800f7887c4SLen Brown 	if (ratio)
19810f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
19820f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19830f7887c4SLen Brown 
19840f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
19850f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
19860f7887c4SLen Brown 
19870f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
19880f7887c4SLen Brown 	if (ratio)
19890f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
19900f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19910f7887c4SLen Brown 
19920f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
19930f7887c4SLen Brown 	if (ratio)
19940f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
19950f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
19960f7887c4SLen Brown 
19970f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
19980f7887c4SLen Brown 	if (ratio)
19990f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
20000f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20010f7887c4SLen Brown 
20020f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
20030f7887c4SLen Brown 	if (ratio)
20040f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
20050f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
20060f7887c4SLen Brown }
20070f7887c4SLen Brown 
20080f7887c4SLen Brown static void
2009fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2010fb5d4327SDasaratharaman Chandramouli {
2011cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2012cbf97abaSHubert Chrzaniuk 
2013fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2014cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2015cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2016cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2017cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2018fb5d4327SDasaratharaman Chandramouli 
2019ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2020fb5d4327SDasaratharaman Chandramouli 
2021b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2022bfae2052SLen Brown 		base_cpu, msr);
2023fb5d4327SDasaratharaman Chandramouli 
2024fb5d4327SDasaratharaman Chandramouli 	/**
2025fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2026cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2027cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2028fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2029fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2030fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2031fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2032fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2033fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2034fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2035fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2036fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2037fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2038fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2039fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2040fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2041fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2042fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2043fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2044fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2045fb5d4327SDasaratharaman Chandramouli 	 */
2046fb5d4327SDasaratharaman Chandramouli 
2047cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2048cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2049cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2050cbf97abaSHubert Chrzaniuk 
2051cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2052fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2053cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2054fb5d4327SDasaratharaman Chandramouli 
2055cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2056cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2057cbf97abaSHubert Chrzaniuk 		b_nr++;
2058fb5d4327SDasaratharaman Chandramouli 	}
2059cbf97abaSHubert Chrzaniuk 
2060cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2061cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2062b7d8c148SLen Brown 			fprintf(outf,
2063710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2064cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2065fb5d4327SDasaratharaman Chandramouli }
2066fb5d4327SDasaratharaman Chandramouli 
2067fb5d4327SDasaratharaman Chandramouli static void
2068fcd17211SLen Brown dump_nhm_cst_cfg(void)
2069fcd17211SLen Brown {
2070fcd17211SLen Brown 	unsigned long long msr;
2071fcd17211SLen Brown 
20721df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2073fcd17211SLen Brown 
2074fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
2075fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
2076fcd17211SLen Brown 
20771df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2078fcd17211SLen Brown 
2079b7d8c148SLen Brown 	fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
2080fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2081fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2082fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2083fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2084fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
20856c34f160SLen Brown 		(unsigned int)msr & 0xF,
2086fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2087fcd17211SLen Brown 	return;
2088103a8feaSLen Brown }
2089103a8feaSLen Brown 
20906fb3143bSLen Brown static void
20916fb3143bSLen Brown dump_config_tdp(void)
20926fb3143bSLen Brown {
20936fb3143bSLen Brown 	unsigned long long msr;
20946fb3143bSLen Brown 
20956fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2096b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2097685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
20986fb3143bSLen Brown 
20996fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2100b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
21016fb3143bSLen Brown 	if (msr) {
2102685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2103685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2104685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2105685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
21066fb3143bSLen Brown 	}
2107b7d8c148SLen Brown 	fprintf(outf, ")\n");
21086fb3143bSLen Brown 
21096fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2110b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
21116fb3143bSLen Brown 	if (msr) {
2112685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2113685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2114685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2115685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
21166fb3143bSLen Brown 	}
2117b7d8c148SLen Brown 	fprintf(outf, ")\n");
21186fb3143bSLen Brown 
21196fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2120b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
21216fb3143bSLen Brown 	if ((msr) & 0x3)
2122b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2123b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2124b7d8c148SLen Brown 	fprintf(outf, ")\n");
21256fb3143bSLen Brown 
21266fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2127b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2128685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2129b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2130b7d8c148SLen Brown 	fprintf(outf, ")\n");
21316fb3143bSLen Brown }
21325a63426eSLen Brown 
21335a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
21345a63426eSLen Brown 
21355a63426eSLen Brown void print_irtl(void)
21365a63426eSLen Brown {
21375a63426eSLen Brown 	unsigned long long msr;
21385a63426eSLen Brown 
21395a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
21405a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
21415a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21425a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21435a63426eSLen Brown 
21445a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
21455a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
21465a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21475a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21485a63426eSLen Brown 
21495a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
21505a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
21515a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21525a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21535a63426eSLen Brown 
21545a63426eSLen Brown 	if (!do_irtl_hsw)
21555a63426eSLen Brown 		return;
21565a63426eSLen Brown 
21575a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
21585a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
21595a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21605a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21615a63426eSLen Brown 
21625a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
21635a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
21645a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21655a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21665a63426eSLen Brown 
21675a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
21685a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
21695a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
21705a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
21715a63426eSLen Brown 
21725a63426eSLen Brown }
217336229897SLen Brown void free_fd_percpu(void)
217436229897SLen Brown {
217536229897SLen Brown 	int i;
217636229897SLen Brown 
217701a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
217836229897SLen Brown 		if (fd_percpu[i] != 0)
217936229897SLen Brown 			close(fd_percpu[i]);
218036229897SLen Brown 	}
218136229897SLen Brown 
218236229897SLen Brown 	free(fd_percpu);
21836fb3143bSLen Brown }
21846fb3143bSLen Brown 
2185c98d5d94SLen Brown void free_all_buffers(void)
2186103a8feaSLen Brown {
2187c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2188c98d5d94SLen Brown 	cpu_present_set = NULL;
218936229897SLen Brown 	cpu_present_setsize = 0;
2190103a8feaSLen Brown 
2191c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2192c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2193c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2194103a8feaSLen Brown 
2195c98d5d94SLen Brown 	free(thread_even);
2196c98d5d94SLen Brown 	free(core_even);
2197c98d5d94SLen Brown 	free(package_even);
2198103a8feaSLen Brown 
2199c98d5d94SLen Brown 	thread_even = NULL;
2200c98d5d94SLen Brown 	core_even = NULL;
2201c98d5d94SLen Brown 	package_even = NULL;
2202103a8feaSLen Brown 
2203c98d5d94SLen Brown 	free(thread_odd);
2204c98d5d94SLen Brown 	free(core_odd);
2205c98d5d94SLen Brown 	free(package_odd);
2206103a8feaSLen Brown 
2207c98d5d94SLen Brown 	thread_odd = NULL;
2208c98d5d94SLen Brown 	core_odd = NULL;
2209c98d5d94SLen Brown 	package_odd = NULL;
2210103a8feaSLen Brown 
2211c98d5d94SLen Brown 	free(output_buffer);
2212c98d5d94SLen Brown 	output_buffer = NULL;
2213c98d5d94SLen Brown 	outp = NULL;
221436229897SLen Brown 
221536229897SLen Brown 	free_fd_percpu();
2216562a2d37SLen Brown 
2217562a2d37SLen Brown 	free(irq_column_2_cpu);
2218562a2d37SLen Brown 	free(irqs_per_cpu);
2219103a8feaSLen Brown }
2220103a8feaSLen Brown 
222157a42a34SJosh Triplett 
222257a42a34SJosh Triplett /*
222395aebc44SJosh Triplett  * Parse a file containing a single int.
222495aebc44SJosh Triplett  */
222595aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
222695aebc44SJosh Triplett {
222795aebc44SJosh Triplett 	va_list args;
222895aebc44SJosh Triplett 	char path[PATH_MAX];
222995aebc44SJosh Triplett 	FILE *filep;
223095aebc44SJosh Triplett 	int value;
223195aebc44SJosh Triplett 
223295aebc44SJosh Triplett 	va_start(args, fmt);
223395aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
223495aebc44SJosh Triplett 	va_end(args);
223557a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2236b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2237b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
223895aebc44SJosh Triplett 	fclose(filep);
223995aebc44SJosh Triplett 	return value;
224095aebc44SJosh Triplett }
224195aebc44SJosh Triplett 
224295aebc44SJosh Triplett /*
2243e275b388SDasaratharaman Chandramouli  * get_cpu_position_in_core(cpu)
2244e275b388SDasaratharaman Chandramouli  * return the position of the CPU among its HT siblings in the core
2245e275b388SDasaratharaman Chandramouli  * return -1 if the sibling is not in list
2246103a8feaSLen Brown  */
2247e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu)
2248103a8feaSLen Brown {
2249e275b388SDasaratharaman Chandramouli 	char path[64];
2250e275b388SDasaratharaman Chandramouli 	FILE *filep;
2251e275b388SDasaratharaman Chandramouli 	int this_cpu;
2252e275b388SDasaratharaman Chandramouli 	char character;
2253e275b388SDasaratharaman Chandramouli 	int i;
2254e275b388SDasaratharaman Chandramouli 
2255e275b388SDasaratharaman Chandramouli 	sprintf(path,
2256e275b388SDasaratharaman Chandramouli 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2257e275b388SDasaratharaman Chandramouli 		cpu);
2258e275b388SDasaratharaman Chandramouli 	filep = fopen(path, "r");
2259e275b388SDasaratharaman Chandramouli 	if (filep == NULL) {
2260e275b388SDasaratharaman Chandramouli 		perror(path);
2261e275b388SDasaratharaman Chandramouli 		exit(1);
2262e275b388SDasaratharaman Chandramouli 	}
2263e275b388SDasaratharaman Chandramouli 
2264e275b388SDasaratharaman Chandramouli 	for (i = 0; i < topo.num_threads_per_core; i++) {
2265e275b388SDasaratharaman Chandramouli 		fscanf(filep, "%d", &this_cpu);
2266e275b388SDasaratharaman Chandramouli 		if (this_cpu == cpu) {
2267e275b388SDasaratharaman Chandramouli 			fclose(filep);
2268e275b388SDasaratharaman Chandramouli 			return i;
2269e275b388SDasaratharaman Chandramouli 		}
2270e275b388SDasaratharaman Chandramouli 
2271e275b388SDasaratharaman Chandramouli 		/* Account for no separator after last thread*/
2272e275b388SDasaratharaman Chandramouli 		if (i != (topo.num_threads_per_core - 1))
2273e275b388SDasaratharaman Chandramouli 			fscanf(filep, "%c", &character);
2274e275b388SDasaratharaman Chandramouli 	}
2275e275b388SDasaratharaman Chandramouli 
2276e275b388SDasaratharaman Chandramouli 	fclose(filep);
2277e275b388SDasaratharaman Chandramouli 	return -1;
2278c98d5d94SLen Brown }
2279103a8feaSLen Brown 
2280c98d5d94SLen Brown /*
2281c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2282c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2283c98d5d94SLen Brown  */
2284c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2285c98d5d94SLen Brown {
228695aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2287103a8feaSLen Brown }
2288103a8feaSLen Brown 
2289103a8feaSLen Brown int get_physical_package_id(int cpu)
2290103a8feaSLen Brown {
229195aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2292103a8feaSLen Brown }
2293103a8feaSLen Brown 
2294103a8feaSLen Brown int get_core_id(int cpu)
2295103a8feaSLen Brown {
229695aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2297103a8feaSLen Brown }
2298103a8feaSLen Brown 
2299c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
2300c98d5d94SLen Brown {
2301c98d5d94SLen Brown 	char path[80];
2302c98d5d94SLen Brown 	FILE *filep;
2303e275b388SDasaratharaman Chandramouli 	int sib1;
2304e275b388SDasaratharaman Chandramouli 	int matches = 0;
2305c98d5d94SLen Brown 	char character;
2306e275b388SDasaratharaman Chandramouli 	char str[100];
2307e275b388SDasaratharaman Chandramouli 	char *ch;
2308c98d5d94SLen Brown 
2309c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
231057a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2311e275b388SDasaratharaman Chandramouli 
2312103a8feaSLen Brown 	/*
2313c98d5d94SLen Brown 	 * file format:
2314e275b388SDasaratharaman Chandramouli 	 * A ',' separated or '-' separated set of numbers
2315e275b388SDasaratharaman Chandramouli 	 * (eg 1-2 or 1,3,4,5)
2316c98d5d94SLen Brown 	 */
2317e275b388SDasaratharaman Chandramouli 	fscanf(filep, "%d%c\n", &sib1, &character);
2318e275b388SDasaratharaman Chandramouli 	fseek(filep, 0, SEEK_SET);
2319e275b388SDasaratharaman Chandramouli 	fgets(str, 100, filep);
2320e275b388SDasaratharaman Chandramouli 	ch = strchr(str, character);
2321e275b388SDasaratharaman Chandramouli 	while (ch != NULL) {
2322e275b388SDasaratharaman Chandramouli 		matches++;
2323e275b388SDasaratharaman Chandramouli 		ch = strchr(ch+1, character);
2324e275b388SDasaratharaman Chandramouli 	}
2325c98d5d94SLen Brown 
2326c98d5d94SLen Brown 	fclose(filep);
2327e275b388SDasaratharaman Chandramouli 	return matches+1;
2328c98d5d94SLen Brown }
2329c98d5d94SLen Brown 
2330c98d5d94SLen Brown /*
2331c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2332c98d5d94SLen Brown  * skip non-present cpus
2333103a8feaSLen Brown  */
2334103a8feaSLen Brown 
2335c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2336c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2337c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2338c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2339c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2340c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2341c98d5d94SLen Brown {
2342c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
2343c98d5d94SLen Brown 
2344c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2345c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2346c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
2347c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
2348c98d5d94SLen Brown 				struct thread_data *t, *t2;
2349c98d5d94SLen Brown 				struct core_data *c, *c2;
2350c98d5d94SLen Brown 				struct pkg_data *p, *p2;
2351c98d5d94SLen Brown 
2352c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2353c98d5d94SLen Brown 
2354c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
2355c98d5d94SLen Brown 					continue;
2356c98d5d94SLen Brown 
2357c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2358c98d5d94SLen Brown 
2359c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
2360c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
2361c98d5d94SLen Brown 
2362c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
2363c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
2364c98d5d94SLen Brown 
2365c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
2366c98d5d94SLen Brown 				if (retval)
2367c98d5d94SLen Brown 					return retval;
2368c98d5d94SLen Brown 			}
2369c98d5d94SLen Brown 		}
2370c98d5d94SLen Brown 	}
2371c98d5d94SLen Brown 	return 0;
2372c98d5d94SLen Brown }
2373c98d5d94SLen Brown 
2374c98d5d94SLen Brown /*
2375c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2376c98d5d94SLen Brown  * return max_cpu number
2377c98d5d94SLen Brown  */
2378c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2379103a8feaSLen Brown {
2380103a8feaSLen Brown 	FILE *fp;
2381c98d5d94SLen Brown 	int cpu_num;
2382103a8feaSLen Brown 	int retval;
2383103a8feaSLen Brown 
238457a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2385103a8feaSLen Brown 
2386103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2387b2c95d90SJosh Triplett 	if (retval != 0)
2388b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2389103a8feaSLen Brown 
2390c98d5d94SLen Brown 	while (1) {
2391c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2392103a8feaSLen Brown 		if (retval != 1)
2393103a8feaSLen Brown 			break;
2394103a8feaSLen Brown 
2395c98d5d94SLen Brown 		retval = func(cpu_num);
2396c98d5d94SLen Brown 		if (retval) {
2397c98d5d94SLen Brown 			fclose(fp);
2398c98d5d94SLen Brown 			return(retval);
2399c98d5d94SLen Brown 		}
2400103a8feaSLen Brown 	}
2401103a8feaSLen Brown 	fclose(fp);
2402c98d5d94SLen Brown 	return 0;
2403103a8feaSLen Brown }
2404103a8feaSLen Brown 
2405103a8feaSLen Brown void re_initialize(void)
2406103a8feaSLen Brown {
2407c98d5d94SLen Brown 	free_all_buffers();
2408c98d5d94SLen Brown 	setup_all_buffers();
2409c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2410103a8feaSLen Brown }
2411103a8feaSLen Brown 
2412c98d5d94SLen Brown 
2413103a8feaSLen Brown /*
2414c98d5d94SLen Brown  * count_cpus()
2415c98d5d94SLen Brown  * remember the last one seen, it will be the max
2416103a8feaSLen Brown  */
2417c98d5d94SLen Brown int count_cpus(int cpu)
2418103a8feaSLen Brown {
2419c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
2420c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
2421103a8feaSLen Brown 
2422c98d5d94SLen Brown 	topo.num_cpus += 1;
2423c98d5d94SLen Brown 	return 0;
2424103a8feaSLen Brown }
2425c98d5d94SLen Brown int mark_cpu_present(int cpu)
2426c98d5d94SLen Brown {
2427c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
242815aaa346SLen Brown 	return 0;
2429103a8feaSLen Brown }
2430103a8feaSLen Brown 
2431562a2d37SLen Brown /*
2432562a2d37SLen Brown  * snapshot_proc_interrupts()
2433562a2d37SLen Brown  *
2434562a2d37SLen Brown  * read and record summary of /proc/interrupts
2435562a2d37SLen Brown  *
2436562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2437562a2d37SLen Brown  */
2438562a2d37SLen Brown int snapshot_proc_interrupts(void)
2439562a2d37SLen Brown {
2440562a2d37SLen Brown 	static FILE *fp;
2441562a2d37SLen Brown 	int column, retval;
2442562a2d37SLen Brown 
2443562a2d37SLen Brown 	if (fp == NULL)
2444562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2445562a2d37SLen Brown 	else
2446562a2d37SLen Brown 		rewind(fp);
2447562a2d37SLen Brown 
2448562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2449562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2450562a2d37SLen Brown 		int cpu_number;
2451562a2d37SLen Brown 
2452562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2453562a2d37SLen Brown 		if (retval != 1)
2454562a2d37SLen Brown 			break;
2455562a2d37SLen Brown 
2456562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2457562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2458562a2d37SLen Brown 			return 1;
2459562a2d37SLen Brown 		}
2460562a2d37SLen Brown 
2461562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2462562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2463562a2d37SLen Brown 	}
2464562a2d37SLen Brown 
2465562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2466562a2d37SLen Brown 	while (1) {
2467562a2d37SLen Brown 		int column;
2468562a2d37SLen Brown 		char buf[64];
2469562a2d37SLen Brown 
2470562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2471562a2d37SLen Brown 		if (retval != 1)
2472562a2d37SLen Brown 			break;
2473562a2d37SLen Brown 
2474562a2d37SLen Brown 		/* read the count per cpu */
2475562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2476562a2d37SLen Brown 
2477562a2d37SLen Brown 			int cpu_number, irq_count;
2478562a2d37SLen Brown 
2479562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2480562a2d37SLen Brown 			if (retval != 1)
2481562a2d37SLen Brown 				break;
2482562a2d37SLen Brown 
2483562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2484562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2485562a2d37SLen Brown 
2486562a2d37SLen Brown 		}
2487562a2d37SLen Brown 
2488562a2d37SLen Brown 		while (getc(fp) != '\n')
2489562a2d37SLen Brown 			;	/* flush interrupt description */
2490562a2d37SLen Brown 
2491562a2d37SLen Brown 	}
2492562a2d37SLen Brown 	return 0;
2493562a2d37SLen Brown }
249427d47356SLen Brown /*
2495fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2496fdf676e5SLen Brown  *
2497fdf676e5SLen Brown  * record snapshot of
2498fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2499fdf676e5SLen Brown  *
2500fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2501fdf676e5SLen Brown  */
2502fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2503fdf676e5SLen Brown {
2504fdf676e5SLen Brown 	FILE *fp;
2505fdf676e5SLen Brown 	int retval;
2506fdf676e5SLen Brown 
2507fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2508fdf676e5SLen Brown 
2509fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2510fdf676e5SLen Brown 	if (retval != 1)
2511fdf676e5SLen Brown 		err(1, "GFX rc6");
2512fdf676e5SLen Brown 
2513fdf676e5SLen Brown 	fclose(fp);
2514fdf676e5SLen Brown 
2515fdf676e5SLen Brown 	return 0;
2516fdf676e5SLen Brown }
2517fdf676e5SLen Brown /*
251827d47356SLen Brown  * snapshot_gfx_mhz()
251927d47356SLen Brown  *
252027d47356SLen Brown  * record snapshot of
252127d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
252227d47356SLen Brown  *
252327d47356SLen Brown  * return 1 if config change requires a restart, else return 0
252427d47356SLen Brown  */
252527d47356SLen Brown int snapshot_gfx_mhz(void)
252627d47356SLen Brown {
252727d47356SLen Brown 	static FILE *fp;
252827d47356SLen Brown 	int retval;
252927d47356SLen Brown 
253027d47356SLen Brown 	if (fp == NULL)
253127d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
253222048c54SLen Brown 	else {
253327d47356SLen Brown 		rewind(fp);
253422048c54SLen Brown 		fflush(fp);
253522048c54SLen Brown 	}
253627d47356SLen Brown 
253727d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
253827d47356SLen Brown 	if (retval != 1)
253927d47356SLen Brown 		err(1, "GFX MHz");
254027d47356SLen Brown 
254127d47356SLen Brown 	return 0;
254227d47356SLen Brown }
2543562a2d37SLen Brown 
2544562a2d37SLen Brown /*
2545562a2d37SLen Brown  * snapshot /proc and /sys files
2546562a2d37SLen Brown  *
2547562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2548562a2d37SLen Brown  */
2549562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2550562a2d37SLen Brown {
2551218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2552562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2553562a2d37SLen Brown 			return 1;
2554562a2d37SLen Brown 
2555812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2556fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2557fdf676e5SLen Brown 
2558812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
255927d47356SLen Brown 		snapshot_gfx_mhz();
256027d47356SLen Brown 
2561562a2d37SLen Brown 	return 0;
2562562a2d37SLen Brown }
2563562a2d37SLen Brown 
2564103a8feaSLen Brown void turbostat_loop()
2565103a8feaSLen Brown {
2566c98d5d94SLen Brown 	int retval;
2567e52966c0SLen Brown 	int restarted = 0;
2568c98d5d94SLen Brown 
2569103a8feaSLen Brown restart:
2570e52966c0SLen Brown 	restarted++;
2571e52966c0SLen Brown 
2572562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2573c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2574d91bb17cSLen Brown 	if (retval < -1) {
2575d91bb17cSLen Brown 		exit(retval);
2576d91bb17cSLen Brown 	} else if (retval == -1) {
2577e52966c0SLen Brown 		if (restarted > 1) {
2578e52966c0SLen Brown 			exit(retval);
2579e52966c0SLen Brown 		}
2580c98d5d94SLen Brown 		re_initialize();
2581c98d5d94SLen Brown 		goto restart;
2582c98d5d94SLen Brown 	}
2583e52966c0SLen Brown 	restarted = 0;
2584103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2585103a8feaSLen Brown 
2586103a8feaSLen Brown 	while (1) {
2587c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2588103a8feaSLen Brown 			re_initialize();
2589103a8feaSLen Brown 			goto restart;
2590103a8feaSLen Brown 		}
25912a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2592562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2593562a2d37SLen Brown 			goto restart;
2594c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
2595d91bb17cSLen Brown 		if (retval < -1) {
2596d91bb17cSLen Brown 			exit(retval);
2597d91bb17cSLen Brown 		} else if (retval == -1) {
259815aaa346SLen Brown 			re_initialize();
259915aaa346SLen Brown 			goto restart;
260015aaa346SLen Brown 		}
2601103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
2602103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
2603ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2604ba3dec99SLen Brown 			re_initialize();
2605ba3dec99SLen Brown 			goto restart;
2606ba3dec99SLen Brown 		}
2607c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
2608c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
2609b7d8c148SLen Brown 		flush_output_stdout();
26102a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2611562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2612562a2d37SLen Brown 			goto restart;
2613c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2614d91bb17cSLen Brown 		if (retval < -1) {
2615d91bb17cSLen Brown 			exit(retval);
2616d91bb17cSLen Brown 		} else if (retval == -1) {
2617103a8feaSLen Brown 			re_initialize();
2618103a8feaSLen Brown 			goto restart;
2619103a8feaSLen Brown 		}
2620103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
2621103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
2622ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2623ba3dec99SLen Brown 			re_initialize();
2624ba3dec99SLen Brown 			goto restart;
2625ba3dec99SLen Brown 		}
2626c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
2627c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
2628b7d8c148SLen Brown 		flush_output_stdout();
2629103a8feaSLen Brown 	}
2630103a8feaSLen Brown }
2631103a8feaSLen Brown 
2632103a8feaSLen Brown void check_dev_msr()
2633103a8feaSLen Brown {
2634103a8feaSLen Brown 	struct stat sb;
26357ce7d5deSPrarit Bhargava 	char pathname[32];
2636103a8feaSLen Brown 
26377ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
26387ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
2639a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2640d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2641103a8feaSLen Brown }
2642103a8feaSLen Brown 
264398481e79SLen Brown void check_permissions()
2644103a8feaSLen Brown {
264598481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
264698481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
264798481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
264898481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
264998481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
265098481e79SLen Brown 	int do_exit = 0;
26517ce7d5deSPrarit Bhargava 	char pathname[32];
265298481e79SLen Brown 
265398481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
265498481e79SLen Brown 	cap_header->pid = getpid();
265598481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
265698481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
265798481e79SLen Brown 		err(-6, "capget(2) failed");
265898481e79SLen Brown 
265998481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
266098481e79SLen Brown 		do_exit++;
266198481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
266298481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
266398481e79SLen Brown 	}
266498481e79SLen Brown 
266598481e79SLen Brown 	/* test file permissions */
26667ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
26677ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
266898481e79SLen Brown 		do_exit++;
266998481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
267098481e79SLen Brown 	}
267198481e79SLen Brown 
267298481e79SLen Brown 	/* if all else fails, thell them to be root */
267398481e79SLen Brown 	if (do_exit)
2674b2c95d90SJosh Triplett 		if (getuid() != 0)
2675d7899447SLen Brown 			warnx("... or simply run as root");
267698481e79SLen Brown 
267798481e79SLen Brown 	if (do_exit)
267898481e79SLen Brown 		exit(-6);
2679103a8feaSLen Brown }
2680103a8feaSLen Brown 
2681d7899447SLen Brown /*
2682d7899447SLen Brown  * NHM adds support for additional MSRs:
2683d7899447SLen Brown  *
2684d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
2685d7899447SLen Brown  *
2686ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
26871df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
2688d7899447SLen Brown  *
2689cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
2690cf4cbe53SLen Brown  *
2691d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
2692d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
2693d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
2694d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
2695d7899447SLen Brown  *
2696ee7e38e3SLen Brown  * Side effect:
26971df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
269833148d67SLen Brown  * sets has_misc_feature_control
2699d7899447SLen Brown  */
2700ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
2701103a8feaSLen Brown {
2702ee7e38e3SLen Brown 	unsigned long long msr;
270321ed5574SLen Brown 	unsigned int base_ratio;
2704ee7e38e3SLen Brown 	int *pkg_cstate_limits;
2705ee7e38e3SLen Brown 
2706103a8feaSLen Brown 	if (!genuine_intel)
2707103a8feaSLen Brown 		return 0;
2708103a8feaSLen Brown 
2709103a8feaSLen Brown 	if (family != 6)
2710103a8feaSLen Brown 		return 0;
2711103a8feaSLen Brown 
271221ed5574SLen Brown 	bclk = discover_bclk(family, model);
271321ed5574SLen Brown 
2714103a8feaSLen Brown 	switch (model) {
2715869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2716869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2717103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
2718869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
2719869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
2720869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2721869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2722ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
2723ee7e38e3SLen Brown 		break;
2724869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
2725869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
2726869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2727869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2728ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
272933148d67SLen Brown 		has_misc_feature_control = 1;
2730ee7e38e3SLen Brown 		break;
2731869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2732869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2733869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2734869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2735869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2736869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2737869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2738869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2739869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2740869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2741869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2742869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2743ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
274433148d67SLen Brown 		has_misc_feature_control = 1;
2745ee7e38e3SLen Brown 		break;
2746d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
2747d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
274833148d67SLen Brown 		has_misc_feature_control = 1;
2749d8ebb442SLen Brown 		break;
2750869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
2751cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2752869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
2753ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
2754ee7e38e3SLen Brown 		break;
2755869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
2756ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
2757cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2758ee7e38e3SLen Brown 		break;
2759869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
2760005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2761ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
2762ee7e38e3SLen Brown 		break;
2763869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
2764ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
2765869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
2766e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
2767e4085d54SLen Brown 		break;
2768103a8feaSLen Brown 	default:
2769103a8feaSLen Brown 		return 0;
2770103a8feaSLen Brown 	}
27711df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2772e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2773ee7e38e3SLen Brown 
2774ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
277521ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
277621ed5574SLen Brown 
277721ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
277821ed5574SLen Brown 	has_base_hz = 1;
2779ee7e38e3SLen Brown 	return 1;
2780103a8feaSLen Brown }
27810f7887c4SLen Brown /*
2782495c7654SLen Brown  * SLV client has support for unique MSRs:
27830f7887c4SLen Brown  *
27840f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
27850f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
27860f7887c4SLen Brown  */
27870f7887c4SLen Brown 
27880f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
27890f7887c4SLen Brown {
27900f7887c4SLen Brown 	if (!genuine_intel)
27910f7887c4SLen Brown 		return 0;
27920f7887c4SLen Brown 
27930f7887c4SLen Brown 	switch (model) {
27940f7887c4SLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
27950f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MERRIFIELD:
27960f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MOOREFIELD:
27970f7887c4SLen Brown 		return 1;
27980f7887c4SLen Brown 	}
27990f7887c4SLen Brown 	return 0;
28000f7887c4SLen Brown }
28017170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
28027170a374SLen Brown {
28037170a374SLen Brown 
28047170a374SLen Brown 	if (!genuine_intel)
28057170a374SLen Brown 		return 0;
28067170a374SLen Brown 
28077170a374SLen Brown 	switch (model) {
28087170a374SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
28097170a374SLen Brown 		return 1;
28107170a374SLen Brown 	}
28117170a374SLen Brown 	return 0;
28127170a374SLen Brown }
2813ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
2814ade0ebacSLen Brown {
2815ade0ebacSLen Brown 
2816ade0ebacSLen Brown 	if (!genuine_intel)
2817ade0ebacSLen Brown 		return 0;
2818ade0ebacSLen Brown 
2819ade0ebacSLen Brown 	switch (model) {
2820ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
2821ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:
2822ade0ebacSLen Brown 		return 1;
2823ade0ebacSLen Brown 	}
2824ade0ebacSLen Brown 	return 0;
2825ade0ebacSLen Brown }
282634c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
282734c76197SLen Brown {
282834c76197SLen Brown 
282934c76197SLen Brown 	if (!genuine_intel)
283034c76197SLen Brown 		return 0;
283134c76197SLen Brown 
283234c76197SLen Brown 	switch (model) {
283334c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
283434c76197SLen Brown 		return 1;
283534c76197SLen Brown 	}
283634c76197SLen Brown 	return 0;
283734c76197SLen Brown }
28380f7887c4SLen Brown 
283931e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
2840d7899447SLen Brown {
28410f7887c4SLen Brown 	if (has_slv_msrs(family, model))
28420f7887c4SLen Brown 		return 0;
28430f7887c4SLen Brown 
2844d7899447SLen Brown 	switch (model) {
2845d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
2846869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2847869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2848869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
2849005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2850d7899447SLen Brown 		return 0;
2851d7899447SLen Brown 	default:
2852d7899447SLen Brown 		return 1;
2853d7899447SLen Brown 	}
2854d7899447SLen Brown }
28550f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
28560f7887c4SLen Brown {
28570f7887c4SLen Brown 	if (has_slv_msrs(family, model))
28580f7887c4SLen Brown 		return 1;
28590f7887c4SLen Brown 
28600f7887c4SLen Brown 	return 0;
28610f7887c4SLen Brown }
28626574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
28636574a5d5SLen Brown {
28646574a5d5SLen Brown 	if (!genuine_intel)
28656574a5d5SLen Brown 		return 0;
28666574a5d5SLen Brown 
28676574a5d5SLen Brown 	if (family != 6)
28686574a5d5SLen Brown 		return 0;
28696574a5d5SLen Brown 
28706574a5d5SLen Brown 	switch (model) {
2871869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2872869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
28736574a5d5SLen Brown 		return 1;
28746574a5d5SLen Brown 	default:
28756574a5d5SLen Brown 		return 0;
28766574a5d5SLen Brown 	}
28776574a5d5SLen Brown }
2878fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2879fcd17211SLen Brown {
2880fcd17211SLen Brown 	if (!genuine_intel)
2881fcd17211SLen Brown 		return 0;
2882fcd17211SLen Brown 
2883fcd17211SLen Brown 	if (family != 6)
2884fcd17211SLen Brown 		return 0;
2885fcd17211SLen Brown 
2886fcd17211SLen Brown 	switch (model) {
2887869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
2888fcd17211SLen Brown 		return 1;
2889fcd17211SLen Brown 	default:
2890fcd17211SLen Brown 		return 0;
2891fcd17211SLen Brown 	}
2892fcd17211SLen Brown }
2893fcd17211SLen Brown 
2894fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2895fb5d4327SDasaratharaman Chandramouli {
2896fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
2897fb5d4327SDasaratharaman Chandramouli 		return 0;
2898fb5d4327SDasaratharaman Chandramouli 
2899fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
2900fb5d4327SDasaratharaman Chandramouli 		return 0;
2901fb5d4327SDasaratharaman Chandramouli 
2902fb5d4327SDasaratharaman Chandramouli 	switch (model) {
2903869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2904005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2905fb5d4327SDasaratharaman Chandramouli 		return 1;
2906fb5d4327SDasaratharaman Chandramouli 	default:
2907fb5d4327SDasaratharaman Chandramouli 		return 0;
2908fb5d4327SDasaratharaman Chandramouli 	}
2909fb5d4327SDasaratharaman Chandramouli }
291031e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
291131e07522SLen Brown {
291231e07522SLen Brown 	if (!genuine_intel)
291331e07522SLen Brown 		return 0;
291431e07522SLen Brown 
291531e07522SLen Brown 	if (family != 6)
291631e07522SLen Brown 		return 0;
291731e07522SLen Brown 
291831e07522SLen Brown 	switch (model) {
291931e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
292031e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
292131e07522SLen Brown 		return 1;
292231e07522SLen Brown 	default:
292331e07522SLen Brown 		return 0;
292431e07522SLen Brown 	}
292531e07522SLen Brown }
29266fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
29276fb3143bSLen Brown {
29286fb3143bSLen Brown 	if (!genuine_intel)
29296fb3143bSLen Brown 		return 0;
29306fb3143bSLen Brown 
29316fb3143bSLen Brown 	if (family != 6)
29326fb3143bSLen Brown 		return 0;
29336fb3143bSLen Brown 
29346fb3143bSLen Brown 	switch (model) {
2935869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2936869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2937869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2938869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2939869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2940869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2941869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2942869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2943869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2944869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2945869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2946869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2947869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2948869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
29496fb3143bSLen Brown 
2950869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2951005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
29526fb3143bSLen Brown 		return 1;
29536fb3143bSLen Brown 	default:
29546fb3143bSLen Brown 		return 0;
29556fb3143bSLen Brown 	}
29566fb3143bSLen Brown }
29576fb3143bSLen Brown 
2958fcd17211SLen Brown static void
29591b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
2960fcd17211SLen Brown {
2961fcd17211SLen Brown 	if (!do_nhm_platform_info)
2962fcd17211SLen Brown 		return;
2963fcd17211SLen Brown 
2964fcd17211SLen Brown 	dump_nhm_platform_info();
2965fcd17211SLen Brown 
2966fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
2967fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
2968fcd17211SLen Brown 
2969fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
2970fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
2971fcd17211SLen Brown 
297231e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
297331e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
2974fcd17211SLen Brown 
29750f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
29760f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
29770f7887c4SLen Brown 
2978fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
2979fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
2980fb5d4327SDasaratharaman Chandramouli 
29816fb3143bSLen Brown 	if (has_config_tdp(family, model))
29826fb3143bSLen Brown 		dump_config_tdp();
29836fb3143bSLen Brown 
2984fcd17211SLen Brown 	dump_nhm_cst_cfg();
2985fcd17211SLen Brown }
2986fcd17211SLen Brown 
298741618e63SLen Brown static void
298841618e63SLen Brown dump_sysfs_cstate_config(void)
298941618e63SLen Brown {
299041618e63SLen Brown 	char path[64];
299141618e63SLen Brown 	char name_buf[16];
299241618e63SLen Brown 	char desc[64];
299341618e63SLen Brown 	FILE *input;
299441618e63SLen Brown 	int state;
299541618e63SLen Brown 	char *sp;
299641618e63SLen Brown 
299741618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
299841618e63SLen Brown 		return;
299941618e63SLen Brown 
300041618e63SLen Brown 	for (state = 0; state < 10; ++state) {
300141618e63SLen Brown 
300241618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
300341618e63SLen Brown 			base_cpu, state);
300441618e63SLen Brown 		input = fopen(path, "r");
300541618e63SLen Brown 		if (input == NULL)
300641618e63SLen Brown 			continue;
300741618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
300841618e63SLen Brown 
300941618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
301041618e63SLen Brown 		sp = strchr(name_buf, '-');
301141618e63SLen Brown 		if (!sp)
301241618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
301341618e63SLen Brown 		*sp = '\0';
301441618e63SLen Brown 
301541618e63SLen Brown 		fclose(input);
301641618e63SLen Brown 
301741618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
301841618e63SLen Brown 			base_cpu, state);
301941618e63SLen Brown 		input = fopen(path, "r");
302041618e63SLen Brown 		if (input == NULL)
302141618e63SLen Brown 			continue;
302241618e63SLen Brown 		fgets(desc, sizeof(desc), input);
302341618e63SLen Brown 
302441618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
302541618e63SLen Brown 		fclose(input);
302641618e63SLen Brown 	}
302741618e63SLen Brown }
30287293fccdSLen Brown static void
30297293fccdSLen Brown dump_sysfs_pstate_config(void)
30307293fccdSLen Brown {
30317293fccdSLen Brown 	char path[64];
30327293fccdSLen Brown 	char driver_buf[64];
30337293fccdSLen Brown 	char governor_buf[64];
30347293fccdSLen Brown 	FILE *input;
30357293fccdSLen Brown 	int turbo;
30367293fccdSLen Brown 
30377293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
30387293fccdSLen Brown 			base_cpu);
30397293fccdSLen Brown 	input = fopen(path, "r");
30407293fccdSLen Brown 	if (input == NULL) {
30417293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
30427293fccdSLen Brown 		return;
30437293fccdSLen Brown 	}
30447293fccdSLen Brown 	fgets(driver_buf, sizeof(driver_buf), input);
30457293fccdSLen Brown 	fclose(input);
30467293fccdSLen Brown 
30477293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
30487293fccdSLen Brown 			base_cpu);
30497293fccdSLen Brown 	input = fopen(path, "r");
30507293fccdSLen Brown 	if (input == NULL) {
30517293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
30527293fccdSLen Brown 		return;
30537293fccdSLen Brown 	}
30547293fccdSLen Brown 	fgets(governor_buf, sizeof(governor_buf), input);
30557293fccdSLen Brown 	fclose(input);
30567293fccdSLen Brown 
30577293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
30587293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
30597293fccdSLen Brown 
30607293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
30617293fccdSLen Brown 	input = fopen(path, "r");
30627293fccdSLen Brown 	if (input != NULL) {
30637293fccdSLen Brown 		fscanf(input, "%d", &turbo);
30647293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
30657293fccdSLen Brown 		fclose(input);
30667293fccdSLen Brown 	}
30677293fccdSLen Brown 
30687293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
30697293fccdSLen Brown 	input = fopen(path, "r");
30707293fccdSLen Brown 	if (input != NULL) {
30717293fccdSLen Brown 		fscanf(input, "%d", &turbo);
30727293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
30737293fccdSLen Brown 		fclose(input);
30747293fccdSLen Brown 	}
30757293fccdSLen Brown }
307641618e63SLen Brown 
30776574a5d5SLen Brown 
3078889facbeSLen Brown /*
3079889facbeSLen Brown  * print_epb()
3080889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3081889facbeSLen Brown  */
3082889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3083889facbeSLen Brown {
3084889facbeSLen Brown 	unsigned long long msr;
3085889facbeSLen Brown 	char *epb_string;
3086889facbeSLen Brown 	int cpu;
3087889facbeSLen Brown 
3088889facbeSLen Brown 	if (!has_epb)
3089889facbeSLen Brown 		return 0;
3090889facbeSLen Brown 
3091889facbeSLen Brown 	cpu = t->cpu_id;
3092889facbeSLen Brown 
3093889facbeSLen Brown 	/* EPB is per-package */
3094889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3095889facbeSLen Brown 		return 0;
3096889facbeSLen Brown 
3097889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3098b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3099889facbeSLen Brown 		return -1;
3100889facbeSLen Brown 	}
3101889facbeSLen Brown 
3102889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3103889facbeSLen Brown 		return 0;
3104889facbeSLen Brown 
3105e9be7dd6SLen Brown 	switch (msr & 0xF) {
3106889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3107889facbeSLen Brown 		epb_string = "performance";
3108889facbeSLen Brown 		break;
3109889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3110889facbeSLen Brown 		epb_string = "balanced";
3111889facbeSLen Brown 		break;
3112889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3113889facbeSLen Brown 		epb_string = "powersave";
3114889facbeSLen Brown 		break;
3115889facbeSLen Brown 	default:
3116889facbeSLen Brown 		epb_string = "custom";
3117889facbeSLen Brown 		break;
3118889facbeSLen Brown 	}
3119b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3120889facbeSLen Brown 
3121889facbeSLen Brown 	return 0;
3122889facbeSLen Brown }
31237f5c258eSLen Brown /*
31247f5c258eSLen Brown  * print_hwp()
31257f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
31267f5c258eSLen Brown  */
31277f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
31287f5c258eSLen Brown {
31297f5c258eSLen Brown 	unsigned long long msr;
31307f5c258eSLen Brown 	int cpu;
31317f5c258eSLen Brown 
31327f5c258eSLen Brown 	if (!has_hwp)
31337f5c258eSLen Brown 		return 0;
31347f5c258eSLen Brown 
31357f5c258eSLen Brown 	cpu = t->cpu_id;
31367f5c258eSLen Brown 
31377f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
31387f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
31397f5c258eSLen Brown 		return 0;
31407f5c258eSLen Brown 
31417f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3142b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
31437f5c258eSLen Brown 		return -1;
31447f5c258eSLen Brown 	}
31457f5c258eSLen Brown 
31467f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
31477f5c258eSLen Brown 		return 0;
31487f5c258eSLen Brown 
3149b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
31507f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
31517f5c258eSLen Brown 
31527f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
31537f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
31547f5c258eSLen Brown 		return 0;
31557f5c258eSLen Brown 
31567f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
31577f5c258eSLen Brown 		return 0;
31587f5c258eSLen Brown 
3159b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
31606dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
31617f5c258eSLen Brown 			cpu, msr,
31627f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
31637f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
31647f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
31657f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
31667f5c258eSLen Brown 
31677f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
31687f5c258eSLen Brown 		return 0;
31697f5c258eSLen Brown 
3170b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
31716dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
31727f5c258eSLen Brown 			cpu, msr,
31737f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
31747f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
31757f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
31767f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
31777f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
31787f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
31797f5c258eSLen Brown 
31807f5c258eSLen Brown 	if (has_hwp_pkg) {
31817f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
31827f5c258eSLen Brown 			return 0;
31837f5c258eSLen Brown 
3184b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
31856dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
31867f5c258eSLen Brown 			cpu, msr,
31877f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
31887f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
31897f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
31907f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
31917f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
31927f5c258eSLen Brown 	}
31937f5c258eSLen Brown 	if (has_hwp_notify) {
31947f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
31957f5c258eSLen Brown 			return 0;
31967f5c258eSLen Brown 
3197b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
31987f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
31997f5c258eSLen Brown 			cpu, msr,
32007f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
32017f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
32027f5c258eSLen Brown 	}
32037f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
32047f5c258eSLen Brown 		return 0;
32057f5c258eSLen Brown 
3206b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
32077f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
32087f5c258eSLen Brown 			cpu, msr,
32097f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
32107f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3211889facbeSLen Brown 
3212889facbeSLen Brown 	return 0;
3213889facbeSLen Brown }
3214889facbeSLen Brown 
32153a9a941dSLen Brown /*
32163a9a941dSLen Brown  * print_perf_limit()
32173a9a941dSLen Brown  */
32183a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
32193a9a941dSLen Brown {
32203a9a941dSLen Brown 	unsigned long long msr;
32213a9a941dSLen Brown 	int cpu;
32223a9a941dSLen Brown 
32233a9a941dSLen Brown 	cpu = t->cpu_id;
32243a9a941dSLen Brown 
32253a9a941dSLen Brown 	/* per-package */
32263a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
32273a9a941dSLen Brown 		return 0;
32283a9a941dSLen Brown 
32293a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3230b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
32313a9a941dSLen Brown 		return -1;
32323a9a941dSLen Brown 	}
32333a9a941dSLen Brown 
32343a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
32353a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3236b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3237b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3238e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
32393a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3240e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3241e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3242e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3243e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3244e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3245e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3246e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3247e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3248e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3249e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3250e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3251e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3252b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3253e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
32543a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3255e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3256e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3257e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3258e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3259e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3260e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3261e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3262e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3263e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3264e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3265e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3266e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
32673a9a941dSLen Brown 
32683a9a941dSLen Brown 	}
32693a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
32703a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3271b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3272b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
32733a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
32743a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
32753a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
32763a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
32773a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
32783a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
32793a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
32803a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3281b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
32823a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
32833a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
32843a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
32853a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
32863a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
32873a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
32883a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
32893a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
32903a9a941dSLen Brown 	}
32913a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
32923a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3293b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3294b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
32953a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
32963a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
32973a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
32983a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
32993a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
33003a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3301b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
33023a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
33033a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
33043a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
33053a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
33063a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
33073a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
33083a9a941dSLen Brown 	}
33093a9a941dSLen Brown 	return 0;
33103a9a941dSLen Brown }
33113a9a941dSLen Brown 
3312889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3313889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3314889facbeSLen Brown 
33151b69317dSColin Ian King double get_tdp(unsigned int model)
3316144b44b1SLen Brown {
3317144b44b1SLen Brown 	unsigned long long msr;
3318144b44b1SLen Brown 
3319144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
33207ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3321144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3322144b44b1SLen Brown 
3323144b44b1SLen Brown 	switch (model) {
3324869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
3325869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:
3326144b44b1SLen Brown 		return 30.0;
3327144b44b1SLen Brown 	default:
3328144b44b1SLen Brown 		return 135.0;
3329144b44b1SLen Brown 	}
3330144b44b1SLen Brown }
3331144b44b1SLen Brown 
333240ee8e3bSAndrey Semin /*
333340ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
333440ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
333540ee8e3bSAndrey Semin  */
333640ee8e3bSAndrey Semin static double
333740ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
333840ee8e3bSAndrey Semin {
333940ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
334040ee8e3bSAndrey Semin 
334140ee8e3bSAndrey Semin 	switch (model) {
3342869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3343869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3344869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3345869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3346005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
334740ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
334840ee8e3bSAndrey Semin 	default:
334940ee8e3bSAndrey Semin 		return (rapl_energy_units);
335040ee8e3bSAndrey Semin 	}
335140ee8e3bSAndrey Semin }
335240ee8e3bSAndrey Semin 
3353144b44b1SLen Brown 
3354889facbeSLen Brown /*
3355889facbeSLen Brown  * rapl_probe()
3356889facbeSLen Brown  *
3357144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3358889facbeSLen Brown  */
3359889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
3360889facbeSLen Brown {
3361889facbeSLen Brown 	unsigned long long msr;
3362144b44b1SLen Brown 	unsigned int time_unit;
3363889facbeSLen Brown 	double tdp;
3364889facbeSLen Brown 
3365889facbeSLen Brown 	if (!genuine_intel)
3366889facbeSLen Brown 		return;
3367889facbeSLen Brown 
3368889facbeSLen Brown 	if (family != 6)
3369889facbeSLen Brown 		return;
3370889facbeSLen Brown 
3371889facbeSLen Brown 	switch (model) {
3372869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3373869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3374869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3375869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3376869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3377869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3378869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3379144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3380812db3f7SLen Brown 		if (rapl_joules) {
3381812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3382812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3383812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3384812db3f7SLen Brown 		} else {
3385812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3386812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3387812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3388812db3f7SLen Brown 		}
3389889facbeSLen Brown 		break;
3390869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3391ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3392e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3393812db3f7SLen Brown 		if (rapl_joules)
3394812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3395812db3f7SLen Brown 		else
3396812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3397e4085d54SLen Brown 		break;
3398869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3399869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3400869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3401869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
340281824921SLen 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;
3403812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3404812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3405812db3f7SLen Brown 		if (rapl_joules) {
3406812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3407812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3408812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
340981824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3410812db3f7SLen Brown 		} else {
3411812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3412812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3413812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
341481824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3415812db3f7SLen Brown 		}
34160b2bb692SLen Brown 		break;
3417869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3418869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3419869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3420869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3421869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3422005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
34230b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3424812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3425812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3426812db3f7SLen Brown 		if (rapl_joules) {
3427812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3428812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3429812db3f7SLen Brown 		} else {
3430812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3431812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3432812db3f7SLen Brown 		}
3433e6f9bb3cSLen Brown 		break;
3434869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3435869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
34360b2bb692SLen 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;
3437812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3438812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3439812db3f7SLen Brown 		if (rapl_joules) {
3440812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3441812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3442812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3443812db3f7SLen Brown 		} else {
3444812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3445812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3446812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3447812db3f7SLen Brown 		}
3448144b44b1SLen Brown 		break;
3449869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3450869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3451144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3452812db3f7SLen Brown 		if (rapl_joules) {
3453812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3454812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3455812db3f7SLen Brown 		} else {
3456812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3457812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3458812db3f7SLen Brown 		}
3459889facbeSLen Brown 		break;
3460869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
34610f644909SJacob 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;
3462812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3463812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3464812db3f7SLen Brown 		if (rapl_joules) {
3465812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3466812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3467812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3468812db3f7SLen Brown 		} else {
3469812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3470812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3471812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3472812db3f7SLen Brown 		}
34730f644909SJacob Pan 		break;
3474889facbeSLen Brown 	default:
3475889facbeSLen Brown 		return;
3476889facbeSLen Brown 	}
3477889facbeSLen Brown 
3478889facbeSLen Brown 	/* units on package 0, verify later other packages match */
34797ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3480889facbeSLen Brown 		return;
3481889facbeSLen Brown 
3482889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3483869ce69eSLen Brown 	if (model == INTEL_FAM6_ATOM_SILVERMONT1)
3484144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3485144b44b1SLen Brown 	else
3486889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3487889facbeSLen Brown 
348840ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
348940ee8e3bSAndrey Semin 
3490144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3491144b44b1SLen Brown 	if (time_unit == 0)
3492144b44b1SLen Brown 		time_unit = 0xA;
3493889facbeSLen Brown 
3494144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3495144b44b1SLen Brown 
3496144b44b1SLen Brown 	tdp = get_tdp(model);
3497889facbeSLen Brown 
3498889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
349996e47158SLen Brown 	if (!quiet)
3500b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3501889facbeSLen Brown 
3502889facbeSLen Brown 	return;
3503889facbeSLen Brown }
3504889facbeSLen Brown 
35051b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
35063a9a941dSLen Brown {
35073a9a941dSLen Brown 	if (!genuine_intel)
35083a9a941dSLen Brown 		return;
35093a9a941dSLen Brown 
35103a9a941dSLen Brown 	if (family != 6)
35113a9a941dSLen Brown 		return;
35123a9a941dSLen Brown 
35133a9a941dSLen Brown 	switch (model) {
3514869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3515869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3516869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
35173a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
3518869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
35193a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
35203a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
35213a9a941dSLen Brown 	default:
35223a9a941dSLen Brown 		return;
35233a9a941dSLen Brown 	}
35243a9a941dSLen Brown }
35253a9a941dSLen Brown 
3526889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3527889facbeSLen Brown {
3528889facbeSLen Brown 	unsigned long long msr;
3529f4896fa5SLen Brown 	unsigned int dts, dts2;
3530889facbeSLen Brown 	int cpu;
3531889facbeSLen Brown 
3532889facbeSLen Brown 	if (!(do_dts || do_ptm))
3533889facbeSLen Brown 		return 0;
3534889facbeSLen Brown 
3535889facbeSLen Brown 	cpu = t->cpu_id;
3536889facbeSLen Brown 
3537889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
3538889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3539889facbeSLen Brown 		return 0;
3540889facbeSLen Brown 
3541889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3542b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3543889facbeSLen Brown 		return -1;
3544889facbeSLen Brown 	}
3545889facbeSLen Brown 
3546889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3547889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3548889facbeSLen Brown 			return 0;
3549889facbeSLen Brown 
3550889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3551b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3552889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
3553889facbeSLen Brown 
3554889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3555889facbeSLen Brown 			return 0;
3556889facbeSLen Brown 
3557889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3558889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3559b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3560889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3561889facbeSLen Brown 	}
3562889facbeSLen Brown 
3563889facbeSLen Brown 
3564f4896fa5SLen Brown 	if (do_dts && debug) {
3565889facbeSLen Brown 		unsigned int resolution;
3566889facbeSLen Brown 
3567889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3568889facbeSLen Brown 			return 0;
3569889facbeSLen Brown 
3570889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3571889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
3572b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3573889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
3574889facbeSLen Brown 
3575889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3576889facbeSLen Brown 			return 0;
3577889facbeSLen Brown 
3578889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3579889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3580b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3581889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3582889facbeSLen Brown 	}
3583889facbeSLen Brown 
3584889facbeSLen Brown 	return 0;
3585889facbeSLen Brown }
3586889facbeSLen Brown 
3587889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3588889facbeSLen Brown {
3589b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
3590889facbeSLen Brown 		cpu, label,
3591889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
3592889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
3593889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3594889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
3595889facbeSLen Brown 
3596889facbeSLen Brown 	return;
3597889facbeSLen Brown }
3598889facbeSLen Brown 
3599889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3600889facbeSLen Brown {
3601889facbeSLen Brown 	unsigned long long msr;
3602889facbeSLen Brown 	int cpu;
3603889facbeSLen Brown 
3604889facbeSLen Brown 	if (!do_rapl)
3605889facbeSLen Brown 		return 0;
3606889facbeSLen Brown 
3607889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
3608889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3609889facbeSLen Brown 		return 0;
3610889facbeSLen Brown 
3611889facbeSLen Brown 	cpu = t->cpu_id;
3612889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3613b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3614889facbeSLen Brown 		return -1;
3615889facbeSLen Brown 	}
3616889facbeSLen Brown 
3617889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3618889facbeSLen Brown 		return -1;
3619889facbeSLen Brown 
362096e47158SLen Brown 	fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3621144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
362296e47158SLen Brown 
3623144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
3624144b44b1SLen Brown 
3625889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3626889facbeSLen Brown                 	return -5;
3627889facbeSLen Brown 
3628889facbeSLen Brown 
3629b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3630889facbeSLen Brown 			cpu, msr,
3631889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3632889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3633889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3634889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3635889facbeSLen Brown 
3636144b44b1SLen Brown 	}
3637144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
3638144b44b1SLen Brown 
3639889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3640889facbeSLen Brown 			return -9;
3641889facbeSLen Brown 
3642b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
3643889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
3644889facbeSLen Brown 
3645889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
3646b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
3647889facbeSLen Brown 			cpu,
3648889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
3649889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
3650889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3651889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
3652889facbeSLen Brown 	}
3653889facbeSLen Brown 
36540b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
3655889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3656889facbeSLen Brown                 	return -6;
3657889facbeSLen Brown 
3658b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3659889facbeSLen Brown 			cpu, msr,
3660889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3661889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3662889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3663889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
36640b2bb692SLen Brown 	}
36650b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
3666889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3667889facbeSLen Brown 			return -9;
3668b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
3669889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3670889facbeSLen Brown 
3671889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
3672889facbeSLen Brown 	}
3673144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
3674889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3675889facbeSLen Brown 			return -7;
3676889facbeSLen Brown 
3677b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
3678144b44b1SLen Brown 	}
36799148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
3680889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3681889facbeSLen Brown 			return -9;
3682b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3683889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3684889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
3685889facbeSLen Brown 	}
3686889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
3687889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3688889facbeSLen Brown 			return -8;
3689889facbeSLen Brown 
3690b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
3691889facbeSLen Brown 
3692889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3693889facbeSLen Brown 			return -9;
3694b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3695889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3696889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
3697889facbeSLen Brown 	}
3698889facbeSLen Brown 	return 0;
3699889facbeSLen Brown }
3700889facbeSLen Brown 
3701d7899447SLen Brown /*
3702d7899447SLen Brown  * SNB adds support for additional MSRs:
3703d7899447SLen Brown  *
3704d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
3705d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
3706d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
3707d7899447SLen Brown  */
3708103a8feaSLen Brown 
3709d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
3710103a8feaSLen Brown {
3711103a8feaSLen Brown 	if (!genuine_intel)
3712103a8feaSLen Brown 		return 0;
3713103a8feaSLen Brown 
3714103a8feaSLen Brown 	switch (model) {
3715869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3716869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3717869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3718869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3719869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3720869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
3721869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3722869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3723869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3724869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3725869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3726869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3727869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3728869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3729869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3730869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3731869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3732869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3733ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
37345bbac26eSXiaolong Wang 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3735103a8feaSLen Brown 		return 1;
3736103a8feaSLen Brown 	}
3737103a8feaSLen Brown 	return 0;
3738103a8feaSLen Brown }
3739103a8feaSLen Brown 
3740d7899447SLen Brown /*
3741d7899447SLen Brown  * HSW adds support for additional MSRs:
3742d7899447SLen Brown  *
3743d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
3744d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
3745d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
37465a63426eSLen Brown  *
37475a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
37485a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
37495a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
37505a63426eSLen Brown  *
3751d7899447SLen Brown  */
3752d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
3753ca58710fSKristen Carlson Accardi {
3754ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
3755ca58710fSKristen Carlson Accardi 		return 0;
3756ca58710fSKristen Carlson Accardi 
3757ca58710fSKristen Carlson Accardi 	switch (model) {
3758869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3759869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3760869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3761869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3762869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3763869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3764869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3765ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3766ca58710fSKristen Carlson Accardi 		return 1;
3767ca58710fSKristen Carlson Accardi 	}
3768ca58710fSKristen Carlson Accardi 	return 0;
3769ca58710fSKristen Carlson Accardi }
3770ca58710fSKristen Carlson Accardi 
37710b2bb692SLen Brown /*
37720b2bb692SLen Brown  * SKL adds support for additional MSRS:
37730b2bb692SLen Brown  *
37740b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
37750b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
37760b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
37770b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
37780b2bb692SLen Brown  */
37790b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
37800b2bb692SLen Brown {
37810b2bb692SLen Brown 	if (!genuine_intel)
37820b2bb692SLen Brown 		return 0;
37830b2bb692SLen Brown 
37840b2bb692SLen Brown 	switch (model) {
3785869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3786869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3787869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3788869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
37890b2bb692SLen Brown 		return 1;
37900b2bb692SLen Brown 	}
37910b2bb692SLen Brown 	return 0;
37920b2bb692SLen Brown }
37930b2bb692SLen Brown 
3794144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
3795144b44b1SLen Brown {
3796144b44b1SLen Brown 	if (!genuine_intel)
3797144b44b1SLen Brown 		return 0;
3798144b44b1SLen Brown 	switch (model) {
3799869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3800869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3801144b44b1SLen Brown 		return 1;
3802144b44b1SLen Brown 	}
3803144b44b1SLen Brown 	return 0;
3804144b44b1SLen Brown }
3805144b44b1SLen Brown 
3806fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
3807fb5d4327SDasaratharaman Chandramouli {
3808fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3809fb5d4327SDasaratharaman Chandramouli 		return 0;
3810fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3811869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3812005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3813fb5d4327SDasaratharaman Chandramouli 		return 1;
3814fb5d4327SDasaratharaman Chandramouli 	}
3815fb5d4327SDasaratharaman Chandramouli 	return 0;
3816fb5d4327SDasaratharaman Chandramouli }
3817fb5d4327SDasaratharaman Chandramouli 
3818b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3819b2b34dfeSHubert Chrzaniuk {
3820b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
3821b2b34dfeSHubert Chrzaniuk 		return 1024;
3822b2b34dfeSHubert Chrzaniuk 	return 1;
3823b2b34dfeSHubert Chrzaniuk }
3824b2b34dfeSHubert Chrzaniuk 
3825144b44b1SLen Brown #define SLM_BCLK_FREQS 5
3826144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3827144b44b1SLen Brown 
3828144b44b1SLen Brown double slm_bclk(void)
3829144b44b1SLen Brown {
3830144b44b1SLen Brown 	unsigned long long msr = 3;
3831144b44b1SLen Brown 	unsigned int i;
3832144b44b1SLen Brown 	double freq;
3833144b44b1SLen Brown 
38347ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3835b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
3836144b44b1SLen Brown 
3837144b44b1SLen Brown 	i = msr & 0xf;
3838144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
3839b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
38400a91e551SColin Ian King 		i = 3;
3841144b44b1SLen Brown 	}
3842144b44b1SLen Brown 	freq = slm_freq_table[i];
3843144b44b1SLen Brown 
384496e47158SLen Brown 	if (!quiet)
3845b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3846144b44b1SLen Brown 
3847144b44b1SLen Brown 	return freq;
3848144b44b1SLen Brown }
3849144b44b1SLen Brown 
3850103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
3851103a8feaSLen Brown {
3852121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
3853103a8feaSLen Brown 		return 100.00;
3854144b44b1SLen Brown 	else if (is_slm(family, model))
3855144b44b1SLen Brown 		return slm_bclk();
3856103a8feaSLen Brown 	else
3857103a8feaSLen Brown 		return 133.33;
3858103a8feaSLen Brown }
3859103a8feaSLen Brown 
3860889facbeSLen Brown /*
3861889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3862889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
3863889facbeSLen Brown  * This is usually equal to tjMax.
3864889facbeSLen Brown  *
3865889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
3866889facbeSLen Brown  * but also allow cmdline over-ride with -T.
3867889facbeSLen Brown  *
3868889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
3869889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
3870889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3871889facbeSLen Brown  */
3872889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3873889facbeSLen Brown {
3874889facbeSLen Brown 	unsigned long long msr;
3875889facbeSLen Brown 	unsigned int target_c_local;
3876889facbeSLen Brown 	int cpu;
3877889facbeSLen Brown 
3878889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
3879889facbeSLen Brown 	if (!(do_dts || do_ptm))
3880889facbeSLen Brown 		return 0;
3881889facbeSLen Brown 
3882889facbeSLen Brown 	/* this is a per-package concept */
3883889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3884889facbeSLen Brown 		return 0;
3885889facbeSLen Brown 
3886889facbeSLen Brown 	cpu = t->cpu_id;
3887889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3888b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3889889facbeSLen Brown 		return -1;
3890889facbeSLen Brown 	}
3891889facbeSLen Brown 
3892889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
3893889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
3894b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
3895889facbeSLen Brown 			cpu, tcc_activation_temp);
3896889facbeSLen Brown 		return 0;
3897889facbeSLen Brown 	}
3898889facbeSLen Brown 
3899889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
3900d7899447SLen Brown 	if (!do_nhm_platform_info)
3901889facbeSLen Brown 		goto guess;
3902889facbeSLen Brown 
39037ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
3904889facbeSLen Brown 		goto guess;
3905889facbeSLen Brown 
39063482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
3907889facbeSLen Brown 
390896e47158SLen Brown 	if (!quiet)
3909b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3910889facbeSLen Brown 			cpu, msr, target_c_local);
3911889facbeSLen Brown 
39123482124aSJean Delvare 	if (!target_c_local)
3913889facbeSLen Brown 		goto guess;
3914889facbeSLen Brown 
3915889facbeSLen Brown 	tcc_activation_temp = target_c_local;
3916889facbeSLen Brown 
3917889facbeSLen Brown 	return 0;
3918889facbeSLen Brown 
3919889facbeSLen Brown guess:
3920889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
3921b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3922889facbeSLen Brown 		cpu, tcc_activation_temp);
3923889facbeSLen Brown 
3924889facbeSLen Brown 	return 0;
3925889facbeSLen Brown }
392669807a63SLen Brown 
3927aa8d8cc7SLen Brown void decode_feature_control_msr(void)
3928aa8d8cc7SLen Brown {
3929aa8d8cc7SLen Brown 	unsigned long long msr;
3930aa8d8cc7SLen Brown 
3931aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3932aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3933aa8d8cc7SLen Brown 			base_cpu, msr,
3934aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3935aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
3936aa8d8cc7SLen Brown }
3937aa8d8cc7SLen Brown 
393869807a63SLen Brown void decode_misc_enable_msr(void)
393969807a63SLen Brown {
394069807a63SLen Brown 	unsigned long long msr;
394169807a63SLen Brown 
394269807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
3943e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
394469807a63SLen Brown 			base_cpu, msr,
3945e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3946e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3947e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3948e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3949e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
395069807a63SLen Brown }
395169807a63SLen Brown 
395233148d67SLen Brown void decode_misc_feature_control(void)
395333148d67SLen Brown {
395433148d67SLen Brown 	unsigned long long msr;
395533148d67SLen Brown 
395633148d67SLen Brown 	if (!has_misc_feature_control)
395733148d67SLen Brown 		return;
395833148d67SLen Brown 
395933148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
396033148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
396133148d67SLen Brown 			base_cpu, msr,
396233148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
396333148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
396433148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
396533148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
396633148d67SLen Brown }
3967f0057310SLen Brown /*
3968f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
3969f0057310SLen Brown  *
3970f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
3971f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
3972f0057310SLen Brown  * bit[1] less so...
3973f0057310SLen Brown  */
3974f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
3975f0057310SLen Brown {
3976f0057310SLen Brown 	unsigned long long msr;
3977f0057310SLen Brown 
3978f0057310SLen Brown 	if (!do_nhm_platform_info)
3979f0057310SLen Brown 		return;
3980f0057310SLen Brown 
3981cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
3982cf4cbe53SLen Brown 		return;
3983cf4cbe53SLen Brown 
3984f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
3985ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
3986f0057310SLen Brown 			base_cpu, msr,
3987f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
3988ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
3989ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
3990f0057310SLen Brown }
399171616c8eSLen Brown /*
399271616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
399371616c8eSLen Brown  *
399471616c8eSLen Brown  * This MSRs are present on Silvermont processors,
399571616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
399671616c8eSLen Brown  */
399771616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
399871616c8eSLen Brown {
399971616c8eSLen Brown 	unsigned long long msr;
400071616c8eSLen Brown 
400171616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
400271616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
400371616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
400471616c8eSLen Brown 
400571616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
400671616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
400771616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
400871616c8eSLen Brown }
40097f5c258eSLen Brown 
4010fcd17211SLen Brown void process_cpuid()
4011103a8feaSLen Brown {
401261a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
4013103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
4014b3a34e93SLen Brown 	unsigned int has_turbo;
4015103a8feaSLen Brown 
4016103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4017103a8feaSLen Brown 
40185aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4019103a8feaSLen Brown 
4020103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4021103a8feaSLen Brown 		genuine_intel = 1;
4022103a8feaSLen Brown 
402396e47158SLen Brown 	if (!quiet)
4024b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4025103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4026103a8feaSLen Brown 
40275aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4028103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4029103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4030103a8feaSLen Brown 	stepping = fms & 0xf;
4031103a8feaSLen Brown 	if (family == 6 || family == 0xf)
4032103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
4033103a8feaSLen Brown 
403496e47158SLen Brown 	if (!quiet) {
4035b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
4036103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
4037aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
403869807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
403969807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
4040aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
404169807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
404269807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
404369807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
404469807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
404569807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
404669807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
404769807a63SLen Brown 	}
4048103a8feaSLen Brown 
4049b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
4050b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
4051103a8feaSLen Brown 
4052103a8feaSLen Brown 	/*
4053103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4054103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4055103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4056103a8feaSLen Brown 	 */
4057103a8feaSLen Brown 	ebx = ecx = edx = 0;
40585aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4059103a8feaSLen Brown 
406061a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4061103a8feaSLen Brown 
4062103a8feaSLen Brown 		/*
4063103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4064103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4065103a8feaSLen Brown 		 */
40665aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
40678209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4068d7899447SLen Brown 	}
4069103a8feaSLen Brown 
4070103a8feaSLen Brown 	/*
4071103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4072103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4073103a8feaSLen Brown 	 */
4074103a8feaSLen Brown 
40755aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
40768209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4077812db3f7SLen Brown 	if (has_aperf) {
4078812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4079812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4080812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4081812db3f7SLen Brown 	}
4082889facbeSLen Brown 	do_dts = eax & (1 << 0);
4083812db3f7SLen Brown 	if (do_dts)
4084812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4085b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4086889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4087812db3f7SLen Brown 	if (do_ptm)
4088812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
40897f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
40907f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
40917f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
40927f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
40937f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4094889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4095889facbeSLen Brown 
409696e47158SLen Brown 	if (!quiet)
4097b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
40987f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
40997f5c258eSLen Brown 			has_aperf ? "" : "No-",
4100b3a34e93SLen Brown 			has_turbo ? "" : "No-",
41017f5c258eSLen Brown 			do_dts ? "" : "No-",
41027f5c258eSLen Brown 			do_ptm ? "" : "No-",
41037f5c258eSLen Brown 			has_hwp ? "" : "No-",
41047f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
41057f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
41067f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
41077f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
41087f5c258eSLen Brown 			has_epb ? "" : "No-");
4109103a8feaSLen Brown 
411096e47158SLen Brown 	if (!quiet)
411169807a63SLen Brown 		decode_misc_enable_msr();
411269807a63SLen Brown 
411333148d67SLen Brown 
411496e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4115aa8d8cc7SLen Brown 		int has_sgx;
4116aa8d8cc7SLen Brown 
4117aa8d8cc7SLen Brown 		ecx = 0;
4118aa8d8cc7SLen Brown 
4119aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4120aa8d8cc7SLen Brown 
4121aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4122aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4123aa8d8cc7SLen Brown 
4124aa8d8cc7SLen Brown 		if (has_sgx)
4125aa8d8cc7SLen Brown 			decode_feature_control_msr();
4126aa8d8cc7SLen Brown 	}
4127aa8d8cc7SLen Brown 
412861a87ba7SLen Brown 	if (max_level >= 0x15) {
41298a5bdf41SLen Brown 		unsigned int eax_crystal;
41308a5bdf41SLen Brown 		unsigned int ebx_tsc;
41318a5bdf41SLen Brown 
41328a5bdf41SLen Brown 		/*
41338a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
41348a5bdf41SLen Brown 		 */
41358a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
41365aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
41378a5bdf41SLen Brown 
41388a5bdf41SLen Brown 		if (ebx_tsc != 0) {
41398a5bdf41SLen Brown 
414096e47158SLen Brown 			if (!quiet && (ebx != 0))
4141b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
41428a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
41438a5bdf41SLen Brown 
41448a5bdf41SLen Brown 			if (crystal_hz == 0)
41458a5bdf41SLen Brown 				switch(model) {
4146869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4147869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4148869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4149869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4150e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4151e8efbc80SLen Brown 					break;
4152869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_X:	/* SKX */
41537268d407SLen Brown 				case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
4154ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4155ec53e594SLen Brown 					break;
4156869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4157ac01ac13SLen Brown 				case INTEL_FAM6_ATOM_GEMINI_LAKE:
4158e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
41598a5bdf41SLen Brown 					break;
41608a5bdf41SLen Brown 				default:
41618a5bdf41SLen Brown 					crystal_hz = 0;
41628a5bdf41SLen Brown 			}
41638a5bdf41SLen Brown 
41648a5bdf41SLen Brown 			if (crystal_hz) {
41658a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
416696e47158SLen Brown 				if (!quiet)
4167b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
41688a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
41698a5bdf41SLen Brown 			}
41708a5bdf41SLen Brown 		}
41718a5bdf41SLen Brown 	}
417261a87ba7SLen Brown 	if (max_level >= 0x16) {
417361a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
417461a87ba7SLen Brown 
417561a87ba7SLen Brown 		/*
417661a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
417761a87ba7SLen Brown 		 */
417861a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
417961a87ba7SLen Brown 
41805aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
418196e47158SLen Brown 		if (!quiet)
4182b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
418361a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
418461a87ba7SLen Brown 	}
41858a5bdf41SLen Brown 
4186b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4187b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4188b2b34dfeSHubert Chrzaniuk 
4189812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4190812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4191812db3f7SLen Brown 
4192812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4193812db3f7SLen Brown 		do_nhm_platform_info = 1;
4194812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4195812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4196812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4197812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4198812db3f7SLen Brown 	}
4199d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4200812db3f7SLen Brown 
4201812db3f7SLen Brown 	if (do_snb_cstates)
4202812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4203812db3f7SLen Brown 
42045a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
42050f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
42060f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
42070f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
42080f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
42090f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
42100f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
42110f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
42120f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
42130539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
42140f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
42150f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
42160f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
42170f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
42180539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
42190539ba11SLen Brown 		use_c1_residency_msr = 1;
42200539ba11SLen Brown 	}
42217170a374SLen Brown 	if (is_dnv(family, model)) {
42227170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
42237170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
42247170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
42257170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
42267170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
42277170a374SLen Brown 		use_c1_residency_msr = 1;
42287170a374SLen Brown 	}
422934c76197SLen Brown 	if (is_skx(family, model)) {
423034c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
423134c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
423234c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
423334c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
423434c76197SLen Brown 	}
4235ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4236ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4237ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4238ade0ebacSLen Brown 	}
42390f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
42400f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
42410f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
42420f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
42430f47c08dSLen Brown 	}
42445a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
4245a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4246a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4247a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4248a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4249a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4250a99d8730SLen Brown 	}
4251144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4252fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4253103a8feaSLen Brown 
425496e47158SLen Brown 	if (!quiet)
4255f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4256f0057310SLen Brown 
425796e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
425871616c8eSLen Brown 		decode_c6_demotion_policy_msr();
425971616c8eSLen Brown 
4260889facbeSLen Brown 	rapl_probe(family, model);
42613a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4262889facbeSLen Brown 
426396e47158SLen Brown 	if (!quiet)
42641b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4265fcd17211SLen Brown 
426641618e63SLen Brown 	if (!quiet)
426741618e63SLen Brown 		dump_sysfs_cstate_config();
42687293fccdSLen Brown 	if (!quiet)
42697293fccdSLen Brown 		dump_sysfs_pstate_config();
427041618e63SLen Brown 
4271a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4272a2b7b749SLen Brown 		calculate_tsc_tweak();
4273a2b7b749SLen Brown 
4274812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4275812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4276fdf676e5SLen Brown 
4277812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4278812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
427927d47356SLen Brown 
428096e47158SLen Brown 	if (!quiet)
428133148d67SLen Brown 		decode_misc_feature_control();
428233148d67SLen Brown 
4283889facbeSLen Brown 	return;
4284103a8feaSLen Brown }
4285103a8feaSLen Brown 
4286103a8feaSLen Brown 
4287103a8feaSLen Brown /*
4288103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4289103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4290103a8feaSLen Brown  */
4291103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4292103a8feaSLen Brown {
4293103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4294103a8feaSLen Brown 		return 1;
4295103a8feaSLen Brown 	else
4296103a8feaSLen Brown 		return 0;
4297103a8feaSLen Brown }
4298103a8feaSLen Brown 
4299103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4300103a8feaSLen Brown {
4301103a8feaSLen Brown 	return 0;
4302103a8feaSLen Brown }
4303103a8feaSLen Brown 
4304c98d5d94SLen Brown void topology_probe()
4305c98d5d94SLen Brown {
4306c98d5d94SLen Brown 	int i;
4307c98d5d94SLen Brown 	int max_core_id = 0;
4308c98d5d94SLen Brown 	int max_package_id = 0;
4309c98d5d94SLen Brown 	int max_siblings = 0;
4310c98d5d94SLen Brown 	struct cpu_topology {
4311c98d5d94SLen Brown 		int core_id;
4312c98d5d94SLen Brown 		int physical_package_id;
4313c98d5d94SLen Brown 	} *cpus;
4314c98d5d94SLen Brown 
4315c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4316c98d5d94SLen Brown 	topo.num_cpus = 0;
4317c98d5d94SLen Brown 	topo.max_cpu_num = 0;
4318c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4319c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4320812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4321c98d5d94SLen Brown 
4322d8af6f5fSLen Brown 	if (debug > 1)
4323b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4324c98d5d94SLen Brown 
4325c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4326b2c95d90SJosh Triplett 	if (cpus == NULL)
4327b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4328c98d5d94SLen Brown 
4329c98d5d94SLen Brown 	/*
4330c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4331c98d5d94SLen Brown 	 */
4332c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4333b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4334b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4335c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4336c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4337c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4338c98d5d94SLen Brown 
4339c98d5d94SLen Brown 	/*
43401ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
43411ef7d21aSLen Brown 	 */
43421ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
43431ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
43441ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
43451ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
43461ef7d21aSLen Brown 	}
43471ef7d21aSLen Brown 
43481ef7d21aSLen Brown 	/*
4349c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4350c98d5d94SLen Brown 	 */
4351c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4352b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4353b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4354c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4355c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4356c98d5d94SLen Brown 
4357c98d5d94SLen Brown 
4358c98d5d94SLen Brown 	/*
4359c98d5d94SLen Brown 	 * For online cpus
4360c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4361c98d5d94SLen Brown 	 */
4362c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4363c98d5d94SLen Brown 		int siblings;
4364c98d5d94SLen Brown 
4365c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4366d8af6f5fSLen Brown 			if (debug > 1)
4367b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4368c98d5d94SLen Brown 			continue;
4369c98d5d94SLen Brown 		}
4370c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
4371c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
4372c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
4373c98d5d94SLen Brown 
4374c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4375c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4376c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4377c98d5d94SLen Brown 
4378c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
4379c98d5d94SLen Brown 		if (siblings > max_siblings)
4380c98d5d94SLen Brown 			max_siblings = siblings;
4381d8af6f5fSLen Brown 		if (debug > 1)
4382b7d8c148SLen Brown 			fprintf(outf, "cpu %d pkg %d core %d\n",
4383c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
4384c98d5d94SLen Brown 	}
4385c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
4386d8af6f5fSLen Brown 	if (debug > 1)
4387b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
4388c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
43890f47c08dSLen Brown 	if (!summary_only && topo.num_cores_per_pkg > 1)
4390812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
4391c98d5d94SLen Brown 
4392c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
4393d8af6f5fSLen Brown 	if (debug > 1)
4394b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4395c98d5d94SLen Brown 			max_package_id, topo.num_packages);
43967da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
4397812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
4398c98d5d94SLen Brown 
4399c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
4400d8af6f5fSLen Brown 	if (debug > 1)
4401b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
4402c98d5d94SLen Brown 
4403c98d5d94SLen Brown 	free(cpus);
4404c98d5d94SLen Brown }
4405c98d5d94SLen Brown 
4406c98d5d94SLen Brown void
4407c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4408c98d5d94SLen Brown {
4409c98d5d94SLen Brown 	int i;
4410c98d5d94SLen Brown 
4411c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
4412678a3bd1SLen Brown 		topo.num_packages, sizeof(struct thread_data));
4413c98d5d94SLen Brown 	if (*t == NULL)
4414c98d5d94SLen Brown 		goto error;
4415c98d5d94SLen Brown 
4416c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
4417c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
4418c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
4419c98d5d94SLen Brown 
4420c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
4421678a3bd1SLen Brown 		sizeof(struct core_data));
4422c98d5d94SLen Brown 	if (*c == NULL)
4423c98d5d94SLen Brown 		goto error;
4424c98d5d94SLen Brown 
4425c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4426c98d5d94SLen Brown 		(*c)[i].core_id = -1;
4427c98d5d94SLen Brown 
4428678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
4429c98d5d94SLen Brown 	if (*p == NULL)
4430c98d5d94SLen Brown 		goto error;
4431c98d5d94SLen Brown 
4432c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
4433c98d5d94SLen Brown 		(*p)[i].package_id = i;
4434c98d5d94SLen Brown 
4435c98d5d94SLen Brown 	return;
4436c98d5d94SLen Brown error:
4437b2c95d90SJosh Triplett 	err(1, "calloc counters");
4438c98d5d94SLen Brown }
4439c98d5d94SLen Brown /*
4440c98d5d94SLen Brown  * init_counter()
4441c98d5d94SLen Brown  *
4442c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
4443c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4444c98d5d94SLen Brown  *
4445c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
4446c98d5d94SLen Brown  */
4447c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4448c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
4449c98d5d94SLen Brown 	int pkg_num, int cpu_id)
4450c98d5d94SLen Brown {
4451c98d5d94SLen Brown 	struct thread_data *t;
4452c98d5d94SLen Brown 	struct core_data *c;
4453c98d5d94SLen Brown 	struct pkg_data *p;
4454c98d5d94SLen Brown 
4455c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4456c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
4457c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
4458c98d5d94SLen Brown 
4459c98d5d94SLen Brown 	t->cpu_id = cpu_id;
4460c98d5d94SLen Brown 	if (thread_num == 0) {
4461c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4462c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
4463c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4464c98d5d94SLen Brown 	}
4465c98d5d94SLen Brown 
4466c98d5d94SLen Brown 	c->core_id = core_num;
4467c98d5d94SLen Brown 	p->package_id = pkg_num;
4468c98d5d94SLen Brown }
4469c98d5d94SLen Brown 
4470c98d5d94SLen Brown 
4471c98d5d94SLen Brown int initialize_counters(int cpu_id)
4472c98d5d94SLen Brown {
4473c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
4474c98d5d94SLen Brown 
4475c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
4476c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
4477e275b388SDasaratharaman Chandramouli 	my_thread_id = get_cpu_position_in_core(cpu_id);
4478e275b388SDasaratharaman Chandramouli 	if (!my_thread_id)
4479c98d5d94SLen Brown 		topo.num_cores++;
4480c98d5d94SLen Brown 
4481c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4482c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4483c98d5d94SLen Brown 	return 0;
4484c98d5d94SLen Brown }
4485c98d5d94SLen Brown 
4486c98d5d94SLen Brown void allocate_output_buffer()
4487c98d5d94SLen Brown {
44883b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4489c98d5d94SLen Brown 	outp = output_buffer;
4490b2c95d90SJosh Triplett 	if (outp == NULL)
4491b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
4492c98d5d94SLen Brown }
449336229897SLen Brown void allocate_fd_percpu(void)
449436229897SLen Brown {
449501a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
449636229897SLen Brown 	if (fd_percpu == NULL)
449736229897SLen Brown 		err(-1, "calloc fd_percpu");
449836229897SLen Brown }
4499562a2d37SLen Brown void allocate_irq_buffers(void)
4500562a2d37SLen Brown {
4501562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4502562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
4503562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
4504c98d5d94SLen Brown 
450501a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4506562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
450701a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
4508562a2d37SLen Brown }
4509c98d5d94SLen Brown void setup_all_buffers(void)
4510c98d5d94SLen Brown {
4511c98d5d94SLen Brown 	topology_probe();
4512562a2d37SLen Brown 	allocate_irq_buffers();
451336229897SLen Brown 	allocate_fd_percpu();
4514c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
4515c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
4516c98d5d94SLen Brown 	allocate_output_buffer();
4517c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
4518c98d5d94SLen Brown }
45193b4d5c7fSAndy Shevchenko 
45207ce7d5deSPrarit Bhargava void set_base_cpu(void)
45217ce7d5deSPrarit Bhargava {
45227ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
45237ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
45247ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
45257ce7d5deSPrarit Bhargava 
45267ce7d5deSPrarit Bhargava 	if (debug > 1)
4527b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
45287ce7d5deSPrarit Bhargava }
45297ce7d5deSPrarit Bhargava 
4530103a8feaSLen Brown void turbostat_init()
4531103a8feaSLen Brown {
45327ce7d5deSPrarit Bhargava 	setup_all_buffers();
45337ce7d5deSPrarit Bhargava 	set_base_cpu();
4534103a8feaSLen Brown 	check_dev_msr();
453598481e79SLen Brown 	check_permissions();
4536fcd17211SLen Brown 	process_cpuid();
4537103a8feaSLen Brown 
4538103a8feaSLen Brown 
453996e47158SLen Brown 	if (!quiet)
45407f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
45417f5c258eSLen Brown 
454296e47158SLen Brown 	if (!quiet)
4543889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
4544889facbeSLen Brown 
454596e47158SLen Brown 	if (!quiet)
45463a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
45473a9a941dSLen Brown 
454896e47158SLen Brown 	if (!quiet)
4549889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
4550889facbeSLen Brown 
4551889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
4552889facbeSLen Brown 
455396e47158SLen Brown 	if (!quiet)
4554889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
45555a63426eSLen Brown 
455696e47158SLen Brown 	if (!quiet && do_irtl_snb)
45575a63426eSLen Brown 		print_irtl();
4558103a8feaSLen Brown }
4559103a8feaSLen Brown 
4560103a8feaSLen Brown int fork_it(char **argv)
4561103a8feaSLen Brown {
4562103a8feaSLen Brown 	pid_t child_pid;
4563d91bb17cSLen Brown 	int status;
4564d15cf7c1SLen Brown 
4565218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
4566d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
4567d91bb17cSLen Brown 	if (status)
4568d91bb17cSLen Brown 		exit(status);
4569d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
4570d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
4571103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
4572103a8feaSLen Brown 
4573103a8feaSLen Brown 	child_pid = fork();
4574103a8feaSLen Brown 	if (!child_pid) {
4575103a8feaSLen Brown 		/* child */
4576103a8feaSLen Brown 		execvp(argv[0], argv);
45770815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
4578103a8feaSLen Brown 	} else {
4579103a8feaSLen Brown 
4580103a8feaSLen Brown 		/* parent */
4581b2c95d90SJosh Triplett 		if (child_pid == -1)
4582b2c95d90SJosh Triplett 			err(1, "fork");
4583103a8feaSLen Brown 
4584103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
4585103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
4586b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
4587b2c95d90SJosh Triplett 			err(status, "waitpid");
4588103a8feaSLen Brown 	}
4589c98d5d94SLen Brown 	/*
4590c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
4591c98d5d94SLen Brown 	 * because re-starting is problematic when forking
4592c98d5d94SLen Brown 	 */
4593218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
4594c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
4595103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
4596103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
4597ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4598ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
4599ba3dec99SLen Brown 	else {
4600c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
4601c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
4602ba3dec99SLen Brown 	}
4603103a8feaSLen Brown 
4604b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4605b7d8c148SLen Brown 
4606b7d8c148SLen Brown 	flush_output_stderr();
4607103a8feaSLen Brown 
4608d91bb17cSLen Brown 	return status;
4609103a8feaSLen Brown }
4610103a8feaSLen Brown 
46113b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
46123b4d5c7fSAndy Shevchenko {
46133b4d5c7fSAndy Shevchenko 	int status;
46143b4d5c7fSAndy Shevchenko 
4615218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
46163b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
46173b4d5c7fSAndy Shevchenko 	if (status)
46183b4d5c7fSAndy Shevchenko 		return status;
46193b4d5c7fSAndy Shevchenko 
46203b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
46213b4d5c7fSAndy Shevchenko 	if (status)
46223b4d5c7fSAndy Shevchenko 		return status;
46233b4d5c7fSAndy Shevchenko 
4624b7d8c148SLen Brown 	flush_output_stdout();
46253b4d5c7fSAndy Shevchenko 
46263b4d5c7fSAndy Shevchenko 	return status;
46273b4d5c7fSAndy Shevchenko }
46283b4d5c7fSAndy Shevchenko 
4629d8af6f5fSLen Brown void print_version() {
46305f9bf02aSLen Brown 	fprintf(outf, "turbostat version 17.04.12"
4631d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
4632d8af6f5fSLen Brown }
4633d8af6f5fSLen Brown 
4634495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
4635495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
463641618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
4637388e9c81SLen Brown {
4638388e9c81SLen Brown 	struct msr_counter *msrp;
4639388e9c81SLen Brown 
4640388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
4641388e9c81SLen Brown 	if (msrp == NULL) {
4642388e9c81SLen Brown 		perror("calloc");
4643388e9c81SLen Brown 		exit(1);
4644388e9c81SLen Brown 	}
4645388e9c81SLen Brown 
4646388e9c81SLen Brown 	msrp->msr_num = msr_num;
4647388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
4648495c7654SLen Brown 	if (path)
4649495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
4650388e9c81SLen Brown 	msrp->width = width;
4651388e9c81SLen Brown 	msrp->type = type;
4652388e9c81SLen Brown 	msrp->format = format;
465341618e63SLen Brown 	msrp->flags = flags;
4654388e9c81SLen Brown 
4655388e9c81SLen Brown 	switch (scope) {
4656388e9c81SLen Brown 
4657388e9c81SLen Brown 	case SCOPE_CPU:
4658388e9c81SLen Brown 		msrp->next = sys.tp;
4659388e9c81SLen Brown 		sys.tp = msrp;
4660678a3bd1SLen Brown 		sys.added_thread_counters++;
4661678a3bd1SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4662678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
4663678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4664678a3bd1SLen Brown 			exit(-1);
4665678a3bd1SLen Brown 		}
4666388e9c81SLen Brown 		break;
4667388e9c81SLen Brown 
4668388e9c81SLen Brown 	case SCOPE_CORE:
4669388e9c81SLen Brown 		msrp->next = sys.cp;
4670388e9c81SLen Brown 		sys.cp = msrp;
4671678a3bd1SLen Brown 		sys.added_core_counters++;
4672678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4673678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
4674678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4675678a3bd1SLen Brown 			exit(-1);
4676678a3bd1SLen Brown 		}
4677388e9c81SLen Brown 		break;
4678388e9c81SLen Brown 
4679388e9c81SLen Brown 	case SCOPE_PACKAGE:
4680388e9c81SLen Brown 		msrp->next = sys.pp;
4681388e9c81SLen Brown 		sys.pp = msrp;
4682678a3bd1SLen Brown 		sys.added_package_counters++;
4683678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4684678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
4685678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4686678a3bd1SLen Brown 			exit(-1);
4687678a3bd1SLen Brown 		}
4688388e9c81SLen Brown 		break;
4689388e9c81SLen Brown 	}
4690388e9c81SLen Brown 
4691388e9c81SLen Brown 	return 0;
4692388e9c81SLen Brown }
4693388e9c81SLen Brown 
4694388e9c81SLen Brown void parse_add_command(char *add_command)
4695388e9c81SLen Brown {
4696388e9c81SLen Brown 	int msr_num = 0;
4697495c7654SLen Brown 	char *path = NULL;
46980f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
4699388e9c81SLen Brown 	int width = 64;
4700388e9c81SLen Brown 	int fail = 0;
4701388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
4702388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
4703388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
4704388e9c81SLen Brown 
4705388e9c81SLen Brown 	while (add_command) {
4706388e9c81SLen Brown 
4707388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4708388e9c81SLen Brown 			goto next;
4709388e9c81SLen Brown 
4710388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
4711388e9c81SLen Brown 			goto next;
4712388e9c81SLen Brown 
4713495c7654SLen Brown 		if (*add_command == '/') {
4714495c7654SLen Brown 			path = add_command;
4715495c7654SLen Brown 			goto next;
4716495c7654SLen Brown 		}
4717495c7654SLen Brown 
4718388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
4719388e9c81SLen Brown 			if ((width == 32) || (width == 64))
4720388e9c81SLen Brown 				goto next;
4721388e9c81SLen Brown 			width = 64;
4722388e9c81SLen Brown 		}
4723388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4724388e9c81SLen Brown 			scope = SCOPE_CPU;
4725388e9c81SLen Brown 			goto next;
4726388e9c81SLen Brown 		}
4727388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
4728388e9c81SLen Brown 			scope = SCOPE_CORE;
4729388e9c81SLen Brown 			goto next;
4730388e9c81SLen Brown 		}
4731388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
4732388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
4733388e9c81SLen Brown 			goto next;
4734388e9c81SLen Brown 		}
4735388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4736388e9c81SLen Brown 			type = COUNTER_CYCLES;
4737388e9c81SLen Brown 			goto next;
4738388e9c81SLen Brown 		}
4739388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4740388e9c81SLen Brown 			type = COUNTER_SECONDS;
4741388e9c81SLen Brown 			goto next;
4742388e9c81SLen Brown 		}
474341618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
474441618e63SLen Brown 			type = COUNTER_USEC;
474541618e63SLen Brown 			goto next;
474641618e63SLen Brown 		}
4747388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
4748388e9c81SLen Brown 			format = FORMAT_RAW;
4749388e9c81SLen Brown 			goto next;
4750388e9c81SLen Brown 		}
4751388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
4752388e9c81SLen Brown 			format = FORMAT_DELTA;
4753388e9c81SLen Brown 			goto next;
4754388e9c81SLen Brown 		}
4755388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
4756388e9c81SLen Brown 			format = FORMAT_PERCENT;
4757388e9c81SLen Brown 			goto next;
4758388e9c81SLen Brown 		}
4759388e9c81SLen Brown 
4760388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
4761388e9c81SLen Brown 			char *eos;
4762388e9c81SLen Brown 
4763388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
4764388e9c81SLen Brown 			if (eos)
4765388e9c81SLen Brown 				*eos = '\0';
4766388e9c81SLen Brown 			goto next;
4767388e9c81SLen Brown 		}
4768388e9c81SLen Brown 
4769388e9c81SLen Brown next:
4770388e9c81SLen Brown 		add_command = strchr(add_command, ',');
4771495c7654SLen Brown 		if (add_command) {
4772495c7654SLen Brown 			*add_command = '\0';
4773388e9c81SLen Brown 			add_command++;
4774495c7654SLen Brown 		}
4775388e9c81SLen Brown 
4776388e9c81SLen Brown 	}
4777495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
4778495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
4779388e9c81SLen Brown 		fail++;
4780388e9c81SLen Brown 	}
4781388e9c81SLen Brown 
4782388e9c81SLen Brown 	/* generate default column header */
4783388e9c81SLen Brown 	if (*name_buffer == '\0') {
4784388e9c81SLen Brown 		if (width == 32)
47855f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4786388e9c81SLen Brown 		else
47875f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4788388e9c81SLen Brown 	}
4789388e9c81SLen Brown 
479041618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
4791388e9c81SLen Brown 		fail++;
4792388e9c81SLen Brown 
4793388e9c81SLen Brown 	if (fail) {
4794388e9c81SLen Brown 		help();
4795388e9c81SLen Brown 		exit(1);
4796388e9c81SLen Brown 	}
4797388e9c81SLen Brown }
479841618e63SLen Brown 
4799dd778a5eSLen Brown int is_deferred_skip(char *name)
4800dd778a5eSLen Brown {
4801dd778a5eSLen Brown 	int i;
4802dd778a5eSLen Brown 
4803dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
4804dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
4805dd778a5eSLen Brown 			return 1;
4806dd778a5eSLen Brown 	return 0;
4807dd778a5eSLen Brown }
4808dd778a5eSLen Brown 
480941618e63SLen Brown void probe_sysfs(void)
481041618e63SLen Brown {
481141618e63SLen Brown 	char path[64];
481241618e63SLen Brown 	char name_buf[16];
481341618e63SLen Brown 	FILE *input;
481441618e63SLen Brown 	int state;
481541618e63SLen Brown 	char *sp;
481641618e63SLen Brown 
481741618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
481841618e63SLen Brown 		return;
481941618e63SLen Brown 
482041618e63SLen Brown 	for (state = 10; state > 0; --state) {
482141618e63SLen Brown 
482241618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
482341618e63SLen Brown 			base_cpu, state);
482441618e63SLen Brown 		input = fopen(path, "r");
482541618e63SLen Brown 		if (input == NULL)
482641618e63SLen Brown 			continue;
482741618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
482841618e63SLen Brown 
482941618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
483041618e63SLen Brown 		sp = strchr(name_buf, '-');
483141618e63SLen Brown 		if (!sp)
483241618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
483341618e63SLen Brown 		*sp = '%';
483441618e63SLen Brown 		*(sp + 1) = '\0';
483541618e63SLen Brown 
483641618e63SLen Brown 		fclose(input);
483741618e63SLen Brown 
483841618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
483941618e63SLen Brown 
4840dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
4841dd778a5eSLen Brown 			continue;
4842dd778a5eSLen Brown 
484341618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
484441618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
484541618e63SLen Brown 	}
484641618e63SLen Brown 
484741618e63SLen Brown 	for (state = 10; state > 0; --state) {
484841618e63SLen Brown 
484941618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
485041618e63SLen Brown 			base_cpu, state);
485141618e63SLen Brown 		input = fopen(path, "r");
485241618e63SLen Brown 		if (input == NULL)
485341618e63SLen Brown 			continue;
485441618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
485541618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
485641618e63SLen Brown 		sp = strchr(name_buf, '-');
485741618e63SLen Brown 		if (!sp)
485841618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
485941618e63SLen Brown 		*sp = '\0';
486041618e63SLen Brown 		fclose(input);
486141618e63SLen Brown 
486241618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
486341618e63SLen Brown 
4864dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
4865dd778a5eSLen Brown 			continue;
4866dd778a5eSLen Brown 
486741618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
486841618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
486941618e63SLen Brown 	}
487041618e63SLen Brown 
487141618e63SLen Brown }
487241618e63SLen Brown 
48731ef7d21aSLen Brown 
48741ef7d21aSLen Brown /*
48751ef7d21aSLen Brown  * parse cpuset with following syntax
48761ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
48771ef7d21aSLen Brown  */
48781ef7d21aSLen Brown void parse_cpu_command(char *optarg)
48791ef7d21aSLen Brown {
48801ef7d21aSLen Brown 	unsigned int start, end;
48811ef7d21aSLen Brown 	char *next;
48821ef7d21aSLen Brown 
48834e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
48844e4e1e7cSLen Brown 		if (cpu_subset)
48854e4e1e7cSLen Brown 			goto error;
48864e4e1e7cSLen Brown 		show_core_only++;
48874e4e1e7cSLen Brown 		return;
48884e4e1e7cSLen Brown 	}
48894e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
48904e4e1e7cSLen Brown 		if (cpu_subset)
48914e4e1e7cSLen Brown 			goto error;
48924e4e1e7cSLen Brown 		show_pkg_only++;
48934e4e1e7cSLen Brown 		return;
48944e4e1e7cSLen Brown 	}
48954e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
48964e4e1e7cSLen Brown 		goto error;
48974e4e1e7cSLen Brown 
48981ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
48991ef7d21aSLen Brown 	if (cpu_subset == NULL)
49001ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
49011ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
49021ef7d21aSLen Brown 
49031ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
49041ef7d21aSLen Brown 
49051ef7d21aSLen Brown 	next = optarg;
49061ef7d21aSLen Brown 
49071ef7d21aSLen Brown 	while (next && *next) {
49081ef7d21aSLen Brown 
49091ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
49101ef7d21aSLen Brown 			goto error;
49111ef7d21aSLen Brown 
49121ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
49131ef7d21aSLen Brown 
49141ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
49151ef7d21aSLen Brown 			goto error;
49161ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
49171ef7d21aSLen Brown 
49181ef7d21aSLen Brown 		if (*next == '\0')
49191ef7d21aSLen Brown 			break;
49201ef7d21aSLen Brown 
49211ef7d21aSLen Brown 		if (*next == ',') {
49221ef7d21aSLen Brown 			next += 1;
49231ef7d21aSLen Brown 			continue;
49241ef7d21aSLen Brown 		}
49251ef7d21aSLen Brown 
49261ef7d21aSLen Brown 		if (*next == '-') {
49271ef7d21aSLen Brown 			next += 1;	/* start range */
49281ef7d21aSLen Brown 		} else if (*next == '.') {
49291ef7d21aSLen Brown 			next += 1;
49301ef7d21aSLen Brown 			if (*next == '.')
49311ef7d21aSLen Brown 				next += 1;	/* start range */
49321ef7d21aSLen Brown 			else
49331ef7d21aSLen Brown 				goto error;
49341ef7d21aSLen Brown 		}
49351ef7d21aSLen Brown 
49361ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
49371ef7d21aSLen Brown 		if (end <= start)
49381ef7d21aSLen Brown 			goto error;
49391ef7d21aSLen Brown 
49401ef7d21aSLen Brown 		while (++start <= end) {
49411ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
49421ef7d21aSLen Brown 				goto error;
49431ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
49441ef7d21aSLen Brown 		}
49451ef7d21aSLen Brown 
49461ef7d21aSLen Brown 		if (*next == ',')
49471ef7d21aSLen Brown 			next += 1;
49481ef7d21aSLen Brown 		else if (*next != '\0')
49491ef7d21aSLen Brown 			goto error;
49501ef7d21aSLen Brown 	}
49511ef7d21aSLen Brown 
49521ef7d21aSLen Brown 	return;
49531ef7d21aSLen Brown 
49541ef7d21aSLen Brown error:
49554e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
49564e4e1e7cSLen Brown 	help();
49571ef7d21aSLen Brown 	exit(-1);
49581ef7d21aSLen Brown }
49591ef7d21aSLen Brown 
4960812db3f7SLen Brown int shown;
4961812db3f7SLen Brown /*
4962812db3f7SLen Brown  * parse_show_hide() - process cmdline to set default counter action
4963812db3f7SLen Brown  */
4964812db3f7SLen Brown void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4965812db3f7SLen Brown {
4966812db3f7SLen Brown 	/*
4967812db3f7SLen Brown 	 * --show: show only those specified
4968812db3f7SLen Brown 	 *  The 1st invocation will clear and replace the enabled mask
4969812db3f7SLen Brown 	 *  subsequent invocations can add to it.
4970812db3f7SLen Brown 	 */
4971812db3f7SLen Brown 	if (new_mode == SHOW_LIST) {
4972812db3f7SLen Brown 		if (shown == 0)
4973dd778a5eSLen Brown 			bic_enabled = bic_lookup(optarg, new_mode);
4974812db3f7SLen Brown 		else
4975dd778a5eSLen Brown 			bic_enabled |= bic_lookup(optarg, new_mode);
4976812db3f7SLen Brown 		shown = 1;
4977812db3f7SLen Brown 
4978812db3f7SLen Brown 		return;
4979812db3f7SLen Brown 	}
4980812db3f7SLen Brown 
4981812db3f7SLen Brown 	/*
4982812db3f7SLen Brown 	 * --hide: do not show those specified
4983812db3f7SLen Brown 	 *  multiple invocations simply clear more bits in enabled mask
4984812db3f7SLen Brown 	 */
4985dd778a5eSLen Brown 	bic_enabled &= ~bic_lookup(optarg, new_mode);
498641618e63SLen Brown 
4987812db3f7SLen Brown }
4988812db3f7SLen Brown 
4989103a8feaSLen Brown void cmdline(int argc, char **argv)
4990103a8feaSLen Brown {
4991103a8feaSLen Brown 	int opt;
4992d8af6f5fSLen Brown 	int option_index = 0;
4993d8af6f5fSLen Brown 	static struct option long_options[] = {
4994388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
49951ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
4996d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
499796e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
4998d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
4999d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5000812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5001d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5002c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5003b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
500496e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5005812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5006d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5007d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5008d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5009d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5010d8af6f5fSLen Brown 	};
5011103a8feaSLen Brown 
5012103a8feaSLen Brown 	progname = argv[0];
5013103a8feaSLen Brown 
50144e4e1e7cSLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:qST:v",
5015d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5016103a8feaSLen Brown 		switch (opt) {
5017388e9c81SLen Brown 		case 'a':
5018388e9c81SLen Brown 			parse_add_command(optarg);
5019388e9c81SLen Brown 			break;
50201ef7d21aSLen Brown 		case 'c':
50211ef7d21aSLen Brown 			parse_cpu_command(optarg);
50221ef7d21aSLen Brown 			break;
5023d8af6f5fSLen Brown 		case 'D':
5024d8af6f5fSLen Brown 			dump_only++;
50258e180f3cSLen Brown 			break;
5026d8af6f5fSLen Brown 		case 'd':
5027d8af6f5fSLen Brown 			debug++;
50282f32edf1SLen Brown 			break;
5029812db3f7SLen Brown 		case 'H':
5030812db3f7SLen Brown 			parse_show_hide(optarg, HIDE_LIST);
5031812db3f7SLen Brown 			break;
5032d8af6f5fSLen Brown 		case 'h':
5033d8af6f5fSLen Brown 		default:
5034d8af6f5fSLen Brown 			help();
5035d8af6f5fSLen Brown 			exit(1);
5036d8af6f5fSLen Brown 		case 'i':
50372a0609c0SLen Brown 			{
50382a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
50392a0609c0SLen Brown 
50402a0609c0SLen Brown 				if (interval < 0.001) {
5041b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
50422a0609c0SLen Brown 						interval);
50432a0609c0SLen Brown 					exit(2);
50442a0609c0SLen Brown 				}
50452a0609c0SLen Brown 
50462a0609c0SLen Brown 				interval_ts.tv_sec = interval;
50472a0609c0SLen Brown 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
50482a0609c0SLen Brown 			}
5049889facbeSLen Brown 			break;
50505c56be9aSDirk Brandewie 		case 'J':
50515c56be9aSDirk Brandewie 			rapl_joules++;
50525c56be9aSDirk Brandewie 			break;
5053c8ade361SLen Brown 		case 'l':
5054c8ade361SLen Brown 			list_header_only++;
5055c8ade361SLen Brown 			quiet++;
5056c8ade361SLen Brown 			break;
5057b7d8c148SLen Brown 		case 'o':
5058b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5059b7d8c148SLen Brown 			break;
506096e47158SLen Brown 		case 'q':
506196e47158SLen Brown 			quiet = 1;
506296e47158SLen Brown 			break;
5063812db3f7SLen Brown 		case 's':
5064812db3f7SLen Brown 			parse_show_hide(optarg, SHOW_LIST);
5065812db3f7SLen Brown 			break;
5066d8af6f5fSLen Brown 		case 'S':
5067d8af6f5fSLen Brown 			summary_only++;
5068d8af6f5fSLen Brown 			break;
5069d8af6f5fSLen Brown 		case 'T':
5070d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5071d8af6f5fSLen Brown 			break;
5072d8af6f5fSLen Brown 		case 'v':
5073d8af6f5fSLen Brown 			print_version();
5074d8af6f5fSLen Brown 			exit(0);
5075d8af6f5fSLen Brown 			break;
5076103a8feaSLen Brown 		}
5077103a8feaSLen Brown 	}
5078103a8feaSLen Brown }
5079103a8feaSLen Brown 
5080103a8feaSLen Brown int main(int argc, char **argv)
5081103a8feaSLen Brown {
5082b7d8c148SLen Brown 	outf = stderr;
5083b7d8c148SLen Brown 
5084103a8feaSLen Brown 	cmdline(argc, argv);
5085103a8feaSLen Brown 
508696e47158SLen Brown 	if (!quiet)
5087d8af6f5fSLen Brown 		print_version();
5088103a8feaSLen Brown 
508941618e63SLen Brown 	probe_sysfs();
509041618e63SLen Brown 
5091103a8feaSLen Brown 	turbostat_init();
5092103a8feaSLen Brown 
50933b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
50943b4d5c7fSAndy Shevchenko 	if (dump_only)
50953b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
50963b4d5c7fSAndy Shevchenko 
5097c8ade361SLen Brown 	/* list header and exit */
5098c8ade361SLen Brown 	if (list_header_only) {
5099c8ade361SLen Brown 		print_header(",");
5100c8ade361SLen Brown 		flush_output_stdout();
5101c8ade361SLen Brown 		return 0;
5102c8ade361SLen Brown 	}
5103c8ade361SLen Brown 
5104103a8feaSLen Brown 	/*
5105103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5106103a8feaSLen Brown 	 */
5107103a8feaSLen Brown 	if (argc - optind)
5108103a8feaSLen Brown 		return fork_it(argv + optind);
5109103a8feaSLen Brown 	else
5110103a8feaSLen Brown 		turbostat_loop();
5111103a8feaSLen Brown 
5112103a8feaSLen Brown 	return 0;
5113103a8feaSLen Brown }
5114