blob: 20a257a12ea5000707edd0ba34a4368df79e35e2 [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
Josh Triplett95aebc42013-08-20 17:20:16 -070024#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040025#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070026#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040027#include <unistd.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/stat.h>
31#include <sys/resource.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050036#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040037#include <dirent.h>
38#include <string.h>
39#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040040#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050041#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070042#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040043#include <linux/capability.h>
44#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040045
Len Brown103a8fe2010-10-22 23:53:03 -040046char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050047FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050048int *fd_percpu;
Len Brown2a0609c2016-02-12 22:44:48 -050049struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050050unsigned int debug;
51unsigned int rapl_joules;
52unsigned int summary_only;
53unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040054unsigned int skip_c0;
55unsigned int skip_c1;
56unsigned int do_nhm_cstates;
57unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070058unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050059unsigned int do_pc2;
60unsigned int do_pc3;
61unsigned int do_pc6;
62unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080063unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040064unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050065unsigned int do_slm_cstates;
66unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040067unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050068unsigned int has_epb;
Len Brownfc04cc62014-02-06 00:55:19 -050069unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040070unsigned int genuine_intel;
71unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050072unsigned int do_nhm_platform_info;
Len Brown2f32edf2012-09-21 23:45:46 -040073unsigned int extra_msr_offset32;
74unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040075unsigned int extra_delta_offset32;
76unsigned int extra_delta_offset64;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020077unsigned int aperf_mperf_multiplier = 1;
Len Brown562a2d32016-02-26 23:48:05 -050078int do_irq = 1;
Len Brown1ed51012013-02-10 17:19:24 -050079int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040080double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040081double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040082unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040083double tsc_tweak = 1.0;
Len Brown103a8fe2010-10-22 23:53:03 -040084unsigned int show_pkg;
85unsigned int show_core;
86unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040087unsigned int show_pkg_only;
88unsigned int show_core_only;
89char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050090unsigned int do_rapl;
91unsigned int do_dts;
92unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050093unsigned int do_gfx_rc6_ms;
94unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050095unsigned int do_gfx_mhz;
96unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050097unsigned int tcc_activation_temp;
98unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050099double rapl_power_units, rapl_time_units;
100double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -0500101double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -0400102unsigned int do_core_perf_limit_reasons;
103unsigned int do_gfx_perf_limit_reasons;
104unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -0400105unsigned int crystal_hz;
106unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400107int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400108double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500109unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
110 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
111unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
112unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
113unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
114unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown889facb2012-11-08 00:48:57 -0500115
Len Browne6f9bb32013-12-03 02:19:19 -0500116#define RAPL_PKG (1 << 0)
117 /* 0x610 MSR_PKG_POWER_LIMIT */
118 /* 0x611 MSR_PKG_ENERGY_STATUS */
119#define RAPL_PKG_PERF_STATUS (1 << 1)
120 /* 0x613 MSR_PKG_PERF_STATUS */
121#define RAPL_PKG_POWER_INFO (1 << 2)
122 /* 0x614 MSR_PKG_POWER_INFO */
123
124#define RAPL_DRAM (1 << 3)
125 /* 0x618 MSR_DRAM_POWER_LIMIT */
126 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500127#define RAPL_DRAM_PERF_STATUS (1 << 4)
128 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400129#define RAPL_DRAM_POWER_INFO (1 << 5)
130 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500131
Len Brown0b2bb692015-03-26 00:50:30 -0400132#define RAPL_CORES (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500133 /* 0x638 MSR_PP0_POWER_LIMIT */
134 /* 0x639 MSR_PP0_ENERGY_STATUS */
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 */
Len Brown889facb2012-11-08 00:48:57 -0500142#define TJMAX_DEFAULT 100
143
144#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400145
146int aperf_mperf_unstable;
147int backwards_count;
148char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400149
Len Brownc98d5d92012-06-04 00:56:40 -0400150cpu_set_t *cpu_present_set, *cpu_affinity_set;
151size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400152
Len Brownc98d5d92012-06-04 00:56:40 -0400153struct thread_data {
154 unsigned long long tsc;
155 unsigned long long aperf;
156 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500157 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400158 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400159 unsigned long long extra_delta64;
160 unsigned long long extra_msr32;
161 unsigned long long extra_delta32;
Len Brown562a2d32016-02-26 23:48:05 -0500162 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500163 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400164 unsigned int cpu_id;
165 unsigned int flags;
166#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
167#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
168} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400169
Len Brownc98d5d92012-06-04 00:56:40 -0400170struct core_data {
171 unsigned long long c3;
172 unsigned long long c6;
173 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500174 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400175 unsigned int core_id;
176} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400177
Len Brownc98d5d92012-06-04 00:56:40 -0400178struct pkg_data {
179 unsigned long long pc2;
180 unsigned long long pc3;
181 unsigned long long pc6;
182 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800183 unsigned long long pc8;
184 unsigned long long pc9;
185 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400186 unsigned long long pkg_wtd_core_c0;
187 unsigned long long pkg_any_core_c0;
188 unsigned long long pkg_any_gfxe_c0;
189 unsigned long long pkg_both_core_gfxe_c0;
Len Brownfdf676e2016-02-27 01:28:12 -0500190 unsigned long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500191 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400192 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500193 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
194 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
195 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
196 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
197 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
198 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
199 unsigned int pkg_temp_c;
200
Len Brownc98d5d92012-06-04 00:56:40 -0400201} *package_even, *package_odd;
202
203#define ODD_COUNTERS thread_odd, core_odd, package_odd
204#define EVEN_COUNTERS thread_even, core_even, package_even
205
206#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
207 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
208 topo.num_threads_per_core + \
209 (core_no) * topo.num_threads_per_core + (thread_no))
210#define GET_CORE(core_base, core_no, pkg_no) \
211 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
212#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
213
214struct system_summary {
215 struct thread_data threads;
216 struct core_data cores;
217 struct pkg_data packages;
218} sum, average;
219
220
221struct topo_params {
222 int num_packages;
223 int num_cpus;
224 int num_cores;
225 int max_cpu_num;
226 int num_cores_per_pkg;
227 int num_threads_per_core;
228} topo;
229
230struct timeval tv_even, tv_odd, tv_delta;
231
Len Brown562a2d32016-02-26 23:48:05 -0500232int *irq_column_2_cpu; /* /proc/interrupts column numbers */
233int *irqs_per_cpu; /* indexed by cpu_num */
234
Len Brownc98d5d92012-06-04 00:56:40 -0400235void setup_all_buffers(void);
236
237int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400238{
Len Brownc98d5d92012-06-04 00:56:40 -0400239 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400240}
Len Brown88c32812012-03-29 21:44:40 -0400241/*
Len Brownc98d5d92012-06-04 00:56:40 -0400242 * run func(thread, core, package) in topology order
243 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400244 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400245
Len Brownc98d5d92012-06-04 00:56:40 -0400246int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
247 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400248{
Len Brownc98d5d92012-06-04 00:56:40 -0400249 int retval, pkg_no, core_no, thread_no;
250
251 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
252 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
253 for (thread_no = 0; thread_no <
254 topo.num_threads_per_core; ++thread_no) {
255 struct thread_data *t;
256 struct core_data *c;
257 struct pkg_data *p;
258
259 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
260
261 if (cpu_is_not_present(t->cpu_id))
262 continue;
263
264 c = GET_CORE(core_base, core_no, pkg_no);
265 p = GET_PKG(pkg_base, pkg_no);
266
267 retval = func(t, c, p);
268 if (retval)
269 return retval;
270 }
271 }
272 }
273 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400274}
275
276int cpu_migrate(int cpu)
277{
Len Brownc98d5d92012-06-04 00:56:40 -0400278 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
279 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
280 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400281 return -1;
282 else
283 return 0;
284}
Len Brown36229892016-02-26 20:51:02 -0500285int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400286{
Len Brown103a8fe2010-10-22 23:53:03 -0400287 char pathname[32];
288 int fd;
289
Len Brown36229892016-02-26 20:51:02 -0500290 fd = fd_percpu[cpu];
291
292 if (fd)
293 return fd;
294
Len Brown103a8fe2010-10-22 23:53:03 -0400295 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
296 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400297 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400298 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 -0400299
Len Brown36229892016-02-26 20:51:02 -0500300 fd_percpu[cpu] = fd;
301
302 return fd;
303}
304
305int get_msr(int cpu, off_t offset, unsigned long long *msr)
306{
307 ssize_t retval;
308
309 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400310
Len Brown98481e72014-08-15 00:36:50 -0400311 if (retval != sizeof *msr)
Len Brown36229892016-02-26 20:51:02 -0500312 err(-1, "msr %d offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400313
314 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400315}
316
Len Brownfc04cc62014-02-06 00:55:19 -0500317/*
318 * Example Format w/ field column widths:
319 *
Len Brown27d47352016-02-27 00:37:54 -0500320 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz IRQ SMI Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp GFXMHz Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
Len Brown562a2d32016-02-26 23:48:05 -0500321 * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500322 */
323
Len Browna829eb42011-02-10 23:36:34 -0500324void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400325{
326 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400327 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400328 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400329 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400330 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400331 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400332 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400333 outp += sprintf(outp, " Avg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500334 if (has_aperf)
Len Brown75d2e442016-02-13 23:41:53 -0500335 outp += sprintf(outp, " Busy%%");
Len Brownfc04cc62014-02-06 00:55:19 -0500336 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400337 outp += sprintf(outp, " Bzy_MHz");
338 outp += sprintf(outp, " TSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500339
Len Brown8e180f32012-09-22 01:25:08 -0400340 if (extra_delta_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400341 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400342 if (extra_delta_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400343 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400344 if (extra_msr_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400345 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400346 if (extra_msr_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400347 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown1cc21f72015-02-23 00:34:57 -0500348
349 if (!debug)
350 goto done;
351
Len Brown562a2d32016-02-26 23:48:05 -0500352 if (do_irq)
353 outp += sprintf(outp, " IRQ");
Len Brown1cc21f72015-02-23 00:34:57 -0500354 if (do_smi)
355 outp += sprintf(outp, " SMI");
356
Len Brown103a8fe2010-10-22 23:53:03 -0400357 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400358 outp += sprintf(outp, " CPU%%c1");
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700359 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400360 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400361 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400362 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400363 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400364 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500365
366 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400367 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500368 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400369 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500370
Len Brownfdf676e2016-02-27 01:28:12 -0500371 if (do_gfx_rc6_ms)
372 outp += sprintf(outp, " GFX%%rc6");
373
Len Brown27d47352016-02-27 00:37:54 -0500374 if (do_gfx_mhz)
375 outp += sprintf(outp, " GFXMHz");
376
Len Brown0b2bb692015-03-26 00:50:30 -0400377 if (do_skl_residency) {
378 outp += sprintf(outp, " Totl%%C0");
379 outp += sprintf(outp, " Any%%C0");
380 outp += sprintf(outp, " GFX%%C0");
381 outp += sprintf(outp, " CPUGFX%%");
382 }
383
Len Brownee7e38e2015-02-09 23:39:45 -0500384 if (do_pc2)
Len Browne7c95ff2014-08-14 21:22:13 -0400385 outp += sprintf(outp, " Pkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500386 if (do_pc3)
Len Browne7c95ff2014-08-14 21:22:13 -0400387 outp += sprintf(outp, " Pkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500388 if (do_pc6)
Len Browne7c95ff2014-08-14 21:22:13 -0400389 outp += sprintf(outp, " Pkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500390 if (do_pc7)
Len Browne7c95ff2014-08-14 21:22:13 -0400391 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800392 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400393 outp += sprintf(outp, " Pkg%%pc8");
394 outp += sprintf(outp, " Pkg%%pc9");
395 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800396 }
Len Brown103a8fe2010-10-22 23:53:03 -0400397
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800398 if (do_rapl && !rapl_joules) {
399 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400400 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800401 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400402 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800403 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400404 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800405 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400406 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800407 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400408 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800409 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400410 outp += sprintf(outp, " RAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500411 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800412 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400413 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800414 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400415 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800416 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400417 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800418 if (do_rapl & RAPL_DRAM)
Len Brownbd6906e2015-07-24 10:35:23 -0400419 outp += sprintf(outp, " RAM_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800420 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400421 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800422 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400423 outp += sprintf(outp, " RAM_%%");
424 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500425
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800426 }
Len Brown1cc21f72015-02-23 00:34:57 -0500427 done:
Len Brownc98d5d92012-06-04 00:56:40 -0400428 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400429}
430
Len Brownc98d5d92012-06-04 00:56:40 -0400431int dump_counters(struct thread_data *t, struct core_data *c,
432 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400433{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200434 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400435
Len Brownc98d5d92012-06-04 00:56:40 -0400436 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200437 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
438 t->cpu_id, t->flags);
439 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
440 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
441 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
442 outp += sprintf(outp, "c1: %016llX\n", t->c1);
443 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400444 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200445 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400446 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200447 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400448 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200449 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400450 extra_msr_offset64, t->extra_msr64);
Len Brown562a2d32016-02-26 23:48:05 -0500451 if (do_irq)
452 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown1ed51012013-02-10 17:19:24 -0500453 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200454 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400455 }
Len Brown103a8fe2010-10-22 23:53:03 -0400456
Len Brownc98d5d92012-06-04 00:56:40 -0400457 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200458 outp += sprintf(outp, "core: %d\n", c->core_id);
459 outp += sprintf(outp, "c3: %016llX\n", c->c3);
460 outp += sprintf(outp, "c6: %016llX\n", c->c6);
461 outp += sprintf(outp, "c7: %016llX\n", c->c7);
462 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400463 }
464
465 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200466 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400467
468 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
469 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
470 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
471 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
472
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200473 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500474 if (do_pc3)
475 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
476 if (do_pc6)
477 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
478 if (do_pc7)
479 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200480 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
481 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
482 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
483 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
484 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
485 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
486 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
487 outp += sprintf(outp, "Throttle PKG: %0X\n",
488 p->rapl_pkg_perf_status);
489 outp += sprintf(outp, "Throttle RAM: %0X\n",
490 p->rapl_dram_perf_status);
491 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400492 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200493
494 outp += sprintf(outp, "\n");
495
Len Brownc98d5d92012-06-04 00:56:40 -0400496 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400497}
498
Len Browne23da032012-02-06 18:37:16 -0500499/*
500 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500501 */
Len Brownc98d5d92012-06-04 00:56:40 -0400502int format_counters(struct thread_data *t, struct core_data *c,
503 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400504{
505 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500506 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400507
Len Brownc98d5d92012-06-04 00:56:40 -0400508 /* if showing only 1st thread in core and this isn't one, bail out */
509 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
510 return 0;
511
512 /* if showing only 1st thread in pkg and this isn't one, bail out */
513 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
514 return 0;
515
Len Brown103a8fe2010-10-22 23:53:03 -0400516 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
517
Len Brownc98d5d92012-06-04 00:56:40 -0400518 /* topo columns, print blanks on 1st (average) line */
519 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400520 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500521 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400522 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500523 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400524 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500525 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400526 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400527 if (show_pkg) {
528 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500529 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400530 else
Len Brownfc04cc62014-02-06 00:55:19 -0500531 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400532 }
Len Brownc98d5d92012-06-04 00:56:40 -0400533 if (show_core) {
534 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500535 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400536 else
Len Brownfc04cc62014-02-06 00:55:19 -0500537 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400538 }
Len Brown103a8fe2010-10-22 23:53:03 -0400539 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500540 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400541 }
Len Brownfc04cc62014-02-06 00:55:19 -0500542
Len Brownd7899442015-01-23 00:12:33 -0500543 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500544 if (has_aperf)
545 outp += sprintf(outp, "%8.0f",
546 1.0 / units * t->aperf / interval_float);
547
Len Brown75d2e442016-02-13 23:41:53 -0500548 /* Busy% */
Len Brownd7899442015-01-23 00:12:33 -0500549 if (has_aperf) {
Len Brown103a8fe2010-10-22 23:53:03 -0400550 if (!skip_c0)
Len Browna2b7b742015-09-26 00:12:38 -0400551 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400552 else
Len Brownfc04cc62014-02-06 00:55:19 -0500553 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400554 }
555
Len Brownd7899442015-01-23 00:12:33 -0500556 /* Bzy_MHz */
Len Brown21ed5572015-10-19 22:37:40 -0400557 if (has_aperf) {
558 if (has_base_hz)
559 outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf);
560 else
561 outp += sprintf(outp, "%8.0f",
562 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
563 }
Len Brown103a8fe2010-10-22 23:53:03 -0400564
Len Brownd7899442015-01-23 00:12:33 -0500565 /* TSC_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500566 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400567
Len Brown8e180f32012-09-22 01:25:08 -0400568 /* delta */
569 if (extra_delta_offset32)
570 outp += sprintf(outp, " %11llu", t->extra_delta32);
571
572 /* DELTA */
573 if (extra_delta_offset64)
574 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400575 /* msr */
576 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400577 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400578
Len Brown130ff302012-09-21 22:56:06 -0400579 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400580 if (extra_msr_offset64)
581 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400582
Len Brown1cc21f72015-02-23 00:34:57 -0500583 if (!debug)
584 goto done;
585
Len Brown562a2d32016-02-26 23:48:05 -0500586 /* IRQ */
587 if (do_irq)
588 outp += sprintf(outp, "%8d", t->irq_count);
589
Len Brown1cc21f72015-02-23 00:34:57 -0500590 /* SMI */
591 if (do_smi)
592 outp += sprintf(outp, "%8d", t->smi_count);
593
Len Brown103a8fe2010-10-22 23:53:03 -0400594 if (do_nhm_cstates) {
595 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500596 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400597 else
Len Brownfc04cc62014-02-06 00:55:19 -0500598 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400599 }
Len Brownc98d5d92012-06-04 00:56:40 -0400600
601 /* print per-core data only for 1st thread in core */
602 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
603 goto done;
604
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700605 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500606 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400607 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500608 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400609 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500610 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400611
Len Brown889facb2012-11-08 00:48:57 -0500612 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500613 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500614
Len Brownc98d5d92012-06-04 00:56:40 -0400615 /* print per-package data only for 1st core in package */
616 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
617 goto done;
618
Len Brown0b2bb692015-03-26 00:50:30 -0400619 /* PkgTmp */
Len Brown889facb2012-11-08 00:48:57 -0500620 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500621 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500622
Len Brownfdf676e2016-02-27 01:28:12 -0500623 /* GFXrc6 */
624 if (do_gfx_rc6_ms)
625 outp += sprintf(outp, "%8.2f", 100.0 * p->gfx_rc6_ms / 1000.0 / interval_float);
626
Len Brown27d47352016-02-27 00:37:54 -0500627 /* GFXMHz */
628 if (do_gfx_mhz)
629 outp += sprintf(outp, "%8d", p->gfx_mhz);
630
Len Brown0b2bb692015-03-26 00:50:30 -0400631 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
632 if (do_skl_residency) {
633 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
634 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
635 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
636 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
637 }
638
Len Brownee7e38e2015-02-09 23:39:45 -0500639 if (do_pc2)
Len Brownfc04cc62014-02-06 00:55:19 -0500640 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500641 if (do_pc3)
Len Brownfc04cc62014-02-06 00:55:19 -0500642 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500643 if (do_pc6)
Len Brownfc04cc62014-02-06 00:55:19 -0500644 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500645 if (do_pc7)
Len Brownfc04cc62014-02-06 00:55:19 -0500646 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800647 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500648 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
649 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
650 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800651 }
Len Brown889facb2012-11-08 00:48:57 -0500652
653 /*
654 * If measurement interval exceeds minimum RAPL Joule Counter range,
655 * indicate that results are suspect by printing "**" in fraction place.
656 */
Len Brownfc04cc62014-02-06 00:55:19 -0500657 if (interval_float < rapl_joule_counter_range)
658 fmt8 = "%8.2f";
659 else
660 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500661
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800662 if (do_rapl && !rapl_joules) {
663 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500664 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800665 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500666 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800667 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500668 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800669 if (do_rapl & RAPL_DRAM)
Andrey Semin40ee8e32014-12-05 00:07:00 -0500670 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800671 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500672 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800673 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500674 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500675 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800676 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500677 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800678 p->energy_pkg * rapl_energy_units);
679 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500680 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800681 p->energy_cores * rapl_energy_units);
682 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500683 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800684 p->energy_gfx * rapl_energy_units);
685 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500686 outp += sprintf(outp, fmt8,
Andrey Semin40ee8e32014-12-05 00:07:00 -0500687 p->energy_dram * rapl_dram_energy_units);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800688 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500689 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800690 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500691 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500692
Len Brownd7899442015-01-23 00:12:33 -0500693 outp += sprintf(outp, fmt8, interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800694 }
Len Brownc98d5d92012-06-04 00:56:40 -0400695done:
Len Brownc98d5d92012-06-04 00:56:40 -0400696 outp += sprintf(outp, "\n");
697
698 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400699}
700
Len Brownb7d8c142016-02-13 23:36:17 -0500701void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400702{
Len Brownb7d8c142016-02-13 23:36:17 -0500703 FILE *filep;
704
705 if (outf == stderr)
706 filep = stdout;
707 else
708 filep = outf;
709
710 fputs(output_buffer, filep);
711 fflush(filep);
712
Len Brownc98d5d92012-06-04 00:56:40 -0400713 outp = output_buffer;
714}
Len Brownb7d8c142016-02-13 23:36:17 -0500715void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400716{
Len Brownb7d8c142016-02-13 23:36:17 -0500717 fputs(output_buffer, outf);
718 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400719 outp = output_buffer;
720}
721void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
722{
Len Browne23da032012-02-06 18:37:16 -0500723 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400724
Len Browne23da032012-02-06 18:37:16 -0500725 if (!printed || !summary_only)
726 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400727
Len Brownc98d5d92012-06-04 00:56:40 -0400728 if (topo.num_cpus > 1)
729 format_counters(&average.threads, &average.cores,
730 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400731
Len Browne23da032012-02-06 18:37:16 -0500732 printed = 1;
733
734 if (summary_only)
735 return;
736
Len Brownc98d5d92012-06-04 00:56:40 -0400737 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400738}
739
Len Brown889facb2012-11-08 00:48:57 -0500740#define DELTA_WRAP32(new, old) \
741 if (new > old) { \
742 old = new - old; \
743 } else { \
744 old = 0x100000000 + new - old; \
745 }
746
Len Brownc98d5d92012-06-04 00:56:40 -0400747void
748delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400749{
Len Brown0b2bb692015-03-26 00:50:30 -0400750
751 if (do_skl_residency) {
752 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
753 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
754 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
755 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
756 }
Len Brownc98d5d92012-06-04 00:56:40 -0400757 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500758 if (do_pc3)
759 old->pc3 = new->pc3 - old->pc3;
760 if (do_pc6)
761 old->pc6 = new->pc6 - old->pc6;
762 if (do_pc7)
763 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800764 old->pc8 = new->pc8 - old->pc8;
765 old->pc9 = new->pc9 - old->pc9;
766 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500767 old->pkg_temp_c = new->pkg_temp_c;
768
Len Brownfdf676e2016-02-27 01:28:12 -0500769 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500770 old->gfx_mhz = new->gfx_mhz;
771
Len Brown889facb2012-11-08 00:48:57 -0500772 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
773 DELTA_WRAP32(new->energy_cores, old->energy_cores);
774 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
775 DELTA_WRAP32(new->energy_dram, old->energy_dram);
776 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
777 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400778}
Len Brown103a8fe2010-10-22 23:53:03 -0400779
Len Brownc98d5d92012-06-04 00:56:40 -0400780void
781delta_core(struct core_data *new, struct core_data *old)
782{
783 old->c3 = new->c3 - old->c3;
784 old->c6 = new->c6 - old->c6;
785 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500786 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400787}
Len Brown103a8fe2010-10-22 23:53:03 -0400788
Len Brownc3ae3312012-06-13 21:31:46 -0400789/*
790 * old = new - old
791 */
Len Brownc98d5d92012-06-04 00:56:40 -0400792void
793delta_thread(struct thread_data *new, struct thread_data *old,
794 struct core_data *core_delta)
795{
796 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400797
Len Brownc98d5d92012-06-04 00:56:40 -0400798 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700799 if (old->tsc < (1000 * 1000))
800 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
801 "You can disable all c-states by booting with \"idle=poll\"\n"
802 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400803
Len Brownc98d5d92012-06-04 00:56:40 -0400804 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400805
Len Browna7296172015-01-23 01:33:58 -0500806 if (has_aperf) {
807 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
808 old->aperf = new->aperf - old->aperf;
809 old->mperf = new->mperf - old->mperf;
810 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400811
Len Browna7296172015-01-23 01:33:58 -0500812 if (!aperf_mperf_unstable) {
Len Brownb7d8c142016-02-13 23:36:17 -0500813 fprintf(outf, "%s: APERF or MPERF went backwards *\n", progname);
814 fprintf(outf, "* Frequency results do not cover entire interval *\n");
815 fprintf(outf, "* fix this by running Linux-2.6.30 or later *\n");
Len Brownc98d5d92012-06-04 00:56:40 -0400816
Len Browna7296172015-01-23 01:33:58 -0500817 aperf_mperf_unstable = 1;
818 }
819 /*
820 * mperf delta is likely a huge "positive" number
821 * can not use it for calculating c0 time
822 */
823 skip_c0 = 1;
824 skip_c1 = 1;
Len Brownc98d5d92012-06-04 00:56:40 -0400825 }
Len Brownc98d5d92012-06-04 00:56:40 -0400826 }
Len Brown103a8fe2010-10-22 23:53:03 -0400827
Len Brown103a8fe2010-10-22 23:53:03 -0400828
Len Brown144b44b2013-11-09 00:30:16 -0500829 if (use_c1_residency_msr) {
830 /*
831 * Some models have a dedicated C1 residency MSR,
832 * which should be more accurate than the derivation below.
833 */
834 } else {
835 /*
836 * As counter collection is not atomic,
837 * it is possible for mperf's non-halted cycles + idle states
838 * to exceed TSC's all cycles: show c1 = 0% in that case.
839 */
840 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
841 old->c1 = 0;
842 else {
843 /* normal case, derive c1 */
844 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400845 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500846 }
Len Brownc98d5d92012-06-04 00:56:40 -0400847 }
Len Brownc3ae3312012-06-13 21:31:46 -0400848
Len Brownc98d5d92012-06-04 00:56:40 -0400849 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -0500850 if (debug > 1)
851 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400852 old->mperf = 1; /* divide by 0 protection */
853 }
854
Len Brown8e180f32012-09-22 01:25:08 -0400855 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
856 old->extra_delta32 &= 0xFFFFFFFF;
857
858 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
859
Len Brownc98d5d92012-06-04 00:56:40 -0400860 /*
Len Brown8e180f32012-09-22 01:25:08 -0400861 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400862 */
Len Brown2f32edf2012-09-21 23:45:46 -0400863 old->extra_msr32 = new->extra_msr32;
864 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500865
Len Brown562a2d32016-02-26 23:48:05 -0500866 if (do_irq)
867 old->irq_count = new->irq_count - old->irq_count;
868
Len Brown1ed51012013-02-10 17:19:24 -0500869 if (do_smi)
870 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400871}
872
873int delta_cpu(struct thread_data *t, struct core_data *c,
874 struct pkg_data *p, struct thread_data *t2,
875 struct core_data *c2, struct pkg_data *p2)
876{
877 /* calculate core delta only for 1st thread in core */
878 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
879 delta_core(c, c2);
880
881 /* always calculate thread delta */
882 delta_thread(t, t2, c2); /* c2 is core delta */
883
884 /* calculate package delta only for 1st core in package */
885 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
886 delta_package(p, p2);
887
888 return 0;
889}
890
891void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
892{
893 t->tsc = 0;
894 t->aperf = 0;
895 t->mperf = 0;
896 t->c1 = 0;
897
Len Brown8e180f32012-09-22 01:25:08 -0400898 t->extra_delta32 = 0;
899 t->extra_delta64 = 0;
900
Len Brown562a2d32016-02-26 23:48:05 -0500901 t->irq_count = 0;
902 t->smi_count = 0;
903
Len Brownc98d5d92012-06-04 00:56:40 -0400904 /* tells format_counters to dump all fields from this set */
905 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
906
907 c->c3 = 0;
908 c->c6 = 0;
909 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500910 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400911
Len Brown0b2bb692015-03-26 00:50:30 -0400912 p->pkg_wtd_core_c0 = 0;
913 p->pkg_any_core_c0 = 0;
914 p->pkg_any_gfxe_c0 = 0;
915 p->pkg_both_core_gfxe_c0 = 0;
916
Len Brownc98d5d92012-06-04 00:56:40 -0400917 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -0500918 if (do_pc3)
919 p->pc3 = 0;
920 if (do_pc6)
921 p->pc6 = 0;
922 if (do_pc7)
923 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800924 p->pc8 = 0;
925 p->pc9 = 0;
926 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500927
928 p->energy_pkg = 0;
929 p->energy_dram = 0;
930 p->energy_cores = 0;
931 p->energy_gfx = 0;
932 p->rapl_pkg_perf_status = 0;
933 p->rapl_dram_perf_status = 0;
934 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -0500935
Len Brownfdf676e2016-02-27 01:28:12 -0500936 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -0500937 p->gfx_mhz = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400938}
939int sum_counters(struct thread_data *t, struct core_data *c,
940 struct pkg_data *p)
941{
942 average.threads.tsc += t->tsc;
943 average.threads.aperf += t->aperf;
944 average.threads.mperf += t->mperf;
945 average.threads.c1 += t->c1;
946
Len Brown8e180f32012-09-22 01:25:08 -0400947 average.threads.extra_delta32 += t->extra_delta32;
948 average.threads.extra_delta64 += t->extra_delta64;
949
Len Brown562a2d32016-02-26 23:48:05 -0500950 average.threads.irq_count += t->irq_count;
951 average.threads.smi_count += t->smi_count;
952
Len Brownc98d5d92012-06-04 00:56:40 -0400953 /* sum per-core values only for 1st thread in core */
954 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
955 return 0;
956
957 average.cores.c3 += c->c3;
958 average.cores.c6 += c->c6;
959 average.cores.c7 += c->c7;
960
Len Brown889facb2012-11-08 00:48:57 -0500961 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
962
Len Brownc98d5d92012-06-04 00:56:40 -0400963 /* sum per-pkg values only for 1st core in pkg */
964 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
965 return 0;
966
Len Brown0b2bb692015-03-26 00:50:30 -0400967 if (do_skl_residency) {
968 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
969 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
970 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
971 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
972 }
973
Len Brownc98d5d92012-06-04 00:56:40 -0400974 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500975 if (do_pc3)
976 average.packages.pc3 += p->pc3;
977 if (do_pc6)
978 average.packages.pc6 += p->pc6;
979 if (do_pc7)
980 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800981 average.packages.pc8 += p->pc8;
982 average.packages.pc9 += p->pc9;
983 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400984
Len Brown889facb2012-11-08 00:48:57 -0500985 average.packages.energy_pkg += p->energy_pkg;
986 average.packages.energy_dram += p->energy_dram;
987 average.packages.energy_cores += p->energy_cores;
988 average.packages.energy_gfx += p->energy_gfx;
989
Len Brownfdf676e2016-02-27 01:28:12 -0500990 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500991 average.packages.gfx_mhz = p->gfx_mhz;
992
Len Brown889facb2012-11-08 00:48:57 -0500993 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
994
995 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
996 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400997 return 0;
998}
999/*
1000 * sum the counters for all cpus in the system
1001 * compute the weighted average
1002 */
1003void compute_average(struct thread_data *t, struct core_data *c,
1004 struct pkg_data *p)
1005{
1006 clear_counters(&average.threads, &average.cores, &average.packages);
1007
1008 for_all_cpus(sum_counters, t, c, p);
1009
1010 average.threads.tsc /= topo.num_cpus;
1011 average.threads.aperf /= topo.num_cpus;
1012 average.threads.mperf /= topo.num_cpus;
1013 average.threads.c1 /= topo.num_cpus;
1014
Len Brown8e180f32012-09-22 01:25:08 -04001015 average.threads.extra_delta32 /= topo.num_cpus;
1016 average.threads.extra_delta32 &= 0xFFFFFFFF;
1017
1018 average.threads.extra_delta64 /= topo.num_cpus;
1019
Len Brownc98d5d92012-06-04 00:56:40 -04001020 average.cores.c3 /= topo.num_cores;
1021 average.cores.c6 /= topo.num_cores;
1022 average.cores.c7 /= topo.num_cores;
1023
Len Brown0b2bb692015-03-26 00:50:30 -04001024 if (do_skl_residency) {
1025 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1026 average.packages.pkg_any_core_c0 /= topo.num_packages;
1027 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1028 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1029 }
1030
Len Brownc98d5d92012-06-04 00:56:40 -04001031 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -05001032 if (do_pc3)
1033 average.packages.pc3 /= topo.num_packages;
1034 if (do_pc6)
1035 average.packages.pc6 /= topo.num_packages;
1036 if (do_pc7)
1037 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001038
1039 average.packages.pc8 /= topo.num_packages;
1040 average.packages.pc9 /= topo.num_packages;
1041 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -04001042}
1043
1044static unsigned long long rdtsc(void)
1045{
1046 unsigned int low, high;
1047
1048 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1049
1050 return low | ((unsigned long long)high) << 32;
1051}
1052
Len Brownc98d5d92012-06-04 00:56:40 -04001053/*
1054 * get_counters(...)
1055 * migrate to cpu
1056 * acquire and record local counters for that cpu
1057 */
1058int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1059{
1060 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001061 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001062 int aperf_mperf_retry_count = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001063
Len Browne52966c2012-11-08 22:38:05 -05001064 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001065 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001066 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001067 }
Len Brownc98d5d92012-06-04 00:56:40 -04001068
Len Brown0102b062016-02-27 03:11:29 -05001069retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001070 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1071
1072 if (has_aperf) {
Len Brown0102b062016-02-27 03:11:29 -05001073 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1074
1075 /*
1076 * The TSC, APERF and MPERF must be read together for
1077 * APERF/MPERF and MPERF/TSC to give accurate results.
1078 *
1079 * Unfortunately, APERF and MPERF are read by
1080 * individual system call, so delays may occur
1081 * between them. If the time to read them
1082 * varies by a large amount, we re-read them.
1083 */
1084
1085 /*
1086 * This initial dummy APERF read has been seen to
1087 * reduce jitter in the subsequent reads.
1088 */
1089
Len Brown9c63a652012-10-31 01:29:52 -04001090 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001091 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001092
1093 t->tsc = rdtsc(); /* re-read close to APERF */
1094
1095 tsc_before = t->tsc;
1096
1097 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1098 return -3;
1099
1100 tsc_between = rdtsc();
1101
Len Brown9c63a652012-10-31 01:29:52 -04001102 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001103 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001104
1105 tsc_after = rdtsc();
1106
1107 aperf_time = tsc_between - tsc_before;
1108 mperf_time = tsc_after - tsc_between;
1109
1110 /*
1111 * If the system call latency to read APERF and MPERF
1112 * differ by more than 2x, then try again.
1113 */
1114 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1115 aperf_mperf_retry_count++;
1116 if (aperf_mperf_retry_count < 5)
1117 goto retry;
1118 else
1119 warnx("cpu%d jitter %lld %lld",
1120 cpu, aperf_time, mperf_time);
1121 }
1122 aperf_mperf_retry_count = 0;
1123
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001124 t->aperf = t->aperf * aperf_mperf_multiplier;
1125 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001126 }
1127
Len Brown562a2d32016-02-26 23:48:05 -05001128 if (do_irq)
1129 t->irq_count = irqs_per_cpu[cpu];
Len Brown1ed51012013-02-10 17:19:24 -05001130 if (do_smi) {
1131 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1132 return -5;
1133 t->smi_count = msr & 0xFFFFFFFF;
1134 }
Len Brown8e180f32012-09-22 01:25:08 -04001135 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001136 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -04001137 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001138 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001139 }
1140
1141 if (extra_delta_offset64)
1142 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1143 return -5;
1144
1145 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001146 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -04001147 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001148 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001149 }
Len Brown2f32edf2012-09-21 23:45:46 -04001150
1151 if (extra_msr_offset64)
1152 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -04001153 return -5;
1154
Len Brown144b44b2013-11-09 00:30:16 -05001155 if (use_c1_residency_msr) {
1156 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1157 return -6;
1158 }
1159
Len Brownc98d5d92012-06-04 00:56:40 -04001160 /* collect core counters only for 1st thread in core */
1161 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1162 return 0;
1163
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001164 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001165 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1166 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001167 }
1168
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001169 if (do_nhm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001170 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1171 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001172 } else if (do_knl_cstates) {
1173 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1174 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001175 }
1176
1177 if (do_snb_cstates)
1178 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1179 return -8;
1180
Len Brown889facb2012-11-08 00:48:57 -05001181 if (do_dts) {
1182 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1183 return -9;
1184 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1185 }
1186
1187
Len Brownc98d5d92012-06-04 00:56:40 -04001188 /* collect package counters only for 1st core in package */
1189 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1190 return 0;
1191
Len Brown0b2bb692015-03-26 00:50:30 -04001192 if (do_skl_residency) {
1193 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1194 return -10;
1195 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1196 return -11;
1197 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1198 return -12;
1199 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1200 return -13;
1201 }
Len Brownee7e38e2015-02-09 23:39:45 -05001202 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001203 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1204 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -05001205 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -04001206 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1207 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -05001208 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001209 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1210 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001211 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001212 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1213 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001214 if (do_c8_c9_c10) {
1215 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1216 return -13;
1217 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1218 return -13;
1219 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1220 return -13;
1221 }
Len Brown889facb2012-11-08 00:48:57 -05001222 if (do_rapl & RAPL_PKG) {
1223 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1224 return -13;
1225 p->energy_pkg = msr & 0xFFFFFFFF;
1226 }
1227 if (do_rapl & RAPL_CORES) {
1228 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1229 return -14;
1230 p->energy_cores = msr & 0xFFFFFFFF;
1231 }
1232 if (do_rapl & RAPL_DRAM) {
1233 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1234 return -15;
1235 p->energy_dram = msr & 0xFFFFFFFF;
1236 }
1237 if (do_rapl & RAPL_GFX) {
1238 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1239 return -16;
1240 p->energy_gfx = msr & 0xFFFFFFFF;
1241 }
1242 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1243 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1244 return -16;
1245 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1246 }
1247 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1248 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1249 return -16;
1250 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1251 }
1252 if (do_ptm) {
1253 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1254 return -17;
1255 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1256 }
Len Brownfdf676e2016-02-27 01:28:12 -05001257
1258 if (do_gfx_rc6_ms)
1259 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1260
Len Brown27d47352016-02-27 00:37:54 -05001261 if (do_gfx_mhz)
1262 p->gfx_mhz = gfx_cur_mhz;
1263
Len Brown103a8fe2010-10-22 23:53:03 -04001264 return 0;
1265}
1266
Len Brownee7e38e2015-02-09 23:39:45 -05001267/*
1268 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1269 * If you change the values, note they are used both in comparisons
1270 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1271 */
1272
1273#define PCLUKN 0 /* Unknown */
1274#define PCLRSV 1 /* Reserved */
1275#define PCL__0 2 /* PC0 */
1276#define PCL__1 3 /* PC1 */
1277#define PCL__2 4 /* PC2 */
1278#define PCL__3 5 /* PC3 */
1279#define PCL__4 6 /* PC4 */
1280#define PCL__6 7 /* PC6 */
1281#define PCL_6N 8 /* PC6 No Retention */
1282#define PCL_6R 9 /* PC6 Retention */
1283#define PCL__7 10 /* PC7 */
1284#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001285#define PCL__8 12 /* PC8 */
1286#define PCL__9 13 /* PC9 */
1287#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001288
1289int pkg_cstate_limit = PCLUKN;
1290char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001291 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001292
Len Browne9257f52015-04-01 21:02:57 -04001293int 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};
1294int 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};
1295int 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};
1296int 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};
1297int 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};
1298int 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 Brownee7e38e2015-02-09 23:39:45 -05001299
Len Browna2b7b742015-09-26 00:12:38 -04001300
1301static void
1302calculate_tsc_tweak()
1303{
Len Browna2b7b742015-09-26 00:12:38 -04001304 tsc_tweak = base_hz / tsc_hz;
1305}
1306
Len Brownfcd17212015-03-23 20:29:09 -04001307static void
1308dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001309{
1310 unsigned long long msr;
1311 unsigned int ratio;
1312
Len Brownec0adc52015-11-12 02:42:31 -05001313 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001314
Len Brownb7d8c142016-02-13 23:36:17 -05001315 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001316
Len Brown103a8fe2010-10-22 23:53:03 -04001317 ratio = (msr >> 40) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001318 fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001319 ratio, bclk, ratio * bclk);
1320
1321 ratio = (msr >> 8) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001322 fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001323 ratio, bclk, ratio * bclk);
1324
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001325 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001326 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001327 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001328
Len Brownfcd17212015-03-23 20:29:09 -04001329 return;
1330}
1331
1332static void
1333dump_hsw_turbo_ratio_limits(void)
1334{
1335 unsigned long long msr;
1336 unsigned int ratio;
1337
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001338 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001339
Len Brownb7d8c142016-02-13 23:36:17 -05001340 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001341
1342 ratio = (msr >> 8) & 0xFF;
1343 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001344 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001345 ratio, bclk, ratio * bclk);
1346
1347 ratio = (msr >> 0) & 0xFF;
1348 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001349 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001350 ratio, bclk, ratio * bclk);
1351 return;
1352}
1353
1354static void
1355dump_ivt_turbo_ratio_limits(void)
1356{
1357 unsigned long long msr;
1358 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001359
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001360 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001361
Len Brownb7d8c142016-02-13 23:36:17 -05001362 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001363
1364 ratio = (msr >> 56) & 0xFF;
1365 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001366 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001367 ratio, bclk, ratio * bclk);
1368
1369 ratio = (msr >> 48) & 0xFF;
1370 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001371 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001372 ratio, bclk, ratio * bclk);
1373
1374 ratio = (msr >> 40) & 0xFF;
1375 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001376 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001377 ratio, bclk, ratio * bclk);
1378
1379 ratio = (msr >> 32) & 0xFF;
1380 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001381 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001382 ratio, bclk, ratio * bclk);
1383
1384 ratio = (msr >> 24) & 0xFF;
1385 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001386 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001387 ratio, bclk, ratio * bclk);
1388
1389 ratio = (msr >> 16) & 0xFF;
1390 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001391 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001392 ratio, bclk, ratio * bclk);
1393
1394 ratio = (msr >> 8) & 0xFF;
1395 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001396 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001397 ratio, bclk, ratio * bclk);
1398
1399 ratio = (msr >> 0) & 0xFF;
1400 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001401 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001402 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001403 return;
1404}
Len Brown6574a5d2012-09-21 00:01:31 -04001405
Len Brownfcd17212015-03-23 20:29:09 -04001406static void
1407dump_nhm_turbo_ratio_limits(void)
1408{
1409 unsigned long long msr;
1410 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001411
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001412 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001413
Len Brownb7d8c142016-02-13 23:36:17 -05001414 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001415
1416 ratio = (msr >> 56) & 0xFF;
1417 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001418 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001419 ratio, bclk, ratio * bclk);
1420
1421 ratio = (msr >> 48) & 0xFF;
1422 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001423 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001424 ratio, bclk, ratio * bclk);
1425
1426 ratio = (msr >> 40) & 0xFF;
1427 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001428 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001429 ratio, bclk, ratio * bclk);
1430
1431 ratio = (msr >> 32) & 0xFF;
1432 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001433 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001434 ratio, bclk, ratio * bclk);
1435
Len Brown103a8fe2010-10-22 23:53:03 -04001436 ratio = (msr >> 24) & 0xFF;
1437 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001438 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001439 ratio, bclk, ratio * bclk);
1440
1441 ratio = (msr >> 16) & 0xFF;
1442 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001443 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001444 ratio, bclk, ratio * bclk);
1445
1446 ratio = (msr >> 8) & 0xFF;
1447 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001448 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001449 ratio, bclk, ratio * bclk);
1450
1451 ratio = (msr >> 0) & 0xFF;
1452 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001453 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001454 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001455 return;
1456}
Len Brown3a9a9412014-08-15 02:39:52 -04001457
Len Brownfcd17212015-03-23 20:29:09 -04001458static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001459dump_knl_turbo_ratio_limits(void)
1460{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001461 const unsigned int buckets_no = 7;
1462
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001463 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001464 int delta_cores, delta_ratio;
1465 int i, b_nr;
1466 unsigned int cores[buckets_no];
1467 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001468
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001469 get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001470
Len Brownb7d8c142016-02-13 23:36:17 -05001471 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001472 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001473
1474 /**
1475 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001476 * [0] -- Reserved
1477 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001478 * [15:8] -- Base value of freq ratio of bucket 1.
1479 * [20:16] -- +ve delta of number of active cores of bucket 2.
1480 * i.e. active cores of bucket 2 =
1481 * active cores of bucket 1 + delta
1482 * [23:21] -- Negative delta of freq ratio of bucket 2.
1483 * i.e. freq ratio of bucket 2 =
1484 * freq ratio of bucket 1 - delta
1485 * [28:24]-- +ve delta of number of active cores of bucket 3.
1486 * [31:29]-- -ve delta of freq ratio of bucket 3.
1487 * [36:32]-- +ve delta of number of active cores of bucket 4.
1488 * [39:37]-- -ve delta of freq ratio of bucket 4.
1489 * [44:40]-- +ve delta of number of active cores of bucket 5.
1490 * [47:45]-- -ve delta of freq ratio of bucket 5.
1491 * [52:48]-- +ve delta of number of active cores of bucket 6.
1492 * [55:53]-- -ve delta of freq ratio of bucket 6.
1493 * [60:56]-- +ve delta of number of active cores of bucket 7.
1494 * [63:61]-- -ve delta of freq ratio of bucket 7.
1495 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001496
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001497 b_nr = 0;
1498 cores[b_nr] = (msr & 0xFF) >> 1;
1499 ratio[b_nr] = (msr >> 8) & 0xFF;
1500
1501 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001502 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001503 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001504
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001505 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1506 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1507 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001508 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001509
1510 for (i = buckets_no - 1; i >= 0; i--)
1511 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001512 fprintf(outf,
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001513 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001514 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001515}
1516
1517static void
Len Brownfcd17212015-03-23 20:29:09 -04001518dump_nhm_cst_cfg(void)
1519{
1520 unsigned long long msr;
1521
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001522 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001523
1524#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1525#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1526
Len Brownb7d8c142016-02-13 23:36:17 -05001527 fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001528
Len Brownb7d8c142016-02-13 23:36:17 -05001529 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001530 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1531 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1532 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1533 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1534 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001535 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001536 pkg_cstate_limit_strings[pkg_cstate_limit]);
1537 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001538}
1539
Len Brown6fb31432015-06-17 16:23:45 -04001540static void
1541dump_config_tdp(void)
1542{
1543 unsigned long long msr;
1544
1545 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001546 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001547 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001548
1549 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001550 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001551 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001552 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1553 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1554 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1555 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001556 }
Len Brownb7d8c142016-02-13 23:36:17 -05001557 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001558
1559 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001560 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001561 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001562 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1563 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1564 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1565 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001566 }
Len Brownb7d8c142016-02-13 23:36:17 -05001567 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001568
1569 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001570 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001571 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001572 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1573 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1574 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001575
Len Brown6fb31432015-06-17 16:23:45 -04001576 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001577 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001578 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001579 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1580 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001581}
Len Brown36229892016-02-26 20:51:02 -05001582void free_fd_percpu(void)
1583{
1584 int i;
1585
1586 for (i = 0; i < topo.max_cpu_num; ++i) {
1587 if (fd_percpu[i] != 0)
1588 close(fd_percpu[i]);
1589 }
1590
1591 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04001592}
1593
Len Brownc98d5d92012-06-04 00:56:40 -04001594void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001595{
Len Brownc98d5d92012-06-04 00:56:40 -04001596 CPU_FREE(cpu_present_set);
1597 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05001598 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001599
Len Brownc98d5d92012-06-04 00:56:40 -04001600 CPU_FREE(cpu_affinity_set);
1601 cpu_affinity_set = NULL;
1602 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001603
Len Brownc98d5d92012-06-04 00:56:40 -04001604 free(thread_even);
1605 free(core_even);
1606 free(package_even);
1607
1608 thread_even = NULL;
1609 core_even = NULL;
1610 package_even = NULL;
1611
1612 free(thread_odd);
1613 free(core_odd);
1614 free(package_odd);
1615
1616 thread_odd = NULL;
1617 core_odd = NULL;
1618 package_odd = NULL;
1619
1620 free(output_buffer);
1621 output_buffer = NULL;
1622 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05001623
1624 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05001625
1626 free(irq_column_2_cpu);
1627 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001628}
1629
Len Brownc98d5d92012-06-04 00:56:40 -04001630/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001631 * Open a file, and exit on failure
1632 */
1633FILE *fopen_or_die(const char *path, const char *mode)
1634{
Len Brownb7d8c142016-02-13 23:36:17 -05001635 FILE *filep = fopen(path, mode);
Josh Triplettb2c95d92013-08-20 17:20:18 -07001636 if (!filep)
1637 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001638 return filep;
1639}
1640
1641/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001642 * Parse a file containing a single int.
1643 */
1644int parse_int_file(const char *fmt, ...)
1645{
1646 va_list args;
1647 char path[PATH_MAX];
1648 FILE *filep;
1649 int value;
1650
1651 va_start(args, fmt);
1652 vsnprintf(path, sizeof(path), fmt, args);
1653 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001654 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001655 if (fscanf(filep, "%d", &value) != 1)
1656 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001657 fclose(filep);
1658 return value;
1659}
1660
1661/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001662 * get_cpu_position_in_core(cpu)
1663 * return the position of the CPU among its HT siblings in the core
1664 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04001665 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001666int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001667{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001668 char path[64];
1669 FILE *filep;
1670 int this_cpu;
1671 char character;
1672 int i;
1673
1674 sprintf(path,
1675 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1676 cpu);
1677 filep = fopen(path, "r");
1678 if (filep == NULL) {
1679 perror(path);
1680 exit(1);
1681 }
1682
1683 for (i = 0; i < topo.num_threads_per_core; i++) {
1684 fscanf(filep, "%d", &this_cpu);
1685 if (this_cpu == cpu) {
1686 fclose(filep);
1687 return i;
1688 }
1689
1690 /* Account for no separator after last thread*/
1691 if (i != (topo.num_threads_per_core - 1))
1692 fscanf(filep, "%c", &character);
1693 }
1694
1695 fclose(filep);
1696 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04001697}
1698
Len Brownc98d5d92012-06-04 00:56:40 -04001699/*
1700 * cpu_is_first_core_in_package(cpu)
1701 * return 1 if given CPU is 1st core in package
1702 */
1703int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001704{
Josh Triplett95aebc42013-08-20 17:20:16 -07001705 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001706}
1707
1708int get_physical_package_id(int cpu)
1709{
Josh Triplett95aebc42013-08-20 17:20:16 -07001710 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001711}
1712
1713int get_core_id(int cpu)
1714{
Josh Triplett95aebc42013-08-20 17:20:16 -07001715 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001716}
1717
Len Brownc98d5d92012-06-04 00:56:40 -04001718int get_num_ht_siblings(int cpu)
1719{
1720 char path[80];
1721 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001722 int sib1;
1723 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001724 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001725 char str[100];
1726 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04001727
1728 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001729 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001730
Len Brownc98d5d92012-06-04 00:56:40 -04001731 /*
1732 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001733 * A ',' separated or '-' separated set of numbers
1734 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04001735 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001736 fscanf(filep, "%d%c\n", &sib1, &character);
1737 fseek(filep, 0, SEEK_SET);
1738 fgets(str, 100, filep);
1739 ch = strchr(str, character);
1740 while (ch != NULL) {
1741 matches++;
1742 ch = strchr(ch+1, character);
1743 }
Len Brownc98d5d92012-06-04 00:56:40 -04001744
1745 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001746 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04001747}
1748
Len Brown103a8fe2010-10-22 23:53:03 -04001749/*
Len Brownc98d5d92012-06-04 00:56:40 -04001750 * run func(thread, core, package) in topology order
1751 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001752 */
1753
Len Brownc98d5d92012-06-04 00:56:40 -04001754int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1755 struct pkg_data *, struct thread_data *, struct core_data *,
1756 struct pkg_data *), struct thread_data *thread_base,
1757 struct core_data *core_base, struct pkg_data *pkg_base,
1758 struct thread_data *thread_base2, struct core_data *core_base2,
1759 struct pkg_data *pkg_base2)
1760{
1761 int retval, pkg_no, core_no, thread_no;
1762
1763 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1764 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1765 for (thread_no = 0; thread_no <
1766 topo.num_threads_per_core; ++thread_no) {
1767 struct thread_data *t, *t2;
1768 struct core_data *c, *c2;
1769 struct pkg_data *p, *p2;
1770
1771 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1772
1773 if (cpu_is_not_present(t->cpu_id))
1774 continue;
1775
1776 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1777
1778 c = GET_CORE(core_base, core_no, pkg_no);
1779 c2 = GET_CORE(core_base2, core_no, pkg_no);
1780
1781 p = GET_PKG(pkg_base, pkg_no);
1782 p2 = GET_PKG(pkg_base2, pkg_no);
1783
1784 retval = func(t, c, p, t2, c2, p2);
1785 if (retval)
1786 return retval;
1787 }
1788 }
1789 }
1790 return 0;
1791}
1792
1793/*
1794 * run func(cpu) on every cpu in /proc/stat
1795 * return max_cpu number
1796 */
1797int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001798{
1799 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001800 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001801 int retval;
1802
Josh Triplett57a42a32013-08-20 17:20:17 -07001803 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001804
1805 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001806 if (retval != 0)
1807 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001808
Len Brownc98d5d92012-06-04 00:56:40 -04001809 while (1) {
1810 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 -04001811 if (retval != 1)
1812 break;
1813
Len Brownc98d5d92012-06-04 00:56:40 -04001814 retval = func(cpu_num);
1815 if (retval) {
1816 fclose(fp);
1817 return(retval);
1818 }
Len Brown103a8fe2010-10-22 23:53:03 -04001819 }
1820 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001821 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001822}
1823
1824void re_initialize(void)
1825{
Len Brownc98d5d92012-06-04 00:56:40 -04001826 free_all_buffers();
1827 setup_all_buffers();
1828 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001829}
1830
Len Brownc98d5d92012-06-04 00:56:40 -04001831
Len Brown103a8fe2010-10-22 23:53:03 -04001832/*
Len Brownc98d5d92012-06-04 00:56:40 -04001833 * count_cpus()
1834 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001835 */
Len Brownc98d5d92012-06-04 00:56:40 -04001836int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001837{
Len Brownc98d5d92012-06-04 00:56:40 -04001838 if (topo.max_cpu_num < cpu)
1839 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001840
Len Brownc98d5d92012-06-04 00:56:40 -04001841 topo.num_cpus += 1;
1842 return 0;
1843}
1844int mark_cpu_present(int cpu)
1845{
1846 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001847 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001848}
1849
Len Brown562a2d32016-02-26 23:48:05 -05001850/*
1851 * snapshot_proc_interrupts()
1852 *
1853 * read and record summary of /proc/interrupts
1854 *
1855 * return 1 if config change requires a restart, else return 0
1856 */
1857int snapshot_proc_interrupts(void)
1858{
1859 static FILE *fp;
1860 int column, retval;
1861
1862 if (fp == NULL)
1863 fp = fopen_or_die("/proc/interrupts", "r");
1864 else
1865 rewind(fp);
1866
1867 /* read 1st line of /proc/interrupts to get cpu* name for each column */
1868 for (column = 0; column < topo.num_cpus; ++column) {
1869 int cpu_number;
1870
1871 retval = fscanf(fp, " CPU%d", &cpu_number);
1872 if (retval != 1)
1873 break;
1874
1875 if (cpu_number > topo.max_cpu_num) {
1876 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
1877 return 1;
1878 }
1879
1880 irq_column_2_cpu[column] = cpu_number;
1881 irqs_per_cpu[cpu_number] = 0;
1882 }
1883
1884 /* read /proc/interrupt count lines and sum up irqs per cpu */
1885 while (1) {
1886 int column;
1887 char buf[64];
1888
1889 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
1890 if (retval != 1)
1891 break;
1892
1893 /* read the count per cpu */
1894 for (column = 0; column < topo.num_cpus; ++column) {
1895
1896 int cpu_number, irq_count;
1897
1898 retval = fscanf(fp, " %d", &irq_count);
1899 if (retval != 1)
1900 break;
1901
1902 cpu_number = irq_column_2_cpu[column];
1903 irqs_per_cpu[cpu_number] += irq_count;
1904
1905 }
1906
1907 while (getc(fp) != '\n')
1908 ; /* flush interrupt description */
1909
1910 }
1911 return 0;
1912}
Len Brown27d47352016-02-27 00:37:54 -05001913/*
Len Brownfdf676e2016-02-27 01:28:12 -05001914 * snapshot_gfx_rc6_ms()
1915 *
1916 * record snapshot of
1917 * /sys/class/drm/card0/power/rc6_residency_ms
1918 *
1919 * return 1 if config change requires a restart, else return 0
1920 */
1921int snapshot_gfx_rc6_ms(void)
1922{
1923 FILE *fp;
1924 int retval;
1925
1926 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
1927
1928 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
1929 if (retval != 1)
1930 err(1, "GFX rc6");
1931
1932 fclose(fp);
1933
1934 return 0;
1935}
1936/*
Len Brown27d47352016-02-27 00:37:54 -05001937 * snapshot_gfx_mhz()
1938 *
1939 * record snapshot of
1940 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
1941 *
1942 * return 1 if config change requires a restart, else return 0
1943 */
1944int snapshot_gfx_mhz(void)
1945{
1946 static FILE *fp;
1947 int retval;
1948
1949 if (fp == NULL)
1950 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
1951 else
1952 rewind(fp);
1953
1954 retval = fscanf(fp, "%d", &gfx_cur_mhz);
1955 if (retval != 1)
1956 err(1, "GFX MHz");
1957
1958 return 0;
1959}
Len Brown562a2d32016-02-26 23:48:05 -05001960
1961/*
1962 * snapshot /proc and /sys files
1963 *
1964 * return 1 if configuration restart needed, else return 0
1965 */
1966int snapshot_proc_sysfs_files(void)
1967{
1968 if (snapshot_proc_interrupts())
1969 return 1;
1970
Len Brownfdf676e2016-02-27 01:28:12 -05001971 if (do_gfx_rc6_ms)
1972 snapshot_gfx_rc6_ms();
1973
Len Brown27d47352016-02-27 00:37:54 -05001974 if (do_gfx_mhz)
1975 snapshot_gfx_mhz();
1976
Len Brown562a2d32016-02-26 23:48:05 -05001977 return 0;
1978}
1979
Len Brown103a8fe2010-10-22 23:53:03 -04001980void turbostat_loop()
1981{
Len Brownc98d5d92012-06-04 00:56:40 -04001982 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001983 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001984
Len Brown103a8fe2010-10-22 23:53:03 -04001985restart:
Len Browne52966c2012-11-08 22:38:05 -05001986 restarted++;
1987
Len Brown562a2d32016-02-26 23:48:05 -05001988 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04001989 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001990 if (retval < -1) {
1991 exit(retval);
1992 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001993 if (restarted > 1) {
1994 exit(retval);
1995 }
Len Brownc98d5d92012-06-04 00:56:40 -04001996 re_initialize();
1997 goto restart;
1998 }
Len Browne52966c2012-11-08 22:38:05 -05001999 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002000 gettimeofday(&tv_even, (struct timezone *)NULL);
2001
2002 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002003 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002004 re_initialize();
2005 goto restart;
2006 }
Len Brown2a0609c2016-02-12 22:44:48 -05002007 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002008 if (snapshot_proc_sysfs_files())
2009 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002010 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002011 if (retval < -1) {
2012 exit(retval);
2013 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002014 re_initialize();
2015 goto restart;
2016 }
Len Brown103a8fe2010-10-22 23:53:03 -04002017 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002018 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002019 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2020 compute_average(EVEN_COUNTERS);
2021 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002022 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002023 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002024 if (snapshot_proc_sysfs_files())
2025 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002026 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002027 if (retval < -1) {
2028 exit(retval);
2029 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002030 re_initialize();
2031 goto restart;
2032 }
Len Brown103a8fe2010-10-22 23:53:03 -04002033 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002034 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002035 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
2036 compute_average(ODD_COUNTERS);
2037 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002038 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002039 }
2040}
2041
2042void check_dev_msr()
2043{
2044 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002045 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002046
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002047 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2048 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002049 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2050 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002051}
2052
Len Brown98481e72014-08-15 00:36:50 -04002053void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002054{
Len Brown98481e72014-08-15 00:36:50 -04002055 struct __user_cap_header_struct cap_header_data;
2056 cap_user_header_t cap_header = &cap_header_data;
2057 struct __user_cap_data_struct cap_data_data;
2058 cap_user_data_t cap_data = &cap_data_data;
2059 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2060 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002061 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002062
2063 /* check for CAP_SYS_RAWIO */
2064 cap_header->pid = getpid();
2065 cap_header->version = _LINUX_CAPABILITY_VERSION;
2066 if (capget(cap_header, cap_data) < 0)
2067 err(-6, "capget(2) failed");
2068
2069 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2070 do_exit++;
2071 warnx("capget(CAP_SYS_RAWIO) failed,"
2072 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2073 }
2074
2075 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002076 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2077 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002078 do_exit++;
2079 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2080 }
2081
2082 /* if all else fails, thell them to be root */
2083 if (do_exit)
2084 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002085 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002086
2087 if (do_exit)
2088 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002089}
2090
Len Brownd7899442015-01-23 00:12:33 -05002091/*
2092 * NHM adds support for additional MSRs:
2093 *
2094 * MSR_SMI_COUNT 0x00000034
2095 *
Len Brownec0adc52015-11-12 02:42:31 -05002096 * MSR_PLATFORM_INFO 0x000000ce
Len Brownd7899442015-01-23 00:12:33 -05002097 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
2098 *
2099 * MSR_PKG_C3_RESIDENCY 0x000003f8
2100 * MSR_PKG_C6_RESIDENCY 0x000003f9
2101 * MSR_CORE_C3_RESIDENCY 0x000003fc
2102 * MSR_CORE_C6_RESIDENCY 0x000003fd
2103 *
Len Brownee7e38e2015-02-09 23:39:45 -05002104 * Side effect:
2105 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05002106 */
Len Brownee7e38e2015-02-09 23:39:45 -05002107int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002108{
Len Brownee7e38e2015-02-09 23:39:45 -05002109 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002110 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002111 int *pkg_cstate_limits;
2112
Len Brown103a8fe2010-10-22 23:53:03 -04002113 if (!genuine_intel)
2114 return 0;
2115
2116 if (family != 6)
2117 return 0;
2118
Len Brown21ed5572015-10-19 22:37:40 -04002119 bclk = discover_bclk(family, model);
2120
Len Brown103a8fe2010-10-22 23:53:03 -04002121 switch (model) {
2122 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2123 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2124 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
2125 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
2126 case 0x2C: /* Westmere EP - Gulftown */
Len Brownee7e38e2015-02-09 23:39:45 -05002127 case 0x2E: /* Nehalem-EX Xeon - Beckton */
2128 case 0x2F: /* Westmere-EX Xeon - Eagleton */
2129 pkg_cstate_limits = nhm_pkg_cstate_limits;
2130 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002131 case 0x2A: /* SNB */
2132 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05002133 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002134 case 0x3E: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002135 pkg_cstate_limits = snb_pkg_cstate_limits;
2136 break;
Len Brown70b43402013-01-08 01:26:07 -05002137 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05002138 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05002139 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002140 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002141 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002142 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002143 case 0x4F: /* BDX */
2144 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04002145 case 0x4E: /* SKL */
2146 case 0x5E: /* SKL */
Len Brownee7e38e2015-02-09 23:39:45 -05002147 pkg_cstate_limits = hsw_pkg_cstate_limits;
2148 break;
2149 case 0x37: /* BYT */
2150 case 0x4D: /* AVN */
2151 pkg_cstate_limits = slv_pkg_cstate_limits;
2152 break;
2153 case 0x4C: /* AMT */
2154 pkg_cstate_limits = amt_pkg_cstate_limits;
2155 break;
2156 case 0x57: /* PHI */
2157 pkg_cstate_limits = phi_pkg_cstate_limits;
2158 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002159 default:
2160 return 0;
2161 }
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002162 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002163 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002164
Len Brownec0adc52015-11-12 02:42:31 -05002165 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002166 base_ratio = (msr >> 8) & 0xFF;
2167
2168 base_hz = base_ratio * bclk * 1000000;
2169 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002170 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002171}
Len Brownd7899442015-01-23 00:12:33 -05002172int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2173{
Len Brownd7899442015-01-23 00:12:33 -05002174 switch (model) {
2175 /* Nehalem compatible, but do not include turbo-ratio limit support */
2176 case 0x2E: /* Nehalem-EX Xeon - Beckton */
2177 case 0x2F: /* Westmere-EX Xeon - Eagleton */
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002178 case 0x57: /* PHI - Knights Landing (different MSR definition) */
Len Brownd7899442015-01-23 00:12:33 -05002179 return 0;
2180 default:
2181 return 1;
2182 }
2183}
Len Brown6574a5d2012-09-21 00:01:31 -04002184int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2185{
2186 if (!genuine_intel)
2187 return 0;
2188
2189 if (family != 6)
2190 return 0;
2191
2192 switch (model) {
2193 case 0x3E: /* IVB Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002194 case 0x3F: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002195 return 1;
2196 default:
2197 return 0;
2198 }
2199}
Len Brownfcd17212015-03-23 20:29:09 -04002200int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2201{
2202 if (!genuine_intel)
2203 return 0;
2204
2205 if (family != 6)
2206 return 0;
2207
2208 switch (model) {
2209 case 0x3F: /* HSW Xeon */
2210 return 1;
2211 default:
2212 return 0;
2213 }
2214}
2215
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002216int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2217{
2218 if (!genuine_intel)
2219 return 0;
2220
2221 if (family != 6)
2222 return 0;
2223
2224 switch (model) {
2225 case 0x57: /* Knights Landing */
2226 return 1;
2227 default:
2228 return 0;
2229 }
2230}
Len Brown6fb31432015-06-17 16:23:45 -04002231int has_config_tdp(unsigned int family, unsigned int model)
2232{
2233 if (!genuine_intel)
2234 return 0;
2235
2236 if (family != 6)
2237 return 0;
2238
2239 switch (model) {
2240 case 0x3A: /* IVB */
Len Brown6fb31432015-06-17 16:23:45 -04002241 case 0x3C: /* HSW */
2242 case 0x3F: /* HSX */
2243 case 0x45: /* HSW */
2244 case 0x46: /* HSW */
2245 case 0x3D: /* BDW */
2246 case 0x47: /* BDW */
2247 case 0x4F: /* BDX */
2248 case 0x56: /* BDX-DE */
2249 case 0x4E: /* SKL */
2250 case 0x5E: /* SKL */
2251
2252 case 0x57: /* Knights Landing */
2253 return 1;
2254 default:
2255 return 0;
2256 }
2257}
2258
Len Brownfcd17212015-03-23 20:29:09 -04002259static void
Colin Ian King1b693172016-03-02 13:50:25 +00002260dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002261{
2262 if (!do_nhm_platform_info)
2263 return;
2264
2265 dump_nhm_platform_info();
2266
2267 if (has_hsw_turbo_ratio_limit(family, model))
2268 dump_hsw_turbo_ratio_limits();
2269
2270 if (has_ivt_turbo_ratio_limit(family, model))
2271 dump_ivt_turbo_ratio_limits();
2272
2273 if (has_nhm_turbo_ratio_limit(family, model))
2274 dump_nhm_turbo_ratio_limits();
2275
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002276 if (has_knl_turbo_ratio_limit(family, model))
2277 dump_knl_turbo_ratio_limits();
2278
Len Brown6fb31432015-06-17 16:23:45 -04002279 if (has_config_tdp(family, model))
2280 dump_config_tdp();
2281
Len Brownfcd17212015-03-23 20:29:09 -04002282 dump_nhm_cst_cfg();
2283}
2284
Len Brown6574a5d2012-09-21 00:01:31 -04002285
Len Brown889facb2012-11-08 00:48:57 -05002286/*
2287 * print_epb()
2288 * Decode the ENERGY_PERF_BIAS MSR
2289 */
2290int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2291{
2292 unsigned long long msr;
2293 char *epb_string;
2294 int cpu;
2295
2296 if (!has_epb)
2297 return 0;
2298
2299 cpu = t->cpu_id;
2300
2301 /* EPB is per-package */
2302 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2303 return 0;
2304
2305 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002306 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002307 return -1;
2308 }
2309
2310 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2311 return 0;
2312
Len Browne9be7dd2015-05-26 12:19:37 -04002313 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002314 case ENERGY_PERF_BIAS_PERFORMANCE:
2315 epb_string = "performance";
2316 break;
2317 case ENERGY_PERF_BIAS_NORMAL:
2318 epb_string = "balanced";
2319 break;
2320 case ENERGY_PERF_BIAS_POWERSAVE:
2321 epb_string = "powersave";
2322 break;
2323 default:
2324 epb_string = "custom";
2325 break;
2326 }
Len Brownb7d8c142016-02-13 23:36:17 -05002327 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002328
2329 return 0;
2330}
Len Brown7f5c2582015-12-01 01:36:39 -05002331/*
2332 * print_hwp()
2333 * Decode the MSR_HWP_CAPABILITIES
2334 */
2335int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2336{
2337 unsigned long long msr;
2338 int cpu;
2339
2340 if (!has_hwp)
2341 return 0;
2342
2343 cpu = t->cpu_id;
2344
2345 /* MSR_HWP_CAPABILITIES is per-package */
2346 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2347 return 0;
2348
2349 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002350 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002351 return -1;
2352 }
2353
2354 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2355 return 0;
2356
Len Brownb7d8c142016-02-13 23:36:17 -05002357 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002358 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2359
2360 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2361 if ((msr & (1 << 0)) == 0)
2362 return 0;
2363
2364 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2365 return 0;
2366
Len Brownb7d8c142016-02-13 23:36:17 -05002367 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002368 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2369 cpu, msr,
2370 (unsigned int)HWP_HIGHEST_PERF(msr),
2371 (unsigned int)HWP_GUARANTEED_PERF(msr),
2372 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2373 (unsigned int)HWP_LOWEST_PERF(msr));
2374
2375 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2376 return 0;
2377
Len Brownb7d8c142016-02-13 23:36:17 -05002378 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002379 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2380 cpu, msr,
2381 (unsigned int)(((msr) >> 0) & 0xff),
2382 (unsigned int)(((msr) >> 8) & 0xff),
2383 (unsigned int)(((msr) >> 16) & 0xff),
2384 (unsigned int)(((msr) >> 24) & 0xff),
2385 (unsigned int)(((msr) >> 32) & 0xff3),
2386 (unsigned int)(((msr) >> 42) & 0x1));
2387
2388 if (has_hwp_pkg) {
2389 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2390 return 0;
2391
Len Brownb7d8c142016-02-13 23:36:17 -05002392 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002393 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2394 cpu, msr,
2395 (unsigned int)(((msr) >> 0) & 0xff),
2396 (unsigned int)(((msr) >> 8) & 0xff),
2397 (unsigned int)(((msr) >> 16) & 0xff),
2398 (unsigned int)(((msr) >> 24) & 0xff),
2399 (unsigned int)(((msr) >> 32) & 0xff3));
2400 }
2401 if (has_hwp_notify) {
2402 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2403 return 0;
2404
Len Brownb7d8c142016-02-13 23:36:17 -05002405 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002406 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2407 cpu, msr,
2408 ((msr) & 0x1) ? "EN" : "Dis",
2409 ((msr) & 0x2) ? "EN" : "Dis");
2410 }
2411 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2412 return 0;
2413
Len Brownb7d8c142016-02-13 23:36:17 -05002414 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002415 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2416 cpu, msr,
2417 ((msr) & 0x1) ? "" : "No-",
2418 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05002419
2420 return 0;
2421}
2422
Len Brown3a9a9412014-08-15 02:39:52 -04002423/*
2424 * print_perf_limit()
2425 */
2426int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2427{
2428 unsigned long long msr;
2429 int cpu;
2430
2431 cpu = t->cpu_id;
2432
2433 /* per-package */
2434 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2435 return 0;
2436
2437 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002438 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002439 return -1;
2440 }
2441
2442 if (do_core_perf_limit_reasons) {
2443 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002444 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2445 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002446 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002447 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002448 (msr & 1 << 13) ? "Transitions, " : "",
2449 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2450 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2451 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2452 (msr & 1 << 9) ? "CorePwr, " : "",
2453 (msr & 1 << 8) ? "Amps, " : "",
2454 (msr & 1 << 6) ? "VR-Therm, " : "",
2455 (msr & 1 << 5) ? "Auto-HWP, " : "",
2456 (msr & 1 << 4) ? "Graphics, " : "",
2457 (msr & 1 << 2) ? "bit2, " : "",
2458 (msr & 1 << 1) ? "ThermStatus, " : "",
2459 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002460 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 -04002461 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002462 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002463 (msr & 1 << 29) ? "Transitions, " : "",
2464 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2465 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2466 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2467 (msr & 1 << 25) ? "CorePwr, " : "",
2468 (msr & 1 << 24) ? "Amps, " : "",
2469 (msr & 1 << 22) ? "VR-Therm, " : "",
2470 (msr & 1 << 21) ? "Auto-HWP, " : "",
2471 (msr & 1 << 20) ? "Graphics, " : "",
2472 (msr & 1 << 18) ? "bit18, " : "",
2473 (msr & 1 << 17) ? "ThermStatus, " : "",
2474 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002475
2476 }
2477 if (do_gfx_perf_limit_reasons) {
2478 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002479 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2480 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002481 (msr & 1 << 0) ? "PROCHOT, " : "",
2482 (msr & 1 << 1) ? "ThermStatus, " : "",
2483 (msr & 1 << 4) ? "Graphics, " : "",
2484 (msr & 1 << 6) ? "VR-Therm, " : "",
2485 (msr & 1 << 8) ? "Amps, " : "",
2486 (msr & 1 << 9) ? "GFXPwr, " : "",
2487 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2488 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002489 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002490 (msr & 1 << 16) ? "PROCHOT, " : "",
2491 (msr & 1 << 17) ? "ThermStatus, " : "",
2492 (msr & 1 << 20) ? "Graphics, " : "",
2493 (msr & 1 << 22) ? "VR-Therm, " : "",
2494 (msr & 1 << 24) ? "Amps, " : "",
2495 (msr & 1 << 25) ? "GFXPwr, " : "",
2496 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2497 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2498 }
2499 if (do_ring_perf_limit_reasons) {
2500 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002501 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2502 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002503 (msr & 1 << 0) ? "PROCHOT, " : "",
2504 (msr & 1 << 1) ? "ThermStatus, " : "",
2505 (msr & 1 << 6) ? "VR-Therm, " : "",
2506 (msr & 1 << 8) ? "Amps, " : "",
2507 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2508 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002509 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002510 (msr & 1 << 16) ? "PROCHOT, " : "",
2511 (msr & 1 << 17) ? "ThermStatus, " : "",
2512 (msr & 1 << 22) ? "VR-Therm, " : "",
2513 (msr & 1 << 24) ? "Amps, " : "",
2514 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2515 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2516 }
2517 return 0;
2518}
2519
Len Brown889facb2012-11-08 00:48:57 -05002520#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2521#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2522
Colin Ian King1b693172016-03-02 13:50:25 +00002523double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05002524{
2525 unsigned long long msr;
2526
2527 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002528 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002529 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2530
2531 switch (model) {
2532 case 0x37:
2533 case 0x4D:
2534 return 30.0;
2535 default:
2536 return 135.0;
2537 }
2538}
2539
Andrey Semin40ee8e32014-12-05 00:07:00 -05002540/*
2541 * rapl_dram_energy_units_probe()
2542 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2543 */
2544static double
2545rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2546{
2547 /* only called for genuine_intel, family 6 */
2548
2549 switch (model) {
2550 case 0x3F: /* HSX */
2551 case 0x4F: /* BDX */
2552 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002553 case 0x57: /* KNL */
Andrey Semin40ee8e32014-12-05 00:07:00 -05002554 return (rapl_dram_energy_units = 15.3 / 1000000);
2555 default:
2556 return (rapl_energy_units);
2557 }
2558}
2559
Len Brown144b44b2013-11-09 00:30:16 -05002560
Len Brown889facb2012-11-08 00:48:57 -05002561/*
2562 * rapl_probe()
2563 *
Len Brown144b44b2013-11-09 00:30:16 -05002564 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002565 */
2566void rapl_probe(unsigned int family, unsigned int model)
2567{
2568 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002569 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002570 double tdp;
2571
2572 if (!genuine_intel)
2573 return;
2574
2575 if (family != 6)
2576 return;
2577
2578 switch (model) {
2579 case 0x2A:
2580 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05002581 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05002582 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002583 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002584 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002585 case 0x47: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05002586 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05002587 break;
Len Brown0b2bb692015-03-26 00:50:30 -04002588 case 0x4E: /* SKL */
2589 case 0x5E: /* SKL */
2590 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2591 break;
Len Browne6f9bb32013-12-03 02:19:19 -05002592 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05002593 case 0x4F: /* BDX */
2594 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002595 case 0x57: /* KNL */
Len Brown0b2bb692015-03-26 00:50:30 -04002596 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 -05002597 break;
Len Brown889facb2012-11-08 00:48:57 -05002598 case 0x2D:
2599 case 0x3E:
Len Brown0b2bb692015-03-26 00:50:30 -04002600 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 -05002601 break;
2602 case 0x37: /* BYT */
2603 case 0x4D: /* AVN */
2604 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05002605 break;
2606 default:
2607 return;
2608 }
2609
2610 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002611 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002612 return;
2613
2614 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05002615 if (model == 0x37)
2616 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2617 else
2618 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05002619
Andrey Semin40ee8e32014-12-05 00:07:00 -05002620 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2621
Len Brown144b44b2013-11-09 00:30:16 -05002622 time_unit = msr >> 16 & 0xF;
2623 if (time_unit == 0)
2624 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05002625
Len Brown144b44b2013-11-09 00:30:16 -05002626 rapl_time_units = 1.0 / (1 << (time_unit));
2627
2628 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05002629
2630 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05002631 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002632 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05002633
2634 return;
2635}
2636
Colin Ian King1b693172016-03-02 13:50:25 +00002637void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04002638{
2639 if (!genuine_intel)
2640 return;
2641
2642 if (family != 6)
2643 return;
2644
2645 switch (model) {
2646 case 0x3C: /* HSW */
2647 case 0x45: /* HSW */
2648 case 0x46: /* HSW */
2649 do_gfx_perf_limit_reasons = 1;
2650 case 0x3F: /* HSX */
2651 do_core_perf_limit_reasons = 1;
2652 do_ring_perf_limit_reasons = 1;
2653 default:
2654 return;
2655 }
2656}
2657
Len Brown889facb2012-11-08 00:48:57 -05002658int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2659{
2660 unsigned long long msr;
2661 unsigned int dts;
2662 int cpu;
2663
2664 if (!(do_dts || do_ptm))
2665 return 0;
2666
2667 cpu = t->cpu_id;
2668
2669 /* DTS is per-core, no need to print for each thread */
2670 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2671 return 0;
2672
2673 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002674 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002675 return -1;
2676 }
2677
2678 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2679 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2680 return 0;
2681
2682 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002683 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002684 cpu, msr, tcc_activation_temp - dts);
2685
2686#ifdef THERM_DEBUG
2687 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2688 return 0;
2689
2690 dts = (msr >> 16) & 0x7F;
2691 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002692 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002693 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2694#endif
2695 }
2696
2697
2698 if (do_dts) {
2699 unsigned int resolution;
2700
2701 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2702 return 0;
2703
2704 dts = (msr >> 16) & 0x7F;
2705 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05002706 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05002707 cpu, msr, tcc_activation_temp - dts, resolution);
2708
2709#ifdef THERM_DEBUG
2710 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2711 return 0;
2712
2713 dts = (msr >> 16) & 0x7F;
2714 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002715 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002716 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2717#endif
2718 }
2719
2720 return 0;
2721}
Len Brown36229892016-02-26 20:51:02 -05002722
Len Brown889facb2012-11-08 00:48:57 -05002723void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2724{
Len Brownb7d8c142016-02-13 23:36:17 -05002725 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05002726 cpu, label,
2727 ((msr >> 15) & 1) ? "EN" : "DIS",
2728 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2729 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2730 (((msr >> 16) & 1) ? "EN" : "DIS"));
2731
2732 return;
2733}
2734
2735int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2736{
2737 unsigned long long msr;
2738 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05002739
2740 if (!do_rapl)
2741 return 0;
2742
2743 /* RAPL counters are per package, so print only for 1st thread/package */
2744 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2745 return 0;
2746
2747 cpu = t->cpu_id;
2748 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002749 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002750 return -1;
2751 }
2752
2753 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2754 return -1;
2755
Len Brownd8af6f52015-02-10 01:56:38 -05002756 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05002757 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
Len Brown889facb2012-11-08 00:48:57 -05002758 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05002759 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05002760 }
Len Brown144b44b2013-11-09 00:30:16 -05002761 if (do_rapl & RAPL_PKG_POWER_INFO) {
2762
Len Brown889facb2012-11-08 00:48:57 -05002763 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2764 return -5;
2765
2766
Len Brownb7d8c142016-02-13 23:36:17 -05002767 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 -05002768 cpu, msr,
2769 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2770 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2771 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2772 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2773
Len Brown144b44b2013-11-09 00:30:16 -05002774 }
2775 if (do_rapl & RAPL_PKG) {
2776
Len Brown889facb2012-11-08 00:48:57 -05002777 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2778 return -9;
2779
Len Brownb7d8c142016-02-13 23:36:17 -05002780 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002781 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2782
2783 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05002784 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05002785 cpu,
2786 ((msr >> 47) & 1) ? "EN" : "DIS",
2787 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2788 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2789 ((msr >> 48) & 1) ? "EN" : "DIS");
2790 }
2791
Len Brown0b2bb692015-03-26 00:50:30 -04002792 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05002793 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2794 return -6;
2795
Len Brownb7d8c142016-02-13 23:36:17 -05002796 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 -05002797 cpu, msr,
2798 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2799 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2800 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2801 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04002802 }
2803 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05002804 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2805 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05002806 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002807 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2808
2809 print_power_limit_msr(cpu, msr, "DRAM Limit");
2810 }
Len Brown144b44b2013-11-09 00:30:16 -05002811 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05002812 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002813 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2814 return -7;
2815
Len Brownb7d8c142016-02-13 23:36:17 -05002816 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05002817 }
2818 }
2819 if (do_rapl & RAPL_CORES) {
Len Brownd8af6f52015-02-10 01:56:38 -05002820 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002821
2822 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2823 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05002824 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002825 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2826 print_power_limit_msr(cpu, msr, "Cores Limit");
2827 }
2828 }
2829 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05002830 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002831 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2832 return -8;
2833
Len Brownb7d8c142016-02-13 23:36:17 -05002834 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05002835
2836 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2837 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05002838 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002839 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2840 print_power_limit_msr(cpu, msr, "GFX Limit");
2841 }
2842 }
2843 return 0;
2844}
2845
Len Brownd7899442015-01-23 00:12:33 -05002846/*
2847 * SNB adds support for additional MSRs:
2848 *
2849 * MSR_PKG_C7_RESIDENCY 0x000003fa
2850 * MSR_CORE_C7_RESIDENCY 0x000003fe
2851 * MSR_PKG_C2_RESIDENCY 0x0000060d
2852 */
Len Brown103a8fe2010-10-22 23:53:03 -04002853
Len Brownd7899442015-01-23 00:12:33 -05002854int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002855{
2856 if (!genuine_intel)
2857 return 0;
2858
2859 switch (model) {
2860 case 0x2A:
2861 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04002862 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002863 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05002864 case 0x3C: /* HSW */
2865 case 0x3F: /* HSW */
2866 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002867 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002868 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002869 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002870 case 0x4F: /* BDX */
2871 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04002872 case 0x4E: /* SKL */
2873 case 0x5E: /* SKL */
Len Brown103a8fe2010-10-22 23:53:03 -04002874 return 1;
2875 }
2876 return 0;
2877}
2878
Len Brownd7899442015-01-23 00:12:33 -05002879/*
2880 * HSW adds support for additional MSRs:
2881 *
2882 * MSR_PKG_C8_RESIDENCY 0x00000630
2883 * MSR_PKG_C9_RESIDENCY 0x00000631
2884 * MSR_PKG_C10_RESIDENCY 0x00000632
2885 */
2886int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002887{
2888 if (!genuine_intel)
2889 return 0;
2890
2891 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05002892 case 0x45: /* HSW */
2893 case 0x3D: /* BDW */
Len Brown0b2bb692015-03-26 00:50:30 -04002894 case 0x4E: /* SKL */
2895 case 0x5E: /* SKL */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002896 return 1;
2897 }
2898 return 0;
2899}
2900
Len Brown0b2bb692015-03-26 00:50:30 -04002901/*
2902 * SKL adds support for additional MSRS:
2903 *
2904 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2905 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2906 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2907 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2908 */
2909int has_skl_msrs(unsigned int family, unsigned int model)
2910{
2911 if (!genuine_intel)
2912 return 0;
2913
2914 switch (model) {
2915 case 0x4E: /* SKL */
2916 case 0x5E: /* SKL */
2917 return 1;
2918 }
2919 return 0;
2920}
2921
2922
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002923
Len Brown144b44b2013-11-09 00:30:16 -05002924int is_slm(unsigned int family, unsigned int model)
2925{
2926 if (!genuine_intel)
2927 return 0;
2928 switch (model) {
2929 case 0x37: /* BYT */
2930 case 0x4D: /* AVN */
2931 return 1;
2932 }
2933 return 0;
2934}
2935
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002936int is_knl(unsigned int family, unsigned int model)
2937{
2938 if (!genuine_intel)
2939 return 0;
2940 switch (model) {
2941 case 0x57: /* KNL */
2942 return 1;
2943 }
2944 return 0;
2945}
2946
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02002947unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
2948{
2949 if (is_knl(family, model))
2950 return 1024;
2951 return 1;
2952}
2953
Len Brown144b44b2013-11-09 00:30:16 -05002954#define SLM_BCLK_FREQS 5
2955double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2956
2957double slm_bclk(void)
2958{
2959 unsigned long long msr = 3;
2960 unsigned int i;
2961 double freq;
2962
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002963 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05002964 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05002965
2966 i = msr & 0xf;
2967 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05002968 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Len Brown144b44b2013-11-09 00:30:16 -05002969 msr = 3;
2970 }
2971 freq = slm_freq_table[i];
2972
Len Brownb7d8c142016-02-13 23:36:17 -05002973 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05002974
2975 return freq;
2976}
2977
Len Brown103a8fe2010-10-22 23:53:03 -04002978double discover_bclk(unsigned int family, unsigned int model)
2979{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01002980 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04002981 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05002982 else if (is_slm(family, model))
2983 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04002984 else
2985 return 133.33;
2986}
2987
Len Brown889facb2012-11-08 00:48:57 -05002988/*
2989 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2990 * the Thermal Control Circuit (TCC) activates.
2991 * This is usually equal to tjMax.
2992 *
2993 * Older processors do not have this MSR, so there we guess,
2994 * but also allow cmdline over-ride with -T.
2995 *
2996 * Several MSR temperature values are in units of degrees-C
2997 * below this value, including the Digital Thermal Sensor (DTS),
2998 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2999 */
3000int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3001{
3002 unsigned long long msr;
3003 unsigned int target_c_local;
3004 int cpu;
3005
3006 /* tcc_activation_temp is used only for dts or ptm */
3007 if (!(do_dts || do_ptm))
3008 return 0;
3009
3010 /* this is a per-package concept */
3011 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3012 return 0;
3013
3014 cpu = t->cpu_id;
3015 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003016 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003017 return -1;
3018 }
3019
3020 if (tcc_activation_temp_override != 0) {
3021 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003022 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003023 cpu, tcc_activation_temp);
3024 return 0;
3025 }
3026
3027 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003028 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003029 goto guess;
3030
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003031 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003032 goto guess;
3033
Jean Delvare34821242014-05-01 11:40:19 +02003034 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003035
Len Brownd8af6f52015-02-10 01:56:38 -05003036 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003037 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003038 cpu, msr, target_c_local);
3039
Jean Delvare34821242014-05-01 11:40:19 +02003040 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003041 goto guess;
3042
3043 tcc_activation_temp = target_c_local;
3044
3045 return 0;
3046
3047guess:
3048 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003049 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003050 cpu, tcc_activation_temp);
3051
3052 return 0;
3053}
Len Brown69807a62015-11-21 12:22:47 -05003054
Len Brownaa8d8cc2016-03-11 13:26:03 -05003055void decode_feature_control_msr(void)
3056{
3057 unsigned long long msr;
3058
3059 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3060 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3061 base_cpu, msr,
3062 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3063 msr & (1 << 18) ? "SGX" : "");
3064}
3065
Len Brown69807a62015-11-21 12:22:47 -05003066void decode_misc_enable_msr(void)
3067{
3068 unsigned long long msr;
3069
3070 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003071 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
Len Brown69807a62015-11-21 12:22:47 -05003072 base_cpu, msr,
3073 msr & (1 << 3) ? "TCC" : "",
3074 msr & (1 << 16) ? "EIST" : "",
3075 msr & (1 << 18) ? "MONITOR" : "");
3076}
3077
Len Brownf0057312015-12-03 01:35:36 -05003078/*
3079 * Decode MSR_MISC_PWR_MGMT
3080 *
3081 * Decode the bits according to the Nehalem documentation
3082 * bit[0] seems to continue to have same meaning going forward
3083 * bit[1] less so...
3084 */
3085void decode_misc_pwr_mgmt_msr(void)
3086{
3087 unsigned long long msr;
3088
3089 if (!do_nhm_platform_info)
3090 return;
3091
3092 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003093 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB)\n",
Len Brownf0057312015-12-03 01:35:36 -05003094 base_cpu, msr,
3095 msr & (1 << 0) ? "DIS" : "EN",
3096 msr & (1 << 1) ? "EN" : "DIS");
3097}
Len Brown7f5c2582015-12-01 01:36:39 -05003098
Len Brownfcd17212015-03-23 20:29:09 -04003099void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003100{
Len Brown61a87ba2015-11-23 02:30:51 -05003101 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003102 unsigned int fms, family, model, stepping;
3103
3104 eax = ebx = ecx = edx = 0;
3105
Len Brown5aea2f72016-03-13 03:14:35 -04003106 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003107
3108 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3109 genuine_intel = 1;
3110
Len Brownd8af6f52015-02-10 01:56:38 -05003111 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003112 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003113 (char *)&ebx, (char *)&edx, (char *)&ecx);
3114
Len Brown5aea2f72016-03-13 03:14:35 -04003115 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003116 family = (fms >> 8) & 0xf;
3117 model = (fms >> 4) & 0xf;
3118 stepping = fms & 0xf;
3119 if (family == 6 || family == 0xf)
3120 model += ((fms >> 16) & 0xf) << 4;
3121
Len Brown69807a62015-11-21 12:22:47 -05003122 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05003123 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003124 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003125 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003126 ecx & (1 << 0) ? "SSE3" : "-",
3127 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003128 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003129 ecx & (1 << 7) ? "EIST" : "-",
3130 ecx & (1 << 8) ? "TM2" : "-",
3131 edx & (1 << 4) ? "TSC" : "-",
3132 edx & (1 << 5) ? "MSR" : "-",
3133 edx & (1 << 22) ? "ACPI-TM" : "-",
3134 edx & (1 << 29) ? "TM" : "-");
3135 }
Len Brown103a8fe2010-10-22 23:53:03 -04003136
Josh Triplettb2c95d92013-08-20 17:20:18 -07003137 if (!(edx & (1 << 5)))
3138 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003139
3140 /*
3141 * check max extended function levels of CPUID.
3142 * This is needed to check for invariant TSC.
3143 * This check is valid for both Intel and AMD.
3144 */
3145 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003146 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003147
Len Brown61a87ba2015-11-23 02:30:51 -05003148 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003149
Len Brownd7899442015-01-23 00:12:33 -05003150 /*
3151 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3152 * this check is valid for both Intel and AMD
3153 */
Len Brown5aea2f72016-03-13 03:14:35 -04003154 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003155 has_invariant_tsc = edx & (1 << 8);
3156 }
Len Brown103a8fe2010-10-22 23:53:03 -04003157
3158 /*
3159 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3160 * this check is valid for both Intel and AMD
3161 */
3162
Len Brown5aea2f72016-03-13 03:14:35 -04003163 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003164 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05003165 do_dts = eax & (1 << 0);
3166 do_ptm = eax & (1 << 6);
Len Brown7f5c2582015-12-01 01:36:39 -05003167 has_hwp = eax & (1 << 7);
3168 has_hwp_notify = eax & (1 << 8);
3169 has_hwp_activity_window = eax & (1 << 9);
3170 has_hwp_epp = eax & (1 << 10);
3171 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003172 has_epb = ecx & (1 << 3);
3173
Len Brownd8af6f52015-02-10 01:56:38 -05003174 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003175 fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003176 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3177 has_aperf ? "" : "No-",
3178 do_dts ? "" : "No-",
3179 do_ptm ? "" : "No-",
3180 has_hwp ? "" : "No-",
3181 has_hwp_notify ? "" : "No-",
3182 has_hwp_activity_window ? "" : "No-",
3183 has_hwp_epp ? "" : "No-",
3184 has_hwp_pkg ? "" : "No-",
3185 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003186
Len Brown69807a62015-11-21 12:22:47 -05003187 if (debug)
3188 decode_misc_enable_msr();
3189
Len Brownaa8d8cc2016-03-11 13:26:03 -05003190 if (max_level >= 0x7) {
3191 int has_sgx;
3192
3193 ecx = 0;
3194
3195 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3196
3197 has_sgx = ebx & (1 << 2);
3198 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3199
3200 if (has_sgx)
3201 decode_feature_control_msr();
3202 }
3203
Len Brown61a87ba2015-11-23 02:30:51 -05003204 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003205 unsigned int eax_crystal;
3206 unsigned int ebx_tsc;
3207
3208 /*
3209 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3210 */
3211 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003212 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003213
3214 if (ebx_tsc != 0) {
3215
3216 if (debug && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003217 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003218 eax_crystal, ebx_tsc, crystal_hz);
3219
3220 if (crystal_hz == 0)
3221 switch(model) {
3222 case 0x4E: /* SKL */
3223 case 0x5E: /* SKL */
3224 crystal_hz = 24000000; /* 24 MHz */
3225 break;
3226 default:
3227 crystal_hz = 0;
3228 }
3229
3230 if (crystal_hz) {
3231 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
3232 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003233 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003234 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3235 }
3236 }
3237 }
Len Brown61a87ba2015-11-23 02:30:51 -05003238 if (max_level >= 0x16) {
3239 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3240
3241 /*
3242 * CPUID 16H Base MHz, Max MHz, Bus MHz
3243 */
3244 base_mhz = max_mhz = bus_mhz = edx = 0;
3245
Len Brown5aea2f72016-03-13 03:14:35 -04003246 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown61a87ba2015-11-23 02:30:51 -05003247 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003248 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003249 base_mhz, max_mhz, bus_mhz);
3250 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003251
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003252 if (has_aperf)
3253 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3254
Len Brownee7e38e2015-02-09 23:39:45 -05003255 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05003256 do_snb_cstates = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05003257 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3258 do_pc3 = (pkg_cstate_limit >= PCL__3);
3259 do_pc6 = (pkg_cstate_limit >= PCL__6);
3260 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05003261 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04003262 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05003263 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003264 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04003265
Len Brownf0057312015-12-03 01:35:36 -05003266 if (debug)
3267 decode_misc_pwr_mgmt_msr();
3268
Len Brown889facb2012-11-08 00:48:57 -05003269 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04003270 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05003271
Len Brownfcd17212015-03-23 20:29:09 -04003272 if (debug)
Colin Ian King1b693172016-03-02 13:50:25 +00003273 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003274
Len Browna2b7b742015-09-26 00:12:38 -04003275 if (has_skl_msrs(family, model))
3276 calculate_tsc_tweak();
3277
Len Brownfdf676e2016-02-27 01:28:12 -05003278 do_gfx_rc6_ms = !access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK);
3279
Len Brown27d47352016-02-27 00:37:54 -05003280 do_gfx_mhz = !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK);
3281
Len Brown889facb2012-11-08 00:48:57 -05003282 return;
Len Brown103a8fe2010-10-22 23:53:03 -04003283}
3284
Len Brownd8af6f52015-02-10 01:56:38 -05003285void help()
Len Brown103a8fe2010-10-22 23:53:03 -04003286{
Len Brownb7d8c142016-02-13 23:36:17 -05003287 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05003288 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3289 "\n"
3290 "Turbostat forks the specified COMMAND and prints statistics\n"
3291 "when COMMAND completes.\n"
3292 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3293 "to print statistics, until interrupted.\n"
3294 "--debug run in \"debug\" mode\n"
3295 "--interval sec Override default 5-second measurement interval\n"
3296 "--help print this help message\n"
3297 "--counter msr print 32-bit counter at address \"msr\"\n"
3298 "--Counter msr print 64-bit Counter at address \"msr\"\n"
Len Brownb7d8c142016-02-13 23:36:17 -05003299 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003300 "--msr msr print 32-bit value at address \"msr\"\n"
3301 "--MSR msr print 64-bit Value at address \"msr\"\n"
3302 "--version print version information\n"
3303 "\n"
3304 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04003305}
3306
3307
3308/*
3309 * in /dev/cpu/ return success for names that are numbers
3310 * ie. filter out ".", "..", "microcode".
3311 */
3312int dir_filter(const struct dirent *dirp)
3313{
3314 if (isdigit(dirp->d_name[0]))
3315 return 1;
3316 else
3317 return 0;
3318}
3319
3320int open_dev_cpu_msr(int dummy1)
3321{
3322 return 0;
3323}
3324
Len Brownc98d5d92012-06-04 00:56:40 -04003325void topology_probe()
3326{
3327 int i;
3328 int max_core_id = 0;
3329 int max_package_id = 0;
3330 int max_siblings = 0;
3331 struct cpu_topology {
3332 int core_id;
3333 int physical_package_id;
3334 } *cpus;
3335
3336 /* Initialize num_cpus, max_cpu_num */
3337 topo.num_cpus = 0;
3338 topo.max_cpu_num = 0;
3339 for_all_proc_cpus(count_cpus);
3340 if (!summary_only && topo.num_cpus > 1)
3341 show_cpu = 1;
3342
Len Brownd8af6f52015-02-10 01:56:38 -05003343 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003344 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04003345
3346 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003347 if (cpus == NULL)
3348 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003349
3350 /*
3351 * Allocate and initialize cpu_present_set
3352 */
3353 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003354 if (cpu_present_set == NULL)
3355 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003356 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3357 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3358 for_all_proc_cpus(mark_cpu_present);
3359
3360 /*
3361 * Allocate and initialize cpu_affinity_set
3362 */
3363 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003364 if (cpu_affinity_set == NULL)
3365 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003366 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3367 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3368
3369
3370 /*
3371 * For online cpus
3372 * find max_core_id, max_package_id
3373 */
3374 for (i = 0; i <= topo.max_cpu_num; ++i) {
3375 int siblings;
3376
3377 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003378 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003379 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04003380 continue;
3381 }
3382 cpus[i].core_id = get_core_id(i);
3383 if (cpus[i].core_id > max_core_id)
3384 max_core_id = cpus[i].core_id;
3385
3386 cpus[i].physical_package_id = get_physical_package_id(i);
3387 if (cpus[i].physical_package_id > max_package_id)
3388 max_package_id = cpus[i].physical_package_id;
3389
3390 siblings = get_num_ht_siblings(i);
3391 if (siblings > max_siblings)
3392 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003393 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003394 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003395 i, cpus[i].physical_package_id, cpus[i].core_id);
3396 }
3397 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003398 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003399 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003400 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003401 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003402 show_core = 1;
3403
3404 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003405 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003406 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003407 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003408 if (debug && !summary_only && topo.num_packages > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003409 show_pkg = 1;
3410
3411 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003412 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003413 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04003414
3415 free(cpus);
3416}
3417
3418void
3419allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3420{
3421 int i;
3422
3423 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3424 topo.num_packages, sizeof(struct thread_data));
3425 if (*t == NULL)
3426 goto error;
3427
3428 for (i = 0; i < topo.num_threads_per_core *
3429 topo.num_cores_per_pkg * topo.num_packages; i++)
3430 (*t)[i].cpu_id = -1;
3431
3432 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
3433 sizeof(struct core_data));
3434 if (*c == NULL)
3435 goto error;
3436
3437 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3438 (*c)[i].core_id = -1;
3439
3440 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
3441 if (*p == NULL)
3442 goto error;
3443
3444 for (i = 0; i < topo.num_packages; i++)
3445 (*p)[i].package_id = i;
3446
3447 return;
3448error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07003449 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04003450}
3451/*
3452 * init_counter()
3453 *
3454 * set cpu_id, core_num, pkg_num
3455 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3456 *
3457 * increment topo.num_cores when 1st core in pkg seen
3458 */
3459void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3460 struct pkg_data *pkg_base, int thread_num, int core_num,
3461 int pkg_num, int cpu_id)
3462{
3463 struct thread_data *t;
3464 struct core_data *c;
3465 struct pkg_data *p;
3466
3467 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3468 c = GET_CORE(core_base, core_num, pkg_num);
3469 p = GET_PKG(pkg_base, pkg_num);
3470
3471 t->cpu_id = cpu_id;
3472 if (thread_num == 0) {
3473 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3474 if (cpu_is_first_core_in_package(cpu_id))
3475 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3476 }
3477
3478 c->core_id = core_num;
3479 p->package_id = pkg_num;
3480}
3481
3482
3483int initialize_counters(int cpu_id)
3484{
3485 int my_thread_id, my_core_id, my_package_id;
3486
3487 my_package_id = get_physical_package_id(cpu_id);
3488 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07003489 my_thread_id = get_cpu_position_in_core(cpu_id);
3490 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04003491 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04003492
3493 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3494 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3495 return 0;
3496}
3497
3498void allocate_output_buffer()
3499{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003500 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04003501 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07003502 if (outp == NULL)
3503 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04003504}
Len Brown36229892016-02-26 20:51:02 -05003505void allocate_fd_percpu(void)
3506{
3507 fd_percpu = calloc(topo.max_cpu_num, sizeof(int));
3508 if (fd_percpu == NULL)
3509 err(-1, "calloc fd_percpu");
3510}
Len Brown562a2d32016-02-26 23:48:05 -05003511void allocate_irq_buffers(void)
3512{
3513 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
3514 if (irq_column_2_cpu == NULL)
3515 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04003516
Len Brown562a2d32016-02-26 23:48:05 -05003517 irqs_per_cpu = calloc(topo.max_cpu_num, sizeof(int));
3518 if (irqs_per_cpu == NULL)
3519 err(-1, "calloc %d", topo.max_cpu_num);
3520}
Len Brownc98d5d92012-06-04 00:56:40 -04003521void setup_all_buffers(void)
3522{
3523 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05003524 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05003525 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04003526 allocate_counters(&thread_even, &core_even, &package_even);
3527 allocate_counters(&thread_odd, &core_odd, &package_odd);
3528 allocate_output_buffer();
3529 for_all_proc_cpus(initialize_counters);
3530}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003531
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003532void set_base_cpu(void)
3533{
3534 base_cpu = sched_getcpu();
3535 if (base_cpu < 0)
3536 err(-ENODEV, "No valid cpus found");
3537
3538 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003539 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003540}
3541
Len Brown103a8fe2010-10-22 23:53:03 -04003542void turbostat_init()
3543{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003544 setup_all_buffers();
3545 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04003546 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04003547 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04003548 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04003549
Len Brown103a8fe2010-10-22 23:53:03 -04003550
Len Brownd8af6f52015-02-10 01:56:38 -05003551 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05003552 for_all_cpus(print_hwp, ODD_COUNTERS);
3553
3554 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003555 for_all_cpus(print_epb, ODD_COUNTERS);
3556
Len Brownd8af6f52015-02-10 01:56:38 -05003557 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04003558 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3559
Len Brownd8af6f52015-02-10 01:56:38 -05003560 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003561 for_all_cpus(print_rapl, ODD_COUNTERS);
3562
3563 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3564
Len Brownd8af6f52015-02-10 01:56:38 -05003565 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003566 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003567}
3568
3569int fork_it(char **argv)
3570{
Len Brown103a8fe2010-10-22 23:53:03 -04003571 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04003572 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04003573
Len Brownd91bb172012-11-01 00:08:19 -04003574 status = for_all_cpus(get_counters, EVEN_COUNTERS);
3575 if (status)
3576 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04003577 /* clear affinity side-effect of get_counters() */
3578 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04003579 gettimeofday(&tv_even, (struct timezone *)NULL);
3580
3581 child_pid = fork();
3582 if (!child_pid) {
3583 /* child */
3584 execvp(argv[0], argv);
3585 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04003586
3587 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07003588 if (child_pid == -1)
3589 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04003590
3591 signal(SIGINT, SIG_IGN);
3592 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07003593 if (waitpid(child_pid, &status, 0) == -1)
3594 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04003595 }
Len Brownc98d5d92012-06-04 00:56:40 -04003596 /*
3597 * n.b. fork_it() does not check for errors from for_all_cpus()
3598 * because re-starting is problematic when forking
3599 */
3600 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003601 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04003602 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04003603 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
3604 compute_average(EVEN_COUNTERS);
3605 format_all_counters(EVEN_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003606
Len Brownb7d8c142016-02-13 23:36:17 -05003607 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3608
3609 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04003610
Len Brownd91bb172012-11-01 00:08:19 -04003611 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04003612}
3613
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003614int get_and_dump_counters(void)
3615{
3616 int status;
3617
3618 status = for_all_cpus(get_counters, ODD_COUNTERS);
3619 if (status)
3620 return status;
3621
3622 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3623 if (status)
3624 return status;
3625
Len Brownb7d8c142016-02-13 23:36:17 -05003626 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003627
3628 return status;
3629}
3630
Len Brownd8af6f52015-02-10 01:56:38 -05003631void print_version() {
Len Brown0102b062016-02-27 03:11:29 -05003632 fprintf(outf, "turbostat version 4.11 27 Feb 2016"
Len Brownd8af6f52015-02-10 01:56:38 -05003633 " - Len Brown <lenb@kernel.org>\n");
3634}
3635
Len Brown103a8fe2010-10-22 23:53:03 -04003636void cmdline(int argc, char **argv)
3637{
3638 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05003639 int option_index = 0;
3640 static struct option long_options[] = {
3641 {"Counter", required_argument, 0, 'C'},
3642 {"counter", required_argument, 0, 'c'},
3643 {"Dump", no_argument, 0, 'D'},
3644 {"debug", no_argument, 0, 'd'},
3645 {"interval", required_argument, 0, 'i'},
3646 {"help", no_argument, 0, 'h'},
3647 {"Joules", no_argument, 0, 'J'},
3648 {"MSR", required_argument, 0, 'M'},
3649 {"msr", required_argument, 0, 'm'},
Len Brownb7d8c142016-02-13 23:36:17 -05003650 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05003651 {"Package", no_argument, 0, 'p'},
3652 {"processor", no_argument, 0, 'p'},
3653 {"Summary", no_argument, 0, 'S'},
3654 {"TCC", required_argument, 0, 'T'},
3655 {"version", no_argument, 0, 'v' },
3656 {0, 0, 0, 0 }
3657 };
Len Brown103a8fe2010-10-22 23:53:03 -04003658
3659 progname = argv[0];
3660
Len Brownb7d8c142016-02-13 23:36:17 -05003661 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05003662 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04003663 switch (opt) {
Len Brownd8af6f52015-02-10 01:56:38 -05003664 case 'C':
3665 sscanf(optarg, "%x", &extra_delta_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04003666 break;
Len Brownf9240812012-10-06 15:26:31 -04003667 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04003668 sscanf(optarg, "%x", &extra_delta_offset32);
3669 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003670 case 'D':
3671 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04003672 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003673 case 'd':
3674 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04003675 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003676 case 'h':
3677 default:
3678 help();
3679 exit(1);
3680 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05003681 {
3682 double interval = strtod(optarg, NULL);
3683
3684 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05003685 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05003686 interval);
3687 exit(2);
3688 }
3689
3690 interval_ts.tv_sec = interval;
3691 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
3692 }
Len Brown889facb2012-11-08 00:48:57 -05003693 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08003694 case 'J':
3695 rapl_joules++;
3696 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003697 case 'M':
3698 sscanf(optarg, "%x", &extra_msr_offset64);
3699 break;
3700 case 'm':
3701 sscanf(optarg, "%x", &extra_msr_offset32);
3702 break;
Len Brownb7d8c142016-02-13 23:36:17 -05003703 case 'o':
3704 outf = fopen_or_die(optarg, "w");
3705 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003706 case 'P':
3707 show_pkg_only++;
3708 break;
3709 case 'p':
3710 show_core_only++;
3711 break;
3712 case 'S':
3713 summary_only++;
3714 break;
3715 case 'T':
3716 tcc_activation_temp_override = atoi(optarg);
3717 break;
3718 case 'v':
3719 print_version();
3720 exit(0);
3721 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003722 }
3723 }
3724}
3725
3726int main(int argc, char **argv)
3727{
Len Brownb7d8c142016-02-13 23:36:17 -05003728 outf = stderr;
3729
Len Brown103a8fe2010-10-22 23:53:03 -04003730 cmdline(argc, argv);
3731
Len Brownd8af6f52015-02-10 01:56:38 -05003732 if (debug)
3733 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04003734
3735 turbostat_init();
3736
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003737 /* dump counters and exit */
3738 if (dump_only)
3739 return get_and_dump_counters();
3740
Len Brown103a8fe2010-10-22 23:53:03 -04003741 /*
3742 * if any params left, it must be a command to fork
3743 */
3744 if (argc - optind)
3745 return fork_it(argv + optind);
3746 else
3747 turbostat_loop();
3748
3749 return 0;
3750}