xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision a01e72fbc41e322ed229465de8b595a7e3ec6549)
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
2495aebc44SJosh Triplett #include <stdarg.h>
25103a8feaSLen Brown #include <stdio.h>
26b2c95d90SJosh Triplett #include <err.h>
27103a8feaSLen Brown #include <unistd.h>
28103a8feaSLen Brown #include <sys/types.h>
29103a8feaSLen Brown #include <sys/wait.h>
30103a8feaSLen Brown #include <sys/stat.h>
31103a8feaSLen Brown #include <sys/resource.h>
32103a8feaSLen Brown #include <fcntl.h>
33103a8feaSLen Brown #include <signal.h>
34103a8feaSLen Brown #include <sys/time.h>
35103a8feaSLen Brown #include <stdlib.h>
36d8af6f5fSLen Brown #include <getopt.h>
37103a8feaSLen Brown #include <dirent.h>
38103a8feaSLen Brown #include <string.h>
39103a8feaSLen Brown #include <ctype.h>
4088c3281fSLen Brown #include <sched.h>
412b92865eSJosh Triplett #include <cpuid.h>
4298481e79SLen Brown #include <linux/capability.h>
4398481e79SLen Brown #include <errno.h>
44103a8feaSLen Brown 
45103a8feaSLen Brown char *proc_stat = "/proc/stat";
46d8af6f5fSLen Brown unsigned int interval_sec = 5;
47d8af6f5fSLen Brown unsigned int debug;
48d8af6f5fSLen Brown unsigned int rapl_joules;
49d8af6f5fSLen Brown unsigned int summary_only;
50d8af6f5fSLen Brown unsigned int dump_only;
51103a8feaSLen Brown unsigned int skip_c0;
52103a8feaSLen Brown unsigned int skip_c1;
53103a8feaSLen Brown unsigned int do_nhm_cstates;
54103a8feaSLen Brown unsigned int do_snb_cstates;
55fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
56ee7e38e3SLen Brown unsigned int do_pc2;
57ee7e38e3SLen Brown unsigned int do_pc3;
58ee7e38e3SLen Brown unsigned int do_pc6;
59ee7e38e3SLen Brown unsigned int do_pc7;
60ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10;
610b2bb692SLen Brown unsigned int do_skl_residency;
62144b44b1SLen Brown unsigned int do_slm_cstates;
63144b44b1SLen Brown unsigned int use_c1_residency_msr;
64103a8feaSLen Brown unsigned int has_aperf;
65889facbeSLen Brown unsigned int has_epb;
66fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
67103a8feaSLen Brown unsigned int genuine_intel;
68103a8feaSLen Brown unsigned int has_invariant_tsc;
69d7899447SLen Brown unsigned int do_nhm_platform_info;
702f32edf1SLen Brown unsigned int extra_msr_offset32;
712f32edf1SLen Brown unsigned int extra_msr_offset64;
728e180f3cSLen Brown unsigned int extra_delta_offset32;
738e180f3cSLen Brown unsigned int extra_delta_offset64;
741ed51011SLen Brown int do_smi;
75103a8feaSLen Brown double bclk;
76103a8feaSLen Brown unsigned int show_pkg;
77103a8feaSLen Brown unsigned int show_core;
78103a8feaSLen Brown unsigned int show_cpu;
79c98d5d94SLen Brown unsigned int show_pkg_only;
80c98d5d94SLen Brown unsigned int show_core_only;
81c98d5d94SLen Brown char *output_buffer, *outp;
82889facbeSLen Brown unsigned int do_rapl;
83889facbeSLen Brown unsigned int do_dts;
84889facbeSLen Brown unsigned int do_ptm;
85889facbeSLen Brown unsigned int tcc_activation_temp;
86889facbeSLen Brown unsigned int tcc_activation_temp_override;
8740ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
8840ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
89889facbeSLen Brown double rapl_joule_counter_range;
903a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
913a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
923a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
938a5bdf41SLen Brown unsigned int crystal_hz;
948a5bdf41SLen Brown unsigned long long tsc_hz;
957ce7d5deSPrarit Bhargava int base_cpu;
96889facbeSLen Brown 
97889facbeSLen Brown #define RAPL_PKG		(1 << 0)
98e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
99e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
100e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
101e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
102e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
103e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
104e6f9bb3cSLen Brown 
105889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
106e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
107e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
108e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
109e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1100b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1110b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
112e6f9bb3cSLen Brown 
1130b2bb692SLen Brown #define RAPL_CORES		(1 << 6)
114e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
115e6f9bb3cSLen Brown 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1160b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
117e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
118e6f9bb3cSLen Brown 
1190b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
120e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
121e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
122e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
123889facbeSLen Brown #define	TJMAX_DEFAULT	100
124889facbeSLen Brown 
125889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
126103a8feaSLen Brown 
127103a8feaSLen Brown int aperf_mperf_unstable;
128103a8feaSLen Brown int backwards_count;
129103a8feaSLen Brown char *progname;
130103a8feaSLen Brown 
131c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set;
132c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize;
133c98d5d94SLen Brown 
134c98d5d94SLen Brown struct thread_data {
135c98d5d94SLen Brown 	unsigned long long tsc;
136c98d5d94SLen Brown 	unsigned long long aperf;
137c98d5d94SLen Brown 	unsigned long long mperf;
138144b44b1SLen Brown 	unsigned long long c1;
1392f32edf1SLen Brown 	unsigned long long extra_msr64;
1408e180f3cSLen Brown 	unsigned long long extra_delta64;
1418e180f3cSLen Brown 	unsigned long long extra_msr32;
1428e180f3cSLen Brown 	unsigned long long extra_delta32;
1431ed51011SLen Brown 	unsigned int smi_count;
144c98d5d94SLen Brown 	unsigned int cpu_id;
145c98d5d94SLen Brown 	unsigned int flags;
146c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
147c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
148c98d5d94SLen Brown } *thread_even, *thread_odd;
149c98d5d94SLen Brown 
150c98d5d94SLen Brown struct core_data {
151c98d5d94SLen Brown 	unsigned long long c3;
152c98d5d94SLen Brown 	unsigned long long c6;
153c98d5d94SLen Brown 	unsigned long long c7;
154889facbeSLen Brown 	unsigned int core_temp_c;
155c98d5d94SLen Brown 	unsigned int core_id;
156c98d5d94SLen Brown } *core_even, *core_odd;
157c98d5d94SLen Brown 
158c98d5d94SLen Brown struct pkg_data {
159c98d5d94SLen Brown 	unsigned long long pc2;
160c98d5d94SLen Brown 	unsigned long long pc3;
161c98d5d94SLen Brown 	unsigned long long pc6;
162c98d5d94SLen Brown 	unsigned long long pc7;
163ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
164ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
165ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1660b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1670b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1680b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1690b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
170c98d5d94SLen Brown 	unsigned int package_id;
171889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
172889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
173889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
174889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
175889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
176889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
177889facbeSLen Brown 	unsigned int pkg_temp_c;
178889facbeSLen Brown 
179c98d5d94SLen Brown } *package_even, *package_odd;
180c98d5d94SLen Brown 
181c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
182c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
183c98d5d94SLen Brown 
184c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
185c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
186c98d5d94SLen Brown 		topo.num_threads_per_core + \
187c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
188c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
189c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
190c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
191c98d5d94SLen Brown 
192c98d5d94SLen Brown struct system_summary {
193c98d5d94SLen Brown 	struct thread_data threads;
194c98d5d94SLen Brown 	struct core_data cores;
195c98d5d94SLen Brown 	struct pkg_data packages;
196c98d5d94SLen Brown } sum, average;
197c98d5d94SLen Brown 
198c98d5d94SLen Brown 
199c98d5d94SLen Brown struct topo_params {
200c98d5d94SLen Brown 	int num_packages;
201103a8feaSLen Brown 	int num_cpus;
202c98d5d94SLen Brown 	int num_cores;
203c98d5d94SLen Brown 	int max_cpu_num;
204c98d5d94SLen Brown 	int num_cores_per_pkg;
205c98d5d94SLen Brown 	int num_threads_per_core;
206c98d5d94SLen Brown } topo;
207103a8feaSLen Brown 
208c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
209103a8feaSLen Brown 
210c98d5d94SLen Brown void setup_all_buffers(void);
211103a8feaSLen Brown 
212c98d5d94SLen Brown int cpu_is_not_present(int cpu)
213d15cf7c1SLen Brown {
214c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
215c98d5d94SLen Brown }
216c98d5d94SLen Brown /*
217c98d5d94SLen Brown  * run func(thread, core, package) in topology order
218c98d5d94SLen Brown  * skip non-present cpus
219c98d5d94SLen Brown  */
220c98d5d94SLen Brown 
221c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
222c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
223c98d5d94SLen Brown {
224c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
225c98d5d94SLen Brown 
226c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
227c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
228c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
229c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
230c98d5d94SLen Brown 				struct thread_data *t;
231c98d5d94SLen Brown 				struct core_data *c;
232c98d5d94SLen Brown 				struct pkg_data *p;
233c98d5d94SLen Brown 
234c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
235c98d5d94SLen Brown 
236c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
237c98d5d94SLen Brown 					continue;
238c98d5d94SLen Brown 
239c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
240c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
241c98d5d94SLen Brown 
242c98d5d94SLen Brown 				retval = func(t, c, p);
243c98d5d94SLen Brown 				if (retval)
244c98d5d94SLen Brown 					return retval;
245c98d5d94SLen Brown 			}
246c98d5d94SLen Brown 		}
247c98d5d94SLen Brown 	}
248d15cf7c1SLen Brown 	return 0;
249d15cf7c1SLen Brown }
250d15cf7c1SLen Brown 
25188c3281fSLen Brown int cpu_migrate(int cpu)
25288c3281fSLen Brown {
253c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
254c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
255c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
25688c3281fSLen Brown 		return -1;
25788c3281fSLen Brown 	else
25888c3281fSLen Brown 		return 0;
25988c3281fSLen Brown }
26088c3281fSLen Brown 
26115aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
262103a8feaSLen Brown {
263103a8feaSLen Brown 	ssize_t retval;
264103a8feaSLen Brown 	char pathname[32];
265103a8feaSLen Brown 	int fd;
266103a8feaSLen Brown 
267103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
268103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
26915aaa346SLen Brown 	if (fd < 0)
27098481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
271103a8feaSLen Brown 
27215aaa346SLen Brown 	retval = pread(fd, msr, sizeof *msr, offset);
273103a8feaSLen Brown 	close(fd);
27415aaa346SLen Brown 
27598481e79SLen Brown 	if (retval != sizeof *msr)
27698481e79SLen Brown 		err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
27715aaa346SLen Brown 
27815aaa346SLen Brown 	return 0;
279103a8feaSLen Brown }
280103a8feaSLen Brown 
281fc04cc67SLen Brown /*
282fc04cc67SLen Brown  * Example Format w/ field column widths:
283fc04cc67SLen Brown  *
284fc04cc67SLen Brown  *  Package    Core     CPU Avg_MHz Bzy_MHz TSC_MHz     SMI   %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp  PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
285e7c95ff3SLen Brown  * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
286fc04cc67SLen Brown  */
287fc04cc67SLen Brown 
288a829eb4dSLen Brown void print_header(void)
289103a8feaSLen Brown {
290103a8feaSLen Brown 	if (show_pkg)
291fc04cc67SLen Brown 		outp += sprintf(outp, " Package");
292103a8feaSLen Brown 	if (show_core)
293fc04cc67SLen Brown 		outp += sprintf(outp, "    Core");
294103a8feaSLen Brown 	if (show_cpu)
295c98d5d94SLen Brown 		outp += sprintf(outp, "     CPU");
296103a8feaSLen Brown 	if (has_aperf)
297fc04cc67SLen Brown 		outp += sprintf(outp, " Avg_MHz");
298d7899447SLen Brown 	if (has_aperf)
299fc04cc67SLen Brown 		outp += sprintf(outp, "   %%Busy");
300fc04cc67SLen Brown 	if (has_aperf)
301fc04cc67SLen Brown 		outp += sprintf(outp, " Bzy_MHz");
302fc04cc67SLen Brown 	outp += sprintf(outp, " TSC_MHz");
3031cc21f7bSLen Brown 
3048e180f3cSLen Brown 	if (extra_delta_offset32)
305f9240813SLen Brown 		outp += sprintf(outp, "  count 0x%03X", extra_delta_offset32);
3068e180f3cSLen Brown 	if (extra_delta_offset64)
307f9240813SLen Brown 		outp += sprintf(outp, "  COUNT 0x%03X", extra_delta_offset64);
3082f32edf1SLen Brown 	if (extra_msr_offset32)
3098e180f3cSLen Brown 		outp += sprintf(outp, "   MSR 0x%03X", extra_msr_offset32);
3102f32edf1SLen Brown 	if (extra_msr_offset64)
3118e180f3cSLen Brown 		outp += sprintf(outp, "           MSR 0x%03X", extra_msr_offset64);
3121cc21f7bSLen Brown 
3131cc21f7bSLen Brown 	if (!debug)
3141cc21f7bSLen Brown 		goto done;
3151cc21f7bSLen Brown 
3161cc21f7bSLen Brown 	if (do_smi)
3171cc21f7bSLen Brown 		outp += sprintf(outp, "     SMI");
3181cc21f7bSLen Brown 
319103a8feaSLen Brown 	if (do_nhm_cstates)
320fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c1");
321fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
322fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c3");
323103a8feaSLen Brown 	if (do_nhm_cstates)
324fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c6");
325103a8feaSLen Brown 	if (do_snb_cstates)
326fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c7");
327889facbeSLen Brown 
328889facbeSLen Brown 	if (do_dts)
329fc04cc67SLen Brown 		outp += sprintf(outp, " CoreTmp");
330889facbeSLen Brown 	if (do_ptm)
331fc04cc67SLen Brown 		outp += sprintf(outp, "  PkgTmp");
332889facbeSLen Brown 
3330b2bb692SLen Brown 	if (do_skl_residency) {
3340b2bb692SLen Brown 		outp += sprintf(outp, " Totl%%C0");
3350b2bb692SLen Brown 		outp += sprintf(outp, "  Any%%C0");
3360b2bb692SLen Brown 		outp += sprintf(outp, "  GFX%%C0");
3370b2bb692SLen Brown 		outp += sprintf(outp, " CPUGFX%%");
3380b2bb692SLen Brown 	}
3390b2bb692SLen Brown 
340ee7e38e3SLen Brown 	if (do_pc2)
341fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc2");
342ee7e38e3SLen Brown 	if (do_pc3)
343fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc3");
344ee7e38e3SLen Brown 	if (do_pc6)
345fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc6");
346ee7e38e3SLen Brown 	if (do_pc7)
347fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc7");
348ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
349fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc8");
350fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc9");
351fc04cc67SLen Brown 		outp += sprintf(outp, " Pk%%pc10");
352ca58710fSKristen Carlson Accardi 	}
353103a8feaSLen Brown 
3545c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
355889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
356fc04cc67SLen Brown 			outp += sprintf(outp, " PkgWatt");
357889facbeSLen Brown 		if (do_rapl & RAPL_CORES)
358fc04cc67SLen Brown 			outp += sprintf(outp, " CorWatt");
359889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
360fc04cc67SLen Brown 			outp += sprintf(outp, " GFXWatt");
361889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
362fc04cc67SLen Brown 			outp += sprintf(outp, " RAMWatt");
363889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
364889facbeSLen Brown 			outp += sprintf(outp, "   PKG_%%");
365889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
366889facbeSLen Brown 			outp += sprintf(outp, "   RAM_%%");
367d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
3685c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
3695c56be9aSDirk Brandewie 			outp += sprintf(outp, "   Pkg_J");
3705c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
3715c56be9aSDirk Brandewie 			outp += sprintf(outp, "   Cor_J");
3725c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
3735c56be9aSDirk Brandewie 			outp += sprintf(outp, "   GFX_J");
3745c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
3755c56be9aSDirk Brandewie 			outp += sprintf(outp, "   RAM_W");
3765c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
3775c56be9aSDirk Brandewie 			outp += sprintf(outp, "   PKG_%%");
3785c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
3795c56be9aSDirk Brandewie 			outp += sprintf(outp, "   RAM_%%");
3805c56be9aSDirk Brandewie 		outp += sprintf(outp, "   time");
381889facbeSLen Brown 
3825c56be9aSDirk Brandewie 	}
3831cc21f7bSLen Brown     done:
384c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
385103a8feaSLen Brown }
386103a8feaSLen Brown 
387c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
388c98d5d94SLen Brown 	struct pkg_data *p)
389103a8feaSLen Brown {
3903b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
391c98d5d94SLen Brown 
392c98d5d94SLen Brown 	if (t) {
3933b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
3943b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
3953b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
3963b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
3973b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
3983b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
3993b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %08llX\n",
4008e180f3cSLen Brown 			extra_delta_offset32, t->extra_delta32);
4013b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %016llX\n",
4028e180f3cSLen Brown 			extra_delta_offset64, t->extra_delta64);
4033b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %08llX\n",
4042f32edf1SLen Brown 			extra_msr_offset32, t->extra_msr32);
4053b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %016llX\n",
4062f32edf1SLen Brown 			extra_msr_offset64, t->extra_msr64);
4071ed51011SLen Brown 		if (do_smi)
4083b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
409103a8feaSLen Brown 	}
410103a8feaSLen Brown 
411c98d5d94SLen Brown 	if (c) {
4123b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
4133b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
4143b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
4153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
4163b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
417c98d5d94SLen Brown 	}
418103a8feaSLen Brown 
419c98d5d94SLen Brown 	if (p) {
4203b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
4210b2bb692SLen Brown 
4220b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
4230b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
4240b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
4250b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
4260b2bb692SLen Brown 
4273b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
428ee7e38e3SLen Brown 		if (do_pc3)
4293b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
430ee7e38e3SLen Brown 		if (do_pc6)
4313b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
432ee7e38e3SLen Brown 		if (do_pc7)
4333b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
4343b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
4353b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
4363b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
4373b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
4383b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
4393b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
4403b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
4413b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
4423b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
4433b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
4443b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
4453b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
446c98d5d94SLen Brown 	}
4473b4d5c7fSAndy Shevchenko 
4483b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
4493b4d5c7fSAndy Shevchenko 
450c98d5d94SLen Brown 	return 0;
451103a8feaSLen Brown }
452103a8feaSLen Brown 
453e23da037SLen Brown /*
454e23da037SLen Brown  * column formatting convention & formats
455e23da037SLen Brown  */
456c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
457c98d5d94SLen Brown 	struct pkg_data *p)
458103a8feaSLen Brown {
459103a8feaSLen Brown 	double interval_float;
460fc04cc67SLen Brown 	char *fmt8;
461103a8feaSLen Brown 
462c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
463c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
464c98d5d94SLen Brown 		return 0;
465c98d5d94SLen Brown 
466c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
467c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
468c98d5d94SLen Brown 		return 0;
469c98d5d94SLen Brown 
470103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
471103a8feaSLen Brown 
472c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
473c98d5d94SLen Brown 	if (t == &average.threads) {
474103a8feaSLen Brown 		if (show_pkg)
475fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
476103a8feaSLen Brown 		if (show_core)
477fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
478103a8feaSLen Brown 		if (show_cpu)
479fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
480103a8feaSLen Brown 	} else {
481c98d5d94SLen Brown 		if (show_pkg) {
482c98d5d94SLen Brown 			if (p)
483fc04cc67SLen Brown 				outp += sprintf(outp, "%8d", p->package_id);
484c98d5d94SLen Brown 			else
485fc04cc67SLen Brown 				outp += sprintf(outp, "       -");
486c98d5d94SLen Brown 		}
487c98d5d94SLen Brown 		if (show_core) {
488c98d5d94SLen Brown 			if (c)
489fc04cc67SLen Brown 				outp += sprintf(outp, "%8d", c->core_id);
490c98d5d94SLen Brown 			else
491fc04cc67SLen Brown 				outp += sprintf(outp, "       -");
492c98d5d94SLen Brown 		}
493103a8feaSLen Brown 		if (show_cpu)
494fc04cc67SLen Brown 			outp += sprintf(outp, "%8d", t->cpu_id);
495103a8feaSLen Brown 	}
496fc04cc67SLen Brown 
497d7899447SLen Brown 	/* Avg_MHz */
498fc04cc67SLen Brown 	if (has_aperf)
499fc04cc67SLen Brown 		outp += sprintf(outp, "%8.0f",
500fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
501fc04cc67SLen Brown 
502d7899447SLen Brown 	/* %Busy */
503d7899447SLen Brown 	if (has_aperf) {
504103a8feaSLen Brown 		if (!skip_c0)
505fc04cc67SLen Brown 			outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
506103a8feaSLen Brown 		else
507fc04cc67SLen Brown 			outp += sprintf(outp, "********");
508103a8feaSLen Brown 	}
509103a8feaSLen Brown 
510d7899447SLen Brown 	/* Bzy_MHz */
511fc04cc67SLen Brown 	if (has_aperf)
512fc04cc67SLen Brown 		outp += sprintf(outp, "%8.0f",
513fc04cc67SLen Brown 			1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
514103a8feaSLen Brown 
515d7899447SLen Brown 	/* TSC_MHz */
516fc04cc67SLen Brown 	outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
517103a8feaSLen Brown 
5188e180f3cSLen Brown 	/* delta */
5198e180f3cSLen Brown 	if (extra_delta_offset32)
5208e180f3cSLen Brown 		outp += sprintf(outp, "  %11llu", t->extra_delta32);
5218e180f3cSLen Brown 
5228e180f3cSLen Brown 	/* DELTA */
5238e180f3cSLen Brown 	if (extra_delta_offset64)
5248e180f3cSLen Brown 		outp += sprintf(outp, "  %11llu", t->extra_delta64);
5252f32edf1SLen Brown 	/* msr */
5262f32edf1SLen Brown 	if (extra_msr_offset32)
5278e180f3cSLen Brown 		outp += sprintf(outp, "  0x%08llx", t->extra_msr32);
5282f32edf1SLen Brown 
529130ff304SLen Brown 	/* MSR */
5302f32edf1SLen Brown 	if (extra_msr_offset64)
5312f32edf1SLen Brown 		outp += sprintf(outp, "  0x%016llx", t->extra_msr64);
532130ff304SLen Brown 
5331cc21f7bSLen Brown 	if (!debug)
5341cc21f7bSLen Brown 		goto done;
5351cc21f7bSLen Brown 
5361cc21f7bSLen Brown 	/* SMI */
5371cc21f7bSLen Brown 	if (do_smi)
5381cc21f7bSLen Brown 		outp += sprintf(outp, "%8d", t->smi_count);
5391cc21f7bSLen Brown 
540103a8feaSLen Brown 	if (do_nhm_cstates) {
541103a8feaSLen Brown 		if (!skip_c1)
542fc04cc67SLen Brown 			outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
543103a8feaSLen Brown 		else
544fc04cc67SLen Brown 			outp += sprintf(outp, "********");
545103a8feaSLen Brown 	}
546c98d5d94SLen Brown 
547c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
548c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
549c98d5d94SLen Brown 		goto done;
550c98d5d94SLen Brown 
551fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
552fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
553103a8feaSLen Brown 	if (do_nhm_cstates)
554fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
555103a8feaSLen Brown 	if (do_snb_cstates)
556fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
557c98d5d94SLen Brown 
558889facbeSLen Brown 	if (do_dts)
559fc04cc67SLen Brown 		outp += sprintf(outp, "%8d", c->core_temp_c);
560889facbeSLen Brown 
561c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
562c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
563c98d5d94SLen Brown 		goto done;
564c98d5d94SLen Brown 
5650b2bb692SLen Brown 	/* PkgTmp */
566889facbeSLen Brown 	if (do_ptm)
567fc04cc67SLen Brown 		outp += sprintf(outp, "%8d", p->pkg_temp_c);
568889facbeSLen Brown 
5690b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
5700b2bb692SLen Brown 	if (do_skl_residency) {
5710b2bb692SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
5720b2bb692SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
5730b2bb692SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
5740b2bb692SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
5750b2bb692SLen Brown 	}
5760b2bb692SLen Brown 
577ee7e38e3SLen Brown 	if (do_pc2)
578fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
579ee7e38e3SLen Brown 	if (do_pc3)
580fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
581ee7e38e3SLen Brown 	if (do_pc6)
582fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
583ee7e38e3SLen Brown 	if (do_pc7)
584fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
585ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
586fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
587fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
588fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
589ca58710fSKristen Carlson Accardi 	}
590889facbeSLen Brown 
591889facbeSLen Brown 	/*
592889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
593889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
594889facbeSLen Brown  	 */
595fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
596fc04cc67SLen Brown 		fmt8 = "%8.2f";
597fc04cc67SLen Brown 	else
598fc04cc67SLen Brown 		fmt8 = " %6.0f**";
599889facbeSLen Brown 
6005c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
601889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
602fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
603889facbeSLen Brown 		if (do_rapl & RAPL_CORES)
604fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
605889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
606fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
607889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
60840ee8e3bSAndrey Semin 			outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
609889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
610fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
611889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
612fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
613d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
6145c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
615fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6165c56be9aSDirk Brandewie 					p->energy_pkg * rapl_energy_units);
6175c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
618fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6195c56be9aSDirk Brandewie 					p->energy_cores * rapl_energy_units);
6205c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
621fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6225c56be9aSDirk Brandewie 					p->energy_gfx * rapl_energy_units);
6235c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
624fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
62540ee8e3bSAndrey Semin 					p->energy_dram * rapl_dram_energy_units);
6265c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
627fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
6285c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
629fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
630889facbeSLen Brown 
631d7899447SLen Brown 		outp += sprintf(outp, fmt8, interval_float);
6325c56be9aSDirk Brandewie 	}
633c98d5d94SLen Brown done:
634c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
635c98d5d94SLen Brown 
636c98d5d94SLen Brown 	return 0;
637103a8feaSLen Brown }
638103a8feaSLen Brown 
639c98d5d94SLen Brown void flush_stdout()
640103a8feaSLen Brown {
641c98d5d94SLen Brown 	fputs(output_buffer, stdout);
642ddac0d68SLen Brown 	fflush(stdout);
643c98d5d94SLen Brown 	outp = output_buffer;
644c98d5d94SLen Brown }
645c98d5d94SLen Brown void flush_stderr()
646c98d5d94SLen Brown {
647c98d5d94SLen Brown 	fputs(output_buffer, stderr);
648c98d5d94SLen Brown 	outp = output_buffer;
649c98d5d94SLen Brown }
650c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
651c98d5d94SLen Brown {
652e23da037SLen Brown 	static int printed;
653103a8feaSLen Brown 
654e23da037SLen Brown 	if (!printed || !summary_only)
655103a8feaSLen Brown 		print_header();
656103a8feaSLen Brown 
657c98d5d94SLen Brown 	if (topo.num_cpus > 1)
658c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
659c98d5d94SLen Brown 			&average.packages);
660103a8feaSLen Brown 
661e23da037SLen Brown 	printed = 1;
662e23da037SLen Brown 
663e23da037SLen Brown 	if (summary_only)
664e23da037SLen Brown 		return;
665e23da037SLen Brown 
666c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
667103a8feaSLen Brown }
668103a8feaSLen Brown 
669889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
670889facbeSLen Brown 	if (new > old) {			\
671889facbeSLen Brown 		old = new - old;		\
672889facbeSLen Brown 	} else {				\
673889facbeSLen Brown 		old = 0x100000000 + new - old;	\
674889facbeSLen Brown 	}
675889facbeSLen Brown 
676c98d5d94SLen Brown void
677c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
678103a8feaSLen Brown {
6790b2bb692SLen Brown 
6800b2bb692SLen Brown 	if (do_skl_residency) {
6810b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
6820b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
6830b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
6840b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
6850b2bb692SLen Brown 	}
686c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
687ee7e38e3SLen Brown 	if (do_pc3)
688c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
689ee7e38e3SLen Brown 	if (do_pc6)
690c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
691ee7e38e3SLen Brown 	if (do_pc7)
692c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
693ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
694ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
695ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
696889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
697889facbeSLen Brown 
698889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
699889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
700889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
701889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
702889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
703889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
704103a8feaSLen Brown }
705103a8feaSLen Brown 
706c98d5d94SLen Brown void
707c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
708c98d5d94SLen Brown {
709c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
710c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
711c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
712889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
713103a8feaSLen Brown }
714c98d5d94SLen Brown 
715c3ae331dSLen Brown /*
716c3ae331dSLen Brown  * old = new - old
717c3ae331dSLen Brown  */
718c98d5d94SLen Brown void
719c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
720c98d5d94SLen Brown 	struct core_data *core_delta)
721c98d5d94SLen Brown {
722c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
723c98d5d94SLen Brown 
724103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
725b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
726b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
727b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
728b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
729103a8feaSLen Brown 
730c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
731c98d5d94SLen Brown 
732a729617cSLen Brown 	if (has_aperf) {
733c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
734c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
735c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
736c98d5d94SLen Brown 		} else {
737c98d5d94SLen Brown 
738103a8feaSLen Brown 			if (!aperf_mperf_unstable) {
739103a8feaSLen Brown 				fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
740103a8feaSLen Brown 				fprintf(stderr, "* Frequency results do not cover entire interval *\n");
741103a8feaSLen Brown 				fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
742103a8feaSLen Brown 
743103a8feaSLen Brown 				aperf_mperf_unstable = 1;
744103a8feaSLen Brown 			}
745103a8feaSLen Brown 			/*
746103a8feaSLen Brown 			 * mperf delta is likely a huge "positive" number
747103a8feaSLen Brown 			 * can not use it for calculating c0 time
748103a8feaSLen Brown 			 */
749103a8feaSLen Brown 			skip_c0 = 1;
750103a8feaSLen Brown 			skip_c1 = 1;
751103a8feaSLen Brown 		}
752a729617cSLen Brown 	}
753103a8feaSLen Brown 
754c98d5d94SLen Brown 
755144b44b1SLen Brown 	if (use_c1_residency_msr) {
756144b44b1SLen Brown 		/*
757144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
758144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
759144b44b1SLen Brown 		 */
760144b44b1SLen Brown 	} else {
761103a8feaSLen Brown 		/*
762c3ae331dSLen Brown 		 * As counter collection is not atomic,
763c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
764103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
765103a8feaSLen Brown 		 */
766c3ae331dSLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
767c98d5d94SLen Brown 			old->c1 = 0;
768c98d5d94SLen Brown 		else {
769c98d5d94SLen Brown 			/* normal case, derive c1 */
770c98d5d94SLen Brown 			old->c1 = old->tsc - old->mperf - core_delta->c3
771c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
772c98d5d94SLen Brown 		}
773144b44b1SLen Brown 	}
774c3ae331dSLen Brown 
775c98d5d94SLen Brown 	if (old->mperf == 0) {
776d8af6f5fSLen Brown 		if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
777c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
778c98d5d94SLen Brown 	}
779103a8feaSLen Brown 
7808e180f3cSLen Brown 	old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
7818e180f3cSLen Brown 	old->extra_delta32 &= 0xFFFFFFFF;
7828e180f3cSLen Brown 
7838e180f3cSLen Brown 	old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
7848e180f3cSLen Brown 
785103a8feaSLen Brown 	/*
7868e180f3cSLen Brown 	 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
787103a8feaSLen Brown 	 */
7882f32edf1SLen Brown 	old->extra_msr32 = new->extra_msr32;
7892f32edf1SLen Brown 	old->extra_msr64 = new->extra_msr64;
7901ed51011SLen Brown 
7911ed51011SLen Brown 	if (do_smi)
7921ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
793103a8feaSLen Brown }
794c98d5d94SLen Brown 
795c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
796c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
797c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
798c98d5d94SLen Brown {
799c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
800c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
801c98d5d94SLen Brown 		delta_core(c, c2);
802c98d5d94SLen Brown 
803c98d5d94SLen Brown 	/* always calculate thread delta */
804c98d5d94SLen Brown 	delta_thread(t, t2, c2);	/* c2 is core delta */
805c98d5d94SLen Brown 
806c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
807c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
808c98d5d94SLen Brown 		delta_package(p, p2);
809c98d5d94SLen Brown 
810103a8feaSLen Brown 	return 0;
811103a8feaSLen Brown }
812103a8feaSLen Brown 
813c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
814103a8feaSLen Brown {
815c98d5d94SLen Brown 	t->tsc = 0;
816c98d5d94SLen Brown 	t->aperf = 0;
817c98d5d94SLen Brown 	t->mperf = 0;
818c98d5d94SLen Brown 	t->c1 = 0;
819103a8feaSLen Brown 
8201ed51011SLen Brown 	t->smi_count = 0;
8218e180f3cSLen Brown 	t->extra_delta32 = 0;
8228e180f3cSLen Brown 	t->extra_delta64 = 0;
8238e180f3cSLen Brown 
824c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
825c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
826c98d5d94SLen Brown 
827c98d5d94SLen Brown 	c->c3 = 0;
828c98d5d94SLen Brown 	c->c6 = 0;
829c98d5d94SLen Brown 	c->c7 = 0;
830889facbeSLen Brown 	c->core_temp_c = 0;
831c98d5d94SLen Brown 
8320b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
8330b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
8340b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
8350b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
8360b2bb692SLen Brown 
837c98d5d94SLen Brown 	p->pc2 = 0;
838ee7e38e3SLen Brown 	if (do_pc3)
839c98d5d94SLen Brown 		p->pc3 = 0;
840ee7e38e3SLen Brown 	if (do_pc6)
841c98d5d94SLen Brown 		p->pc6 = 0;
842ee7e38e3SLen Brown 	if (do_pc7)
843c98d5d94SLen Brown 		p->pc7 = 0;
844ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
845ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
846ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
847889facbeSLen Brown 
848889facbeSLen Brown 	p->energy_pkg = 0;
849889facbeSLen Brown 	p->energy_dram = 0;
850889facbeSLen Brown 	p->energy_cores = 0;
851889facbeSLen Brown 	p->energy_gfx = 0;
852889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
853889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
854889facbeSLen Brown 	p->pkg_temp_c = 0;
855103a8feaSLen Brown }
856c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
857c98d5d94SLen Brown 	struct pkg_data *p)
858103a8feaSLen Brown {
859c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
860c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
861c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
862c98d5d94SLen Brown 	average.threads.c1 += t->c1;
86315aaa346SLen Brown 
8648e180f3cSLen Brown 	average.threads.extra_delta32 += t->extra_delta32;
8658e180f3cSLen Brown 	average.threads.extra_delta64 += t->extra_delta64;
8668e180f3cSLen Brown 
867c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
868c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
869c98d5d94SLen Brown 		return 0;
870c98d5d94SLen Brown 
871c98d5d94SLen Brown 	average.cores.c3 += c->c3;
872c98d5d94SLen Brown 	average.cores.c6 += c->c6;
873c98d5d94SLen Brown 	average.cores.c7 += c->c7;
874c98d5d94SLen Brown 
875889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
876889facbeSLen Brown 
877c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
878c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
879c98d5d94SLen Brown 		return 0;
880c98d5d94SLen Brown 
8810b2bb692SLen Brown 	if (do_skl_residency) {
8820b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
8830b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
8840b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
8850b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
8860b2bb692SLen Brown 	}
8870b2bb692SLen Brown 
888c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
889ee7e38e3SLen Brown 	if (do_pc3)
890c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
891ee7e38e3SLen Brown 	if (do_pc6)
892c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
893ee7e38e3SLen Brown 	if (do_pc7)
894c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
895ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
896ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
897ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
898c98d5d94SLen Brown 
899889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
900889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
901889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
902889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
903889facbeSLen Brown 
904889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
905889facbeSLen Brown 
906889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
907889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
908c98d5d94SLen Brown 	return 0;
909c98d5d94SLen Brown }
910c98d5d94SLen Brown /*
911c98d5d94SLen Brown  * sum the counters for all cpus in the system
912c98d5d94SLen Brown  * compute the weighted average
913c98d5d94SLen Brown  */
914c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
915c98d5d94SLen Brown 	struct pkg_data *p)
916c98d5d94SLen Brown {
917c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
918c98d5d94SLen Brown 
919c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
920c98d5d94SLen Brown 
921c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
922c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
923c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
924c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
925c98d5d94SLen Brown 
9268e180f3cSLen Brown 	average.threads.extra_delta32 /= topo.num_cpus;
9278e180f3cSLen Brown 	average.threads.extra_delta32 &= 0xFFFFFFFF;
9288e180f3cSLen Brown 
9298e180f3cSLen Brown 	average.threads.extra_delta64 /= topo.num_cpus;
9308e180f3cSLen Brown 
931c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
932c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
933c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
934c98d5d94SLen Brown 
9350b2bb692SLen Brown 	if (do_skl_residency) {
9360b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
9370b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
9380b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
9390b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
9400b2bb692SLen Brown 	}
9410b2bb692SLen Brown 
942c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
943ee7e38e3SLen Brown 	if (do_pc3)
944c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
945ee7e38e3SLen Brown 	if (do_pc6)
946c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
947ee7e38e3SLen Brown 	if (do_pc7)
948c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
949ca58710fSKristen Carlson Accardi 
950ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
951ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
952ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
953c98d5d94SLen Brown }
954c98d5d94SLen Brown 
955c98d5d94SLen Brown static unsigned long long rdtsc(void)
956c98d5d94SLen Brown {
957c98d5d94SLen Brown 	unsigned int low, high;
958c98d5d94SLen Brown 
959c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
960c98d5d94SLen Brown 
961c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
962c98d5d94SLen Brown }
963c98d5d94SLen Brown 
964c98d5d94SLen Brown 
965c98d5d94SLen Brown /*
966c98d5d94SLen Brown  * get_counters(...)
967c98d5d94SLen Brown  * migrate to cpu
968c98d5d94SLen Brown  * acquire and record local counters for that cpu
969c98d5d94SLen Brown  */
970c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
971c98d5d94SLen Brown {
972c98d5d94SLen Brown 	int cpu = t->cpu_id;
973889facbeSLen Brown 	unsigned long long msr;
974c98d5d94SLen Brown 
975e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
976e52966c0SLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
97715aaa346SLen Brown 		return -1;
978e52966c0SLen Brown 	}
97915aaa346SLen Brown 
980c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
98115aaa346SLen Brown 
98215aaa346SLen Brown 	if (has_aperf) {
9839c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
984c98d5d94SLen Brown 			return -3;
9859c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
986c98d5d94SLen Brown 			return -4;
98788c3281fSLen Brown 	}
98888c3281fSLen Brown 
9891ed51011SLen Brown 	if (do_smi) {
9901ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
9911ed51011SLen Brown 			return -5;
9921ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
9931ed51011SLen Brown 	}
9948e180f3cSLen Brown 	if (extra_delta_offset32) {
995889facbeSLen Brown 		if (get_msr(cpu, extra_delta_offset32, &msr))
9962f32edf1SLen Brown 			return -5;
997889facbeSLen Brown 		t->extra_delta32 = msr & 0xFFFFFFFF;
9988e180f3cSLen Brown 	}
9998e180f3cSLen Brown 
10008e180f3cSLen Brown 	if (extra_delta_offset64)
10018e180f3cSLen Brown 		if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
10028e180f3cSLen Brown 			return -5;
10038e180f3cSLen Brown 
10048e180f3cSLen Brown 	if (extra_msr_offset32) {
1005889facbeSLen Brown 		if (get_msr(cpu, extra_msr_offset32, &msr))
10068e180f3cSLen Brown 			return -5;
1007889facbeSLen Brown 		t->extra_msr32 = msr & 0xFFFFFFFF;
10088e180f3cSLen Brown 	}
10092f32edf1SLen Brown 
10102f32edf1SLen Brown 	if (extra_msr_offset64)
10112f32edf1SLen Brown 		if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
1012c98d5d94SLen Brown 			return -5;
1013c98d5d94SLen Brown 
1014144b44b1SLen Brown 	if (use_c1_residency_msr) {
1015144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1016144b44b1SLen Brown 			return -6;
1017144b44b1SLen Brown 	}
1018144b44b1SLen Brown 
1019c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1020c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1021c98d5d94SLen Brown 		return 0;
1022c98d5d94SLen Brown 
1023fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
1024c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1025c98d5d94SLen Brown 			return -6;
1026144b44b1SLen Brown 	}
1027144b44b1SLen Brown 
1028fb5d4327SDasaratharaman Chandramouli 	if (do_nhm_cstates && !do_knl_cstates) {
1029c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1030c98d5d94SLen Brown 			return -7;
1031fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1032fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1033fb5d4327SDasaratharaman Chandramouli 			return -7;
1034103a8feaSLen Brown 	}
103515aaa346SLen Brown 
103615aaa346SLen Brown 	if (do_snb_cstates)
1037c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1038c98d5d94SLen Brown 			return -8;
1039c98d5d94SLen Brown 
1040889facbeSLen Brown 	if (do_dts) {
1041889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1042889facbeSLen Brown 			return -9;
1043889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1044889facbeSLen Brown 	}
1045889facbeSLen Brown 
1046889facbeSLen Brown 
1047c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1048c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1049c98d5d94SLen Brown 		return 0;
105015aaa346SLen Brown 
10510b2bb692SLen Brown 	if (do_skl_residency) {
10520b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
10530b2bb692SLen Brown 			return -10;
10540b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
10550b2bb692SLen Brown 			return -11;
10560b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
10570b2bb692SLen Brown 			return -12;
10580b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
10590b2bb692SLen Brown 			return -13;
10600b2bb692SLen Brown 	}
1061ee7e38e3SLen Brown 	if (do_pc3)
1062c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1063c98d5d94SLen Brown 			return -9;
1064ee7e38e3SLen Brown 	if (do_pc6)
1065c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1066c98d5d94SLen Brown 			return -10;
1067ee7e38e3SLen Brown 	if (do_pc2)
1068c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1069c98d5d94SLen Brown 			return -11;
1070ee7e38e3SLen Brown 	if (do_pc7)
1071c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1072c98d5d94SLen Brown 			return -12;
1073ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
1074ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1075ca58710fSKristen Carlson Accardi 			return -13;
1076ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1077ca58710fSKristen Carlson Accardi 			return -13;
1078ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1079ca58710fSKristen Carlson Accardi 			return -13;
1080ca58710fSKristen Carlson Accardi 	}
1081889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1082889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1083889facbeSLen Brown 			return -13;
1084889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1085889facbeSLen Brown 	}
1086889facbeSLen Brown 	if (do_rapl & RAPL_CORES) {
1087889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1088889facbeSLen Brown 			return -14;
1089889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1090889facbeSLen Brown 	}
1091889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1092889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1093889facbeSLen Brown 			return -15;
1094889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1095889facbeSLen Brown 	}
1096889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1097889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1098889facbeSLen Brown 			return -16;
1099889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1100889facbeSLen Brown 	}
1101889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1102889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1103889facbeSLen Brown 			return -16;
1104889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1105889facbeSLen Brown 	}
1106889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1107889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1108889facbeSLen Brown 			return -16;
1109889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1110889facbeSLen Brown 	}
1111889facbeSLen Brown 	if (do_ptm) {
1112889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1113889facbeSLen Brown 			return -17;
1114889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1115889facbeSLen Brown 	}
111615aaa346SLen Brown 	return 0;
1117103a8feaSLen Brown }
1118103a8feaSLen Brown 
1119ee7e38e3SLen Brown /*
1120ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1121ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1122ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1123ee7e38e3SLen Brown  */
1124ee7e38e3SLen Brown 
1125ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1126ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1127ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1128ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1129ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1130ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1131ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1132ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1133ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1134ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1135ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1136ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
11370b2bb692SLen Brown #define PCL__8 12 /* PC8 */
11380b2bb692SLen Brown #define PCL__9 13 /* PC9 */
11390b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1140ee7e38e3SLen Brown 
1141ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1142ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
11430b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1144ee7e38e3SLen Brown 
1145e9257f5fSLen 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};
1146e9257f5fSLen 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};
1147e9257f5fSLen 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};
1148e9257f5fSLen 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};
1149e9257f5fSLen 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};
1150e9257f5fSLen 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};
1151ee7e38e3SLen Brown 
1152fcd17211SLen Brown static void
1153fcd17211SLen Brown dump_nhm_platform_info(void)
1154103a8feaSLen Brown {
1155103a8feaSLen Brown 	unsigned long long msr;
1156103a8feaSLen Brown 	unsigned int ratio;
1157103a8feaSLen Brown 
11587ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_PLATFORM_INFO, &msr);
1159103a8feaSLen Brown 
1160bfae2052SLen Brown 	fprintf(stderr, "cpu%d: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
11616574a5d5SLen Brown 
1162103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
11638f61f359SLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1164103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1165103a8feaSLen Brown 
1166103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
11678f61f359SLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
1168103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1169103a8feaSLen Brown 
11707ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1171bfae2052SLen Brown 	fprintf(stderr, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1172bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
117367920418SLen Brown 
1174fcd17211SLen Brown 	return;
1175fcd17211SLen Brown }
1176fcd17211SLen Brown 
1177fcd17211SLen Brown static void
1178fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1179fcd17211SLen Brown {
1180fcd17211SLen Brown 	unsigned long long msr;
1181fcd17211SLen Brown 	unsigned int ratio;
1182fcd17211SLen Brown 
11837ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1184fcd17211SLen Brown 
1185bfae2052SLen Brown 	fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1186fcd17211SLen Brown 
1187fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1188fcd17211SLen Brown 	if (ratio)
1189fcd17211SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1190fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1191fcd17211SLen Brown 
1192fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1193fcd17211SLen Brown 	if (ratio)
1194fcd17211SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1195fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1196fcd17211SLen Brown 	return;
1197fcd17211SLen Brown }
1198fcd17211SLen Brown 
1199fcd17211SLen Brown static void
1200fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1201fcd17211SLen Brown {
1202fcd17211SLen Brown 	unsigned long long msr;
1203fcd17211SLen Brown 	unsigned int ratio;
12046574a5d5SLen Brown 
12057ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
12066574a5d5SLen Brown 
1207bfae2052SLen Brown 	fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
12086574a5d5SLen Brown 
12096574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
12106574a5d5SLen Brown 	if (ratio)
12116574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
12126574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12136574a5d5SLen Brown 
12146574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
12156574a5d5SLen Brown 	if (ratio)
12166574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
12176574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12186574a5d5SLen Brown 
12196574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
12206574a5d5SLen Brown 	if (ratio)
12216574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
12226574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12236574a5d5SLen Brown 
12246574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
12256574a5d5SLen Brown 	if (ratio)
12266574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
12276574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12286574a5d5SLen Brown 
12296574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
12306574a5d5SLen Brown 	if (ratio)
12316574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
12326574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12336574a5d5SLen Brown 
12346574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
12356574a5d5SLen Brown 	if (ratio)
12366574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
12376574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12386574a5d5SLen Brown 
12396574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
12406574a5d5SLen Brown 	if (ratio)
12416574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
12426574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12436574a5d5SLen Brown 
12446574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
12456574a5d5SLen Brown 	if (ratio)
12466574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
12476574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1248103a8feaSLen Brown 	return;
1249fcd17211SLen Brown }
1250fcd17211SLen Brown 
1251fcd17211SLen Brown static void
1252fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void)
1253fcd17211SLen Brown {
1254fcd17211SLen Brown 	unsigned long long msr;
1255fcd17211SLen Brown 	unsigned int ratio;
1256103a8feaSLen Brown 
12577ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1258103a8feaSLen Brown 
1259bfae2052SLen Brown 	fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
12606574a5d5SLen Brown 
12616574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
12626574a5d5SLen Brown 	if (ratio)
12636574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
12646574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12656574a5d5SLen Brown 
12666574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
12676574a5d5SLen Brown 	if (ratio)
12686574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
12696574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12706574a5d5SLen Brown 
12716574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
12726574a5d5SLen Brown 	if (ratio)
12736574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
12746574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12756574a5d5SLen Brown 
12766574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
12776574a5d5SLen Brown 	if (ratio)
12786574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
12796574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12806574a5d5SLen Brown 
1281103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
1282103a8feaSLen Brown 	if (ratio)
1283103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1284103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1285103a8feaSLen Brown 
1286103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
1287103a8feaSLen Brown 	if (ratio)
1288103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1289103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1290103a8feaSLen Brown 
1291103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1292103a8feaSLen Brown 	if (ratio)
1293103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1294103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1295103a8feaSLen Brown 
1296103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
1297103a8feaSLen Brown 	if (ratio)
1298103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1299103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1300fcd17211SLen Brown 	return;
1301fcd17211SLen Brown }
13023a9a941dSLen Brown 
1303fcd17211SLen Brown static void
1304fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
1305fb5d4327SDasaratharaman Chandramouli {
1306fb5d4327SDasaratharaman Chandramouli 	int cores;
1307fb5d4327SDasaratharaman Chandramouli 	unsigned int ratio;
1308fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
1309fb5d4327SDasaratharaman Chandramouli 	int delta_cores;
1310fb5d4327SDasaratharaman Chandramouli 	int delta_ratio;
1311fb5d4327SDasaratharaman Chandramouli 	int i;
1312fb5d4327SDasaratharaman Chandramouli 
13137ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1314fb5d4327SDasaratharaman Chandramouli 
1315bfae2052SLen Brown 	fprintf(stderr, "cpu%d: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
1316bfae2052SLen Brown 		base_cpu, msr);
1317fb5d4327SDasaratharaman Chandramouli 
1318fb5d4327SDasaratharaman Chandramouli 	/**
1319fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
1320fb5d4327SDasaratharaman Chandramouli 	 * [7:0] -- Base value of number of active cores of bucket 1.
1321fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
1322fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
1323fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
1324fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
1325fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
1326fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
1327fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
1328fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
1329fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
1330fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
1331fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
1332fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
1333fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
1334fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
1335fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
1336fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
1337fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
1338fb5d4327SDasaratharaman Chandramouli 	 */
1339fb5d4327SDasaratharaman Chandramouli 	cores = msr & 0xFF;
1340fb5d4327SDasaratharaman Chandramouli 	ratio = (msr >> 8) && 0xFF;
1341fb5d4327SDasaratharaman Chandramouli 	if (ratio > 0)
1342fb5d4327SDasaratharaman Chandramouli 		fprintf(stderr,
1343fb5d4327SDasaratharaman Chandramouli 			"%d * %.0f = %.0f MHz max turbo %d active cores\n",
1344fb5d4327SDasaratharaman Chandramouli 			ratio, bclk, ratio * bclk, cores);
1345fb5d4327SDasaratharaman Chandramouli 
1346fb5d4327SDasaratharaman Chandramouli 	for (i = 16; i < 64; i = i + 8) {
1347fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
1348fb5d4327SDasaratharaman Chandramouli 		delta_ratio = (msr >> (i + 5)) && 0x7;
1349fb5d4327SDasaratharaman Chandramouli 		if (!delta_cores || !delta_ratio)
1350fb5d4327SDasaratharaman Chandramouli 			return;
1351fb5d4327SDasaratharaman Chandramouli 		cores = cores + delta_cores;
1352fb5d4327SDasaratharaman Chandramouli 		ratio = ratio - delta_ratio;
1353fb5d4327SDasaratharaman Chandramouli 
1354fb5d4327SDasaratharaman Chandramouli 		/** -ve ratios will make successive ratio calculations
1355fb5d4327SDasaratharaman Chandramouli 		 * negative. Hence return instead of carrying on.
1356fb5d4327SDasaratharaman Chandramouli 		 */
1357fb5d4327SDasaratharaman Chandramouli 		if (ratio > 0)
1358fb5d4327SDasaratharaman Chandramouli 			fprintf(stderr,
1359fb5d4327SDasaratharaman Chandramouli 				"%d * %.0f = %.0f MHz max turbo %d active cores\n",
1360fb5d4327SDasaratharaman Chandramouli 				ratio, bclk, ratio * bclk, cores);
1361fb5d4327SDasaratharaman Chandramouli 	}
1362fb5d4327SDasaratharaman Chandramouli }
1363fb5d4327SDasaratharaman Chandramouli 
1364fb5d4327SDasaratharaman Chandramouli static void
1365fcd17211SLen Brown dump_nhm_cst_cfg(void)
1366fcd17211SLen Brown {
1367fcd17211SLen Brown 	unsigned long long msr;
1368fcd17211SLen Brown 
13697ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1370fcd17211SLen Brown 
1371fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
1372fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
1373fcd17211SLen Brown 
1374bfae2052SLen Brown 	fprintf(stderr, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
1375fcd17211SLen Brown 
1376fcd17211SLen Brown 	fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1377fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1378fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1379fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1380fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1381fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
1382fcd17211SLen Brown 		(unsigned int)msr & 7,
1383fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
1384fcd17211SLen Brown 	return;
1385103a8feaSLen Brown }
1386103a8feaSLen Brown 
13876fb3143bSLen Brown static void
13886fb3143bSLen Brown dump_config_tdp(void)
13896fb3143bSLen Brown {
13906fb3143bSLen Brown 	unsigned long long msr;
13916fb3143bSLen Brown 
13926fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
13936fb3143bSLen Brown 	fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
13946fb3143bSLen Brown 	fprintf(stderr, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF);
13956fb3143bSLen Brown 
13966fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
13976fb3143bSLen Brown 	fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
13986fb3143bSLen Brown 	if (msr) {
13996fb3143bSLen Brown 		fprintf(stderr, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
14006fb3143bSLen Brown 		fprintf(stderr, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
14016fb3143bSLen Brown 		fprintf(stderr, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
14026fb3143bSLen Brown 		fprintf(stderr, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF);
14036fb3143bSLen Brown 	}
14046fb3143bSLen Brown 	fprintf(stderr, ")\n");
14056fb3143bSLen Brown 
14066fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
14076fb3143bSLen Brown 	fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
14086fb3143bSLen Brown 	if (msr) {
14096fb3143bSLen Brown 		fprintf(stderr, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
14106fb3143bSLen Brown 		fprintf(stderr, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
14116fb3143bSLen Brown 		fprintf(stderr, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
14126fb3143bSLen Brown 		fprintf(stderr, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF);
14136fb3143bSLen Brown 	}
14146fb3143bSLen Brown 	fprintf(stderr, ")\n");
14156fb3143bSLen Brown 
14166fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
14176fb3143bSLen Brown 	fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
14186fb3143bSLen Brown 	if ((msr) & 0x3)
14196fb3143bSLen Brown 		fprintf(stderr, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
14206fb3143bSLen Brown 	fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
14216fb3143bSLen Brown 	fprintf(stderr, ")\n");
14226fb3143bSLen Brown 
14236fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
14246fb3143bSLen Brown 	fprintf(stderr, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
14256fb3143bSLen Brown 	fprintf(stderr, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xEF);
14266fb3143bSLen Brown 	fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
14276fb3143bSLen Brown 	fprintf(stderr, ")\n");
14286fb3143bSLen Brown }
14296fb3143bSLen Brown 
1430c98d5d94SLen Brown void free_all_buffers(void)
1431103a8feaSLen Brown {
1432c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
1433c98d5d94SLen Brown 	cpu_present_set = NULL;
1434c98d5d94SLen Brown 	cpu_present_set = 0;
1435103a8feaSLen Brown 
1436c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
1437c98d5d94SLen Brown 	cpu_affinity_set = NULL;
1438c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
1439103a8feaSLen Brown 
1440c98d5d94SLen Brown 	free(thread_even);
1441c98d5d94SLen Brown 	free(core_even);
1442c98d5d94SLen Brown 	free(package_even);
1443103a8feaSLen Brown 
1444c98d5d94SLen Brown 	thread_even = NULL;
1445c98d5d94SLen Brown 	core_even = NULL;
1446c98d5d94SLen Brown 	package_even = NULL;
1447103a8feaSLen Brown 
1448c98d5d94SLen Brown 	free(thread_odd);
1449c98d5d94SLen Brown 	free(core_odd);
1450c98d5d94SLen Brown 	free(package_odd);
1451103a8feaSLen Brown 
1452c98d5d94SLen Brown 	thread_odd = NULL;
1453c98d5d94SLen Brown 	core_odd = NULL;
1454c98d5d94SLen Brown 	package_odd = NULL;
1455103a8feaSLen Brown 
1456c98d5d94SLen Brown 	free(output_buffer);
1457c98d5d94SLen Brown 	output_buffer = NULL;
1458c98d5d94SLen Brown 	outp = NULL;
1459103a8feaSLen Brown }
1460103a8feaSLen Brown 
1461103a8feaSLen Brown /*
146257a42a34SJosh Triplett  * Open a file, and exit on failure
146357a42a34SJosh Triplett  */
146457a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode)
146557a42a34SJosh Triplett {
146657a42a34SJosh Triplett 	FILE *filep = fopen(path, "r");
1467b2c95d90SJosh Triplett 	if (!filep)
1468b2c95d90SJosh Triplett 		err(1, "%s: open failed", path);
146957a42a34SJosh Triplett 	return filep;
147057a42a34SJosh Triplett }
147157a42a34SJosh Triplett 
147257a42a34SJosh Triplett /*
147395aebc44SJosh Triplett  * Parse a file containing a single int.
147495aebc44SJosh Triplett  */
147595aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
147695aebc44SJosh Triplett {
147795aebc44SJosh Triplett 	va_list args;
147895aebc44SJosh Triplett 	char path[PATH_MAX];
147995aebc44SJosh Triplett 	FILE *filep;
148095aebc44SJosh Triplett 	int value;
148195aebc44SJosh Triplett 
148295aebc44SJosh Triplett 	va_start(args, fmt);
148395aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
148495aebc44SJosh Triplett 	va_end(args);
148557a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1486b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
1487b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
148895aebc44SJosh Triplett 	fclose(filep);
148995aebc44SJosh Triplett 	return value;
149095aebc44SJosh Triplett }
149195aebc44SJosh Triplett 
149295aebc44SJosh Triplett /*
1493e275b388SDasaratharaman Chandramouli  * get_cpu_position_in_core(cpu)
1494e275b388SDasaratharaman Chandramouli  * return the position of the CPU among its HT siblings in the core
1495e275b388SDasaratharaman Chandramouli  * return -1 if the sibling is not in list
1496103a8feaSLen Brown  */
1497e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu)
1498103a8feaSLen Brown {
1499e275b388SDasaratharaman Chandramouli 	char path[64];
1500e275b388SDasaratharaman Chandramouli 	FILE *filep;
1501e275b388SDasaratharaman Chandramouli 	int this_cpu;
1502e275b388SDasaratharaman Chandramouli 	char character;
1503e275b388SDasaratharaman Chandramouli 	int i;
1504e275b388SDasaratharaman Chandramouli 
1505e275b388SDasaratharaman Chandramouli 	sprintf(path,
1506e275b388SDasaratharaman Chandramouli 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1507e275b388SDasaratharaman Chandramouli 		cpu);
1508e275b388SDasaratharaman Chandramouli 	filep = fopen(path, "r");
1509e275b388SDasaratharaman Chandramouli 	if (filep == NULL) {
1510e275b388SDasaratharaman Chandramouli 		perror(path);
1511e275b388SDasaratharaman Chandramouli 		exit(1);
1512e275b388SDasaratharaman Chandramouli 	}
1513e275b388SDasaratharaman Chandramouli 
1514e275b388SDasaratharaman Chandramouli 	for (i = 0; i < topo.num_threads_per_core; i++) {
1515e275b388SDasaratharaman Chandramouli 		fscanf(filep, "%d", &this_cpu);
1516e275b388SDasaratharaman Chandramouli 		if (this_cpu == cpu) {
1517e275b388SDasaratharaman Chandramouli 			fclose(filep);
1518e275b388SDasaratharaman Chandramouli 			return i;
1519e275b388SDasaratharaman Chandramouli 		}
1520e275b388SDasaratharaman Chandramouli 
1521e275b388SDasaratharaman Chandramouli 		/* Account for no separator after last thread*/
1522e275b388SDasaratharaman Chandramouli 		if (i != (topo.num_threads_per_core - 1))
1523e275b388SDasaratharaman Chandramouli 			fscanf(filep, "%c", &character);
1524e275b388SDasaratharaman Chandramouli 	}
1525e275b388SDasaratharaman Chandramouli 
1526e275b388SDasaratharaman Chandramouli 	fclose(filep);
1527e275b388SDasaratharaman Chandramouli 	return -1;
1528c98d5d94SLen Brown }
1529103a8feaSLen Brown 
1530c98d5d94SLen Brown /*
1531c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
1532c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
1533c98d5d94SLen Brown  */
1534c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
1535c98d5d94SLen Brown {
153695aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1537103a8feaSLen Brown }
1538103a8feaSLen Brown 
1539103a8feaSLen Brown int get_physical_package_id(int cpu)
1540103a8feaSLen Brown {
154195aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1542103a8feaSLen Brown }
1543103a8feaSLen Brown 
1544103a8feaSLen Brown int get_core_id(int cpu)
1545103a8feaSLen Brown {
154695aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1547103a8feaSLen Brown }
1548103a8feaSLen Brown 
1549c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
1550c98d5d94SLen Brown {
1551c98d5d94SLen Brown 	char path[80];
1552c98d5d94SLen Brown 	FILE *filep;
1553e275b388SDasaratharaman Chandramouli 	int sib1;
1554e275b388SDasaratharaman Chandramouli 	int matches = 0;
1555c98d5d94SLen Brown 	char character;
1556e275b388SDasaratharaman Chandramouli 	char str[100];
1557e275b388SDasaratharaman Chandramouli 	char *ch;
1558c98d5d94SLen Brown 
1559c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
156057a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1561e275b388SDasaratharaman Chandramouli 
1562103a8feaSLen Brown 	/*
1563c98d5d94SLen Brown 	 * file format:
1564e275b388SDasaratharaman Chandramouli 	 * A ',' separated or '-' separated set of numbers
1565e275b388SDasaratharaman Chandramouli 	 * (eg 1-2 or 1,3,4,5)
1566c98d5d94SLen Brown 	 */
1567e275b388SDasaratharaman Chandramouli 	fscanf(filep, "%d%c\n", &sib1, &character);
1568e275b388SDasaratharaman Chandramouli 	fseek(filep, 0, SEEK_SET);
1569e275b388SDasaratharaman Chandramouli 	fgets(str, 100, filep);
1570e275b388SDasaratharaman Chandramouli 	ch = strchr(str, character);
1571e275b388SDasaratharaman Chandramouli 	while (ch != NULL) {
1572e275b388SDasaratharaman Chandramouli 		matches++;
1573e275b388SDasaratharaman Chandramouli 		ch = strchr(ch+1, character);
1574e275b388SDasaratharaman Chandramouli 	}
1575c98d5d94SLen Brown 
1576c98d5d94SLen Brown 	fclose(filep);
1577e275b388SDasaratharaman Chandramouli 	return matches+1;
1578c98d5d94SLen Brown }
1579c98d5d94SLen Brown 
1580c98d5d94SLen Brown /*
1581c98d5d94SLen Brown  * run func(thread, core, package) in topology order
1582c98d5d94SLen Brown  * skip non-present cpus
1583103a8feaSLen Brown  */
1584103a8feaSLen Brown 
1585c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1586c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
1587c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
1588c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
1589c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
1590c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
1591c98d5d94SLen Brown {
1592c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
1593c98d5d94SLen Brown 
1594c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1595c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1596c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
1597c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
1598c98d5d94SLen Brown 				struct thread_data *t, *t2;
1599c98d5d94SLen Brown 				struct core_data *c, *c2;
1600c98d5d94SLen Brown 				struct pkg_data *p, *p2;
1601c98d5d94SLen Brown 
1602c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1603c98d5d94SLen Brown 
1604c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
1605c98d5d94SLen Brown 					continue;
1606c98d5d94SLen Brown 
1607c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1608c98d5d94SLen Brown 
1609c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
1610c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
1611c98d5d94SLen Brown 
1612c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
1613c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
1614c98d5d94SLen Brown 
1615c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
1616c98d5d94SLen Brown 				if (retval)
1617c98d5d94SLen Brown 					return retval;
1618c98d5d94SLen Brown 			}
1619c98d5d94SLen Brown 		}
1620c98d5d94SLen Brown 	}
1621c98d5d94SLen Brown 	return 0;
1622c98d5d94SLen Brown }
1623c98d5d94SLen Brown 
1624c98d5d94SLen Brown /*
1625c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
1626c98d5d94SLen Brown  * return max_cpu number
1627c98d5d94SLen Brown  */
1628c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
1629103a8feaSLen Brown {
1630103a8feaSLen Brown 	FILE *fp;
1631c98d5d94SLen Brown 	int cpu_num;
1632103a8feaSLen Brown 	int retval;
1633103a8feaSLen Brown 
163457a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
1635103a8feaSLen Brown 
1636103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1637b2c95d90SJosh Triplett 	if (retval != 0)
1638b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
1639103a8feaSLen Brown 
1640c98d5d94SLen Brown 	while (1) {
1641c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1642103a8feaSLen Brown 		if (retval != 1)
1643103a8feaSLen Brown 			break;
1644103a8feaSLen Brown 
1645c98d5d94SLen Brown 		retval = func(cpu_num);
1646c98d5d94SLen Brown 		if (retval) {
1647c98d5d94SLen Brown 			fclose(fp);
1648c98d5d94SLen Brown 			return(retval);
1649c98d5d94SLen Brown 		}
1650103a8feaSLen Brown 	}
1651103a8feaSLen Brown 	fclose(fp);
1652c98d5d94SLen Brown 	return 0;
1653103a8feaSLen Brown }
1654103a8feaSLen Brown 
1655103a8feaSLen Brown void re_initialize(void)
1656103a8feaSLen Brown {
1657c98d5d94SLen Brown 	free_all_buffers();
1658c98d5d94SLen Brown 	setup_all_buffers();
1659c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1660103a8feaSLen Brown }
1661103a8feaSLen Brown 
1662c98d5d94SLen Brown 
1663103a8feaSLen Brown /*
1664c98d5d94SLen Brown  * count_cpus()
1665c98d5d94SLen Brown  * remember the last one seen, it will be the max
1666103a8feaSLen Brown  */
1667c98d5d94SLen Brown int count_cpus(int cpu)
1668103a8feaSLen Brown {
1669c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
1670c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
1671103a8feaSLen Brown 
1672c98d5d94SLen Brown 	topo.num_cpus += 1;
1673c98d5d94SLen Brown 	return 0;
1674103a8feaSLen Brown }
1675c98d5d94SLen Brown int mark_cpu_present(int cpu)
1676c98d5d94SLen Brown {
1677c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
167815aaa346SLen Brown 	return 0;
1679103a8feaSLen Brown }
1680103a8feaSLen Brown 
1681103a8feaSLen Brown void turbostat_loop()
1682103a8feaSLen Brown {
1683c98d5d94SLen Brown 	int retval;
1684e52966c0SLen Brown 	int restarted = 0;
1685c98d5d94SLen Brown 
1686103a8feaSLen Brown restart:
1687e52966c0SLen Brown 	restarted++;
1688e52966c0SLen Brown 
1689c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1690d91bb17cSLen Brown 	if (retval < -1) {
1691d91bb17cSLen Brown 		exit(retval);
1692d91bb17cSLen Brown 	} else if (retval == -1) {
1693e52966c0SLen Brown 		if (restarted > 1) {
1694e52966c0SLen Brown 			exit(retval);
1695e52966c0SLen Brown 		}
1696c98d5d94SLen Brown 		re_initialize();
1697c98d5d94SLen Brown 		goto restart;
1698c98d5d94SLen Brown 	}
1699e52966c0SLen Brown 	restarted = 0;
1700103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
1701103a8feaSLen Brown 
1702103a8feaSLen Brown 	while (1) {
1703c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
1704103a8feaSLen Brown 			re_initialize();
1705103a8feaSLen Brown 			goto restart;
1706103a8feaSLen Brown 		}
1707103a8feaSLen Brown 		sleep(interval_sec);
1708c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
1709d91bb17cSLen Brown 		if (retval < -1) {
1710d91bb17cSLen Brown 			exit(retval);
1711d91bb17cSLen Brown 		} else if (retval == -1) {
171215aaa346SLen Brown 			re_initialize();
171315aaa346SLen Brown 			goto restart;
171415aaa346SLen Brown 		}
1715103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
1716103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
1717c98d5d94SLen Brown 		for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1718c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
1719c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
1720c98d5d94SLen Brown 		flush_stdout();
172115aaa346SLen Brown 		sleep(interval_sec);
1722c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1723d91bb17cSLen Brown 		if (retval < -1) {
1724d91bb17cSLen Brown 			exit(retval);
1725d91bb17cSLen Brown 		} else if (retval == -1) {
1726103a8feaSLen Brown 			re_initialize();
1727103a8feaSLen Brown 			goto restart;
1728103a8feaSLen Brown 		}
1729103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
1730103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
1731c98d5d94SLen Brown 		for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1732c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
1733c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
1734c98d5d94SLen Brown 		flush_stdout();
1735103a8feaSLen Brown 	}
1736103a8feaSLen Brown }
1737103a8feaSLen Brown 
1738103a8feaSLen Brown void check_dev_msr()
1739103a8feaSLen Brown {
1740103a8feaSLen Brown 	struct stat sb;
17417ce7d5deSPrarit Bhargava 	char pathname[32];
1742103a8feaSLen Brown 
17437ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
17447ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
1745a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1746d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1747103a8feaSLen Brown }
1748103a8feaSLen Brown 
174998481e79SLen Brown void check_permissions()
1750103a8feaSLen Brown {
175198481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
175298481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
175398481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
175498481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
175598481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
175698481e79SLen Brown 	int do_exit = 0;
17577ce7d5deSPrarit Bhargava 	char pathname[32];
175898481e79SLen Brown 
175998481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
176098481e79SLen Brown 	cap_header->pid = getpid();
176198481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
176298481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
176398481e79SLen Brown 		err(-6, "capget(2) failed");
176498481e79SLen Brown 
176598481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
176698481e79SLen Brown 		do_exit++;
176798481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
176898481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
176998481e79SLen Brown 	}
177098481e79SLen Brown 
177198481e79SLen Brown 	/* test file permissions */
17727ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
17737ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
177498481e79SLen Brown 		do_exit++;
177598481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
177698481e79SLen Brown 	}
177798481e79SLen Brown 
177898481e79SLen Brown 	/* if all else fails, thell them to be root */
177998481e79SLen Brown 	if (do_exit)
1780b2c95d90SJosh Triplett 		if (getuid() != 0)
1781d7899447SLen Brown 			warnx("... or simply run as root");
178298481e79SLen Brown 
178398481e79SLen Brown 	if (do_exit)
178498481e79SLen Brown 		exit(-6);
1785103a8feaSLen Brown }
1786103a8feaSLen Brown 
1787d7899447SLen Brown /*
1788d7899447SLen Brown  * NHM adds support for additional MSRs:
1789d7899447SLen Brown  *
1790d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
1791d7899447SLen Brown  *
1792d7899447SLen Brown  * MSR_NHM_PLATFORM_INFO           0x000000ce
1793d7899447SLen Brown  * MSR_NHM_SNB_PKG_CST_CFG_CTL     0x000000e2
1794d7899447SLen Brown  *
1795d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
1796d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
1797d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
1798d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
1799d7899447SLen Brown  *
1800ee7e38e3SLen Brown  * Side effect:
1801ee7e38e3SLen Brown  * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
1802d7899447SLen Brown  */
1803ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
1804103a8feaSLen Brown {
1805ee7e38e3SLen Brown 	unsigned long long msr;
1806ee7e38e3SLen Brown 	int *pkg_cstate_limits;
1807ee7e38e3SLen Brown 
1808103a8feaSLen Brown 	if (!genuine_intel)
1809103a8feaSLen Brown 		return 0;
1810103a8feaSLen Brown 
1811103a8feaSLen Brown 	if (family != 6)
1812103a8feaSLen Brown 		return 0;
1813103a8feaSLen Brown 
1814103a8feaSLen Brown 	switch (model) {
1815103a8feaSLen Brown 	case 0x1A:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1816103a8feaSLen Brown 	case 0x1E:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1817103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
1818103a8feaSLen Brown 	case 0x25:	/* Westmere Client - Clarkdale, Arrandale */
1819103a8feaSLen Brown 	case 0x2C:	/* Westmere EP - Gulftown */
1820ee7e38e3SLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
1821ee7e38e3SLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
1822ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
1823ee7e38e3SLen Brown 		break;
1824103a8feaSLen Brown 	case 0x2A:	/* SNB */
1825103a8feaSLen Brown 	case 0x2D:	/* SNB Xeon */
1826553575f1SLen Brown 	case 0x3A:	/* IVB */
18271300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
1828ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
1829ee7e38e3SLen Brown 		break;
183070b43400SLen Brown 	case 0x3C:	/* HSW */
1831e6f9bb3cSLen Brown 	case 0x3F:	/* HSX */
183270b43400SLen Brown 	case 0x45:	/* HSW */
1833149c2319SLen Brown 	case 0x46:	/* HSW */
18344e8e863fSLen Brown 	case 0x3D:	/* BDW */
183548a0631cSLen Brown 	case 0x47:	/* BDW */
18364e8e863fSLen Brown 	case 0x4F:	/* BDX */
18374e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
18380b2bb692SLen Brown 	case 0x4E:	/* SKL */
18390b2bb692SLen Brown 	case 0x5E:	/* SKL */
1840ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
1841ee7e38e3SLen Brown 		break;
1842ee7e38e3SLen Brown 	case 0x37:	/* BYT */
1843ee7e38e3SLen Brown 	case 0x4D:	/* AVN */
1844ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
1845ee7e38e3SLen Brown 		break;
1846ee7e38e3SLen Brown 	case 0x4C:	/* AMT */
1847ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
1848ee7e38e3SLen Brown 		break;
1849ee7e38e3SLen Brown 	case 0x57:	/* PHI */
1850ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
1851ee7e38e3SLen Brown 		break;
1852103a8feaSLen Brown 	default:
1853103a8feaSLen Brown 		return 0;
1854103a8feaSLen Brown 	}
18557ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1856ee7e38e3SLen Brown 
1857e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
1858ee7e38e3SLen Brown 
1859ee7e38e3SLen Brown 	return 1;
1860103a8feaSLen Brown }
1861d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1862d7899447SLen Brown {
1863d7899447SLen Brown 	switch (model) {
1864d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
1865d7899447SLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
1866d7899447SLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
1867d7899447SLen Brown 		return 0;
1868d7899447SLen Brown 	default:
1869d7899447SLen Brown 		return 1;
1870d7899447SLen Brown 	}
1871d7899447SLen Brown }
18726574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
18736574a5d5SLen Brown {
18746574a5d5SLen Brown 	if (!genuine_intel)
18756574a5d5SLen Brown 		return 0;
18766574a5d5SLen Brown 
18776574a5d5SLen Brown 	if (family != 6)
18786574a5d5SLen Brown 		return 0;
18796574a5d5SLen Brown 
18806574a5d5SLen Brown 	switch (model) {
18816574a5d5SLen Brown 	case 0x3E:	/* IVB Xeon */
1882fcd17211SLen Brown 	case 0x3F:	/* HSW Xeon */
18836574a5d5SLen Brown 		return 1;
18846574a5d5SLen Brown 	default:
18856574a5d5SLen Brown 		return 0;
18866574a5d5SLen Brown 	}
18876574a5d5SLen Brown }
1888fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1889fcd17211SLen Brown {
1890fcd17211SLen Brown 	if (!genuine_intel)
1891fcd17211SLen Brown 		return 0;
1892fcd17211SLen Brown 
1893fcd17211SLen Brown 	if (family != 6)
1894fcd17211SLen Brown 		return 0;
1895fcd17211SLen Brown 
1896fcd17211SLen Brown 	switch (model) {
1897fcd17211SLen Brown 	case 0x3F:	/* HSW Xeon */
1898fcd17211SLen Brown 		return 1;
1899fcd17211SLen Brown 	default:
1900fcd17211SLen Brown 		return 0;
1901fcd17211SLen Brown 	}
1902fcd17211SLen Brown }
1903fcd17211SLen Brown 
1904fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1905fb5d4327SDasaratharaman Chandramouli {
1906fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
1907fb5d4327SDasaratharaman Chandramouli 		return 0;
1908fb5d4327SDasaratharaman Chandramouli 
1909fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
1910fb5d4327SDasaratharaman Chandramouli 		return 0;
1911fb5d4327SDasaratharaman Chandramouli 
1912fb5d4327SDasaratharaman Chandramouli 	switch (model) {
1913fb5d4327SDasaratharaman Chandramouli 	case 0x57:	/* Knights Landing */
1914fb5d4327SDasaratharaman Chandramouli 		return 1;
1915fb5d4327SDasaratharaman Chandramouli 	default:
1916fb5d4327SDasaratharaman Chandramouli 		return 0;
1917fb5d4327SDasaratharaman Chandramouli 	}
1918fb5d4327SDasaratharaman Chandramouli }
19196fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
19206fb3143bSLen Brown {
19216fb3143bSLen Brown 	if (!genuine_intel)
19226fb3143bSLen Brown 		return 0;
19236fb3143bSLen Brown 
19246fb3143bSLen Brown 	if (family != 6)
19256fb3143bSLen Brown 		return 0;
19266fb3143bSLen Brown 
19276fb3143bSLen Brown 	switch (model) {
19286fb3143bSLen Brown 	case 0x3A:	/* IVB */
19296fb3143bSLen Brown 	case 0x3E:	/* IVB Xeon */
19306fb3143bSLen Brown 
19316fb3143bSLen Brown 	case 0x3C:	/* HSW */
19326fb3143bSLen Brown 	case 0x3F:	/* HSX */
19336fb3143bSLen Brown 	case 0x45:	/* HSW */
19346fb3143bSLen Brown 	case 0x46:	/* HSW */
19356fb3143bSLen Brown 	case 0x3D:	/* BDW */
19366fb3143bSLen Brown 	case 0x47:	/* BDW */
19376fb3143bSLen Brown 	case 0x4F:	/* BDX */
19386fb3143bSLen Brown 	case 0x56:	/* BDX-DE */
19396fb3143bSLen Brown 	case 0x4E:	/* SKL */
19406fb3143bSLen Brown 	case 0x5E:	/* SKL */
19416fb3143bSLen Brown 
19426fb3143bSLen Brown 	case 0x57:	/* Knights Landing */
19436fb3143bSLen Brown 		return 1;
19446fb3143bSLen Brown 	default:
19456fb3143bSLen Brown 		return 0;
19466fb3143bSLen Brown 	}
19476fb3143bSLen Brown }
19486fb3143bSLen Brown 
1949fcd17211SLen Brown static void
1950fcd17211SLen Brown dump_cstate_pstate_config_info(family, model)
1951fcd17211SLen Brown {
1952fcd17211SLen Brown 	if (!do_nhm_platform_info)
1953fcd17211SLen Brown 		return;
1954fcd17211SLen Brown 
1955fcd17211SLen Brown 	dump_nhm_platform_info();
1956fcd17211SLen Brown 
1957fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
1958fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
1959fcd17211SLen Brown 
1960fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
1961fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
1962fcd17211SLen Brown 
1963fcd17211SLen Brown 	if (has_nhm_turbo_ratio_limit(family, model))
1964fcd17211SLen Brown 		dump_nhm_turbo_ratio_limits();
1965fcd17211SLen Brown 
1966fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
1967fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
1968fb5d4327SDasaratharaman Chandramouli 
19696fb3143bSLen Brown 	if (has_config_tdp(family, model))
19706fb3143bSLen Brown 		dump_config_tdp();
19716fb3143bSLen Brown 
1972fcd17211SLen Brown 	dump_nhm_cst_cfg();
1973fcd17211SLen Brown }
1974fcd17211SLen Brown 
19756574a5d5SLen Brown 
1976889facbeSLen Brown /*
1977889facbeSLen Brown  * print_epb()
1978889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
1979889facbeSLen Brown  */
1980889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1981889facbeSLen Brown {
1982889facbeSLen Brown 	unsigned long long msr;
1983889facbeSLen Brown 	char *epb_string;
1984889facbeSLen Brown 	int cpu;
1985889facbeSLen Brown 
1986889facbeSLen Brown 	if (!has_epb)
1987889facbeSLen Brown 		return 0;
1988889facbeSLen Brown 
1989889facbeSLen Brown 	cpu = t->cpu_id;
1990889facbeSLen Brown 
1991889facbeSLen Brown 	/* EPB is per-package */
1992889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1993889facbeSLen Brown 		return 0;
1994889facbeSLen Brown 
1995889facbeSLen Brown 	if (cpu_migrate(cpu)) {
1996889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1997889facbeSLen Brown 		return -1;
1998889facbeSLen Brown 	}
1999889facbeSLen Brown 
2000889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2001889facbeSLen Brown 		return 0;
2002889facbeSLen Brown 
2003e9be7dd6SLen Brown 	switch (msr & 0xF) {
2004889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
2005889facbeSLen Brown 		epb_string = "performance";
2006889facbeSLen Brown 		break;
2007889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
2008889facbeSLen Brown 		epb_string = "balanced";
2009889facbeSLen Brown 		break;
2010889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
2011889facbeSLen Brown 		epb_string = "powersave";
2012889facbeSLen Brown 		break;
2013889facbeSLen Brown 	default:
2014889facbeSLen Brown 		epb_string = "custom";
2015889facbeSLen Brown 		break;
2016889facbeSLen Brown 	}
2017889facbeSLen Brown 	fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2018889facbeSLen Brown 
2019889facbeSLen Brown 	return 0;
2020889facbeSLen Brown }
2021889facbeSLen Brown 
20223a9a941dSLen Brown /*
20233a9a941dSLen Brown  * print_perf_limit()
20243a9a941dSLen Brown  */
20253a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
20263a9a941dSLen Brown {
20273a9a941dSLen Brown 	unsigned long long msr;
20283a9a941dSLen Brown 	int cpu;
20293a9a941dSLen Brown 
20303a9a941dSLen Brown 	cpu = t->cpu_id;
20313a9a941dSLen Brown 
20323a9a941dSLen Brown 	/* per-package */
20333a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
20343a9a941dSLen Brown 		return 0;
20353a9a941dSLen Brown 
20363a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
20373a9a941dSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
20383a9a941dSLen Brown 		return -1;
20393a9a941dSLen Brown 	}
20403a9a941dSLen Brown 
20413a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
20423a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
20433a9a941dSLen Brown 		fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
20443a9a941dSLen Brown 		fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
2045e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
20463a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
2047e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
2048e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2049e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
2050e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
2051e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
2052e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
2053e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
2054e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
2055e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
2056e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
2057e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
2058e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
20593a9a941dSLen Brown 		fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
2060e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
20613a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
2062e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
2063e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2064e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
2065e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
2066e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
2067e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
2068e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
2069e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
2070e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
2071e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
2072e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
2073e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
20743a9a941dSLen Brown 
20753a9a941dSLen Brown 	}
20763a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
20773a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
20783a9a941dSLen Brown 		fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
20793a9a941dSLen Brown 		fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
20803a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
20813a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
20823a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
20833a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
20843a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
20853a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
20863a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
20873a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
20883a9a941dSLen Brown 		fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
20893a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
20903a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
20913a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
20923a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
20933a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
20943a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
20953a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
20963a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
20973a9a941dSLen Brown 	}
20983a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
20993a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
21003a9a941dSLen Brown 		fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
21013a9a941dSLen Brown 		fprintf(stderr, " (Active: %s%s%s%s%s%s)",
21023a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
21033a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
21043a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
21053a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
21063a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
21073a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
21083a9a941dSLen Brown 		fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
21093a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
21103a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
21113a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
21123a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
21133a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
21143a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
21153a9a941dSLen Brown 	}
21163a9a941dSLen Brown 	return 0;
21173a9a941dSLen Brown }
21183a9a941dSLen Brown 
2119889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
2120889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
2121889facbeSLen Brown 
2122144b44b1SLen Brown double get_tdp(model)
2123144b44b1SLen Brown {
2124144b44b1SLen Brown 	unsigned long long msr;
2125144b44b1SLen Brown 
2126144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
21277ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
2128144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2129144b44b1SLen Brown 
2130144b44b1SLen Brown 	switch (model) {
2131144b44b1SLen Brown 	case 0x37:
2132144b44b1SLen Brown 	case 0x4D:
2133144b44b1SLen Brown 		return 30.0;
2134144b44b1SLen Brown 	default:
2135144b44b1SLen Brown 		return 135.0;
2136144b44b1SLen Brown 	}
2137144b44b1SLen Brown }
2138144b44b1SLen Brown 
213940ee8e3bSAndrey Semin /*
214040ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
214140ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
214240ee8e3bSAndrey Semin  */
214340ee8e3bSAndrey Semin static double
214440ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
214540ee8e3bSAndrey Semin {
214640ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
214740ee8e3bSAndrey Semin 
214840ee8e3bSAndrey Semin 	switch (model) {
214940ee8e3bSAndrey Semin 	case 0x3F:	/* HSX */
215040ee8e3bSAndrey Semin 	case 0x4F:	/* BDX */
215140ee8e3bSAndrey Semin 	case 0x56:	/* BDX-DE */
2152fb5d4327SDasaratharaman Chandramouli 	case 0x57:	/* KNL */
215340ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
215440ee8e3bSAndrey Semin 	default:
215540ee8e3bSAndrey Semin 		return (rapl_energy_units);
215640ee8e3bSAndrey Semin 	}
215740ee8e3bSAndrey Semin }
215840ee8e3bSAndrey Semin 
2159144b44b1SLen Brown 
2160889facbeSLen Brown /*
2161889facbeSLen Brown  * rapl_probe()
2162889facbeSLen Brown  *
2163144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2164889facbeSLen Brown  */
2165889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
2166889facbeSLen Brown {
2167889facbeSLen Brown 	unsigned long long msr;
2168144b44b1SLen Brown 	unsigned int time_unit;
2169889facbeSLen Brown 	double tdp;
2170889facbeSLen Brown 
2171889facbeSLen Brown 	if (!genuine_intel)
2172889facbeSLen Brown 		return;
2173889facbeSLen Brown 
2174889facbeSLen Brown 	if (family != 6)
2175889facbeSLen Brown 		return;
2176889facbeSLen Brown 
2177889facbeSLen Brown 	switch (model) {
2178889facbeSLen Brown 	case 0x2A:
2179889facbeSLen Brown 	case 0x3A:
218070b43400SLen Brown 	case 0x3C:	/* HSW */
218170b43400SLen Brown 	case 0x45:	/* HSW */
2182149c2319SLen Brown 	case 0x46:	/* HSW */
21834e8e863fSLen Brown 	case 0x3D:	/* BDW */
218448a0631cSLen Brown 	case 0x47:	/* BDW */
2185144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
2186889facbeSLen Brown 		break;
21870b2bb692SLen Brown 	case 0x4E:	/* SKL */
21880b2bb692SLen Brown 	case 0x5E:	/* SKL */
21890b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
21900b2bb692SLen Brown 		break;
2191e6f9bb3cSLen Brown 	case 0x3F:	/* HSX */
21924e8e863fSLen Brown 	case 0x4F:	/* BDX */
21934e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
2194fb5d4327SDasaratharaman Chandramouli 	case 0x57:	/* KNL */
21950b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2196e6f9bb3cSLen Brown 		break;
2197889facbeSLen Brown 	case 0x2D:
2198889facbeSLen Brown 	case 0x3E:
21990b2bb692SLen 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;
2200144b44b1SLen Brown 		break;
2201144b44b1SLen Brown 	case 0x37:	/* BYT */
2202144b44b1SLen Brown 	case 0x4D:	/* AVN */
2203144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES ;
2204889facbeSLen Brown 		break;
2205889facbeSLen Brown 	default:
2206889facbeSLen Brown 		return;
2207889facbeSLen Brown 	}
2208889facbeSLen Brown 
2209889facbeSLen Brown 	/* units on package 0, verify later other packages match */
22107ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
2211889facbeSLen Brown 		return;
2212889facbeSLen Brown 
2213889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
2214144b44b1SLen Brown 	if (model == 0x37)
2215144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2216144b44b1SLen Brown 	else
2217889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
2218889facbeSLen Brown 
221940ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
222040ee8e3bSAndrey Semin 
2221144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
2222144b44b1SLen Brown 	if (time_unit == 0)
2223144b44b1SLen Brown 		time_unit = 0xA;
2224889facbeSLen Brown 
2225144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
2226144b44b1SLen Brown 
2227144b44b1SLen Brown 	tdp = get_tdp(model);
2228889facbeSLen Brown 
2229889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
2230d8af6f5fSLen Brown 	if (debug)
2231144b44b1SLen Brown 		fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
2232889facbeSLen Brown 
2233889facbeSLen Brown 	return;
2234889facbeSLen Brown }
2235889facbeSLen Brown 
22363a9a941dSLen Brown void perf_limit_reasons_probe(family, model)
22373a9a941dSLen Brown {
22383a9a941dSLen Brown 	if (!genuine_intel)
22393a9a941dSLen Brown 		return;
22403a9a941dSLen Brown 
22413a9a941dSLen Brown 	if (family != 6)
22423a9a941dSLen Brown 		return;
22433a9a941dSLen Brown 
22443a9a941dSLen Brown 	switch (model) {
22453a9a941dSLen Brown 	case 0x3C:	/* HSW */
22463a9a941dSLen Brown 	case 0x45:	/* HSW */
22473a9a941dSLen Brown 	case 0x46:	/* HSW */
22483a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
22493a9a941dSLen Brown 	case 0x3F:	/* HSX */
22503a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
22513a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
22523a9a941dSLen Brown 	default:
22533a9a941dSLen Brown 		return;
22543a9a941dSLen Brown 	}
22553a9a941dSLen Brown }
22563a9a941dSLen Brown 
2257889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2258889facbeSLen Brown {
2259889facbeSLen Brown 	unsigned long long msr;
2260889facbeSLen Brown 	unsigned int dts;
2261889facbeSLen Brown 	int cpu;
2262889facbeSLen Brown 
2263889facbeSLen Brown 	if (!(do_dts || do_ptm))
2264889facbeSLen Brown 		return 0;
2265889facbeSLen Brown 
2266889facbeSLen Brown 	cpu = t->cpu_id;
2267889facbeSLen Brown 
2268889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
2269889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2270889facbeSLen Brown 		return 0;
2271889facbeSLen Brown 
2272889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2273889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2274889facbeSLen Brown 		return -1;
2275889facbeSLen Brown 	}
2276889facbeSLen Brown 
2277889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2278889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2279889facbeSLen Brown 			return 0;
2280889facbeSLen Brown 
2281889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2282889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2283889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
2284889facbeSLen Brown 
2285889facbeSLen Brown #ifdef	THERM_DEBUG
2286889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2287889facbeSLen Brown 			return 0;
2288889facbeSLen Brown 
2289889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2290889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
2291889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2292889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2293889facbeSLen Brown #endif
2294889facbeSLen Brown 	}
2295889facbeSLen Brown 
2296889facbeSLen Brown 
2297889facbeSLen Brown 	if (do_dts) {
2298889facbeSLen Brown 		unsigned int resolution;
2299889facbeSLen Brown 
2300889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2301889facbeSLen Brown 			return 0;
2302889facbeSLen Brown 
2303889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2304889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
2305889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2306889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
2307889facbeSLen Brown 
2308889facbeSLen Brown #ifdef THERM_DEBUG
2309889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2310889facbeSLen Brown 			return 0;
2311889facbeSLen Brown 
2312889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2313889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
2314889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2315889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2316889facbeSLen Brown #endif
2317889facbeSLen Brown 	}
2318889facbeSLen Brown 
2319889facbeSLen Brown 	return 0;
2320889facbeSLen Brown }
2321889facbeSLen Brown 
2322889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2323889facbeSLen Brown {
2324889facbeSLen Brown 	fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2325889facbeSLen Brown 		cpu, label,
2326889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
2327889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
2328889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2329889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
2330889facbeSLen Brown 
2331889facbeSLen Brown 	return;
2332889facbeSLen Brown }
2333889facbeSLen Brown 
2334889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2335889facbeSLen Brown {
2336889facbeSLen Brown 	unsigned long long msr;
2337889facbeSLen Brown 	int cpu;
2338889facbeSLen Brown 
2339889facbeSLen Brown 	if (!do_rapl)
2340889facbeSLen Brown 		return 0;
2341889facbeSLen Brown 
2342889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
2343889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2344889facbeSLen Brown 		return 0;
2345889facbeSLen Brown 
2346889facbeSLen Brown 	cpu = t->cpu_id;
2347889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2348889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2349889facbeSLen Brown 		return -1;
2350889facbeSLen Brown 	}
2351889facbeSLen Brown 
2352889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2353889facbeSLen Brown 		return -1;
2354889facbeSLen Brown 
2355d8af6f5fSLen Brown 	if (debug) {
2356889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2357889facbeSLen Brown 			"(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
2358144b44b1SLen Brown 			rapl_power_units, rapl_energy_units, rapl_time_units);
2359889facbeSLen Brown 	}
2360144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
2361144b44b1SLen Brown 
2362889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2363889facbeSLen Brown                 	return -5;
2364889facbeSLen Brown 
2365889facbeSLen Brown 
2366889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2367889facbeSLen Brown 			cpu, msr,
2368889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2369889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2370889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2371889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2372889facbeSLen Brown 
2373144b44b1SLen Brown 	}
2374144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
2375144b44b1SLen Brown 
2376889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2377889facbeSLen Brown 			return -9;
2378889facbeSLen Brown 
2379889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2380889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "": "UN");
2381889facbeSLen Brown 
2382889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
2383889facbeSLen Brown 		fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2384889facbeSLen Brown 			cpu,
2385889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
2386889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
2387889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2388889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
2389889facbeSLen Brown 	}
2390889facbeSLen Brown 
23910b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
2392889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2393889facbeSLen Brown                 	return -6;
2394889facbeSLen Brown 
2395889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2396889facbeSLen Brown 			cpu, msr,
2397889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2398889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2399889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2400889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
24010b2bb692SLen Brown 	}
24020b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
2403889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2404889facbeSLen Brown 			return -9;
2405889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2406889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "": "UN");
2407889facbeSLen Brown 
2408889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
2409889facbeSLen Brown 	}
2410144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
2411d8af6f5fSLen Brown 		if (debug) {
2412889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2413889facbeSLen Brown 				return -7;
2414889facbeSLen Brown 
2415889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
2416144b44b1SLen Brown 		}
2417144b44b1SLen Brown 	}
2418144b44b1SLen Brown 	if (do_rapl & RAPL_CORES) {
2419d8af6f5fSLen Brown 		if (debug) {
2420889facbeSLen Brown 
2421889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2422889facbeSLen Brown 				return -9;
2423889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2424889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
2425889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "Cores Limit");
2426889facbeSLen Brown 		}
2427889facbeSLen Brown 	}
2428889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
2429d8af6f5fSLen Brown 		if (debug) {
2430889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2431889facbeSLen Brown 				return -8;
2432889facbeSLen Brown 
2433889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2434889facbeSLen Brown 
2435889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2436889facbeSLen Brown 				return -9;
2437889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2438889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
2439889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "GFX Limit");
2440889facbeSLen Brown 		}
2441889facbeSLen Brown 	}
2442889facbeSLen Brown 	return 0;
2443889facbeSLen Brown }
2444889facbeSLen Brown 
2445d7899447SLen Brown /*
2446d7899447SLen Brown  * SNB adds support for additional MSRs:
2447d7899447SLen Brown  *
2448d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
2449d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
2450d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
2451d7899447SLen Brown  */
2452103a8feaSLen Brown 
2453d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
2454103a8feaSLen Brown {
2455103a8feaSLen Brown 	if (!genuine_intel)
2456103a8feaSLen Brown 		return 0;
2457103a8feaSLen Brown 
2458103a8feaSLen Brown 	switch (model) {
2459103a8feaSLen Brown 	case 0x2A:
2460103a8feaSLen Brown 	case 0x2D:
2461650a37f3SLen Brown 	case 0x3A:	/* IVB */
24621300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
246370b43400SLen Brown 	case 0x3C:	/* HSW */
246470b43400SLen Brown 	case 0x3F:	/* HSW */
246570b43400SLen Brown 	case 0x45:	/* HSW */
2466149c2319SLen Brown 	case 0x46:	/* HSW */
24674e8e863fSLen Brown 	case 0x3D:	/* BDW */
246848a0631cSLen Brown 	case 0x47:	/* BDW */
24694e8e863fSLen Brown 	case 0x4F:	/* BDX */
24704e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
24710b2bb692SLen Brown 	case 0x4E:	/* SKL */
24720b2bb692SLen Brown 	case 0x5E:	/* SKL */
2473103a8feaSLen Brown 		return 1;
2474103a8feaSLen Brown 	}
2475103a8feaSLen Brown 	return 0;
2476103a8feaSLen Brown }
2477103a8feaSLen Brown 
2478d7899447SLen Brown /*
2479d7899447SLen Brown  * HSW adds support for additional MSRs:
2480d7899447SLen Brown  *
2481d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY            0x00000630
2482d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY            0x00000631
2483d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY           0x00000632
2484d7899447SLen Brown  */
2485d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
2486ca58710fSKristen Carlson Accardi {
2487ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
2488ca58710fSKristen Carlson Accardi 		return 0;
2489ca58710fSKristen Carlson Accardi 
2490ca58710fSKristen Carlson Accardi 	switch (model) {
24914e8e863fSLen Brown 	case 0x45:	/* HSW */
24924e8e863fSLen Brown 	case 0x3D:	/* BDW */
24930b2bb692SLen Brown 	case 0x4E:	/* SKL */
24940b2bb692SLen Brown 	case 0x5E:	/* SKL */
2495ca58710fSKristen Carlson Accardi 		return 1;
2496ca58710fSKristen Carlson Accardi 	}
2497ca58710fSKristen Carlson Accardi 	return 0;
2498ca58710fSKristen Carlson Accardi }
2499ca58710fSKristen Carlson Accardi 
25000b2bb692SLen Brown /*
25010b2bb692SLen Brown  * SKL adds support for additional MSRS:
25020b2bb692SLen Brown  *
25030b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
25040b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
25050b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
25060b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
25070b2bb692SLen Brown  */
25080b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
25090b2bb692SLen Brown {
25100b2bb692SLen Brown 	if (!genuine_intel)
25110b2bb692SLen Brown 		return 0;
25120b2bb692SLen Brown 
25130b2bb692SLen Brown 	switch (model) {
25140b2bb692SLen Brown 	case 0x4E:	/* SKL */
25150b2bb692SLen Brown 	case 0x5E:	/* SKL */
25160b2bb692SLen Brown 		return 1;
25170b2bb692SLen Brown 	}
25180b2bb692SLen Brown 	return 0;
25190b2bb692SLen Brown }
25200b2bb692SLen Brown 
25210b2bb692SLen Brown 
2522ca58710fSKristen Carlson Accardi 
2523144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
2524144b44b1SLen Brown {
2525144b44b1SLen Brown 	if (!genuine_intel)
2526144b44b1SLen Brown 		return 0;
2527144b44b1SLen Brown 	switch (model) {
2528144b44b1SLen Brown 	case 0x37:	/* BYT */
2529144b44b1SLen Brown 	case 0x4D:	/* AVN */
2530144b44b1SLen Brown 		return 1;
2531144b44b1SLen Brown 	}
2532144b44b1SLen Brown 	return 0;
2533144b44b1SLen Brown }
2534144b44b1SLen Brown 
2535fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
2536fb5d4327SDasaratharaman Chandramouli {
2537fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
2538fb5d4327SDasaratharaman Chandramouli 		return 0;
2539fb5d4327SDasaratharaman Chandramouli 	switch (model) {
2540fb5d4327SDasaratharaman Chandramouli 	case 0x57:	/* KNL */
2541fb5d4327SDasaratharaman Chandramouli 		return 1;
2542fb5d4327SDasaratharaman Chandramouli 	}
2543fb5d4327SDasaratharaman Chandramouli 	return 0;
2544fb5d4327SDasaratharaman Chandramouli }
2545fb5d4327SDasaratharaman Chandramouli 
2546144b44b1SLen Brown #define SLM_BCLK_FREQS 5
2547144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2548144b44b1SLen Brown 
2549144b44b1SLen Brown double slm_bclk(void)
2550144b44b1SLen Brown {
2551144b44b1SLen Brown 	unsigned long long msr = 3;
2552144b44b1SLen Brown 	unsigned int i;
2553144b44b1SLen Brown 	double freq;
2554144b44b1SLen Brown 
25557ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
2556144b44b1SLen Brown 		fprintf(stderr, "SLM BCLK: unknown\n");
2557144b44b1SLen Brown 
2558144b44b1SLen Brown 	i = msr & 0xf;
2559144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
2560144b44b1SLen Brown 		fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2561144b44b1SLen Brown 		msr = 3;
2562144b44b1SLen Brown 	}
2563144b44b1SLen Brown 	freq = slm_freq_table[i];
2564144b44b1SLen Brown 
2565144b44b1SLen Brown 	fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2566144b44b1SLen Brown 
2567144b44b1SLen Brown 	return freq;
2568144b44b1SLen Brown }
2569144b44b1SLen Brown 
2570103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
2571103a8feaSLen Brown {
2572d7899447SLen Brown 	if (has_snb_msrs(family, model))
2573103a8feaSLen Brown 		return 100.00;
2574144b44b1SLen Brown 	else if (is_slm(family, model))
2575144b44b1SLen Brown 		return slm_bclk();
2576103a8feaSLen Brown 	else
2577103a8feaSLen Brown 		return 133.33;
2578103a8feaSLen Brown }
2579103a8feaSLen Brown 
2580889facbeSLen Brown /*
2581889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2582889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
2583889facbeSLen Brown  * This is usually equal to tjMax.
2584889facbeSLen Brown  *
2585889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
2586889facbeSLen Brown  * but also allow cmdline over-ride with -T.
2587889facbeSLen Brown  *
2588889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
2589889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
2590889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2591889facbeSLen Brown  */
2592889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2593889facbeSLen Brown {
2594889facbeSLen Brown 	unsigned long long msr;
2595889facbeSLen Brown 	unsigned int target_c_local;
2596889facbeSLen Brown 	int cpu;
2597889facbeSLen Brown 
2598889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
2599889facbeSLen Brown 	if (!(do_dts || do_ptm))
2600889facbeSLen Brown 		return 0;
2601889facbeSLen Brown 
2602889facbeSLen Brown 	/* this is a per-package concept */
2603889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2604889facbeSLen Brown 		return 0;
2605889facbeSLen Brown 
2606889facbeSLen Brown 	cpu = t->cpu_id;
2607889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2608889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2609889facbeSLen Brown 		return -1;
2610889facbeSLen Brown 	}
2611889facbeSLen Brown 
2612889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
2613889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
2614889facbeSLen Brown 		fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2615889facbeSLen Brown 			cpu, tcc_activation_temp);
2616889facbeSLen Brown 		return 0;
2617889facbeSLen Brown 	}
2618889facbeSLen Brown 
2619889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
2620d7899447SLen Brown 	if (!do_nhm_platform_info)
2621889facbeSLen Brown 		goto guess;
2622889facbeSLen Brown 
26237ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
2624889facbeSLen Brown 		goto guess;
2625889facbeSLen Brown 
26263482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
2627889facbeSLen Brown 
2628d8af6f5fSLen Brown 	if (debug)
2629889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2630889facbeSLen Brown 			cpu, msr, target_c_local);
2631889facbeSLen Brown 
26323482124aSJean Delvare 	if (!target_c_local)
2633889facbeSLen Brown 		goto guess;
2634889facbeSLen Brown 
2635889facbeSLen Brown 	tcc_activation_temp = target_c_local;
2636889facbeSLen Brown 
2637889facbeSLen Brown 	return 0;
2638889facbeSLen Brown 
2639889facbeSLen Brown guess:
2640889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
2641889facbeSLen Brown 	fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2642889facbeSLen Brown 		cpu, tcc_activation_temp);
2643889facbeSLen Brown 
2644889facbeSLen Brown 	return 0;
2645889facbeSLen Brown }
2646fcd17211SLen Brown void process_cpuid()
2647103a8feaSLen Brown {
2648103a8feaSLen Brown 	unsigned int eax, ebx, ecx, edx, max_level;
2649103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
2650103a8feaSLen Brown 
2651103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
2652103a8feaSLen Brown 
26532b92865eSJosh Triplett 	__get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2654103a8feaSLen Brown 
2655103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2656103a8feaSLen Brown 		genuine_intel = 1;
2657103a8feaSLen Brown 
2658d8af6f5fSLen Brown 	if (debug)
2659889facbeSLen Brown 		fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2660103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
2661103a8feaSLen Brown 
26622b92865eSJosh Triplett 	__get_cpuid(1, &fms, &ebx, &ecx, &edx);
2663103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
2664103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
2665103a8feaSLen Brown 	stepping = fms & 0xf;
2666103a8feaSLen Brown 	if (family == 6 || family == 0xf)
2667103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
2668103a8feaSLen Brown 
2669d8af6f5fSLen Brown 	if (debug)
2670103a8feaSLen Brown 		fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2671103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
2672103a8feaSLen Brown 
2673b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
2674b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
2675103a8feaSLen Brown 
2676103a8feaSLen Brown 	/*
2677103a8feaSLen Brown 	 * check max extended function levels of CPUID.
2678103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
2679103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
2680103a8feaSLen Brown 	 */
2681103a8feaSLen Brown 	ebx = ecx = edx = 0;
26822b92865eSJosh Triplett 	__get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
2683103a8feaSLen Brown 
2684d7899447SLen Brown 	if (max_level >= 0x80000007) {
2685103a8feaSLen Brown 
2686103a8feaSLen Brown 		/*
2687103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2688103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
2689103a8feaSLen Brown 		 */
26902b92865eSJosh Triplett 		__get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
26918209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
2692d7899447SLen Brown 	}
2693103a8feaSLen Brown 
2694103a8feaSLen Brown 	/*
2695103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2696103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
2697103a8feaSLen Brown 	 */
2698103a8feaSLen Brown 
26992b92865eSJosh Triplett 	__get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
27008209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
2701889facbeSLen Brown 	do_dts = eax & (1 << 0);
2702889facbeSLen Brown 	do_ptm = eax & (1 << 6);
2703889facbeSLen Brown 	has_epb = ecx & (1 << 3);
2704889facbeSLen Brown 
2705d8af6f5fSLen Brown 	if (debug)
2706a729617cSLen Brown 		fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2707a729617cSLen Brown 			has_aperf ? "" : "No ",
2708a729617cSLen Brown 			do_dts ? "" : "No ",
2709a729617cSLen Brown 			do_ptm ? "" : "No ",
2710a729617cSLen Brown 			has_epb ? "" : "No ");
2711103a8feaSLen Brown 
27128a5bdf41SLen Brown 	if (max_level > 0x15) {
27138a5bdf41SLen Brown 		unsigned int eax_crystal;
27148a5bdf41SLen Brown 		unsigned int ebx_tsc;
27158a5bdf41SLen Brown 
27168a5bdf41SLen Brown 		/*
27178a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
27188a5bdf41SLen Brown 		 */
27198a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
27208a5bdf41SLen Brown 		__get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
27218a5bdf41SLen Brown 
27228a5bdf41SLen Brown 		if (ebx_tsc != 0) {
27238a5bdf41SLen Brown 
27248a5bdf41SLen Brown 			if (debug && (ebx != 0))
27258a5bdf41SLen Brown 				fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
27268a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
27278a5bdf41SLen Brown 
27288a5bdf41SLen Brown 			if (crystal_hz == 0)
27298a5bdf41SLen Brown 				switch(model) {
27308a5bdf41SLen Brown 				case 0x4E:	/* SKL */
27318a5bdf41SLen Brown 				case 0x5E:	/* SKL */
27328a5bdf41SLen Brown 					crystal_hz = 24000000;	/* 24 MHz */
27338a5bdf41SLen Brown 					break;
27348a5bdf41SLen Brown 				default:
27358a5bdf41SLen Brown 					crystal_hz = 0;
27368a5bdf41SLen Brown 			}
27378a5bdf41SLen Brown 
27388a5bdf41SLen Brown 			if (crystal_hz) {
27398a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
27408a5bdf41SLen Brown 				if (debug)
27418a5bdf41SLen Brown 					fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
27428a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
27438a5bdf41SLen Brown 			}
27448a5bdf41SLen Brown 		}
27458a5bdf41SLen Brown 	}
27468a5bdf41SLen Brown 
2747ee7e38e3SLen Brown 	do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
2748d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
2749ee7e38e3SLen Brown 	do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2750ee7e38e3SLen Brown 	do_pc3 = (pkg_cstate_limit >= PCL__3);
2751ee7e38e3SLen Brown 	do_pc6 = (pkg_cstate_limit >= PCL__6);
2752ee7e38e3SLen Brown 	do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
2753d7899447SLen Brown 	do_c8_c9_c10 = has_hsw_msrs(family, model);
27540b2bb692SLen Brown 	do_skl_residency = has_skl_msrs(family, model);
2755144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
2756fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
2757103a8feaSLen Brown 	bclk = discover_bclk(family, model);
2758103a8feaSLen Brown 
2759889facbeSLen Brown 	rapl_probe(family, model);
27603a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
2761889facbeSLen Brown 
2762fcd17211SLen Brown 	if (debug)
2763fcd17211SLen Brown 		dump_cstate_pstate_config_info();
2764fcd17211SLen Brown 
2765889facbeSLen Brown 	return;
2766103a8feaSLen Brown }
2767103a8feaSLen Brown 
2768d8af6f5fSLen Brown void help()
2769103a8feaSLen Brown {
2770d8af6f5fSLen Brown 	fprintf(stderr,
2771d8af6f5fSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2772d8af6f5fSLen Brown 	"\n"
2773d8af6f5fSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
2774d8af6f5fSLen Brown 	"when COMMAND completes.\n"
2775d8af6f5fSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2776d8af6f5fSLen Brown 	"to print statistics, until interrupted.\n"
2777d8af6f5fSLen Brown 	"--debug	run in \"debug\" mode\n"
2778d8af6f5fSLen Brown 	"--interval sec	Override default 5-second measurement interval\n"
2779d8af6f5fSLen Brown 	"--help		print this help message\n"
2780d8af6f5fSLen Brown 	"--counter msr	print 32-bit counter at address \"msr\"\n"
2781d8af6f5fSLen Brown 	"--Counter msr	print 64-bit Counter at address \"msr\"\n"
2782d8af6f5fSLen Brown 	"--msr msr	print 32-bit value at address \"msr\"\n"
2783d8af6f5fSLen Brown 	"--MSR msr	print 64-bit Value at address \"msr\"\n"
2784d8af6f5fSLen Brown 	"--version	print version information\n"
2785d8af6f5fSLen Brown 	"\n"
2786d8af6f5fSLen Brown 	"For more help, run \"man turbostat\"\n");
2787103a8feaSLen Brown }
2788103a8feaSLen Brown 
2789103a8feaSLen Brown 
2790103a8feaSLen Brown /*
2791103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
2792103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
2793103a8feaSLen Brown  */
2794103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
2795103a8feaSLen Brown {
2796103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
2797103a8feaSLen Brown 		return 1;
2798103a8feaSLen Brown 	else
2799103a8feaSLen Brown 		return 0;
2800103a8feaSLen Brown }
2801103a8feaSLen Brown 
2802103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
2803103a8feaSLen Brown {
2804103a8feaSLen Brown 	return 0;
2805103a8feaSLen Brown }
2806103a8feaSLen Brown 
2807c98d5d94SLen Brown void topology_probe()
2808c98d5d94SLen Brown {
2809c98d5d94SLen Brown 	int i;
2810c98d5d94SLen Brown 	int max_core_id = 0;
2811c98d5d94SLen Brown 	int max_package_id = 0;
2812c98d5d94SLen Brown 	int max_siblings = 0;
2813c98d5d94SLen Brown 	struct cpu_topology {
2814c98d5d94SLen Brown 		int core_id;
2815c98d5d94SLen Brown 		int physical_package_id;
2816c98d5d94SLen Brown 	} *cpus;
2817c98d5d94SLen Brown 
2818c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
2819c98d5d94SLen Brown 	topo.num_cpus = 0;
2820c98d5d94SLen Brown 	topo.max_cpu_num = 0;
2821c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
2822c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
2823c98d5d94SLen Brown 		show_cpu = 1;
2824c98d5d94SLen Brown 
2825d8af6f5fSLen Brown 	if (debug > 1)
2826c98d5d94SLen Brown 		fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2827c98d5d94SLen Brown 
2828c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
2829b2c95d90SJosh Triplett 	if (cpus == NULL)
2830b2c95d90SJosh Triplett 		err(1, "calloc cpus");
2831c98d5d94SLen Brown 
2832c98d5d94SLen Brown 	/*
2833c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
2834c98d5d94SLen Brown 	 */
2835c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2836b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
2837b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
2838c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2839c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2840c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
2841c98d5d94SLen Brown 
2842c98d5d94SLen Brown 	/*
2843c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
2844c98d5d94SLen Brown 	 */
2845c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2846b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
2847b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
2848c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2849c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2850c98d5d94SLen Brown 
2851c98d5d94SLen Brown 
2852c98d5d94SLen Brown 	/*
2853c98d5d94SLen Brown 	 * For online cpus
2854c98d5d94SLen Brown 	 * find max_core_id, max_package_id
2855c98d5d94SLen Brown 	 */
2856c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
2857c98d5d94SLen Brown 		int siblings;
2858c98d5d94SLen Brown 
2859c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
2860d8af6f5fSLen Brown 			if (debug > 1)
2861c98d5d94SLen Brown 				fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2862c98d5d94SLen Brown 			continue;
2863c98d5d94SLen Brown 		}
2864c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
2865c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
2866c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
2867c98d5d94SLen Brown 
2868c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
2869c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
2870c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
2871c98d5d94SLen Brown 
2872c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
2873c98d5d94SLen Brown 		if (siblings > max_siblings)
2874c98d5d94SLen Brown 			max_siblings = siblings;
2875d8af6f5fSLen Brown 		if (debug > 1)
2876c98d5d94SLen Brown 			fprintf(stderr, "cpu %d pkg %d core %d\n",
2877c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
2878c98d5d94SLen Brown 	}
2879c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
2880d8af6f5fSLen Brown 	if (debug > 1)
2881c98d5d94SLen Brown 		fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2882c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
28831cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_cores_per_pkg > 1)
2884c98d5d94SLen Brown 		show_core = 1;
2885c98d5d94SLen Brown 
2886c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
2887d8af6f5fSLen Brown 	if (debug > 1)
2888c98d5d94SLen Brown 		fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2889c98d5d94SLen Brown 			max_package_id, topo.num_packages);
28901cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_packages > 1)
2891c98d5d94SLen Brown 		show_pkg = 1;
2892c98d5d94SLen Brown 
2893c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
2894d8af6f5fSLen Brown 	if (debug > 1)
2895c98d5d94SLen Brown 		fprintf(stderr, "max_siblings %d\n", max_siblings);
2896c98d5d94SLen Brown 
2897c98d5d94SLen Brown 	free(cpus);
2898c98d5d94SLen Brown }
2899c98d5d94SLen Brown 
2900c98d5d94SLen Brown void
2901c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2902c98d5d94SLen Brown {
2903c98d5d94SLen Brown 	int i;
2904c98d5d94SLen Brown 
2905c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2906c98d5d94SLen Brown 		topo.num_packages, sizeof(struct thread_data));
2907c98d5d94SLen Brown 	if (*t == NULL)
2908c98d5d94SLen Brown 		goto error;
2909c98d5d94SLen Brown 
2910c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
2911c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
2912c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
2913c98d5d94SLen Brown 
2914c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2915c98d5d94SLen Brown 		sizeof(struct core_data));
2916c98d5d94SLen Brown 	if (*c == NULL)
2917c98d5d94SLen Brown 		goto error;
2918c98d5d94SLen Brown 
2919c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2920c98d5d94SLen Brown 		(*c)[i].core_id = -1;
2921c98d5d94SLen Brown 
2922c98d5d94SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
2923c98d5d94SLen Brown 	if (*p == NULL)
2924c98d5d94SLen Brown 		goto error;
2925c98d5d94SLen Brown 
2926c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
2927c98d5d94SLen Brown 		(*p)[i].package_id = i;
2928c98d5d94SLen Brown 
2929c98d5d94SLen Brown 	return;
2930c98d5d94SLen Brown error:
2931b2c95d90SJosh Triplett 	err(1, "calloc counters");
2932c98d5d94SLen Brown }
2933c98d5d94SLen Brown /*
2934c98d5d94SLen Brown  * init_counter()
2935c98d5d94SLen Brown  *
2936c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
2937c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2938c98d5d94SLen Brown  *
2939c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
2940c98d5d94SLen Brown  */
2941c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2942c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
2943c98d5d94SLen Brown 	int pkg_num, int cpu_id)
2944c98d5d94SLen Brown {
2945c98d5d94SLen Brown 	struct thread_data *t;
2946c98d5d94SLen Brown 	struct core_data *c;
2947c98d5d94SLen Brown 	struct pkg_data *p;
2948c98d5d94SLen Brown 
2949c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2950c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
2951c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
2952c98d5d94SLen Brown 
2953c98d5d94SLen Brown 	t->cpu_id = cpu_id;
2954c98d5d94SLen Brown 	if (thread_num == 0) {
2955c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2956c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
2957c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2958c98d5d94SLen Brown 	}
2959c98d5d94SLen Brown 
2960c98d5d94SLen Brown 	c->core_id = core_num;
2961c98d5d94SLen Brown 	p->package_id = pkg_num;
2962c98d5d94SLen Brown }
2963c98d5d94SLen Brown 
2964c98d5d94SLen Brown 
2965c98d5d94SLen Brown int initialize_counters(int cpu_id)
2966c98d5d94SLen Brown {
2967c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
2968c98d5d94SLen Brown 
2969c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
2970c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
2971e275b388SDasaratharaman Chandramouli 	my_thread_id = get_cpu_position_in_core(cpu_id);
2972e275b388SDasaratharaman Chandramouli 	if (!my_thread_id)
2973c98d5d94SLen Brown 		topo.num_cores++;
2974c98d5d94SLen Brown 
2975c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2976c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2977c98d5d94SLen Brown 	return 0;
2978c98d5d94SLen Brown }
2979c98d5d94SLen Brown 
2980c98d5d94SLen Brown void allocate_output_buffer()
2981c98d5d94SLen Brown {
29823b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
2983c98d5d94SLen Brown 	outp = output_buffer;
2984b2c95d90SJosh Triplett 	if (outp == NULL)
2985b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
2986c98d5d94SLen Brown }
2987c98d5d94SLen Brown 
2988c98d5d94SLen Brown void setup_all_buffers(void)
2989c98d5d94SLen Brown {
2990c98d5d94SLen Brown 	topology_probe();
2991c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
2992c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
2993c98d5d94SLen Brown 	allocate_output_buffer();
2994c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
2995c98d5d94SLen Brown }
29963b4d5c7fSAndy Shevchenko 
29977ce7d5deSPrarit Bhargava void set_base_cpu(void)
29987ce7d5deSPrarit Bhargava {
29997ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
30007ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
30017ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
30027ce7d5deSPrarit Bhargava 
30037ce7d5deSPrarit Bhargava 	if (debug > 1)
30047ce7d5deSPrarit Bhargava 		fprintf(stderr, "base_cpu = %d\n", base_cpu);
30057ce7d5deSPrarit Bhargava }
30067ce7d5deSPrarit Bhargava 
3007103a8feaSLen Brown void turbostat_init()
3008103a8feaSLen Brown {
30097ce7d5deSPrarit Bhargava 	setup_all_buffers();
30107ce7d5deSPrarit Bhargava 	set_base_cpu();
3011103a8feaSLen Brown 	check_dev_msr();
301298481e79SLen Brown 	check_permissions();
3013fcd17211SLen Brown 	process_cpuid();
3014103a8feaSLen Brown 
3015103a8feaSLen Brown 
3016d8af6f5fSLen Brown 	if (debug)
3017889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
3018889facbeSLen Brown 
3019d8af6f5fSLen Brown 	if (debug)
30203a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
30213a9a941dSLen Brown 
3022d8af6f5fSLen Brown 	if (debug)
3023889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
3024889facbeSLen Brown 
3025889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
3026889facbeSLen Brown 
3027d8af6f5fSLen Brown 	if (debug)
3028889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
3029103a8feaSLen Brown }
3030103a8feaSLen Brown 
3031103a8feaSLen Brown int fork_it(char **argv)
3032103a8feaSLen Brown {
3033103a8feaSLen Brown 	pid_t child_pid;
3034d91bb17cSLen Brown 	int status;
3035d15cf7c1SLen Brown 
3036d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
3037d91bb17cSLen Brown 	if (status)
3038d91bb17cSLen Brown 		exit(status);
3039d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
3040d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
3041103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3042103a8feaSLen Brown 
3043103a8feaSLen Brown 	child_pid = fork();
3044103a8feaSLen Brown 	if (!child_pid) {
3045103a8feaSLen Brown 		/* child */
3046103a8feaSLen Brown 		execvp(argv[0], argv);
3047103a8feaSLen Brown 	} else {
3048103a8feaSLen Brown 
3049103a8feaSLen Brown 		/* parent */
3050b2c95d90SJosh Triplett 		if (child_pid == -1)
3051b2c95d90SJosh Triplett 			err(1, "fork");
3052103a8feaSLen Brown 
3053103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
3054103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
3055b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
3056b2c95d90SJosh Triplett 			err(status, "waitpid");
3057103a8feaSLen Brown 	}
3058c98d5d94SLen Brown 	/*
3059c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
3060c98d5d94SLen Brown 	 * because re-starting is problematic when forking
3061c98d5d94SLen Brown 	 */
3062c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
3063103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
3064103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
3065c98d5d94SLen Brown 	for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
3066c98d5d94SLen Brown 	compute_average(EVEN_COUNTERS);
3067c98d5d94SLen Brown 	format_all_counters(EVEN_COUNTERS);
3068c98d5d94SLen Brown 	flush_stderr();
3069103a8feaSLen Brown 
30706eab04a8SJustin P. Mattock 	fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3071103a8feaSLen Brown 
3072d91bb17cSLen Brown 	return status;
3073103a8feaSLen Brown }
3074103a8feaSLen Brown 
30753b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
30763b4d5c7fSAndy Shevchenko {
30773b4d5c7fSAndy Shevchenko 	int status;
30783b4d5c7fSAndy Shevchenko 
30793b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
30803b4d5c7fSAndy Shevchenko 	if (status)
30813b4d5c7fSAndy Shevchenko 		return status;
30823b4d5c7fSAndy Shevchenko 
30833b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
30843b4d5c7fSAndy Shevchenko 	if (status)
30853b4d5c7fSAndy Shevchenko 		return status;
30863b4d5c7fSAndy Shevchenko 
30873b4d5c7fSAndy Shevchenko 	flush_stdout();
30883b4d5c7fSAndy Shevchenko 
30893b4d5c7fSAndy Shevchenko 	return status;
30903b4d5c7fSAndy Shevchenko }
30913b4d5c7fSAndy Shevchenko 
3092d8af6f5fSLen Brown void print_version() {
30936fb3143bSLen Brown 	fprintf(stderr, "turbostat version 4.7 17-June, 2015"
3094d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
3095d8af6f5fSLen Brown }
3096d8af6f5fSLen Brown 
3097103a8feaSLen Brown void cmdline(int argc, char **argv)
3098103a8feaSLen Brown {
3099103a8feaSLen Brown 	int opt;
3100d8af6f5fSLen Brown 	int option_index = 0;
3101d8af6f5fSLen Brown 	static struct option long_options[] = {
3102d8af6f5fSLen Brown 		{"Counter",	required_argument,	0, 'C'},
3103d8af6f5fSLen Brown 		{"counter",	required_argument,	0, 'c'},
3104d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
3105d8af6f5fSLen Brown 		{"debug",	no_argument,		0, 'd'},
3106d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
3107d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
3108d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
3109d8af6f5fSLen Brown 		{"MSR",		required_argument,	0, 'M'},
3110d8af6f5fSLen Brown 		{"msr",		required_argument,	0, 'm'},
3111d8af6f5fSLen Brown 		{"Package",	no_argument,		0, 'p'},
3112d8af6f5fSLen Brown 		{"processor",	no_argument,		0, 'p'},
3113d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
3114d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
3115d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
3116d8af6f5fSLen Brown 		{0,		0,			0,  0 }
3117d8af6f5fSLen Brown 	};
3118103a8feaSLen Brown 
3119103a8feaSLen Brown 	progname = argv[0];
3120103a8feaSLen Brown 
3121*a01e72fbSLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:PpST:v",
3122d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
3123103a8feaSLen Brown 		switch (opt) {
3124d8af6f5fSLen Brown 		case 'C':
3125d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset64);
3126103a8feaSLen Brown 			break;
3127f9240813SLen Brown 		case 'c':
31288e180f3cSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset32);
31298e180f3cSLen Brown 			break;
3130d8af6f5fSLen Brown 		case 'D':
3131d8af6f5fSLen Brown 			dump_only++;
31328e180f3cSLen Brown 			break;
3133d8af6f5fSLen Brown 		case 'd':
3134d8af6f5fSLen Brown 			debug++;
31352f32edf1SLen Brown 			break;
3136d8af6f5fSLen Brown 		case 'h':
3137d8af6f5fSLen Brown 		default:
3138d8af6f5fSLen Brown 			help();
3139d8af6f5fSLen Brown 			exit(1);
3140d8af6f5fSLen Brown 		case 'i':
3141d8af6f5fSLen Brown 			interval_sec = atoi(optarg);
3142889facbeSLen Brown 			break;
31435c56be9aSDirk Brandewie 		case 'J':
31445c56be9aSDirk Brandewie 			rapl_joules++;
31455c56be9aSDirk Brandewie 			break;
3146d8af6f5fSLen Brown 		case 'M':
3147d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_msr_offset64);
3148d8af6f5fSLen Brown 			break;
3149d8af6f5fSLen Brown 		case 'm':
3150d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_msr_offset32);
3151d8af6f5fSLen Brown 			break;
3152d8af6f5fSLen Brown 		case 'P':
3153d8af6f5fSLen Brown 			show_pkg_only++;
3154d8af6f5fSLen Brown 			break;
3155d8af6f5fSLen Brown 		case 'p':
3156d8af6f5fSLen Brown 			show_core_only++;
3157d8af6f5fSLen Brown 			break;
3158d8af6f5fSLen Brown 		case 'S':
3159d8af6f5fSLen Brown 			summary_only++;
3160d8af6f5fSLen Brown 			break;
3161d8af6f5fSLen Brown 		case 'T':
3162d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
3163d8af6f5fSLen Brown 			break;
3164d8af6f5fSLen Brown 		case 'v':
3165d8af6f5fSLen Brown 			print_version();
3166d8af6f5fSLen Brown 			exit(0);
3167d8af6f5fSLen Brown 			break;
3168103a8feaSLen Brown 		}
3169103a8feaSLen Brown 	}
3170103a8feaSLen Brown }
3171103a8feaSLen Brown 
3172103a8feaSLen Brown int main(int argc, char **argv)
3173103a8feaSLen Brown {
3174103a8feaSLen Brown 	cmdline(argc, argv);
3175103a8feaSLen Brown 
3176d8af6f5fSLen Brown 	if (debug)
3177d8af6f5fSLen Brown 		print_version();
3178103a8feaSLen Brown 
3179103a8feaSLen Brown 	turbostat_init();
3180103a8feaSLen Brown 
31813b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
31823b4d5c7fSAndy Shevchenko 	if (dump_only)
31833b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
31843b4d5c7fSAndy Shevchenko 
3185103a8feaSLen Brown 	/*
3186103a8feaSLen Brown 	 * if any params left, it must be a command to fork
3187103a8feaSLen Brown 	 */
3188103a8feaSLen Brown 	if (argc - optind)
3189103a8feaSLen Brown 		return fork_it(argv + optind);
3190103a8feaSLen Brown 	else
3191103a8feaSLen Brown 		turbostat_loop();
3192103a8feaSLen Brown 
3193103a8feaSLen Brown 	return 0;
3194103a8feaSLen Brown }
3195