xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision e9257f5fa48cc296d7eed35acf9f2ad195184122)
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;
55ee7e38e3SLen Brown unsigned int do_pc2;
56ee7e38e3SLen Brown unsigned int do_pc3;
57ee7e38e3SLen Brown unsigned int do_pc6;
58ee7e38e3SLen Brown unsigned int do_pc7;
59ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10;
600b2bb692SLen Brown unsigned int do_skl_residency;
61144b44b1SLen Brown unsigned int do_slm_cstates;
62144b44b1SLen Brown unsigned int use_c1_residency_msr;
63103a8feaSLen Brown unsigned int has_aperf;
64889facbeSLen Brown unsigned int has_epb;
65fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
66103a8feaSLen Brown unsigned int genuine_intel;
67103a8feaSLen Brown unsigned int has_invariant_tsc;
68d7899447SLen Brown unsigned int do_nhm_platform_info;
692f32edf1SLen Brown unsigned int extra_msr_offset32;
702f32edf1SLen Brown unsigned int extra_msr_offset64;
718e180f3cSLen Brown unsigned int extra_delta_offset32;
728e180f3cSLen Brown unsigned int extra_delta_offset64;
731ed51011SLen Brown int do_smi;
74103a8feaSLen Brown double bclk;
75103a8feaSLen Brown unsigned int show_pkg;
76103a8feaSLen Brown unsigned int show_core;
77103a8feaSLen Brown unsigned int show_cpu;
78c98d5d94SLen Brown unsigned int show_pkg_only;
79c98d5d94SLen Brown unsigned int show_core_only;
80c98d5d94SLen Brown char *output_buffer, *outp;
81889facbeSLen Brown unsigned int do_rapl;
82889facbeSLen Brown unsigned int do_dts;
83889facbeSLen Brown unsigned int do_ptm;
84889facbeSLen Brown unsigned int tcc_activation_temp;
85889facbeSLen Brown unsigned int tcc_activation_temp_override;
8640ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
8740ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
88889facbeSLen Brown double rapl_joule_counter_range;
893a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
903a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
913a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
928a5bdf41SLen Brown unsigned int crystal_hz;
938a5bdf41SLen Brown unsigned long long tsc_hz;
94889facbeSLen Brown 
95889facbeSLen Brown #define RAPL_PKG		(1 << 0)
96e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
97e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
98e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
99e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
100e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
101e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
102e6f9bb3cSLen Brown 
103889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
104e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
105e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
106e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
107e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1080b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1090b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
110e6f9bb3cSLen Brown 
1110b2bb692SLen Brown #define RAPL_CORES		(1 << 6)
112e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
113e6f9bb3cSLen Brown 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1140b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
115e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
116e6f9bb3cSLen Brown 
1170b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
118e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
119e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
120e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
121889facbeSLen Brown #define	TJMAX_DEFAULT	100
122889facbeSLen Brown 
123889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
124103a8feaSLen Brown 
125103a8feaSLen Brown int aperf_mperf_unstable;
126103a8feaSLen Brown int backwards_count;
127103a8feaSLen Brown char *progname;
128103a8feaSLen Brown 
129c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set;
130c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize;
131c98d5d94SLen Brown 
132c98d5d94SLen Brown struct thread_data {
133c98d5d94SLen Brown 	unsigned long long tsc;
134c98d5d94SLen Brown 	unsigned long long aperf;
135c98d5d94SLen Brown 	unsigned long long mperf;
136144b44b1SLen Brown 	unsigned long long c1;
1372f32edf1SLen Brown 	unsigned long long extra_msr64;
1388e180f3cSLen Brown 	unsigned long long extra_delta64;
1398e180f3cSLen Brown 	unsigned long long extra_msr32;
1408e180f3cSLen Brown 	unsigned long long extra_delta32;
1411ed51011SLen Brown 	unsigned int smi_count;
142c98d5d94SLen Brown 	unsigned int cpu_id;
143c98d5d94SLen Brown 	unsigned int flags;
144c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
145c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
146c98d5d94SLen Brown } *thread_even, *thread_odd;
147c98d5d94SLen Brown 
148c98d5d94SLen Brown struct core_data {
149c98d5d94SLen Brown 	unsigned long long c3;
150c98d5d94SLen Brown 	unsigned long long c6;
151c98d5d94SLen Brown 	unsigned long long c7;
152889facbeSLen Brown 	unsigned int core_temp_c;
153c98d5d94SLen Brown 	unsigned int core_id;
154c98d5d94SLen Brown } *core_even, *core_odd;
155c98d5d94SLen Brown 
156c98d5d94SLen Brown struct pkg_data {
157c98d5d94SLen Brown 	unsigned long long pc2;
158c98d5d94SLen Brown 	unsigned long long pc3;
159c98d5d94SLen Brown 	unsigned long long pc6;
160c98d5d94SLen Brown 	unsigned long long pc7;
161ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
162ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
163ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1640b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1650b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1660b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1670b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
168c98d5d94SLen Brown 	unsigned int package_id;
169889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
170889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
171889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
172889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
173889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
174889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
175889facbeSLen Brown 	unsigned int pkg_temp_c;
176889facbeSLen Brown 
177c98d5d94SLen Brown } *package_even, *package_odd;
178c98d5d94SLen Brown 
179c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
180c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
181c98d5d94SLen Brown 
182c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
183c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
184c98d5d94SLen Brown 		topo.num_threads_per_core + \
185c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
186c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
187c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
188c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
189c98d5d94SLen Brown 
190c98d5d94SLen Brown struct system_summary {
191c98d5d94SLen Brown 	struct thread_data threads;
192c98d5d94SLen Brown 	struct core_data cores;
193c98d5d94SLen Brown 	struct pkg_data packages;
194c98d5d94SLen Brown } sum, average;
195c98d5d94SLen Brown 
196c98d5d94SLen Brown 
197c98d5d94SLen Brown struct topo_params {
198c98d5d94SLen Brown 	int num_packages;
199103a8feaSLen Brown 	int num_cpus;
200c98d5d94SLen Brown 	int num_cores;
201c98d5d94SLen Brown 	int max_cpu_num;
202c98d5d94SLen Brown 	int num_cores_per_pkg;
203c98d5d94SLen Brown 	int num_threads_per_core;
204c98d5d94SLen Brown } topo;
205103a8feaSLen Brown 
206c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
207103a8feaSLen Brown 
208c98d5d94SLen Brown void setup_all_buffers(void);
209103a8feaSLen Brown 
210c98d5d94SLen Brown int cpu_is_not_present(int cpu)
211d15cf7c1SLen Brown {
212c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
213c98d5d94SLen Brown }
214c98d5d94SLen Brown /*
215c98d5d94SLen Brown  * run func(thread, core, package) in topology order
216c98d5d94SLen Brown  * skip non-present cpus
217c98d5d94SLen Brown  */
218c98d5d94SLen Brown 
219c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
220c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
221c98d5d94SLen Brown {
222c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
223c98d5d94SLen Brown 
224c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
225c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
226c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
227c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
228c98d5d94SLen Brown 				struct thread_data *t;
229c98d5d94SLen Brown 				struct core_data *c;
230c98d5d94SLen Brown 				struct pkg_data *p;
231c98d5d94SLen Brown 
232c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
233c98d5d94SLen Brown 
234c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
235c98d5d94SLen Brown 					continue;
236c98d5d94SLen Brown 
237c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
238c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
239c98d5d94SLen Brown 
240c98d5d94SLen Brown 				retval = func(t, c, p);
241c98d5d94SLen Brown 				if (retval)
242c98d5d94SLen Brown 					return retval;
243c98d5d94SLen Brown 			}
244c98d5d94SLen Brown 		}
245c98d5d94SLen Brown 	}
246d15cf7c1SLen Brown 	return 0;
247d15cf7c1SLen Brown }
248d15cf7c1SLen Brown 
24988c3281fSLen Brown int cpu_migrate(int cpu)
25088c3281fSLen Brown {
251c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
252c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
253c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
25488c3281fSLen Brown 		return -1;
25588c3281fSLen Brown 	else
25688c3281fSLen Brown 		return 0;
25788c3281fSLen Brown }
25888c3281fSLen Brown 
25915aaa346SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
260103a8feaSLen Brown {
261103a8feaSLen Brown 	ssize_t retval;
262103a8feaSLen Brown 	char pathname[32];
263103a8feaSLen Brown 	int fd;
264103a8feaSLen Brown 
265103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
266103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
26715aaa346SLen Brown 	if (fd < 0)
26898481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
269103a8feaSLen Brown 
27015aaa346SLen Brown 	retval = pread(fd, msr, sizeof *msr, offset);
271103a8feaSLen Brown 	close(fd);
27215aaa346SLen Brown 
27398481e79SLen Brown 	if (retval != sizeof *msr)
27498481e79SLen Brown 		err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
27515aaa346SLen Brown 
27615aaa346SLen Brown 	return 0;
277103a8feaSLen Brown }
278103a8feaSLen Brown 
279fc04cc67SLen Brown /*
280fc04cc67SLen Brown  * Example Format w/ field column widths:
281fc04cc67SLen Brown  *
282fc04cc67SLen 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
283e7c95ff3SLen Brown  * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
284fc04cc67SLen Brown  */
285fc04cc67SLen Brown 
286a829eb4dSLen Brown void print_header(void)
287103a8feaSLen Brown {
288103a8feaSLen Brown 	if (show_pkg)
289fc04cc67SLen Brown 		outp += sprintf(outp, " Package");
290103a8feaSLen Brown 	if (show_core)
291fc04cc67SLen Brown 		outp += sprintf(outp, "    Core");
292103a8feaSLen Brown 	if (show_cpu)
293c98d5d94SLen Brown 		outp += sprintf(outp, "     CPU");
294103a8feaSLen Brown 	if (has_aperf)
295fc04cc67SLen Brown 		outp += sprintf(outp, " Avg_MHz");
296d7899447SLen Brown 	if (has_aperf)
297fc04cc67SLen Brown 		outp += sprintf(outp, "   %%Busy");
298fc04cc67SLen Brown 	if (has_aperf)
299fc04cc67SLen Brown 		outp += sprintf(outp, " Bzy_MHz");
300fc04cc67SLen Brown 	outp += sprintf(outp, " TSC_MHz");
3011cc21f7bSLen Brown 
3028e180f3cSLen Brown 	if (extra_delta_offset32)
303f9240813SLen Brown 		outp += sprintf(outp, "  count 0x%03X", extra_delta_offset32);
3048e180f3cSLen Brown 	if (extra_delta_offset64)
305f9240813SLen Brown 		outp += sprintf(outp, "  COUNT 0x%03X", extra_delta_offset64);
3062f32edf1SLen Brown 	if (extra_msr_offset32)
3078e180f3cSLen Brown 		outp += sprintf(outp, "   MSR 0x%03X", extra_msr_offset32);
3082f32edf1SLen Brown 	if (extra_msr_offset64)
3098e180f3cSLen Brown 		outp += sprintf(outp, "           MSR 0x%03X", extra_msr_offset64);
3101cc21f7bSLen Brown 
3111cc21f7bSLen Brown 	if (!debug)
3121cc21f7bSLen Brown 		goto done;
3131cc21f7bSLen Brown 
3141cc21f7bSLen Brown 	if (do_smi)
3151cc21f7bSLen Brown 		outp += sprintf(outp, "     SMI");
3161cc21f7bSLen Brown 
317103a8feaSLen Brown 	if (do_nhm_cstates)
318fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c1");
319144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates)
320fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c3");
321103a8feaSLen Brown 	if (do_nhm_cstates)
322fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c6");
323103a8feaSLen Brown 	if (do_snb_cstates)
324fc04cc67SLen Brown 		outp += sprintf(outp, "  CPU%%c7");
325889facbeSLen Brown 
326889facbeSLen Brown 	if (do_dts)
327fc04cc67SLen Brown 		outp += sprintf(outp, " CoreTmp");
328889facbeSLen Brown 	if (do_ptm)
329fc04cc67SLen Brown 		outp += sprintf(outp, "  PkgTmp");
330889facbeSLen Brown 
3310b2bb692SLen Brown 	if (do_skl_residency) {
3320b2bb692SLen Brown 		outp += sprintf(outp, " Totl%%C0");
3330b2bb692SLen Brown 		outp += sprintf(outp, "  Any%%C0");
3340b2bb692SLen Brown 		outp += sprintf(outp, "  GFX%%C0");
3350b2bb692SLen Brown 		outp += sprintf(outp, " CPUGFX%%");
3360b2bb692SLen Brown 	}
3370b2bb692SLen Brown 
338ee7e38e3SLen Brown 	if (do_pc2)
339fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc2");
340ee7e38e3SLen Brown 	if (do_pc3)
341fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc3");
342ee7e38e3SLen Brown 	if (do_pc6)
343fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc6");
344ee7e38e3SLen Brown 	if (do_pc7)
345fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc7");
346ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
347fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc8");
348fc04cc67SLen Brown 		outp += sprintf(outp, " Pkg%%pc9");
349fc04cc67SLen Brown 		outp += sprintf(outp, " Pk%%pc10");
350ca58710fSKristen Carlson Accardi 	}
351103a8feaSLen Brown 
3525c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
353889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
354fc04cc67SLen Brown 			outp += sprintf(outp, " PkgWatt");
355889facbeSLen Brown 		if (do_rapl & RAPL_CORES)
356fc04cc67SLen Brown 			outp += sprintf(outp, " CorWatt");
357889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
358fc04cc67SLen Brown 			outp += sprintf(outp, " GFXWatt");
359889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
360fc04cc67SLen Brown 			outp += sprintf(outp, " RAMWatt");
361889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
362889facbeSLen Brown 			outp += sprintf(outp, "   PKG_%%");
363889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
364889facbeSLen Brown 			outp += sprintf(outp, "   RAM_%%");
365d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
3665c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
3675c56be9aSDirk Brandewie 			outp += sprintf(outp, "   Pkg_J");
3685c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
3695c56be9aSDirk Brandewie 			outp += sprintf(outp, "   Cor_J");
3705c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
3715c56be9aSDirk Brandewie 			outp += sprintf(outp, "   GFX_J");
3725c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
3735c56be9aSDirk Brandewie 			outp += sprintf(outp, "   RAM_W");
3745c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
3755c56be9aSDirk Brandewie 			outp += sprintf(outp, "   PKG_%%");
3765c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
3775c56be9aSDirk Brandewie 			outp += sprintf(outp, "   RAM_%%");
3785c56be9aSDirk Brandewie 		outp += sprintf(outp, "   time");
379889facbeSLen Brown 
3805c56be9aSDirk Brandewie 	}
3811cc21f7bSLen Brown     done:
382c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
383103a8feaSLen Brown }
384103a8feaSLen Brown 
385c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
386c98d5d94SLen Brown 	struct pkg_data *p)
387103a8feaSLen Brown {
3883b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
389c98d5d94SLen Brown 
390c98d5d94SLen Brown 	if (t) {
3913b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
3923b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
3933b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
3943b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
3953b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
3963b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
3973b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %08llX\n",
3988e180f3cSLen Brown 			extra_delta_offset32, t->extra_delta32);
3993b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %016llX\n",
4008e180f3cSLen Brown 			extra_delta_offset64, t->extra_delta64);
4013b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %08llX\n",
4022f32edf1SLen Brown 			extra_msr_offset32, t->extra_msr32);
4033b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "msr0x%x: %016llX\n",
4042f32edf1SLen Brown 			extra_msr_offset64, t->extra_msr64);
4051ed51011SLen Brown 		if (do_smi)
4063b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
407103a8feaSLen Brown 	}
408103a8feaSLen Brown 
409c98d5d94SLen Brown 	if (c) {
4103b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
4113b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
4123b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
4133b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
4143b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
415c98d5d94SLen Brown 	}
416103a8feaSLen Brown 
417c98d5d94SLen Brown 	if (p) {
4183b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
4190b2bb692SLen Brown 
4200b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
4210b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
4220b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
4230b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
4240b2bb692SLen Brown 
4253b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
426ee7e38e3SLen Brown 		if (do_pc3)
4273b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
428ee7e38e3SLen Brown 		if (do_pc6)
4293b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
430ee7e38e3SLen Brown 		if (do_pc7)
4313b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
4323b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
4333b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
4343b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
4353b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
4363b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
4373b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
4383b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
4393b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
4403b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
4413b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
4423b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
4433b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
444c98d5d94SLen Brown 	}
4453b4d5c7fSAndy Shevchenko 
4463b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
4473b4d5c7fSAndy Shevchenko 
448c98d5d94SLen Brown 	return 0;
449103a8feaSLen Brown }
450103a8feaSLen Brown 
451e23da037SLen Brown /*
452e23da037SLen Brown  * column formatting convention & formats
453e23da037SLen Brown  */
454c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
455c98d5d94SLen Brown 	struct pkg_data *p)
456103a8feaSLen Brown {
457103a8feaSLen Brown 	double interval_float;
458fc04cc67SLen Brown 	char *fmt8;
459103a8feaSLen Brown 
460c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
461c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
462c98d5d94SLen Brown 		return 0;
463c98d5d94SLen Brown 
464c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
465c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
466c98d5d94SLen Brown 		return 0;
467c98d5d94SLen Brown 
468103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
469103a8feaSLen Brown 
470c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
471c98d5d94SLen Brown 	if (t == &average.threads) {
472103a8feaSLen Brown 		if (show_pkg)
473fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
474103a8feaSLen Brown 		if (show_core)
475fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
476103a8feaSLen Brown 		if (show_cpu)
477fc04cc67SLen Brown 			outp += sprintf(outp, "       -");
478103a8feaSLen Brown 	} else {
479c98d5d94SLen Brown 		if (show_pkg) {
480c98d5d94SLen Brown 			if (p)
481fc04cc67SLen Brown 				outp += sprintf(outp, "%8d", p->package_id);
482c98d5d94SLen Brown 			else
483fc04cc67SLen Brown 				outp += sprintf(outp, "       -");
484c98d5d94SLen Brown 		}
485c98d5d94SLen Brown 		if (show_core) {
486c98d5d94SLen Brown 			if (c)
487fc04cc67SLen Brown 				outp += sprintf(outp, "%8d", c->core_id);
488c98d5d94SLen Brown 			else
489fc04cc67SLen Brown 				outp += sprintf(outp, "       -");
490c98d5d94SLen Brown 		}
491103a8feaSLen Brown 		if (show_cpu)
492fc04cc67SLen Brown 			outp += sprintf(outp, "%8d", t->cpu_id);
493103a8feaSLen Brown 	}
494fc04cc67SLen Brown 
495d7899447SLen Brown 	/* Avg_MHz */
496fc04cc67SLen Brown 	if (has_aperf)
497fc04cc67SLen Brown 		outp += sprintf(outp, "%8.0f",
498fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
499fc04cc67SLen Brown 
500d7899447SLen Brown 	/* %Busy */
501d7899447SLen Brown 	if (has_aperf) {
502103a8feaSLen Brown 		if (!skip_c0)
503fc04cc67SLen Brown 			outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
504103a8feaSLen Brown 		else
505fc04cc67SLen Brown 			outp += sprintf(outp, "********");
506103a8feaSLen Brown 	}
507103a8feaSLen Brown 
508d7899447SLen Brown 	/* Bzy_MHz */
509fc04cc67SLen Brown 	if (has_aperf)
510fc04cc67SLen Brown 		outp += sprintf(outp, "%8.0f",
511fc04cc67SLen Brown 			1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
512103a8feaSLen Brown 
513d7899447SLen Brown 	/* TSC_MHz */
514fc04cc67SLen Brown 	outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
515103a8feaSLen Brown 
5168e180f3cSLen Brown 	/* delta */
5178e180f3cSLen Brown 	if (extra_delta_offset32)
5188e180f3cSLen Brown 		outp += sprintf(outp, "  %11llu", t->extra_delta32);
5198e180f3cSLen Brown 
5208e180f3cSLen Brown 	/* DELTA */
5218e180f3cSLen Brown 	if (extra_delta_offset64)
5228e180f3cSLen Brown 		outp += sprintf(outp, "  %11llu", t->extra_delta64);
5232f32edf1SLen Brown 	/* msr */
5242f32edf1SLen Brown 	if (extra_msr_offset32)
5258e180f3cSLen Brown 		outp += sprintf(outp, "  0x%08llx", t->extra_msr32);
5262f32edf1SLen Brown 
527130ff304SLen Brown 	/* MSR */
5282f32edf1SLen Brown 	if (extra_msr_offset64)
5292f32edf1SLen Brown 		outp += sprintf(outp, "  0x%016llx", t->extra_msr64);
530130ff304SLen Brown 
5311cc21f7bSLen Brown 	if (!debug)
5321cc21f7bSLen Brown 		goto done;
5331cc21f7bSLen Brown 
5341cc21f7bSLen Brown 	/* SMI */
5351cc21f7bSLen Brown 	if (do_smi)
5361cc21f7bSLen Brown 		outp += sprintf(outp, "%8d", t->smi_count);
5371cc21f7bSLen Brown 
538103a8feaSLen Brown 	if (do_nhm_cstates) {
539103a8feaSLen Brown 		if (!skip_c1)
540fc04cc67SLen Brown 			outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
541103a8feaSLen Brown 		else
542fc04cc67SLen Brown 			outp += sprintf(outp, "********");
543103a8feaSLen Brown 	}
544c98d5d94SLen Brown 
545c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
546c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
547c98d5d94SLen Brown 		goto done;
548c98d5d94SLen Brown 
549144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates)
550fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
551103a8feaSLen Brown 	if (do_nhm_cstates)
552fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
553103a8feaSLen Brown 	if (do_snb_cstates)
554fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
555c98d5d94SLen Brown 
556889facbeSLen Brown 	if (do_dts)
557fc04cc67SLen Brown 		outp += sprintf(outp, "%8d", c->core_temp_c);
558889facbeSLen Brown 
559c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
560c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
561c98d5d94SLen Brown 		goto done;
562c98d5d94SLen Brown 
5630b2bb692SLen Brown 	/* PkgTmp */
564889facbeSLen Brown 	if (do_ptm)
565fc04cc67SLen Brown 		outp += sprintf(outp, "%8d", p->pkg_temp_c);
566889facbeSLen Brown 
5670b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
5680b2bb692SLen Brown 	if (do_skl_residency) {
5690b2bb692SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
5700b2bb692SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
5710b2bb692SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
5720b2bb692SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
5730b2bb692SLen Brown 	}
5740b2bb692SLen Brown 
575ee7e38e3SLen Brown 	if (do_pc2)
576fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
577ee7e38e3SLen Brown 	if (do_pc3)
578fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
579ee7e38e3SLen Brown 	if (do_pc6)
580fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
581ee7e38e3SLen Brown 	if (do_pc7)
582fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
583ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
584fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
585fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
586fc04cc67SLen Brown 		outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
587ca58710fSKristen Carlson Accardi 	}
588889facbeSLen Brown 
589889facbeSLen Brown 	/*
590889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
591889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
592889facbeSLen Brown  	 */
593fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
594fc04cc67SLen Brown 		fmt8 = "%8.2f";
595fc04cc67SLen Brown 	else
596fc04cc67SLen Brown 		fmt8 = " %6.0f**";
597889facbeSLen Brown 
5985c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
599889facbeSLen Brown 		if (do_rapl & RAPL_PKG)
600fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
601889facbeSLen Brown 		if (do_rapl & RAPL_CORES)
602fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
603889facbeSLen Brown 		if (do_rapl & RAPL_GFX)
604fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
605889facbeSLen Brown 		if (do_rapl & RAPL_DRAM)
60640ee8e3bSAndrey Semin 			outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
607889facbeSLen Brown 		if (do_rapl & RAPL_PKG_PERF_STATUS)
608fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
609889facbeSLen Brown 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
610fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
611d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
6125c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG)
613fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6145c56be9aSDirk Brandewie 					p->energy_pkg * rapl_energy_units);
6155c56be9aSDirk Brandewie 		if (do_rapl & RAPL_CORES)
616fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6175c56be9aSDirk Brandewie 					p->energy_cores * rapl_energy_units);
6185c56be9aSDirk Brandewie 		if (do_rapl & RAPL_GFX)
619fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
6205c56be9aSDirk Brandewie 					p->energy_gfx * rapl_energy_units);
6215c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM)
622fc04cc67SLen Brown 			outp += sprintf(outp, fmt8,
62340ee8e3bSAndrey Semin 					p->energy_dram * rapl_dram_energy_units);
6245c56be9aSDirk Brandewie 		if (do_rapl & RAPL_PKG_PERF_STATUS)
625fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
6265c56be9aSDirk Brandewie 		if (do_rapl & RAPL_DRAM_PERF_STATUS)
627fc04cc67SLen Brown 			outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
628889facbeSLen Brown 
629d7899447SLen Brown 		outp += sprintf(outp, fmt8, interval_float);
6305c56be9aSDirk Brandewie 	}
631c98d5d94SLen Brown done:
632c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
633c98d5d94SLen Brown 
634c98d5d94SLen Brown 	return 0;
635103a8feaSLen Brown }
636103a8feaSLen Brown 
637c98d5d94SLen Brown void flush_stdout()
638103a8feaSLen Brown {
639c98d5d94SLen Brown 	fputs(output_buffer, stdout);
640ddac0d68SLen Brown 	fflush(stdout);
641c98d5d94SLen Brown 	outp = output_buffer;
642c98d5d94SLen Brown }
643c98d5d94SLen Brown void flush_stderr()
644c98d5d94SLen Brown {
645c98d5d94SLen Brown 	fputs(output_buffer, stderr);
646c98d5d94SLen Brown 	outp = output_buffer;
647c98d5d94SLen Brown }
648c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
649c98d5d94SLen Brown {
650e23da037SLen Brown 	static int printed;
651103a8feaSLen Brown 
652e23da037SLen Brown 	if (!printed || !summary_only)
653103a8feaSLen Brown 		print_header();
654103a8feaSLen Brown 
655c98d5d94SLen Brown 	if (topo.num_cpus > 1)
656c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
657c98d5d94SLen Brown 			&average.packages);
658103a8feaSLen Brown 
659e23da037SLen Brown 	printed = 1;
660e23da037SLen Brown 
661e23da037SLen Brown 	if (summary_only)
662e23da037SLen Brown 		return;
663e23da037SLen Brown 
664c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
665103a8feaSLen Brown }
666103a8feaSLen Brown 
667889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
668889facbeSLen Brown 	if (new > old) {			\
669889facbeSLen Brown 		old = new - old;		\
670889facbeSLen Brown 	} else {				\
671889facbeSLen Brown 		old = 0x100000000 + new - old;	\
672889facbeSLen Brown 	}
673889facbeSLen Brown 
674c98d5d94SLen Brown void
675c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
676103a8feaSLen Brown {
6770b2bb692SLen Brown 
6780b2bb692SLen Brown 	if (do_skl_residency) {
6790b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
6800b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
6810b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
6820b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
6830b2bb692SLen Brown 	}
684c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
685ee7e38e3SLen Brown 	if (do_pc3)
686c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
687ee7e38e3SLen Brown 	if (do_pc6)
688c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
689ee7e38e3SLen Brown 	if (do_pc7)
690c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
691ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
692ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
693ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
694889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
695889facbeSLen Brown 
696889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
697889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
698889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
699889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
700889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
701889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
702103a8feaSLen Brown }
703103a8feaSLen Brown 
704c98d5d94SLen Brown void
705c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
706c98d5d94SLen Brown {
707c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
708c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
709c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
710889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
711103a8feaSLen Brown }
712c98d5d94SLen Brown 
713c3ae331dSLen Brown /*
714c3ae331dSLen Brown  * old = new - old
715c3ae331dSLen Brown  */
716c98d5d94SLen Brown void
717c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
718c98d5d94SLen Brown 	struct core_data *core_delta)
719c98d5d94SLen Brown {
720c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
721c98d5d94SLen Brown 
722103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
723b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
724b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
725b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
726b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
727103a8feaSLen Brown 
728c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
729c98d5d94SLen Brown 
730a729617cSLen Brown 	if (has_aperf) {
731c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
732c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
733c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
734c98d5d94SLen Brown 		} else {
735c98d5d94SLen Brown 
736103a8feaSLen Brown 			if (!aperf_mperf_unstable) {
737103a8feaSLen Brown 				fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
738103a8feaSLen Brown 				fprintf(stderr, "* Frequency results do not cover entire interval *\n");
739103a8feaSLen Brown 				fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
740103a8feaSLen Brown 
741103a8feaSLen Brown 				aperf_mperf_unstable = 1;
742103a8feaSLen Brown 			}
743103a8feaSLen Brown 			/*
744103a8feaSLen Brown 			 * mperf delta is likely a huge "positive" number
745103a8feaSLen Brown 			 * can not use it for calculating c0 time
746103a8feaSLen Brown 			 */
747103a8feaSLen Brown 			skip_c0 = 1;
748103a8feaSLen Brown 			skip_c1 = 1;
749103a8feaSLen Brown 		}
750a729617cSLen Brown 	}
751103a8feaSLen Brown 
752c98d5d94SLen Brown 
753144b44b1SLen Brown 	if (use_c1_residency_msr) {
754144b44b1SLen Brown 		/*
755144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
756144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
757144b44b1SLen Brown 		 */
758144b44b1SLen Brown 	} else {
759103a8feaSLen Brown 		/*
760c3ae331dSLen Brown 		 * As counter collection is not atomic,
761c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
762103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
763103a8feaSLen Brown 		 */
764c3ae331dSLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
765c98d5d94SLen Brown 			old->c1 = 0;
766c98d5d94SLen Brown 		else {
767c98d5d94SLen Brown 			/* normal case, derive c1 */
768c98d5d94SLen Brown 			old->c1 = old->tsc - old->mperf - core_delta->c3
769c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
770c98d5d94SLen Brown 		}
771144b44b1SLen Brown 	}
772c3ae331dSLen Brown 
773c98d5d94SLen Brown 	if (old->mperf == 0) {
774d8af6f5fSLen Brown 		if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
775c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
776c98d5d94SLen Brown 	}
777103a8feaSLen Brown 
7788e180f3cSLen Brown 	old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
7798e180f3cSLen Brown 	old->extra_delta32 &= 0xFFFFFFFF;
7808e180f3cSLen Brown 
7818e180f3cSLen Brown 	old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
7828e180f3cSLen Brown 
783103a8feaSLen Brown 	/*
7848e180f3cSLen Brown 	 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
785103a8feaSLen Brown 	 */
7862f32edf1SLen Brown 	old->extra_msr32 = new->extra_msr32;
7872f32edf1SLen Brown 	old->extra_msr64 = new->extra_msr64;
7881ed51011SLen Brown 
7891ed51011SLen Brown 	if (do_smi)
7901ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
791103a8feaSLen Brown }
792c98d5d94SLen Brown 
793c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
794c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
795c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
796c98d5d94SLen Brown {
797c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
798c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
799c98d5d94SLen Brown 		delta_core(c, c2);
800c98d5d94SLen Brown 
801c98d5d94SLen Brown 	/* always calculate thread delta */
802c98d5d94SLen Brown 	delta_thread(t, t2, c2);	/* c2 is core delta */
803c98d5d94SLen Brown 
804c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
805c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
806c98d5d94SLen Brown 		delta_package(p, p2);
807c98d5d94SLen Brown 
808103a8feaSLen Brown 	return 0;
809103a8feaSLen Brown }
810103a8feaSLen Brown 
811c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
812103a8feaSLen Brown {
813c98d5d94SLen Brown 	t->tsc = 0;
814c98d5d94SLen Brown 	t->aperf = 0;
815c98d5d94SLen Brown 	t->mperf = 0;
816c98d5d94SLen Brown 	t->c1 = 0;
817103a8feaSLen Brown 
8181ed51011SLen Brown 	t->smi_count = 0;
8198e180f3cSLen Brown 	t->extra_delta32 = 0;
8208e180f3cSLen Brown 	t->extra_delta64 = 0;
8218e180f3cSLen Brown 
822c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
823c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
824c98d5d94SLen Brown 
825c98d5d94SLen Brown 	c->c3 = 0;
826c98d5d94SLen Brown 	c->c6 = 0;
827c98d5d94SLen Brown 	c->c7 = 0;
828889facbeSLen Brown 	c->core_temp_c = 0;
829c98d5d94SLen Brown 
8300b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
8310b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
8320b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
8330b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
8340b2bb692SLen Brown 
835c98d5d94SLen Brown 	p->pc2 = 0;
836ee7e38e3SLen Brown 	if (do_pc3)
837c98d5d94SLen Brown 		p->pc3 = 0;
838ee7e38e3SLen Brown 	if (do_pc6)
839c98d5d94SLen Brown 		p->pc6 = 0;
840ee7e38e3SLen Brown 	if (do_pc7)
841c98d5d94SLen Brown 		p->pc7 = 0;
842ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
843ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
844ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
845889facbeSLen Brown 
846889facbeSLen Brown 	p->energy_pkg = 0;
847889facbeSLen Brown 	p->energy_dram = 0;
848889facbeSLen Brown 	p->energy_cores = 0;
849889facbeSLen Brown 	p->energy_gfx = 0;
850889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
851889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
852889facbeSLen Brown 	p->pkg_temp_c = 0;
853103a8feaSLen Brown }
854c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
855c98d5d94SLen Brown 	struct pkg_data *p)
856103a8feaSLen Brown {
857c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
858c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
859c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
860c98d5d94SLen Brown 	average.threads.c1 += t->c1;
86115aaa346SLen Brown 
8628e180f3cSLen Brown 	average.threads.extra_delta32 += t->extra_delta32;
8638e180f3cSLen Brown 	average.threads.extra_delta64 += t->extra_delta64;
8648e180f3cSLen Brown 
865c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
866c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
867c98d5d94SLen Brown 		return 0;
868c98d5d94SLen Brown 
869c98d5d94SLen Brown 	average.cores.c3 += c->c3;
870c98d5d94SLen Brown 	average.cores.c6 += c->c6;
871c98d5d94SLen Brown 	average.cores.c7 += c->c7;
872c98d5d94SLen Brown 
873889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
874889facbeSLen Brown 
875c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
876c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
877c98d5d94SLen Brown 		return 0;
878c98d5d94SLen Brown 
8790b2bb692SLen Brown 	if (do_skl_residency) {
8800b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
8810b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
8820b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
8830b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
8840b2bb692SLen Brown 	}
8850b2bb692SLen Brown 
886c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
887ee7e38e3SLen Brown 	if (do_pc3)
888c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
889ee7e38e3SLen Brown 	if (do_pc6)
890c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
891ee7e38e3SLen Brown 	if (do_pc7)
892c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
893ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
894ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
895ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
896c98d5d94SLen Brown 
897889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
898889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
899889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
900889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
901889facbeSLen Brown 
902889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
903889facbeSLen Brown 
904889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
905889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
906c98d5d94SLen Brown 	return 0;
907c98d5d94SLen Brown }
908c98d5d94SLen Brown /*
909c98d5d94SLen Brown  * sum the counters for all cpus in the system
910c98d5d94SLen Brown  * compute the weighted average
911c98d5d94SLen Brown  */
912c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
913c98d5d94SLen Brown 	struct pkg_data *p)
914c98d5d94SLen Brown {
915c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
916c98d5d94SLen Brown 
917c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
918c98d5d94SLen Brown 
919c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
920c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
921c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
922c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
923c98d5d94SLen Brown 
9248e180f3cSLen Brown 	average.threads.extra_delta32 /= topo.num_cpus;
9258e180f3cSLen Brown 	average.threads.extra_delta32 &= 0xFFFFFFFF;
9268e180f3cSLen Brown 
9278e180f3cSLen Brown 	average.threads.extra_delta64 /= topo.num_cpus;
9288e180f3cSLen Brown 
929c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
930c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
931c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
932c98d5d94SLen Brown 
9330b2bb692SLen Brown 	if (do_skl_residency) {
9340b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
9350b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
9360b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
9370b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
9380b2bb692SLen Brown 	}
9390b2bb692SLen Brown 
940c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
941ee7e38e3SLen Brown 	if (do_pc3)
942c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
943ee7e38e3SLen Brown 	if (do_pc6)
944c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
945ee7e38e3SLen Brown 	if (do_pc7)
946c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
947ca58710fSKristen Carlson Accardi 
948ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
949ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
950ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
951c98d5d94SLen Brown }
952c98d5d94SLen Brown 
953c98d5d94SLen Brown static unsigned long long rdtsc(void)
954c98d5d94SLen Brown {
955c98d5d94SLen Brown 	unsigned int low, high;
956c98d5d94SLen Brown 
957c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
958c98d5d94SLen Brown 
959c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
960c98d5d94SLen Brown }
961c98d5d94SLen Brown 
962c98d5d94SLen Brown 
963c98d5d94SLen Brown /*
964c98d5d94SLen Brown  * get_counters(...)
965c98d5d94SLen Brown  * migrate to cpu
966c98d5d94SLen Brown  * acquire and record local counters for that cpu
967c98d5d94SLen Brown  */
968c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
969c98d5d94SLen Brown {
970c98d5d94SLen Brown 	int cpu = t->cpu_id;
971889facbeSLen Brown 	unsigned long long msr;
972c98d5d94SLen Brown 
973e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
974e52966c0SLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
97515aaa346SLen Brown 		return -1;
976e52966c0SLen Brown 	}
97715aaa346SLen Brown 
978c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
97915aaa346SLen Brown 
98015aaa346SLen Brown 	if (has_aperf) {
9819c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
982c98d5d94SLen Brown 			return -3;
9839c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
984c98d5d94SLen Brown 			return -4;
98588c3281fSLen Brown 	}
98688c3281fSLen Brown 
9871ed51011SLen Brown 	if (do_smi) {
9881ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
9891ed51011SLen Brown 			return -5;
9901ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
9911ed51011SLen Brown 	}
9928e180f3cSLen Brown 	if (extra_delta_offset32) {
993889facbeSLen Brown 		if (get_msr(cpu, extra_delta_offset32, &msr))
9942f32edf1SLen Brown 			return -5;
995889facbeSLen Brown 		t->extra_delta32 = msr & 0xFFFFFFFF;
9968e180f3cSLen Brown 	}
9978e180f3cSLen Brown 
9988e180f3cSLen Brown 	if (extra_delta_offset64)
9998e180f3cSLen Brown 		if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
10008e180f3cSLen Brown 			return -5;
10018e180f3cSLen Brown 
10028e180f3cSLen Brown 	if (extra_msr_offset32) {
1003889facbeSLen Brown 		if (get_msr(cpu, extra_msr_offset32, &msr))
10048e180f3cSLen Brown 			return -5;
1005889facbeSLen Brown 		t->extra_msr32 = msr & 0xFFFFFFFF;
10068e180f3cSLen Brown 	}
10072f32edf1SLen Brown 
10082f32edf1SLen Brown 	if (extra_msr_offset64)
10092f32edf1SLen Brown 		if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
1010c98d5d94SLen Brown 			return -5;
1011c98d5d94SLen Brown 
1012144b44b1SLen Brown 	if (use_c1_residency_msr) {
1013144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1014144b44b1SLen Brown 			return -6;
1015144b44b1SLen Brown 	}
1016144b44b1SLen Brown 
1017c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1018c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1019c98d5d94SLen Brown 		return 0;
1020c98d5d94SLen Brown 
1021144b44b1SLen Brown 	if (do_nhm_cstates && !do_slm_cstates) {
1022c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1023c98d5d94SLen Brown 			return -6;
1024144b44b1SLen Brown 	}
1025144b44b1SLen Brown 
1026144b44b1SLen Brown 	if (do_nhm_cstates) {
1027c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1028c98d5d94SLen Brown 			return -7;
1029103a8feaSLen Brown 	}
103015aaa346SLen Brown 
103115aaa346SLen Brown 	if (do_snb_cstates)
1032c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1033c98d5d94SLen Brown 			return -8;
1034c98d5d94SLen Brown 
1035889facbeSLen Brown 	if (do_dts) {
1036889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1037889facbeSLen Brown 			return -9;
1038889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1039889facbeSLen Brown 	}
1040889facbeSLen Brown 
1041889facbeSLen Brown 
1042c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1043c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1044c98d5d94SLen Brown 		return 0;
104515aaa346SLen Brown 
10460b2bb692SLen Brown 	if (do_skl_residency) {
10470b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
10480b2bb692SLen Brown 			return -10;
10490b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
10500b2bb692SLen Brown 			return -11;
10510b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
10520b2bb692SLen Brown 			return -12;
10530b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
10540b2bb692SLen Brown 			return -13;
10550b2bb692SLen Brown 	}
1056ee7e38e3SLen Brown 	if (do_pc3)
1057c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1058c98d5d94SLen Brown 			return -9;
1059ee7e38e3SLen Brown 	if (do_pc6)
1060c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1061c98d5d94SLen Brown 			return -10;
1062ee7e38e3SLen Brown 	if (do_pc2)
1063c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1064c98d5d94SLen Brown 			return -11;
1065ee7e38e3SLen Brown 	if (do_pc7)
1066c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1067c98d5d94SLen Brown 			return -12;
1068ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
1069ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1070ca58710fSKristen Carlson Accardi 			return -13;
1071ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1072ca58710fSKristen Carlson Accardi 			return -13;
1073ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1074ca58710fSKristen Carlson Accardi 			return -13;
1075ca58710fSKristen Carlson Accardi 	}
1076889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1077889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1078889facbeSLen Brown 			return -13;
1079889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1080889facbeSLen Brown 	}
1081889facbeSLen Brown 	if (do_rapl & RAPL_CORES) {
1082889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1083889facbeSLen Brown 			return -14;
1084889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1085889facbeSLen Brown 	}
1086889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1087889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1088889facbeSLen Brown 			return -15;
1089889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1090889facbeSLen Brown 	}
1091889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1092889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1093889facbeSLen Brown 			return -16;
1094889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1095889facbeSLen Brown 	}
1096889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1097889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1098889facbeSLen Brown 			return -16;
1099889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1100889facbeSLen Brown 	}
1101889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1102889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1103889facbeSLen Brown 			return -16;
1104889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1105889facbeSLen Brown 	}
1106889facbeSLen Brown 	if (do_ptm) {
1107889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1108889facbeSLen Brown 			return -17;
1109889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1110889facbeSLen Brown 	}
111115aaa346SLen Brown 	return 0;
1112103a8feaSLen Brown }
1113103a8feaSLen Brown 
1114ee7e38e3SLen Brown /*
1115ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1116ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1117ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1118ee7e38e3SLen Brown  */
1119ee7e38e3SLen Brown 
1120ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1121ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1122ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1123ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1124ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1125ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1126ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1127ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1128ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1129ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1130ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1131ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
11320b2bb692SLen Brown #define PCL__8 12 /* PC8 */
11330b2bb692SLen Brown #define PCL__9 13 /* PC9 */
11340b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1135ee7e38e3SLen Brown 
1136ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1137ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
11380b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1139ee7e38e3SLen Brown 
1140*e9257f5fSLen 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};
1141*e9257f5fSLen 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};
1142*e9257f5fSLen 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};
1143*e9257f5fSLen 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};
1144*e9257f5fSLen 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};
1145*e9257f5fSLen 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};
1146ee7e38e3SLen Brown 
1147fcd17211SLen Brown static void
1148fcd17211SLen Brown dump_nhm_platform_info(void)
1149103a8feaSLen Brown {
1150103a8feaSLen Brown 	unsigned long long msr;
1151103a8feaSLen Brown 	unsigned int ratio;
1152103a8feaSLen Brown 
11539c63a650SLen Brown 	get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
1154103a8feaSLen Brown 
1155889facbeSLen Brown 	fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
11566574a5d5SLen Brown 
1157103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
11588f61f359SLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1159103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1160103a8feaSLen Brown 
1161103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
11628f61f359SLen Brown 	fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
1163103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1164103a8feaSLen Brown 
116567920418SLen Brown 	get_msr(0, MSR_IA32_POWER_CTL, &msr);
1166144b44b1SLen Brown 	fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
116767920418SLen Brown 		msr, msr & 0x2 ? "EN" : "DIS");
116867920418SLen Brown 
1169fcd17211SLen Brown 	return;
1170fcd17211SLen Brown }
1171fcd17211SLen Brown 
1172fcd17211SLen Brown static void
1173fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1174fcd17211SLen Brown {
1175fcd17211SLen Brown 	unsigned long long msr;
1176fcd17211SLen Brown 	unsigned int ratio;
1177fcd17211SLen Brown 
1178fcd17211SLen Brown 	get_msr(0, MSR_TURBO_RATIO_LIMIT2, &msr);
1179fcd17211SLen Brown 
1180fcd17211SLen Brown 	fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", msr);
1181fcd17211SLen Brown 
1182fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1183fcd17211SLen Brown 	if (ratio)
1184fcd17211SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1185fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1186fcd17211SLen Brown 
1187fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1188fcd17211SLen Brown 	if (ratio)
1189fcd17211SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1190fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1191fcd17211SLen Brown 	return;
1192fcd17211SLen Brown }
1193fcd17211SLen Brown 
1194fcd17211SLen Brown static void
1195fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1196fcd17211SLen Brown {
1197fcd17211SLen Brown 	unsigned long long msr;
1198fcd17211SLen Brown 	unsigned int ratio;
11996574a5d5SLen Brown 
120012bb43c6SLen Brown 	get_msr(0, MSR_TURBO_RATIO_LIMIT1, &msr);
12016574a5d5SLen Brown 
120212bb43c6SLen Brown 	fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", msr);
12036574a5d5SLen Brown 
12046574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
12056574a5d5SLen Brown 	if (ratio)
12066574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
12076574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12086574a5d5SLen Brown 
12096574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
12106574a5d5SLen Brown 	if (ratio)
12116574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
12126574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12136574a5d5SLen Brown 
12146574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
12156574a5d5SLen Brown 	if (ratio)
12166574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
12176574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12186574a5d5SLen Brown 
12196574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
12206574a5d5SLen Brown 	if (ratio)
12216574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
12226574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12236574a5d5SLen Brown 
12246574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
12256574a5d5SLen Brown 	if (ratio)
12266574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
12276574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12286574a5d5SLen Brown 
12296574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
12306574a5d5SLen Brown 	if (ratio)
12316574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
12326574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12336574a5d5SLen Brown 
12346574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
12356574a5d5SLen Brown 	if (ratio)
12366574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
12376574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12386574a5d5SLen Brown 
12396574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
12406574a5d5SLen Brown 	if (ratio)
12416574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
12426574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1243103a8feaSLen Brown 	return;
1244fcd17211SLen Brown }
1245fcd17211SLen Brown 
1246fcd17211SLen Brown static void
1247fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void)
1248fcd17211SLen Brown {
1249fcd17211SLen Brown 	unsigned long long msr;
1250fcd17211SLen Brown 	unsigned int ratio;
1251103a8feaSLen Brown 
125212bb43c6SLen Brown 	get_msr(0, MSR_TURBO_RATIO_LIMIT, &msr);
1253103a8feaSLen Brown 
125412bb43c6SLen Brown 	fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
12556574a5d5SLen Brown 
12566574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
12576574a5d5SLen Brown 	if (ratio)
12586574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
12596574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12606574a5d5SLen Brown 
12616574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
12626574a5d5SLen Brown 	if (ratio)
12636574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
12646574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12656574a5d5SLen Brown 
12666574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
12676574a5d5SLen Brown 	if (ratio)
12686574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
12696574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12706574a5d5SLen Brown 
12716574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
12726574a5d5SLen Brown 	if (ratio)
12736574a5d5SLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
12746574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
12756574a5d5SLen Brown 
1276103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
1277103a8feaSLen Brown 	if (ratio)
1278103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1279103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1280103a8feaSLen Brown 
1281103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
1282103a8feaSLen Brown 	if (ratio)
1283103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1284103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1285103a8feaSLen Brown 
1286103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1287103a8feaSLen Brown 	if (ratio)
1288103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1289103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1290103a8feaSLen Brown 
1291103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
1292103a8feaSLen Brown 	if (ratio)
1293103a8feaSLen Brown 		fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1294103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1295fcd17211SLen Brown 	return;
1296fcd17211SLen Brown }
12973a9a941dSLen Brown 
1298fcd17211SLen Brown static void
1299fcd17211SLen Brown dump_nhm_cst_cfg(void)
1300fcd17211SLen Brown {
1301fcd17211SLen Brown 	unsigned long long msr;
1302fcd17211SLen Brown 
1303fcd17211SLen Brown 	get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1304fcd17211SLen Brown 
1305fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
1306fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
1307fcd17211SLen Brown 
1308fcd17211SLen Brown 	fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1309fcd17211SLen Brown 
1310fcd17211SLen Brown 	fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1311fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1312fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1313fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1314fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1315fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
1316fcd17211SLen Brown 		(unsigned int)msr & 7,
1317fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
1318fcd17211SLen Brown 	return;
1319103a8feaSLen Brown }
1320103a8feaSLen Brown 
1321c98d5d94SLen Brown void free_all_buffers(void)
1322103a8feaSLen Brown {
1323c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
1324c98d5d94SLen Brown 	cpu_present_set = NULL;
1325c98d5d94SLen Brown 	cpu_present_set = 0;
1326103a8feaSLen Brown 
1327c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
1328c98d5d94SLen Brown 	cpu_affinity_set = NULL;
1329c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
1330103a8feaSLen Brown 
1331c98d5d94SLen Brown 	free(thread_even);
1332c98d5d94SLen Brown 	free(core_even);
1333c98d5d94SLen Brown 	free(package_even);
1334103a8feaSLen Brown 
1335c98d5d94SLen Brown 	thread_even = NULL;
1336c98d5d94SLen Brown 	core_even = NULL;
1337c98d5d94SLen Brown 	package_even = NULL;
1338103a8feaSLen Brown 
1339c98d5d94SLen Brown 	free(thread_odd);
1340c98d5d94SLen Brown 	free(core_odd);
1341c98d5d94SLen Brown 	free(package_odd);
1342103a8feaSLen Brown 
1343c98d5d94SLen Brown 	thread_odd = NULL;
1344c98d5d94SLen Brown 	core_odd = NULL;
1345c98d5d94SLen Brown 	package_odd = NULL;
1346103a8feaSLen Brown 
1347c98d5d94SLen Brown 	free(output_buffer);
1348c98d5d94SLen Brown 	output_buffer = NULL;
1349c98d5d94SLen Brown 	outp = NULL;
1350103a8feaSLen Brown }
1351103a8feaSLen Brown 
1352103a8feaSLen Brown /*
135357a42a34SJosh Triplett  * Open a file, and exit on failure
135457a42a34SJosh Triplett  */
135557a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode)
135657a42a34SJosh Triplett {
135757a42a34SJosh Triplett 	FILE *filep = fopen(path, "r");
1358b2c95d90SJosh Triplett 	if (!filep)
1359b2c95d90SJosh Triplett 		err(1, "%s: open failed", path);
136057a42a34SJosh Triplett 	return filep;
136157a42a34SJosh Triplett }
136257a42a34SJosh Triplett 
136357a42a34SJosh Triplett /*
136495aebc44SJosh Triplett  * Parse a file containing a single int.
136595aebc44SJosh Triplett  */
136695aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
136795aebc44SJosh Triplett {
136895aebc44SJosh Triplett 	va_list args;
136995aebc44SJosh Triplett 	char path[PATH_MAX];
137095aebc44SJosh Triplett 	FILE *filep;
137195aebc44SJosh Triplett 	int value;
137295aebc44SJosh Triplett 
137395aebc44SJosh Triplett 	va_start(args, fmt);
137495aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
137595aebc44SJosh Triplett 	va_end(args);
137657a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1377b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
1378b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
137995aebc44SJosh Triplett 	fclose(filep);
138095aebc44SJosh Triplett 	return value;
138195aebc44SJosh Triplett }
138295aebc44SJosh Triplett 
138395aebc44SJosh Triplett /*
1384c98d5d94SLen Brown  * cpu_is_first_sibling_in_core(cpu)
1385c98d5d94SLen Brown  * return 1 if given CPU is 1st HT sibling in the core
1386103a8feaSLen Brown  */
1387c98d5d94SLen Brown int cpu_is_first_sibling_in_core(int cpu)
1388103a8feaSLen Brown {
138995aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1390c98d5d94SLen Brown }
1391103a8feaSLen Brown 
1392c98d5d94SLen Brown /*
1393c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
1394c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
1395c98d5d94SLen Brown  */
1396c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
1397c98d5d94SLen Brown {
139895aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1399103a8feaSLen Brown }
1400103a8feaSLen Brown 
1401103a8feaSLen Brown int get_physical_package_id(int cpu)
1402103a8feaSLen Brown {
140395aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1404103a8feaSLen Brown }
1405103a8feaSLen Brown 
1406103a8feaSLen Brown int get_core_id(int cpu)
1407103a8feaSLen Brown {
140895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1409103a8feaSLen Brown }
1410103a8feaSLen Brown 
1411c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
1412c98d5d94SLen Brown {
1413c98d5d94SLen Brown 	char path[80];
1414c98d5d94SLen Brown 	FILE *filep;
1415c98d5d94SLen Brown 	int sib1, sib2;
1416c98d5d94SLen Brown 	int matches;
1417c98d5d94SLen Brown 	char character;
1418c98d5d94SLen Brown 
1419c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
142057a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1421103a8feaSLen Brown 	/*
1422c98d5d94SLen Brown 	 * file format:
1423c98d5d94SLen Brown 	 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1424c98d5d94SLen Brown 	 * otherwinse 1 sibling (self).
1425c98d5d94SLen Brown 	 */
1426c98d5d94SLen Brown 	matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1427c98d5d94SLen Brown 
1428c98d5d94SLen Brown 	fclose(filep);
1429c98d5d94SLen Brown 
1430c98d5d94SLen Brown 	if (matches == 3)
1431c98d5d94SLen Brown 		return 2;
1432c98d5d94SLen Brown 	else
1433c98d5d94SLen Brown 		return 1;
1434c98d5d94SLen Brown }
1435c98d5d94SLen Brown 
1436c98d5d94SLen Brown /*
1437c98d5d94SLen Brown  * run func(thread, core, package) in topology order
1438c98d5d94SLen Brown  * skip non-present cpus
1439103a8feaSLen Brown  */
1440103a8feaSLen Brown 
1441c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1442c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
1443c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
1444c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
1445c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
1446c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
1447c98d5d94SLen Brown {
1448c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
1449c98d5d94SLen Brown 
1450c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1451c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1452c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
1453c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
1454c98d5d94SLen Brown 				struct thread_data *t, *t2;
1455c98d5d94SLen Brown 				struct core_data *c, *c2;
1456c98d5d94SLen Brown 				struct pkg_data *p, *p2;
1457c98d5d94SLen Brown 
1458c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1459c98d5d94SLen Brown 
1460c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
1461c98d5d94SLen Brown 					continue;
1462c98d5d94SLen Brown 
1463c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1464c98d5d94SLen Brown 
1465c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
1466c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
1467c98d5d94SLen Brown 
1468c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
1469c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
1470c98d5d94SLen Brown 
1471c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
1472c98d5d94SLen Brown 				if (retval)
1473c98d5d94SLen Brown 					return retval;
1474c98d5d94SLen Brown 			}
1475c98d5d94SLen Brown 		}
1476c98d5d94SLen Brown 	}
1477c98d5d94SLen Brown 	return 0;
1478c98d5d94SLen Brown }
1479c98d5d94SLen Brown 
1480c98d5d94SLen Brown /*
1481c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
1482c98d5d94SLen Brown  * return max_cpu number
1483c98d5d94SLen Brown  */
1484c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
1485103a8feaSLen Brown {
1486103a8feaSLen Brown 	FILE *fp;
1487c98d5d94SLen Brown 	int cpu_num;
1488103a8feaSLen Brown 	int retval;
1489103a8feaSLen Brown 
149057a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
1491103a8feaSLen Brown 
1492103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1493b2c95d90SJosh Triplett 	if (retval != 0)
1494b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
1495103a8feaSLen Brown 
1496c98d5d94SLen Brown 	while (1) {
1497c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1498103a8feaSLen Brown 		if (retval != 1)
1499103a8feaSLen Brown 			break;
1500103a8feaSLen Brown 
1501c98d5d94SLen Brown 		retval = func(cpu_num);
1502c98d5d94SLen Brown 		if (retval) {
1503c98d5d94SLen Brown 			fclose(fp);
1504c98d5d94SLen Brown 			return(retval);
1505c98d5d94SLen Brown 		}
1506103a8feaSLen Brown 	}
1507103a8feaSLen Brown 	fclose(fp);
1508c98d5d94SLen Brown 	return 0;
1509103a8feaSLen Brown }
1510103a8feaSLen Brown 
1511103a8feaSLen Brown void re_initialize(void)
1512103a8feaSLen Brown {
1513c98d5d94SLen Brown 	free_all_buffers();
1514c98d5d94SLen Brown 	setup_all_buffers();
1515c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1516103a8feaSLen Brown }
1517103a8feaSLen Brown 
1518c98d5d94SLen Brown 
1519103a8feaSLen Brown /*
1520c98d5d94SLen Brown  * count_cpus()
1521c98d5d94SLen Brown  * remember the last one seen, it will be the max
1522103a8feaSLen Brown  */
1523c98d5d94SLen Brown int count_cpus(int cpu)
1524103a8feaSLen Brown {
1525c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
1526c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
1527103a8feaSLen Brown 
1528c98d5d94SLen Brown 	topo.num_cpus += 1;
1529c98d5d94SLen Brown 	return 0;
1530103a8feaSLen Brown }
1531c98d5d94SLen Brown int mark_cpu_present(int cpu)
1532c98d5d94SLen Brown {
1533c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
153415aaa346SLen Brown 	return 0;
1535103a8feaSLen Brown }
1536103a8feaSLen Brown 
1537103a8feaSLen Brown void turbostat_loop()
1538103a8feaSLen Brown {
1539c98d5d94SLen Brown 	int retval;
1540e52966c0SLen Brown 	int restarted = 0;
1541c98d5d94SLen Brown 
1542103a8feaSLen Brown restart:
1543e52966c0SLen Brown 	restarted++;
1544e52966c0SLen Brown 
1545c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1546d91bb17cSLen Brown 	if (retval < -1) {
1547d91bb17cSLen Brown 		exit(retval);
1548d91bb17cSLen Brown 	} else if (retval == -1) {
1549e52966c0SLen Brown 		if (restarted > 1) {
1550e52966c0SLen Brown 			exit(retval);
1551e52966c0SLen Brown 		}
1552c98d5d94SLen Brown 		re_initialize();
1553c98d5d94SLen Brown 		goto restart;
1554c98d5d94SLen Brown 	}
1555e52966c0SLen Brown 	restarted = 0;
1556103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
1557103a8feaSLen Brown 
1558103a8feaSLen Brown 	while (1) {
1559c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
1560103a8feaSLen Brown 			re_initialize();
1561103a8feaSLen Brown 			goto restart;
1562103a8feaSLen Brown 		}
1563103a8feaSLen Brown 		sleep(interval_sec);
1564c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
1565d91bb17cSLen Brown 		if (retval < -1) {
1566d91bb17cSLen Brown 			exit(retval);
1567d91bb17cSLen Brown 		} else if (retval == -1) {
156815aaa346SLen Brown 			re_initialize();
156915aaa346SLen Brown 			goto restart;
157015aaa346SLen Brown 		}
1571103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
1572103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
1573c98d5d94SLen Brown 		for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1574c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
1575c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
1576c98d5d94SLen Brown 		flush_stdout();
157715aaa346SLen Brown 		sleep(interval_sec);
1578c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1579d91bb17cSLen Brown 		if (retval < -1) {
1580d91bb17cSLen Brown 			exit(retval);
1581d91bb17cSLen Brown 		} else if (retval == -1) {
1582103a8feaSLen Brown 			re_initialize();
1583103a8feaSLen Brown 			goto restart;
1584103a8feaSLen Brown 		}
1585103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
1586103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
1587c98d5d94SLen Brown 		for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1588c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
1589c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
1590c98d5d94SLen Brown 		flush_stdout();
1591103a8feaSLen Brown 	}
1592103a8feaSLen Brown }
1593103a8feaSLen Brown 
1594103a8feaSLen Brown void check_dev_msr()
1595103a8feaSLen Brown {
1596103a8feaSLen Brown 	struct stat sb;
1597103a8feaSLen Brown 
1598b2c95d90SJosh Triplett 	if (stat("/dev/cpu/0/msr", &sb))
1599a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1600d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1601103a8feaSLen Brown }
1602103a8feaSLen Brown 
160398481e79SLen Brown void check_permissions()
1604103a8feaSLen Brown {
160598481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
160698481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
160798481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
160898481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
160998481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
161098481e79SLen Brown 	int do_exit = 0;
161198481e79SLen Brown 
161298481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
161398481e79SLen Brown 	cap_header->pid = getpid();
161498481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
161598481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
161698481e79SLen Brown 		err(-6, "capget(2) failed");
161798481e79SLen Brown 
161898481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
161998481e79SLen Brown 		do_exit++;
162098481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
162198481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
162298481e79SLen Brown 	}
162398481e79SLen Brown 
162498481e79SLen Brown 	/* test file permissions */
162598481e79SLen Brown 	if (euidaccess("/dev/cpu/0/msr", R_OK)) {
162698481e79SLen Brown 		do_exit++;
162798481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
162898481e79SLen Brown 	}
162998481e79SLen Brown 
163098481e79SLen Brown 	/* if all else fails, thell them to be root */
163198481e79SLen Brown 	if (do_exit)
1632b2c95d90SJosh Triplett 		if (getuid() != 0)
1633d7899447SLen Brown 			warnx("... or simply run as root");
163498481e79SLen Brown 
163598481e79SLen Brown 	if (do_exit)
163698481e79SLen Brown 		exit(-6);
1637103a8feaSLen Brown }
1638103a8feaSLen Brown 
1639d7899447SLen Brown /*
1640d7899447SLen Brown  * NHM adds support for additional MSRs:
1641d7899447SLen Brown  *
1642d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
1643d7899447SLen Brown  *
1644d7899447SLen Brown  * MSR_NHM_PLATFORM_INFO           0x000000ce
1645d7899447SLen Brown  * MSR_NHM_SNB_PKG_CST_CFG_CTL     0x000000e2
1646d7899447SLen Brown  *
1647d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
1648d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
1649d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
1650d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
1651d7899447SLen Brown  *
1652ee7e38e3SLen Brown  * Side effect:
1653ee7e38e3SLen Brown  * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
1654d7899447SLen Brown  */
1655ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
1656103a8feaSLen Brown {
1657ee7e38e3SLen Brown 	unsigned long long msr;
1658ee7e38e3SLen Brown 	int *pkg_cstate_limits;
1659ee7e38e3SLen Brown 
1660103a8feaSLen Brown 	if (!genuine_intel)
1661103a8feaSLen Brown 		return 0;
1662103a8feaSLen Brown 
1663103a8feaSLen Brown 	if (family != 6)
1664103a8feaSLen Brown 		return 0;
1665103a8feaSLen Brown 
1666103a8feaSLen Brown 	switch (model) {
1667103a8feaSLen Brown 	case 0x1A:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1668103a8feaSLen Brown 	case 0x1E:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1669103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
1670103a8feaSLen Brown 	case 0x25:	/* Westmere Client - Clarkdale, Arrandale */
1671103a8feaSLen Brown 	case 0x2C:	/* Westmere EP - Gulftown */
1672ee7e38e3SLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
1673ee7e38e3SLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
1674ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
1675ee7e38e3SLen Brown 		break;
1676103a8feaSLen Brown 	case 0x2A:	/* SNB */
1677103a8feaSLen Brown 	case 0x2D:	/* SNB Xeon */
1678553575f1SLen Brown 	case 0x3A:	/* IVB */
16791300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
1680ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
1681ee7e38e3SLen Brown 		break;
168270b43400SLen Brown 	case 0x3C:	/* HSW */
1683e6f9bb3cSLen Brown 	case 0x3F:	/* HSX */
168470b43400SLen Brown 	case 0x45:	/* HSW */
1685149c2319SLen Brown 	case 0x46:	/* HSW */
16864e8e863fSLen Brown 	case 0x3D:	/* BDW */
168748a0631cSLen Brown 	case 0x47:	/* BDW */
16884e8e863fSLen Brown 	case 0x4F:	/* BDX */
16894e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
16900b2bb692SLen Brown 	case 0x4E:	/* SKL */
16910b2bb692SLen Brown 	case 0x5E:	/* SKL */
1692ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
1693ee7e38e3SLen Brown 		break;
1694ee7e38e3SLen Brown 	case 0x37:	/* BYT */
1695ee7e38e3SLen Brown 	case 0x4D:	/* AVN */
1696ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
1697ee7e38e3SLen Brown 		break;
1698ee7e38e3SLen Brown 	case 0x4C:	/* AMT */
1699ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
1700ee7e38e3SLen Brown 		break;
1701ee7e38e3SLen Brown 	case 0x57:	/* PHI */
1702ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
1703ee7e38e3SLen Brown 		break;
1704103a8feaSLen Brown 	default:
1705103a8feaSLen Brown 		return 0;
1706103a8feaSLen Brown 	}
1707ee7e38e3SLen Brown 	get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1708ee7e38e3SLen Brown 
1709*e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
1710ee7e38e3SLen Brown 
1711ee7e38e3SLen Brown 	return 1;
1712103a8feaSLen Brown }
1713d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1714d7899447SLen Brown {
1715d7899447SLen Brown 	switch (model) {
1716d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
1717d7899447SLen Brown 	case 0x2E:	/* Nehalem-EX Xeon - Beckton */
1718d7899447SLen Brown 	case 0x2F:	/* Westmere-EX Xeon - Eagleton */
1719d7899447SLen Brown 		return 0;
1720d7899447SLen Brown 	default:
1721d7899447SLen Brown 		return 1;
1722d7899447SLen Brown 	}
1723d7899447SLen Brown }
17246574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
17256574a5d5SLen Brown {
17266574a5d5SLen Brown 	if (!genuine_intel)
17276574a5d5SLen Brown 		return 0;
17286574a5d5SLen Brown 
17296574a5d5SLen Brown 	if (family != 6)
17306574a5d5SLen Brown 		return 0;
17316574a5d5SLen Brown 
17326574a5d5SLen Brown 	switch (model) {
17336574a5d5SLen Brown 	case 0x3E:	/* IVB Xeon */
1734fcd17211SLen Brown 	case 0x3F:	/* HSW Xeon */
17356574a5d5SLen Brown 		return 1;
17366574a5d5SLen Brown 	default:
17376574a5d5SLen Brown 		return 0;
17386574a5d5SLen Brown 	}
17396574a5d5SLen Brown }
1740fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1741fcd17211SLen Brown {
1742fcd17211SLen Brown 	if (!genuine_intel)
1743fcd17211SLen Brown 		return 0;
1744fcd17211SLen Brown 
1745fcd17211SLen Brown 	if (family != 6)
1746fcd17211SLen Brown 		return 0;
1747fcd17211SLen Brown 
1748fcd17211SLen Brown 	switch (model) {
1749fcd17211SLen Brown 	case 0x3F:	/* HSW Xeon */
1750fcd17211SLen Brown 		return 1;
1751fcd17211SLen Brown 	default:
1752fcd17211SLen Brown 		return 0;
1753fcd17211SLen Brown 	}
1754fcd17211SLen Brown }
1755fcd17211SLen Brown 
1756fcd17211SLen Brown static void
1757fcd17211SLen Brown dump_cstate_pstate_config_info(family, model)
1758fcd17211SLen Brown {
1759fcd17211SLen Brown 	if (!do_nhm_platform_info)
1760fcd17211SLen Brown 		return;
1761fcd17211SLen Brown 
1762fcd17211SLen Brown 	dump_nhm_platform_info();
1763fcd17211SLen Brown 
1764fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
1765fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
1766fcd17211SLen Brown 
1767fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
1768fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
1769fcd17211SLen Brown 
1770fcd17211SLen Brown 	if (has_nhm_turbo_ratio_limit(family, model))
1771fcd17211SLen Brown 		dump_nhm_turbo_ratio_limits();
1772fcd17211SLen Brown 
1773fcd17211SLen Brown 	dump_nhm_cst_cfg();
1774fcd17211SLen Brown }
1775fcd17211SLen Brown 
17766574a5d5SLen Brown 
1777889facbeSLen Brown /*
1778889facbeSLen Brown  * print_epb()
1779889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
1780889facbeSLen Brown  */
1781889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1782889facbeSLen Brown {
1783889facbeSLen Brown 	unsigned long long msr;
1784889facbeSLen Brown 	char *epb_string;
1785889facbeSLen Brown 	int cpu;
1786889facbeSLen Brown 
1787889facbeSLen Brown 	if (!has_epb)
1788889facbeSLen Brown 		return 0;
1789889facbeSLen Brown 
1790889facbeSLen Brown 	cpu = t->cpu_id;
1791889facbeSLen Brown 
1792889facbeSLen Brown 	/* EPB is per-package */
1793889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1794889facbeSLen Brown 		return 0;
1795889facbeSLen Brown 
1796889facbeSLen Brown 	if (cpu_migrate(cpu)) {
1797889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1798889facbeSLen Brown 		return -1;
1799889facbeSLen Brown 	}
1800889facbeSLen Brown 
1801889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1802889facbeSLen Brown 		return 0;
1803889facbeSLen Brown 
1804889facbeSLen Brown 	switch (msr & 0x7) {
1805889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
1806889facbeSLen Brown 		epb_string = "performance";
1807889facbeSLen Brown 		break;
1808889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
1809889facbeSLen Brown 		epb_string = "balanced";
1810889facbeSLen Brown 		break;
1811889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
1812889facbeSLen Brown 		epb_string = "powersave";
1813889facbeSLen Brown 		break;
1814889facbeSLen Brown 	default:
1815889facbeSLen Brown 		epb_string = "custom";
1816889facbeSLen Brown 		break;
1817889facbeSLen Brown 	}
1818889facbeSLen Brown 	fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1819889facbeSLen Brown 
1820889facbeSLen Brown 	return 0;
1821889facbeSLen Brown }
1822889facbeSLen Brown 
18233a9a941dSLen Brown /*
18243a9a941dSLen Brown  * print_perf_limit()
18253a9a941dSLen Brown  */
18263a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
18273a9a941dSLen Brown {
18283a9a941dSLen Brown 	unsigned long long msr;
18293a9a941dSLen Brown 	int cpu;
18303a9a941dSLen Brown 
18313a9a941dSLen Brown 	cpu = t->cpu_id;
18323a9a941dSLen Brown 
18333a9a941dSLen Brown 	/* per-package */
18343a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
18353a9a941dSLen Brown 		return 0;
18363a9a941dSLen Brown 
18373a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
18383a9a941dSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
18393a9a941dSLen Brown 		return -1;
18403a9a941dSLen Brown 	}
18413a9a941dSLen Brown 
18423a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
18433a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
18443a9a941dSLen Brown 		fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
18453a9a941dSLen Brown 		fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
1846e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
18473a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
1848e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
1849e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
1850e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
1851e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
1852e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
1853e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
1854e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
1855e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
1856e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
1857e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
1858e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
1859e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
18603a9a941dSLen Brown 		fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
1861e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
18623a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
1863e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
1864e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
1865e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
1866e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
1867e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
1868e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
1869e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
1870e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
1871e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
1872e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
1873e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
1874e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
18753a9a941dSLen Brown 
18763a9a941dSLen Brown 	}
18773a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
18783a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
18793a9a941dSLen Brown 		fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
18803a9a941dSLen Brown 		fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
18813a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
18823a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
18833a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
18843a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
18853a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
18863a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
18873a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
18883a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
18893a9a941dSLen Brown 		fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
18903a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
18913a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
18923a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
18933a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
18943a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
18953a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
18963a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
18973a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
18983a9a941dSLen Brown 	}
18993a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
19003a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
19013a9a941dSLen Brown 		fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
19023a9a941dSLen Brown 		fprintf(stderr, " (Active: %s%s%s%s%s%s)",
19033a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
19043a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
19053a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
19063a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
19073a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
19083a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
19093a9a941dSLen Brown 		fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
19103a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
19113a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
19123a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
19133a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
19143a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
19153a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
19163a9a941dSLen Brown 	}
19173a9a941dSLen Brown 	return 0;
19183a9a941dSLen Brown }
19193a9a941dSLen Brown 
1920889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
1921889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
1922889facbeSLen Brown 
1923144b44b1SLen Brown double get_tdp(model)
1924144b44b1SLen Brown {
1925144b44b1SLen Brown 	unsigned long long msr;
1926144b44b1SLen Brown 
1927144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
1928144b44b1SLen Brown 		if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1929144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1930144b44b1SLen Brown 
1931144b44b1SLen Brown 	switch (model) {
1932144b44b1SLen Brown 	case 0x37:
1933144b44b1SLen Brown 	case 0x4D:
1934144b44b1SLen Brown 		return 30.0;
1935144b44b1SLen Brown 	default:
1936144b44b1SLen Brown 		return 135.0;
1937144b44b1SLen Brown 	}
1938144b44b1SLen Brown }
1939144b44b1SLen Brown 
194040ee8e3bSAndrey Semin /*
194140ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
194240ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
194340ee8e3bSAndrey Semin  */
194440ee8e3bSAndrey Semin static double
194540ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
194640ee8e3bSAndrey Semin {
194740ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
194840ee8e3bSAndrey Semin 
194940ee8e3bSAndrey Semin 	switch (model) {
195040ee8e3bSAndrey Semin 	case 0x3F:	/* HSX */
195140ee8e3bSAndrey Semin 	case 0x4F:	/* BDX */
195240ee8e3bSAndrey Semin 	case 0x56:	/* BDX-DE */
195340ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
195440ee8e3bSAndrey Semin 	default:
195540ee8e3bSAndrey Semin 		return (rapl_energy_units);
195640ee8e3bSAndrey Semin 	}
195740ee8e3bSAndrey Semin }
195840ee8e3bSAndrey Semin 
1959144b44b1SLen Brown 
1960889facbeSLen Brown /*
1961889facbeSLen Brown  * rapl_probe()
1962889facbeSLen Brown  *
1963144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
1964889facbeSLen Brown  */
1965889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
1966889facbeSLen Brown {
1967889facbeSLen Brown 	unsigned long long msr;
1968144b44b1SLen Brown 	unsigned int time_unit;
1969889facbeSLen Brown 	double tdp;
1970889facbeSLen Brown 
1971889facbeSLen Brown 	if (!genuine_intel)
1972889facbeSLen Brown 		return;
1973889facbeSLen Brown 
1974889facbeSLen Brown 	if (family != 6)
1975889facbeSLen Brown 		return;
1976889facbeSLen Brown 
1977889facbeSLen Brown 	switch (model) {
1978889facbeSLen Brown 	case 0x2A:
1979889facbeSLen Brown 	case 0x3A:
198070b43400SLen Brown 	case 0x3C:	/* HSW */
198170b43400SLen Brown 	case 0x45:	/* HSW */
1982149c2319SLen Brown 	case 0x46:	/* HSW */
19834e8e863fSLen Brown 	case 0x3D:	/* BDW */
198448a0631cSLen Brown 	case 0x47:	/* BDW */
1985144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1986889facbeSLen Brown 		break;
19870b2bb692SLen Brown 	case 0x4E:	/* SKL */
19880b2bb692SLen Brown 	case 0x5E:	/* SKL */
19890b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
19900b2bb692SLen Brown 		break;
1991e6f9bb3cSLen Brown 	case 0x3F:	/* HSX */
19924e8e863fSLen Brown 	case 0x4F:	/* BDX */
19934e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
19940b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1995e6f9bb3cSLen Brown 		break;
1996889facbeSLen Brown 	case 0x2D:
1997889facbeSLen Brown 	case 0x3E:
19980b2bb692SLen 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;
1999144b44b1SLen Brown 		break;
2000144b44b1SLen Brown 	case 0x37:	/* BYT */
2001144b44b1SLen Brown 	case 0x4D:	/* AVN */
2002144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES ;
2003889facbeSLen Brown 		break;
2004889facbeSLen Brown 	default:
2005889facbeSLen Brown 		return;
2006889facbeSLen Brown 	}
2007889facbeSLen Brown 
2008889facbeSLen Brown 	/* units on package 0, verify later other packages match */
2009889facbeSLen Brown 	if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
2010889facbeSLen Brown 		return;
2011889facbeSLen Brown 
2012889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
2013144b44b1SLen Brown 	if (model == 0x37)
2014144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2015144b44b1SLen Brown 	else
2016889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
2017889facbeSLen Brown 
201840ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
201940ee8e3bSAndrey Semin 
2020144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
2021144b44b1SLen Brown 	if (time_unit == 0)
2022144b44b1SLen Brown 		time_unit = 0xA;
2023889facbeSLen Brown 
2024144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
2025144b44b1SLen Brown 
2026144b44b1SLen Brown 	tdp = get_tdp(model);
2027889facbeSLen Brown 
2028889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
2029d8af6f5fSLen Brown 	if (debug)
2030144b44b1SLen Brown 		fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
2031889facbeSLen Brown 
2032889facbeSLen Brown 	return;
2033889facbeSLen Brown }
2034889facbeSLen Brown 
20353a9a941dSLen Brown void perf_limit_reasons_probe(family, model)
20363a9a941dSLen Brown {
20373a9a941dSLen Brown 	if (!genuine_intel)
20383a9a941dSLen Brown 		return;
20393a9a941dSLen Brown 
20403a9a941dSLen Brown 	if (family != 6)
20413a9a941dSLen Brown 		return;
20423a9a941dSLen Brown 
20433a9a941dSLen Brown 	switch (model) {
20443a9a941dSLen Brown 	case 0x3C:	/* HSW */
20453a9a941dSLen Brown 	case 0x45:	/* HSW */
20463a9a941dSLen Brown 	case 0x46:	/* HSW */
20473a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
20483a9a941dSLen Brown 	case 0x3F:	/* HSX */
20493a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
20503a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
20513a9a941dSLen Brown 	default:
20523a9a941dSLen Brown 		return;
20533a9a941dSLen Brown 	}
20543a9a941dSLen Brown }
20553a9a941dSLen Brown 
2056889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2057889facbeSLen Brown {
2058889facbeSLen Brown 	unsigned long long msr;
2059889facbeSLen Brown 	unsigned int dts;
2060889facbeSLen Brown 	int cpu;
2061889facbeSLen Brown 
2062889facbeSLen Brown 	if (!(do_dts || do_ptm))
2063889facbeSLen Brown 		return 0;
2064889facbeSLen Brown 
2065889facbeSLen Brown 	cpu = t->cpu_id;
2066889facbeSLen Brown 
2067889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
2068889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2069889facbeSLen Brown 		return 0;
2070889facbeSLen Brown 
2071889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2072889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2073889facbeSLen Brown 		return -1;
2074889facbeSLen Brown 	}
2075889facbeSLen Brown 
2076889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2077889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2078889facbeSLen Brown 			return 0;
2079889facbeSLen Brown 
2080889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2081889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2082889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
2083889facbeSLen Brown 
2084889facbeSLen Brown #ifdef	THERM_DEBUG
2085889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2086889facbeSLen Brown 			return 0;
2087889facbeSLen Brown 
2088889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2089889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
2090889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2091889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2092889facbeSLen Brown #endif
2093889facbeSLen Brown 	}
2094889facbeSLen Brown 
2095889facbeSLen Brown 
2096889facbeSLen Brown 	if (do_dts) {
2097889facbeSLen Brown 		unsigned int resolution;
2098889facbeSLen Brown 
2099889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2100889facbeSLen Brown 			return 0;
2101889facbeSLen Brown 
2102889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2103889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
2104889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2105889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
2106889facbeSLen Brown 
2107889facbeSLen Brown #ifdef THERM_DEBUG
2108889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2109889facbeSLen Brown 			return 0;
2110889facbeSLen Brown 
2111889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
2112889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
2113889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2114889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2115889facbeSLen Brown #endif
2116889facbeSLen Brown 	}
2117889facbeSLen Brown 
2118889facbeSLen Brown 	return 0;
2119889facbeSLen Brown }
2120889facbeSLen Brown 
2121889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2122889facbeSLen Brown {
2123889facbeSLen Brown 	fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2124889facbeSLen Brown 		cpu, label,
2125889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
2126889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
2127889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2128889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
2129889facbeSLen Brown 
2130889facbeSLen Brown 	return;
2131889facbeSLen Brown }
2132889facbeSLen Brown 
2133889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2134889facbeSLen Brown {
2135889facbeSLen Brown 	unsigned long long msr;
2136889facbeSLen Brown 	int cpu;
2137889facbeSLen Brown 
2138889facbeSLen Brown 	if (!do_rapl)
2139889facbeSLen Brown 		return 0;
2140889facbeSLen Brown 
2141889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
2142889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2143889facbeSLen Brown 		return 0;
2144889facbeSLen Brown 
2145889facbeSLen Brown 	cpu = t->cpu_id;
2146889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2147889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2148889facbeSLen Brown 		return -1;
2149889facbeSLen Brown 	}
2150889facbeSLen Brown 
2151889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2152889facbeSLen Brown 		return -1;
2153889facbeSLen Brown 
2154d8af6f5fSLen Brown 	if (debug) {
2155889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2156889facbeSLen Brown 			"(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
2157144b44b1SLen Brown 			rapl_power_units, rapl_energy_units, rapl_time_units);
2158889facbeSLen Brown 	}
2159144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
2160144b44b1SLen Brown 
2161889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2162889facbeSLen Brown                 	return -5;
2163889facbeSLen Brown 
2164889facbeSLen Brown 
2165889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2166889facbeSLen Brown 			cpu, msr,
2167889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2168889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2169889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2170889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2171889facbeSLen Brown 
2172144b44b1SLen Brown 	}
2173144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
2174144b44b1SLen Brown 
2175889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2176889facbeSLen Brown 			return -9;
2177889facbeSLen Brown 
2178889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2179889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "": "UN");
2180889facbeSLen Brown 
2181889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
2182889facbeSLen Brown 		fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2183889facbeSLen Brown 			cpu,
2184889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
2185889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
2186889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2187889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
2188889facbeSLen Brown 	}
2189889facbeSLen Brown 
21900b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
2191889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2192889facbeSLen Brown                 	return -6;
2193889facbeSLen Brown 
2194889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2195889facbeSLen Brown 			cpu, msr,
2196889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2197889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2198889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2199889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
22000b2bb692SLen Brown 	}
22010b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
2202889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2203889facbeSLen Brown 			return -9;
2204889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2205889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "": "UN");
2206889facbeSLen Brown 
2207889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
2208889facbeSLen Brown 	}
2209144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
2210d8af6f5fSLen Brown 		if (debug) {
2211889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2212889facbeSLen Brown 				return -7;
2213889facbeSLen Brown 
2214889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
2215144b44b1SLen Brown 		}
2216144b44b1SLen Brown 	}
2217144b44b1SLen Brown 	if (do_rapl & RAPL_CORES) {
2218d8af6f5fSLen Brown 		if (debug) {
2219889facbeSLen Brown 
2220889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2221889facbeSLen Brown 				return -9;
2222889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2223889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
2224889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "Cores Limit");
2225889facbeSLen Brown 		}
2226889facbeSLen Brown 	}
2227889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
2228d8af6f5fSLen Brown 		if (debug) {
2229889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2230889facbeSLen Brown 				return -8;
2231889facbeSLen Brown 
2232889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2233889facbeSLen Brown 
2234889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2235889facbeSLen Brown 				return -9;
2236889facbeSLen Brown 			fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2237889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
2238889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "GFX Limit");
2239889facbeSLen Brown 		}
2240889facbeSLen Brown 	}
2241889facbeSLen Brown 	return 0;
2242889facbeSLen Brown }
2243889facbeSLen Brown 
2244d7899447SLen Brown /*
2245d7899447SLen Brown  * SNB adds support for additional MSRs:
2246d7899447SLen Brown  *
2247d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
2248d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
2249d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
2250d7899447SLen Brown  */
2251103a8feaSLen Brown 
2252d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
2253103a8feaSLen Brown {
2254103a8feaSLen Brown 	if (!genuine_intel)
2255103a8feaSLen Brown 		return 0;
2256103a8feaSLen Brown 
2257103a8feaSLen Brown 	switch (model) {
2258103a8feaSLen Brown 	case 0x2A:
2259103a8feaSLen Brown 	case 0x2D:
2260650a37f3SLen Brown 	case 0x3A:	/* IVB */
22611300651bSLen Brown 	case 0x3E:	/* IVB Xeon */
226270b43400SLen Brown 	case 0x3C:	/* HSW */
226370b43400SLen Brown 	case 0x3F:	/* HSW */
226470b43400SLen Brown 	case 0x45:	/* HSW */
2265149c2319SLen Brown 	case 0x46:	/* HSW */
22664e8e863fSLen Brown 	case 0x3D:	/* BDW */
226748a0631cSLen Brown 	case 0x47:	/* BDW */
22684e8e863fSLen Brown 	case 0x4F:	/* BDX */
22694e8e863fSLen Brown 	case 0x56:	/* BDX-DE */
22700b2bb692SLen Brown 	case 0x4E:	/* SKL */
22710b2bb692SLen Brown 	case 0x5E:	/* SKL */
2272103a8feaSLen Brown 		return 1;
2273103a8feaSLen Brown 	}
2274103a8feaSLen Brown 	return 0;
2275103a8feaSLen Brown }
2276103a8feaSLen Brown 
2277d7899447SLen Brown /*
2278d7899447SLen Brown  * HSW adds support for additional MSRs:
2279d7899447SLen Brown  *
2280d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY            0x00000630
2281d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY            0x00000631
2282d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY           0x00000632
2283d7899447SLen Brown  */
2284d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
2285ca58710fSKristen Carlson Accardi {
2286ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
2287ca58710fSKristen Carlson Accardi 		return 0;
2288ca58710fSKristen Carlson Accardi 
2289ca58710fSKristen Carlson Accardi 	switch (model) {
22904e8e863fSLen Brown 	case 0x45:	/* HSW */
22914e8e863fSLen Brown 	case 0x3D:	/* BDW */
22920b2bb692SLen Brown 	case 0x4E:	/* SKL */
22930b2bb692SLen Brown 	case 0x5E:	/* SKL */
2294ca58710fSKristen Carlson Accardi 		return 1;
2295ca58710fSKristen Carlson Accardi 	}
2296ca58710fSKristen Carlson Accardi 	return 0;
2297ca58710fSKristen Carlson Accardi }
2298ca58710fSKristen Carlson Accardi 
22990b2bb692SLen Brown /*
23000b2bb692SLen Brown  * SKL adds support for additional MSRS:
23010b2bb692SLen Brown  *
23020b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
23030b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
23040b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
23050b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
23060b2bb692SLen Brown  */
23070b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
23080b2bb692SLen Brown {
23090b2bb692SLen Brown 	if (!genuine_intel)
23100b2bb692SLen Brown 		return 0;
23110b2bb692SLen Brown 
23120b2bb692SLen Brown 	switch (model) {
23130b2bb692SLen Brown 	case 0x4E:	/* SKL */
23140b2bb692SLen Brown 	case 0x5E:	/* SKL */
23150b2bb692SLen Brown 		return 1;
23160b2bb692SLen Brown 	}
23170b2bb692SLen Brown 	return 0;
23180b2bb692SLen Brown }
23190b2bb692SLen Brown 
23200b2bb692SLen Brown 
2321ca58710fSKristen Carlson Accardi 
2322144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
2323144b44b1SLen Brown {
2324144b44b1SLen Brown 	if (!genuine_intel)
2325144b44b1SLen Brown 		return 0;
2326144b44b1SLen Brown 	switch (model) {
2327144b44b1SLen Brown 	case 0x37:	/* BYT */
2328144b44b1SLen Brown 	case 0x4D:	/* AVN */
2329144b44b1SLen Brown 		return 1;
2330144b44b1SLen Brown 	}
2331144b44b1SLen Brown 	return 0;
2332144b44b1SLen Brown }
2333144b44b1SLen Brown 
2334144b44b1SLen Brown #define SLM_BCLK_FREQS 5
2335144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2336144b44b1SLen Brown 
2337144b44b1SLen Brown double slm_bclk(void)
2338144b44b1SLen Brown {
2339144b44b1SLen Brown 	unsigned long long msr = 3;
2340144b44b1SLen Brown 	unsigned int i;
2341144b44b1SLen Brown 	double freq;
2342144b44b1SLen Brown 
2343144b44b1SLen Brown 	if (get_msr(0, MSR_FSB_FREQ, &msr))
2344144b44b1SLen Brown 		fprintf(stderr, "SLM BCLK: unknown\n");
2345144b44b1SLen Brown 
2346144b44b1SLen Brown 	i = msr & 0xf;
2347144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
2348144b44b1SLen Brown 		fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2349144b44b1SLen Brown 		msr = 3;
2350144b44b1SLen Brown 	}
2351144b44b1SLen Brown 	freq = slm_freq_table[i];
2352144b44b1SLen Brown 
2353144b44b1SLen Brown 	fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2354144b44b1SLen Brown 
2355144b44b1SLen Brown 	return freq;
2356144b44b1SLen Brown }
2357144b44b1SLen Brown 
2358103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
2359103a8feaSLen Brown {
2360d7899447SLen Brown 	if (has_snb_msrs(family, model))
2361103a8feaSLen Brown 		return 100.00;
2362144b44b1SLen Brown 	else if (is_slm(family, model))
2363144b44b1SLen Brown 		return slm_bclk();
2364103a8feaSLen Brown 	else
2365103a8feaSLen Brown 		return 133.33;
2366103a8feaSLen Brown }
2367103a8feaSLen Brown 
2368889facbeSLen Brown /*
2369889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2370889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
2371889facbeSLen Brown  * This is usually equal to tjMax.
2372889facbeSLen Brown  *
2373889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
2374889facbeSLen Brown  * but also allow cmdline over-ride with -T.
2375889facbeSLen Brown  *
2376889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
2377889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
2378889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2379889facbeSLen Brown  */
2380889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2381889facbeSLen Brown {
2382889facbeSLen Brown 	unsigned long long msr;
2383889facbeSLen Brown 	unsigned int target_c_local;
2384889facbeSLen Brown 	int cpu;
2385889facbeSLen Brown 
2386889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
2387889facbeSLen Brown 	if (!(do_dts || do_ptm))
2388889facbeSLen Brown 		return 0;
2389889facbeSLen Brown 
2390889facbeSLen Brown 	/* this is a per-package concept */
2391889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2392889facbeSLen Brown 		return 0;
2393889facbeSLen Brown 
2394889facbeSLen Brown 	cpu = t->cpu_id;
2395889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2396889facbeSLen Brown 		fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2397889facbeSLen Brown 		return -1;
2398889facbeSLen Brown 	}
2399889facbeSLen Brown 
2400889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
2401889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
2402889facbeSLen Brown 		fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2403889facbeSLen Brown 			cpu, tcc_activation_temp);
2404889facbeSLen Brown 		return 0;
2405889facbeSLen Brown 	}
2406889facbeSLen Brown 
2407889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
2408d7899447SLen Brown 	if (!do_nhm_platform_info)
2409889facbeSLen Brown 		goto guess;
2410889facbeSLen Brown 
2411889facbeSLen Brown 	if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2412889facbeSLen Brown 		goto guess;
2413889facbeSLen Brown 
24143482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
2415889facbeSLen Brown 
2416d8af6f5fSLen Brown 	if (debug)
2417889facbeSLen Brown 		fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2418889facbeSLen Brown 			cpu, msr, target_c_local);
2419889facbeSLen Brown 
24203482124aSJean Delvare 	if (!target_c_local)
2421889facbeSLen Brown 		goto guess;
2422889facbeSLen Brown 
2423889facbeSLen Brown 	tcc_activation_temp = target_c_local;
2424889facbeSLen Brown 
2425889facbeSLen Brown 	return 0;
2426889facbeSLen Brown 
2427889facbeSLen Brown guess:
2428889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
2429889facbeSLen Brown 	fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2430889facbeSLen Brown 		cpu, tcc_activation_temp);
2431889facbeSLen Brown 
2432889facbeSLen Brown 	return 0;
2433889facbeSLen Brown }
2434fcd17211SLen Brown void process_cpuid()
2435103a8feaSLen Brown {
2436103a8feaSLen Brown 	unsigned int eax, ebx, ecx, edx, max_level;
2437103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
2438103a8feaSLen Brown 
2439103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
2440103a8feaSLen Brown 
24412b92865eSJosh Triplett 	__get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2442103a8feaSLen Brown 
2443103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2444103a8feaSLen Brown 		genuine_intel = 1;
2445103a8feaSLen Brown 
2446d8af6f5fSLen Brown 	if (debug)
2447889facbeSLen Brown 		fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2448103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
2449103a8feaSLen Brown 
24502b92865eSJosh Triplett 	__get_cpuid(1, &fms, &ebx, &ecx, &edx);
2451103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
2452103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
2453103a8feaSLen Brown 	stepping = fms & 0xf;
2454103a8feaSLen Brown 	if (family == 6 || family == 0xf)
2455103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
2456103a8feaSLen Brown 
2457d8af6f5fSLen Brown 	if (debug)
2458103a8feaSLen Brown 		fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2459103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
2460103a8feaSLen Brown 
2461b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
2462b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
2463103a8feaSLen Brown 
2464103a8feaSLen Brown 	/*
2465103a8feaSLen Brown 	 * check max extended function levels of CPUID.
2466103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
2467103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
2468103a8feaSLen Brown 	 */
2469103a8feaSLen Brown 	ebx = ecx = edx = 0;
24702b92865eSJosh Triplett 	__get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
2471103a8feaSLen Brown 
2472d7899447SLen Brown 	if (max_level >= 0x80000007) {
2473103a8feaSLen Brown 
2474103a8feaSLen Brown 		/*
2475103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2476103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
2477103a8feaSLen Brown 		 */
24782b92865eSJosh Triplett 		__get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
24798209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
2480d7899447SLen Brown 	}
2481103a8feaSLen Brown 
2482103a8feaSLen Brown 	/*
2483103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2484103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
2485103a8feaSLen Brown 	 */
2486103a8feaSLen Brown 
24872b92865eSJosh Triplett 	__get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
24888209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
2489889facbeSLen Brown 	do_dts = eax & (1 << 0);
2490889facbeSLen Brown 	do_ptm = eax & (1 << 6);
2491889facbeSLen Brown 	has_epb = ecx & (1 << 3);
2492889facbeSLen Brown 
2493d8af6f5fSLen Brown 	if (debug)
2494a729617cSLen Brown 		fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2495a729617cSLen Brown 			has_aperf ? "" : "No ",
2496a729617cSLen Brown 			do_dts ? "" : "No ",
2497a729617cSLen Brown 			do_ptm ? "" : "No ",
2498a729617cSLen Brown 			has_epb ? "" : "No ");
2499103a8feaSLen Brown 
25008a5bdf41SLen Brown 	if (max_level > 0x15) {
25018a5bdf41SLen Brown 		unsigned int eax_crystal;
25028a5bdf41SLen Brown 		unsigned int ebx_tsc;
25038a5bdf41SLen Brown 
25048a5bdf41SLen Brown 		/*
25058a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
25068a5bdf41SLen Brown 		 */
25078a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
25088a5bdf41SLen Brown 		__get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
25098a5bdf41SLen Brown 
25108a5bdf41SLen Brown 		if (ebx_tsc != 0) {
25118a5bdf41SLen Brown 
25128a5bdf41SLen Brown 			if (debug && (ebx != 0))
25138a5bdf41SLen Brown 				fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
25148a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
25158a5bdf41SLen Brown 
25168a5bdf41SLen Brown 			if (crystal_hz == 0)
25178a5bdf41SLen Brown 				switch(model) {
25188a5bdf41SLen Brown 				case 0x4E:	/* SKL */
25198a5bdf41SLen Brown 				case 0x5E:	/* SKL */
25208a5bdf41SLen Brown 					crystal_hz = 24000000;	/* 24 MHz */
25218a5bdf41SLen Brown 					break;
25228a5bdf41SLen Brown 				default:
25238a5bdf41SLen Brown 					crystal_hz = 0;
25248a5bdf41SLen Brown 			}
25258a5bdf41SLen Brown 
25268a5bdf41SLen Brown 			if (crystal_hz) {
25278a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
25288a5bdf41SLen Brown 				if (debug)
25298a5bdf41SLen Brown 					fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
25308a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
25318a5bdf41SLen Brown 			}
25328a5bdf41SLen Brown 		}
25338a5bdf41SLen Brown 	}
25348a5bdf41SLen Brown 
2535ee7e38e3SLen Brown 	do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
2536d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
2537ee7e38e3SLen Brown 	do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2538ee7e38e3SLen Brown 	do_pc3 = (pkg_cstate_limit >= PCL__3);
2539ee7e38e3SLen Brown 	do_pc6 = (pkg_cstate_limit >= PCL__6);
2540ee7e38e3SLen Brown 	do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
2541d7899447SLen Brown 	do_c8_c9_c10 = has_hsw_msrs(family, model);
25420b2bb692SLen Brown 	do_skl_residency = has_skl_msrs(family, model);
2543144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
2544103a8feaSLen Brown 	bclk = discover_bclk(family, model);
2545103a8feaSLen Brown 
2546889facbeSLen Brown 	rapl_probe(family, model);
25473a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
2548889facbeSLen Brown 
2549fcd17211SLen Brown 	if (debug)
2550fcd17211SLen Brown 		dump_cstate_pstate_config_info();
2551fcd17211SLen Brown 
2552889facbeSLen Brown 	return;
2553103a8feaSLen Brown }
2554103a8feaSLen Brown 
2555d8af6f5fSLen Brown void help()
2556103a8feaSLen Brown {
2557d8af6f5fSLen Brown 	fprintf(stderr,
2558d8af6f5fSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2559d8af6f5fSLen Brown 	"\n"
2560d8af6f5fSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
2561d8af6f5fSLen Brown 	"when COMMAND completes.\n"
2562d8af6f5fSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2563d8af6f5fSLen Brown 	"to print statistics, until interrupted.\n"
2564d8af6f5fSLen Brown 	"--debug	run in \"debug\" mode\n"
2565d8af6f5fSLen Brown 	"--interval sec	Override default 5-second measurement interval\n"
2566d8af6f5fSLen Brown 	"--help		print this help message\n"
2567d8af6f5fSLen Brown 	"--counter msr	print 32-bit counter at address \"msr\"\n"
2568d8af6f5fSLen Brown 	"--Counter msr	print 64-bit Counter at address \"msr\"\n"
2569d8af6f5fSLen Brown 	"--msr msr	print 32-bit value at address \"msr\"\n"
2570d8af6f5fSLen Brown 	"--MSR msr	print 64-bit Value at address \"msr\"\n"
2571d8af6f5fSLen Brown 	"--version	print version information\n"
2572d8af6f5fSLen Brown 	"\n"
2573d8af6f5fSLen Brown 	"For more help, run \"man turbostat\"\n");
2574103a8feaSLen Brown }
2575103a8feaSLen Brown 
2576103a8feaSLen Brown 
2577103a8feaSLen Brown /*
2578103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
2579103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
2580103a8feaSLen Brown  */
2581103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
2582103a8feaSLen Brown {
2583103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
2584103a8feaSLen Brown 		return 1;
2585103a8feaSLen Brown 	else
2586103a8feaSLen Brown 		return 0;
2587103a8feaSLen Brown }
2588103a8feaSLen Brown 
2589103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
2590103a8feaSLen Brown {
2591103a8feaSLen Brown 	return 0;
2592103a8feaSLen Brown }
2593103a8feaSLen Brown 
2594c98d5d94SLen Brown void topology_probe()
2595c98d5d94SLen Brown {
2596c98d5d94SLen Brown 	int i;
2597c98d5d94SLen Brown 	int max_core_id = 0;
2598c98d5d94SLen Brown 	int max_package_id = 0;
2599c98d5d94SLen Brown 	int max_siblings = 0;
2600c98d5d94SLen Brown 	struct cpu_topology {
2601c98d5d94SLen Brown 		int core_id;
2602c98d5d94SLen Brown 		int physical_package_id;
2603c98d5d94SLen Brown 	} *cpus;
2604c98d5d94SLen Brown 
2605c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
2606c98d5d94SLen Brown 	topo.num_cpus = 0;
2607c98d5d94SLen Brown 	topo.max_cpu_num = 0;
2608c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
2609c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
2610c98d5d94SLen Brown 		show_cpu = 1;
2611c98d5d94SLen Brown 
2612d8af6f5fSLen Brown 	if (debug > 1)
2613c98d5d94SLen Brown 		fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2614c98d5d94SLen Brown 
2615c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
2616b2c95d90SJosh Triplett 	if (cpus == NULL)
2617b2c95d90SJosh Triplett 		err(1, "calloc cpus");
2618c98d5d94SLen Brown 
2619c98d5d94SLen Brown 	/*
2620c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
2621c98d5d94SLen Brown 	 */
2622c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2623b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
2624b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
2625c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2626c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2627c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
2628c98d5d94SLen Brown 
2629c98d5d94SLen Brown 	/*
2630c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
2631c98d5d94SLen Brown 	 */
2632c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2633b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
2634b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
2635c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2636c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2637c98d5d94SLen Brown 
2638c98d5d94SLen Brown 
2639c98d5d94SLen Brown 	/*
2640c98d5d94SLen Brown 	 * For online cpus
2641c98d5d94SLen Brown 	 * find max_core_id, max_package_id
2642c98d5d94SLen Brown 	 */
2643c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
2644c98d5d94SLen Brown 		int siblings;
2645c98d5d94SLen Brown 
2646c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
2647d8af6f5fSLen Brown 			if (debug > 1)
2648c98d5d94SLen Brown 				fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2649c98d5d94SLen Brown 			continue;
2650c98d5d94SLen Brown 		}
2651c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
2652c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
2653c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
2654c98d5d94SLen Brown 
2655c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
2656c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
2657c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
2658c98d5d94SLen Brown 
2659c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
2660c98d5d94SLen Brown 		if (siblings > max_siblings)
2661c98d5d94SLen Brown 			max_siblings = siblings;
2662d8af6f5fSLen Brown 		if (debug > 1)
2663c98d5d94SLen Brown 			fprintf(stderr, "cpu %d pkg %d core %d\n",
2664c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
2665c98d5d94SLen Brown 	}
2666c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
2667d8af6f5fSLen Brown 	if (debug > 1)
2668c98d5d94SLen Brown 		fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2669c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
26701cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_cores_per_pkg > 1)
2671c98d5d94SLen Brown 		show_core = 1;
2672c98d5d94SLen Brown 
2673c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
2674d8af6f5fSLen Brown 	if (debug > 1)
2675c98d5d94SLen Brown 		fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2676c98d5d94SLen Brown 			max_package_id, topo.num_packages);
26771cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_packages > 1)
2678c98d5d94SLen Brown 		show_pkg = 1;
2679c98d5d94SLen Brown 
2680c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
2681d8af6f5fSLen Brown 	if (debug > 1)
2682c98d5d94SLen Brown 		fprintf(stderr, "max_siblings %d\n", max_siblings);
2683c98d5d94SLen Brown 
2684c98d5d94SLen Brown 	free(cpus);
2685c98d5d94SLen Brown }
2686c98d5d94SLen Brown 
2687c98d5d94SLen Brown void
2688c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2689c98d5d94SLen Brown {
2690c98d5d94SLen Brown 	int i;
2691c98d5d94SLen Brown 
2692c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2693c98d5d94SLen Brown 		topo.num_packages, sizeof(struct thread_data));
2694c98d5d94SLen Brown 	if (*t == NULL)
2695c98d5d94SLen Brown 		goto error;
2696c98d5d94SLen Brown 
2697c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
2698c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
2699c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
2700c98d5d94SLen Brown 
2701c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2702c98d5d94SLen Brown 		sizeof(struct core_data));
2703c98d5d94SLen Brown 	if (*c == NULL)
2704c98d5d94SLen Brown 		goto error;
2705c98d5d94SLen Brown 
2706c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2707c98d5d94SLen Brown 		(*c)[i].core_id = -1;
2708c98d5d94SLen Brown 
2709c98d5d94SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
2710c98d5d94SLen Brown 	if (*p == NULL)
2711c98d5d94SLen Brown 		goto error;
2712c98d5d94SLen Brown 
2713c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
2714c98d5d94SLen Brown 		(*p)[i].package_id = i;
2715c98d5d94SLen Brown 
2716c98d5d94SLen Brown 	return;
2717c98d5d94SLen Brown error:
2718b2c95d90SJosh Triplett 	err(1, "calloc counters");
2719c98d5d94SLen Brown }
2720c98d5d94SLen Brown /*
2721c98d5d94SLen Brown  * init_counter()
2722c98d5d94SLen Brown  *
2723c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
2724c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2725c98d5d94SLen Brown  *
2726c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
2727c98d5d94SLen Brown  */
2728c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2729c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
2730c98d5d94SLen Brown 	int pkg_num, int cpu_id)
2731c98d5d94SLen Brown {
2732c98d5d94SLen Brown 	struct thread_data *t;
2733c98d5d94SLen Brown 	struct core_data *c;
2734c98d5d94SLen Brown 	struct pkg_data *p;
2735c98d5d94SLen Brown 
2736c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2737c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
2738c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
2739c98d5d94SLen Brown 
2740c98d5d94SLen Brown 	t->cpu_id = cpu_id;
2741c98d5d94SLen Brown 	if (thread_num == 0) {
2742c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2743c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
2744c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2745c98d5d94SLen Brown 	}
2746c98d5d94SLen Brown 
2747c98d5d94SLen Brown 	c->core_id = core_num;
2748c98d5d94SLen Brown 	p->package_id = pkg_num;
2749c98d5d94SLen Brown }
2750c98d5d94SLen Brown 
2751c98d5d94SLen Brown 
2752c98d5d94SLen Brown int initialize_counters(int cpu_id)
2753c98d5d94SLen Brown {
2754c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
2755c98d5d94SLen Brown 
2756c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
2757c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
2758c98d5d94SLen Brown 
2759c98d5d94SLen Brown 	if (cpu_is_first_sibling_in_core(cpu_id)) {
2760c98d5d94SLen Brown 		my_thread_id = 0;
2761c98d5d94SLen Brown 		topo.num_cores++;
2762c98d5d94SLen Brown 	} else {
2763c98d5d94SLen Brown 		my_thread_id = 1;
2764c98d5d94SLen Brown 	}
2765c98d5d94SLen Brown 
2766c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2767c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2768c98d5d94SLen Brown 	return 0;
2769c98d5d94SLen Brown }
2770c98d5d94SLen Brown 
2771c98d5d94SLen Brown void allocate_output_buffer()
2772c98d5d94SLen Brown {
27733b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
2774c98d5d94SLen Brown 	outp = output_buffer;
2775b2c95d90SJosh Triplett 	if (outp == NULL)
2776b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
2777c98d5d94SLen Brown }
2778c98d5d94SLen Brown 
2779c98d5d94SLen Brown void setup_all_buffers(void)
2780c98d5d94SLen Brown {
2781c98d5d94SLen Brown 	topology_probe();
2782c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
2783c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
2784c98d5d94SLen Brown 	allocate_output_buffer();
2785c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
2786c98d5d94SLen Brown }
27873b4d5c7fSAndy Shevchenko 
2788103a8feaSLen Brown void turbostat_init()
2789103a8feaSLen Brown {
2790103a8feaSLen Brown 	check_dev_msr();
279198481e79SLen Brown 	check_permissions();
2792fcd17211SLen Brown 	process_cpuid();
2793103a8feaSLen Brown 
2794c98d5d94SLen Brown 	setup_all_buffers();
2795103a8feaSLen Brown 
2796d8af6f5fSLen Brown 	if (debug)
2797889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
2798889facbeSLen Brown 
2799d8af6f5fSLen Brown 	if (debug)
28003a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
28013a9a941dSLen Brown 
2802d8af6f5fSLen Brown 	if (debug)
2803889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
2804889facbeSLen Brown 
2805889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
2806889facbeSLen Brown 
2807d8af6f5fSLen Brown 	if (debug)
2808889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
2809103a8feaSLen Brown }
2810103a8feaSLen Brown 
2811103a8feaSLen Brown int fork_it(char **argv)
2812103a8feaSLen Brown {
2813103a8feaSLen Brown 	pid_t child_pid;
2814d91bb17cSLen Brown 	int status;
2815d15cf7c1SLen Brown 
2816d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
2817d91bb17cSLen Brown 	if (status)
2818d91bb17cSLen Brown 		exit(status);
2819d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
2820d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2821103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2822103a8feaSLen Brown 
2823103a8feaSLen Brown 	child_pid = fork();
2824103a8feaSLen Brown 	if (!child_pid) {
2825103a8feaSLen Brown 		/* child */
2826103a8feaSLen Brown 		execvp(argv[0], argv);
2827103a8feaSLen Brown 	} else {
2828103a8feaSLen Brown 
2829103a8feaSLen Brown 		/* parent */
2830b2c95d90SJosh Triplett 		if (child_pid == -1)
2831b2c95d90SJosh Triplett 			err(1, "fork");
2832103a8feaSLen Brown 
2833103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
2834103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
2835b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
2836b2c95d90SJosh Triplett 			err(status, "waitpid");
2837103a8feaSLen Brown 	}
2838c98d5d94SLen Brown 	/*
2839c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
2840c98d5d94SLen Brown 	 * because re-starting is problematic when forking
2841c98d5d94SLen Brown 	 */
2842c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
2843103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
2844103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
2845c98d5d94SLen Brown 	for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2846c98d5d94SLen Brown 	compute_average(EVEN_COUNTERS);
2847c98d5d94SLen Brown 	format_all_counters(EVEN_COUNTERS);
2848c98d5d94SLen Brown 	flush_stderr();
2849103a8feaSLen Brown 
28506eab04a8SJustin P. Mattock 	fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2851103a8feaSLen Brown 
2852d91bb17cSLen Brown 	return status;
2853103a8feaSLen Brown }
2854103a8feaSLen Brown 
28553b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
28563b4d5c7fSAndy Shevchenko {
28573b4d5c7fSAndy Shevchenko 	int status;
28583b4d5c7fSAndy Shevchenko 
28593b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
28603b4d5c7fSAndy Shevchenko 	if (status)
28613b4d5c7fSAndy Shevchenko 		return status;
28623b4d5c7fSAndy Shevchenko 
28633b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
28643b4d5c7fSAndy Shevchenko 	if (status)
28653b4d5c7fSAndy Shevchenko 		return status;
28663b4d5c7fSAndy Shevchenko 
28673b4d5c7fSAndy Shevchenko 	flush_stdout();
28683b4d5c7fSAndy Shevchenko 
28693b4d5c7fSAndy Shevchenko 	return status;
28703b4d5c7fSAndy Shevchenko }
28713b4d5c7fSAndy Shevchenko 
2872d8af6f5fSLen Brown void print_version() {
2873*e9257f5fSLen Brown 	fprintf(stderr, "turbostat version 4.5 2 Apr, 2015"
2874d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
2875d8af6f5fSLen Brown }
2876d8af6f5fSLen Brown 
2877103a8feaSLen Brown void cmdline(int argc, char **argv)
2878103a8feaSLen Brown {
2879103a8feaSLen Brown 	int opt;
2880d8af6f5fSLen Brown 	int option_index = 0;
2881d8af6f5fSLen Brown 	static struct option long_options[] = {
2882d8af6f5fSLen Brown 		{"Counter",	required_argument,	0, 'C'},
2883d8af6f5fSLen Brown 		{"counter",	required_argument,	0, 'c'},
2884d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
2885d8af6f5fSLen Brown 		{"debug",	no_argument,		0, 'd'},
2886d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
2887d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
2888d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
2889d8af6f5fSLen Brown 		{"MSR",		required_argument,	0, 'M'},
2890d8af6f5fSLen Brown 		{"msr",		required_argument,	0, 'm'},
2891d8af6f5fSLen Brown 		{"Package",	no_argument,		0, 'p'},
2892d8af6f5fSLen Brown 		{"processor",	no_argument,		0, 'p'},
2893d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
2894d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
2895d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
2896d8af6f5fSLen Brown 		{0,		0,			0,  0 }
2897d8af6f5fSLen Brown 	};
2898103a8feaSLen Brown 
2899103a8feaSLen Brown 	progname = argv[0];
2900103a8feaSLen Brown 
2901d8af6f5fSLen Brown 	while ((opt = getopt_long_only(argc, argv, "C:c:Ddhi:JM:m:PpST:v",
2902d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
2903103a8feaSLen Brown 		switch (opt) {
2904d8af6f5fSLen Brown 		case 'C':
2905d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset64);
2906103a8feaSLen Brown 			break;
2907f9240813SLen Brown 		case 'c':
29088e180f3cSLen Brown 			sscanf(optarg, "%x", &extra_delta_offset32);
29098e180f3cSLen Brown 			break;
2910d8af6f5fSLen Brown 		case 'D':
2911d8af6f5fSLen Brown 			dump_only++;
29128e180f3cSLen Brown 			break;
2913d8af6f5fSLen Brown 		case 'd':
2914d8af6f5fSLen Brown 			debug++;
29152f32edf1SLen Brown 			break;
2916d8af6f5fSLen Brown 		case 'h':
2917d8af6f5fSLen Brown 		default:
2918d8af6f5fSLen Brown 			help();
2919d8af6f5fSLen Brown 			exit(1);
2920d8af6f5fSLen Brown 		case 'i':
2921d8af6f5fSLen Brown 			interval_sec = atoi(optarg);
2922889facbeSLen Brown 			break;
29235c56be9aSDirk Brandewie 		case 'J':
29245c56be9aSDirk Brandewie 			rapl_joules++;
29255c56be9aSDirk Brandewie 			break;
2926d8af6f5fSLen Brown 		case 'M':
2927d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_msr_offset64);
2928d8af6f5fSLen Brown 			break;
2929d8af6f5fSLen Brown 		case 'm':
2930d8af6f5fSLen Brown 			sscanf(optarg, "%x", &extra_msr_offset32);
2931d8af6f5fSLen Brown 			break;
2932d8af6f5fSLen Brown 		case 'P':
2933d8af6f5fSLen Brown 			show_pkg_only++;
2934d8af6f5fSLen Brown 			break;
2935d8af6f5fSLen Brown 		case 'p':
2936d8af6f5fSLen Brown 			show_core_only++;
2937d8af6f5fSLen Brown 			break;
2938d8af6f5fSLen Brown 		case 'S':
2939d8af6f5fSLen Brown 			summary_only++;
2940d8af6f5fSLen Brown 			break;
2941d8af6f5fSLen Brown 		case 'T':
2942d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
2943d8af6f5fSLen Brown 			break;
2944d8af6f5fSLen Brown 		case 'v':
2945d8af6f5fSLen Brown 			print_version();
2946d8af6f5fSLen Brown 			exit(0);
2947d8af6f5fSLen Brown 			break;
2948103a8feaSLen Brown 		}
2949103a8feaSLen Brown 	}
2950103a8feaSLen Brown }
2951103a8feaSLen Brown 
2952103a8feaSLen Brown int main(int argc, char **argv)
2953103a8feaSLen Brown {
2954103a8feaSLen Brown 	cmdline(argc, argv);
2955103a8feaSLen Brown 
2956d8af6f5fSLen Brown 	if (debug)
2957d8af6f5fSLen Brown 		print_version();
2958103a8feaSLen Brown 
2959103a8feaSLen Brown 	turbostat_init();
2960103a8feaSLen Brown 
29613b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
29623b4d5c7fSAndy Shevchenko 	if (dump_only)
29633b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
29643b4d5c7fSAndy Shevchenko 
2965103a8feaSLen Brown 	/*
2966103a8feaSLen Brown 	 * if any params left, it must be a command to fork
2967103a8feaSLen Brown 	 */
2968103a8feaSLen Brown 	if (argc - optind)
2969103a8feaSLen Brown 		return fork_it(argv + optind);
2970103a8feaSLen Brown 	else
2971103a8feaSLen Brown 		turbostat_loop();
2972103a8feaSLen Brown 
2973103a8feaSLen Brown 	return 0;
2974103a8feaSLen Brown }
2975