xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 678a3bd1b3de6d2ebf604e7d708bc8150bb667e9)
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;
52d8af6f5fSLen Brown unsigned int rapl_joules;
53d8af6f5fSLen Brown unsigned int summary_only;
54d8af6f5fSLen Brown unsigned int dump_only;
55103a8feaSLen Brown unsigned int do_nhm_cstates;
56103a8feaSLen Brown unsigned int do_snb_cstates;
57fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
58ee7e38e3SLen Brown unsigned int do_pc2;
59ee7e38e3SLen Brown unsigned int do_pc3;
60ee7e38e3SLen Brown unsigned int do_pc6;
61ee7e38e3SLen Brown unsigned int do_pc7;
62ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10;
630b2bb692SLen Brown unsigned int do_skl_residency;
64144b44b1SLen Brown unsigned int do_slm_cstates;
65144b44b1SLen Brown unsigned int use_c1_residency_msr;
66103a8feaSLen Brown unsigned int has_aperf;
67889facbeSLen Brown unsigned int has_epb;
685a63426eSLen Brown unsigned int do_irtl_snb;
695a63426eSLen Brown unsigned int do_irtl_hsw;
70fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
71103a8feaSLen Brown unsigned int genuine_intel;
72103a8feaSLen Brown unsigned int has_invariant_tsc;
73d7899447SLen Brown unsigned int do_nhm_platform_info;
74b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
75562a2d37SLen Brown int do_irq = 1;
761ed51011SLen Brown int do_smi;
77103a8feaSLen Brown double bclk;
78a2b7b749SLen Brown double base_hz;
7921ed5574SLen Brown unsigned int has_base_hz;
80a2b7b749SLen Brown double tsc_tweak = 1.0;
81103a8feaSLen Brown unsigned int show_pkg;
82103a8feaSLen Brown unsigned int show_core;
83103a8feaSLen Brown unsigned int show_cpu;
84c98d5d94SLen Brown unsigned int show_pkg_only;
85c98d5d94SLen Brown unsigned int show_core_only;
86c98d5d94SLen Brown char *output_buffer, *outp;
87889facbeSLen Brown unsigned int do_rapl;
88889facbeSLen Brown unsigned int do_dts;
89889facbeSLen Brown unsigned int do_ptm;
90fdf676e5SLen Brown unsigned int do_gfx_rc6_ms;
91fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
9227d47356SLen Brown unsigned int do_gfx_mhz;
9327d47356SLen Brown unsigned int gfx_cur_mhz;
94889facbeSLen Brown unsigned int tcc_activation_temp;
95889facbeSLen Brown unsigned int tcc_activation_temp_override;
9640ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
9740ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
98889facbeSLen Brown double rapl_joule_counter_range;
993a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
1003a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
1013a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
1028a5bdf41SLen Brown unsigned int crystal_hz;
1038a5bdf41SLen Brown unsigned long long tsc_hz;
1047ce7d5deSPrarit Bhargava int base_cpu;
10521ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
1067f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
1077f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
1087f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
1097f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1107f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1117f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
112889facbeSLen Brown 
113889facbeSLen Brown #define RAPL_PKG		(1 << 0)
114e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
115e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
116e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
117e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
118e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
119e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
120e6f9bb3cSLen Brown 
121889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
122e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
123e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
124e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
125e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1260b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1270b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
128e6f9bb3cSLen Brown 
1299148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
130e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1310b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
132e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
133e6f9bb3cSLen Brown 
1340b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
135e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
136e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
137e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1389148494cSJacob Pan 
1399148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1409148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1419148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
142889facbeSLen Brown #define	TJMAX_DEFAULT	100
143889facbeSLen Brown 
144889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
145103a8feaSLen Brown 
146388e9c81SLen Brown /*
147388e9c81SLen Brown  * buffer size used by sscanf() for added column names
148388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
149388e9c81SLen Brown  */
150388e9c81SLen Brown #define	NAME_BYTES 20
151388e9c81SLen Brown 
152103a8feaSLen Brown int backwards_count;
153103a8feaSLen Brown char *progname;
154103a8feaSLen Brown 
155c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set;
156c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize;
157*678a3bd1SLen Brown #define MAX_ADDED_COUNTERS 16
158c98d5d94SLen Brown 
159c98d5d94SLen Brown struct thread_data {
160c98d5d94SLen Brown 	unsigned long long tsc;
161c98d5d94SLen Brown 	unsigned long long aperf;
162c98d5d94SLen Brown 	unsigned long long mperf;
163144b44b1SLen Brown 	unsigned long long c1;
164562a2d37SLen Brown 	unsigned int irq_count;
1651ed51011SLen Brown 	unsigned int smi_count;
166c98d5d94SLen Brown 	unsigned int cpu_id;
167c98d5d94SLen Brown 	unsigned int flags;
168c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
169c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
170*678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
171c98d5d94SLen Brown } *thread_even, *thread_odd;
172c98d5d94SLen Brown 
173c98d5d94SLen Brown struct core_data {
174c98d5d94SLen Brown 	unsigned long long c3;
175c98d5d94SLen Brown 	unsigned long long c6;
176c98d5d94SLen Brown 	unsigned long long c7;
177889facbeSLen Brown 	unsigned int core_temp_c;
178c98d5d94SLen Brown 	unsigned int core_id;
179*678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
180c98d5d94SLen Brown } *core_even, *core_odd;
181c98d5d94SLen Brown 
182c98d5d94SLen Brown struct pkg_data {
183c98d5d94SLen Brown 	unsigned long long pc2;
184c98d5d94SLen Brown 	unsigned long long pc3;
185c98d5d94SLen Brown 	unsigned long long pc6;
186c98d5d94SLen Brown 	unsigned long long pc7;
187ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
188ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
189ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1900b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1910b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1920b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1930b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
1949185e988SLen Brown 	long long gfx_rc6_ms;
19527d47356SLen Brown 	unsigned int gfx_mhz;
196c98d5d94SLen Brown 	unsigned int package_id;
197889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
198889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
199889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
200889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
201889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
202889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
203889facbeSLen Brown 	unsigned int pkg_temp_c;
204*678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
205c98d5d94SLen Brown } *package_even, *package_odd;
206c98d5d94SLen Brown 
207c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
208c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
209c98d5d94SLen Brown 
210c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
211c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
212c98d5d94SLen Brown 		topo.num_threads_per_core + \
213c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
214c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
215c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
216c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
217c98d5d94SLen Brown 
218388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
219388e9c81SLen Brown enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
220388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
221388e9c81SLen Brown 
222388e9c81SLen Brown struct msr_counter {
223388e9c81SLen Brown 	unsigned int msr_num;
224388e9c81SLen Brown 	char name[NAME_BYTES];
225388e9c81SLen Brown 	unsigned int width;
226388e9c81SLen Brown 	enum counter_type type;
227388e9c81SLen Brown 	enum counter_format format;
228388e9c81SLen Brown 	struct msr_counter *next;
229388e9c81SLen Brown };
230388e9c81SLen Brown 
231388e9c81SLen Brown struct sys_counters {
232*678a3bd1SLen Brown 	unsigned int added_thread_counters;
233*678a3bd1SLen Brown 	unsigned int added_core_counters;
234*678a3bd1SLen Brown 	unsigned int added_package_counters;
235388e9c81SLen Brown 	struct msr_counter *tp;
236388e9c81SLen Brown 	struct msr_counter *cp;
237388e9c81SLen Brown 	struct msr_counter *pp;
238388e9c81SLen Brown } sys;
239388e9c81SLen Brown 
240c98d5d94SLen Brown struct system_summary {
241c98d5d94SLen Brown 	struct thread_data threads;
242c98d5d94SLen Brown 	struct core_data cores;
243c98d5d94SLen Brown 	struct pkg_data packages;
244388e9c81SLen Brown } average;
245c98d5d94SLen Brown 
246c98d5d94SLen Brown 
247c98d5d94SLen Brown struct topo_params {
248c98d5d94SLen Brown 	int num_packages;
249103a8feaSLen Brown 	int num_cpus;
250c98d5d94SLen Brown 	int num_cores;
251c98d5d94SLen Brown 	int max_cpu_num;
252c98d5d94SLen Brown 	int num_cores_per_pkg;
253c98d5d94SLen Brown 	int num_threads_per_core;
254c98d5d94SLen Brown } topo;
255103a8feaSLen Brown 
256c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
257103a8feaSLen Brown 
258562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
259562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
260562a2d37SLen Brown 
261c98d5d94SLen Brown void setup_all_buffers(void);
262103a8feaSLen Brown 
263c98d5d94SLen Brown int cpu_is_not_present(int cpu)
264d15cf7c1SLen Brown {
265c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
266c98d5d94SLen Brown }
267c98d5d94SLen Brown /*
268c98d5d94SLen Brown  * run func(thread, core, package) in topology order
269c98d5d94SLen Brown  * skip non-present cpus
270c98d5d94SLen Brown  */
271c98d5d94SLen Brown 
272c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
273c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
274c98d5d94SLen Brown {
275c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
276c98d5d94SLen Brown 
277c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
278c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
279c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
280c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
281c98d5d94SLen Brown 				struct thread_data *t;
282c98d5d94SLen Brown 				struct core_data *c;
283c98d5d94SLen Brown 				struct pkg_data *p;
284c98d5d94SLen Brown 
285c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
286c98d5d94SLen Brown 
287c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
288c98d5d94SLen Brown 					continue;
289c98d5d94SLen Brown 
290c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
291c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
292c98d5d94SLen Brown 
293c98d5d94SLen Brown 				retval = func(t, c, p);
294c98d5d94SLen Brown 				if (retval)
295c98d5d94SLen Brown 					return retval;
296c98d5d94SLen Brown 			}
297c98d5d94SLen Brown 		}
298c98d5d94SLen Brown 	}
299d15cf7c1SLen Brown 	return 0;
300d15cf7c1SLen Brown }
301d15cf7c1SLen Brown 
30288c3281fSLen Brown int cpu_migrate(int cpu)
30388c3281fSLen Brown {
304c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
305c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
306c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
30788c3281fSLen Brown 		return -1;
30888c3281fSLen Brown 	else
30988c3281fSLen Brown 		return 0;
31088c3281fSLen Brown }
31136229897SLen Brown int get_msr_fd(int cpu)
312103a8feaSLen Brown {
313103a8feaSLen Brown 	char pathname[32];
314103a8feaSLen Brown 	int fd;
315103a8feaSLen Brown 
31636229897SLen Brown 	fd = fd_percpu[cpu];
31736229897SLen Brown 
31836229897SLen Brown 	if (fd)
31936229897SLen Brown 		return fd;
32036229897SLen Brown 
321103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
322103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
32315aaa346SLen Brown 	if (fd < 0)
32498481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
325103a8feaSLen Brown 
32636229897SLen Brown 	fd_percpu[cpu] = fd;
32736229897SLen Brown 
32836229897SLen Brown 	return fd;
32936229897SLen Brown }
33036229897SLen Brown 
33136229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
33236229897SLen Brown {
33336229897SLen Brown 	ssize_t retval;
33436229897SLen Brown 
33536229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
33615aaa346SLen Brown 
33798481e79SLen Brown 	if (retval != sizeof *msr)
33836229897SLen Brown 		err(-1, "msr %d offset 0x%llx read failed", cpu, (unsigned long long)offset);
33915aaa346SLen Brown 
34015aaa346SLen Brown 	return 0;
341103a8feaSLen Brown }
342103a8feaSLen Brown 
343fc04cc67SLen Brown /*
344fc04cc67SLen Brown  * Example Format w/ field column widths:
345fc04cc67SLen Brown  *
346388e9c81SLen Brown  *  Package    Core     CPU Avg_MHz Bzy_MHz TSC_MHz     IRQ   SMI   Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 ThreadC CoreTmp  CoreCnt PkgTmp  GFXMHz Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt PkgCnt
347562a2d37SLen Brown  * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
348fc04cc67SLen Brown  */
349fc04cc67SLen Brown 
350a829eb4dSLen Brown void print_header(void)
351103a8feaSLen Brown {
352388e9c81SLen Brown 	struct msr_counter *mp;
353388e9c81SLen Brown 
354103a8feaSLen Brown 	if (show_pkg)
3553d109de2SLen Brown 		outp += sprintf(outp, "\tPackage");
356103a8feaSLen Brown 	if (show_core)
3573d109de2SLen Brown 		outp += sprintf(outp, "\tCore");
358103a8feaSLen Brown 	if (show_cpu)
3593d109de2SLen Brown 		outp += sprintf(outp, "\tCPU");
360103a8feaSLen Brown 	if (has_aperf)
3613d109de2SLen Brown 		outp += sprintf(outp, "\tAvg_MHz");
362d7899447SLen Brown 	if (has_aperf)
3633d109de2SLen Brown 		outp += sprintf(outp, "\tBusy%%");
364fc04cc67SLen Brown 	if (has_aperf)
3653d109de2SLen Brown 		outp += sprintf(outp, "\tBzy_MHz");
3663d109de2SLen Brown 	outp += sprintf(outp, "\tTSC_MHz");
3671cc21f7bSLen Brown 
3681cc21f7bSLen Brown 	if (!debug)
3691cc21f7bSLen Brown 		goto done;
3701cc21f7bSLen Brown 
371562a2d37SLen Brown 	if (do_irq)
3723d109de2SLen Brown 		outp += sprintf(outp, "\tIRQ");
3731cc21f7bSLen Brown 	if (do_smi)
3743d109de2SLen Brown 		outp += sprintf(outp, "\tSMI");
3751cc21f7bSLen Brown 
376103a8feaSLen Brown 	if (do_nhm_cstates)
3773d109de2SLen Brown 		outp += sprintf(outp, "\tCPU%%c1");
378889facbeSLen Brown 
379388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
380388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
381388e9c81SLen Brown 			if (mp->width == 64)
382388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
383388e9c81SLen Brown 			else
384388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
385388e9c81SLen Brown 		} else {
386388e9c81SLen Brown 			outp += sprintf(outp, "\t%-7.7s", mp->name);
387388e9c81SLen Brown 		}
388388e9c81SLen Brown 	}
389388e9c81SLen Brown 
390*678a3bd1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
391*678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c3");
392*678a3bd1SLen Brown 	if (do_nhm_cstates)
393*678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c6");
394*678a3bd1SLen Brown 	if (do_snb_cstates)
395*678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c7");
396*678a3bd1SLen Brown 
397*678a3bd1SLen Brown 
398889facbeSLen Brown 	if (do_dts)
3993d109de2SLen Brown 		outp += sprintf(outp, "\tCoreTmp");
400388e9c81SLen Brown 
401388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
402388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
403388e9c81SLen Brown 			if (mp->width == 64)
404388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
405388e9c81SLen Brown 			else
406388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
407388e9c81SLen Brown 		} else {
408388e9c81SLen Brown 			outp += sprintf(outp, "\t%-7.7s", mp->name);
409388e9c81SLen Brown 		}
410388e9c81SLen Brown 	}
411388e9c81SLen Brown 
412889facbeSLen Brown 	if (do_ptm)
4133d109de2SLen Brown 		outp += sprintf(outp, "\tPkgTmp");
414889facbeSLen Brown 
415fdf676e5SLen Brown 	if (do_gfx_rc6_ms)
4163d109de2SLen Brown 		outp += sprintf(outp, "\tGFX%%rc6");
417fdf676e5SLen Brown 
41827d47356SLen Brown 	if (do_gfx_mhz)
4193d109de2SLen Brown 		outp += sprintf(outp, "\tGFXMHz");
42027d47356SLen Brown 
4210b2bb692SLen Brown 	if (do_skl_residency) {
4223d109de2SLen Brown 		outp += sprintf(outp, "\tTotl%%C0");
4233d109de2SLen Brown 		outp += sprintf(outp, "\tAny%%C0");
4243d109de2SLen Brown 		outp += sprintf(outp, "\tGFX%%C0");
4253d109de2SLen Brown 		outp += sprintf(outp, "\tCPUGFX%%");
4260b2bb692SLen Brown 	}
4270b2bb692SLen Brown 
428ee7e38e3SLen Brown 	if (do_pc2)
4293d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc2");
430ee7e38e3SLen Brown 	if (do_pc3)
4313d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc3");
432ee7e38e3SLen Brown 	if (do_pc6)
4333d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc6");
434ee7e38e3SLen Brown 	if (do_pc7)
4353d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc7");
436ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
4373d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc8");
4383d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc9");
4393d109de2SLen Brown 		outp += sprintf(outp, "\tPk%%pc10");
440ca58710fSKristen Carlson Accardi 	}
441103a8feaSLen Brown 
4425c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
443889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
4443d109de2SLen Brown 			outp += sprintf(outp, "\tPkgWatt");
4459148494cSJacob Pan 		if (do_rapl & RAPL_CORES_ENERGY_STATUS)
4463d109de2SLen Brown 			outp += sprintf(outp, "\tCorWatt");
447889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
4483d109de2SLen Brown 			outp += sprintf(outp, "\tGFXWatt");
449889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
4503d109de2SLen Brown 			outp += sprintf(outp, "\tRAMWatt");
451889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
4523d109de2SLen Brown 			outp += sprintf(outp, "\tPKG_%%");
453889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
4543d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_%%");
455d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
4565c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
4573d109de2SLen Brown 			outp += sprintf(outp, "\tPkg_J");
4589148494cSJacob Pan 		if (do_rapl & RAPL_CORES_ENERGY_STATUS)
4593d109de2SLen Brown 			outp += sprintf(outp, "\tCor_J");
4605c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
4613d109de2SLen Brown 			outp += sprintf(outp, "\tGFX_J");
4625c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
4633d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_J");
4645c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
4653d109de2SLen Brown 			outp += sprintf(outp, "\tPKG_%%");
4665c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
4673d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_%%");
4685c56be9aSDirk Brandewie 	}
469388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
470388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
471388e9c81SLen Brown 			if (mp->width == 64)
472388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
473388e9c81SLen Brown 			else
474388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
475388e9c81SLen Brown 		} else {
476388e9c81SLen Brown 			outp += sprintf(outp, "\t%-7.7s", mp->name);
477388e9c81SLen Brown 		}
478388e9c81SLen Brown 	}
479388e9c81SLen Brown 
4801cc21f7bSLen Brown done:
481c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
482103a8feaSLen Brown }
483103a8feaSLen Brown 
484c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
485c98d5d94SLen Brown 	struct pkg_data *p)
486103a8feaSLen Brown {
487388e9c81SLen Brown 	int i;
488388e9c81SLen Brown 	struct msr_counter *mp;
489388e9c81SLen Brown 
4903b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
491c98d5d94SLen Brown 
492c98d5d94SLen Brown 	if (t) {
4933b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
4943b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
4953b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
4963b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
4973b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
4983b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
4996886fee4SLen Brown 
500562a2d37SLen Brown 		if (do_irq)
501562a2d37SLen Brown 			outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
5021ed51011SLen Brown 		if (do_smi)
5033b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
504388e9c81SLen Brown 
505388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
506388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
507388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
508388e9c81SLen Brown 		}
509103a8feaSLen Brown 	}
510103a8feaSLen Brown 
511c98d5d94SLen Brown 	if (c) {
5123b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
5133b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
5143b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
5153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
5163b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
517388e9c81SLen Brown 
518388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
519388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
520388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
521388e9c81SLen Brown 		}
522c98d5d94SLen Brown 	}
523103a8feaSLen Brown 
524c98d5d94SLen Brown 	if (p) {
5253b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
5260b2bb692SLen Brown 
5270b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
5280b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
5290b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
5300b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
5310b2bb692SLen Brown 
5323b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
533ee7e38e3SLen Brown 		if (do_pc3)
5343b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
535ee7e38e3SLen Brown 		if (do_pc6)
5363b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
537ee7e38e3SLen Brown 		if (do_pc7)
5383b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
5393b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
5403b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
5413b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
5423b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
5433b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
5443b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
5453b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
5463b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
5473b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
5483b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
5493b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
5503b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
551388e9c81SLen Brown 
552388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
553388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
554388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
555388e9c81SLen Brown 		}
556c98d5d94SLen Brown 	}
5573b4d5c7fSAndy Shevchenko 
5583b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
5593b4d5c7fSAndy Shevchenko 
560c98d5d94SLen Brown 	return 0;
561103a8feaSLen Brown }
562103a8feaSLen Brown 
563e23da037SLen Brown /*
564e23da037SLen Brown  * column formatting convention & formats
565e23da037SLen Brown  */
566c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
567c98d5d94SLen Brown 	struct pkg_data *p)
568103a8feaSLen Brown {
569103a8feaSLen Brown 	double interval_float;
570fc04cc67SLen Brown 	char *fmt8;
571388e9c81SLen Brown 	int i;
572388e9c81SLen Brown 	struct msr_counter *mp;
573103a8feaSLen Brown 
574c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
575c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
576c98d5d94SLen Brown 		return 0;
577c98d5d94SLen Brown 
578c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
579c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
580c98d5d94SLen Brown 		return 0;
581c98d5d94SLen Brown 
582103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
583103a8feaSLen Brown 
584c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
585c98d5d94SLen Brown 	if (t == &average.threads) {
586103a8feaSLen Brown 		if (show_pkg)
5873d109de2SLen Brown 			outp += sprintf(outp, "\t-");
588103a8feaSLen Brown 		if (show_core)
5893d109de2SLen Brown 			outp += sprintf(outp, "\t-");
590103a8feaSLen Brown 		if (show_cpu)
5913d109de2SLen Brown 			outp += sprintf(outp, "\t-");
592103a8feaSLen Brown 	} else {
593c98d5d94SLen Brown 		if (show_pkg) {
594c98d5d94SLen Brown 			if (p)
5953d109de2SLen Brown 				outp += sprintf(outp, "\t%d", p->package_id);
596c98d5d94SLen Brown 			else
5973d109de2SLen Brown 				outp += sprintf(outp, "\t-");
598c98d5d94SLen Brown 		}
599c98d5d94SLen Brown 		if (show_core) {
600c98d5d94SLen Brown 			if (c)
6013d109de2SLen Brown 				outp += sprintf(outp, "\t%d", c->core_id);
602c98d5d94SLen Brown 			else
6033d109de2SLen Brown 				outp += sprintf(outp, "\t-");
604c98d5d94SLen Brown 		}
605103a8feaSLen Brown 		if (show_cpu)
6063d109de2SLen Brown 			outp += sprintf(outp, "\t%d", t->cpu_id);
607103a8feaSLen Brown 	}
608fc04cc67SLen Brown 
609d7899447SLen Brown 	/* Avg_MHz */
610fc04cc67SLen Brown 	if (has_aperf)
6113d109de2SLen Brown 		outp += sprintf(outp, "\t%.0f",
612fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
613fc04cc67SLen Brown 
61475d2e44eSLen Brown 	/* Busy% */
615ba3dec99SLen Brown 	if (has_aperf)
6163d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
617103a8feaSLen Brown 
618d7899447SLen Brown 	/* Bzy_MHz */
61921ed5574SLen Brown 	if (has_aperf) {
62021ed5574SLen Brown 		if (has_base_hz)
6213d109de2SLen Brown 			outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
62221ed5574SLen Brown 		else
6233d109de2SLen Brown 			outp += sprintf(outp, "\t%.0f",
62421ed5574SLen Brown 				1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
62521ed5574SLen Brown 	}
626103a8feaSLen Brown 
627d7899447SLen Brown 	/* TSC_MHz */
6283d109de2SLen Brown 	outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
629103a8feaSLen Brown 
6301cc21f7bSLen Brown 	if (!debug)
6311cc21f7bSLen Brown 		goto done;
6321cc21f7bSLen Brown 
633562a2d37SLen Brown 	/* IRQ */
634562a2d37SLen Brown 	if (do_irq)
6353d109de2SLen Brown 		outp += sprintf(outp, "\t%d", t->irq_count);
636562a2d37SLen Brown 
6371cc21f7bSLen Brown 	/* SMI */
6381cc21f7bSLen Brown 	if (do_smi)
6393d109de2SLen Brown 		outp += sprintf(outp, "\t%d", t->smi_count);
6401cc21f7bSLen Brown 
641*678a3bd1SLen Brown 	/* C1 */
642ba3dec99SLen Brown 	if (do_nhm_cstates)
6433d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
644c98d5d94SLen Brown 
645*678a3bd1SLen Brown 	/* Added counters */
646*678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
647*678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
648*678a3bd1SLen Brown 			if (mp->width == 32)
649*678a3bd1SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
650*678a3bd1SLen Brown 			else
651*678a3bd1SLen Brown 				outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
652*678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
653*678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", t->counter[i]);
654*678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
655*678a3bd1SLen Brown 			outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/t->tsc);
656*678a3bd1SLen Brown 		}
657*678a3bd1SLen Brown 	}
658*678a3bd1SLen Brown 
659c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
660c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
661c98d5d94SLen Brown 		goto done;
662c98d5d94SLen Brown 
663fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
6643d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
665103a8feaSLen Brown 	if (do_nhm_cstates)
6663d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
667103a8feaSLen Brown 	if (do_snb_cstates)
6683d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
669c98d5d94SLen Brown 
670388e9c81SLen Brown 
671889facbeSLen Brown 	if (do_dts)
6723d109de2SLen Brown 		outp += sprintf(outp, "\t%d", c->core_temp_c);
673889facbeSLen Brown 
674388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
675388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
676388e9c81SLen Brown 			if (mp->width == 32)
677388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
678388e9c81SLen Brown 			else
679388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
680388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
681*678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", c->counter[i]);
682388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
683388e9c81SLen Brown 			outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/t->tsc);
684388e9c81SLen Brown 		}
685388e9c81SLen Brown 	}
686388e9c81SLen Brown 
687c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
688c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
689c98d5d94SLen Brown 		goto done;
690c98d5d94SLen Brown 
6910b2bb692SLen Brown 	/* PkgTmp */
692889facbeSLen Brown 	if (do_ptm)
6933d109de2SLen Brown 		outp += sprintf(outp, "\t%d", p->pkg_temp_c);
694889facbeSLen Brown 
695fdf676e5SLen Brown 	/* GFXrc6 */
6969185e988SLen Brown 	if (do_gfx_rc6_ms) {
697ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
6983d109de2SLen Brown 			outp += sprintf(outp, "\t**.**");
6999185e988SLen Brown 		} else {
7003d109de2SLen Brown 			outp += sprintf(outp, "\t%.2f",
7019185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
7029185e988SLen Brown 		}
7039185e988SLen Brown 	}
704fdf676e5SLen Brown 
70527d47356SLen Brown 	/* GFXMHz */
70627d47356SLen Brown 	if (do_gfx_mhz)
7073d109de2SLen Brown 		outp += sprintf(outp, "\t%d", p->gfx_mhz);
70827d47356SLen Brown 
7090b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
7100b2bb692SLen Brown 	if (do_skl_residency) {
7113d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
7123d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
7133d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
7143d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
7150b2bb692SLen Brown 	}
7160b2bb692SLen Brown 
717ee7e38e3SLen Brown 	if (do_pc2)
7183d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
719ee7e38e3SLen Brown 	if (do_pc3)
7203d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
721ee7e38e3SLen Brown 	if (do_pc6)
7223d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
723ee7e38e3SLen Brown 	if (do_pc7)
7243d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
725ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
7263d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
7273d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
7283d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
729ca58710fSKristen Carlson Accardi 	}
730889facbeSLen Brown 
731889facbeSLen Brown 	/*
732889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
733889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
734889facbeSLen Brown  	 */
735fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
7363d109de2SLen Brown 		fmt8 = "\t%.2f";
737fc04cc67SLen Brown 	else
738fc04cc67SLen Brown 		fmt8 = "%6.0f**";
739889facbeSLen Brown 
7405c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
741889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
742fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
7439148494cSJacob Pan 		if (do_rapl & RAPL_CORES_ENERGY_STATUS)
744fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
745889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
746fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
747889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
74840ee8e3bSAndrey Semin 			outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
749889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
750fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
751889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
752fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
753d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
7545c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
755fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
7565c56be9aSDirk Brandewie 					p->energy_pkg * rapl_energy_units);
7575c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
758fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
7595c56be9aSDirk Brandewie 					p->energy_cores * rapl_energy_units);
7605c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
761fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
7625c56be9aSDirk Brandewie 					p->energy_gfx * rapl_energy_units);
7635c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
764fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
76540ee8e3bSAndrey Semin 					p->energy_dram * rapl_dram_energy_units);
7665c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
767fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
7685c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
769fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
7705c56be9aSDirk Brandewie 	}
771388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
772388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
773388e9c81SLen Brown 			if (mp->width == 32)
774388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
775388e9c81SLen Brown 			else
776388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
777388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
778*678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", p->counter[i]);
779388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
780388e9c81SLen Brown 			outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/t->tsc);
781388e9c81SLen Brown 		}
782388e9c81SLen Brown 	}
783388e9c81SLen Brown 
784c98d5d94SLen Brown done:
785c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
786c98d5d94SLen Brown 
787c98d5d94SLen Brown 	return 0;
788103a8feaSLen Brown }
789103a8feaSLen Brown 
790b7d8c148SLen Brown void flush_output_stdout(void)
791103a8feaSLen Brown {
792b7d8c148SLen Brown 	FILE *filep;
793b7d8c148SLen Brown 
794b7d8c148SLen Brown 	if (outf == stderr)
795b7d8c148SLen Brown 		filep = stdout;
796b7d8c148SLen Brown 	else
797b7d8c148SLen Brown 		filep = outf;
798b7d8c148SLen Brown 
799b7d8c148SLen Brown 	fputs(output_buffer, filep);
800b7d8c148SLen Brown 	fflush(filep);
801b7d8c148SLen Brown 
802c98d5d94SLen Brown 	outp = output_buffer;
803c98d5d94SLen Brown }
804b7d8c148SLen Brown void flush_output_stderr(void)
805c98d5d94SLen Brown {
806b7d8c148SLen Brown 	fputs(output_buffer, outf);
807b7d8c148SLen Brown 	fflush(outf);
808c98d5d94SLen Brown 	outp = output_buffer;
809c98d5d94SLen Brown }
810c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
811c98d5d94SLen Brown {
812e23da037SLen Brown 	static int printed;
813103a8feaSLen Brown 
814e23da037SLen Brown 	if (!printed || !summary_only)
815103a8feaSLen Brown 		print_header();
816103a8feaSLen Brown 
817c98d5d94SLen Brown 	if (topo.num_cpus > 1)
818c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
819c98d5d94SLen Brown 			&average.packages);
820103a8feaSLen Brown 
821e23da037SLen Brown 	printed = 1;
822e23da037SLen Brown 
823e23da037SLen Brown 	if (summary_only)
824e23da037SLen Brown 		return;
825e23da037SLen Brown 
826c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
827103a8feaSLen Brown }
828103a8feaSLen Brown 
829889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
830889facbeSLen Brown 	if (new > old) {			\
831889facbeSLen Brown 		old = new - old;		\
832889facbeSLen Brown 	} else {				\
833889facbeSLen Brown 		old = 0x100000000 + new - old;	\
834889facbeSLen Brown 	}
835889facbeSLen Brown 
836ba3dec99SLen Brown int
837c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
838103a8feaSLen Brown {
839388e9c81SLen Brown 	int i;
840388e9c81SLen Brown 	struct msr_counter *mp;
8410b2bb692SLen Brown 
8420b2bb692SLen Brown 	if (do_skl_residency) {
8430b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
8440b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
8450b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
8460b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
8470b2bb692SLen Brown 	}
848c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
849ee7e38e3SLen Brown 	if (do_pc3)
850c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
851ee7e38e3SLen Brown 	if (do_pc6)
852c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
853ee7e38e3SLen Brown 	if (do_pc7)
854c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
855ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
856ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
857ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
858889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
859889facbeSLen Brown 
8609185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
8619185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
8629185e988SLen Brown 		old->gfx_rc6_ms = -1;
8639185e988SLen Brown 	else
864fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
8659185e988SLen Brown 
86627d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
86727d47356SLen Brown 
868889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
869889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
870889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
871889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
872889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
873889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
874ba3dec99SLen Brown 
875388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
876388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
877388e9c81SLen Brown 			old->counter[i] = new->counter[i];
878388e9c81SLen Brown 		else
879388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
880388e9c81SLen Brown 	}
881388e9c81SLen Brown 
882ba3dec99SLen Brown 	return 0;
883103a8feaSLen Brown }
884103a8feaSLen Brown 
885c98d5d94SLen Brown void
886c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
887c98d5d94SLen Brown {
888388e9c81SLen Brown 	int i;
889388e9c81SLen Brown 	struct msr_counter *mp;
890388e9c81SLen Brown 
891c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
892c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
893c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
894889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
895388e9c81SLen Brown 
896388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
897388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
898388e9c81SLen Brown 			old->counter[i] = new->counter[i];
899388e9c81SLen Brown 		else
900388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
901388e9c81SLen Brown 	}
902103a8feaSLen Brown }
903c98d5d94SLen Brown 
904c3ae331dSLen Brown /*
905c3ae331dSLen Brown  * old = new - old
906c3ae331dSLen Brown  */
907ba3dec99SLen Brown int
908c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
909c98d5d94SLen Brown 	struct core_data *core_delta)
910c98d5d94SLen Brown {
911388e9c81SLen Brown 	int i;
912388e9c81SLen Brown 	struct msr_counter *mp;
913388e9c81SLen Brown 
914c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
915c98d5d94SLen Brown 
916103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
917b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
918b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
919b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
920b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
921103a8feaSLen Brown 
922c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
923c98d5d94SLen Brown 
924a729617cSLen Brown 	if (has_aperf) {
925c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
926c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
927c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
928c98d5d94SLen Brown 		} else {
929ba3dec99SLen Brown 			return -1;
930103a8feaSLen Brown 		}
931a729617cSLen Brown 	}
932103a8feaSLen Brown 
933c98d5d94SLen Brown 
934144b44b1SLen Brown 	if (use_c1_residency_msr) {
935144b44b1SLen Brown 		/*
936144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
937144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
938144b44b1SLen Brown 		 */
939144b44b1SLen Brown 	} else {
940103a8feaSLen Brown 		/*
941c3ae331dSLen Brown 		 * As counter collection is not atomic,
942c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
943103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
944103a8feaSLen Brown 		 */
945c3ae331dSLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
946c98d5d94SLen Brown 			old->c1 = 0;
947c98d5d94SLen Brown 		else {
948c98d5d94SLen Brown 			/* normal case, derive c1 */
949c98d5d94SLen Brown 			old->c1 = old->tsc - old->mperf - core_delta->c3
950c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
951c98d5d94SLen Brown 		}
952144b44b1SLen Brown 	}
953c3ae331dSLen Brown 
954c98d5d94SLen Brown 	if (old->mperf == 0) {
955b7d8c148SLen Brown 		if (debug > 1)
956b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
957c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
958c98d5d94SLen Brown 	}
959103a8feaSLen Brown 
960562a2d37SLen Brown 	if (do_irq)
961562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
962562a2d37SLen Brown 
9631ed51011SLen Brown 	if (do_smi)
9641ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
965ba3dec99SLen Brown 
966388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
967388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
968388e9c81SLen Brown 			old->counter[i] = new->counter[i];
969388e9c81SLen Brown 		else
970388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
971388e9c81SLen Brown 	}
972ba3dec99SLen Brown 	return 0;
973103a8feaSLen Brown }
974c98d5d94SLen Brown 
975c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
976c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
977c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
978c98d5d94SLen Brown {
979ba3dec99SLen Brown 	int retval = 0;
980ba3dec99SLen Brown 
981c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
982c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
983c98d5d94SLen Brown 		delta_core(c, c2);
984c98d5d94SLen Brown 
985c98d5d94SLen Brown 	/* always calculate thread delta */
986ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
987ba3dec99SLen Brown 	if (retval)
988ba3dec99SLen Brown 		return retval;
989c98d5d94SLen Brown 
990c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
991c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
992ba3dec99SLen Brown 		retval = delta_package(p, p2);
993c98d5d94SLen Brown 
994ba3dec99SLen Brown 	return retval;
995103a8feaSLen Brown }
996103a8feaSLen Brown 
997c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
998103a8feaSLen Brown {
999388e9c81SLen Brown 	int i;
1000388e9c81SLen Brown 	struct msr_counter  *mp;
1001388e9c81SLen Brown 
1002c98d5d94SLen Brown 	t->tsc = 0;
1003c98d5d94SLen Brown 	t->aperf = 0;
1004c98d5d94SLen Brown 	t->mperf = 0;
1005c98d5d94SLen Brown 	t->c1 = 0;
1006103a8feaSLen Brown 
1007562a2d37SLen Brown 	t->irq_count = 0;
1008562a2d37SLen Brown 	t->smi_count = 0;
1009562a2d37SLen Brown 
1010c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1011c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1012c98d5d94SLen Brown 
1013c98d5d94SLen Brown 	c->c3 = 0;
1014c98d5d94SLen Brown 	c->c6 = 0;
1015c98d5d94SLen Brown 	c->c7 = 0;
1016889facbeSLen Brown 	c->core_temp_c = 0;
1017c98d5d94SLen Brown 
10180b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
10190b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
10200b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
10210b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
10220b2bb692SLen Brown 
1023c98d5d94SLen Brown 	p->pc2 = 0;
1024ee7e38e3SLen Brown 	if (do_pc3)
1025c98d5d94SLen Brown 		p->pc3 = 0;
1026ee7e38e3SLen Brown 	if (do_pc6)
1027c98d5d94SLen Brown 		p->pc6 = 0;
1028ee7e38e3SLen Brown 	if (do_pc7)
1029c98d5d94SLen Brown 		p->pc7 = 0;
1030ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1031ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1032ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1033889facbeSLen Brown 
1034889facbeSLen Brown 	p->energy_pkg = 0;
1035889facbeSLen Brown 	p->energy_dram = 0;
1036889facbeSLen Brown 	p->energy_cores = 0;
1037889facbeSLen Brown 	p->energy_gfx = 0;
1038889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1039889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1040889facbeSLen Brown 	p->pkg_temp_c = 0;
104127d47356SLen Brown 
1042fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
104327d47356SLen Brown 	p->gfx_mhz = 0;
1044388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1045388e9c81SLen Brown 		t->counter[i] = 0;
1046388e9c81SLen Brown 
1047388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1048388e9c81SLen Brown 		c->counter[i] = 0;
1049388e9c81SLen Brown 
1050388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1051388e9c81SLen Brown 		p->counter[i] = 0;
1052103a8feaSLen Brown }
1053c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1054c98d5d94SLen Brown 	struct pkg_data *p)
1055103a8feaSLen Brown {
1056388e9c81SLen Brown 	int i;
1057388e9c81SLen Brown 	struct msr_counter *mp;
1058388e9c81SLen Brown 
1059c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1060c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1061c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1062c98d5d94SLen Brown 	average.threads.c1 += t->c1;
106315aaa346SLen Brown 
1064562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1065562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1066562a2d37SLen Brown 
1067388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1068388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1069388e9c81SLen Brown 			continue;
1070388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1071388e9c81SLen Brown 	}
1072388e9c81SLen Brown 
1073c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1074c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1075c98d5d94SLen Brown 		return 0;
1076c98d5d94SLen Brown 
1077c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1078c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1079c98d5d94SLen Brown 	average.cores.c7 += c->c7;
1080c98d5d94SLen Brown 
1081889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1082889facbeSLen Brown 
1083388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1084388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1085388e9c81SLen Brown 			continue;
1086388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1087388e9c81SLen Brown 	}
1088388e9c81SLen Brown 
1089c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1090c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1091c98d5d94SLen Brown 		return 0;
1092c98d5d94SLen Brown 
10930b2bb692SLen Brown 	if (do_skl_residency) {
10940b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
10950b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
10960b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
10970b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
10980b2bb692SLen Brown 	}
10990b2bb692SLen Brown 
1100c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
1101ee7e38e3SLen Brown 	if (do_pc3)
1102c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
1103ee7e38e3SLen Brown 	if (do_pc6)
1104c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
1105ee7e38e3SLen Brown 	if (do_pc7)
1106c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1107ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1108ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1109ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1110c98d5d94SLen Brown 
1111889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1112889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1113889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1114889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1115889facbeSLen Brown 
1116fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
111727d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
111827d47356SLen Brown 
1119889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1120889facbeSLen Brown 
1121889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1122889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1123388e9c81SLen Brown 
1124388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1125388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1126388e9c81SLen Brown 			continue;
1127388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1128388e9c81SLen Brown 	}
1129c98d5d94SLen Brown 	return 0;
1130c98d5d94SLen Brown }
1131c98d5d94SLen Brown /*
1132c98d5d94SLen Brown  * sum the counters for all cpus in the system
1133c98d5d94SLen Brown  * compute the weighted average
1134c98d5d94SLen Brown  */
1135c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1136c98d5d94SLen Brown 	struct pkg_data *p)
1137c98d5d94SLen Brown {
1138388e9c81SLen Brown 	int i;
1139388e9c81SLen Brown 	struct msr_counter *mp;
1140388e9c81SLen Brown 
1141c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1142c98d5d94SLen Brown 
1143c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1144c98d5d94SLen Brown 
1145c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1146c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1147c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1148c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1149c98d5d94SLen Brown 
1150c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1151c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1152c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
1153c98d5d94SLen Brown 
11540b2bb692SLen Brown 	if (do_skl_residency) {
11550b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
11560b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
11570b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
11580b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
11590b2bb692SLen Brown 	}
11600b2bb692SLen Brown 
1161c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
1162ee7e38e3SLen Brown 	if (do_pc3)
1163c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
1164ee7e38e3SLen Brown 	if (do_pc6)
1165c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
1166ee7e38e3SLen Brown 	if (do_pc7)
1167c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1168ca58710fSKristen Carlson Accardi 
1169ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1170ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1171ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1172388e9c81SLen Brown 
1173388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1174388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1175388e9c81SLen Brown 			continue;
1176388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1177388e9c81SLen Brown 	}
1178388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1179388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1180388e9c81SLen Brown 			continue;
1181388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1182388e9c81SLen Brown 	}
1183388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1184388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1185388e9c81SLen Brown 			continue;
1186388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1187388e9c81SLen Brown 	}
1188c98d5d94SLen Brown }
1189c98d5d94SLen Brown 
1190c98d5d94SLen Brown static unsigned long long rdtsc(void)
1191c98d5d94SLen Brown {
1192c98d5d94SLen Brown 	unsigned int low, high;
1193c98d5d94SLen Brown 
1194c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1195c98d5d94SLen Brown 
1196c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1197c98d5d94SLen Brown }
1198c98d5d94SLen Brown 
1199c98d5d94SLen Brown /*
1200c98d5d94SLen Brown  * get_counters(...)
1201c98d5d94SLen Brown  * migrate to cpu
1202c98d5d94SLen Brown  * acquire and record local counters for that cpu
1203c98d5d94SLen Brown  */
1204c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1205c98d5d94SLen Brown {
1206c98d5d94SLen Brown 	int cpu = t->cpu_id;
1207889facbeSLen Brown 	unsigned long long msr;
12080102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1209388e9c81SLen Brown 	struct msr_counter *mp;
1210388e9c81SLen Brown 	int i;
1211c98d5d94SLen Brown 
1212e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1213b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
121415aaa346SLen Brown 		return -1;
1215e52966c0SLen Brown 	}
121615aaa346SLen Brown 
12170102b067SLen Brown retry:
1218c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
121915aaa346SLen Brown 
122015aaa346SLen Brown 	if (has_aperf) {
12210102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
12220102b067SLen Brown 
12230102b067SLen Brown 		/*
12240102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
12250102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
12260102b067SLen Brown 		 *
12270102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
12280102b067SLen Brown 		 * individual system call, so delays may occur
12290102b067SLen Brown 		 * between them.  If the time to read them
12300102b067SLen Brown 		 * varies by a large amount, we re-read them.
12310102b067SLen Brown 		 */
12320102b067SLen Brown 
12330102b067SLen Brown 		/*
12340102b067SLen Brown 		 * This initial dummy APERF read has been seen to
12350102b067SLen Brown 		 * reduce jitter in the subsequent reads.
12360102b067SLen Brown 		 */
12370102b067SLen Brown 
12389c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1239c98d5d94SLen Brown 			return -3;
12400102b067SLen Brown 
12410102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
12420102b067SLen Brown 
12430102b067SLen Brown 		tsc_before = t->tsc;
12440102b067SLen Brown 
12450102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
12460102b067SLen Brown 			return -3;
12470102b067SLen Brown 
12480102b067SLen Brown 		tsc_between = rdtsc();
12490102b067SLen Brown 
12509c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1251c98d5d94SLen Brown 			return -4;
12520102b067SLen Brown 
12530102b067SLen Brown 		tsc_after = rdtsc();
12540102b067SLen Brown 
12550102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
12560102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
12570102b067SLen Brown 
12580102b067SLen Brown 		/*
12590102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
12600102b067SLen Brown 		 * differ by more than 2x, then try again.
12610102b067SLen Brown 		 */
12620102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
12630102b067SLen Brown 			aperf_mperf_retry_count++;
12640102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
12650102b067SLen Brown 				goto retry;
12660102b067SLen Brown 			else
12670102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
12680102b067SLen Brown 					cpu, aperf_time, mperf_time);
12690102b067SLen Brown 		}
12700102b067SLen Brown 		aperf_mperf_retry_count = 0;
12710102b067SLen Brown 
1272b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1273b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
127488c3281fSLen Brown 	}
127588c3281fSLen Brown 
1276562a2d37SLen Brown 	if (do_irq)
1277562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
12781ed51011SLen Brown 	if (do_smi) {
12791ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
12801ed51011SLen Brown 			return -5;
12811ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
12821ed51011SLen Brown 	}
1283c98d5d94SLen Brown 
1284144b44b1SLen Brown 	if (use_c1_residency_msr) {
1285144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1286144b44b1SLen Brown 			return -6;
1287144b44b1SLen Brown 	}
1288144b44b1SLen Brown 
1289388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1290388e9c81SLen Brown 		if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1291388e9c81SLen Brown 			return -10;
1292388e9c81SLen Brown 	}
1293388e9c81SLen Brown 
1294388e9c81SLen Brown 
1295c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1296c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1297c98d5d94SLen Brown 		return 0;
1298c98d5d94SLen Brown 
1299fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
1300c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1301c98d5d94SLen Brown 			return -6;
1302144b44b1SLen Brown 	}
1303144b44b1SLen Brown 
1304fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_knl_cstates) {
1305c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1306c98d5d94SLen Brown 			return -7;
1307fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1308fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1309fb5d4327SDasaratharaman Chandramouli 			return -7;
1310103a8feaSLen Brown 	}
131115aaa346SLen Brown 
131215aaa346SLen Brown 	if (do_snb_cstates)
1313c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1314c98d5d94SLen Brown 			return -8;
1315c98d5d94SLen Brown 
1316889facbeSLen Brown 	if (do_dts) {
1317889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1318889facbeSLen Brown 			return -9;
1319889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1320889facbeSLen Brown 	}
1321889facbeSLen Brown 
1322388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1323388e9c81SLen Brown 		if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1324388e9c81SLen Brown 			return -10;
1325388e9c81SLen Brown 	}
1326889facbeSLen Brown 
1327c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1328c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1329c98d5d94SLen Brown 		return 0;
133015aaa346SLen Brown 
13310b2bb692SLen Brown 	if (do_skl_residency) {
13320b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
13330b2bb692SLen Brown 			return -10;
13340b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
13350b2bb692SLen Brown 			return -11;
13360b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
13370b2bb692SLen Brown 			return -12;
13380b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
13390b2bb692SLen Brown 			return -13;
13400b2bb692SLen Brown 	}
1341ee7e38e3SLen Brown 	if (do_pc3)
1342c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1343c98d5d94SLen Brown 			return -9;
1344ee7e38e3SLen Brown 	if (do_pc6)
1345c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1346c98d5d94SLen Brown 			return -10;
1347ee7e38e3SLen Brown 	if (do_pc2)
1348c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1349c98d5d94SLen Brown 			return -11;
1350ee7e38e3SLen Brown 	if (do_pc7)
1351c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1352c98d5d94SLen Brown 			return -12;
1353ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
1354ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1355ca58710fSKristen Carlson Accardi 			return -13;
1356ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1357ca58710fSKristen Carlson Accardi 			return -13;
1358ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1359ca58710fSKristen Carlson Accardi 			return -13;
1360ca58710fSKristen Carlson Accardi 	}
1361889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1362889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1363889facbeSLen Brown 			return -13;
1364889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1365889facbeSLen Brown 	}
13669148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1367889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1368889facbeSLen Brown 			return -14;
1369889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1370889facbeSLen Brown 	}
1371889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1372889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1373889facbeSLen Brown 			return -15;
1374889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1375889facbeSLen Brown 	}
1376889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1377889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1378889facbeSLen Brown 			return -16;
1379889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1380889facbeSLen Brown 	}
1381889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1382889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1383889facbeSLen Brown 			return -16;
1384889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1385889facbeSLen Brown 	}
1386889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1387889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1388889facbeSLen Brown 			return -16;
1389889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1390889facbeSLen Brown 	}
1391889facbeSLen Brown 	if (do_ptm) {
1392889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1393889facbeSLen Brown 			return -17;
1394889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1395889facbeSLen Brown 	}
1396fdf676e5SLen Brown 
1397fdf676e5SLen Brown 	if (do_gfx_rc6_ms)
1398fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1399fdf676e5SLen Brown 
140027d47356SLen Brown 	if (do_gfx_mhz)
140127d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
140227d47356SLen Brown 
1403388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1404388e9c81SLen Brown 		if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1405388e9c81SLen Brown 			return -10;
1406388e9c81SLen Brown 	}
1407388e9c81SLen Brown 
140815aaa346SLen Brown 	return 0;
1409103a8feaSLen Brown }
1410103a8feaSLen Brown 
1411ee7e38e3SLen Brown /*
1412ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1413ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1414ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1415ee7e38e3SLen Brown  */
1416ee7e38e3SLen Brown 
1417ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1418ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1419ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1420ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1421ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1422ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1423ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1424ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1425ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1426ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1427ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1428ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
14290b2bb692SLen Brown #define PCL__8 12 /* PC8 */
14300b2bb692SLen Brown #define PCL__9 13 /* PC9 */
14310b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1432ee7e38e3SLen Brown 
1433ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1434ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
14350b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1436ee7e38e3SLen Brown 
1437e9257f5fSLen 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};
1438e9257f5fSLen 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};
1439e9257f5fSLen 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};
1440e9257f5fSLen 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, PCLRSV, PCLRSV};
1441e9257f5fSLen Brown int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1442e9257f5fSLen 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};
1443e4085d54SLen 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};
1444d8ebb442SLen 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};
1445ee7e38e3SLen Brown 
1446a2b7b749SLen Brown 
1447a2b7b749SLen Brown static void
1448a2b7b749SLen Brown calculate_tsc_tweak()
1449a2b7b749SLen Brown {
1450a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1451a2b7b749SLen Brown }
1452a2b7b749SLen Brown 
1453fcd17211SLen Brown static void
1454fcd17211SLen Brown dump_nhm_platform_info(void)
1455103a8feaSLen Brown {
1456103a8feaSLen Brown 	unsigned long long msr;
1457103a8feaSLen Brown 	unsigned int ratio;
1458103a8feaSLen Brown 
1459ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1460103a8feaSLen Brown 
1461b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
14626574a5d5SLen Brown 
1463103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1464b7d8c148SLen Brown 	fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1465103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1466103a8feaSLen Brown 
1467103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1468b7d8c148SLen Brown 	fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n",
1469103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1470103a8feaSLen Brown 
14717ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1472b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1473bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
147467920418SLen Brown 
1475fcd17211SLen Brown 	return;
1476fcd17211SLen Brown }
1477fcd17211SLen Brown 
1478fcd17211SLen Brown static void
1479fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1480fcd17211SLen Brown {
1481fcd17211SLen Brown 	unsigned long long msr;
1482fcd17211SLen Brown 	unsigned int ratio;
1483fcd17211SLen Brown 
14847ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1485fcd17211SLen Brown 
1486b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1487fcd17211SLen Brown 
1488fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1489fcd17211SLen Brown 	if (ratio)
1490b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1491fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1492fcd17211SLen Brown 
1493fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1494fcd17211SLen Brown 	if (ratio)
1495b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1496fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1497fcd17211SLen Brown 	return;
1498fcd17211SLen Brown }
1499fcd17211SLen Brown 
1500fcd17211SLen Brown static void
1501fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1502fcd17211SLen Brown {
1503fcd17211SLen Brown 	unsigned long long msr;
1504fcd17211SLen Brown 	unsigned int ratio;
15056574a5d5SLen Brown 
15067ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
15076574a5d5SLen Brown 
1508b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
15096574a5d5SLen Brown 
15106574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
15116574a5d5SLen Brown 	if (ratio)
1512b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
15136574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15146574a5d5SLen Brown 
15156574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
15166574a5d5SLen Brown 	if (ratio)
1517b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
15186574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15196574a5d5SLen Brown 
15206574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
15216574a5d5SLen Brown 	if (ratio)
1522b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
15236574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15246574a5d5SLen Brown 
15256574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
15266574a5d5SLen Brown 	if (ratio)
1527b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
15286574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15296574a5d5SLen Brown 
15306574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
15316574a5d5SLen Brown 	if (ratio)
1532b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
15336574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15346574a5d5SLen Brown 
15356574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
15366574a5d5SLen Brown 	if (ratio)
1537b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
15386574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15396574a5d5SLen Brown 
15406574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
15416574a5d5SLen Brown 	if (ratio)
1542b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
15436574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15446574a5d5SLen Brown 
15456574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
15466574a5d5SLen Brown 	if (ratio)
1547b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
15486574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1549103a8feaSLen Brown 	return;
1550fcd17211SLen Brown }
1551fcd17211SLen Brown 
1552fcd17211SLen Brown static void
1553fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void)
1554fcd17211SLen Brown {
1555fcd17211SLen Brown 	unsigned long long msr;
1556fcd17211SLen Brown 	unsigned int ratio;
1557103a8feaSLen Brown 
15587ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1559103a8feaSLen Brown 
1560b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
15616574a5d5SLen Brown 
15626574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
15636574a5d5SLen Brown 	if (ratio)
1564b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
15656574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15666574a5d5SLen Brown 
15676574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
15686574a5d5SLen Brown 	if (ratio)
1569b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
15706574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15716574a5d5SLen Brown 
15726574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
15736574a5d5SLen Brown 	if (ratio)
1574b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
15756574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15766574a5d5SLen Brown 
15776574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
15786574a5d5SLen Brown 	if (ratio)
1579b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
15806574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
15816574a5d5SLen Brown 
1582103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
1583103a8feaSLen Brown 	if (ratio)
1584b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1585103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1586103a8feaSLen Brown 
1587103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
1588103a8feaSLen Brown 	if (ratio)
1589b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1590103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1591103a8feaSLen Brown 
1592103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1593103a8feaSLen Brown 	if (ratio)
1594b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1595103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1596103a8feaSLen Brown 
1597103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
1598103a8feaSLen Brown 	if (ratio)
1599b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1600103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1601fcd17211SLen Brown 	return;
1602fcd17211SLen Brown }
16033a9a941dSLen Brown 
1604fcd17211SLen Brown static void
1605fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
1606fb5d4327SDasaratharaman Chandramouli {
1607cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
1608cbf97abaSHubert Chrzaniuk 
1609fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
1610cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
1611cbf97abaSHubert Chrzaniuk 	int i, b_nr;
1612cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
1613cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
1614fb5d4327SDasaratharaman Chandramouli 
1615ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1616fb5d4327SDasaratharaman Chandramouli 
1617b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
1618bfae2052SLen Brown 		base_cpu, msr);
1619fb5d4327SDasaratharaman Chandramouli 
1620fb5d4327SDasaratharaman Chandramouli 	/**
1621fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
1622cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
1623cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
1624fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
1625fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
1626fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
1627fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
1628fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
1629fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
1630fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
1631fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
1632fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
1633fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
1634fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
1635fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
1636fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
1637fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
1638fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
1639fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
1640fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
1641fb5d4327SDasaratharaman Chandramouli 	 */
1642fb5d4327SDasaratharaman Chandramouli 
1643cbf97abaSHubert Chrzaniuk 	b_nr = 0;
1644cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
1645cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
1646cbf97abaSHubert Chrzaniuk 
1647cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
1648fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
1649cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
1650fb5d4327SDasaratharaman Chandramouli 
1651cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
1652cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1653cbf97abaSHubert Chrzaniuk 		b_nr++;
1654fb5d4327SDasaratharaman Chandramouli 	}
1655cbf97abaSHubert Chrzaniuk 
1656cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
1657cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
1658b7d8c148SLen Brown 			fprintf(outf,
1659fb5d4327SDasaratharaman Chandramouli 				"%d * %.0f = %.0f MHz max turbo %d active cores\n",
1660cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
1661fb5d4327SDasaratharaman Chandramouli }
1662fb5d4327SDasaratharaman Chandramouli 
1663fb5d4327SDasaratharaman Chandramouli static void
1664fcd17211SLen Brown dump_nhm_cst_cfg(void)
1665fcd17211SLen Brown {
1666fcd17211SLen Brown 	unsigned long long msr;
1667fcd17211SLen Brown 
16687ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1669fcd17211SLen Brown 
1670fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
1671fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
1672fcd17211SLen Brown 
1673b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
1674fcd17211SLen Brown 
1675b7d8c148SLen Brown 	fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1676fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1677fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1678fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1679fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1680fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
16816c34f160SLen Brown 		(unsigned int)msr & 0xF,
1682fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
1683fcd17211SLen Brown 	return;
1684103a8feaSLen Brown }
1685103a8feaSLen Brown 
16866fb3143bSLen Brown static void
16876fb3143bSLen Brown dump_config_tdp(void)
16886fb3143bSLen Brown {
16896fb3143bSLen Brown 	unsigned long long msr;
16906fb3143bSLen Brown 
16916fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1692b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1693685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
16946fb3143bSLen Brown 
16956fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1696b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
16976fb3143bSLen Brown 	if (msr) {
1698685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1699685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1700685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1701685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
17026fb3143bSLen Brown 	}
1703b7d8c148SLen Brown 	fprintf(outf, ")\n");
17046fb3143bSLen Brown 
17056fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1706b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
17076fb3143bSLen Brown 	if (msr) {
1708685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1709685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1710685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1711685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
17126fb3143bSLen Brown 	}
1713b7d8c148SLen Brown 	fprintf(outf, ")\n");
17146fb3143bSLen Brown 
17156fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1716b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
17176fb3143bSLen Brown 	if ((msr) & 0x3)
1718b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1719b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1720b7d8c148SLen Brown 	fprintf(outf, ")\n");
17216fb3143bSLen Brown 
17226fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1723b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
1724685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
1725b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1726b7d8c148SLen Brown 	fprintf(outf, ")\n");
17276fb3143bSLen Brown }
17285a63426eSLen Brown 
17295a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
17305a63426eSLen Brown 
17315a63426eSLen Brown void print_irtl(void)
17325a63426eSLen Brown {
17335a63426eSLen Brown 	unsigned long long msr;
17345a63426eSLen Brown 
17355a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
17365a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
17375a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
17385a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
17395a63426eSLen Brown 
17405a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
17415a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
17425a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
17435a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
17445a63426eSLen Brown 
17455a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
17465a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
17475a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
17485a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
17495a63426eSLen Brown 
17505a63426eSLen Brown 	if (!do_irtl_hsw)
17515a63426eSLen Brown 		return;
17525a63426eSLen Brown 
17535a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
17545a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
17555a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
17565a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
17575a63426eSLen Brown 
17585a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
17595a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
17605a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
17615a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
17625a63426eSLen Brown 
17635a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
17645a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
17655a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
17665a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
17675a63426eSLen Brown 
17685a63426eSLen Brown }
176936229897SLen Brown void free_fd_percpu(void)
177036229897SLen Brown {
177136229897SLen Brown 	int i;
177236229897SLen Brown 
177301a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
177436229897SLen Brown 		if (fd_percpu[i] != 0)
177536229897SLen Brown 			close(fd_percpu[i]);
177636229897SLen Brown 	}
177736229897SLen Brown 
177836229897SLen Brown 	free(fd_percpu);
17796fb3143bSLen Brown }
17806fb3143bSLen Brown 
1781c98d5d94SLen Brown void free_all_buffers(void)
1782103a8feaSLen Brown {
1783c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
1784c98d5d94SLen Brown 	cpu_present_set = NULL;
178536229897SLen Brown 	cpu_present_setsize = 0;
1786103a8feaSLen Brown 
1787c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
1788c98d5d94SLen Brown 	cpu_affinity_set = NULL;
1789c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
1790103a8feaSLen Brown 
1791c98d5d94SLen Brown 	free(thread_even);
1792c98d5d94SLen Brown 	free(core_even);
1793c98d5d94SLen Brown 	free(package_even);
1794103a8feaSLen Brown 
1795c98d5d94SLen Brown 	thread_even = NULL;
1796c98d5d94SLen Brown 	core_even = NULL;
1797c98d5d94SLen Brown 	package_even = NULL;
1798103a8feaSLen Brown 
1799c98d5d94SLen Brown 	free(thread_odd);
1800c98d5d94SLen Brown 	free(core_odd);
1801c98d5d94SLen Brown 	free(package_odd);
1802103a8feaSLen Brown 
1803c98d5d94SLen Brown 	thread_odd = NULL;
1804c98d5d94SLen Brown 	core_odd = NULL;
1805c98d5d94SLen Brown 	package_odd = NULL;
1806103a8feaSLen Brown 
1807c98d5d94SLen Brown 	free(output_buffer);
1808c98d5d94SLen Brown 	output_buffer = NULL;
1809c98d5d94SLen Brown 	outp = NULL;
181036229897SLen Brown 
181136229897SLen Brown 	free_fd_percpu();
1812562a2d37SLen Brown 
1813562a2d37SLen Brown 	free(irq_column_2_cpu);
1814562a2d37SLen Brown 	free(irqs_per_cpu);
1815103a8feaSLen Brown }
1816103a8feaSLen Brown 
1817103a8feaSLen Brown /*
181857a42a34SJosh Triplett  * Open a file, and exit on failure
181957a42a34SJosh Triplett  */
182057a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode)
182157a42a34SJosh Triplett {
1822b7d8c148SLen Brown 	FILE *filep = fopen(path, mode);
1823b2c95d90SJosh Triplett 	if (!filep)
1824b2c95d90SJosh Triplett 		err(1, "%s: open failed", path);
182557a42a34SJosh Triplett 	return filep;
182657a42a34SJosh Triplett }
182757a42a34SJosh Triplett 
182857a42a34SJosh Triplett /*
182995aebc44SJosh Triplett  * Parse a file containing a single int.
183095aebc44SJosh Triplett  */
183195aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
183295aebc44SJosh Triplett {
183395aebc44SJosh Triplett 	va_list args;
183495aebc44SJosh Triplett 	char path[PATH_MAX];
183595aebc44SJosh Triplett 	FILE *filep;
183695aebc44SJosh Triplett 	int value;
183795aebc44SJosh Triplett 
183895aebc44SJosh Triplett 	va_start(args, fmt);
183995aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
184095aebc44SJosh Triplett 	va_end(args);
184157a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1842b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
1843b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
184495aebc44SJosh Triplett 	fclose(filep);
184595aebc44SJosh Triplett 	return value;
184695aebc44SJosh Triplett }
184795aebc44SJosh Triplett 
184895aebc44SJosh Triplett /*
1849e275b388SDasaratharaman Chandramouli  * get_cpu_position_in_core(cpu)
1850e275b388SDasaratharaman Chandramouli  * return the position of the CPU among its HT siblings in the core
1851e275b388SDasaratharaman Chandramouli  * return -1 if the sibling is not in list
1852103a8feaSLen Brown  */
1853e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu)
1854103a8feaSLen Brown {
1855e275b388SDasaratharaman Chandramouli 	char path[64];
1856e275b388SDasaratharaman Chandramouli 	FILE *filep;
1857e275b388SDasaratharaman Chandramouli 	int this_cpu;
1858e275b388SDasaratharaman Chandramouli 	char character;
1859e275b388SDasaratharaman Chandramouli 	int i;
1860e275b388SDasaratharaman Chandramouli 
1861e275b388SDasaratharaman Chandramouli 	sprintf(path,
1862e275b388SDasaratharaman Chandramouli 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1863e275b388SDasaratharaman Chandramouli 		cpu);
1864e275b388SDasaratharaman Chandramouli 	filep = fopen(path, "r");
1865e275b388SDasaratharaman Chandramouli 	if (filep == NULL) {
1866e275b388SDasaratharaman Chandramouli 		perror(path);
1867e275b388SDasaratharaman Chandramouli 		exit(1);
1868e275b388SDasaratharaman Chandramouli 	}
1869e275b388SDasaratharaman Chandramouli 
1870e275b388SDasaratharaman Chandramouli 	for (i = 0; i < topo.num_threads_per_core; i++) {
1871e275b388SDasaratharaman Chandramouli 		fscanf(filep, "%d", &this_cpu);
1872e275b388SDasaratharaman Chandramouli 		if (this_cpu == cpu) {
1873e275b388SDasaratharaman Chandramouli 			fclose(filep);
1874e275b388SDasaratharaman Chandramouli 			return i;
1875e275b388SDasaratharaman Chandramouli 		}
1876e275b388SDasaratharaman Chandramouli 
1877e275b388SDasaratharaman Chandramouli 		/* Account for no separator after last thread*/
1878e275b388SDasaratharaman Chandramouli 		if (i != (topo.num_threads_per_core - 1))
1879e275b388SDasaratharaman Chandramouli 			fscanf(filep, "%c", &character);
1880e275b388SDasaratharaman Chandramouli 	}
1881e275b388SDasaratharaman Chandramouli 
1882e275b388SDasaratharaman Chandramouli 	fclose(filep);
1883e275b388SDasaratharaman Chandramouli 	return -1;
1884c98d5d94SLen Brown }
1885103a8feaSLen Brown 
1886c98d5d94SLen Brown /*
1887c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
1888c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
1889c98d5d94SLen Brown  */
1890c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
1891c98d5d94SLen Brown {
189295aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1893103a8feaSLen Brown }
1894103a8feaSLen Brown 
1895103a8feaSLen Brown int get_physical_package_id(int cpu)
1896103a8feaSLen Brown {
189795aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1898103a8feaSLen Brown }
1899103a8feaSLen Brown 
1900103a8feaSLen Brown int get_core_id(int cpu)
1901103a8feaSLen Brown {
190295aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1903103a8feaSLen Brown }
1904103a8feaSLen Brown 
1905c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
1906c98d5d94SLen Brown {
1907c98d5d94SLen Brown 	char path[80];
1908c98d5d94SLen Brown 	FILE *filep;
1909e275b388SDasaratharaman Chandramouli 	int sib1;
1910e275b388SDasaratharaman Chandramouli 	int matches = 0;
1911c98d5d94SLen Brown 	char character;
1912e275b388SDasaratharaman Chandramouli 	char str[100];
1913e275b388SDasaratharaman Chandramouli 	char *ch;
1914c98d5d94SLen Brown 
1915c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
191657a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1917e275b388SDasaratharaman Chandramouli 
1918103a8feaSLen Brown 	/*
1919c98d5d94SLen Brown 	 * file format:
1920e275b388SDasaratharaman Chandramouli 	 * A ',' separated or '-' separated set of numbers
1921e275b388SDasaratharaman Chandramouli 	 * (eg 1-2 or 1,3,4,5)
1922c98d5d94SLen Brown 	 */
1923e275b388SDasaratharaman Chandramouli 	fscanf(filep, "%d%c\n", &sib1, &character);
1924e275b388SDasaratharaman Chandramouli 	fseek(filep, 0, SEEK_SET);
1925e275b388SDasaratharaman Chandramouli 	fgets(str, 100, filep);
1926e275b388SDasaratharaman Chandramouli 	ch = strchr(str, character);
1927e275b388SDasaratharaman Chandramouli 	while (ch != NULL) {
1928e275b388SDasaratharaman Chandramouli 		matches++;
1929e275b388SDasaratharaman Chandramouli 		ch = strchr(ch+1, character);
1930e275b388SDasaratharaman Chandramouli 	}
1931c98d5d94SLen Brown 
1932c98d5d94SLen Brown 	fclose(filep);
1933e275b388SDasaratharaman Chandramouli 	return matches+1;
1934c98d5d94SLen Brown }
1935c98d5d94SLen Brown 
1936c98d5d94SLen Brown /*
1937c98d5d94SLen Brown  * run func(thread, core, package) in topology order
1938c98d5d94SLen Brown  * skip non-present cpus
1939103a8feaSLen Brown  */
1940103a8feaSLen Brown 
1941c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1942c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
1943c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
1944c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
1945c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
1946c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
1947c98d5d94SLen Brown {
1948c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
1949c98d5d94SLen Brown 
1950c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1951c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1952c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
1953c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
1954c98d5d94SLen Brown 				struct thread_data *t, *t2;
1955c98d5d94SLen Brown 				struct core_data *c, *c2;
1956c98d5d94SLen Brown 				struct pkg_data *p, *p2;
1957c98d5d94SLen Brown 
1958c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1959c98d5d94SLen Brown 
1960c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
1961c98d5d94SLen Brown 					continue;
1962c98d5d94SLen Brown 
1963c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1964c98d5d94SLen Brown 
1965c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
1966c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
1967c98d5d94SLen Brown 
1968c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
1969c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
1970c98d5d94SLen Brown 
1971c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
1972c98d5d94SLen Brown 				if (retval)
1973c98d5d94SLen Brown 					return retval;
1974c98d5d94SLen Brown 			}
1975c98d5d94SLen Brown 		}
1976c98d5d94SLen Brown 	}
1977c98d5d94SLen Brown 	return 0;
1978c98d5d94SLen Brown }
1979c98d5d94SLen Brown 
1980c98d5d94SLen Brown /*
1981c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
1982c98d5d94SLen Brown  * return max_cpu number
1983c98d5d94SLen Brown  */
1984c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
1985103a8feaSLen Brown {
1986103a8feaSLen Brown 	FILE *fp;
1987c98d5d94SLen Brown 	int cpu_num;
1988103a8feaSLen Brown 	int retval;
1989103a8feaSLen Brown 
199057a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
1991103a8feaSLen Brown 
1992103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1993b2c95d90SJosh Triplett 	if (retval != 0)
1994b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
1995103a8feaSLen Brown 
1996c98d5d94SLen Brown 	while (1) {
1997c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1998103a8feaSLen Brown 		if (retval != 1)
1999103a8feaSLen Brown 			break;
2000103a8feaSLen Brown 
2001c98d5d94SLen Brown 		retval = func(cpu_num);
2002c98d5d94SLen Brown 		if (retval) {
2003c98d5d94SLen Brown 			fclose(fp);
2004c98d5d94SLen Brown 			return(retval);
2005c98d5d94SLen Brown 		}
2006103a8feaSLen Brown 	}
2007103a8feaSLen Brown 	fclose(fp);
2008c98d5d94SLen Brown 	return 0;
2009103a8feaSLen Brown }
2010103a8feaSLen Brown 
2011103a8feaSLen Brown void re_initialize(void)
2012103a8feaSLen Brown {
2013c98d5d94SLen Brown 	free_all_buffers();
2014c98d5d94SLen Brown 	setup_all_buffers();
2015c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2016103a8feaSLen Brown }
2017103a8feaSLen Brown 
2018c98d5d94SLen Brown 
2019103a8feaSLen Brown /*
2020c98d5d94SLen Brown  * count_cpus()
2021c98d5d94SLen Brown  * remember the last one seen, it will be the max
2022103a8feaSLen Brown  */
2023c98d5d94SLen Brown int count_cpus(int cpu)
2024103a8feaSLen Brown {
2025c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
2026c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
2027103a8feaSLen Brown 
2028c98d5d94SLen Brown 	topo.num_cpus += 1;
2029c98d5d94SLen Brown 	return 0;
2030103a8feaSLen Brown }
2031c98d5d94SLen Brown int mark_cpu_present(int cpu)
2032c98d5d94SLen Brown {
2033c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
203415aaa346SLen Brown 	return 0;
2035103a8feaSLen Brown }
2036103a8feaSLen Brown 
2037562a2d37SLen Brown /*
2038562a2d37SLen Brown  * snapshot_proc_interrupts()
2039562a2d37SLen Brown  *
2040562a2d37SLen Brown  * read and record summary of /proc/interrupts
2041562a2d37SLen Brown  *
2042562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2043562a2d37SLen Brown  */
2044562a2d37SLen Brown int snapshot_proc_interrupts(void)
2045562a2d37SLen Brown {
2046562a2d37SLen Brown 	static FILE *fp;
2047562a2d37SLen Brown 	int column, retval;
2048562a2d37SLen Brown 
2049562a2d37SLen Brown 	if (fp == NULL)
2050562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2051562a2d37SLen Brown 	else
2052562a2d37SLen Brown 		rewind(fp);
2053562a2d37SLen Brown 
2054562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2055562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2056562a2d37SLen Brown 		int cpu_number;
2057562a2d37SLen Brown 
2058562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2059562a2d37SLen Brown 		if (retval != 1)
2060562a2d37SLen Brown 			break;
2061562a2d37SLen Brown 
2062562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2063562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2064562a2d37SLen Brown 			return 1;
2065562a2d37SLen Brown 		}
2066562a2d37SLen Brown 
2067562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2068562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2069562a2d37SLen Brown 	}
2070562a2d37SLen Brown 
2071562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2072562a2d37SLen Brown 	while (1) {
2073562a2d37SLen Brown 		int column;
2074562a2d37SLen Brown 		char buf[64];
2075562a2d37SLen Brown 
2076562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2077562a2d37SLen Brown 		if (retval != 1)
2078562a2d37SLen Brown 			break;
2079562a2d37SLen Brown 
2080562a2d37SLen Brown 		/* read the count per cpu */
2081562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2082562a2d37SLen Brown 
2083562a2d37SLen Brown 			int cpu_number, irq_count;
2084562a2d37SLen Brown 
2085562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2086562a2d37SLen Brown 			if (retval != 1)
2087562a2d37SLen Brown 				break;
2088562a2d37SLen Brown 
2089562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2090562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2091562a2d37SLen Brown 
2092562a2d37SLen Brown 		}
2093562a2d37SLen Brown 
2094562a2d37SLen Brown 		while (getc(fp) != '\n')
2095562a2d37SLen Brown 			;	/* flush interrupt description */
2096562a2d37SLen Brown 
2097562a2d37SLen Brown 	}
2098562a2d37SLen Brown 	return 0;
2099562a2d37SLen Brown }
210027d47356SLen Brown /*
2101fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2102fdf676e5SLen Brown  *
2103fdf676e5SLen Brown  * record snapshot of
2104fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2105fdf676e5SLen Brown  *
2106fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2107fdf676e5SLen Brown  */
2108fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2109fdf676e5SLen Brown {
2110fdf676e5SLen Brown 	FILE *fp;
2111fdf676e5SLen Brown 	int retval;
2112fdf676e5SLen Brown 
2113fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2114fdf676e5SLen Brown 
2115fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2116fdf676e5SLen Brown 	if (retval != 1)
2117fdf676e5SLen Brown 		err(1, "GFX rc6");
2118fdf676e5SLen Brown 
2119fdf676e5SLen Brown 	fclose(fp);
2120fdf676e5SLen Brown 
2121fdf676e5SLen Brown 	return 0;
2122fdf676e5SLen Brown }
2123fdf676e5SLen Brown /*
212427d47356SLen Brown  * snapshot_gfx_mhz()
212527d47356SLen Brown  *
212627d47356SLen Brown  * record snapshot of
212727d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
212827d47356SLen Brown  *
212927d47356SLen Brown  * return 1 if config change requires a restart, else return 0
213027d47356SLen Brown  */
213127d47356SLen Brown int snapshot_gfx_mhz(void)
213227d47356SLen Brown {
213327d47356SLen Brown 	static FILE *fp;
213427d47356SLen Brown 	int retval;
213527d47356SLen Brown 
213627d47356SLen Brown 	if (fp == NULL)
213727d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
213827d47356SLen Brown 	else
213927d47356SLen Brown 		rewind(fp);
214027d47356SLen Brown 
214127d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
214227d47356SLen Brown 	if (retval != 1)
214327d47356SLen Brown 		err(1, "GFX MHz");
214427d47356SLen Brown 
214527d47356SLen Brown 	return 0;
214627d47356SLen Brown }
2147562a2d37SLen Brown 
2148562a2d37SLen Brown /*
2149562a2d37SLen Brown  * snapshot /proc and /sys files
2150562a2d37SLen Brown  *
2151562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2152562a2d37SLen Brown  */
2153562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2154562a2d37SLen Brown {
2155562a2d37SLen Brown 	if (snapshot_proc_interrupts())
2156562a2d37SLen Brown 		return 1;
2157562a2d37SLen Brown 
2158fdf676e5SLen Brown 	if (do_gfx_rc6_ms)
2159fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2160fdf676e5SLen Brown 
216127d47356SLen Brown 	if (do_gfx_mhz)
216227d47356SLen Brown 		snapshot_gfx_mhz();
216327d47356SLen Brown 
2164562a2d37SLen Brown 	return 0;
2165562a2d37SLen Brown }
2166562a2d37SLen Brown 
2167103a8feaSLen Brown void turbostat_loop()
2168103a8feaSLen Brown {
2169c98d5d94SLen Brown 	int retval;
2170e52966c0SLen Brown 	int restarted = 0;
2171c98d5d94SLen Brown 
2172103a8feaSLen Brown restart:
2173e52966c0SLen Brown 	restarted++;
2174e52966c0SLen Brown 
2175562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2176c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2177d91bb17cSLen Brown 	if (retval < -1) {
2178d91bb17cSLen Brown 		exit(retval);
2179d91bb17cSLen Brown 	} else if (retval == -1) {
2180e52966c0SLen Brown 		if (restarted > 1) {
2181e52966c0SLen Brown 			exit(retval);
2182e52966c0SLen Brown 		}
2183c98d5d94SLen Brown 		re_initialize();
2184c98d5d94SLen Brown 		goto restart;
2185c98d5d94SLen Brown 	}
2186e52966c0SLen Brown 	restarted = 0;
2187103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2188103a8feaSLen Brown 
2189103a8feaSLen Brown 	while (1) {
2190c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2191103a8feaSLen Brown 			re_initialize();
2192103a8feaSLen Brown 			goto restart;
2193103a8feaSLen Brown 		}
21942a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2195562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2196562a2d37SLen Brown 			goto restart;
2197c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
2198d91bb17cSLen Brown 		if (retval < -1) {
2199d91bb17cSLen Brown 			exit(retval);
2200d91bb17cSLen Brown 		} else if (retval == -1) {
220115aaa346SLen Brown 			re_initialize();
220215aaa346SLen Brown 			goto restart;
220315aaa346SLen Brown 		}
2204103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
2205103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
2206ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2207ba3dec99SLen Brown 			re_initialize();
2208ba3dec99SLen Brown 			goto restart;
2209ba3dec99SLen Brown 		}
2210c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
2211c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
2212b7d8c148SLen Brown 		flush_output_stdout();
22132a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2214562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2215562a2d37SLen Brown 			goto restart;
2216c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2217d91bb17cSLen Brown 		if (retval < -1) {
2218d91bb17cSLen Brown 			exit(retval);
2219d91bb17cSLen Brown 		} else if (retval == -1) {
2220103a8feaSLen Brown 			re_initialize();
2221103a8feaSLen Brown 			goto restart;
2222103a8feaSLen Brown 		}
2223103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
2224103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
2225ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2226ba3dec99SLen Brown 			re_initialize();
2227ba3dec99SLen Brown 			goto restart;
2228ba3dec99SLen Brown 		}
2229c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
2230c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
2231b7d8c148SLen Brown 		flush_output_stdout();
2232103a8feaSLen Brown 	}
2233103a8feaSLen Brown }
2234103a8feaSLen Brown 
2235103a8feaSLen Brown void check_dev_msr()
2236103a8feaSLen Brown {
2237103a8feaSLen Brown 	struct stat sb;
22387ce7d5deSPrarit Bhargava 	char pathname[32];
2239103a8feaSLen Brown 
22407ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
22417ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
2242a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2243d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2244103a8feaSLen Brown }
2245103a8feaSLen Brown 
224698481e79SLen Brown void check_permissions()
2247103a8feaSLen Brown {
224898481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
224998481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
225098481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
225198481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
225298481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
225398481e79SLen Brown 	int do_exit = 0;
22547ce7d5deSPrarit Bhargava 	char pathname[32];
225598481e79SLen Brown 
225698481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
225798481e79SLen Brown 	cap_header->pid = getpid();
225898481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
225998481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
226098481e79SLen Brown 		err(-6, "capget(2) failed");
226198481e79SLen Brown 
226298481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
226398481e79SLen Brown 		do_exit++;
226498481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
226598481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
226698481e79SLen Brown 	}
226798481e79SLen Brown 
226898481e79SLen Brown 	/* test file permissions */
22697ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
22707ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
227198481e79SLen Brown 		do_exit++;
227298481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
227398481e79SLen Brown 	}
227498481e79SLen Brown 
227598481e79SLen Brown 	/* if all else fails, thell them to be root */
227698481e79SLen Brown 	if (do_exit)
2277b2c95d90SJosh Triplett 		if (getuid() != 0)
2278d7899447SLen Brown 			warnx("... or simply run as root");
227998481e79SLen Brown 
228098481e79SLen Brown 	if (do_exit)
228198481e79SLen Brown 		exit(-6);
2282103a8feaSLen Brown }
2283103a8feaSLen Brown 
2284d7899447SLen Brown /*
2285d7899447SLen Brown  * NHM adds support for additional MSRs:
2286d7899447SLen Brown  *
2287d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
2288d7899447SLen Brown  *
2289ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
2290d7899447SLen Brown  * MSR_NHM_SNB_PKG_CST_CFG_CTL     0x000000e2
2291d7899447SLen Brown  *
2292d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
2293d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
2294d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
2295d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
2296d7899447SLen Brown  *
2297ee7e38e3SLen Brown  * Side effect:
2298ee7e38e3SLen Brown  * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
2299d7899447SLen Brown  */
2300ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
2301103a8feaSLen Brown {
2302ee7e38e3SLen Brown 	unsigned long long msr;
230321ed5574SLen Brown 	unsigned int base_ratio;
2304ee7e38e3SLen Brown 	int *pkg_cstate_limits;
2305ee7e38e3SLen Brown 
2306103a8feaSLen Brown 	if (!genuine_intel)
2307103a8feaSLen Brown 		return 0;
2308103a8feaSLen Brown 
2309103a8feaSLen Brown 	if (family != 6)
2310103a8feaSLen Brown 		return 0;
2311103a8feaSLen Brown 
231221ed5574SLen Brown 	bclk = discover_bclk(family, model);
231321ed5574SLen Brown 
2314103a8feaSLen Brown 	switch (model) {
2315869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2316869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2317103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
2318869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
2319869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
2320869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2321869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2322ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
2323ee7e38e3SLen Brown 		break;
2324869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
2325869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
2326869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2327869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2328ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
2329ee7e38e3SLen Brown 		break;
2330869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2331869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2332869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2333869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2334869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2335869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2336869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2337869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2338869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2339869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2340869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2341869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2342ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
2343ee7e38e3SLen Brown 		break;
2344d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
2345d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
2346d8ebb442SLen Brown 		break;
2347869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
2348869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
2349ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
2350ee7e38e3SLen Brown 		break;
2351869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
2352ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
2353ee7e38e3SLen Brown 		break;
2354869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
2355005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2356ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
2357ee7e38e3SLen Brown 		break;
2358869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
2359869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
2360e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
2361e4085d54SLen Brown 		break;
2362103a8feaSLen Brown 	default:
2363103a8feaSLen Brown 		return 0;
2364103a8feaSLen Brown 	}
23657ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
2366e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2367ee7e38e3SLen Brown 
2368ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
236921ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
237021ed5574SLen Brown 
237121ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
237221ed5574SLen Brown 	has_base_hz = 1;
2373ee7e38e3SLen Brown 	return 1;
2374103a8feaSLen Brown }
2375d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2376d7899447SLen Brown {
2377d7899447SLen Brown 	switch (model) {
2378d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
2379869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2380869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2381869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
2382005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2383d7899447SLen Brown 		return 0;
2384d7899447SLen Brown 	default:
2385d7899447SLen Brown 		return 1;
2386d7899447SLen Brown 	}
2387d7899447SLen Brown }
23886574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
23896574a5d5SLen Brown {
23906574a5d5SLen Brown 	if (!genuine_intel)
23916574a5d5SLen Brown 		return 0;
23926574a5d5SLen Brown 
23936574a5d5SLen Brown 	if (family != 6)
23946574a5d5SLen Brown 		return 0;
23956574a5d5SLen Brown 
23966574a5d5SLen Brown 	switch (model) {
2397869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2398869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
23996574a5d5SLen Brown 		return 1;
24006574a5d5SLen Brown 	default:
24016574a5d5SLen Brown 		return 0;
24026574a5d5SLen Brown 	}
24036574a5d5SLen Brown }
2404fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2405fcd17211SLen Brown {
2406fcd17211SLen Brown 	if (!genuine_intel)
2407fcd17211SLen Brown 		return 0;
2408fcd17211SLen Brown 
2409fcd17211SLen Brown 	if (family != 6)
2410fcd17211SLen Brown 		return 0;
2411fcd17211SLen Brown 
2412fcd17211SLen Brown 	switch (model) {
2413869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
2414fcd17211SLen Brown 		return 1;
2415fcd17211SLen Brown 	default:
2416fcd17211SLen Brown 		return 0;
2417fcd17211SLen Brown 	}
2418fcd17211SLen Brown }
2419fcd17211SLen Brown 
2420fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2421fb5d4327SDasaratharaman Chandramouli {
2422fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
2423fb5d4327SDasaratharaman Chandramouli 		return 0;
2424fb5d4327SDasaratharaman Chandramouli 
2425fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
2426fb5d4327SDasaratharaman Chandramouli 		return 0;
2427fb5d4327SDasaratharaman Chandramouli 
2428fb5d4327SDasaratharaman Chandramouli 	switch (model) {
2429869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2430005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2431fb5d4327SDasaratharaman Chandramouli 		return 1;
2432fb5d4327SDasaratharaman Chandramouli 	default:
2433fb5d4327SDasaratharaman Chandramouli 		return 0;
2434fb5d4327SDasaratharaman Chandramouli 	}
2435fb5d4327SDasaratharaman Chandramouli }
24366fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
24376fb3143bSLen Brown {
24386fb3143bSLen Brown 	if (!genuine_intel)
24396fb3143bSLen Brown 		return 0;
24406fb3143bSLen Brown 
24416fb3143bSLen Brown 	if (family != 6)
24426fb3143bSLen Brown 		return 0;
24436fb3143bSLen Brown 
24446fb3143bSLen Brown 	switch (model) {
2445869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2446869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2447869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2448869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2449869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2450869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2451869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2452869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2453869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2454869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2455869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2456869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2457869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2458869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
24596fb3143bSLen Brown 
2460869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2461005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
24626fb3143bSLen Brown 		return 1;
24636fb3143bSLen Brown 	default:
24646fb3143bSLen Brown 		return 0;
24656fb3143bSLen Brown 	}
24666fb3143bSLen Brown }
24676fb3143bSLen Brown 
2468fcd17211SLen Brown static void
24691b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
2470fcd17211SLen Brown {
2471fcd17211SLen Brown 	if (!do_nhm_platform_info)
2472fcd17211SLen Brown 		return;
2473fcd17211SLen Brown 
2474fcd17211SLen Brown 	dump_nhm_platform_info();
2475fcd17211SLen Brown 
2476fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
2477fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
2478fcd17211SLen Brown 
2479fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
2480fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
2481fcd17211SLen Brown 
2482fcd17211SLen Brown 	if (has_nhm_turbo_ratio_limit(family, model))
2483fcd17211SLen Brown 		dump_nhm_turbo_ratio_limits();
2484fcd17211SLen Brown 
2485fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
2486fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
2487fb5d4327SDasaratharaman Chandramouli 
24886fb3143bSLen Brown 	if (has_config_tdp(family, model))
24896fb3143bSLen Brown 		dump_config_tdp();
24906fb3143bSLen Brown 
2491fcd17211SLen Brown 	dump_nhm_cst_cfg();
2492fcd17211SLen Brown }
2493fcd17211SLen Brown 
24946574a5d5SLen Brown 
2495889facbeSLen Brown /*
2496889facbeSLen Brown  * print_epb()
2497889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
2498889facbeSLen Brown  */
2499889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2500889facbeSLen Brown {
2501889facbeSLen Brown 	unsigned long long msr;
2502889facbeSLen Brown 	char *epb_string;
2503889facbeSLen Brown 	int cpu;
2504889facbeSLen Brown 
2505889facbeSLen Brown 	if (!has_epb)
2506889facbeSLen Brown 		return 0;
2507889facbeSLen Brown 
2508889facbeSLen Brown 	cpu = t->cpu_id;
2509889facbeSLen Brown 
2510889facbeSLen Brown 	/* EPB is per-package */
2511889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2512889facbeSLen Brown 		return 0;
2513889facbeSLen Brown 
2514889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2515b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2516889facbeSLen Brown 		return -1;
2517889facbeSLen Brown 	}
2518889facbeSLen Brown 
2519889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2520889facbeSLen Brown 		return 0;
2521889facbeSLen Brown 
2522e9be7dd6SLen Brown 	switch (msr & 0xF) {
2523889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
2524889facbeSLen Brown 		epb_string = "performance";
2525889facbeSLen Brown 		break;
2526889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
2527889facbeSLen Brown 		epb_string = "balanced";
2528889facbeSLen Brown 		break;
2529889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
2530889facbeSLen Brown 		epb_string = "powersave";
2531889facbeSLen Brown 		break;
2532889facbeSLen Brown 	default:
2533889facbeSLen Brown 		epb_string = "custom";
2534889facbeSLen Brown 		break;
2535889facbeSLen Brown 	}
2536b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2537889facbeSLen Brown 
2538889facbeSLen Brown 	return 0;
2539889facbeSLen Brown }
25407f5c258eSLen Brown /*
25417f5c258eSLen Brown  * print_hwp()
25427f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
25437f5c258eSLen Brown  */
25447f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
25457f5c258eSLen Brown {
25467f5c258eSLen Brown 	unsigned long long msr;
25477f5c258eSLen Brown 	int cpu;
25487f5c258eSLen Brown 
25497f5c258eSLen Brown 	if (!has_hwp)
25507f5c258eSLen Brown 		return 0;
25517f5c258eSLen Brown 
25527f5c258eSLen Brown 	cpu = t->cpu_id;
25537f5c258eSLen Brown 
25547f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
25557f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
25567f5c258eSLen Brown 		return 0;
25577f5c258eSLen Brown 
25587f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
2559b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
25607f5c258eSLen Brown 		return -1;
25617f5c258eSLen Brown 	}
25627f5c258eSLen Brown 
25637f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
25647f5c258eSLen Brown 		return 0;
25657f5c258eSLen Brown 
2566b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
25677f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
25687f5c258eSLen Brown 
25697f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
25707f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
25717f5c258eSLen Brown 		return 0;
25727f5c258eSLen Brown 
25737f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
25747f5c258eSLen Brown 		return 0;
25757f5c258eSLen Brown 
2576b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
25777f5c258eSLen Brown 			"(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
25787f5c258eSLen Brown 			cpu, msr,
25797f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
25807f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
25817f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
25827f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
25837f5c258eSLen Brown 
25847f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
25857f5c258eSLen Brown 		return 0;
25867f5c258eSLen Brown 
2587b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
25887f5c258eSLen Brown 			"(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
25897f5c258eSLen Brown 			cpu, msr,
25907f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
25917f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
25927f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
25937f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
25947f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
25957f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
25967f5c258eSLen Brown 
25977f5c258eSLen Brown 	if (has_hwp_pkg) {
25987f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
25997f5c258eSLen Brown 			return 0;
26007f5c258eSLen Brown 
2601b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
26027f5c258eSLen Brown 			"(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
26037f5c258eSLen Brown 			cpu, msr,
26047f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
26057f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
26067f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
26077f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
26087f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
26097f5c258eSLen Brown 	}
26107f5c258eSLen Brown 	if (has_hwp_notify) {
26117f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
26127f5c258eSLen Brown 			return 0;
26137f5c258eSLen Brown 
2614b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
26157f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
26167f5c258eSLen Brown 			cpu, msr,
26177f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
26187f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
26197f5c258eSLen Brown 	}
26207f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
26217f5c258eSLen Brown 		return 0;
26227f5c258eSLen Brown 
2623b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
26247f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
26257f5c258eSLen Brown 			cpu, msr,
26267f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
26277f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
2628889facbeSLen Brown 
2629889facbeSLen Brown 	return 0;
2630889facbeSLen Brown }
2631889facbeSLen Brown 
26323a9a941dSLen Brown /*
26333a9a941dSLen Brown  * print_perf_limit()
26343a9a941dSLen Brown  */
26353a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
26363a9a941dSLen Brown {
26373a9a941dSLen Brown 	unsigned long long msr;
26383a9a941dSLen Brown 	int cpu;
26393a9a941dSLen Brown 
26403a9a941dSLen Brown 	cpu = t->cpu_id;
26413a9a941dSLen Brown 
26423a9a941dSLen Brown 	/* per-package */
26433a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
26443a9a941dSLen Brown 		return 0;
26453a9a941dSLen Brown 
26463a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
2647b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
26483a9a941dSLen Brown 		return -1;
26493a9a941dSLen Brown 	}
26503a9a941dSLen Brown 
26513a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
26523a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
2653b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2654b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
2655e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
26563a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
2657e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
2658e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2659e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
2660e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
2661e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
2662e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
2663e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
2664e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
2665e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
2666e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
2667e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
2668e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
2669b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
2670e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
26713a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
2672e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
2673e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2674e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
2675e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
2676e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
2677e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
2678e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
2679e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
2680e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
2681e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
2682e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
2683e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
26843a9a941dSLen Brown 
26853a9a941dSLen Brown 	}
26863a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
26873a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
2688b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2689b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
26903a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
26913a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
26923a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
26933a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
26943a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
26953a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
26963a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
26973a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
2698b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
26993a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
27003a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
27013a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
27023a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
27033a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
27043a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
27053a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
27063a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
27073a9a941dSLen Brown 	}
27083a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
27093a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2710b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2711b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
27123a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
27133a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
27143a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
27153a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
27163a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
27173a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
2718b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
27193a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
27203a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
27213a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
27223a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
27233a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
27243a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
27253a9a941dSLen Brown 	}
27263a9a941dSLen Brown 	return 0;
27273a9a941dSLen Brown }
27283a9a941dSLen Brown 
2729889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
2730889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
2731889facbeSLen Brown 
27321b69317dSColin Ian King double get_tdp(unsigned int model)
2733144b44b1SLen Brown {
2734144b44b1SLen Brown 	unsigned long long msr;
2735144b44b1SLen Brown 
2736144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
27377ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
2738144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2739144b44b1SLen Brown 
2740144b44b1SLen Brown 	switch (model) {
2741869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
2742869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:
2743144b44b1SLen Brown 		return 30.0;
2744144b44b1SLen Brown 	default:
2745144b44b1SLen Brown 		return 135.0;
2746144b44b1SLen Brown 	}
2747144b44b1SLen Brown }
2748144b44b1SLen Brown 
274940ee8e3bSAndrey Semin /*
275040ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
275140ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
275240ee8e3bSAndrey Semin  */
275340ee8e3bSAndrey Semin static double
275440ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
275540ee8e3bSAndrey Semin {
275640ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
275740ee8e3bSAndrey Semin 
275840ee8e3bSAndrey Semin 	switch (model) {
2759869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2760869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2761869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2762869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
2763005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
276440ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
276540ee8e3bSAndrey Semin 	default:
276640ee8e3bSAndrey Semin 		return (rapl_energy_units);
276740ee8e3bSAndrey Semin 	}
276840ee8e3bSAndrey Semin }
276940ee8e3bSAndrey Semin 
2770144b44b1SLen Brown 
2771889facbeSLen Brown /*
2772889facbeSLen Brown  * rapl_probe()
2773889facbeSLen Brown  *
2774144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2775889facbeSLen Brown  */
2776889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
2777889facbeSLen Brown {
2778889facbeSLen Brown 	unsigned long long msr;
2779144b44b1SLen Brown 	unsigned int time_unit;
2780889facbeSLen Brown 	double tdp;
2781889facbeSLen Brown 
2782889facbeSLen Brown 	if (!genuine_intel)
2783889facbeSLen Brown 		return;
2784889facbeSLen Brown 
2785889facbeSLen Brown 	if (family != 6)
2786889facbeSLen Brown 		return;
2787889facbeSLen Brown 
2788889facbeSLen Brown 	switch (model) {
2789869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
2790869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
2791869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2792869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2793869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2794869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2795869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2796144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
2797889facbeSLen Brown 		break;
2798869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
2799e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
2800e4085d54SLen Brown 		break;
2801869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2802869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2803869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2804869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
28050b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
28060b2bb692SLen Brown 		break;
2807869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2808869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2809869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2810869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
2811869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
2812005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
28130b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2814e6f9bb3cSLen Brown 		break;
2815869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
2816869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
28170b2bb692SLen 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;
2818144b44b1SLen Brown 		break;
2819869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
2820869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
2821144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
2822889facbeSLen Brown 		break;
2823869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
28240f644909SJacob 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;
28250f644909SJacob Pan 		break;
2826889facbeSLen Brown 	default:
2827889facbeSLen Brown 		return;
2828889facbeSLen Brown 	}
2829889facbeSLen Brown 
2830889facbeSLen Brown 	/* units on package 0, verify later other packages match */
28317ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
2832889facbeSLen Brown 		return;
2833889facbeSLen Brown 
2834889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
2835869ce69eSLen Brown 	if (model == INTEL_FAM6_ATOM_SILVERMONT1)
2836144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2837144b44b1SLen Brown 	else
2838889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
2839889facbeSLen Brown 
284040ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
284140ee8e3bSAndrey Semin 
2842144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
2843144b44b1SLen Brown 	if (time_unit == 0)
2844144b44b1SLen Brown 		time_unit = 0xA;
2845889facbeSLen Brown 
2846144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
2847144b44b1SLen Brown 
2848144b44b1SLen Brown 	tdp = get_tdp(model);
2849889facbeSLen Brown 
2850889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
2851d8af6f5fSLen Brown 	if (debug)
2852b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
2853889facbeSLen Brown 
2854889facbeSLen Brown 	return;
2855889facbeSLen Brown }
2856889facbeSLen Brown 
28571b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
28583a9a941dSLen Brown {
28593a9a941dSLen Brown 	if (!genuine_intel)
28603a9a941dSLen Brown 		return;
28613a9a941dSLen Brown 
28623a9a941dSLen Brown 	if (family != 6)
28633a9a941dSLen Brown 		return;
28643a9a941dSLen Brown 
28653a9a941dSLen Brown 	switch (model) {
2866869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2867869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2868869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
28693a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
2870869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
28713a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
28723a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
28733a9a941dSLen Brown 	default:
28743a9a941dSLen Brown 		return;
28753a9a941dSLen Brown 	}
28763a9a941dSLen Brown }
28773a9a941dSLen Brown 
2878889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2879889facbeSLen Brown {
2880889facbeSLen Brown 	unsigned long long msr;
2881889facbeSLen Brown 	unsigned int dts;
2882889facbeSLen Brown 	int cpu;
2883889facbeSLen Brown 
2884889facbeSLen Brown 	if (!(do_dts || do_ptm))
2885889facbeSLen Brown 		return 0;
2886889facbeSLen Brown 
2887889facbeSLen Brown 	cpu = t->cpu_id;
2888889facbeSLen Brown 
2889889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
2890889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2891889facbeSLen Brown 		return 0;
2892889facbeSLen Brown 
2893889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2894b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2895889facbeSLen Brown 		return -1;
2896889facbeSLen Brown 	}
2897889facbeSLen Brown 
2898889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2899889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2900889facbeSLen Brown 			return 0;
2901889facbeSLen Brown 
2902889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2903b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2904889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
2905889facbeSLen Brown 
2906889facbeSLen Brown #ifdef	THERM_DEBUG
2907889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2908889facbeSLen Brown 			return 0;
2909889facbeSLen Brown 
2910889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2911889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
2912b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2913889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2914889facbeSLen Brown #endif
2915889facbeSLen Brown 	}
2916889facbeSLen Brown 
2917889facbeSLen Brown 
2918889facbeSLen Brown 	if (do_dts) {
2919889facbeSLen Brown 		unsigned int resolution;
2920889facbeSLen Brown 
2921889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2922889facbeSLen Brown 			return 0;
2923889facbeSLen Brown 
2924889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2925889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
2926b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2927889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
2928889facbeSLen Brown 
2929889facbeSLen Brown #ifdef THERM_DEBUG
2930889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2931889facbeSLen Brown 			return 0;
2932889facbeSLen Brown 
2933889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2934889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
2935b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2936889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2937889facbeSLen Brown #endif
2938889facbeSLen Brown 	}
2939889facbeSLen Brown 
2940889facbeSLen Brown 	return 0;
2941889facbeSLen Brown }
2942889facbeSLen Brown 
2943889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2944889facbeSLen Brown {
2945b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2946889facbeSLen Brown 		cpu, label,
2947889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
2948889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
2949889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2950889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
2951889facbeSLen Brown 
2952889facbeSLen Brown 	return;
2953889facbeSLen Brown }
2954889facbeSLen Brown 
2955889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2956889facbeSLen Brown {
2957889facbeSLen Brown 	unsigned long long msr;
2958889facbeSLen Brown 	int cpu;
2959889facbeSLen Brown 
2960889facbeSLen Brown 	if (!do_rapl)
2961889facbeSLen Brown 		return 0;
2962889facbeSLen Brown 
2963889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
2964889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2965889facbeSLen Brown 		return 0;
2966889facbeSLen Brown 
2967889facbeSLen Brown 	cpu = t->cpu_id;
2968889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2969b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2970889facbeSLen Brown 		return -1;
2971889facbeSLen Brown 	}
2972889facbeSLen Brown 
2973889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2974889facbeSLen Brown 		return -1;
2975889facbeSLen Brown 
2976d8af6f5fSLen Brown 	if (debug) {
2977b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2978889facbeSLen Brown 			"(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
2979144b44b1SLen Brown 			rapl_power_units, rapl_energy_units, rapl_time_units);
2980889facbeSLen Brown 	}
2981144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
2982144b44b1SLen Brown 
2983889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2984889facbeSLen Brown                 	return -5;
2985889facbeSLen Brown 
2986889facbeSLen Brown 
2987b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2988889facbeSLen Brown 			cpu, msr,
2989889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2990889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2991889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2992889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2993889facbeSLen Brown 
2994144b44b1SLen Brown 	}
2995144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
2996144b44b1SLen Brown 
2997889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2998889facbeSLen Brown 			return -9;
2999889facbeSLen Brown 
3000b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
3001889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "": "UN");
3002889facbeSLen Brown 
3003889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
3004b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
3005889facbeSLen Brown 			cpu,
3006889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
3007889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
3008889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3009889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
3010889facbeSLen Brown 	}
3011889facbeSLen Brown 
30120b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
3013889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3014889facbeSLen Brown                 	return -6;
3015889facbeSLen Brown 
3016b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3017889facbeSLen Brown 			cpu, msr,
3018889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3019889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3020889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3021889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
30220b2bb692SLen Brown 	}
30230b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
3024889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3025889facbeSLen Brown 			return -9;
3026b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
3027889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "": "UN");
3028889facbeSLen Brown 
3029889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
3030889facbeSLen Brown 	}
3031144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
3032d8af6f5fSLen Brown 		if (debug) {
3033889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3034889facbeSLen Brown 				return -7;
3035889facbeSLen Brown 
3036b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
3037144b44b1SLen Brown 		}
3038144b44b1SLen Brown 	}
30399148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
3040d8af6f5fSLen Brown 		if (debug) {
3041889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3042889facbeSLen Brown 				return -9;
3043b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3044889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
3045889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "Cores Limit");
3046889facbeSLen Brown 		}
3047889facbeSLen Brown 	}
3048889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
3049d8af6f5fSLen Brown 		if (debug) {
3050889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3051889facbeSLen Brown 				return -8;
3052889facbeSLen Brown 
3053b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
3054889facbeSLen Brown 
3055889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3056889facbeSLen Brown 				return -9;
3057b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3058889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
3059889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "GFX Limit");
3060889facbeSLen Brown 		}
3061889facbeSLen Brown 	}
3062889facbeSLen Brown 	return 0;
3063889facbeSLen Brown }
3064889facbeSLen Brown 
3065d7899447SLen Brown /*
3066d7899447SLen Brown  * SNB adds support for additional MSRs:
3067d7899447SLen Brown  *
3068d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
3069d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
3070d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
3071d7899447SLen Brown  */
3072103a8feaSLen Brown 
3073d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
3074103a8feaSLen Brown {
3075103a8feaSLen Brown 	if (!genuine_intel)
3076103a8feaSLen Brown 		return 0;
3077103a8feaSLen Brown 
3078103a8feaSLen Brown 	switch (model) {
3079869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3080869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3081869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3082869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3083869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3084869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
3085869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3086869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3087869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3088869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3089869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3090869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3091869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3092869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3093869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3094869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3095869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3096869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
30975bbac26eSXiaolong Wang 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3098103a8feaSLen Brown 		return 1;
3099103a8feaSLen Brown 	}
3100103a8feaSLen Brown 	return 0;
3101103a8feaSLen Brown }
3102103a8feaSLen Brown 
3103d7899447SLen Brown /*
3104d7899447SLen Brown  * HSW adds support for additional MSRs:
3105d7899447SLen Brown  *
3106d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
3107d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
3108d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
31095a63426eSLen Brown  *
31105a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
31115a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
31125a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
31135a63426eSLen Brown  *
3114d7899447SLen Brown  */
3115d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
3116ca58710fSKristen Carlson Accardi {
3117ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
3118ca58710fSKristen Carlson Accardi 		return 0;
3119ca58710fSKristen Carlson Accardi 
3120ca58710fSKristen Carlson Accardi 	switch (model) {
3121869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3122869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3123869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3124869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3125869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3126869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3127869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3128ca58710fSKristen Carlson Accardi 		return 1;
3129ca58710fSKristen Carlson Accardi 	}
3130ca58710fSKristen Carlson Accardi 	return 0;
3131ca58710fSKristen Carlson Accardi }
3132ca58710fSKristen Carlson Accardi 
31330b2bb692SLen Brown /*
31340b2bb692SLen Brown  * SKL adds support for additional MSRS:
31350b2bb692SLen Brown  *
31360b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
31370b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
31380b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
31390b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
31400b2bb692SLen Brown  */
31410b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
31420b2bb692SLen Brown {
31430b2bb692SLen Brown 	if (!genuine_intel)
31440b2bb692SLen Brown 		return 0;
31450b2bb692SLen Brown 
31460b2bb692SLen Brown 	switch (model) {
3147869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3148869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3149869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3150869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
31510b2bb692SLen Brown 		return 1;
31520b2bb692SLen Brown 	}
31530b2bb692SLen Brown 	return 0;
31540b2bb692SLen Brown }
31550b2bb692SLen Brown 
31560b2bb692SLen Brown 
3157ca58710fSKristen Carlson Accardi 
3158144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
3159144b44b1SLen Brown {
3160144b44b1SLen Brown 	if (!genuine_intel)
3161144b44b1SLen Brown 		return 0;
3162144b44b1SLen Brown 	switch (model) {
3163869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3164869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3165144b44b1SLen Brown 		return 1;
3166144b44b1SLen Brown 	}
3167144b44b1SLen Brown 	return 0;
3168144b44b1SLen Brown }
3169144b44b1SLen Brown 
3170fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
3171fb5d4327SDasaratharaman Chandramouli {
3172fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3173fb5d4327SDasaratharaman Chandramouli 		return 0;
3174fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3175869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3176005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3177fb5d4327SDasaratharaman Chandramouli 		return 1;
3178fb5d4327SDasaratharaman Chandramouli 	}
3179fb5d4327SDasaratharaman Chandramouli 	return 0;
3180fb5d4327SDasaratharaman Chandramouli }
3181fb5d4327SDasaratharaman Chandramouli 
3182b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3183b2b34dfeSHubert Chrzaniuk {
3184b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
3185b2b34dfeSHubert Chrzaniuk 		return 1024;
3186b2b34dfeSHubert Chrzaniuk 	return 1;
3187b2b34dfeSHubert Chrzaniuk }
3188b2b34dfeSHubert Chrzaniuk 
3189144b44b1SLen Brown #define SLM_BCLK_FREQS 5
3190144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3191144b44b1SLen Brown 
3192144b44b1SLen Brown double slm_bclk(void)
3193144b44b1SLen Brown {
3194144b44b1SLen Brown 	unsigned long long msr = 3;
3195144b44b1SLen Brown 	unsigned int i;
3196144b44b1SLen Brown 	double freq;
3197144b44b1SLen Brown 
31987ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3199b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
3200144b44b1SLen Brown 
3201144b44b1SLen Brown 	i = msr & 0xf;
3202144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
3203b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
32040a91e551SColin Ian King 		i = 3;
3205144b44b1SLen Brown 	}
3206144b44b1SLen Brown 	freq = slm_freq_table[i];
3207144b44b1SLen Brown 
3208b7d8c148SLen Brown 	fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3209144b44b1SLen Brown 
3210144b44b1SLen Brown 	return freq;
3211144b44b1SLen Brown }
3212144b44b1SLen Brown 
3213103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
3214103a8feaSLen Brown {
3215121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
3216103a8feaSLen Brown 		return 100.00;
3217144b44b1SLen Brown 	else if (is_slm(family, model))
3218144b44b1SLen Brown 		return slm_bclk();
3219103a8feaSLen Brown 	else
3220103a8feaSLen Brown 		return 133.33;
3221103a8feaSLen Brown }
3222103a8feaSLen Brown 
3223889facbeSLen Brown /*
3224889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3225889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
3226889facbeSLen Brown  * This is usually equal to tjMax.
3227889facbeSLen Brown  *
3228889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
3229889facbeSLen Brown  * but also allow cmdline over-ride with -T.
3230889facbeSLen Brown  *
3231889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
3232889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
3233889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3234889facbeSLen Brown  */
3235889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3236889facbeSLen Brown {
3237889facbeSLen Brown 	unsigned long long msr;
3238889facbeSLen Brown 	unsigned int target_c_local;
3239889facbeSLen Brown 	int cpu;
3240889facbeSLen Brown 
3241889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
3242889facbeSLen Brown 	if (!(do_dts || do_ptm))
3243889facbeSLen Brown 		return 0;
3244889facbeSLen Brown 
3245889facbeSLen Brown 	/* this is a per-package concept */
3246889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3247889facbeSLen Brown 		return 0;
3248889facbeSLen Brown 
3249889facbeSLen Brown 	cpu = t->cpu_id;
3250889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3251b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3252889facbeSLen Brown 		return -1;
3253889facbeSLen Brown 	}
3254889facbeSLen Brown 
3255889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
3256889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
3257b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
3258889facbeSLen Brown 			cpu, tcc_activation_temp);
3259889facbeSLen Brown 		return 0;
3260889facbeSLen Brown 	}
3261889facbeSLen Brown 
3262889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
3263d7899447SLen Brown 	if (!do_nhm_platform_info)
3264889facbeSLen Brown 		goto guess;
3265889facbeSLen Brown 
32667ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
3267889facbeSLen Brown 		goto guess;
3268889facbeSLen Brown 
32693482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
3270889facbeSLen Brown 
3271d8af6f5fSLen Brown 	if (debug)
3272b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3273889facbeSLen Brown 			cpu, msr, target_c_local);
3274889facbeSLen Brown 
32753482124aSJean Delvare 	if (!target_c_local)
3276889facbeSLen Brown 		goto guess;
3277889facbeSLen Brown 
3278889facbeSLen Brown 	tcc_activation_temp = target_c_local;
3279889facbeSLen Brown 
3280889facbeSLen Brown 	return 0;
3281889facbeSLen Brown 
3282889facbeSLen Brown guess:
3283889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
3284b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3285889facbeSLen Brown 		cpu, tcc_activation_temp);
3286889facbeSLen Brown 
3287889facbeSLen Brown 	return 0;
3288889facbeSLen Brown }
328969807a63SLen Brown 
3290aa8d8cc7SLen Brown void decode_feature_control_msr(void)
3291aa8d8cc7SLen Brown {
3292aa8d8cc7SLen Brown 	unsigned long long msr;
3293aa8d8cc7SLen Brown 
3294aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3295aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3296aa8d8cc7SLen Brown 			base_cpu, msr,
3297aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3298aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
3299aa8d8cc7SLen Brown }
3300aa8d8cc7SLen Brown 
330169807a63SLen Brown void decode_misc_enable_msr(void)
330269807a63SLen Brown {
330369807a63SLen Brown 	unsigned long long msr;
330469807a63SLen Brown 
330569807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
3306b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
330769807a63SLen Brown 			base_cpu, msr,
330869807a63SLen Brown 			msr & (1 << 3) ? "TCC" : "",
330969807a63SLen Brown 			msr & (1 << 16) ? "EIST" : "",
331069807a63SLen Brown 			msr & (1 << 18) ? "MONITOR" : "");
331169807a63SLen Brown }
331269807a63SLen Brown 
3313f0057310SLen Brown /*
3314f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
3315f0057310SLen Brown  *
3316f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
3317f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
3318f0057310SLen Brown  * bit[1] less so...
3319f0057310SLen Brown  */
3320f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
3321f0057310SLen Brown {
3322f0057310SLen Brown 	unsigned long long msr;
3323f0057310SLen Brown 
3324f0057310SLen Brown 	if (!do_nhm_platform_info)
3325f0057310SLen Brown 		return;
3326f0057310SLen Brown 
3327f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
3328ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
3329f0057310SLen Brown 			base_cpu, msr,
3330f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
3331ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
3332ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
3333f0057310SLen Brown }
33347f5c258eSLen Brown 
3335fcd17211SLen Brown void process_cpuid()
3336103a8feaSLen Brown {
333761a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
3338103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
3339103a8feaSLen Brown 
3340103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
3341103a8feaSLen Brown 
33425aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
3343103a8feaSLen Brown 
3344103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3345103a8feaSLen Brown 		genuine_intel = 1;
3346103a8feaSLen Brown 
3347d8af6f5fSLen Brown 	if (debug)
3348b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
3349103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
3350103a8feaSLen Brown 
33515aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
3352103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
3353103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
3354103a8feaSLen Brown 	stepping = fms & 0xf;
3355103a8feaSLen Brown 	if (family == 6 || family == 0xf)
3356103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
3357103a8feaSLen Brown 
335869807a63SLen Brown 	if (debug) {
3359b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
3360103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
3361aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
336269807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
336369807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
3364aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
336569807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
336669807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
336769807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
336869807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
336969807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
337069807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
337169807a63SLen Brown 	}
3372103a8feaSLen Brown 
3373b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
3374b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
3375103a8feaSLen Brown 
3376103a8feaSLen Brown 	/*
3377103a8feaSLen Brown 	 * check max extended function levels of CPUID.
3378103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
3379103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
3380103a8feaSLen Brown 	 */
3381103a8feaSLen Brown 	ebx = ecx = edx = 0;
33825aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
3383103a8feaSLen Brown 
338461a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
3385103a8feaSLen Brown 
3386103a8feaSLen Brown 		/*
3387103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3388103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
3389103a8feaSLen Brown 		 */
33905aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
33918209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
3392d7899447SLen Brown 	}
3393103a8feaSLen Brown 
3394103a8feaSLen Brown 	/*
3395103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3396103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
3397103a8feaSLen Brown 	 */
3398103a8feaSLen Brown 
33995aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
34008209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
3401889facbeSLen Brown 	do_dts = eax & (1 << 0);
3402889facbeSLen Brown 	do_ptm = eax & (1 << 6);
34037f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
34047f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
34057f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
34067f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
34077f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
3408889facbeSLen Brown 	has_epb = ecx & (1 << 3);
3409889facbeSLen Brown 
3410d8af6f5fSLen Brown 	if (debug)
3411b7d8c148SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
34127f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
34137f5c258eSLen Brown 			has_aperf ? "" : "No-",
34147f5c258eSLen Brown 			do_dts ? "" : "No-",
34157f5c258eSLen Brown 			do_ptm ? "" : "No-",
34167f5c258eSLen Brown 			has_hwp ? "" : "No-",
34177f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
34187f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
34197f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
34207f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
34217f5c258eSLen Brown 			has_epb ? "" : "No-");
3422103a8feaSLen Brown 
342369807a63SLen Brown 	if (debug)
342469807a63SLen Brown 		decode_misc_enable_msr();
342569807a63SLen Brown 
34268ae72255SLen Brown 	if (max_level >= 0x7 && debug) {
3427aa8d8cc7SLen Brown 		int has_sgx;
3428aa8d8cc7SLen Brown 
3429aa8d8cc7SLen Brown 		ecx = 0;
3430aa8d8cc7SLen Brown 
3431aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3432aa8d8cc7SLen Brown 
3433aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
3434aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3435aa8d8cc7SLen Brown 
3436aa8d8cc7SLen Brown 		if (has_sgx)
3437aa8d8cc7SLen Brown 			decode_feature_control_msr();
3438aa8d8cc7SLen Brown 	}
3439aa8d8cc7SLen Brown 
344061a87ba7SLen Brown 	if (max_level >= 0x15) {
34418a5bdf41SLen Brown 		unsigned int eax_crystal;
34428a5bdf41SLen Brown 		unsigned int ebx_tsc;
34438a5bdf41SLen Brown 
34448a5bdf41SLen Brown 		/*
34458a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
34468a5bdf41SLen Brown 		 */
34478a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
34485aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
34498a5bdf41SLen Brown 
34508a5bdf41SLen Brown 		if (ebx_tsc != 0) {
34518a5bdf41SLen Brown 
34528a5bdf41SLen Brown 			if (debug && (ebx != 0))
3453b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
34548a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
34558a5bdf41SLen Brown 
34568a5bdf41SLen Brown 			if (crystal_hz == 0)
34578a5bdf41SLen Brown 				switch(model) {
3458869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3459869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3460869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3461869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3462e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
3463e8efbc80SLen Brown 					break;
3464869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_X:	/* SKX */
34657268d407SLen Brown 				case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3466ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
3467ec53e594SLen Brown 					break;
3468869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3469e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
34708a5bdf41SLen Brown 					break;
34718a5bdf41SLen Brown 				default:
34728a5bdf41SLen Brown 					crystal_hz = 0;
34738a5bdf41SLen Brown 			}
34748a5bdf41SLen Brown 
34758a5bdf41SLen Brown 			if (crystal_hz) {
34768a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
34778a5bdf41SLen Brown 				if (debug)
3478b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
34798a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
34808a5bdf41SLen Brown 			}
34818a5bdf41SLen Brown 		}
34828a5bdf41SLen Brown 	}
348361a87ba7SLen Brown 	if (max_level >= 0x16) {
348461a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
348561a87ba7SLen Brown 
348661a87ba7SLen Brown 		/*
348761a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
348861a87ba7SLen Brown 		 */
348961a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
349061a87ba7SLen Brown 
34915aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
349261a87ba7SLen Brown 		if (debug)
3493b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
349461a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
349561a87ba7SLen Brown 	}
34968a5bdf41SLen Brown 
3497b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
3498b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3499b2b34dfeSHubert Chrzaniuk 
3500ee7e38e3SLen Brown 	do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
3501d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
35025a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
3503ee7e38e3SLen Brown 	do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3504ee7e38e3SLen Brown 	do_pc3 = (pkg_cstate_limit >= PCL__3);
3505ee7e38e3SLen Brown 	do_pc6 = (pkg_cstate_limit >= PCL__6);
3506ee7e38e3SLen Brown 	do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
3507d7899447SLen Brown 	do_c8_c9_c10 = has_hsw_msrs(family, model);
35085a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
35090b2bb692SLen Brown 	do_skl_residency = has_skl_msrs(family, model);
3510144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
3511fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
3512103a8feaSLen Brown 
3513f0057310SLen Brown 	if (debug)
3514f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
3515f0057310SLen Brown 
3516889facbeSLen Brown 	rapl_probe(family, model);
35173a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
3518889facbeSLen Brown 
3519fcd17211SLen Brown 	if (debug)
35201b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
3521fcd17211SLen Brown 
3522a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
3523a2b7b749SLen Brown 		calculate_tsc_tweak();
3524a2b7b749SLen Brown 
3525fdf676e5SLen Brown 	do_gfx_rc6_ms = !access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK);
3526fdf676e5SLen Brown 
352727d47356SLen Brown 	do_gfx_mhz = !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK);
352827d47356SLen Brown 
3529889facbeSLen Brown 	return;
3530103a8feaSLen Brown }
3531103a8feaSLen Brown 
3532d8af6f5fSLen Brown void help()
3533103a8feaSLen Brown {
3534b7d8c148SLen Brown 	fprintf(outf,
3535d8af6f5fSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3536d8af6f5fSLen Brown 	"\n"
3537d8af6f5fSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
3538d8af6f5fSLen Brown 	"when COMMAND completes.\n"
3539d8af6f5fSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3540d8af6f5fSLen Brown 	"to print statistics, until interrupted.\n"
3541388e9c81SLen Brown 	"--add		add a counter\n"
3542388e9c81SLen Brown 	"		eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
3543d8af6f5fSLen Brown 	"--debug	run in \"debug\" mode\n"
3544d8af6f5fSLen Brown 	"--interval sec	Override default 5-second measurement interval\n"
3545d8af6f5fSLen Brown 	"--help		print this help message\n"
3546b7d8c148SLen Brown 	"--out file	create or truncate \"file\" for all output\n"
3547d8af6f5fSLen Brown 	"--version	print version information\n"
3548d8af6f5fSLen Brown 	"\n"
3549d8af6f5fSLen Brown 	"For more help, run \"man turbostat\"\n");
3550103a8feaSLen Brown }
3551103a8feaSLen Brown 
3552103a8feaSLen Brown 
3553103a8feaSLen Brown /*
3554103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
3555103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
3556103a8feaSLen Brown  */
3557103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
3558103a8feaSLen Brown {
3559103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
3560103a8feaSLen Brown 		return 1;
3561103a8feaSLen Brown 	else
3562103a8feaSLen Brown 		return 0;
3563103a8feaSLen Brown }
3564103a8feaSLen Brown 
3565103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
3566103a8feaSLen Brown {
3567103a8feaSLen Brown 	return 0;
3568103a8feaSLen Brown }
3569103a8feaSLen Brown 
3570c98d5d94SLen Brown void topology_probe()
3571c98d5d94SLen Brown {
3572c98d5d94SLen Brown 	int i;
3573c98d5d94SLen Brown 	int max_core_id = 0;
3574c98d5d94SLen Brown 	int max_package_id = 0;
3575c98d5d94SLen Brown 	int max_siblings = 0;
3576c98d5d94SLen Brown 	struct cpu_topology {
3577c98d5d94SLen Brown 		int core_id;
3578c98d5d94SLen Brown 		int physical_package_id;
3579c98d5d94SLen Brown 	} *cpus;
3580c98d5d94SLen Brown 
3581c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
3582c98d5d94SLen Brown 	topo.num_cpus = 0;
3583c98d5d94SLen Brown 	topo.max_cpu_num = 0;
3584c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
3585c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
3586c98d5d94SLen Brown 		show_cpu = 1;
3587c98d5d94SLen Brown 
3588d8af6f5fSLen Brown 	if (debug > 1)
3589b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
3590c98d5d94SLen Brown 
3591c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
3592b2c95d90SJosh Triplett 	if (cpus == NULL)
3593b2c95d90SJosh Triplett 		err(1, "calloc cpus");
3594c98d5d94SLen Brown 
3595c98d5d94SLen Brown 	/*
3596c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
3597c98d5d94SLen Brown 	 */
3598c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
3599b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
3600b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
3601c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3602c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3603c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
3604c98d5d94SLen Brown 
3605c98d5d94SLen Brown 	/*
3606c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
3607c98d5d94SLen Brown 	 */
3608c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
3609b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
3610b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
3611c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3612c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3613c98d5d94SLen Brown 
3614c98d5d94SLen Brown 
3615c98d5d94SLen Brown 	/*
3616c98d5d94SLen Brown 	 * For online cpus
3617c98d5d94SLen Brown 	 * find max_core_id, max_package_id
3618c98d5d94SLen Brown 	 */
3619c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
3620c98d5d94SLen Brown 		int siblings;
3621c98d5d94SLen Brown 
3622c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
3623d8af6f5fSLen Brown 			if (debug > 1)
3624b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
3625c98d5d94SLen Brown 			continue;
3626c98d5d94SLen Brown 		}
3627c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
3628c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
3629c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
3630c98d5d94SLen Brown 
3631c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
3632c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
3633c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
3634c98d5d94SLen Brown 
3635c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
3636c98d5d94SLen Brown 		if (siblings > max_siblings)
3637c98d5d94SLen Brown 			max_siblings = siblings;
3638d8af6f5fSLen Brown 		if (debug > 1)
3639b7d8c148SLen Brown 			fprintf(outf, "cpu %d pkg %d core %d\n",
3640c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
3641c98d5d94SLen Brown 	}
3642c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
3643d8af6f5fSLen Brown 	if (debug > 1)
3644b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
3645c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
36461cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_cores_per_pkg > 1)
3647c98d5d94SLen Brown 		show_core = 1;
3648c98d5d94SLen Brown 
3649c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
3650d8af6f5fSLen Brown 	if (debug > 1)
3651b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
3652c98d5d94SLen Brown 			max_package_id, topo.num_packages);
36531cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_packages > 1)
3654c98d5d94SLen Brown 		show_pkg = 1;
3655c98d5d94SLen Brown 
3656c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
3657d8af6f5fSLen Brown 	if (debug > 1)
3658b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
3659c98d5d94SLen Brown 
3660c98d5d94SLen Brown 	free(cpus);
3661c98d5d94SLen Brown }
3662c98d5d94SLen Brown 
3663c98d5d94SLen Brown void
3664c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3665c98d5d94SLen Brown {
3666c98d5d94SLen Brown 	int i;
3667c98d5d94SLen Brown 
3668c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3669*678a3bd1SLen Brown 		topo.num_packages, sizeof(struct thread_data));
3670c98d5d94SLen Brown 	if (*t == NULL)
3671c98d5d94SLen Brown 		goto error;
3672c98d5d94SLen Brown 
3673c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
3674c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
3675c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
3676c98d5d94SLen Brown 
3677c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
3678*678a3bd1SLen Brown 		sizeof(struct core_data));
3679c98d5d94SLen Brown 	if (*c == NULL)
3680c98d5d94SLen Brown 		goto error;
3681c98d5d94SLen Brown 
3682c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3683c98d5d94SLen Brown 		(*c)[i].core_id = -1;
3684c98d5d94SLen Brown 
3685*678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
3686c98d5d94SLen Brown 	if (*p == NULL)
3687c98d5d94SLen Brown 		goto error;
3688c98d5d94SLen Brown 
3689c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
3690c98d5d94SLen Brown 		(*p)[i].package_id = i;
3691c98d5d94SLen Brown 
3692c98d5d94SLen Brown 	return;
3693c98d5d94SLen Brown error:
3694b2c95d90SJosh Triplett 	err(1, "calloc counters");
3695c98d5d94SLen Brown }
3696c98d5d94SLen Brown /*
3697c98d5d94SLen Brown  * init_counter()
3698c98d5d94SLen Brown  *
3699c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
3700c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3701c98d5d94SLen Brown  *
3702c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
3703c98d5d94SLen Brown  */
3704c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3705c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
3706c98d5d94SLen Brown 	int pkg_num, int cpu_id)
3707c98d5d94SLen Brown {
3708c98d5d94SLen Brown 	struct thread_data *t;
3709c98d5d94SLen Brown 	struct core_data *c;
3710c98d5d94SLen Brown 	struct pkg_data *p;
3711c98d5d94SLen Brown 
3712c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3713c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
3714c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
3715c98d5d94SLen Brown 
3716c98d5d94SLen Brown 	t->cpu_id = cpu_id;
3717c98d5d94SLen Brown 	if (thread_num == 0) {
3718c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3719c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
3720c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3721c98d5d94SLen Brown 	}
3722c98d5d94SLen Brown 
3723c98d5d94SLen Brown 	c->core_id = core_num;
3724c98d5d94SLen Brown 	p->package_id = pkg_num;
3725c98d5d94SLen Brown }
3726c98d5d94SLen Brown 
3727c98d5d94SLen Brown 
3728c98d5d94SLen Brown int initialize_counters(int cpu_id)
3729c98d5d94SLen Brown {
3730c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
3731c98d5d94SLen Brown 
3732c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
3733c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
3734e275b388SDasaratharaman Chandramouli 	my_thread_id = get_cpu_position_in_core(cpu_id);
3735e275b388SDasaratharaman Chandramouli 	if (!my_thread_id)
3736c98d5d94SLen Brown 		topo.num_cores++;
3737c98d5d94SLen Brown 
3738c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3739c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3740c98d5d94SLen Brown 	return 0;
3741c98d5d94SLen Brown }
3742c98d5d94SLen Brown 
3743c98d5d94SLen Brown void allocate_output_buffer()
3744c98d5d94SLen Brown {
37453b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
3746c98d5d94SLen Brown 	outp = output_buffer;
3747b2c95d90SJosh Triplett 	if (outp == NULL)
3748b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
3749c98d5d94SLen Brown }
375036229897SLen Brown void allocate_fd_percpu(void)
375136229897SLen Brown {
375201a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
375336229897SLen Brown 	if (fd_percpu == NULL)
375436229897SLen Brown 		err(-1, "calloc fd_percpu");
375536229897SLen Brown }
3756562a2d37SLen Brown void allocate_irq_buffers(void)
3757562a2d37SLen Brown {
3758562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
3759562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
3760562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
3761c98d5d94SLen Brown 
376201a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
3763562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
376401a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
3765562a2d37SLen Brown }
3766c98d5d94SLen Brown void setup_all_buffers(void)
3767c98d5d94SLen Brown {
3768c98d5d94SLen Brown 	topology_probe();
3769562a2d37SLen Brown 	allocate_irq_buffers();
377036229897SLen Brown 	allocate_fd_percpu();
3771c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
3772c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
3773c98d5d94SLen Brown 	allocate_output_buffer();
3774c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
3775c98d5d94SLen Brown }
37763b4d5c7fSAndy Shevchenko 
37777ce7d5deSPrarit Bhargava void set_base_cpu(void)
37787ce7d5deSPrarit Bhargava {
37797ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
37807ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
37817ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
37827ce7d5deSPrarit Bhargava 
37837ce7d5deSPrarit Bhargava 	if (debug > 1)
3784b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
37857ce7d5deSPrarit Bhargava }
37867ce7d5deSPrarit Bhargava 
3787103a8feaSLen Brown void turbostat_init()
3788103a8feaSLen Brown {
37897ce7d5deSPrarit Bhargava 	setup_all_buffers();
37907ce7d5deSPrarit Bhargava 	set_base_cpu();
3791103a8feaSLen Brown 	check_dev_msr();
379298481e79SLen Brown 	check_permissions();
3793fcd17211SLen Brown 	process_cpuid();
3794103a8feaSLen Brown 
3795103a8feaSLen Brown 
3796d8af6f5fSLen Brown 	if (debug)
37977f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
37987f5c258eSLen Brown 
37997f5c258eSLen Brown 	if (debug)
3800889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
3801889facbeSLen Brown 
3802d8af6f5fSLen Brown 	if (debug)
38033a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
38043a9a941dSLen Brown 
3805d8af6f5fSLen Brown 	if (debug)
3806889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
3807889facbeSLen Brown 
3808889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
3809889facbeSLen Brown 
3810d8af6f5fSLen Brown 	if (debug)
3811889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
38125a63426eSLen Brown 
38135a63426eSLen Brown 	if (debug && do_irtl_snb)
38145a63426eSLen Brown 		print_irtl();
3815103a8feaSLen Brown }
3816103a8feaSLen Brown 
3817103a8feaSLen Brown int fork_it(char **argv)
3818103a8feaSLen Brown {
3819103a8feaSLen Brown 	pid_t child_pid;
3820d91bb17cSLen Brown 	int status;
3821d15cf7c1SLen Brown 
3822d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
3823d91bb17cSLen Brown 	if (status)
3824d91bb17cSLen Brown 		exit(status);
3825d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
3826d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
3827103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3828103a8feaSLen Brown 
3829103a8feaSLen Brown 	child_pid = fork();
3830103a8feaSLen Brown 	if (!child_pid) {
3831103a8feaSLen Brown 		/* child */
3832103a8feaSLen Brown 		execvp(argv[0], argv);
3833103a8feaSLen Brown 	} else {
3834103a8feaSLen Brown 
3835103a8feaSLen Brown 		/* parent */
3836b2c95d90SJosh Triplett 		if (child_pid == -1)
3837b2c95d90SJosh Triplett 			err(1, "fork");
3838103a8feaSLen Brown 
3839103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
3840103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
3841b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
3842b2c95d90SJosh Triplett 			err(status, "waitpid");
3843103a8feaSLen Brown 	}
3844c98d5d94SLen Brown 	/*
3845c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
3846c98d5d94SLen Brown 	 * because re-starting is problematic when forking
3847c98d5d94SLen Brown 	 */
3848c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
3849103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
3850103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
3851ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
3852ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
3853ba3dec99SLen Brown 	else {
3854c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3855c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3856ba3dec99SLen Brown 	}
3857103a8feaSLen Brown 
3858b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3859b7d8c148SLen Brown 
3860b7d8c148SLen Brown 	flush_output_stderr();
3861103a8feaSLen Brown 
3862d91bb17cSLen Brown 	return status;
3863103a8feaSLen Brown }
3864103a8feaSLen Brown 
38653b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
38663b4d5c7fSAndy Shevchenko {
38673b4d5c7fSAndy Shevchenko 	int status;
38683b4d5c7fSAndy Shevchenko 
38693b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
38703b4d5c7fSAndy Shevchenko 	if (status)
38713b4d5c7fSAndy Shevchenko 		return status;
38723b4d5c7fSAndy Shevchenko 
38733b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
38743b4d5c7fSAndy Shevchenko 	if (status)
38753b4d5c7fSAndy Shevchenko 		return status;
38763b4d5c7fSAndy Shevchenko 
3877b7d8c148SLen Brown 	flush_output_stdout();
38783b4d5c7fSAndy Shevchenko 
38793b4d5c7fSAndy Shevchenko 	return status;
38803b4d5c7fSAndy Shevchenko }
38813b4d5c7fSAndy Shevchenko 
3882d8af6f5fSLen Brown void print_version() {
38836886fee4SLen Brown 	fprintf(outf, "turbostat version 4.16 24 Dec 2016"
3884d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
3885d8af6f5fSLen Brown }
3886d8af6f5fSLen Brown 
3887388e9c81SLen Brown int add_counter(unsigned int msr_num, char *name, unsigned int width,
3888388e9c81SLen Brown 	enum counter_scope scope, enum counter_type type,
3889388e9c81SLen Brown 	enum counter_format format)
3890388e9c81SLen Brown {
3891388e9c81SLen Brown 	struct msr_counter *msrp;
3892388e9c81SLen Brown 
3893388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
3894388e9c81SLen Brown 	if (msrp == NULL) {
3895388e9c81SLen Brown 		perror("calloc");
3896388e9c81SLen Brown 		exit(1);
3897388e9c81SLen Brown 	}
3898388e9c81SLen Brown 
3899388e9c81SLen Brown 	msrp->msr_num = msr_num;
3900388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
3901388e9c81SLen Brown 	msrp->width = width;
3902388e9c81SLen Brown 	msrp->type = type;
3903388e9c81SLen Brown 	msrp->format = format;
3904388e9c81SLen Brown 
3905388e9c81SLen Brown 	switch (scope) {
3906388e9c81SLen Brown 
3907388e9c81SLen Brown 	case SCOPE_CPU:
3908388e9c81SLen Brown 		msrp->next = sys.tp;
3909388e9c81SLen Brown 		sys.tp = msrp;
3910*678a3bd1SLen Brown 		sys.added_thread_counters++;
3911*678a3bd1SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
3912*678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
3913*678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
3914*678a3bd1SLen Brown 			exit(-1);
3915*678a3bd1SLen Brown 		}
3916388e9c81SLen Brown 		break;
3917388e9c81SLen Brown 
3918388e9c81SLen Brown 	case SCOPE_CORE:
3919388e9c81SLen Brown 		msrp->next = sys.cp;
3920388e9c81SLen Brown 		sys.cp = msrp;
3921*678a3bd1SLen Brown 		sys.added_core_counters++;
3922*678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
3923*678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
3924*678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
3925*678a3bd1SLen Brown 			exit(-1);
3926*678a3bd1SLen Brown 		}
3927388e9c81SLen Brown 		break;
3928388e9c81SLen Brown 
3929388e9c81SLen Brown 	case SCOPE_PACKAGE:
3930388e9c81SLen Brown 		msrp->next = sys.pp;
3931388e9c81SLen Brown 		sys.pp = msrp;
3932*678a3bd1SLen Brown 		sys.added_package_counters++;
3933*678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
3934*678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
3935*678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
3936*678a3bd1SLen Brown 			exit(-1);
3937*678a3bd1SLen Brown 		}
3938388e9c81SLen Brown 		break;
3939388e9c81SLen Brown 	}
3940388e9c81SLen Brown 
3941388e9c81SLen Brown 	return 0;
3942388e9c81SLen Brown }
3943388e9c81SLen Brown 
3944388e9c81SLen Brown void parse_add_command(char *add_command)
3945388e9c81SLen Brown {
3946388e9c81SLen Brown 	int msr_num = 0;
3947388e9c81SLen Brown 	char name_buffer[NAME_BYTES];
3948388e9c81SLen Brown 	int width = 64;
3949388e9c81SLen Brown 	int fail = 0;
3950388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
3951388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
3952388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
3953388e9c81SLen Brown 
3954388e9c81SLen Brown 	while (add_command) {
3955388e9c81SLen Brown 
3956388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
3957388e9c81SLen Brown 			goto next;
3958388e9c81SLen Brown 
3959388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
3960388e9c81SLen Brown 			goto next;
3961388e9c81SLen Brown 
3962388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
3963388e9c81SLen Brown 			if ((width == 32) || (width == 64))
3964388e9c81SLen Brown 				goto next;
3965388e9c81SLen Brown 			width = 64;
3966388e9c81SLen Brown 		}
3967388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
3968388e9c81SLen Brown 			scope = SCOPE_CPU;
3969388e9c81SLen Brown 			goto next;
3970388e9c81SLen Brown 		}
3971388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
3972388e9c81SLen Brown 			scope = SCOPE_CORE;
3973388e9c81SLen Brown 			goto next;
3974388e9c81SLen Brown 		}
3975388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
3976388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
3977388e9c81SLen Brown 			goto next;
3978388e9c81SLen Brown 		}
3979388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
3980388e9c81SLen Brown 			type = COUNTER_CYCLES;
3981388e9c81SLen Brown 			goto next;
3982388e9c81SLen Brown 		}
3983388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
3984388e9c81SLen Brown 			type = COUNTER_SECONDS;
3985388e9c81SLen Brown 			goto next;
3986388e9c81SLen Brown 		}
3987388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
3988388e9c81SLen Brown 			format = FORMAT_RAW;
3989388e9c81SLen Brown 			goto next;
3990388e9c81SLen Brown 		}
3991388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
3992388e9c81SLen Brown 			format = FORMAT_DELTA;
3993388e9c81SLen Brown 			goto next;
3994388e9c81SLen Brown 		}
3995388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
3996388e9c81SLen Brown 			format = FORMAT_PERCENT;
3997388e9c81SLen Brown 			goto next;
3998388e9c81SLen Brown 		}
3999388e9c81SLen Brown 
4000388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
4001388e9c81SLen Brown 			char *eos;
4002388e9c81SLen Brown 
4003388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
4004388e9c81SLen Brown 			if (eos)
4005388e9c81SLen Brown 				*eos = '\0';
4006388e9c81SLen Brown 			goto next;
4007388e9c81SLen Brown 		}
4008388e9c81SLen Brown 
4009388e9c81SLen Brown next:
4010388e9c81SLen Brown 		add_command = strchr(add_command, ',');
4011388e9c81SLen Brown 		if (add_command)
4012388e9c81SLen Brown 			add_command++;
4013388e9c81SLen Brown 
4014388e9c81SLen Brown 	}
4015388e9c81SLen Brown 	if (msr_num == 0) {
4016388e9c81SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4017388e9c81SLen Brown 		fail++;
4018388e9c81SLen Brown 	}
4019388e9c81SLen Brown 
4020388e9c81SLen Brown 	/* generate default column header */
4021388e9c81SLen Brown 	if (*name_buffer == '\0') {
4022388e9c81SLen Brown 		if (format == FORMAT_RAW) {
4023388e9c81SLen Brown 			if (width == 32)
4024388e9c81SLen Brown 				sprintf(name_buffer, "msr%d", msr_num);
4025388e9c81SLen Brown 			else
4026388e9c81SLen Brown 				sprintf(name_buffer, "MSR%d", msr_num);
4027388e9c81SLen Brown 		} else if (format == FORMAT_DELTA) {
4028388e9c81SLen Brown 			if (width == 32)
4029388e9c81SLen Brown 				sprintf(name_buffer, "cnt%d", msr_num);
4030388e9c81SLen Brown 			else
4031388e9c81SLen Brown 				sprintf(name_buffer, "CNT%d", msr_num);
4032388e9c81SLen Brown 		} else if (format == FORMAT_PERCENT) {
4033388e9c81SLen Brown 			if (width == 32)
4034388e9c81SLen Brown 				sprintf(name_buffer, "msr%d%%", msr_num);
4035388e9c81SLen Brown 			else
4036388e9c81SLen Brown 				sprintf(name_buffer, "MSR%d%%", msr_num);
4037388e9c81SLen Brown 		}
4038388e9c81SLen Brown 	}
4039388e9c81SLen Brown 
4040388e9c81SLen Brown 	if (add_counter(msr_num, name_buffer, width, scope, type, format))
4041388e9c81SLen Brown 		fail++;
4042388e9c81SLen Brown 
4043388e9c81SLen Brown 	if (fail) {
4044388e9c81SLen Brown 		help();
4045388e9c81SLen Brown 		exit(1);
4046388e9c81SLen Brown 	}
4047388e9c81SLen Brown }
4048103a8feaSLen Brown void cmdline(int argc, char **argv)
4049103a8feaSLen Brown {
4050103a8feaSLen Brown 	int opt;
4051d8af6f5fSLen Brown 	int option_index = 0;
4052d8af6f5fSLen Brown 	static struct option long_options[] = {
4053388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
4054d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
4055d8af6f5fSLen Brown 		{"debug",	no_argument,		0, 'd'},
4056d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
4057d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
4058d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
4059b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
4060d8af6f5fSLen Brown 		{"Package",	no_argument,		0, 'p'},
4061d8af6f5fSLen Brown 		{"processor",	no_argument,		0, 'p'},
4062d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
4063d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
4064d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
4065d8af6f5fSLen Brown 		{0,		0,			0,  0 }
4066d8af6f5fSLen Brown 	};
4067103a8feaSLen Brown 
4068103a8feaSLen Brown 	progname = argv[0];
4069103a8feaSLen Brown 
4070b7d8c148SLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
4071d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
4072103a8feaSLen Brown 		switch (opt) {
4073388e9c81SLen Brown 		case 'a':
4074388e9c81SLen Brown 			parse_add_command(optarg);
4075388e9c81SLen Brown 			break;
4076d8af6f5fSLen Brown 		case 'D':
4077d8af6f5fSLen Brown 			dump_only++;
40788e180f3cSLen Brown 			break;
4079d8af6f5fSLen Brown 		case 'd':
4080d8af6f5fSLen Brown 			debug++;
40812f32edf1SLen Brown 			break;
4082d8af6f5fSLen Brown 		case 'h':
4083d8af6f5fSLen Brown 		default:
4084d8af6f5fSLen Brown 			help();
4085d8af6f5fSLen Brown 			exit(1);
4086d8af6f5fSLen Brown 		case 'i':
40872a0609c0SLen Brown 			{
40882a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
40892a0609c0SLen Brown 
40902a0609c0SLen Brown 				if (interval < 0.001) {
4091b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
40922a0609c0SLen Brown 						interval);
40932a0609c0SLen Brown 					exit(2);
40942a0609c0SLen Brown 				}
40952a0609c0SLen Brown 
40962a0609c0SLen Brown 				interval_ts.tv_sec = interval;
40972a0609c0SLen Brown 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
40982a0609c0SLen Brown 			}
4099889facbeSLen Brown 			break;
41005c56be9aSDirk Brandewie 		case 'J':
41015c56be9aSDirk Brandewie 			rapl_joules++;
41025c56be9aSDirk Brandewie 			break;
4103b7d8c148SLen Brown 		case 'o':
4104b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
4105b7d8c148SLen Brown 			break;
4106d8af6f5fSLen Brown 		case 'P':
4107d8af6f5fSLen Brown 			show_pkg_only++;
4108d8af6f5fSLen Brown 			break;
4109d8af6f5fSLen Brown 		case 'p':
4110d8af6f5fSLen Brown 			show_core_only++;
4111d8af6f5fSLen Brown 			break;
4112d8af6f5fSLen Brown 		case 'S':
4113d8af6f5fSLen Brown 			summary_only++;
4114d8af6f5fSLen Brown 			break;
4115d8af6f5fSLen Brown 		case 'T':
4116d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
4117d8af6f5fSLen Brown 			break;
4118d8af6f5fSLen Brown 		case 'v':
4119d8af6f5fSLen Brown 			print_version();
4120d8af6f5fSLen Brown 			exit(0);
4121d8af6f5fSLen Brown 			break;
4122103a8feaSLen Brown 		}
4123103a8feaSLen Brown 	}
4124103a8feaSLen Brown }
4125103a8feaSLen Brown 
4126103a8feaSLen Brown int main(int argc, char **argv)
4127103a8feaSLen Brown {
4128b7d8c148SLen Brown 	outf = stderr;
4129b7d8c148SLen Brown 
4130103a8feaSLen Brown 	cmdline(argc, argv);
4131103a8feaSLen Brown 
4132d8af6f5fSLen Brown 	if (debug)
4133d8af6f5fSLen Brown 		print_version();
4134103a8feaSLen Brown 
4135103a8feaSLen Brown 	turbostat_init();
4136103a8feaSLen Brown 
41373b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
41383b4d5c7fSAndy Shevchenko 	if (dump_only)
41393b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
41403b4d5c7fSAndy Shevchenko 
4141103a8feaSLen Brown 	/*
4142103a8feaSLen Brown 	 * if any params left, it must be a command to fork
4143103a8feaSLen Brown 	 */
4144103a8feaSLen Brown 	if (argc - optind)
4145103a8feaSLen Brown 		return fork_it(argv + optind);
4146103a8feaSLen Brown 	else
4147103a8feaSLen Brown 		turbostat_loop();
4148103a8feaSLen Brown 
4149103a8feaSLen Brown 	return 0;
4150103a8feaSLen Brown }
4151