blob: 4490a776bbaeb9d9aaff28d50276f594f3c2c818 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
32#include <sys/resource.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <sys/time.h>
36#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050037#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038#include <dirent.h>
39#include <string.h>
40#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040041#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050042#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070043#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040044#include <linux/capability.h>
45#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040046
Len Brown103a8fe2010-10-22 23:53:03 -040047char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050048FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050049int *fd_percpu;
Len Brown2a0609c2016-02-12 22:44:48 -050050struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050051unsigned int debug;
52unsigned int rapl_joules;
53unsigned int summary_only;
54unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040055unsigned int do_nhm_cstates;
56unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070057unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050058unsigned int do_pc2;
59unsigned int do_pc3;
60unsigned int do_pc6;
61unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080062unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040063unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050064unsigned int do_slm_cstates;
65unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050067unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040068unsigned int do_irtl_snb;
69unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050070unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040071unsigned int genuine_intel;
72unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050073unsigned int do_nhm_platform_info;
Len Brown2f32edf2012-09-21 23:45:46 -040074unsigned int extra_msr_offset32;
75unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040076unsigned int extra_delta_offset32;
77unsigned int extra_delta_offset64;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020078unsigned int aperf_mperf_multiplier = 1;
Len Brown562a2d32016-02-26 23:48:05 -050079int do_irq = 1;
Len Brown1ed51012013-02-10 17:19:24 -050080int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040081double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040082double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040083unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040084double tsc_tweak = 1.0;
Len Brown103a8fe2010-10-22 23:53:03 -040085unsigned int show_pkg;
86unsigned int show_core;
87unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040088unsigned int show_pkg_only;
89unsigned int show_core_only;
90char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050091unsigned int do_rapl;
92unsigned int do_dts;
93unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050094unsigned int do_gfx_rc6_ms;
95unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050096unsigned int do_gfx_mhz;
97unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050098unsigned int tcc_activation_temp;
99unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -0500100double rapl_power_units, rapl_time_units;
101double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -0500102double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -0400103unsigned int do_core_perf_limit_reasons;
104unsigned int do_gfx_perf_limit_reasons;
105unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -0400106unsigned int crystal_hz;
107unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400108int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400109double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500110unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
111 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
112unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
113unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
114unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
115unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown889facb2012-11-08 00:48:57 -0500116
Len Browne6f9bb32013-12-03 02:19:19 -0500117#define RAPL_PKG (1 << 0)
118 /* 0x610 MSR_PKG_POWER_LIMIT */
119 /* 0x611 MSR_PKG_ENERGY_STATUS */
120#define RAPL_PKG_PERF_STATUS (1 << 1)
121 /* 0x613 MSR_PKG_PERF_STATUS */
122#define RAPL_PKG_POWER_INFO (1 << 2)
123 /* 0x614 MSR_PKG_POWER_INFO */
124
125#define RAPL_DRAM (1 << 3)
126 /* 0x618 MSR_DRAM_POWER_LIMIT */
127 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500128#define RAPL_DRAM_PERF_STATUS (1 << 4)
129 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400130#define RAPL_DRAM_POWER_INFO (1 << 5)
131 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500132
Jacob Pan91484942016-06-16 09:48:20 -0700133#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500134 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400135#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500136 /* 0x63a MSR_PP0_POLICY */
137
Len Brown0b2bb692015-03-26 00:50:30 -0400138#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500139 /* 0x640 MSR_PP1_POWER_LIMIT */
140 /* 0x641 MSR_PP1_ENERGY_STATUS */
141 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700142
143#define RAPL_CORES_ENERGY_STATUS (1 << 9)
144 /* 0x639 MSR_PP0_ENERGY_STATUS */
145#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500146#define TJMAX_DEFAULT 100
147
148#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400149
Len Brown388e9c82016-12-22 23:57:55 -0500150/*
151 * buffer size used by sscanf() for added column names
152 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
153 */
154#define NAME_BYTES 20
155
Len Brown103a8fe2010-10-22 23:53:03 -0400156int backwards_count;
157char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400158
Len Brownc98d5d92012-06-04 00:56:40 -0400159cpu_set_t *cpu_present_set, *cpu_affinity_set;
160size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400161
Len Brownc98d5d92012-06-04 00:56:40 -0400162struct thread_data {
163 unsigned long long tsc;
164 unsigned long long aperf;
165 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500166 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400167 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400168 unsigned long long extra_delta64;
169 unsigned long long extra_msr32;
170 unsigned long long extra_delta32;
Len Brown562a2d32016-02-26 23:48:05 -0500171 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500172 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400173 unsigned int cpu_id;
174 unsigned int flags;
175#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
176#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown388e9c82016-12-22 23:57:55 -0500177 unsigned long long counter[1];
Len Brownc98d5d92012-06-04 00:56:40 -0400178} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400179
Len Brownc98d5d92012-06-04 00:56:40 -0400180struct core_data {
181 unsigned long long c3;
182 unsigned long long c6;
183 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500184 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400185 unsigned int core_id;
Len Brown388e9c82016-12-22 23:57:55 -0500186 unsigned long long counter[1];
Len Brownc98d5d92012-06-04 00:56:40 -0400187} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400188
Len Brownc98d5d92012-06-04 00:56:40 -0400189struct pkg_data {
190 unsigned long long pc2;
191 unsigned long long pc3;
192 unsigned long long pc6;
193 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800194 unsigned long long pc8;
195 unsigned long long pc9;
196 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400197 unsigned long long pkg_wtd_core_c0;
198 unsigned long long pkg_any_core_c0;
199 unsigned long long pkg_any_gfxe_c0;
200 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400201 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500202 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400203 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500204 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
205 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
206 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
207 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
208 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
209 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
210 unsigned int pkg_temp_c;
Len Brown388e9c82016-12-22 23:57:55 -0500211 unsigned long long counter[1];
Len Brownc98d5d92012-06-04 00:56:40 -0400212} *package_even, *package_odd;
213
214#define ODD_COUNTERS thread_odd, core_odd, package_odd
215#define EVEN_COUNTERS thread_even, core_even, package_even
216
217#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
218 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
219 topo.num_threads_per_core + \
220 (core_no) * topo.num_threads_per_core + (thread_no))
221#define GET_CORE(core_base, core_no, pkg_no) \
222 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
223#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
224
Len Brown388e9c82016-12-22 23:57:55 -0500225enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
226enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
227enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
228
229struct msr_counter {
230 unsigned int msr_num;
231 char name[NAME_BYTES];
232 unsigned int width;
233 enum counter_type type;
234 enum counter_format format;
235 struct msr_counter *next;
236};
237
238struct sys_counters {
239 unsigned int thread_counter_bytes;
240 unsigned int core_counter_bytes;
241 unsigned int package_counter_bytes;
242 struct msr_counter *tp;
243 struct msr_counter *cp;
244 struct msr_counter *pp;
245} sys;
246
Len Brownc98d5d92012-06-04 00:56:40 -0400247struct system_summary {
248 struct thread_data threads;
249 struct core_data cores;
250 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500251} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400252
253
254struct topo_params {
255 int num_packages;
256 int num_cpus;
257 int num_cores;
258 int max_cpu_num;
259 int num_cores_per_pkg;
260 int num_threads_per_core;
261} topo;
262
263struct timeval tv_even, tv_odd, tv_delta;
264
Len Brown562a2d32016-02-26 23:48:05 -0500265int *irq_column_2_cpu; /* /proc/interrupts column numbers */
266int *irqs_per_cpu; /* indexed by cpu_num */
267
Len Brownc98d5d92012-06-04 00:56:40 -0400268void setup_all_buffers(void);
269
270int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400271{
Len Brownc98d5d92012-06-04 00:56:40 -0400272 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400273}
Len Brown88c32812012-03-29 21:44:40 -0400274/*
Len Brownc98d5d92012-06-04 00:56:40 -0400275 * run func(thread, core, package) in topology order
276 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400277 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400278
Len Brownc98d5d92012-06-04 00:56:40 -0400279int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
280 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400281{
Len Brownc98d5d92012-06-04 00:56:40 -0400282 int retval, pkg_no, core_no, thread_no;
283
284 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
285 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
286 for (thread_no = 0; thread_no <
287 topo.num_threads_per_core; ++thread_no) {
288 struct thread_data *t;
289 struct core_data *c;
290 struct pkg_data *p;
291
292 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
293
294 if (cpu_is_not_present(t->cpu_id))
295 continue;
296
297 c = GET_CORE(core_base, core_no, pkg_no);
298 p = GET_PKG(pkg_base, pkg_no);
299
300 retval = func(t, c, p);
301 if (retval)
302 return retval;
303 }
304 }
305 }
306 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400307}
308
309int cpu_migrate(int cpu)
310{
Len Brownc98d5d92012-06-04 00:56:40 -0400311 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
312 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
313 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400314 return -1;
315 else
316 return 0;
317}
Len Brown36229892016-02-26 20:51:02 -0500318int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400319{
Len Brown103a8fe2010-10-22 23:53:03 -0400320 char pathname[32];
321 int fd;
322
Len Brown36229892016-02-26 20:51:02 -0500323 fd = fd_percpu[cpu];
324
325 if (fd)
326 return fd;
327
Len Brown103a8fe2010-10-22 23:53:03 -0400328 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
329 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400330 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400331 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
Len Brown103a8fe2010-10-22 23:53:03 -0400332
Len Brown36229892016-02-26 20:51:02 -0500333 fd_percpu[cpu] = fd;
334
335 return fd;
336}
337
338int get_msr(int cpu, off_t offset, unsigned long long *msr)
339{
340 ssize_t retval;
341
342 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400343
Len Brown98481e72014-08-15 00:36:50 -0400344 if (retval != sizeof *msr)
Len Brown36229892016-02-26 20:51:02 -0500345 err(-1, "msr %d offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400346
347 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400348}
349
Len Brownfc04cc62014-02-06 00:55:19 -0500350/*
351 * Example Format w/ field column widths:
352 *
Len Brown388e9c82016-12-22 23:57:55 -0500353 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz IRQ SMI Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 ThreadC CoreTmp CoreCnt PkgTmp GFXMHz Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt PkgCnt
Len Brown562a2d32016-02-26 23:48:05 -0500354 * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500355 */
356
Len Browna829eb42011-02-10 23:36:34 -0500357void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400358{
Len Brown388e9c82016-12-22 23:57:55 -0500359 struct msr_counter *mp;
360
Len Brown103a8fe2010-10-22 23:53:03 -0400361 if (show_pkg)
Len Brown3d109de2016-04-22 23:24:27 -0400362 outp += sprintf(outp, "\tPackage");
Len Brown103a8fe2010-10-22 23:53:03 -0400363 if (show_core)
Len Brown3d109de2016-04-22 23:24:27 -0400364 outp += sprintf(outp, "\tCore");
Len Brown103a8fe2010-10-22 23:53:03 -0400365 if (show_cpu)
Len Brown3d109de2016-04-22 23:24:27 -0400366 outp += sprintf(outp, "\tCPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400367 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400368 outp += sprintf(outp, "\tAvg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500369 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400370 outp += sprintf(outp, "\tBusy%%");
Len Brownfc04cc62014-02-06 00:55:19 -0500371 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400372 outp += sprintf(outp, "\tBzy_MHz");
373 outp += sprintf(outp, "\tTSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500374
Len Brown8e180f32012-09-22 01:25:08 -0400375 if (extra_delta_offset32)
Len Brown3d109de2016-04-22 23:24:27 -0400376 outp += sprintf(outp, "\tcount 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400377 if (extra_delta_offset64)
Len Brown3d109de2016-04-22 23:24:27 -0400378 outp += sprintf(outp, "\tCOUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400379 if (extra_msr_offset32)
Len Brown5cc63232016-12-01 21:14:38 -0500380 outp += sprintf(outp, "\t MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400381 if (extra_msr_offset64)
Len Brown3d109de2016-04-22 23:24:27 -0400382 outp += sprintf(outp, "\tMSR 0x%03X", extra_msr_offset64);
Len Brown1cc21f72015-02-23 00:34:57 -0500383
384 if (!debug)
385 goto done;
386
Len Brown562a2d32016-02-26 23:48:05 -0500387 if (do_irq)
Len Brown3d109de2016-04-22 23:24:27 -0400388 outp += sprintf(outp, "\tIRQ");
Len Brown1cc21f72015-02-23 00:34:57 -0500389 if (do_smi)
Len Brown3d109de2016-04-22 23:24:27 -0400390 outp += sprintf(outp, "\tSMI");
Len Brown1cc21f72015-02-23 00:34:57 -0500391
Len Brown103a8fe2010-10-22 23:53:03 -0400392 if (do_nhm_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400393 outp += sprintf(outp, "\tCPU%%c1");
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700394 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400395 outp += sprintf(outp, "\tCPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400396 if (do_nhm_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400397 outp += sprintf(outp, "\tCPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400398 if (do_snb_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400399 outp += sprintf(outp, "\tCPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500400
Len Brown388e9c82016-12-22 23:57:55 -0500401 for (mp = sys.tp; mp; mp = mp->next) {
402 if (mp->format == FORMAT_RAW) {
403 if (mp->width == 64)
404 outp += sprintf(outp, "\t%18.18s", mp->name);
405 else
406 outp += sprintf(outp, "\t%10.10s", mp->name);
407 } else {
408 outp += sprintf(outp, "\t%-7.7s", mp->name);
409 }
410 }
411
Len Brown889facb2012-11-08 00:48:57 -0500412 if (do_dts)
Len Brown3d109de2016-04-22 23:24:27 -0400413 outp += sprintf(outp, "\tCoreTmp");
Len Brown388e9c82016-12-22 23:57:55 -0500414
415 for (mp = sys.cp; mp; mp = mp->next) {
416 if (mp->format == FORMAT_RAW) {
417 if (mp->width == 64)
418 outp += sprintf(outp, "\t%18.18s", mp->name);
419 else
420 outp += sprintf(outp, "\t%10.10s", mp->name);
421 } else {
422 outp += sprintf(outp, "\t%-7.7s", mp->name);
423 }
424 }
425
Len Brown889facb2012-11-08 00:48:57 -0500426 if (do_ptm)
Len Brown3d109de2016-04-22 23:24:27 -0400427 outp += sprintf(outp, "\tPkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500428
Len Brownfdf676e2016-02-27 01:28:12 -0500429 if (do_gfx_rc6_ms)
Len Brown3d109de2016-04-22 23:24:27 -0400430 outp += sprintf(outp, "\tGFX%%rc6");
Len Brownfdf676e2016-02-27 01:28:12 -0500431
Len Brown27d47352016-02-27 00:37:54 -0500432 if (do_gfx_mhz)
Len Brown3d109de2016-04-22 23:24:27 -0400433 outp += sprintf(outp, "\tGFXMHz");
Len Brown27d47352016-02-27 00:37:54 -0500434
Len Brown0b2bb692015-03-26 00:50:30 -0400435 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400436 outp += sprintf(outp, "\tTotl%%C0");
437 outp += sprintf(outp, "\tAny%%C0");
438 outp += sprintf(outp, "\tGFX%%C0");
439 outp += sprintf(outp, "\tCPUGFX%%");
Len Brown0b2bb692015-03-26 00:50:30 -0400440 }
441
Len Brownee7e38e2015-02-09 23:39:45 -0500442 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400443 outp += sprintf(outp, "\tPkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500444 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400445 outp += sprintf(outp, "\tPkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500446 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400447 outp += sprintf(outp, "\tPkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500448 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400449 outp += sprintf(outp, "\tPkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800450 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400451 outp += sprintf(outp, "\tPkg%%pc8");
452 outp += sprintf(outp, "\tPkg%%pc9");
453 outp += sprintf(outp, "\tPk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800454 }
Len Brown103a8fe2010-10-22 23:53:03 -0400455
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800456 if (do_rapl && !rapl_joules) {
457 if (do_rapl & RAPL_PKG)
Len Brown3d109de2016-04-22 23:24:27 -0400458 outp += sprintf(outp, "\tPkgWatt");
Jacob Pan91484942016-06-16 09:48:20 -0700459 if (do_rapl & RAPL_CORES_ENERGY_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400460 outp += sprintf(outp, "\tCorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800461 if (do_rapl & RAPL_GFX)
Len Brown3d109de2016-04-22 23:24:27 -0400462 outp += sprintf(outp, "\tGFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800463 if (do_rapl & RAPL_DRAM)
Len Brown3d109de2016-04-22 23:24:27 -0400464 outp += sprintf(outp, "\tRAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800465 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400466 outp += sprintf(outp, "\tPKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800467 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400468 outp += sprintf(outp, "\tRAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500469 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800470 if (do_rapl & RAPL_PKG)
Len Brown3d109de2016-04-22 23:24:27 -0400471 outp += sprintf(outp, "\tPkg_J");
Jacob Pan91484942016-06-16 09:48:20 -0700472 if (do_rapl & RAPL_CORES_ENERGY_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400473 outp += sprintf(outp, "\tCor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800474 if (do_rapl & RAPL_GFX)
Len Brown3d109de2016-04-22 23:24:27 -0400475 outp += sprintf(outp, "\tGFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800476 if (do_rapl & RAPL_DRAM)
Len Brown3d109de2016-04-22 23:24:27 -0400477 outp += sprintf(outp, "\tRAM_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800478 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400479 outp += sprintf(outp, "\tPKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800480 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400481 outp += sprintf(outp, "\tRAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800482 }
Len Brown388e9c82016-12-22 23:57:55 -0500483 for (mp = sys.pp; mp; mp = mp->next) {
484 if (mp->format == FORMAT_RAW) {
485 if (mp->width == 64)
486 outp += sprintf(outp, "\t%18.18s", mp->name);
487 else
488 outp += sprintf(outp, "\t%10.10s", mp->name);
489 } else {
490 outp += sprintf(outp, "\t%-7.7s", mp->name);
491 }
492 }
493
494done:
Len Brownc98d5d92012-06-04 00:56:40 -0400495 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400496}
497
Len Brownc98d5d92012-06-04 00:56:40 -0400498int dump_counters(struct thread_data *t, struct core_data *c,
499 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400500{
Len Brown388e9c82016-12-22 23:57:55 -0500501 int i;
502 struct msr_counter *mp;
503
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200504 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400505
Len Brownc98d5d92012-06-04 00:56:40 -0400506 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200507 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
508 t->cpu_id, t->flags);
509 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
510 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
511 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
512 outp += sprintf(outp, "c1: %016llX\n", t->c1);
513 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400514 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200515 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400516 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200517 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400518 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200519 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400520 extra_msr_offset64, t->extra_msr64);
Len Brown562a2d32016-02-26 23:48:05 -0500521 if (do_irq)
522 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown1ed51012013-02-10 17:19:24 -0500523 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200524 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500525
526 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
527 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
528 i, mp->msr_num, t->counter[i]);
529 }
Len Brownc98d5d92012-06-04 00:56:40 -0400530 }
Len Brown103a8fe2010-10-22 23:53:03 -0400531
Len Brownc98d5d92012-06-04 00:56:40 -0400532 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200533 outp += sprintf(outp, "core: %d\n", c->core_id);
534 outp += sprintf(outp, "c3: %016llX\n", c->c3);
535 outp += sprintf(outp, "c6: %016llX\n", c->c6);
536 outp += sprintf(outp, "c7: %016llX\n", c->c7);
537 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500538
539 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
540 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
541 i, mp->msr_num, c->counter[i]);
542 }
Len Brownc98d5d92012-06-04 00:56:40 -0400543 }
544
545 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200546 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400547
548 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
549 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
550 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
551 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
552
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200553 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500554 if (do_pc3)
555 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
556 if (do_pc6)
557 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
558 if (do_pc7)
559 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200560 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
561 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
562 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
563 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
564 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
565 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
566 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
567 outp += sprintf(outp, "Throttle PKG: %0X\n",
568 p->rapl_pkg_perf_status);
569 outp += sprintf(outp, "Throttle RAM: %0X\n",
570 p->rapl_dram_perf_status);
571 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500572
573 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
574 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
575 i, mp->msr_num, p->counter[i]);
576 }
Len Brownc98d5d92012-06-04 00:56:40 -0400577 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200578
579 outp += sprintf(outp, "\n");
580
Len Brownc98d5d92012-06-04 00:56:40 -0400581 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400582}
583
Len Browne23da032012-02-06 18:37:16 -0500584/*
585 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500586 */
Len Brownc98d5d92012-06-04 00:56:40 -0400587int format_counters(struct thread_data *t, struct core_data *c,
588 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400589{
590 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500591 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500592 int i;
593 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400594
Len Brownc98d5d92012-06-04 00:56:40 -0400595 /* if showing only 1st thread in core and this isn't one, bail out */
596 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
597 return 0;
598
599 /* if showing only 1st thread in pkg and this isn't one, bail out */
600 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
601 return 0;
602
Len Brown103a8fe2010-10-22 23:53:03 -0400603 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
604
Len Brownc98d5d92012-06-04 00:56:40 -0400605 /* topo columns, print blanks on 1st (average) line */
606 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400607 if (show_pkg)
Len Brown3d109de2016-04-22 23:24:27 -0400608 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400609 if (show_core)
Len Brown3d109de2016-04-22 23:24:27 -0400610 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400611 if (show_cpu)
Len Brown3d109de2016-04-22 23:24:27 -0400612 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400613 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400614 if (show_pkg) {
615 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400616 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400617 else
Len Brown3d109de2016-04-22 23:24:27 -0400618 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400619 }
Len Brownc98d5d92012-06-04 00:56:40 -0400620 if (show_core) {
621 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400622 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400623 else
Len Brown3d109de2016-04-22 23:24:27 -0400624 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400625 }
Len Brown103a8fe2010-10-22 23:53:03 -0400626 if (show_cpu)
Len Brown3d109de2016-04-22 23:24:27 -0400627 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400628 }
Len Brownfc04cc62014-02-06 00:55:19 -0500629
Len Brownd7899442015-01-23 00:12:33 -0500630 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500631 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400632 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500633 1.0 / units * t->aperf / interval_float);
634
Len Brown75d2e442016-02-13 23:41:53 -0500635 /* Busy% */
Len Brownba3dec92016-04-22 20:31:46 -0400636 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400637 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400638
Len Brownd7899442015-01-23 00:12:33 -0500639 /* Bzy_MHz */
Len Brown21ed5572015-10-19 22:37:40 -0400640 if (has_aperf) {
641 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400642 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400643 else
Len Brown3d109de2016-04-22 23:24:27 -0400644 outp += sprintf(outp, "\t%.0f",
Len Brown21ed5572015-10-19 22:37:40 -0400645 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
646 }
Len Brown103a8fe2010-10-22 23:53:03 -0400647
Len Brownd7899442015-01-23 00:12:33 -0500648 /* TSC_MHz */
Len Brown3d109de2016-04-22 23:24:27 -0400649 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400650
Len Brown8e180f32012-09-22 01:25:08 -0400651 /* delta */
652 if (extra_delta_offset32)
Len Brown3d109de2016-04-22 23:24:27 -0400653 outp += sprintf(outp, "\t%11llu", t->extra_delta32);
Len Brown8e180f32012-09-22 01:25:08 -0400654
655 /* DELTA */
656 if (extra_delta_offset64)
Len Brown3d109de2016-04-22 23:24:27 -0400657 outp += sprintf(outp, "\t%11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400658 /* msr */
659 if (extra_msr_offset32)
Len Brown3d109de2016-04-22 23:24:27 -0400660 outp += sprintf(outp, "\t0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400661
Len Brown130ff302012-09-21 22:56:06 -0400662 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400663 if (extra_msr_offset64)
Len Brown3d109de2016-04-22 23:24:27 -0400664 outp += sprintf(outp, "\t0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400665
Len Brown1cc21f72015-02-23 00:34:57 -0500666 if (!debug)
667 goto done;
668
Len Brown562a2d32016-02-26 23:48:05 -0500669 /* IRQ */
670 if (do_irq)
Len Brown3d109de2016-04-22 23:24:27 -0400671 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500672
Len Brown1cc21f72015-02-23 00:34:57 -0500673 /* SMI */
674 if (do_smi)
Len Brown3d109de2016-04-22 23:24:27 -0400675 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500676
Len Brownba3dec92016-04-22 20:31:46 -0400677 if (do_nhm_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400678 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400679
680 /* print per-core data only for 1st thread in core */
681 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
682 goto done;
683
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700684 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400685 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400686 if (do_nhm_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400687 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400688 if (do_snb_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400689 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400690
Len Brown388e9c82016-12-22 23:57:55 -0500691 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
692 if (mp->format == FORMAT_RAW) {
693 if (mp->width == 32)
694 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
695 else
696 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
697 } else if (mp->format == FORMAT_DELTA) {
698 outp += sprintf(outp, "\t%8lld", t->counter[i]);
699 } else if (mp->format == FORMAT_PERCENT) {
700 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/t->tsc);
701 }
702 }
703
704
Len Brown889facb2012-11-08 00:48:57 -0500705 if (do_dts)
Len Brown3d109de2016-04-22 23:24:27 -0400706 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500707
Len Brown388e9c82016-12-22 23:57:55 -0500708 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
709 if (mp->format == FORMAT_RAW) {
710 if (mp->width == 32)
711 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
712 else
713 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
714 } else if (mp->format == FORMAT_DELTA) {
715 outp += sprintf(outp, "\t%8lld", c->counter[i]);
716 } else if (mp->format == FORMAT_PERCENT) {
717 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/t->tsc);
718 }
719 }
720
Len Brownc98d5d92012-06-04 00:56:40 -0400721 /* print per-package data only for 1st core in package */
722 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
723 goto done;
724
Len Brown0b2bb692015-03-26 00:50:30 -0400725 /* PkgTmp */
Len Brown889facb2012-11-08 00:48:57 -0500726 if (do_ptm)
Len Brown3d109de2016-04-22 23:24:27 -0400727 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500728
Len Brownfdf676e2016-02-27 01:28:12 -0500729 /* GFXrc6 */
Len Brown9185e982016-04-06 17:16:00 -0400730 if (do_gfx_rc6_ms) {
Len Brownba3dec92016-04-22 20:31:46 -0400731 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400732 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400733 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400734 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400735 p->gfx_rc6_ms / 10.0 / interval_float);
736 }
737 }
Len Brownfdf676e2016-02-27 01:28:12 -0500738
Len Brown27d47352016-02-27 00:37:54 -0500739 /* GFXMHz */
740 if (do_gfx_mhz)
Len Brown3d109de2016-04-22 23:24:27 -0400741 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500742
Len Brown0b2bb692015-03-26 00:50:30 -0400743 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
744 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400745 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
746 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
747 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
748 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400749 }
750
Len Brownee7e38e2015-02-09 23:39:45 -0500751 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400752 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500753 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400754 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500755 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400756 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500757 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400758 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800759 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400760 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
761 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
762 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800763 }
Len Brown889facb2012-11-08 00:48:57 -0500764
765 /*
766 * If measurement interval exceeds minimum RAPL Joule Counter range,
767 * indicate that results are suspect by printing "**" in fraction place.
768 */
Len Brownfc04cc62014-02-06 00:55:19 -0500769 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400770 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500771 else
Len Browne975db52016-04-06 23:56:02 -0400772 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500773
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800774 if (do_rapl && !rapl_joules) {
775 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500776 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Jacob Pan91484942016-06-16 09:48:20 -0700777 if (do_rapl & RAPL_CORES_ENERGY_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500778 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800779 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500780 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800781 if (do_rapl & RAPL_DRAM)
Andrey Semin40ee8e32014-12-05 00:07:00 -0500782 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800783 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500784 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800785 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500786 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500787 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800788 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500789 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800790 p->energy_pkg * rapl_energy_units);
791 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500792 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800793 p->energy_cores * rapl_energy_units);
794 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500795 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800796 p->energy_gfx * rapl_energy_units);
797 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500798 outp += sprintf(outp, fmt8,
Andrey Semin40ee8e32014-12-05 00:07:00 -0500799 p->energy_dram * rapl_dram_energy_units);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800800 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500801 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800802 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500803 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800804 }
Len Brown388e9c82016-12-22 23:57:55 -0500805 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
806 if (mp->format == FORMAT_RAW) {
807 if (mp->width == 32)
808 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
809 else
810 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
811 } else if (mp->format == FORMAT_DELTA) {
812 outp += sprintf(outp, "\t%8lld", p->counter[i]);
813 } else if (mp->format == FORMAT_PERCENT) {
814 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/t->tsc);
815 }
816 }
817
Len Brownc98d5d92012-06-04 00:56:40 -0400818done:
Len Brownc98d5d92012-06-04 00:56:40 -0400819 outp += sprintf(outp, "\n");
820
821 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400822}
823
Len Brownb7d8c142016-02-13 23:36:17 -0500824void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400825{
Len Brownb7d8c142016-02-13 23:36:17 -0500826 FILE *filep;
827
828 if (outf == stderr)
829 filep = stdout;
830 else
831 filep = outf;
832
833 fputs(output_buffer, filep);
834 fflush(filep);
835
Len Brownc98d5d92012-06-04 00:56:40 -0400836 outp = output_buffer;
837}
Len Brownb7d8c142016-02-13 23:36:17 -0500838void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400839{
Len Brownb7d8c142016-02-13 23:36:17 -0500840 fputs(output_buffer, outf);
841 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400842 outp = output_buffer;
843}
844void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
845{
Len Browne23da032012-02-06 18:37:16 -0500846 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400847
Len Browne23da032012-02-06 18:37:16 -0500848 if (!printed || !summary_only)
849 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400850
Len Brownc98d5d92012-06-04 00:56:40 -0400851 if (topo.num_cpus > 1)
852 format_counters(&average.threads, &average.cores,
853 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400854
Len Browne23da032012-02-06 18:37:16 -0500855 printed = 1;
856
857 if (summary_only)
858 return;
859
Len Brownc98d5d92012-06-04 00:56:40 -0400860 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400861}
862
Len Brown889facb2012-11-08 00:48:57 -0500863#define DELTA_WRAP32(new, old) \
864 if (new > old) { \
865 old = new - old; \
866 } else { \
867 old = 0x100000000 + new - old; \
868 }
869
Len Brownba3dec92016-04-22 20:31:46 -0400870int
Len Brownc98d5d92012-06-04 00:56:40 -0400871delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400872{
Len Brown388e9c82016-12-22 23:57:55 -0500873 int i;
874 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400875
876 if (do_skl_residency) {
877 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
878 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
879 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
880 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
881 }
Len Brownc98d5d92012-06-04 00:56:40 -0400882 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500883 if (do_pc3)
884 old->pc3 = new->pc3 - old->pc3;
885 if (do_pc6)
886 old->pc6 = new->pc6 - old->pc6;
887 if (do_pc7)
888 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800889 old->pc8 = new->pc8 - old->pc8;
890 old->pc9 = new->pc9 - old->pc9;
891 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500892 old->pkg_temp_c = new->pkg_temp_c;
893
Len Brown9185e982016-04-06 17:16:00 -0400894 /* flag an error when rc6 counter resets/wraps */
895 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
896 old->gfx_rc6_ms = -1;
897 else
898 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
899
Len Brown27d47352016-02-27 00:37:54 -0500900 old->gfx_mhz = new->gfx_mhz;
901
Len Brown889facb2012-11-08 00:48:57 -0500902 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
903 DELTA_WRAP32(new->energy_cores, old->energy_cores);
904 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
905 DELTA_WRAP32(new->energy_dram, old->energy_dram);
906 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
907 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400908
Len Brown388e9c82016-12-22 23:57:55 -0500909 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
910 if (mp->format == FORMAT_RAW)
911 old->counter[i] = new->counter[i];
912 else
913 old->counter[i] = new->counter[i] - old->counter[i];
914 }
915
Len Brownba3dec92016-04-22 20:31:46 -0400916 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400917}
Len Brown103a8fe2010-10-22 23:53:03 -0400918
Len Brownc98d5d92012-06-04 00:56:40 -0400919void
920delta_core(struct core_data *new, struct core_data *old)
921{
Len Brown388e9c82016-12-22 23:57:55 -0500922 int i;
923 struct msr_counter *mp;
924
Len Brownc98d5d92012-06-04 00:56:40 -0400925 old->c3 = new->c3 - old->c3;
926 old->c6 = new->c6 - old->c6;
927 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500928 old->core_temp_c = new->core_temp_c;
Len Brown388e9c82016-12-22 23:57:55 -0500929
930 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
931 if (mp->format == FORMAT_RAW)
932 old->counter[i] = new->counter[i];
933 else
934 old->counter[i] = new->counter[i] - old->counter[i];
935 }
Len Brownc98d5d92012-06-04 00:56:40 -0400936}
Len Brown103a8fe2010-10-22 23:53:03 -0400937
Len Brownc3ae3312012-06-13 21:31:46 -0400938/*
939 * old = new - old
940 */
Len Brownba3dec92016-04-22 20:31:46 -0400941int
Len Brownc98d5d92012-06-04 00:56:40 -0400942delta_thread(struct thread_data *new, struct thread_data *old,
943 struct core_data *core_delta)
944{
Len Brown388e9c82016-12-22 23:57:55 -0500945 int i;
946 struct msr_counter *mp;
947
Len Brownc98d5d92012-06-04 00:56:40 -0400948 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400949
Len Brownc98d5d92012-06-04 00:56:40 -0400950 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700951 if (old->tsc < (1000 * 1000))
952 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
953 "You can disable all c-states by booting with \"idle=poll\"\n"
954 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400955
Len Brownc98d5d92012-06-04 00:56:40 -0400956 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400957
Len Browna7296172015-01-23 01:33:58 -0500958 if (has_aperf) {
959 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
960 old->aperf = new->aperf - old->aperf;
961 old->mperf = new->mperf - old->mperf;
962 } else {
Len Brownba3dec92016-04-22 20:31:46 -0400963 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -0400964 }
Len Brownc98d5d92012-06-04 00:56:40 -0400965 }
Len Brown103a8fe2010-10-22 23:53:03 -0400966
Len Brown103a8fe2010-10-22 23:53:03 -0400967
Len Brown144b44b2013-11-09 00:30:16 -0500968 if (use_c1_residency_msr) {
969 /*
970 * Some models have a dedicated C1 residency MSR,
971 * which should be more accurate than the derivation below.
972 */
973 } else {
974 /*
975 * As counter collection is not atomic,
976 * it is possible for mperf's non-halted cycles + idle states
977 * to exceed TSC's all cycles: show c1 = 0% in that case.
978 */
979 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
980 old->c1 = 0;
981 else {
982 /* normal case, derive c1 */
983 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400984 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500985 }
Len Brownc98d5d92012-06-04 00:56:40 -0400986 }
Len Brownc3ae3312012-06-13 21:31:46 -0400987
Len Brownc98d5d92012-06-04 00:56:40 -0400988 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -0500989 if (debug > 1)
990 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400991 old->mperf = 1; /* divide by 0 protection */
992 }
993
Len Brown8e180f32012-09-22 01:25:08 -0400994 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
995 old->extra_delta32 &= 0xFFFFFFFF;
996
997 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
998
Len Brownc98d5d92012-06-04 00:56:40 -0400999 /*
Len Brown8e180f32012-09-22 01:25:08 -04001000 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -04001001 */
Len Brown2f32edf2012-09-21 23:45:46 -04001002 old->extra_msr32 = new->extra_msr32;
1003 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -05001004
Len Brown562a2d32016-02-26 23:48:05 -05001005 if (do_irq)
1006 old->irq_count = new->irq_count - old->irq_count;
1007
Len Brown1ed51012013-02-10 17:19:24 -05001008 if (do_smi)
1009 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001010
Len Brown388e9c82016-12-22 23:57:55 -05001011 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1012 if (mp->format == FORMAT_RAW)
1013 old->counter[i] = new->counter[i];
1014 else
1015 old->counter[i] = new->counter[i] - old->counter[i];
1016 }
Len Brownba3dec92016-04-22 20:31:46 -04001017 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001018}
1019
1020int delta_cpu(struct thread_data *t, struct core_data *c,
1021 struct pkg_data *p, struct thread_data *t2,
1022 struct core_data *c2, struct pkg_data *p2)
1023{
Len Brownba3dec92016-04-22 20:31:46 -04001024 int retval = 0;
1025
Len Brownc98d5d92012-06-04 00:56:40 -04001026 /* calculate core delta only for 1st thread in core */
1027 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1028 delta_core(c, c2);
1029
1030 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001031 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1032 if (retval)
1033 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001034
1035 /* calculate package delta only for 1st core in package */
1036 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001037 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001038
Len Brownba3dec92016-04-22 20:31:46 -04001039 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001040}
1041
1042void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1043{
Len Brown388e9c82016-12-22 23:57:55 -05001044 int i;
1045 struct msr_counter *mp;
1046
Len Brownc98d5d92012-06-04 00:56:40 -04001047 t->tsc = 0;
1048 t->aperf = 0;
1049 t->mperf = 0;
1050 t->c1 = 0;
1051
Len Brown8e180f32012-09-22 01:25:08 -04001052 t->extra_delta32 = 0;
1053 t->extra_delta64 = 0;
1054
Len Brown562a2d32016-02-26 23:48:05 -05001055 t->irq_count = 0;
1056 t->smi_count = 0;
1057
Len Brownc98d5d92012-06-04 00:56:40 -04001058 /* tells format_counters to dump all fields from this set */
1059 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1060
1061 c->c3 = 0;
1062 c->c6 = 0;
1063 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001064 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001065
Len Brown0b2bb692015-03-26 00:50:30 -04001066 p->pkg_wtd_core_c0 = 0;
1067 p->pkg_any_core_c0 = 0;
1068 p->pkg_any_gfxe_c0 = 0;
1069 p->pkg_both_core_gfxe_c0 = 0;
1070
Len Brownc98d5d92012-06-04 00:56:40 -04001071 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -05001072 if (do_pc3)
1073 p->pc3 = 0;
1074 if (do_pc6)
1075 p->pc6 = 0;
1076 if (do_pc7)
1077 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001078 p->pc8 = 0;
1079 p->pc9 = 0;
1080 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001081
1082 p->energy_pkg = 0;
1083 p->energy_dram = 0;
1084 p->energy_cores = 0;
1085 p->energy_gfx = 0;
1086 p->rapl_pkg_perf_status = 0;
1087 p->rapl_dram_perf_status = 0;
1088 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001089
Len Brownfdf676e2016-02-27 01:28:12 -05001090 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001091 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001092
1093 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1094 t->counter[i] = 0;
1095
1096 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1097 c->counter[i] = 0;
1098
1099 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1100 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001101}
1102int sum_counters(struct thread_data *t, struct core_data *c,
1103 struct pkg_data *p)
1104{
Len Brown388e9c82016-12-22 23:57:55 -05001105 int i;
1106 struct msr_counter *mp;
1107
Len Brownc98d5d92012-06-04 00:56:40 -04001108 average.threads.tsc += t->tsc;
1109 average.threads.aperf += t->aperf;
1110 average.threads.mperf += t->mperf;
1111 average.threads.c1 += t->c1;
1112
Len Brown8e180f32012-09-22 01:25:08 -04001113 average.threads.extra_delta32 += t->extra_delta32;
1114 average.threads.extra_delta64 += t->extra_delta64;
1115
Len Brown562a2d32016-02-26 23:48:05 -05001116 average.threads.irq_count += t->irq_count;
1117 average.threads.smi_count += t->smi_count;
1118
Len Brown388e9c82016-12-22 23:57:55 -05001119 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1120 if (mp->format == FORMAT_RAW)
1121 continue;
1122 average.threads.counter[i] += t->counter[i];
1123 }
1124
Len Brownc98d5d92012-06-04 00:56:40 -04001125 /* sum per-core values only for 1st thread in core */
1126 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1127 return 0;
1128
1129 average.cores.c3 += c->c3;
1130 average.cores.c6 += c->c6;
1131 average.cores.c7 += c->c7;
1132
Len Brown889facb2012-11-08 00:48:57 -05001133 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1134
Len Brown388e9c82016-12-22 23:57:55 -05001135 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1136 if (mp->format == FORMAT_RAW)
1137 continue;
1138 average.cores.counter[i] += c->counter[i];
1139 }
1140
Len Brownc98d5d92012-06-04 00:56:40 -04001141 /* sum per-pkg values only for 1st core in pkg */
1142 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1143 return 0;
1144
Len Brown0b2bb692015-03-26 00:50:30 -04001145 if (do_skl_residency) {
1146 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1147 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1148 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1149 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1150 }
1151
Len Brownc98d5d92012-06-04 00:56:40 -04001152 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -05001153 if (do_pc3)
1154 average.packages.pc3 += p->pc3;
1155 if (do_pc6)
1156 average.packages.pc6 += p->pc6;
1157 if (do_pc7)
1158 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001159 average.packages.pc8 += p->pc8;
1160 average.packages.pc9 += p->pc9;
1161 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001162
Len Brown889facb2012-11-08 00:48:57 -05001163 average.packages.energy_pkg += p->energy_pkg;
1164 average.packages.energy_dram += p->energy_dram;
1165 average.packages.energy_cores += p->energy_cores;
1166 average.packages.energy_gfx += p->energy_gfx;
1167
Len Brownfdf676e2016-02-27 01:28:12 -05001168 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001169 average.packages.gfx_mhz = p->gfx_mhz;
1170
Len Brown889facb2012-11-08 00:48:57 -05001171 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1172
1173 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1174 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001175
1176 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1177 if (mp->format == FORMAT_RAW)
1178 continue;
1179 average.packages.counter[i] += p->counter[i];
1180 }
Len Brownc98d5d92012-06-04 00:56:40 -04001181 return 0;
1182}
1183/*
1184 * sum the counters for all cpus in the system
1185 * compute the weighted average
1186 */
1187void compute_average(struct thread_data *t, struct core_data *c,
1188 struct pkg_data *p)
1189{
Len Brown388e9c82016-12-22 23:57:55 -05001190 int i;
1191 struct msr_counter *mp;
1192
Len Brownc98d5d92012-06-04 00:56:40 -04001193 clear_counters(&average.threads, &average.cores, &average.packages);
1194
1195 for_all_cpus(sum_counters, t, c, p);
1196
1197 average.threads.tsc /= topo.num_cpus;
1198 average.threads.aperf /= topo.num_cpus;
1199 average.threads.mperf /= topo.num_cpus;
1200 average.threads.c1 /= topo.num_cpus;
1201
Len Brown8e180f32012-09-22 01:25:08 -04001202 average.threads.extra_delta32 /= topo.num_cpus;
1203 average.threads.extra_delta32 &= 0xFFFFFFFF;
1204
1205 average.threads.extra_delta64 /= topo.num_cpus;
1206
Len Brownc98d5d92012-06-04 00:56:40 -04001207 average.cores.c3 /= topo.num_cores;
1208 average.cores.c6 /= topo.num_cores;
1209 average.cores.c7 /= topo.num_cores;
1210
Len Brown0b2bb692015-03-26 00:50:30 -04001211 if (do_skl_residency) {
1212 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1213 average.packages.pkg_any_core_c0 /= topo.num_packages;
1214 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1215 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1216 }
1217
Len Brownc98d5d92012-06-04 00:56:40 -04001218 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -05001219 if (do_pc3)
1220 average.packages.pc3 /= topo.num_packages;
1221 if (do_pc6)
1222 average.packages.pc6 /= topo.num_packages;
1223 if (do_pc7)
1224 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001225
1226 average.packages.pc8 /= topo.num_packages;
1227 average.packages.pc9 /= topo.num_packages;
1228 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001229
1230 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1231 if (mp->format == FORMAT_RAW)
1232 continue;
1233 average.threads.counter[i] /= topo.num_cpus;
1234 }
1235 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1236 if (mp->format == FORMAT_RAW)
1237 continue;
1238 average.cores.counter[i] /= topo.num_cores;
1239 }
1240 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1241 if (mp->format == FORMAT_RAW)
1242 continue;
1243 average.packages.counter[i] /= topo.num_packages;
1244 }
Len Brownc98d5d92012-06-04 00:56:40 -04001245}
1246
1247static unsigned long long rdtsc(void)
1248{
1249 unsigned int low, high;
1250
1251 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1252
1253 return low | ((unsigned long long)high) << 32;
1254}
1255
Len Brownc98d5d92012-06-04 00:56:40 -04001256/*
1257 * get_counters(...)
1258 * migrate to cpu
1259 * acquire and record local counters for that cpu
1260 */
1261int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1262{
1263 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001264 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001265 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001266 struct msr_counter *mp;
1267 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001268
Len Browne52966c2012-11-08 22:38:05 -05001269 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001270 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001271 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001272 }
Len Brownc98d5d92012-06-04 00:56:40 -04001273
Len Brown0102b062016-02-27 03:11:29 -05001274retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001275 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1276
1277 if (has_aperf) {
Len Brown0102b062016-02-27 03:11:29 -05001278 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1279
1280 /*
1281 * The TSC, APERF and MPERF must be read together for
1282 * APERF/MPERF and MPERF/TSC to give accurate results.
1283 *
1284 * Unfortunately, APERF and MPERF are read by
1285 * individual system call, so delays may occur
1286 * between them. If the time to read them
1287 * varies by a large amount, we re-read them.
1288 */
1289
1290 /*
1291 * This initial dummy APERF read has been seen to
1292 * reduce jitter in the subsequent reads.
1293 */
1294
Len Brown9c63a652012-10-31 01:29:52 -04001295 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001296 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001297
1298 t->tsc = rdtsc(); /* re-read close to APERF */
1299
1300 tsc_before = t->tsc;
1301
1302 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1303 return -3;
1304
1305 tsc_between = rdtsc();
1306
Len Brown9c63a652012-10-31 01:29:52 -04001307 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001308 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001309
1310 tsc_after = rdtsc();
1311
1312 aperf_time = tsc_between - tsc_before;
1313 mperf_time = tsc_after - tsc_between;
1314
1315 /*
1316 * If the system call latency to read APERF and MPERF
1317 * differ by more than 2x, then try again.
1318 */
1319 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1320 aperf_mperf_retry_count++;
1321 if (aperf_mperf_retry_count < 5)
1322 goto retry;
1323 else
1324 warnx("cpu%d jitter %lld %lld",
1325 cpu, aperf_time, mperf_time);
1326 }
1327 aperf_mperf_retry_count = 0;
1328
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001329 t->aperf = t->aperf * aperf_mperf_multiplier;
1330 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001331 }
1332
Len Brown562a2d32016-02-26 23:48:05 -05001333 if (do_irq)
1334 t->irq_count = irqs_per_cpu[cpu];
Len Brown1ed51012013-02-10 17:19:24 -05001335 if (do_smi) {
1336 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1337 return -5;
1338 t->smi_count = msr & 0xFFFFFFFF;
1339 }
Len Brown8e180f32012-09-22 01:25:08 -04001340 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001341 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -04001342 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001343 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001344 }
1345
1346 if (extra_delta_offset64)
1347 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1348 return -5;
1349
1350 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001351 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -04001352 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001353 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001354 }
Len Brown2f32edf2012-09-21 23:45:46 -04001355
1356 if (extra_msr_offset64)
1357 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -04001358 return -5;
1359
Len Brown144b44b2013-11-09 00:30:16 -05001360 if (use_c1_residency_msr) {
1361 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1362 return -6;
1363 }
1364
Len Brown388e9c82016-12-22 23:57:55 -05001365 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1366 if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1367 return -10;
1368 }
1369
1370
Len Brownc98d5d92012-06-04 00:56:40 -04001371 /* collect core counters only for 1st thread in core */
1372 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1373 return 0;
1374
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001375 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001376 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1377 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001378 }
1379
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001380 if (do_nhm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001381 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1382 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001383 } else if (do_knl_cstates) {
1384 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1385 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001386 }
1387
1388 if (do_snb_cstates)
1389 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1390 return -8;
1391
Len Brown889facb2012-11-08 00:48:57 -05001392 if (do_dts) {
1393 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1394 return -9;
1395 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1396 }
1397
Len Brown388e9c82016-12-22 23:57:55 -05001398 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1399 if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1400 return -10;
1401 }
Len Brown889facb2012-11-08 00:48:57 -05001402
Len Brownc98d5d92012-06-04 00:56:40 -04001403 /* collect package counters only for 1st core in package */
1404 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1405 return 0;
1406
Len Brown0b2bb692015-03-26 00:50:30 -04001407 if (do_skl_residency) {
1408 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1409 return -10;
1410 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1411 return -11;
1412 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1413 return -12;
1414 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1415 return -13;
1416 }
Len Brownee7e38e2015-02-09 23:39:45 -05001417 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001418 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1419 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -05001420 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -04001421 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1422 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -05001423 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001424 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1425 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001426 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001427 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1428 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001429 if (do_c8_c9_c10) {
1430 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1431 return -13;
1432 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1433 return -13;
1434 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1435 return -13;
1436 }
Len Brown889facb2012-11-08 00:48:57 -05001437 if (do_rapl & RAPL_PKG) {
1438 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1439 return -13;
1440 p->energy_pkg = msr & 0xFFFFFFFF;
1441 }
Jacob Pan91484942016-06-16 09:48:20 -07001442 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001443 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1444 return -14;
1445 p->energy_cores = msr & 0xFFFFFFFF;
1446 }
1447 if (do_rapl & RAPL_DRAM) {
1448 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1449 return -15;
1450 p->energy_dram = msr & 0xFFFFFFFF;
1451 }
1452 if (do_rapl & RAPL_GFX) {
1453 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1454 return -16;
1455 p->energy_gfx = msr & 0xFFFFFFFF;
1456 }
1457 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1458 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1459 return -16;
1460 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1461 }
1462 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1463 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1464 return -16;
1465 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1466 }
1467 if (do_ptm) {
1468 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1469 return -17;
1470 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1471 }
Len Brownfdf676e2016-02-27 01:28:12 -05001472
1473 if (do_gfx_rc6_ms)
1474 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1475
Len Brown27d47352016-02-27 00:37:54 -05001476 if (do_gfx_mhz)
1477 p->gfx_mhz = gfx_cur_mhz;
1478
Len Brown388e9c82016-12-22 23:57:55 -05001479 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1480 if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1481 return -10;
1482 }
1483
Len Brown103a8fe2010-10-22 23:53:03 -04001484 return 0;
1485}
1486
Len Brownee7e38e2015-02-09 23:39:45 -05001487/*
1488 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1489 * If you change the values, note they are used both in comparisons
1490 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1491 */
1492
1493#define PCLUKN 0 /* Unknown */
1494#define PCLRSV 1 /* Reserved */
1495#define PCL__0 2 /* PC0 */
1496#define PCL__1 3 /* PC1 */
1497#define PCL__2 4 /* PC2 */
1498#define PCL__3 5 /* PC3 */
1499#define PCL__4 6 /* PC4 */
1500#define PCL__6 7 /* PC6 */
1501#define PCL_6N 8 /* PC6 No Retention */
1502#define PCL_6R 9 /* PC6 Retention */
1503#define PCL__7 10 /* PC7 */
1504#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001505#define PCL__8 12 /* PC8 */
1506#define PCL__9 13 /* PC9 */
1507#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001508
1509int pkg_cstate_limit = PCLUKN;
1510char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001511 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001512
Len Browne9257f52015-04-01 21:02:57 -04001513int 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};
1514int 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};
1515int 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};
1516int 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};
1517int 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};
1518int 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};
Len Browne4085d52016-04-06 17:15:56 -04001519int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brownd8ebb442016-12-01 20:27:46 -05001520int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brownee7e38e2015-02-09 23:39:45 -05001521
Len Browna2b7b742015-09-26 00:12:38 -04001522
1523static void
1524calculate_tsc_tweak()
1525{
Len Browna2b7b742015-09-26 00:12:38 -04001526 tsc_tweak = base_hz / tsc_hz;
1527}
1528
Len Brownfcd17212015-03-23 20:29:09 -04001529static void
1530dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001531{
1532 unsigned long long msr;
1533 unsigned int ratio;
1534
Len Brownec0adc52015-11-12 02:42:31 -05001535 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001536
Len Brownb7d8c142016-02-13 23:36:17 -05001537 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001538
Len Brown103a8fe2010-10-22 23:53:03 -04001539 ratio = (msr >> 40) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001540 fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001541 ratio, bclk, ratio * bclk);
1542
1543 ratio = (msr >> 8) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001544 fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001545 ratio, bclk, ratio * bclk);
1546
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001547 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001548 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001549 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001550
Len Brownfcd17212015-03-23 20:29:09 -04001551 return;
1552}
1553
1554static void
1555dump_hsw_turbo_ratio_limits(void)
1556{
1557 unsigned long long msr;
1558 unsigned int ratio;
1559
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001560 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001561
Len Brownb7d8c142016-02-13 23:36:17 -05001562 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001563
1564 ratio = (msr >> 8) & 0xFF;
1565 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001566 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001567 ratio, bclk, ratio * bclk);
1568
1569 ratio = (msr >> 0) & 0xFF;
1570 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001571 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001572 ratio, bclk, ratio * bclk);
1573 return;
1574}
1575
1576static void
1577dump_ivt_turbo_ratio_limits(void)
1578{
1579 unsigned long long msr;
1580 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001581
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001582 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001583
Len Brownb7d8c142016-02-13 23:36:17 -05001584 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001585
1586 ratio = (msr >> 56) & 0xFF;
1587 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001588 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001589 ratio, bclk, ratio * bclk);
1590
1591 ratio = (msr >> 48) & 0xFF;
1592 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001593 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001594 ratio, bclk, ratio * bclk);
1595
1596 ratio = (msr >> 40) & 0xFF;
1597 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001598 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001599 ratio, bclk, ratio * bclk);
1600
1601 ratio = (msr >> 32) & 0xFF;
1602 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001603 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001604 ratio, bclk, ratio * bclk);
1605
1606 ratio = (msr >> 24) & 0xFF;
1607 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001608 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001609 ratio, bclk, ratio * bclk);
1610
1611 ratio = (msr >> 16) & 0xFF;
1612 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001613 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001614 ratio, bclk, ratio * bclk);
1615
1616 ratio = (msr >> 8) & 0xFF;
1617 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001618 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001619 ratio, bclk, ratio * bclk);
1620
1621 ratio = (msr >> 0) & 0xFF;
1622 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001623 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001624 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001625 return;
1626}
Len Brown6574a5d2012-09-21 00:01:31 -04001627
Len Brownfcd17212015-03-23 20:29:09 -04001628static void
1629dump_nhm_turbo_ratio_limits(void)
1630{
1631 unsigned long long msr;
1632 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001633
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001634 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001635
Len Brownb7d8c142016-02-13 23:36:17 -05001636 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001637
1638 ratio = (msr >> 56) & 0xFF;
1639 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001640 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001641 ratio, bclk, ratio * bclk);
1642
1643 ratio = (msr >> 48) & 0xFF;
1644 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001645 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001646 ratio, bclk, ratio * bclk);
1647
1648 ratio = (msr >> 40) & 0xFF;
1649 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001650 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001651 ratio, bclk, ratio * bclk);
1652
1653 ratio = (msr >> 32) & 0xFF;
1654 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001655 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001656 ratio, bclk, ratio * bclk);
1657
Len Brown103a8fe2010-10-22 23:53:03 -04001658 ratio = (msr >> 24) & 0xFF;
1659 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001660 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001661 ratio, bclk, ratio * bclk);
1662
1663 ratio = (msr >> 16) & 0xFF;
1664 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001665 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001666 ratio, bclk, ratio * bclk);
1667
1668 ratio = (msr >> 8) & 0xFF;
1669 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001670 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001671 ratio, bclk, ratio * bclk);
1672
1673 ratio = (msr >> 0) & 0xFF;
1674 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001675 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001676 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001677 return;
1678}
Len Brown3a9a9412014-08-15 02:39:52 -04001679
Len Brownfcd17212015-03-23 20:29:09 -04001680static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001681dump_knl_turbo_ratio_limits(void)
1682{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001683 const unsigned int buckets_no = 7;
1684
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001685 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001686 int delta_cores, delta_ratio;
1687 int i, b_nr;
1688 unsigned int cores[buckets_no];
1689 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001690
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001691 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001692
Len Brownb7d8c142016-02-13 23:36:17 -05001693 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001694 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001695
1696 /**
1697 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001698 * [0] -- Reserved
1699 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001700 * [15:8] -- Base value of freq ratio of bucket 1.
1701 * [20:16] -- +ve delta of number of active cores of bucket 2.
1702 * i.e. active cores of bucket 2 =
1703 * active cores of bucket 1 + delta
1704 * [23:21] -- Negative delta of freq ratio of bucket 2.
1705 * i.e. freq ratio of bucket 2 =
1706 * freq ratio of bucket 1 - delta
1707 * [28:24]-- +ve delta of number of active cores of bucket 3.
1708 * [31:29]-- -ve delta of freq ratio of bucket 3.
1709 * [36:32]-- +ve delta of number of active cores of bucket 4.
1710 * [39:37]-- -ve delta of freq ratio of bucket 4.
1711 * [44:40]-- +ve delta of number of active cores of bucket 5.
1712 * [47:45]-- -ve delta of freq ratio of bucket 5.
1713 * [52:48]-- +ve delta of number of active cores of bucket 6.
1714 * [55:53]-- -ve delta of freq ratio of bucket 6.
1715 * [60:56]-- +ve delta of number of active cores of bucket 7.
1716 * [63:61]-- -ve delta of freq ratio of bucket 7.
1717 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001718
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001719 b_nr = 0;
1720 cores[b_nr] = (msr & 0xFF) >> 1;
1721 ratio[b_nr] = (msr >> 8) & 0xFF;
1722
1723 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001724 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001725 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001726
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001727 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1728 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1729 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001730 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001731
1732 for (i = buckets_no - 1; i >= 0; i--)
1733 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001734 fprintf(outf,
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001735 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001736 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001737}
1738
1739static void
Len Brownfcd17212015-03-23 20:29:09 -04001740dump_nhm_cst_cfg(void)
1741{
1742 unsigned long long msr;
1743
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001744 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001745
1746#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1747#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1748
Len Brownb7d8c142016-02-13 23:36:17 -05001749 fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001750
Len Brownb7d8c142016-02-13 23:36:17 -05001751 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001752 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1753 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1754 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1755 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1756 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001757 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001758 pkg_cstate_limit_strings[pkg_cstate_limit]);
1759 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001760}
1761
Len Brown6fb31432015-06-17 16:23:45 -04001762static void
1763dump_config_tdp(void)
1764{
1765 unsigned long long msr;
1766
1767 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001768 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001769 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001770
1771 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001772 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001773 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001774 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1775 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1776 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1777 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001778 }
Len Brownb7d8c142016-02-13 23:36:17 -05001779 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001780
1781 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001782 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001783 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001784 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1785 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1786 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1787 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001788 }
Len Brownb7d8c142016-02-13 23:36:17 -05001789 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001790
1791 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001792 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001793 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001794 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1795 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1796 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001797
Len Brown6fb31432015-06-17 16:23:45 -04001798 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001799 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001800 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001801 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1802 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001803}
Len Brown5a634262016-04-06 17:15:55 -04001804
1805unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1806
1807void print_irtl(void)
1808{
1809 unsigned long long msr;
1810
1811 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1812 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1813 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1814 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1815
1816 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
1817 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
1818 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1819 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1820
1821 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
1822 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
1823 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1824 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1825
1826 if (!do_irtl_hsw)
1827 return;
1828
1829 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
1830 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
1831 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1832 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1833
1834 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
1835 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
1836 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1837 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1838
1839 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
1840 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
1841 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1842 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1843
1844}
Len Brown36229892016-02-26 20:51:02 -05001845void free_fd_percpu(void)
1846{
1847 int i;
1848
Mika Westerberg01a67ad2016-04-22 11:13:23 +03001849 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05001850 if (fd_percpu[i] != 0)
1851 close(fd_percpu[i]);
1852 }
1853
1854 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04001855}
1856
Len Brownc98d5d92012-06-04 00:56:40 -04001857void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001858{
Len Brownc98d5d92012-06-04 00:56:40 -04001859 CPU_FREE(cpu_present_set);
1860 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05001861 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001862
Len Brownc98d5d92012-06-04 00:56:40 -04001863 CPU_FREE(cpu_affinity_set);
1864 cpu_affinity_set = NULL;
1865 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001866
Len Brownc98d5d92012-06-04 00:56:40 -04001867 free(thread_even);
1868 free(core_even);
1869 free(package_even);
1870
1871 thread_even = NULL;
1872 core_even = NULL;
1873 package_even = NULL;
1874
1875 free(thread_odd);
1876 free(core_odd);
1877 free(package_odd);
1878
1879 thread_odd = NULL;
1880 core_odd = NULL;
1881 package_odd = NULL;
1882
1883 free(output_buffer);
1884 output_buffer = NULL;
1885 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05001886
1887 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05001888
1889 free(irq_column_2_cpu);
1890 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001891}
1892
Len Brownc98d5d92012-06-04 00:56:40 -04001893/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001894 * Open a file, and exit on failure
1895 */
1896FILE *fopen_or_die(const char *path, const char *mode)
1897{
Len Brownb7d8c142016-02-13 23:36:17 -05001898 FILE *filep = fopen(path, mode);
Josh Triplettb2c95d92013-08-20 17:20:18 -07001899 if (!filep)
1900 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001901 return filep;
1902}
1903
1904/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001905 * Parse a file containing a single int.
1906 */
1907int parse_int_file(const char *fmt, ...)
1908{
1909 va_list args;
1910 char path[PATH_MAX];
1911 FILE *filep;
1912 int value;
1913
1914 va_start(args, fmt);
1915 vsnprintf(path, sizeof(path), fmt, args);
1916 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001917 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001918 if (fscanf(filep, "%d", &value) != 1)
1919 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001920 fclose(filep);
1921 return value;
1922}
1923
1924/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001925 * get_cpu_position_in_core(cpu)
1926 * return the position of the CPU among its HT siblings in the core
1927 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04001928 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001929int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001930{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001931 char path[64];
1932 FILE *filep;
1933 int this_cpu;
1934 char character;
1935 int i;
1936
1937 sprintf(path,
1938 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1939 cpu);
1940 filep = fopen(path, "r");
1941 if (filep == NULL) {
1942 perror(path);
1943 exit(1);
1944 }
1945
1946 for (i = 0; i < topo.num_threads_per_core; i++) {
1947 fscanf(filep, "%d", &this_cpu);
1948 if (this_cpu == cpu) {
1949 fclose(filep);
1950 return i;
1951 }
1952
1953 /* Account for no separator after last thread*/
1954 if (i != (topo.num_threads_per_core - 1))
1955 fscanf(filep, "%c", &character);
1956 }
1957
1958 fclose(filep);
1959 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04001960}
1961
Len Brownc98d5d92012-06-04 00:56:40 -04001962/*
1963 * cpu_is_first_core_in_package(cpu)
1964 * return 1 if given CPU is 1st core in package
1965 */
1966int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001967{
Josh Triplett95aebc42013-08-20 17:20:16 -07001968 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001969}
1970
1971int get_physical_package_id(int cpu)
1972{
Josh Triplett95aebc42013-08-20 17:20:16 -07001973 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001974}
1975
1976int get_core_id(int cpu)
1977{
Josh Triplett95aebc42013-08-20 17:20:16 -07001978 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001979}
1980
Len Brownc98d5d92012-06-04 00:56:40 -04001981int get_num_ht_siblings(int cpu)
1982{
1983 char path[80];
1984 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001985 int sib1;
1986 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001987 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001988 char str[100];
1989 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04001990
1991 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001992 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001993
Len Brownc98d5d92012-06-04 00:56:40 -04001994 /*
1995 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001996 * A ',' separated or '-' separated set of numbers
1997 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04001998 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001999 fscanf(filep, "%d%c\n", &sib1, &character);
2000 fseek(filep, 0, SEEK_SET);
2001 fgets(str, 100, filep);
2002 ch = strchr(str, character);
2003 while (ch != NULL) {
2004 matches++;
2005 ch = strchr(ch+1, character);
2006 }
Len Brownc98d5d92012-06-04 00:56:40 -04002007
2008 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002009 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002010}
2011
Len Brown103a8fe2010-10-22 23:53:03 -04002012/*
Len Brownc98d5d92012-06-04 00:56:40 -04002013 * run func(thread, core, package) in topology order
2014 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002015 */
2016
Len Brownc98d5d92012-06-04 00:56:40 -04002017int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2018 struct pkg_data *, struct thread_data *, struct core_data *,
2019 struct pkg_data *), struct thread_data *thread_base,
2020 struct core_data *core_base, struct pkg_data *pkg_base,
2021 struct thread_data *thread_base2, struct core_data *core_base2,
2022 struct pkg_data *pkg_base2)
2023{
2024 int retval, pkg_no, core_no, thread_no;
2025
2026 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2027 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2028 for (thread_no = 0; thread_no <
2029 topo.num_threads_per_core; ++thread_no) {
2030 struct thread_data *t, *t2;
2031 struct core_data *c, *c2;
2032 struct pkg_data *p, *p2;
2033
2034 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2035
2036 if (cpu_is_not_present(t->cpu_id))
2037 continue;
2038
2039 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2040
2041 c = GET_CORE(core_base, core_no, pkg_no);
2042 c2 = GET_CORE(core_base2, core_no, pkg_no);
2043
2044 p = GET_PKG(pkg_base, pkg_no);
2045 p2 = GET_PKG(pkg_base2, pkg_no);
2046
2047 retval = func(t, c, p, t2, c2, p2);
2048 if (retval)
2049 return retval;
2050 }
2051 }
2052 }
2053 return 0;
2054}
2055
2056/*
2057 * run func(cpu) on every cpu in /proc/stat
2058 * return max_cpu number
2059 */
2060int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002061{
2062 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002063 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002064 int retval;
2065
Josh Triplett57a42a32013-08-20 17:20:17 -07002066 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002067
2068 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002069 if (retval != 0)
2070 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002071
Len Brownc98d5d92012-06-04 00:56:40 -04002072 while (1) {
2073 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
Len Brown103a8fe2010-10-22 23:53:03 -04002074 if (retval != 1)
2075 break;
2076
Len Brownc98d5d92012-06-04 00:56:40 -04002077 retval = func(cpu_num);
2078 if (retval) {
2079 fclose(fp);
2080 return(retval);
2081 }
Len Brown103a8fe2010-10-22 23:53:03 -04002082 }
2083 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002084 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002085}
2086
2087void re_initialize(void)
2088{
Len Brownc98d5d92012-06-04 00:56:40 -04002089 free_all_buffers();
2090 setup_all_buffers();
2091 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002092}
2093
Len Brownc98d5d92012-06-04 00:56:40 -04002094
Len Brown103a8fe2010-10-22 23:53:03 -04002095/*
Len Brownc98d5d92012-06-04 00:56:40 -04002096 * count_cpus()
2097 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002098 */
Len Brownc98d5d92012-06-04 00:56:40 -04002099int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002100{
Len Brownc98d5d92012-06-04 00:56:40 -04002101 if (topo.max_cpu_num < cpu)
2102 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002103
Len Brownc98d5d92012-06-04 00:56:40 -04002104 topo.num_cpus += 1;
2105 return 0;
2106}
2107int mark_cpu_present(int cpu)
2108{
2109 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002110 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002111}
2112
Len Brown562a2d32016-02-26 23:48:05 -05002113/*
2114 * snapshot_proc_interrupts()
2115 *
2116 * read and record summary of /proc/interrupts
2117 *
2118 * return 1 if config change requires a restart, else return 0
2119 */
2120int snapshot_proc_interrupts(void)
2121{
2122 static FILE *fp;
2123 int column, retval;
2124
2125 if (fp == NULL)
2126 fp = fopen_or_die("/proc/interrupts", "r");
2127 else
2128 rewind(fp);
2129
2130 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2131 for (column = 0; column < topo.num_cpus; ++column) {
2132 int cpu_number;
2133
2134 retval = fscanf(fp, " CPU%d", &cpu_number);
2135 if (retval != 1)
2136 break;
2137
2138 if (cpu_number > topo.max_cpu_num) {
2139 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2140 return 1;
2141 }
2142
2143 irq_column_2_cpu[column] = cpu_number;
2144 irqs_per_cpu[cpu_number] = 0;
2145 }
2146
2147 /* read /proc/interrupt count lines and sum up irqs per cpu */
2148 while (1) {
2149 int column;
2150 char buf[64];
2151
2152 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2153 if (retval != 1)
2154 break;
2155
2156 /* read the count per cpu */
2157 for (column = 0; column < topo.num_cpus; ++column) {
2158
2159 int cpu_number, irq_count;
2160
2161 retval = fscanf(fp, " %d", &irq_count);
2162 if (retval != 1)
2163 break;
2164
2165 cpu_number = irq_column_2_cpu[column];
2166 irqs_per_cpu[cpu_number] += irq_count;
2167
2168 }
2169
2170 while (getc(fp) != '\n')
2171 ; /* flush interrupt description */
2172
2173 }
2174 return 0;
2175}
Len Brown27d47352016-02-27 00:37:54 -05002176/*
Len Brownfdf676e2016-02-27 01:28:12 -05002177 * snapshot_gfx_rc6_ms()
2178 *
2179 * record snapshot of
2180 * /sys/class/drm/card0/power/rc6_residency_ms
2181 *
2182 * return 1 if config change requires a restart, else return 0
2183 */
2184int snapshot_gfx_rc6_ms(void)
2185{
2186 FILE *fp;
2187 int retval;
2188
2189 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2190
2191 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2192 if (retval != 1)
2193 err(1, "GFX rc6");
2194
2195 fclose(fp);
2196
2197 return 0;
2198}
2199/*
Len Brown27d47352016-02-27 00:37:54 -05002200 * snapshot_gfx_mhz()
2201 *
2202 * record snapshot of
2203 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2204 *
2205 * return 1 if config change requires a restart, else return 0
2206 */
2207int snapshot_gfx_mhz(void)
2208{
2209 static FILE *fp;
2210 int retval;
2211
2212 if (fp == NULL)
2213 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2214 else
2215 rewind(fp);
2216
2217 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2218 if (retval != 1)
2219 err(1, "GFX MHz");
2220
2221 return 0;
2222}
Len Brown562a2d32016-02-26 23:48:05 -05002223
2224/*
2225 * snapshot /proc and /sys files
2226 *
2227 * return 1 if configuration restart needed, else return 0
2228 */
2229int snapshot_proc_sysfs_files(void)
2230{
2231 if (snapshot_proc_interrupts())
2232 return 1;
2233
Len Brownfdf676e2016-02-27 01:28:12 -05002234 if (do_gfx_rc6_ms)
2235 snapshot_gfx_rc6_ms();
2236
Len Brown27d47352016-02-27 00:37:54 -05002237 if (do_gfx_mhz)
2238 snapshot_gfx_mhz();
2239
Len Brown562a2d32016-02-26 23:48:05 -05002240 return 0;
2241}
2242
Len Brown103a8fe2010-10-22 23:53:03 -04002243void turbostat_loop()
2244{
Len Brownc98d5d92012-06-04 00:56:40 -04002245 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002246 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002247
Len Brown103a8fe2010-10-22 23:53:03 -04002248restart:
Len Browne52966c2012-11-08 22:38:05 -05002249 restarted++;
2250
Len Brown562a2d32016-02-26 23:48:05 -05002251 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002252 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002253 if (retval < -1) {
2254 exit(retval);
2255 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002256 if (restarted > 1) {
2257 exit(retval);
2258 }
Len Brownc98d5d92012-06-04 00:56:40 -04002259 re_initialize();
2260 goto restart;
2261 }
Len Browne52966c2012-11-08 22:38:05 -05002262 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002263 gettimeofday(&tv_even, (struct timezone *)NULL);
2264
2265 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002266 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002267 re_initialize();
2268 goto restart;
2269 }
Len Brown2a0609c2016-02-12 22:44:48 -05002270 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002271 if (snapshot_proc_sysfs_files())
2272 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002273 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002274 if (retval < -1) {
2275 exit(retval);
2276 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002277 re_initialize();
2278 goto restart;
2279 }
Len Brown103a8fe2010-10-22 23:53:03 -04002280 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002281 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002282 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2283 re_initialize();
2284 goto restart;
2285 }
Len Brownc98d5d92012-06-04 00:56:40 -04002286 compute_average(EVEN_COUNTERS);
2287 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002288 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002289 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002290 if (snapshot_proc_sysfs_files())
2291 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002292 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002293 if (retval < -1) {
2294 exit(retval);
2295 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002296 re_initialize();
2297 goto restart;
2298 }
Len Brown103a8fe2010-10-22 23:53:03 -04002299 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002300 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002301 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2302 re_initialize();
2303 goto restart;
2304 }
Len Brownc98d5d92012-06-04 00:56:40 -04002305 compute_average(ODD_COUNTERS);
2306 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002307 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002308 }
2309}
2310
2311void check_dev_msr()
2312{
2313 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002314 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002315
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002316 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2317 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002318 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2319 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002320}
2321
Len Brown98481e72014-08-15 00:36:50 -04002322void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002323{
Len Brown98481e72014-08-15 00:36:50 -04002324 struct __user_cap_header_struct cap_header_data;
2325 cap_user_header_t cap_header = &cap_header_data;
2326 struct __user_cap_data_struct cap_data_data;
2327 cap_user_data_t cap_data = &cap_data_data;
2328 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2329 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002330 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002331
2332 /* check for CAP_SYS_RAWIO */
2333 cap_header->pid = getpid();
2334 cap_header->version = _LINUX_CAPABILITY_VERSION;
2335 if (capget(cap_header, cap_data) < 0)
2336 err(-6, "capget(2) failed");
2337
2338 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2339 do_exit++;
2340 warnx("capget(CAP_SYS_RAWIO) failed,"
2341 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2342 }
2343
2344 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002345 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2346 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002347 do_exit++;
2348 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2349 }
2350
2351 /* if all else fails, thell them to be root */
2352 if (do_exit)
2353 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002354 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002355
2356 if (do_exit)
2357 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002358}
2359
Len Brownd7899442015-01-23 00:12:33 -05002360/*
2361 * NHM adds support for additional MSRs:
2362 *
2363 * MSR_SMI_COUNT 0x00000034
2364 *
Len Brownec0adc52015-11-12 02:42:31 -05002365 * MSR_PLATFORM_INFO 0x000000ce
Len Brownd7899442015-01-23 00:12:33 -05002366 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
2367 *
2368 * MSR_PKG_C3_RESIDENCY 0x000003f8
2369 * MSR_PKG_C6_RESIDENCY 0x000003f9
2370 * MSR_CORE_C3_RESIDENCY 0x000003fc
2371 * MSR_CORE_C6_RESIDENCY 0x000003fd
2372 *
Len Brownee7e38e2015-02-09 23:39:45 -05002373 * Side effect:
2374 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05002375 */
Len Brownee7e38e2015-02-09 23:39:45 -05002376int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002377{
Len Brownee7e38e2015-02-09 23:39:45 -05002378 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002379 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002380 int *pkg_cstate_limits;
2381
Len Brown103a8fe2010-10-22 23:53:03 -04002382 if (!genuine_intel)
2383 return 0;
2384
2385 if (family != 6)
2386 return 0;
2387
Len Brown21ed5572015-10-19 22:37:40 -04002388 bclk = discover_bclk(family, model);
2389
Len Brown103a8fe2010-10-22 23:53:03 -04002390 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002391 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2392 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002393 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002394 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2395 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2396 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2397 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002398 pkg_cstate_limits = nhm_pkg_cstate_limits;
2399 break;
Len Brown869ce692016-06-16 23:22:37 -04002400 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2401 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2402 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2403 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002404 pkg_cstate_limits = snb_pkg_cstate_limits;
2405 break;
Len Brown869ce692016-06-16 23:22:37 -04002406 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2407 case INTEL_FAM6_HASWELL_X: /* HSX */
2408 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2409 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2410 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2411 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2412 case INTEL_FAM6_BROADWELL_X: /* BDX */
2413 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2414 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2415 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2416 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2417 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002418 pkg_cstate_limits = hsw_pkg_cstate_limits;
2419 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002420 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2421 pkg_cstate_limits = skx_pkg_cstate_limits;
2422 break;
Len Brown869ce692016-06-16 23:22:37 -04002423 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
2424 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002425 pkg_cstate_limits = slv_pkg_cstate_limits;
2426 break;
Len Brown869ce692016-06-16 23:22:37 -04002427 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002428 pkg_cstate_limits = amt_pkg_cstate_limits;
2429 break;
Len Brown869ce692016-06-16 23:22:37 -04002430 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002431 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002432 pkg_cstate_limits = phi_pkg_cstate_limits;
2433 break;
Len Brown869ce692016-06-16 23:22:37 -04002434 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
2435 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002436 pkg_cstate_limits = bxt_pkg_cstate_limits;
2437 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002438 default:
2439 return 0;
2440 }
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002441 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002442 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002443
Len Brownec0adc52015-11-12 02:42:31 -05002444 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002445 base_ratio = (msr >> 8) & 0xFF;
2446
2447 base_hz = base_ratio * bclk * 1000000;
2448 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002449 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002450}
Len Brownd7899442015-01-23 00:12:33 -05002451int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2452{
Len Brownd7899442015-01-23 00:12:33 -05002453 switch (model) {
2454 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002455 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2456 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2457 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002458 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002459 return 0;
2460 default:
2461 return 1;
2462 }
2463}
Len Brown6574a5d2012-09-21 00:01:31 -04002464int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2465{
2466 if (!genuine_intel)
2467 return 0;
2468
2469 if (family != 6)
2470 return 0;
2471
2472 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002473 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2474 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002475 return 1;
2476 default:
2477 return 0;
2478 }
2479}
Len Brownfcd17212015-03-23 20:29:09 -04002480int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2481{
2482 if (!genuine_intel)
2483 return 0;
2484
2485 if (family != 6)
2486 return 0;
2487
2488 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002489 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002490 return 1;
2491 default:
2492 return 0;
2493 }
2494}
2495
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002496int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2497{
2498 if (!genuine_intel)
2499 return 0;
2500
2501 if (family != 6)
2502 return 0;
2503
2504 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002505 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002506 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002507 return 1;
2508 default:
2509 return 0;
2510 }
2511}
Len Brown6fb31432015-06-17 16:23:45 -04002512int has_config_tdp(unsigned int family, unsigned int model)
2513{
2514 if (!genuine_intel)
2515 return 0;
2516
2517 if (family != 6)
2518 return 0;
2519
2520 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002521 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2522 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2523 case INTEL_FAM6_HASWELL_X: /* HSX */
2524 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2525 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2526 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2527 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2528 case INTEL_FAM6_BROADWELL_X: /* BDX */
2529 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2530 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2531 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2532 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2533 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2534 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002535
Len Brown869ce692016-06-16 23:22:37 -04002536 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002537 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002538 return 1;
2539 default:
2540 return 0;
2541 }
2542}
2543
Len Brownfcd17212015-03-23 20:29:09 -04002544static void
Colin Ian King1b693172016-03-02 13:50:25 +00002545dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002546{
2547 if (!do_nhm_platform_info)
2548 return;
2549
2550 dump_nhm_platform_info();
2551
2552 if (has_hsw_turbo_ratio_limit(family, model))
2553 dump_hsw_turbo_ratio_limits();
2554
2555 if (has_ivt_turbo_ratio_limit(family, model))
2556 dump_ivt_turbo_ratio_limits();
2557
2558 if (has_nhm_turbo_ratio_limit(family, model))
2559 dump_nhm_turbo_ratio_limits();
2560
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002561 if (has_knl_turbo_ratio_limit(family, model))
2562 dump_knl_turbo_ratio_limits();
2563
Len Brown6fb31432015-06-17 16:23:45 -04002564 if (has_config_tdp(family, model))
2565 dump_config_tdp();
2566
Len Brownfcd17212015-03-23 20:29:09 -04002567 dump_nhm_cst_cfg();
2568}
2569
Len Brown6574a5d2012-09-21 00:01:31 -04002570
Len Brown889facb2012-11-08 00:48:57 -05002571/*
2572 * print_epb()
2573 * Decode the ENERGY_PERF_BIAS MSR
2574 */
2575int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2576{
2577 unsigned long long msr;
2578 char *epb_string;
2579 int cpu;
2580
2581 if (!has_epb)
2582 return 0;
2583
2584 cpu = t->cpu_id;
2585
2586 /* EPB is per-package */
2587 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2588 return 0;
2589
2590 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002591 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002592 return -1;
2593 }
2594
2595 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2596 return 0;
2597
Len Browne9be7dd2015-05-26 12:19:37 -04002598 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002599 case ENERGY_PERF_BIAS_PERFORMANCE:
2600 epb_string = "performance";
2601 break;
2602 case ENERGY_PERF_BIAS_NORMAL:
2603 epb_string = "balanced";
2604 break;
2605 case ENERGY_PERF_BIAS_POWERSAVE:
2606 epb_string = "powersave";
2607 break;
2608 default:
2609 epb_string = "custom";
2610 break;
2611 }
Len Brownb7d8c142016-02-13 23:36:17 -05002612 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002613
2614 return 0;
2615}
Len Brown7f5c2582015-12-01 01:36:39 -05002616/*
2617 * print_hwp()
2618 * Decode the MSR_HWP_CAPABILITIES
2619 */
2620int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2621{
2622 unsigned long long msr;
2623 int cpu;
2624
2625 if (!has_hwp)
2626 return 0;
2627
2628 cpu = t->cpu_id;
2629
2630 /* MSR_HWP_CAPABILITIES is per-package */
2631 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2632 return 0;
2633
2634 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002635 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002636 return -1;
2637 }
2638
2639 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2640 return 0;
2641
Len Brownb7d8c142016-02-13 23:36:17 -05002642 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002643 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2644
2645 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2646 if ((msr & (1 << 0)) == 0)
2647 return 0;
2648
2649 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2650 return 0;
2651
Len Brownb7d8c142016-02-13 23:36:17 -05002652 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002653 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2654 cpu, msr,
2655 (unsigned int)HWP_HIGHEST_PERF(msr),
2656 (unsigned int)HWP_GUARANTEED_PERF(msr),
2657 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2658 (unsigned int)HWP_LOWEST_PERF(msr));
2659
2660 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2661 return 0;
2662
Len Brownb7d8c142016-02-13 23:36:17 -05002663 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002664 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2665 cpu, msr,
2666 (unsigned int)(((msr) >> 0) & 0xff),
2667 (unsigned int)(((msr) >> 8) & 0xff),
2668 (unsigned int)(((msr) >> 16) & 0xff),
2669 (unsigned int)(((msr) >> 24) & 0xff),
2670 (unsigned int)(((msr) >> 32) & 0xff3),
2671 (unsigned int)(((msr) >> 42) & 0x1));
2672
2673 if (has_hwp_pkg) {
2674 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2675 return 0;
2676
Len Brownb7d8c142016-02-13 23:36:17 -05002677 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002678 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2679 cpu, msr,
2680 (unsigned int)(((msr) >> 0) & 0xff),
2681 (unsigned int)(((msr) >> 8) & 0xff),
2682 (unsigned int)(((msr) >> 16) & 0xff),
2683 (unsigned int)(((msr) >> 24) & 0xff),
2684 (unsigned int)(((msr) >> 32) & 0xff3));
2685 }
2686 if (has_hwp_notify) {
2687 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2688 return 0;
2689
Len Brownb7d8c142016-02-13 23:36:17 -05002690 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002691 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2692 cpu, msr,
2693 ((msr) & 0x1) ? "EN" : "Dis",
2694 ((msr) & 0x2) ? "EN" : "Dis");
2695 }
2696 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2697 return 0;
2698
Len Brownb7d8c142016-02-13 23:36:17 -05002699 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002700 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2701 cpu, msr,
2702 ((msr) & 0x1) ? "" : "No-",
2703 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05002704
2705 return 0;
2706}
2707
Len Brown3a9a9412014-08-15 02:39:52 -04002708/*
2709 * print_perf_limit()
2710 */
2711int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2712{
2713 unsigned long long msr;
2714 int cpu;
2715
2716 cpu = t->cpu_id;
2717
2718 /* per-package */
2719 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2720 return 0;
2721
2722 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002723 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002724 return -1;
2725 }
2726
2727 if (do_core_perf_limit_reasons) {
2728 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002729 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2730 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002731 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002732 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002733 (msr & 1 << 13) ? "Transitions, " : "",
2734 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2735 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2736 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2737 (msr & 1 << 9) ? "CorePwr, " : "",
2738 (msr & 1 << 8) ? "Amps, " : "",
2739 (msr & 1 << 6) ? "VR-Therm, " : "",
2740 (msr & 1 << 5) ? "Auto-HWP, " : "",
2741 (msr & 1 << 4) ? "Graphics, " : "",
2742 (msr & 1 << 2) ? "bit2, " : "",
2743 (msr & 1 << 1) ? "ThermStatus, " : "",
2744 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002745 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
Len Browne33cbe82015-03-13 16:30:57 -04002746 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002747 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002748 (msr & 1 << 29) ? "Transitions, " : "",
2749 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2750 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2751 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2752 (msr & 1 << 25) ? "CorePwr, " : "",
2753 (msr & 1 << 24) ? "Amps, " : "",
2754 (msr & 1 << 22) ? "VR-Therm, " : "",
2755 (msr & 1 << 21) ? "Auto-HWP, " : "",
2756 (msr & 1 << 20) ? "Graphics, " : "",
2757 (msr & 1 << 18) ? "bit18, " : "",
2758 (msr & 1 << 17) ? "ThermStatus, " : "",
2759 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002760
2761 }
2762 if (do_gfx_perf_limit_reasons) {
2763 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002764 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2765 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002766 (msr & 1 << 0) ? "PROCHOT, " : "",
2767 (msr & 1 << 1) ? "ThermStatus, " : "",
2768 (msr & 1 << 4) ? "Graphics, " : "",
2769 (msr & 1 << 6) ? "VR-Therm, " : "",
2770 (msr & 1 << 8) ? "Amps, " : "",
2771 (msr & 1 << 9) ? "GFXPwr, " : "",
2772 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2773 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002774 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002775 (msr & 1 << 16) ? "PROCHOT, " : "",
2776 (msr & 1 << 17) ? "ThermStatus, " : "",
2777 (msr & 1 << 20) ? "Graphics, " : "",
2778 (msr & 1 << 22) ? "VR-Therm, " : "",
2779 (msr & 1 << 24) ? "Amps, " : "",
2780 (msr & 1 << 25) ? "GFXPwr, " : "",
2781 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2782 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2783 }
2784 if (do_ring_perf_limit_reasons) {
2785 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002786 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2787 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002788 (msr & 1 << 0) ? "PROCHOT, " : "",
2789 (msr & 1 << 1) ? "ThermStatus, " : "",
2790 (msr & 1 << 6) ? "VR-Therm, " : "",
2791 (msr & 1 << 8) ? "Amps, " : "",
2792 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2793 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002794 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002795 (msr & 1 << 16) ? "PROCHOT, " : "",
2796 (msr & 1 << 17) ? "ThermStatus, " : "",
2797 (msr & 1 << 22) ? "VR-Therm, " : "",
2798 (msr & 1 << 24) ? "Amps, " : "",
2799 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2800 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2801 }
2802 return 0;
2803}
2804
Len Brown889facb2012-11-08 00:48:57 -05002805#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2806#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2807
Colin Ian King1b693172016-03-02 13:50:25 +00002808double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05002809{
2810 unsigned long long msr;
2811
2812 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002813 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002814 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2815
2816 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002817 case INTEL_FAM6_ATOM_SILVERMONT1:
2818 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05002819 return 30.0;
2820 default:
2821 return 135.0;
2822 }
2823}
2824
Andrey Semin40ee8e32014-12-05 00:07:00 -05002825/*
2826 * rapl_dram_energy_units_probe()
2827 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2828 */
2829static double
2830rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2831{
2832 /* only called for genuine_intel, family 6 */
2833
2834 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002835 case INTEL_FAM6_HASWELL_X: /* HSX */
2836 case INTEL_FAM6_BROADWELL_X: /* BDX */
2837 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2838 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05002839 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05002840 return (rapl_dram_energy_units = 15.3 / 1000000);
2841 default:
2842 return (rapl_energy_units);
2843 }
2844}
2845
Len Brown144b44b2013-11-09 00:30:16 -05002846
Len Brown889facb2012-11-08 00:48:57 -05002847/*
2848 * rapl_probe()
2849 *
Len Brown144b44b2013-11-09 00:30:16 -05002850 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002851 */
2852void rapl_probe(unsigned int family, unsigned int model)
2853{
2854 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002855 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002856 double tdp;
2857
2858 if (!genuine_intel)
2859 return;
2860
2861 if (family != 6)
2862 return;
2863
2864 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002865 case INTEL_FAM6_SANDYBRIDGE:
2866 case INTEL_FAM6_IVYBRIDGE:
2867 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2868 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2869 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2870 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2871 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05002872 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05002873 break;
Len Brown869ce692016-06-16 23:22:37 -04002874 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne4085d52016-04-06 17:15:56 -04002875 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
2876 break;
Len Brown869ce692016-06-16 23:22:37 -04002877 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2878 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2879 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2880 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04002881 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2882 break;
Len Brown869ce692016-06-16 23:22:37 -04002883 case INTEL_FAM6_HASWELL_X: /* HSX */
2884 case INTEL_FAM6_BROADWELL_X: /* BDX */
2885 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2886 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2887 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05002888 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04002889 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
Len Browne6f9bb32013-12-03 02:19:19 -05002890 break;
Len Brown869ce692016-06-16 23:22:37 -04002891 case INTEL_FAM6_SANDYBRIDGE_X:
2892 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04002893 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;
Len Brown144b44b2013-11-09 00:30:16 -05002894 break;
Len Brown869ce692016-06-16 23:22:37 -04002895 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
2896 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07002897 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown889facb2012-11-08 00:48:57 -05002898 break;
Len Brown869ce692016-06-16 23:22:37 -04002899 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07002900 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
2901 break;
Len Brown889facb2012-11-08 00:48:57 -05002902 default:
2903 return;
2904 }
2905
2906 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002907 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002908 return;
2909
2910 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04002911 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05002912 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2913 else
2914 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05002915
Andrey Semin40ee8e32014-12-05 00:07:00 -05002916 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2917
Len Brown144b44b2013-11-09 00:30:16 -05002918 time_unit = msr >> 16 & 0xF;
2919 if (time_unit == 0)
2920 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05002921
Len Brown144b44b2013-11-09 00:30:16 -05002922 rapl_time_units = 1.0 / (1 << (time_unit));
2923
2924 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05002925
2926 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05002927 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002928 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05002929
2930 return;
2931}
2932
Colin Ian King1b693172016-03-02 13:50:25 +00002933void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04002934{
2935 if (!genuine_intel)
2936 return;
2937
2938 if (family != 6)
2939 return;
2940
2941 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002942 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2943 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2944 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04002945 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04002946 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04002947 do_core_perf_limit_reasons = 1;
2948 do_ring_perf_limit_reasons = 1;
2949 default:
2950 return;
2951 }
2952}
2953
Len Brown889facb2012-11-08 00:48:57 -05002954int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2955{
2956 unsigned long long msr;
2957 unsigned int dts;
2958 int cpu;
2959
2960 if (!(do_dts || do_ptm))
2961 return 0;
2962
2963 cpu = t->cpu_id;
2964
2965 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05002966 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05002967 return 0;
2968
2969 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002970 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002971 return -1;
2972 }
2973
2974 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2975 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2976 return 0;
2977
2978 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002979 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002980 cpu, msr, tcc_activation_temp - dts);
2981
2982#ifdef THERM_DEBUG
2983 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2984 return 0;
2985
2986 dts = (msr >> 16) & 0x7F;
2987 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002988 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002989 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2990#endif
2991 }
2992
2993
2994 if (do_dts) {
2995 unsigned int resolution;
2996
2997 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2998 return 0;
2999
3000 dts = (msr >> 16) & 0x7F;
3001 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003002 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003003 cpu, msr, tcc_activation_temp - dts, resolution);
3004
3005#ifdef THERM_DEBUG
3006 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3007 return 0;
3008
3009 dts = (msr >> 16) & 0x7F;
3010 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003011 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003012 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3013#endif
3014 }
3015
3016 return 0;
3017}
Len Brown36229892016-02-26 20:51:02 -05003018
Len Brown889facb2012-11-08 00:48:57 -05003019void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3020{
Len Brownb7d8c142016-02-13 23:36:17 -05003021 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003022 cpu, label,
3023 ((msr >> 15) & 1) ? "EN" : "DIS",
3024 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3025 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3026 (((msr >> 16) & 1) ? "EN" : "DIS"));
3027
3028 return;
3029}
3030
3031int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3032{
3033 unsigned long long msr;
3034 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003035
3036 if (!do_rapl)
3037 return 0;
3038
3039 /* RAPL counters are per package, so print only for 1st thread/package */
3040 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3041 return 0;
3042
3043 cpu = t->cpu_id;
3044 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003045 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003046 return -1;
3047 }
3048
3049 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3050 return -1;
3051
Len Brownd8af6f52015-02-10 01:56:38 -05003052 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05003053 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
Len Brown889facb2012-11-08 00:48:57 -05003054 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05003055 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05003056 }
Len Brown144b44b2013-11-09 00:30:16 -05003057 if (do_rapl & RAPL_PKG_POWER_INFO) {
3058
Len Brown889facb2012-11-08 00:48:57 -05003059 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3060 return -5;
3061
3062
Len Brownb7d8c142016-02-13 23:36:17 -05003063 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
Len Brown889facb2012-11-08 00:48:57 -05003064 cpu, msr,
3065 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3066 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3067 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3068 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3069
Len Brown144b44b2013-11-09 00:30:16 -05003070 }
3071 if (do_rapl & RAPL_PKG) {
3072
Len Brown889facb2012-11-08 00:48:57 -05003073 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3074 return -9;
3075
Len Brownb7d8c142016-02-13 23:36:17 -05003076 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003077 cpu, msr, (msr >> 63) & 1 ? "": "UN");
3078
3079 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003080 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003081 cpu,
3082 ((msr >> 47) & 1) ? "EN" : "DIS",
3083 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3084 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3085 ((msr >> 48) & 1) ? "EN" : "DIS");
3086 }
3087
Len Brown0b2bb692015-03-26 00:50:30 -04003088 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003089 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3090 return -6;
3091
Len Brownb7d8c142016-02-13 23:36:17 -05003092 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
Len Brown889facb2012-11-08 00:48:57 -05003093 cpu, msr,
3094 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3095 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3096 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3097 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003098 }
3099 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003100 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3101 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003102 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003103 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3104
3105 print_power_limit_msr(cpu, msr, "DRAM Limit");
3106 }
Len Brown144b44b2013-11-09 00:30:16 -05003107 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05003108 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003109 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3110 return -7;
3111
Len Brownb7d8c142016-02-13 23:36:17 -05003112 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003113 }
3114 }
Jacob Pan91484942016-06-16 09:48:20 -07003115 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brownd8af6f52015-02-10 01:56:38 -05003116 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003117 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3118 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003119 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003120 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3121 print_power_limit_msr(cpu, msr, "Cores Limit");
3122 }
3123 }
3124 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05003125 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003126 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3127 return -8;
3128
Len Brownb7d8c142016-02-13 23:36:17 -05003129 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003130
3131 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3132 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003133 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003134 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3135 print_power_limit_msr(cpu, msr, "GFX Limit");
3136 }
3137 }
3138 return 0;
3139}
3140
Len Brownd7899442015-01-23 00:12:33 -05003141/*
3142 * SNB adds support for additional MSRs:
3143 *
3144 * MSR_PKG_C7_RESIDENCY 0x000003fa
3145 * MSR_CORE_C7_RESIDENCY 0x000003fe
3146 * MSR_PKG_C2_RESIDENCY 0x0000060d
3147 */
Len Brown103a8fe2010-10-22 23:53:03 -04003148
Len Brownd7899442015-01-23 00:12:33 -05003149int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003150{
3151 if (!genuine_intel)
3152 return 0;
3153
3154 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003155 case INTEL_FAM6_SANDYBRIDGE:
3156 case INTEL_FAM6_SANDYBRIDGE_X:
3157 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3158 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3159 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3160 case INTEL_FAM6_HASWELL_X: /* HSW */
3161 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3162 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3163 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3164 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3165 case INTEL_FAM6_BROADWELL_X: /* BDX */
3166 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3167 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3168 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3169 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3170 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3171 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3172 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003173 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003174 return 1;
3175 }
3176 return 0;
3177}
3178
Len Brownd7899442015-01-23 00:12:33 -05003179/*
3180 * HSW adds support for additional MSRs:
3181 *
Len Brown5a634262016-04-06 17:15:55 -04003182 * MSR_PKG_C8_RESIDENCY 0x00000630
3183 * MSR_PKG_C9_RESIDENCY 0x00000631
3184 * MSR_PKG_C10_RESIDENCY 0x00000632
3185 *
3186 * MSR_PKGC8_IRTL 0x00000633
3187 * MSR_PKGC9_IRTL 0x00000634
3188 * MSR_PKGC10_IRTL 0x00000635
3189 *
Len Brownd7899442015-01-23 00:12:33 -05003190 */
3191int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003192{
3193 if (!genuine_intel)
3194 return 0;
3195
3196 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003197 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3198 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3199 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3200 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3201 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3202 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3203 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003204 return 1;
3205 }
3206 return 0;
3207}
3208
Len Brown0b2bb692015-03-26 00:50:30 -04003209/*
3210 * SKL adds support for additional MSRS:
3211 *
3212 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3213 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3214 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3215 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3216 */
3217int has_skl_msrs(unsigned int family, unsigned int model)
3218{
3219 if (!genuine_intel)
3220 return 0;
3221
3222 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003223 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3224 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3225 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3226 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003227 return 1;
3228 }
3229 return 0;
3230}
3231
3232
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003233
Len Brown144b44b2013-11-09 00:30:16 -05003234int is_slm(unsigned int family, unsigned int model)
3235{
3236 if (!genuine_intel)
3237 return 0;
3238 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003239 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3240 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003241 return 1;
3242 }
3243 return 0;
3244}
3245
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003246int is_knl(unsigned int family, unsigned int model)
3247{
3248 if (!genuine_intel)
3249 return 0;
3250 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003251 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003252 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003253 return 1;
3254 }
3255 return 0;
3256}
3257
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003258unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3259{
3260 if (is_knl(family, model))
3261 return 1024;
3262 return 1;
3263}
3264
Len Brown144b44b2013-11-09 00:30:16 -05003265#define SLM_BCLK_FREQS 5
3266double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3267
3268double slm_bclk(void)
3269{
3270 unsigned long long msr = 3;
3271 unsigned int i;
3272 double freq;
3273
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003274 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003275 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003276
3277 i = msr & 0xf;
3278 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003279 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003280 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003281 }
3282 freq = slm_freq_table[i];
3283
Len Brownb7d8c142016-02-13 23:36:17 -05003284 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003285
3286 return freq;
3287}
3288
Len Brown103a8fe2010-10-22 23:53:03 -04003289double discover_bclk(unsigned int family, unsigned int model)
3290{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003291 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003292 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003293 else if (is_slm(family, model))
3294 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003295 else
3296 return 133.33;
3297}
3298
Len Brown889facb2012-11-08 00:48:57 -05003299/*
3300 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3301 * the Thermal Control Circuit (TCC) activates.
3302 * This is usually equal to tjMax.
3303 *
3304 * Older processors do not have this MSR, so there we guess,
3305 * but also allow cmdline over-ride with -T.
3306 *
3307 * Several MSR temperature values are in units of degrees-C
3308 * below this value, including the Digital Thermal Sensor (DTS),
3309 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3310 */
3311int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3312{
3313 unsigned long long msr;
3314 unsigned int target_c_local;
3315 int cpu;
3316
3317 /* tcc_activation_temp is used only for dts or ptm */
3318 if (!(do_dts || do_ptm))
3319 return 0;
3320
3321 /* this is a per-package concept */
3322 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3323 return 0;
3324
3325 cpu = t->cpu_id;
3326 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003327 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003328 return -1;
3329 }
3330
3331 if (tcc_activation_temp_override != 0) {
3332 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003333 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003334 cpu, tcc_activation_temp);
3335 return 0;
3336 }
3337
3338 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003339 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003340 goto guess;
3341
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003342 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003343 goto guess;
3344
Jean Delvare34821242014-05-01 11:40:19 +02003345 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003346
Len Brownd8af6f52015-02-10 01:56:38 -05003347 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003348 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003349 cpu, msr, target_c_local);
3350
Jean Delvare34821242014-05-01 11:40:19 +02003351 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003352 goto guess;
3353
3354 tcc_activation_temp = target_c_local;
3355
3356 return 0;
3357
3358guess:
3359 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003360 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003361 cpu, tcc_activation_temp);
3362
3363 return 0;
3364}
Len Brown69807a62015-11-21 12:22:47 -05003365
Len Brownaa8d8cc2016-03-11 13:26:03 -05003366void decode_feature_control_msr(void)
3367{
3368 unsigned long long msr;
3369
3370 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3371 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3372 base_cpu, msr,
3373 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3374 msr & (1 << 18) ? "SGX" : "");
3375}
3376
Len Brown69807a62015-11-21 12:22:47 -05003377void decode_misc_enable_msr(void)
3378{
3379 unsigned long long msr;
3380
3381 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003382 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
Len Brown69807a62015-11-21 12:22:47 -05003383 base_cpu, msr,
3384 msr & (1 << 3) ? "TCC" : "",
3385 msr & (1 << 16) ? "EIST" : "",
3386 msr & (1 << 18) ? "MONITOR" : "");
3387}
3388
Len Brownf0057312015-12-03 01:35:36 -05003389/*
3390 * Decode MSR_MISC_PWR_MGMT
3391 *
3392 * Decode the bits according to the Nehalem documentation
3393 * bit[0] seems to continue to have same meaning going forward
3394 * bit[1] less so...
3395 */
3396void decode_misc_pwr_mgmt_msr(void)
3397{
3398 unsigned long long msr;
3399
3400 if (!do_nhm_platform_info)
3401 return;
3402
3403 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003404 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
Len Brownf0057312015-12-03 01:35:36 -05003405 base_cpu, msr,
3406 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003407 msr & (1 << 1) ? "EN" : "DIS",
3408 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003409}
Len Brown7f5c2582015-12-01 01:36:39 -05003410
Len Brownfcd17212015-03-23 20:29:09 -04003411void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003412{
Len Brown61a87ba2015-11-23 02:30:51 -05003413 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003414 unsigned int fms, family, model, stepping;
3415
3416 eax = ebx = ecx = edx = 0;
3417
Len Brown5aea2f72016-03-13 03:14:35 -04003418 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003419
3420 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3421 genuine_intel = 1;
3422
Len Brownd8af6f52015-02-10 01:56:38 -05003423 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003424 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003425 (char *)&ebx, (char *)&edx, (char *)&ecx);
3426
Len Brown5aea2f72016-03-13 03:14:35 -04003427 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003428 family = (fms >> 8) & 0xf;
3429 model = (fms >> 4) & 0xf;
3430 stepping = fms & 0xf;
3431 if (family == 6 || family == 0xf)
3432 model += ((fms >> 16) & 0xf) << 4;
3433
Len Brown69807a62015-11-21 12:22:47 -05003434 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05003435 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003436 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003437 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003438 ecx & (1 << 0) ? "SSE3" : "-",
3439 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003440 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003441 ecx & (1 << 7) ? "EIST" : "-",
3442 ecx & (1 << 8) ? "TM2" : "-",
3443 edx & (1 << 4) ? "TSC" : "-",
3444 edx & (1 << 5) ? "MSR" : "-",
3445 edx & (1 << 22) ? "ACPI-TM" : "-",
3446 edx & (1 << 29) ? "TM" : "-");
3447 }
Len Brown103a8fe2010-10-22 23:53:03 -04003448
Josh Triplettb2c95d92013-08-20 17:20:18 -07003449 if (!(edx & (1 << 5)))
3450 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003451
3452 /*
3453 * check max extended function levels of CPUID.
3454 * This is needed to check for invariant TSC.
3455 * This check is valid for both Intel and AMD.
3456 */
3457 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003458 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003459
Len Brown61a87ba2015-11-23 02:30:51 -05003460 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003461
Len Brownd7899442015-01-23 00:12:33 -05003462 /*
3463 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3464 * this check is valid for both Intel and AMD
3465 */
Len Brown5aea2f72016-03-13 03:14:35 -04003466 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003467 has_invariant_tsc = edx & (1 << 8);
3468 }
Len Brown103a8fe2010-10-22 23:53:03 -04003469
3470 /*
3471 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3472 * this check is valid for both Intel and AMD
3473 */
3474
Len Brown5aea2f72016-03-13 03:14:35 -04003475 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003476 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05003477 do_dts = eax & (1 << 0);
3478 do_ptm = eax & (1 << 6);
Len Brown7f5c2582015-12-01 01:36:39 -05003479 has_hwp = eax & (1 << 7);
3480 has_hwp_notify = eax & (1 << 8);
3481 has_hwp_activity_window = eax & (1 << 9);
3482 has_hwp_epp = eax & (1 << 10);
3483 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003484 has_epb = ecx & (1 << 3);
3485
Len Brownd8af6f52015-02-10 01:56:38 -05003486 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003487 fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003488 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3489 has_aperf ? "" : "No-",
3490 do_dts ? "" : "No-",
3491 do_ptm ? "" : "No-",
3492 has_hwp ? "" : "No-",
3493 has_hwp_notify ? "" : "No-",
3494 has_hwp_activity_window ? "" : "No-",
3495 has_hwp_epp ? "" : "No-",
3496 has_hwp_pkg ? "" : "No-",
3497 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003498
Len Brown69807a62015-11-21 12:22:47 -05003499 if (debug)
3500 decode_misc_enable_msr();
3501
Len Brown8ae72252016-04-06 17:15:54 -04003502 if (max_level >= 0x7 && debug) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003503 int has_sgx;
3504
3505 ecx = 0;
3506
3507 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3508
3509 has_sgx = ebx & (1 << 2);
3510 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3511
3512 if (has_sgx)
3513 decode_feature_control_msr();
3514 }
3515
Len Brown61a87ba2015-11-23 02:30:51 -05003516 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003517 unsigned int eax_crystal;
3518 unsigned int ebx_tsc;
3519
3520 /*
3521 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3522 */
3523 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003524 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003525
3526 if (ebx_tsc != 0) {
3527
3528 if (debug && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003529 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003530 eax_crystal, ebx_tsc, crystal_hz);
3531
3532 if (crystal_hz == 0)
3533 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04003534 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3535 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3536 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3537 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003538 crystal_hz = 24000000; /* 24.0 MHz */
3539 break;
Len Brown869ce692016-06-16 23:22:37 -04003540 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003541 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003542 crystal_hz = 25000000; /* 25.0 MHz */
3543 break;
Len Brown869ce692016-06-16 23:22:37 -04003544 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne8efbc82016-04-06 17:15:57 -04003545 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003546 break;
3547 default:
3548 crystal_hz = 0;
3549 }
3550
3551 if (crystal_hz) {
3552 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
3553 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003554 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003555 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3556 }
3557 }
3558 }
Len Brown61a87ba2015-11-23 02:30:51 -05003559 if (max_level >= 0x16) {
3560 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3561
3562 /*
3563 * CPUID 16H Base MHz, Max MHz, Bus MHz
3564 */
3565 base_mhz = max_mhz = bus_mhz = edx = 0;
3566
Len Brown5aea2f72016-03-13 03:14:35 -04003567 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown61a87ba2015-11-23 02:30:51 -05003568 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003569 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003570 base_mhz, max_mhz, bus_mhz);
3571 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003572
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003573 if (has_aperf)
3574 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3575
Len Brownee7e38e2015-02-09 23:39:45 -05003576 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05003577 do_snb_cstates = has_snb_msrs(family, model);
Len Brown5a634262016-04-06 17:15:55 -04003578 do_irtl_snb = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05003579 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3580 do_pc3 = (pkg_cstate_limit >= PCL__3);
3581 do_pc6 = (pkg_cstate_limit >= PCL__6);
3582 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05003583 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown5a634262016-04-06 17:15:55 -04003584 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04003585 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05003586 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003587 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04003588
Len Brownf0057312015-12-03 01:35:36 -05003589 if (debug)
3590 decode_misc_pwr_mgmt_msr();
3591
Len Brown889facb2012-11-08 00:48:57 -05003592 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04003593 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05003594
Len Brownfcd17212015-03-23 20:29:09 -04003595 if (debug)
Colin Ian King1b693172016-03-02 13:50:25 +00003596 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003597
Len Browna2b7b742015-09-26 00:12:38 -04003598 if (has_skl_msrs(family, model))
3599 calculate_tsc_tweak();
3600
Len Brownfdf676e2016-02-27 01:28:12 -05003601 do_gfx_rc6_ms = !access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK);
3602
Len Brown27d47352016-02-27 00:37:54 -05003603 do_gfx_mhz = !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK);
3604
Len Brown889facb2012-11-08 00:48:57 -05003605 return;
Len Brown103a8fe2010-10-22 23:53:03 -04003606}
3607
Len Brownd8af6f52015-02-10 01:56:38 -05003608void help()
Len Brown103a8fe2010-10-22 23:53:03 -04003609{
Len Brownb7d8c142016-02-13 23:36:17 -05003610 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05003611 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3612 "\n"
3613 "Turbostat forks the specified COMMAND and prints statistics\n"
3614 "when COMMAND completes.\n"
3615 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3616 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05003617 "--add add a counter\n"
3618 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003619 "--debug run in \"debug\" mode\n"
3620 "--interval sec Override default 5-second measurement interval\n"
3621 "--help print this help message\n"
3622 "--counter msr print 32-bit counter at address \"msr\"\n"
3623 "--Counter msr print 64-bit Counter at address \"msr\"\n"
Len Brownb7d8c142016-02-13 23:36:17 -05003624 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003625 "--msr msr print 32-bit value at address \"msr\"\n"
3626 "--MSR msr print 64-bit Value at address \"msr\"\n"
3627 "--version print version information\n"
3628 "\n"
3629 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04003630}
3631
3632
3633/*
3634 * in /dev/cpu/ return success for names that are numbers
3635 * ie. filter out ".", "..", "microcode".
3636 */
3637int dir_filter(const struct dirent *dirp)
3638{
3639 if (isdigit(dirp->d_name[0]))
3640 return 1;
3641 else
3642 return 0;
3643}
3644
3645int open_dev_cpu_msr(int dummy1)
3646{
3647 return 0;
3648}
3649
Len Brownc98d5d92012-06-04 00:56:40 -04003650void topology_probe()
3651{
3652 int i;
3653 int max_core_id = 0;
3654 int max_package_id = 0;
3655 int max_siblings = 0;
3656 struct cpu_topology {
3657 int core_id;
3658 int physical_package_id;
3659 } *cpus;
3660
3661 /* Initialize num_cpus, max_cpu_num */
3662 topo.num_cpus = 0;
3663 topo.max_cpu_num = 0;
3664 for_all_proc_cpus(count_cpus);
3665 if (!summary_only && topo.num_cpus > 1)
3666 show_cpu = 1;
3667
Len Brownd8af6f52015-02-10 01:56:38 -05003668 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003669 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04003670
3671 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003672 if (cpus == NULL)
3673 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003674
3675 /*
3676 * Allocate and initialize cpu_present_set
3677 */
3678 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003679 if (cpu_present_set == NULL)
3680 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003681 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3682 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3683 for_all_proc_cpus(mark_cpu_present);
3684
3685 /*
3686 * Allocate and initialize cpu_affinity_set
3687 */
3688 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003689 if (cpu_affinity_set == NULL)
3690 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003691 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3692 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3693
3694
3695 /*
3696 * For online cpus
3697 * find max_core_id, max_package_id
3698 */
3699 for (i = 0; i <= topo.max_cpu_num; ++i) {
3700 int siblings;
3701
3702 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003703 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003704 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04003705 continue;
3706 }
3707 cpus[i].core_id = get_core_id(i);
3708 if (cpus[i].core_id > max_core_id)
3709 max_core_id = cpus[i].core_id;
3710
3711 cpus[i].physical_package_id = get_physical_package_id(i);
3712 if (cpus[i].physical_package_id > max_package_id)
3713 max_package_id = cpus[i].physical_package_id;
3714
3715 siblings = get_num_ht_siblings(i);
3716 if (siblings > max_siblings)
3717 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003718 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003719 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003720 i, cpus[i].physical_package_id, cpus[i].core_id);
3721 }
3722 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003723 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003724 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003725 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003726 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003727 show_core = 1;
3728
3729 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003730 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003731 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003732 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003733 if (debug && !summary_only && topo.num_packages > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003734 show_pkg = 1;
3735
3736 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003737 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003738 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04003739
3740 free(cpus);
3741}
3742
3743void
3744allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3745{
3746 int i;
3747
3748 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown388e9c82016-12-22 23:57:55 -05003749 topo.num_packages, sizeof(struct thread_data) + sys.thread_counter_bytes);
Len Brownc98d5d92012-06-04 00:56:40 -04003750 if (*t == NULL)
3751 goto error;
3752
3753 for (i = 0; i < topo.num_threads_per_core *
3754 topo.num_cores_per_pkg * topo.num_packages; i++)
3755 (*t)[i].cpu_id = -1;
3756
3757 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown388e9c82016-12-22 23:57:55 -05003758 sizeof(struct core_data) + sys.core_counter_bytes);
Len Brownc98d5d92012-06-04 00:56:40 -04003759 if (*c == NULL)
3760 goto error;
3761
3762 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3763 (*c)[i].core_id = -1;
3764
Len Brown388e9c82016-12-22 23:57:55 -05003765 *p = calloc(topo.num_packages, sizeof(struct pkg_data) + sys.package_counter_bytes);
Len Brownc98d5d92012-06-04 00:56:40 -04003766 if (*p == NULL)
3767 goto error;
3768
3769 for (i = 0; i < topo.num_packages; i++)
3770 (*p)[i].package_id = i;
3771
3772 return;
3773error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07003774 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04003775}
3776/*
3777 * init_counter()
3778 *
3779 * set cpu_id, core_num, pkg_num
3780 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3781 *
3782 * increment topo.num_cores when 1st core in pkg seen
3783 */
3784void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3785 struct pkg_data *pkg_base, int thread_num, int core_num,
3786 int pkg_num, int cpu_id)
3787{
3788 struct thread_data *t;
3789 struct core_data *c;
3790 struct pkg_data *p;
3791
3792 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3793 c = GET_CORE(core_base, core_num, pkg_num);
3794 p = GET_PKG(pkg_base, pkg_num);
3795
3796 t->cpu_id = cpu_id;
3797 if (thread_num == 0) {
3798 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3799 if (cpu_is_first_core_in_package(cpu_id))
3800 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3801 }
3802
3803 c->core_id = core_num;
3804 p->package_id = pkg_num;
3805}
3806
3807
3808int initialize_counters(int cpu_id)
3809{
3810 int my_thread_id, my_core_id, my_package_id;
3811
3812 my_package_id = get_physical_package_id(cpu_id);
3813 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07003814 my_thread_id = get_cpu_position_in_core(cpu_id);
3815 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04003816 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04003817
3818 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3819 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3820 return 0;
3821}
3822
3823void allocate_output_buffer()
3824{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003825 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04003826 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07003827 if (outp == NULL)
3828 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04003829}
Len Brown36229892016-02-26 20:51:02 -05003830void allocate_fd_percpu(void)
3831{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003832 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05003833 if (fd_percpu == NULL)
3834 err(-1, "calloc fd_percpu");
3835}
Len Brown562a2d32016-02-26 23:48:05 -05003836void allocate_irq_buffers(void)
3837{
3838 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
3839 if (irq_column_2_cpu == NULL)
3840 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04003841
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003842 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05003843 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003844 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05003845}
Len Brownc98d5d92012-06-04 00:56:40 -04003846void setup_all_buffers(void)
3847{
3848 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05003849 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05003850 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04003851 allocate_counters(&thread_even, &core_even, &package_even);
3852 allocate_counters(&thread_odd, &core_odd, &package_odd);
3853 allocate_output_buffer();
3854 for_all_proc_cpus(initialize_counters);
3855}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003856
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003857void set_base_cpu(void)
3858{
3859 base_cpu = sched_getcpu();
3860 if (base_cpu < 0)
3861 err(-ENODEV, "No valid cpus found");
3862
3863 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003864 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003865}
3866
Len Brown103a8fe2010-10-22 23:53:03 -04003867void turbostat_init()
3868{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003869 setup_all_buffers();
3870 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04003871 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04003872 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04003873 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04003874
Len Brown103a8fe2010-10-22 23:53:03 -04003875
Len Brownd8af6f52015-02-10 01:56:38 -05003876 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05003877 for_all_cpus(print_hwp, ODD_COUNTERS);
3878
3879 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003880 for_all_cpus(print_epb, ODD_COUNTERS);
3881
Len Brownd8af6f52015-02-10 01:56:38 -05003882 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04003883 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3884
Len Brownd8af6f52015-02-10 01:56:38 -05003885 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003886 for_all_cpus(print_rapl, ODD_COUNTERS);
3887
3888 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3889
Len Brownd8af6f52015-02-10 01:56:38 -05003890 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003891 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04003892
3893 if (debug && do_irtl_snb)
3894 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04003895}
3896
3897int fork_it(char **argv)
3898{
Len Brown103a8fe2010-10-22 23:53:03 -04003899 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04003900 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04003901
Len Brownd91bb172012-11-01 00:08:19 -04003902 status = for_all_cpus(get_counters, EVEN_COUNTERS);
3903 if (status)
3904 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04003905 /* clear affinity side-effect of get_counters() */
3906 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04003907 gettimeofday(&tv_even, (struct timezone *)NULL);
3908
3909 child_pid = fork();
3910 if (!child_pid) {
3911 /* child */
3912 execvp(argv[0], argv);
3913 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04003914
3915 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07003916 if (child_pid == -1)
3917 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04003918
3919 signal(SIGINT, SIG_IGN);
3920 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07003921 if (waitpid(child_pid, &status, 0) == -1)
3922 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04003923 }
Len Brownc98d5d92012-06-04 00:56:40 -04003924 /*
3925 * n.b. fork_it() does not check for errors from for_all_cpus()
3926 * because re-starting is problematic when forking
3927 */
3928 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003929 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04003930 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04003931 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
3932 fprintf(outf, "%s: Counter reset detected\n", progname);
3933 else {
3934 compute_average(EVEN_COUNTERS);
3935 format_all_counters(EVEN_COUNTERS);
3936 }
Len Brown103a8fe2010-10-22 23:53:03 -04003937
Len Brownb7d8c142016-02-13 23:36:17 -05003938 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3939
3940 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04003941
Len Brownd91bb172012-11-01 00:08:19 -04003942 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04003943}
3944
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003945int get_and_dump_counters(void)
3946{
3947 int status;
3948
3949 status = for_all_cpus(get_counters, ODD_COUNTERS);
3950 if (status)
3951 return status;
3952
3953 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3954 if (status)
3955 return status;
3956
Len Brownb7d8c142016-02-13 23:36:17 -05003957 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003958
3959 return status;
3960}
3961
Len Brownd8af6f52015-02-10 01:56:38 -05003962void print_version() {
Len Brown388e9c82016-12-22 23:57:55 -05003963 fprintf(outf, "turbostat version 4.15 21 Dec 2016"
Len Brownd8af6f52015-02-10 01:56:38 -05003964 " - Len Brown <lenb@kernel.org>\n");
3965}
3966
Len Brown388e9c82016-12-22 23:57:55 -05003967int add_counter(unsigned int msr_num, char *name, unsigned int width,
3968 enum counter_scope scope, enum counter_type type,
3969 enum counter_format format)
3970{
3971 struct msr_counter *msrp;
3972
3973 msrp = calloc(1, sizeof(struct msr_counter));
3974 if (msrp == NULL) {
3975 perror("calloc");
3976 exit(1);
3977 }
3978
3979 msrp->msr_num = msr_num;
3980 strncpy(msrp->name, name, NAME_BYTES);
3981 msrp->width = width;
3982 msrp->type = type;
3983 msrp->format = format;
3984
3985 switch (scope) {
3986
3987 case SCOPE_CPU:
3988 sys.thread_counter_bytes += 64;
3989 msrp->next = sys.tp;
3990 sys.tp = msrp;
3991 sys.thread_counter_bytes += sizeof(unsigned long long);
3992 break;
3993
3994 case SCOPE_CORE:
3995 sys.core_counter_bytes += 64;
3996 msrp->next = sys.cp;
3997 sys.cp = msrp;
3998 sys.core_counter_bytes += sizeof(unsigned long long);
3999 break;
4000
4001 case SCOPE_PACKAGE:
4002 sys.package_counter_bytes += 64;
4003 msrp->next = sys.pp;
4004 sys.pp = msrp;
4005 sys.package_counter_bytes += sizeof(unsigned long long);
4006 break;
4007 }
4008
4009 return 0;
4010}
4011
4012void parse_add_command(char *add_command)
4013{
4014 int msr_num = 0;
4015 char name_buffer[NAME_BYTES];
4016 int width = 64;
4017 int fail = 0;
4018 enum counter_scope scope = SCOPE_CPU;
4019 enum counter_type type = COUNTER_CYCLES;
4020 enum counter_format format = FORMAT_DELTA;
4021
4022 while (add_command) {
4023
4024 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4025 goto next;
4026
4027 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4028 goto next;
4029
4030 if (sscanf(add_command, "u%d", &width) == 1) {
4031 if ((width == 32) || (width == 64))
4032 goto next;
4033 width = 64;
4034 }
4035 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4036 scope = SCOPE_CPU;
4037 goto next;
4038 }
4039 if (!strncmp(add_command, "core", strlen("core"))) {
4040 scope = SCOPE_CORE;
4041 goto next;
4042 }
4043 if (!strncmp(add_command, "package", strlen("package"))) {
4044 scope = SCOPE_PACKAGE;
4045 goto next;
4046 }
4047 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4048 type = COUNTER_CYCLES;
4049 goto next;
4050 }
4051 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4052 type = COUNTER_SECONDS;
4053 goto next;
4054 }
4055 if (!strncmp(add_command, "raw", strlen("raw"))) {
4056 format = FORMAT_RAW;
4057 goto next;
4058 }
4059 if (!strncmp(add_command, "delta", strlen("delta"))) {
4060 format = FORMAT_DELTA;
4061 goto next;
4062 }
4063 if (!strncmp(add_command, "percent", strlen("percent"))) {
4064 format = FORMAT_PERCENT;
4065 goto next;
4066 }
4067
4068 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4069 char *eos;
4070
4071 eos = strchr(name_buffer, ',');
4072 if (eos)
4073 *eos = '\0';
4074 goto next;
4075 }
4076
4077next:
4078 add_command = strchr(add_command, ',');
4079 if (add_command)
4080 add_command++;
4081
4082 }
4083 if (msr_num == 0) {
4084 fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4085 fail++;
4086 }
4087
4088 /* generate default column header */
4089 if (*name_buffer == '\0') {
4090 if (format == FORMAT_RAW) {
4091 if (width == 32)
4092 sprintf(name_buffer, "msr%d", msr_num);
4093 else
4094 sprintf(name_buffer, "MSR%d", msr_num);
4095 } else if (format == FORMAT_DELTA) {
4096 if (width == 32)
4097 sprintf(name_buffer, "cnt%d", msr_num);
4098 else
4099 sprintf(name_buffer, "CNT%d", msr_num);
4100 } else if (format == FORMAT_PERCENT) {
4101 if (width == 32)
4102 sprintf(name_buffer, "msr%d%%", msr_num);
4103 else
4104 sprintf(name_buffer, "MSR%d%%", msr_num);
4105 }
4106 }
4107
4108 if (add_counter(msr_num, name_buffer, width, scope, type, format))
4109 fail++;
4110
4111 if (fail) {
4112 help();
4113 exit(1);
4114 }
4115}
Len Brown103a8fe2010-10-22 23:53:03 -04004116void cmdline(int argc, char **argv)
4117{
4118 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004119 int option_index = 0;
4120 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004121 {"add", required_argument, 0, 'a'},
Len Brownd8af6f52015-02-10 01:56:38 -05004122 {"Counter", required_argument, 0, 'C'},
4123 {"counter", required_argument, 0, 'c'},
4124 {"Dump", no_argument, 0, 'D'},
4125 {"debug", no_argument, 0, 'd'},
4126 {"interval", required_argument, 0, 'i'},
4127 {"help", no_argument, 0, 'h'},
4128 {"Joules", no_argument, 0, 'J'},
4129 {"MSR", required_argument, 0, 'M'},
4130 {"msr", required_argument, 0, 'm'},
Len Brownb7d8c142016-02-13 23:36:17 -05004131 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004132 {"Package", no_argument, 0, 'p'},
4133 {"processor", no_argument, 0, 'p'},
4134 {"Summary", no_argument, 0, 'S'},
4135 {"TCC", required_argument, 0, 'T'},
4136 {"version", no_argument, 0, 'v' },
4137 {0, 0, 0, 0 }
4138 };
Len Brown103a8fe2010-10-22 23:53:03 -04004139
4140 progname = argv[0];
4141
Len Brownb7d8c142016-02-13 23:36:17 -05004142 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004143 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004144 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004145 case 'a':
4146 parse_add_command(optarg);
4147 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004148 case 'C':
4149 sscanf(optarg, "%x", &extra_delta_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04004150 break;
Len Brownf9240812012-10-06 15:26:31 -04004151 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04004152 sscanf(optarg, "%x", &extra_delta_offset32);
4153 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004154 case 'D':
4155 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004156 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004157 case 'd':
4158 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004159 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004160 case 'h':
4161 default:
4162 help();
4163 exit(1);
4164 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004165 {
4166 double interval = strtod(optarg, NULL);
4167
4168 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004169 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004170 interval);
4171 exit(2);
4172 }
4173
4174 interval_ts.tv_sec = interval;
4175 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4176 }
Len Brown889facb2012-11-08 00:48:57 -05004177 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004178 case 'J':
4179 rapl_joules++;
4180 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004181 case 'M':
4182 sscanf(optarg, "%x", &extra_msr_offset64);
4183 break;
4184 case 'm':
4185 sscanf(optarg, "%x", &extra_msr_offset32);
4186 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004187 case 'o':
4188 outf = fopen_or_die(optarg, "w");
4189 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004190 case 'P':
4191 show_pkg_only++;
4192 break;
4193 case 'p':
4194 show_core_only++;
4195 break;
4196 case 'S':
4197 summary_only++;
4198 break;
4199 case 'T':
4200 tcc_activation_temp_override = atoi(optarg);
4201 break;
4202 case 'v':
4203 print_version();
4204 exit(0);
4205 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004206 }
4207 }
4208}
4209
4210int main(int argc, char **argv)
4211{
Len Brownb7d8c142016-02-13 23:36:17 -05004212 outf = stderr;
4213
Len Brown103a8fe2010-10-22 23:53:03 -04004214 cmdline(argc, argv);
4215
Len Brownd8af6f52015-02-10 01:56:38 -05004216 if (debug)
4217 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004218
4219 turbostat_init();
4220
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004221 /* dump counters and exit */
4222 if (dump_only)
4223 return get_and_dump_counters();
4224
Len Brown103a8fe2010-10-22 23:53:03 -04004225 /*
4226 * if any params left, it must be a command to fork
4227 */
4228 if (argc - optind)
4229 return fork_it(argv + optind);
4230 else
4231 turbostat_loop();
4232
4233 return 0;
4234}