blob: 841f837e317fc7eb08b65faced6ce71e5cd3b1b6 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
32#include <sys/resource.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <sys/time.h>
36#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050037#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038#include <dirent.h>
39#include <string.h>
40#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040041#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050042#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070043#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040044#include <linux/capability.h>
45#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040046
Len Brown103a8fe2010-10-22 23:53:03 -040047char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050048FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050049int *fd_percpu;
Len Brown2a0609c2016-02-12 22:44:48 -050050struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050051unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050052unsigned int quiet;
Len Brownd8af6f52015-02-10 01:56:38 -050053unsigned int rapl_joules;
54unsigned int summary_only;
55unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040056unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070057unsigned int do_knl_cstates;
Len Brown0b2bb692015-03-26 00:50:30 -040058unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050059unsigned int do_slm_cstates;
60unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040061unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050062unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040063unsigned int do_irtl_snb;
64unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050065unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int genuine_intel;
67unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050068unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050069unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020070unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040071double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040072double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040073unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040074double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040075unsigned int show_pkg_only;
76unsigned int show_core_only;
77char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050078unsigned int do_rapl;
79unsigned int do_dts;
80unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050081unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050082unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050083unsigned int tcc_activation_temp;
84unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050085double rapl_power_units, rapl_time_units;
86double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050087double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040088unsigned int do_core_perf_limit_reasons;
89unsigned int do_gfx_perf_limit_reasons;
90unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040091unsigned int crystal_hz;
92unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040093int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040094double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050095unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
96 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
97unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
98unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
99unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
100unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500101unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500102
Len Browne6f9bb32013-12-03 02:19:19 -0500103#define RAPL_PKG (1 << 0)
104 /* 0x610 MSR_PKG_POWER_LIMIT */
105 /* 0x611 MSR_PKG_ENERGY_STATUS */
106#define RAPL_PKG_PERF_STATUS (1 << 1)
107 /* 0x613 MSR_PKG_PERF_STATUS */
108#define RAPL_PKG_POWER_INFO (1 << 2)
109 /* 0x614 MSR_PKG_POWER_INFO */
110
111#define RAPL_DRAM (1 << 3)
112 /* 0x618 MSR_DRAM_POWER_LIMIT */
113 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500114#define RAPL_DRAM_PERF_STATUS (1 << 4)
115 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400116#define RAPL_DRAM_POWER_INFO (1 << 5)
117 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500118
Jacob Pan91484942016-06-16 09:48:20 -0700119#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500120 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400121#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500122 /* 0x63a MSR_PP0_POLICY */
123
Len Brown0b2bb692015-03-26 00:50:30 -0400124#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500125 /* 0x640 MSR_PP1_POWER_LIMIT */
126 /* 0x641 MSR_PP1_ENERGY_STATUS */
127 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700128
129#define RAPL_CORES_ENERGY_STATUS (1 << 9)
130 /* 0x639 MSR_PP0_ENERGY_STATUS */
131#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500132#define TJMAX_DEFAULT 100
133
134#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400135
Len Brown388e9c82016-12-22 23:57:55 -0500136/*
137 * buffer size used by sscanf() for added column names
138 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
139 */
140#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500141#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500142
Len Brown103a8fe2010-10-22 23:53:03 -0400143int backwards_count;
144char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brownc98d5d92012-06-04 00:56:40 -0400146cpu_set_t *cpu_present_set, *cpu_affinity_set;
147size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown678a3bd2017-02-09 22:22:13 -0500148#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400149
Len Brownc98d5d92012-06-04 00:56:40 -0400150struct thread_data {
151 unsigned long long tsc;
152 unsigned long long aperf;
153 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500154 unsigned long long c1;
Len Brown562a2d32016-02-26 23:48:05 -0500155 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500156 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400157 unsigned int cpu_id;
158 unsigned int flags;
159#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
160#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500161 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400162} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400163
Len Brownc98d5d92012-06-04 00:56:40 -0400164struct core_data {
165 unsigned long long c3;
166 unsigned long long c6;
167 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500168 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500169 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400170 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500171 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400172} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400173
Len Brownc98d5d92012-06-04 00:56:40 -0400174struct pkg_data {
175 unsigned long long pc2;
176 unsigned long long pc3;
177 unsigned long long pc6;
178 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800179 unsigned long long pc8;
180 unsigned long long pc9;
181 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400182 unsigned long long pkg_wtd_core_c0;
183 unsigned long long pkg_any_core_c0;
184 unsigned long long pkg_any_gfxe_c0;
185 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400186 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500187 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400188 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500189 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
190 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
191 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
192 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
193 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
194 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
195 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500196 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400197} *package_even, *package_odd;
198
199#define ODD_COUNTERS thread_odd, core_odd, package_odd
200#define EVEN_COUNTERS thread_even, core_even, package_even
201
202#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
203 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
204 topo.num_threads_per_core + \
205 (core_no) * topo.num_threads_per_core + (thread_no))
206#define GET_CORE(core_base, core_no, pkg_no) \
207 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
208#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
209
Len Brown388e9c82016-12-22 23:57:55 -0500210enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
211enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
212enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
213
214struct msr_counter {
215 unsigned int msr_num;
216 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500217 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500218 unsigned int width;
219 enum counter_type type;
220 enum counter_format format;
221 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500222 unsigned int flags;
223#define FLAGS_HIDE (1 << 0)
224#define FLAGS_SHOW (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500225};
226
227struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500228 unsigned int added_thread_counters;
229 unsigned int added_core_counters;
230 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500231 struct msr_counter *tp;
232 struct msr_counter *cp;
233 struct msr_counter *pp;
234} sys;
235
Len Brownc98d5d92012-06-04 00:56:40 -0400236struct system_summary {
237 struct thread_data threads;
238 struct core_data cores;
239 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500240} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400241
242
243struct topo_params {
244 int num_packages;
245 int num_cpus;
246 int num_cores;
247 int max_cpu_num;
248 int num_cores_per_pkg;
249 int num_threads_per_core;
250} topo;
251
252struct timeval tv_even, tv_odd, tv_delta;
253
Len Brown562a2d32016-02-26 23:48:05 -0500254int *irq_column_2_cpu; /* /proc/interrupts column numbers */
255int *irqs_per_cpu; /* indexed by cpu_num */
256
Len Brownc98d5d92012-06-04 00:56:40 -0400257void setup_all_buffers(void);
258
259int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400260{
Len Brownc98d5d92012-06-04 00:56:40 -0400261 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400262}
Len Brown88c32812012-03-29 21:44:40 -0400263/*
Len Brownc98d5d92012-06-04 00:56:40 -0400264 * run func(thread, core, package) in topology order
265 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400266 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400267
Len Brownc98d5d92012-06-04 00:56:40 -0400268int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
269 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400270{
Len Brownc98d5d92012-06-04 00:56:40 -0400271 int retval, pkg_no, core_no, thread_no;
272
273 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
274 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
275 for (thread_no = 0; thread_no <
276 topo.num_threads_per_core; ++thread_no) {
277 struct thread_data *t;
278 struct core_data *c;
279 struct pkg_data *p;
280
281 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
282
283 if (cpu_is_not_present(t->cpu_id))
284 continue;
285
286 c = GET_CORE(core_base, core_no, pkg_no);
287 p = GET_PKG(pkg_base, pkg_no);
288
289 retval = func(t, c, p);
290 if (retval)
291 return retval;
292 }
293 }
294 }
295 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400296}
297
298int cpu_migrate(int cpu)
299{
Len Brownc98d5d92012-06-04 00:56:40 -0400300 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
301 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
302 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400303 return -1;
304 else
305 return 0;
306}
Len Brown36229892016-02-26 20:51:02 -0500307int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400308{
Len Brown103a8fe2010-10-22 23:53:03 -0400309 char pathname[32];
310 int fd;
311
Len Brown36229892016-02-26 20:51:02 -0500312 fd = fd_percpu[cpu];
313
314 if (fd)
315 return fd;
316
Len Brown103a8fe2010-10-22 23:53:03 -0400317 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
318 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400319 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400320 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 -0400321
Len Brown36229892016-02-26 20:51:02 -0500322 fd_percpu[cpu] = fd;
323
324 return fd;
325}
326
327int get_msr(int cpu, off_t offset, unsigned long long *msr)
328{
329 ssize_t retval;
330
331 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400332
Len Brown98481e72014-08-15 00:36:50 -0400333 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500334 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400335
336 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400337}
338
Len Brownfc04cc62014-02-06 00:55:19 -0500339/*
Len Brown812db3f2017-02-10 00:25:41 -0500340 * Each string in this array is compared in --show and --hide cmdline.
341 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500342 */
Len Brown812db3f2017-02-10 00:25:41 -0500343struct msr_counter bic[] = {
344 { 0x0, "Package" },
345 { 0x0, "Avg_MHz" },
346 { 0x0, "Bzy_MHz" },
347 { 0x0, "TSC_MHz" },
348 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500349 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500350 { 0x0, "Busy%" },
351 { 0x0, "CPU%c1" },
352 { 0x0, "CPU%c3" },
353 { 0x0, "CPU%c6" },
354 { 0x0, "CPU%c7" },
355 { 0x0, "ThreadC" },
356 { 0x0, "CoreTmp" },
357 { 0x0, "CoreCnt" },
358 { 0x0, "PkgTmp" },
359 { 0x0, "GFX%rc6" },
360 { 0x0, "GFXMHz" },
361 { 0x0, "Pkg%pc2" },
362 { 0x0, "Pkg%pc3" },
363 { 0x0, "Pkg%pc6" },
364 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500365 { 0x0, "Pkg%pc8" },
366 { 0x0, "Pkg%pc9" },
367 { 0x0, "Pkg%pc10" },
Len Brown812db3f2017-02-10 00:25:41 -0500368 { 0x0, "PkgWatt" },
369 { 0x0, "CorWatt" },
370 { 0x0, "GFXWatt" },
371 { 0x0, "PkgCnt" },
372 { 0x0, "RAMWatt" },
373 { 0x0, "PKG_%" },
374 { 0x0, "RAM_%" },
375 { 0x0, "Pkg_J" },
376 { 0x0, "Cor_J" },
377 { 0x0, "GFX_J" },
378 { 0x0, "RAM_J" },
379 { 0x0, "Core" },
380 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500381 { 0x0, "Mod%c6" },
Len Brown812db3f2017-02-10 00:25:41 -0500382};
383
384#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
385#define BIC_Package (1ULL << 0)
386#define BIC_Avg_MHz (1ULL << 1)
387#define BIC_Bzy_MHz (1ULL << 2)
388#define BIC_TSC_MHz (1ULL << 3)
389#define BIC_IRQ (1ULL << 4)
390#define BIC_SMI (1ULL << 5)
391#define BIC_Busy (1ULL << 6)
392#define BIC_CPU_c1 (1ULL << 7)
393#define BIC_CPU_c3 (1ULL << 8)
394#define BIC_CPU_c6 (1ULL << 9)
395#define BIC_CPU_c7 (1ULL << 10)
396#define BIC_ThreadC (1ULL << 11)
397#define BIC_CoreTmp (1ULL << 12)
398#define BIC_CoreCnt (1ULL << 13)
399#define BIC_PkgTmp (1ULL << 14)
400#define BIC_GFX_rc6 (1ULL << 15)
401#define BIC_GFXMHz (1ULL << 16)
402#define BIC_Pkgpc2 (1ULL << 17)
403#define BIC_Pkgpc3 (1ULL << 18)
404#define BIC_Pkgpc6 (1ULL << 19)
405#define BIC_Pkgpc7 (1ULL << 20)
Len Brown0f47c082017-01-27 00:50:45 -0500406#define BIC_Pkgpc8 (1ULL << 21)
407#define BIC_Pkgpc9 (1ULL << 22)
408#define BIC_Pkgpc10 (1ULL << 23)
409#define BIC_PkgWatt (1ULL << 24)
410#define BIC_CorWatt (1ULL << 25)
411#define BIC_GFXWatt (1ULL << 26)
412#define BIC_PkgCnt (1ULL << 27)
413#define BIC_RAMWatt (1ULL << 28)
414#define BIC_PKG__ (1ULL << 29)
415#define BIC_RAM__ (1ULL << 30)
416#define BIC_Pkg_J (1ULL << 31)
417#define BIC_Cor_J (1ULL << 32)
418#define BIC_GFX_J (1ULL << 33)
419#define BIC_RAM_J (1ULL << 34)
420#define BIC_Core (1ULL << 35)
421#define BIC_CPU (1ULL << 36)
422#define BIC_Mod_c6 (1ULL << 37)
Len Brown812db3f2017-02-10 00:25:41 -0500423
424unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
425unsigned long long bic_present;
426
427#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
428#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500429#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500430
431/*
432 * bic_lookup
433 * for all the strings in comma separate name_list,
434 * set the approprate bit in return value.
435 */
436unsigned long long bic_lookup(char *name_list)
437{
438 int i;
439 unsigned long long retval = 0;
440
441 while (name_list) {
442 char *comma;
443
444 comma = strchr(name_list, ',');
445
446 if (comma)
447 *comma = '\0';
448
449 for (i = 0; i < MAX_BIC; ++i) {
450 if (!strcmp(name_list, bic[i].name)) {
451 retval |= (1ULL << i);
452 break;
453 }
454 }
455 if (i == MAX_BIC) {
456 fprintf(stderr, "Invalid counter name: %s\n", name_list);
457 exit(-1);
458 }
459
460 name_list = comma;
461 if (name_list)
462 name_list++;
463
464 }
465 return retval;
466}
Len Brownfc04cc62014-02-06 00:55:19 -0500467
Len Browna829eb42011-02-10 23:36:34 -0500468void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400469{
Len Brown388e9c82016-12-22 23:57:55 -0500470 struct msr_counter *mp;
471
Len Brown812db3f2017-02-10 00:25:41 -0500472 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400473 outp += sprintf(outp, "\tPackage");
Len Brown812db3f2017-02-10 00:25:41 -0500474 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400475 outp += sprintf(outp, "\tCore");
Len Brown812db3f2017-02-10 00:25:41 -0500476 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400477 outp += sprintf(outp, "\tCPU");
Len Brown812db3f2017-02-10 00:25:41 -0500478 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400479 outp += sprintf(outp, "\tAvg_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500480 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400481 outp += sprintf(outp, "\tBusy%%");
Len Brown812db3f2017-02-10 00:25:41 -0500482 if (DO_BIC(BIC_Bzy_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400483 outp += sprintf(outp, "\tBzy_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500484 if (DO_BIC(BIC_TSC_MHz))
485 outp += sprintf(outp, "\tTSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500486
Len Brown812db3f2017-02-10 00:25:41 -0500487 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400488 outp += sprintf(outp, "\tIRQ");
Len Brown812db3f2017-02-10 00:25:41 -0500489 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400490 outp += sprintf(outp, "\tSMI");
Len Brown1cc21f72015-02-23 00:34:57 -0500491
Len Brown812db3f2017-02-10 00:25:41 -0500492 if (DO_BIC(BIC_CPU_c1))
Len Brown3d109de2016-04-22 23:24:27 -0400493 outp += sprintf(outp, "\tCPU%%c1");
Len Brown889facb2012-11-08 00:48:57 -0500494
Len Brown388e9c82016-12-22 23:57:55 -0500495 for (mp = sys.tp; mp; mp = mp->next) {
496 if (mp->format == FORMAT_RAW) {
497 if (mp->width == 64)
498 outp += sprintf(outp, "\t%18.18s", mp->name);
499 else
500 outp += sprintf(outp, "\t%10.10s", mp->name);
501 } else {
502 outp += sprintf(outp, "\t%-7.7s", mp->name);
503 }
504 }
505
Len Brown812db3f2017-02-10 00:25:41 -0500506 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown678a3bd2017-02-09 22:22:13 -0500507 outp += sprintf(outp, "\tCPU%%c3");
Len Brown812db3f2017-02-10 00:25:41 -0500508 if (DO_BIC(BIC_CPU_c6))
Len Brown678a3bd2017-02-09 22:22:13 -0500509 outp += sprintf(outp, "\tCPU%%c6");
Len Brown812db3f2017-02-10 00:25:41 -0500510 if (DO_BIC(BIC_CPU_c7))
Len Brown678a3bd2017-02-09 22:22:13 -0500511 outp += sprintf(outp, "\tCPU%%c7");
512
Len Brown0539ba12017-02-10 00:27:20 -0500513 if (DO_BIC(BIC_Mod_c6))
514 outp += sprintf(outp, "\tMod%%c6");
Len Brown678a3bd2017-02-09 22:22:13 -0500515
Len Brown812db3f2017-02-10 00:25:41 -0500516 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400517 outp += sprintf(outp, "\tCoreTmp");
Len Brown388e9c82016-12-22 23:57:55 -0500518
519 for (mp = sys.cp; mp; mp = mp->next) {
520 if (mp->format == FORMAT_RAW) {
521 if (mp->width == 64)
522 outp += sprintf(outp, "\t%18.18s", mp->name);
523 else
524 outp += sprintf(outp, "\t%10.10s", mp->name);
525 } else {
526 outp += sprintf(outp, "\t%-7.7s", mp->name);
527 }
528 }
529
Len Brown812db3f2017-02-10 00:25:41 -0500530 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400531 outp += sprintf(outp, "\tPkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500532
Len Brown812db3f2017-02-10 00:25:41 -0500533 if (DO_BIC(BIC_GFX_rc6))
Len Brown3d109de2016-04-22 23:24:27 -0400534 outp += sprintf(outp, "\tGFX%%rc6");
Len Brownfdf676e2016-02-27 01:28:12 -0500535
Len Brown812db3f2017-02-10 00:25:41 -0500536 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400537 outp += sprintf(outp, "\tGFXMHz");
Len Brown27d47352016-02-27 00:37:54 -0500538
Len Brown0b2bb692015-03-26 00:50:30 -0400539 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400540 outp += sprintf(outp, "\tTotl%%C0");
541 outp += sprintf(outp, "\tAny%%C0");
542 outp += sprintf(outp, "\tGFX%%C0");
543 outp += sprintf(outp, "\tCPUGFX%%");
Len Brown0b2bb692015-03-26 00:50:30 -0400544 }
545
Len Brown0f47c082017-01-27 00:50:45 -0500546 if (DO_BIC(BIC_Pkgpc2))
Len Brown3d109de2016-04-22 23:24:27 -0400547 outp += sprintf(outp, "\tPkg%%pc2");
Len Brown0f47c082017-01-27 00:50:45 -0500548 if (DO_BIC(BIC_Pkgpc3))
Len Brown3d109de2016-04-22 23:24:27 -0400549 outp += sprintf(outp, "\tPkg%%pc3");
Len Brown0f47c082017-01-27 00:50:45 -0500550 if (DO_BIC(BIC_Pkgpc6))
Len Brown3d109de2016-04-22 23:24:27 -0400551 outp += sprintf(outp, "\tPkg%%pc6");
Len Brown0f47c082017-01-27 00:50:45 -0500552 if (DO_BIC(BIC_Pkgpc7))
Len Brown3d109de2016-04-22 23:24:27 -0400553 outp += sprintf(outp, "\tPkg%%pc7");
Len Brown0f47c082017-01-27 00:50:45 -0500554 if (DO_BIC(BIC_Pkgpc8))
Len Brown3d109de2016-04-22 23:24:27 -0400555 outp += sprintf(outp, "\tPkg%%pc8");
Len Brown0f47c082017-01-27 00:50:45 -0500556 if (DO_BIC(BIC_Pkgpc9))
Len Brown3d109de2016-04-22 23:24:27 -0400557 outp += sprintf(outp, "\tPkg%%pc9");
Len Brown0f47c082017-01-27 00:50:45 -0500558 if (DO_BIC(BIC_Pkgpc10))
Len Brown3d109de2016-04-22 23:24:27 -0400559 outp += sprintf(outp, "\tPk%%pc10");
Len Brown103a8fe2010-10-22 23:53:03 -0400560
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800561 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500562 if (DO_BIC(BIC_PkgWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400563 outp += sprintf(outp, "\tPkgWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500564 if (DO_BIC(BIC_CorWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400565 outp += sprintf(outp, "\tCorWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500566 if (DO_BIC(BIC_GFXWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400567 outp += sprintf(outp, "\tGFXWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500568 if (DO_BIC(BIC_RAMWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400569 outp += sprintf(outp, "\tRAMWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500570 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400571 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500572 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400573 outp += sprintf(outp, "\tRAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500574 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500575 if (DO_BIC(BIC_Pkg_J))
Len Brown3d109de2016-04-22 23:24:27 -0400576 outp += sprintf(outp, "\tPkg_J");
Len Brown812db3f2017-02-10 00:25:41 -0500577 if (DO_BIC(BIC_Cor_J))
Len Brown3d109de2016-04-22 23:24:27 -0400578 outp += sprintf(outp, "\tCor_J");
Len Brown812db3f2017-02-10 00:25:41 -0500579 if (DO_BIC(BIC_GFX_J))
Len Brown3d109de2016-04-22 23:24:27 -0400580 outp += sprintf(outp, "\tGFX_J");
Len Brown812db3f2017-02-10 00:25:41 -0500581 if (DO_BIC(BIC_RAM_J))
Len Brown3d109de2016-04-22 23:24:27 -0400582 outp += sprintf(outp, "\tRAM_J");
Len Brown812db3f2017-02-10 00:25:41 -0500583 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400584 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500585 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400586 outp += sprintf(outp, "\tRAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800587 }
Len Brown388e9c82016-12-22 23:57:55 -0500588 for (mp = sys.pp; mp; mp = mp->next) {
589 if (mp->format == FORMAT_RAW) {
590 if (mp->width == 64)
591 outp += sprintf(outp, "\t%18.18s", mp->name);
592 else
593 outp += sprintf(outp, "\t%10.10s", mp->name);
594 } else {
595 outp += sprintf(outp, "\t%-7.7s", mp->name);
596 }
597 }
598
Len Brownc98d5d92012-06-04 00:56:40 -0400599 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400600}
601
Len Brownc98d5d92012-06-04 00:56:40 -0400602int dump_counters(struct thread_data *t, struct core_data *c,
603 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400604{
Len Brown388e9c82016-12-22 23:57:55 -0500605 int i;
606 struct msr_counter *mp;
607
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200608 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400609
Len Brownc98d5d92012-06-04 00:56:40 -0400610 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200611 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
612 t->cpu_id, t->flags);
613 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
614 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
615 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
616 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500617
Len Brown812db3f2017-02-10 00:25:41 -0500618 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -0500619 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500620 if (DO_BIC(BIC_SMI))
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200621 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500622
623 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
624 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
625 i, mp->msr_num, t->counter[i]);
626 }
Len Brownc98d5d92012-06-04 00:56:40 -0400627 }
Len Brown103a8fe2010-10-22 23:53:03 -0400628
Len Brownc98d5d92012-06-04 00:56:40 -0400629 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200630 outp += sprintf(outp, "core: %d\n", c->core_id);
631 outp += sprintf(outp, "c3: %016llX\n", c->c3);
632 outp += sprintf(outp, "c6: %016llX\n", c->c6);
633 outp += sprintf(outp, "c7: %016llX\n", c->c7);
634 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500635
636 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
637 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
638 i, mp->msr_num, c->counter[i]);
639 }
Len Brown0539ba12017-02-10 00:27:20 -0500640 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400641 }
642
643 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200644 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400645
646 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
647 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
648 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
649 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
650
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200651 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500652 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500653 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500654 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500655 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500656 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500657 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200658 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
659 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
660 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
661 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
662 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
663 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
664 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
665 outp += sprintf(outp, "Throttle PKG: %0X\n",
666 p->rapl_pkg_perf_status);
667 outp += sprintf(outp, "Throttle RAM: %0X\n",
668 p->rapl_dram_perf_status);
669 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500670
671 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
672 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
673 i, mp->msr_num, p->counter[i]);
674 }
Len Brownc98d5d92012-06-04 00:56:40 -0400675 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200676
677 outp += sprintf(outp, "\n");
678
Len Brownc98d5d92012-06-04 00:56:40 -0400679 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400680}
681
Len Browne23da032012-02-06 18:37:16 -0500682/*
683 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500684 */
Len Brownc98d5d92012-06-04 00:56:40 -0400685int format_counters(struct thread_data *t, struct core_data *c,
686 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400687{
Len Brown008d396e2017-02-10 00:29:51 -0500688 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500689 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500690 int i;
691 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400692
Len Brownc98d5d92012-06-04 00:56:40 -0400693 /* if showing only 1st thread in core and this isn't one, bail out */
694 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
695 return 0;
696
697 /* if showing only 1st thread in pkg and this isn't one, bail out */
698 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
699 return 0;
700
Len Brown103a8fe2010-10-22 23:53:03 -0400701 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
702
Len Brown008d396e2017-02-10 00:29:51 -0500703 tsc = t->tsc * tsc_tweak;
704
Len Brownc98d5d92012-06-04 00:56:40 -0400705 /* topo columns, print blanks on 1st (average) line */
706 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500707 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400708 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500709 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400710 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500711 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400712 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400713 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500714 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400715 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400716 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400717 else
Len Brown3d109de2016-04-22 23:24:27 -0400718 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400719 }
Len Brown812db3f2017-02-10 00:25:41 -0500720 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400721 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400722 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400723 else
Len Brown3d109de2016-04-22 23:24:27 -0400724 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400725 }
Len Brown812db3f2017-02-10 00:25:41 -0500726 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400727 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400728 }
Len Brownfc04cc62014-02-06 00:55:19 -0500729
Len Brown812db3f2017-02-10 00:25:41 -0500730 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400731 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500732 1.0 / units * t->aperf / interval_float);
733
Len Brown812db3f2017-02-10 00:25:41 -0500734 if (DO_BIC(BIC_Busy))
Len Brown008d396e2017-02-10 00:29:51 -0500735 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400736
Len Brown812db3f2017-02-10 00:25:41 -0500737 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400738 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400739 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400740 else
Len Brown3d109de2016-04-22 23:24:27 -0400741 outp += sprintf(outp, "\t%.0f",
Len Brown008d396e2017-02-10 00:29:51 -0500742 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400743 }
Len Brown103a8fe2010-10-22 23:53:03 -0400744
Len Brown812db3f2017-02-10 00:25:41 -0500745 if (DO_BIC(BIC_TSC_MHz))
746 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400747
Len Brown562a2d32016-02-26 23:48:05 -0500748 /* IRQ */
Len Brown812db3f2017-02-10 00:25:41 -0500749 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400750 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500751
Len Brown1cc21f72015-02-23 00:34:57 -0500752 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500753 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400754 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500755
Len Brown678a3bd2017-02-09 22:22:13 -0500756 /* C1 */
Len Brown812db3f2017-02-10 00:25:41 -0500757 if (DO_BIC(BIC_CPU_c1))
Len Brown008d396e2017-02-10 00:29:51 -0500758 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400759
Len Brown678a3bd2017-02-09 22:22:13 -0500760 /* Added counters */
761 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
762 if (mp->format == FORMAT_RAW) {
763 if (mp->width == 32)
764 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
765 else
766 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
767 } else if (mp->format == FORMAT_DELTA) {
768 outp += sprintf(outp, "\t%lld", t->counter[i]);
769 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500770 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500771 }
772 }
773
Len Brownc98d5d92012-06-04 00:56:40 -0400774 /* print per-core data only for 1st thread in core */
775 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
776 goto done;
777
Len Brown812db3f2017-02-10 00:25:41 -0500778 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown008d396e2017-02-10 00:29:51 -0500779 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500780 if (DO_BIC(BIC_CPU_c6))
Len Brown008d396e2017-02-10 00:29:51 -0500781 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500782 if (DO_BIC(BIC_CPU_c7))
Len Brown008d396e2017-02-10 00:29:51 -0500783 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400784
Len Brown0539ba12017-02-10 00:27:20 -0500785 /* Mod%c6 */
786 if (DO_BIC(BIC_Mod_c6))
Len Brown008d396e2017-02-10 00:29:51 -0500787 outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500788
Len Brown812db3f2017-02-10 00:25:41 -0500789 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400790 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500791
Len Brown388e9c82016-12-22 23:57:55 -0500792 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
793 if (mp->format == FORMAT_RAW) {
794 if (mp->width == 32)
795 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
796 else
797 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
798 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500799 outp += sprintf(outp, "\t%lld", c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500800 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500801 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500802 }
803 }
804
Len Brownc98d5d92012-06-04 00:56:40 -0400805 /* print per-package data only for 1st core in package */
806 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
807 goto done;
808
Len Brown0b2bb692015-03-26 00:50:30 -0400809 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500810 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400811 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500812
Len Brownfdf676e2016-02-27 01:28:12 -0500813 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500814 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400815 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400816 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400817 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400818 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400819 p->gfx_rc6_ms / 10.0 / interval_float);
820 }
821 }
Len Brownfdf676e2016-02-27 01:28:12 -0500822
Len Brown27d47352016-02-27 00:37:54 -0500823 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500824 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400825 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500826
Len Brown0b2bb692015-03-26 00:50:30 -0400827 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
828 if (do_skl_residency) {
Len Brown008d396e2017-02-10 00:29:51 -0500829 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/tsc);
830 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/tsc);
831 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/tsc);
832 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400833 }
834
Len Brown0f47c082017-01-27 00:50:45 -0500835 if (DO_BIC(BIC_Pkgpc2))
Len Brown008d396e2017-02-10 00:29:51 -0500836 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500837 if (DO_BIC(BIC_Pkgpc3))
Len Brown008d396e2017-02-10 00:29:51 -0500838 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500839 if (DO_BIC(BIC_Pkgpc6))
Len Brown008d396e2017-02-10 00:29:51 -0500840 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500841 if (DO_BIC(BIC_Pkgpc7))
Len Brown008d396e2017-02-10 00:29:51 -0500842 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500843 if (DO_BIC(BIC_Pkgpc8))
Len Brown008d396e2017-02-10 00:29:51 -0500844 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500845 if (DO_BIC(BIC_Pkgpc9))
Len Brown008d396e2017-02-10 00:29:51 -0500846 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500847 if (DO_BIC(BIC_Pkgpc10))
Len Brown008d396e2017-02-10 00:29:51 -0500848 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500849
850 /*
851 * If measurement interval exceeds minimum RAPL Joule Counter range,
852 * indicate that results are suspect by printing "**" in fraction place.
853 */
Len Brownfc04cc62014-02-06 00:55:19 -0500854 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400855 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500856 else
Len Browne975db52016-04-06 23:56:02 -0400857 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500858
Len Brown812db3f2017-02-10 00:25:41 -0500859 if (DO_BIC(BIC_PkgWatt))
860 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
861 if (DO_BIC(BIC_CorWatt))
862 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
863 if (DO_BIC(BIC_GFXWatt))
864 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
865 if (DO_BIC(BIC_RAMWatt))
866 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
867 if (DO_BIC(BIC_Pkg_J))
868 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
869 if (DO_BIC(BIC_Cor_J))
870 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
871 if (DO_BIC(BIC_GFX_J))
872 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
873 if (DO_BIC(BIC_RAM_J))
874 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
875 if (DO_BIC(BIC_PKG__))
876 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
877 if (DO_BIC(BIC_RAM__))
878 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
879
Len Brown388e9c82016-12-22 23:57:55 -0500880 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
881 if (mp->format == FORMAT_RAW) {
882 if (mp->width == 32)
883 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
884 else
885 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
886 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500887 outp += sprintf(outp, "\t%lld", p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500888 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500889 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500890 }
891 }
892
Len Brownc98d5d92012-06-04 00:56:40 -0400893done:
Len Brownc98d5d92012-06-04 00:56:40 -0400894 outp += sprintf(outp, "\n");
895
896 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400897}
898
Len Brownb7d8c142016-02-13 23:36:17 -0500899void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400900{
Len Brownb7d8c142016-02-13 23:36:17 -0500901 FILE *filep;
902
903 if (outf == stderr)
904 filep = stdout;
905 else
906 filep = outf;
907
908 fputs(output_buffer, filep);
909 fflush(filep);
910
Len Brownc98d5d92012-06-04 00:56:40 -0400911 outp = output_buffer;
912}
Len Brownb7d8c142016-02-13 23:36:17 -0500913void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400914{
Len Brownb7d8c142016-02-13 23:36:17 -0500915 fputs(output_buffer, outf);
916 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400917 outp = output_buffer;
918}
919void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
920{
Len Browne23da032012-02-06 18:37:16 -0500921 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400922
Len Browne23da032012-02-06 18:37:16 -0500923 if (!printed || !summary_only)
924 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400925
Len Brownc98d5d92012-06-04 00:56:40 -0400926 if (topo.num_cpus > 1)
927 format_counters(&average.threads, &average.cores,
928 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400929
Len Browne23da032012-02-06 18:37:16 -0500930 printed = 1;
931
932 if (summary_only)
933 return;
934
Len Brownc98d5d92012-06-04 00:56:40 -0400935 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400936}
937
Len Brown889facb2012-11-08 00:48:57 -0500938#define DELTA_WRAP32(new, old) \
939 if (new > old) { \
940 old = new - old; \
941 } else { \
942 old = 0x100000000 + new - old; \
943 }
944
Len Brownba3dec92016-04-22 20:31:46 -0400945int
Len Brownc98d5d92012-06-04 00:56:40 -0400946delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400947{
Len Brown388e9c82016-12-22 23:57:55 -0500948 int i;
949 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400950
951 if (do_skl_residency) {
952 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
953 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
954 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
955 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
956 }
Len Brownc98d5d92012-06-04 00:56:40 -0400957 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -0500958 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500959 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -0500960 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500961 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -0500962 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500963 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800964 old->pc8 = new->pc8 - old->pc8;
965 old->pc9 = new->pc9 - old->pc9;
966 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500967 old->pkg_temp_c = new->pkg_temp_c;
968
Len Brown9185e982016-04-06 17:16:00 -0400969 /* flag an error when rc6 counter resets/wraps */
970 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
971 old->gfx_rc6_ms = -1;
972 else
973 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
974
Len Brown27d47352016-02-27 00:37:54 -0500975 old->gfx_mhz = new->gfx_mhz;
976
Len Brown889facb2012-11-08 00:48:57 -0500977 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
978 DELTA_WRAP32(new->energy_cores, old->energy_cores);
979 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
980 DELTA_WRAP32(new->energy_dram, old->energy_dram);
981 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
982 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400983
Len Brown388e9c82016-12-22 23:57:55 -0500984 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
985 if (mp->format == FORMAT_RAW)
986 old->counter[i] = new->counter[i];
987 else
988 old->counter[i] = new->counter[i] - old->counter[i];
989 }
990
Len Brownba3dec92016-04-22 20:31:46 -0400991 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400992}
Len Brown103a8fe2010-10-22 23:53:03 -0400993
Len Brownc98d5d92012-06-04 00:56:40 -0400994void
995delta_core(struct core_data *new, struct core_data *old)
996{
Len Brown388e9c82016-12-22 23:57:55 -0500997 int i;
998 struct msr_counter *mp;
999
Len Brownc98d5d92012-06-04 00:56:40 -04001000 old->c3 = new->c3 - old->c3;
1001 old->c6 = new->c6 - old->c6;
1002 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001003 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001004 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001005
1006 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1007 if (mp->format == FORMAT_RAW)
1008 old->counter[i] = new->counter[i];
1009 else
1010 old->counter[i] = new->counter[i] - old->counter[i];
1011 }
Len Brownc98d5d92012-06-04 00:56:40 -04001012}
Len Brown103a8fe2010-10-22 23:53:03 -04001013
Len Brownc3ae3312012-06-13 21:31:46 -04001014/*
1015 * old = new - old
1016 */
Len Brownba3dec92016-04-22 20:31:46 -04001017int
Len Brownc98d5d92012-06-04 00:56:40 -04001018delta_thread(struct thread_data *new, struct thread_data *old,
1019 struct core_data *core_delta)
1020{
Len Brown388e9c82016-12-22 23:57:55 -05001021 int i;
1022 struct msr_counter *mp;
1023
Len Brownc98d5d92012-06-04 00:56:40 -04001024 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001025
Len Brownc98d5d92012-06-04 00:56:40 -04001026 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001027 if (old->tsc < (1000 * 1000))
1028 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1029 "You can disable all c-states by booting with \"idle=poll\"\n"
1030 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001031
Len Brownc98d5d92012-06-04 00:56:40 -04001032 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001033
Len Brown812db3f2017-02-10 00:25:41 -05001034 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001035 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1036 old->aperf = new->aperf - old->aperf;
1037 old->mperf = new->mperf - old->mperf;
1038 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001039 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001040 }
Len Brownc98d5d92012-06-04 00:56:40 -04001041 }
Len Brown103a8fe2010-10-22 23:53:03 -04001042
Len Brown103a8fe2010-10-22 23:53:03 -04001043
Len Brown144b44b2013-11-09 00:30:16 -05001044 if (use_c1_residency_msr) {
1045 /*
1046 * Some models have a dedicated C1 residency MSR,
1047 * which should be more accurate than the derivation below.
1048 */
1049 } else {
1050 /*
1051 * As counter collection is not atomic,
1052 * it is possible for mperf's non-halted cycles + idle states
1053 * to exceed TSC's all cycles: show c1 = 0% in that case.
1054 */
1055 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1056 old->c1 = 0;
1057 else {
1058 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001059 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001060 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001061 }
Len Brownc98d5d92012-06-04 00:56:40 -04001062 }
Len Brownc3ae3312012-06-13 21:31:46 -04001063
Len Brownc98d5d92012-06-04 00:56:40 -04001064 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001065 if (debug > 1)
1066 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001067 old->mperf = 1; /* divide by 0 protection */
1068 }
1069
Len Brown812db3f2017-02-10 00:25:41 -05001070 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001071 old->irq_count = new->irq_count - old->irq_count;
1072
Len Brown812db3f2017-02-10 00:25:41 -05001073 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001074 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001075
Len Brown388e9c82016-12-22 23:57:55 -05001076 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1077 if (mp->format == FORMAT_RAW)
1078 old->counter[i] = new->counter[i];
1079 else
1080 old->counter[i] = new->counter[i] - old->counter[i];
1081 }
Len Brownba3dec92016-04-22 20:31:46 -04001082 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001083}
1084
1085int delta_cpu(struct thread_data *t, struct core_data *c,
1086 struct pkg_data *p, struct thread_data *t2,
1087 struct core_data *c2, struct pkg_data *p2)
1088{
Len Brownba3dec92016-04-22 20:31:46 -04001089 int retval = 0;
1090
Len Brownc98d5d92012-06-04 00:56:40 -04001091 /* calculate core delta only for 1st thread in core */
1092 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1093 delta_core(c, c2);
1094
1095 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001096 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1097 if (retval)
1098 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001099
1100 /* calculate package delta only for 1st core in package */
1101 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001102 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001103
Len Brownba3dec92016-04-22 20:31:46 -04001104 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001105}
1106
1107void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1108{
Len Brown388e9c82016-12-22 23:57:55 -05001109 int i;
1110 struct msr_counter *mp;
1111
Len Brownc98d5d92012-06-04 00:56:40 -04001112 t->tsc = 0;
1113 t->aperf = 0;
1114 t->mperf = 0;
1115 t->c1 = 0;
1116
Len Brown562a2d32016-02-26 23:48:05 -05001117 t->irq_count = 0;
1118 t->smi_count = 0;
1119
Len Brownc98d5d92012-06-04 00:56:40 -04001120 /* tells format_counters to dump all fields from this set */
1121 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1122
1123 c->c3 = 0;
1124 c->c6 = 0;
1125 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001126 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001127 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001128
Len Brown0b2bb692015-03-26 00:50:30 -04001129 p->pkg_wtd_core_c0 = 0;
1130 p->pkg_any_core_c0 = 0;
1131 p->pkg_any_gfxe_c0 = 0;
1132 p->pkg_both_core_gfxe_c0 = 0;
1133
Len Brownc98d5d92012-06-04 00:56:40 -04001134 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001135 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001136 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001137 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001138 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001139 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001140 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001141 p->pc8 = 0;
1142 p->pc9 = 0;
1143 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001144
1145 p->energy_pkg = 0;
1146 p->energy_dram = 0;
1147 p->energy_cores = 0;
1148 p->energy_gfx = 0;
1149 p->rapl_pkg_perf_status = 0;
1150 p->rapl_dram_perf_status = 0;
1151 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001152
Len Brownfdf676e2016-02-27 01:28:12 -05001153 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001154 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001155 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1156 t->counter[i] = 0;
1157
1158 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1159 c->counter[i] = 0;
1160
1161 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1162 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001163}
1164int sum_counters(struct thread_data *t, struct core_data *c,
1165 struct pkg_data *p)
1166{
Len Brown388e9c82016-12-22 23:57:55 -05001167 int i;
1168 struct msr_counter *mp;
1169
Len Brownc98d5d92012-06-04 00:56:40 -04001170 average.threads.tsc += t->tsc;
1171 average.threads.aperf += t->aperf;
1172 average.threads.mperf += t->mperf;
1173 average.threads.c1 += t->c1;
1174
Len Brown562a2d32016-02-26 23:48:05 -05001175 average.threads.irq_count += t->irq_count;
1176 average.threads.smi_count += t->smi_count;
1177
Len Brown388e9c82016-12-22 23:57:55 -05001178 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1179 if (mp->format == FORMAT_RAW)
1180 continue;
1181 average.threads.counter[i] += t->counter[i];
1182 }
1183
Len Brownc98d5d92012-06-04 00:56:40 -04001184 /* sum per-core values only for 1st thread in core */
1185 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1186 return 0;
1187
1188 average.cores.c3 += c->c3;
1189 average.cores.c6 += c->c6;
1190 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001191 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001192
Len Brown889facb2012-11-08 00:48:57 -05001193 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1194
Len Brown388e9c82016-12-22 23:57:55 -05001195 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1196 if (mp->format == FORMAT_RAW)
1197 continue;
1198 average.cores.counter[i] += c->counter[i];
1199 }
1200
Len Brownc98d5d92012-06-04 00:56:40 -04001201 /* sum per-pkg values only for 1st core in pkg */
1202 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1203 return 0;
1204
Len Brown0b2bb692015-03-26 00:50:30 -04001205 if (do_skl_residency) {
1206 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1207 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1208 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1209 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1210 }
1211
Len Brownc98d5d92012-06-04 00:56:40 -04001212 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001213 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001214 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001215 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001216 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001217 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001218 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001219 average.packages.pc8 += p->pc8;
1220 average.packages.pc9 += p->pc9;
1221 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001222
Len Brown889facb2012-11-08 00:48:57 -05001223 average.packages.energy_pkg += p->energy_pkg;
1224 average.packages.energy_dram += p->energy_dram;
1225 average.packages.energy_cores += p->energy_cores;
1226 average.packages.energy_gfx += p->energy_gfx;
1227
Len Brownfdf676e2016-02-27 01:28:12 -05001228 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001229 average.packages.gfx_mhz = p->gfx_mhz;
1230
Len Brown889facb2012-11-08 00:48:57 -05001231 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1232
1233 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1234 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001235
1236 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1237 if (mp->format == FORMAT_RAW)
1238 continue;
1239 average.packages.counter[i] += p->counter[i];
1240 }
Len Brownc98d5d92012-06-04 00:56:40 -04001241 return 0;
1242}
1243/*
1244 * sum the counters for all cpus in the system
1245 * compute the weighted average
1246 */
1247void compute_average(struct thread_data *t, struct core_data *c,
1248 struct pkg_data *p)
1249{
Len Brown388e9c82016-12-22 23:57:55 -05001250 int i;
1251 struct msr_counter *mp;
1252
Len Brownc98d5d92012-06-04 00:56:40 -04001253 clear_counters(&average.threads, &average.cores, &average.packages);
1254
1255 for_all_cpus(sum_counters, t, c, p);
1256
1257 average.threads.tsc /= topo.num_cpus;
1258 average.threads.aperf /= topo.num_cpus;
1259 average.threads.mperf /= topo.num_cpus;
1260 average.threads.c1 /= topo.num_cpus;
1261
1262 average.cores.c3 /= topo.num_cores;
1263 average.cores.c6 /= topo.num_cores;
1264 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001265 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001266
Len Brown0b2bb692015-03-26 00:50:30 -04001267 if (do_skl_residency) {
1268 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1269 average.packages.pkg_any_core_c0 /= topo.num_packages;
1270 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1271 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1272 }
1273
Len Brownc98d5d92012-06-04 00:56:40 -04001274 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001275 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001276 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001277 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001278 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001279 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001280 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001281
1282 average.packages.pc8 /= topo.num_packages;
1283 average.packages.pc9 /= topo.num_packages;
1284 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001285
1286 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1287 if (mp->format == FORMAT_RAW)
1288 continue;
1289 average.threads.counter[i] /= topo.num_cpus;
1290 }
1291 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1292 if (mp->format == FORMAT_RAW)
1293 continue;
1294 average.cores.counter[i] /= topo.num_cores;
1295 }
1296 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1297 if (mp->format == FORMAT_RAW)
1298 continue;
1299 average.packages.counter[i] /= topo.num_packages;
1300 }
Len Brownc98d5d92012-06-04 00:56:40 -04001301}
1302
1303static unsigned long long rdtsc(void)
1304{
1305 unsigned int low, high;
1306
1307 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1308
1309 return low | ((unsigned long long)high) << 32;
1310}
1311
Len Brownc98d5d92012-06-04 00:56:40 -04001312/*
Len Brown495c76542017-02-08 02:41:51 -05001313 * Open a file, and exit on failure
1314 */
1315FILE *fopen_or_die(const char *path, const char *mode)
1316{
1317 FILE *filep = fopen(path, mode);
1318
1319 if (!filep)
1320 err(1, "%s: open failed", path);
1321 return filep;
1322}
1323/*
1324 * snapshot_sysfs_counter()
1325 *
1326 * return snapshot of given counter
1327 */
1328unsigned long long snapshot_sysfs_counter(char *path)
1329{
1330 FILE *fp;
1331 int retval;
1332 unsigned long long counter;
1333
1334 fp = fopen_or_die(path, "r");
1335
1336 retval = fscanf(fp, "%lld", &counter);
1337 if (retval != 1)
1338 err(1, "snapshot_sysfs_counter(%s)", path);
1339
1340 fclose(fp);
1341
1342 return counter;
1343}
1344
1345int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1346{
1347 if (mp->msr_num != 0) {
1348 if (get_msr(cpu, mp->msr_num, counterp))
1349 return -1;
1350 } else {
1351 *counterp = snapshot_sysfs_counter(mp->path);
1352 }
1353
1354 return 0;
1355}
1356
1357/*
Len Brownc98d5d92012-06-04 00:56:40 -04001358 * get_counters(...)
1359 * migrate to cpu
1360 * acquire and record local counters for that cpu
1361 */
1362int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1363{
1364 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001365 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001366 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001367 struct msr_counter *mp;
1368 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001369
Len Browne52966c2012-11-08 22:38:05 -05001370 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001371 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001372 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001373 }
Len Brownc98d5d92012-06-04 00:56:40 -04001374
Len Brown0102b062016-02-27 03:11:29 -05001375retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001376 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1377
Len Brown812db3f2017-02-10 00:25:41 -05001378 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001379 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1380
1381 /*
1382 * The TSC, APERF and MPERF must be read together for
1383 * APERF/MPERF and MPERF/TSC to give accurate results.
1384 *
1385 * Unfortunately, APERF and MPERF are read by
1386 * individual system call, so delays may occur
1387 * between them. If the time to read them
1388 * varies by a large amount, we re-read them.
1389 */
1390
1391 /*
1392 * This initial dummy APERF read has been seen to
1393 * reduce jitter in the subsequent reads.
1394 */
1395
Len Brown9c63a652012-10-31 01:29:52 -04001396 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001397 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001398
1399 t->tsc = rdtsc(); /* re-read close to APERF */
1400
1401 tsc_before = t->tsc;
1402
1403 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1404 return -3;
1405
1406 tsc_between = rdtsc();
1407
Len Brown9c63a652012-10-31 01:29:52 -04001408 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001409 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001410
1411 tsc_after = rdtsc();
1412
1413 aperf_time = tsc_between - tsc_before;
1414 mperf_time = tsc_after - tsc_between;
1415
1416 /*
1417 * If the system call latency to read APERF and MPERF
1418 * differ by more than 2x, then try again.
1419 */
1420 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1421 aperf_mperf_retry_count++;
1422 if (aperf_mperf_retry_count < 5)
1423 goto retry;
1424 else
1425 warnx("cpu%d jitter %lld %lld",
1426 cpu, aperf_time, mperf_time);
1427 }
1428 aperf_mperf_retry_count = 0;
1429
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001430 t->aperf = t->aperf * aperf_mperf_multiplier;
1431 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001432 }
1433
Len Brown812db3f2017-02-10 00:25:41 -05001434 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001435 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001436 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001437 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1438 return -5;
1439 t->smi_count = msr & 0xFFFFFFFF;
1440 }
Len Brown0539ba12017-02-10 00:27:20 -05001441 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001442 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1443 return -6;
1444 }
1445
Len Brown388e9c82016-12-22 23:57:55 -05001446 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001447 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001448 return -10;
1449 }
1450
Len Brownc98d5d92012-06-04 00:56:40 -04001451 /* collect core counters only for 1st thread in core */
1452 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1453 return 0;
1454
Len Brown812db3f2017-02-10 00:25:41 -05001455 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001456 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1457 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001458 }
1459
Len Brown812db3f2017-02-10 00:25:41 -05001460 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001461 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1462 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001463 } else if (do_knl_cstates) {
1464 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1465 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001466 }
1467
Len Brown812db3f2017-02-10 00:25:41 -05001468 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001469 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1470 return -8;
1471
Len Brown0539ba12017-02-10 00:27:20 -05001472 if (DO_BIC(BIC_Mod_c6))
1473 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1474 return -8;
1475
Len Brown812db3f2017-02-10 00:25:41 -05001476 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001477 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1478 return -9;
1479 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1480 }
1481
Len Brown388e9c82016-12-22 23:57:55 -05001482 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001483 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001484 return -10;
1485 }
Len Brown889facb2012-11-08 00:48:57 -05001486
Len Brownc98d5d92012-06-04 00:56:40 -04001487 /* collect package counters only for 1st core in package */
1488 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1489 return 0;
1490
Len Brown0b2bb692015-03-26 00:50:30 -04001491 if (do_skl_residency) {
1492 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1493 return -10;
1494 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1495 return -11;
1496 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1497 return -12;
1498 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1499 return -13;
1500 }
Len Brown0f47c082017-01-27 00:50:45 -05001501 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001502 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1503 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001504 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001505 if (do_slm_cstates) {
1506 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1507 return -10;
1508 } else {
1509 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1510 return -10;
1511 }
1512 }
1513
Len Brown0f47c082017-01-27 00:50:45 -05001514 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001515 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1516 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001517 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001518 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1519 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001520 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001521 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1522 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001523 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001524 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1525 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001526 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001527 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1528 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001529
Len Brown889facb2012-11-08 00:48:57 -05001530 if (do_rapl & RAPL_PKG) {
1531 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1532 return -13;
1533 p->energy_pkg = msr & 0xFFFFFFFF;
1534 }
Jacob Pan91484942016-06-16 09:48:20 -07001535 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001536 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1537 return -14;
1538 p->energy_cores = msr & 0xFFFFFFFF;
1539 }
1540 if (do_rapl & RAPL_DRAM) {
1541 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1542 return -15;
1543 p->energy_dram = msr & 0xFFFFFFFF;
1544 }
1545 if (do_rapl & RAPL_GFX) {
1546 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1547 return -16;
1548 p->energy_gfx = msr & 0xFFFFFFFF;
1549 }
1550 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1551 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1552 return -16;
1553 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1554 }
1555 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1556 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1557 return -16;
1558 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1559 }
Len Brown812db3f2017-02-10 00:25:41 -05001560 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001561 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1562 return -17;
1563 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1564 }
Len Brownfdf676e2016-02-27 01:28:12 -05001565
Len Brown812db3f2017-02-10 00:25:41 -05001566 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001567 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1568
Len Brown812db3f2017-02-10 00:25:41 -05001569 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001570 p->gfx_mhz = gfx_cur_mhz;
1571
Len Brown388e9c82016-12-22 23:57:55 -05001572 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001573 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001574 return -10;
1575 }
1576
Len Brown103a8fe2010-10-22 23:53:03 -04001577 return 0;
1578}
1579
Len Brownee7e38e2015-02-09 23:39:45 -05001580/*
1581 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1582 * If you change the values, note they are used both in comparisons
1583 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1584 */
1585
1586#define PCLUKN 0 /* Unknown */
1587#define PCLRSV 1 /* Reserved */
1588#define PCL__0 2 /* PC0 */
1589#define PCL__1 3 /* PC1 */
1590#define PCL__2 4 /* PC2 */
1591#define PCL__3 5 /* PC3 */
1592#define PCL__4 6 /* PC4 */
1593#define PCL__6 7 /* PC6 */
1594#define PCL_6N 8 /* PC6 No Retention */
1595#define PCL_6R 9 /* PC6 Retention */
1596#define PCL__7 10 /* PC7 */
1597#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001598#define PCL__8 12 /* PC8 */
1599#define PCL__9 13 /* PC9 */
1600#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001601
1602int pkg_cstate_limit = PCLUKN;
1603char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001604 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001605
Len Browne9257f52015-04-01 21:02:57 -04001606int 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};
1607int 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};
1608int 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};
Len Brown0539ba12017-02-10 00:27:20 -05001609int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7};
Len Brownf2642882017-01-07 23:13:15 -05001610int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Browne9257f52015-04-01 21:02:57 -04001611int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Browne4085d52016-04-06 17:15:56 -04001612int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brownd8ebb442016-12-01 20:27:46 -05001613int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brownee7e38e2015-02-09 23:39:45 -05001614
Len Browna2b7b742015-09-26 00:12:38 -04001615
1616static void
1617calculate_tsc_tweak()
1618{
Len Browna2b7b742015-09-26 00:12:38 -04001619 tsc_tweak = base_hz / tsc_hz;
1620}
1621
Len Brownfcd17212015-03-23 20:29:09 -04001622static void
1623dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001624{
1625 unsigned long long msr;
1626 unsigned int ratio;
1627
Len Brownec0adc52015-11-12 02:42:31 -05001628 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001629
Len Brownb7d8c142016-02-13 23:36:17 -05001630 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001631
Len Brown103a8fe2010-10-22 23:53:03 -04001632 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001633 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001634 ratio, bclk, ratio * bclk);
1635
1636 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001637 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001638 ratio, bclk, ratio * bclk);
1639
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001640 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001641 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001642 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001643
Len Brownfcd17212015-03-23 20:29:09 -04001644 return;
1645}
1646
1647static void
1648dump_hsw_turbo_ratio_limits(void)
1649{
1650 unsigned long long msr;
1651 unsigned int ratio;
1652
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001653 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001654
Len Brownb7d8c142016-02-13 23:36:17 -05001655 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001656
1657 ratio = (msr >> 8) & 0xFF;
1658 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001659 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001660 ratio, bclk, ratio * bclk);
1661
1662 ratio = (msr >> 0) & 0xFF;
1663 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001664 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001665 ratio, bclk, ratio * bclk);
1666 return;
1667}
1668
1669static void
1670dump_ivt_turbo_ratio_limits(void)
1671{
1672 unsigned long long msr;
1673 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001674
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001675 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001676
Len Brownb7d8c142016-02-13 23:36:17 -05001677 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001678
1679 ratio = (msr >> 56) & 0xFF;
1680 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001681 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001682 ratio, bclk, ratio * bclk);
1683
1684 ratio = (msr >> 48) & 0xFF;
1685 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001686 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001687 ratio, bclk, ratio * bclk);
1688
1689 ratio = (msr >> 40) & 0xFF;
1690 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001691 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001692 ratio, bclk, ratio * bclk);
1693
1694 ratio = (msr >> 32) & 0xFF;
1695 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001696 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001697 ratio, bclk, ratio * bclk);
1698
1699 ratio = (msr >> 24) & 0xFF;
1700 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001701 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001702 ratio, bclk, ratio * bclk);
1703
1704 ratio = (msr >> 16) & 0xFF;
1705 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001706 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001707 ratio, bclk, ratio * bclk);
1708
1709 ratio = (msr >> 8) & 0xFF;
1710 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001711 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001712 ratio, bclk, ratio * bclk);
1713
1714 ratio = (msr >> 0) & 0xFF;
1715 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001716 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001717 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001718 return;
1719}
Len Brown31e07522017-01-31 23:07:49 -05001720int has_turbo_ratio_group_limits(int family, int model)
1721{
1722
1723 if (!genuine_intel)
1724 return 0;
1725
1726 switch (model) {
1727 case INTEL_FAM6_ATOM_GOLDMONT:
1728 case INTEL_FAM6_SKYLAKE_X:
1729 case INTEL_FAM6_ATOM_DENVERTON:
1730 return 1;
1731 }
1732 return 0;
1733}
Len Brown6574a5d2012-09-21 00:01:31 -04001734
Len Brownfcd17212015-03-23 20:29:09 -04001735static void
Len Brown31e07522017-01-31 23:07:49 -05001736dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001737{
Len Brown31e07522017-01-31 23:07:49 -05001738 unsigned long long msr, core_counts;
1739 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001740
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001741 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001742 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001743
Len Brown31e07522017-01-31 23:07:49 -05001744 if (has_turbo_ratio_group_limits(family, model)) {
1745 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1746 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1747 } else {
1748 core_counts = 0x0807060504030201;
1749 }
1750
Len Brown6574a5d2012-09-21 00:01:31 -04001751 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001752 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001753 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001754 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1755 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001756
1757 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001758 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001759 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001760 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1761 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001762
1763 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001764 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001765 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001766 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1767 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001768
1769 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001770 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001771 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001772 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1773 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001774
Len Brown103a8fe2010-10-22 23:53:03 -04001775 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001776 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001777 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001778 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1779 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001780
1781 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001782 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001783 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001784 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1785 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001786
1787 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001788 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001789 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001790 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1791 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001792
1793 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001794 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001795 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001796 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1797 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04001798 return;
1799}
Len Brown3a9a9412014-08-15 02:39:52 -04001800
Len Brownfcd17212015-03-23 20:29:09 -04001801static void
Len Brown0f7887c2017-01-12 23:49:18 -05001802dump_atom_turbo_ratio_limits(void)
1803{
1804 unsigned long long msr;
1805 unsigned int ratio;
1806
1807 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1808 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1809
1810 ratio = (msr >> 0) & 0x3F;
1811 if (ratio)
1812 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1813 ratio, bclk, ratio * bclk);
1814
1815 ratio = (msr >> 8) & 0x3F;
1816 if (ratio)
1817 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1818 ratio, bclk, ratio * bclk);
1819
1820 ratio = (msr >> 16) & 0x3F;
1821 if (ratio)
1822 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1823 ratio, bclk, ratio * bclk);
1824
1825 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1826 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1827
1828 ratio = (msr >> 24) & 0x3F;
1829 if (ratio)
1830 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1831 ratio, bclk, ratio * bclk);
1832
1833 ratio = (msr >> 16) & 0x3F;
1834 if (ratio)
1835 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1836 ratio, bclk, ratio * bclk);
1837
1838 ratio = (msr >> 8) & 0x3F;
1839 if (ratio)
1840 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1841 ratio, bclk, ratio * bclk);
1842
1843 ratio = (msr >> 0) & 0x3F;
1844 if (ratio)
1845 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1846 ratio, bclk, ratio * bclk);
1847}
1848
1849static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001850dump_knl_turbo_ratio_limits(void)
1851{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001852 const unsigned int buckets_no = 7;
1853
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001854 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001855 int delta_cores, delta_ratio;
1856 int i, b_nr;
1857 unsigned int cores[buckets_no];
1858 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001859
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001860 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001861
Len Brownb7d8c142016-02-13 23:36:17 -05001862 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001863 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001864
1865 /**
1866 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001867 * [0] -- Reserved
1868 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001869 * [15:8] -- Base value of freq ratio of bucket 1.
1870 * [20:16] -- +ve delta of number of active cores of bucket 2.
1871 * i.e. active cores of bucket 2 =
1872 * active cores of bucket 1 + delta
1873 * [23:21] -- Negative delta of freq ratio of bucket 2.
1874 * i.e. freq ratio of bucket 2 =
1875 * freq ratio of bucket 1 - delta
1876 * [28:24]-- +ve delta of number of active cores of bucket 3.
1877 * [31:29]-- -ve delta of freq ratio of bucket 3.
1878 * [36:32]-- +ve delta of number of active cores of bucket 4.
1879 * [39:37]-- -ve delta of freq ratio of bucket 4.
1880 * [44:40]-- +ve delta of number of active cores of bucket 5.
1881 * [47:45]-- -ve delta of freq ratio of bucket 5.
1882 * [52:48]-- +ve delta of number of active cores of bucket 6.
1883 * [55:53]-- -ve delta of freq ratio of bucket 6.
1884 * [60:56]-- +ve delta of number of active cores of bucket 7.
1885 * [63:61]-- -ve delta of freq ratio of bucket 7.
1886 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001887
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001888 b_nr = 0;
1889 cores[b_nr] = (msr & 0xFF) >> 1;
1890 ratio[b_nr] = (msr >> 8) & 0xFF;
1891
1892 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001893 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001894 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001895
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001896 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1897 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1898 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001899 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001900
1901 for (i = buckets_no - 1; i >= 0; i--)
1902 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001903 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05001904 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001905 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001906}
1907
1908static void
Len Brownfcd17212015-03-23 20:29:09 -04001909dump_nhm_cst_cfg(void)
1910{
1911 unsigned long long msr;
1912
Len Brown1df2e552017-01-07 23:24:57 -05001913 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001914
1915#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1916#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1917
Len Brown1df2e552017-01-07 23:24:57 -05001918 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001919
Len Brownb7d8c142016-02-13 23:36:17 -05001920 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001921 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1922 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1923 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1924 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1925 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001926 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001927 pkg_cstate_limit_strings[pkg_cstate_limit]);
1928 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001929}
1930
Len Brown6fb31432015-06-17 16:23:45 -04001931static void
1932dump_config_tdp(void)
1933{
1934 unsigned long long msr;
1935
1936 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001937 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001938 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001939
1940 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001941 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001942 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001943 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1944 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1945 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1946 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001947 }
Len Brownb7d8c142016-02-13 23:36:17 -05001948 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001949
1950 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001951 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001952 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001953 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1954 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1955 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1956 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001957 }
Len Brownb7d8c142016-02-13 23:36:17 -05001958 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001959
1960 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001961 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001962 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001963 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1964 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1965 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001966
Len Brown6fb31432015-06-17 16:23:45 -04001967 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001968 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001969 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001970 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1971 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001972}
Len Brown5a634262016-04-06 17:15:55 -04001973
1974unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1975
1976void print_irtl(void)
1977{
1978 unsigned long long msr;
1979
1980 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1981 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1982 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1983 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1984
1985 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
1986 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
1987 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1988 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1989
1990 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
1991 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
1992 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1993 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1994
1995 if (!do_irtl_hsw)
1996 return;
1997
1998 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
1999 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2000 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2001 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2002
2003 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2004 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2005 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2006 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2007
2008 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2009 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2010 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2011 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2012
2013}
Len Brown36229892016-02-26 20:51:02 -05002014void free_fd_percpu(void)
2015{
2016 int i;
2017
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002018 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002019 if (fd_percpu[i] != 0)
2020 close(fd_percpu[i]);
2021 }
2022
2023 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002024}
2025
Len Brownc98d5d92012-06-04 00:56:40 -04002026void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002027{
Len Brownc98d5d92012-06-04 00:56:40 -04002028 CPU_FREE(cpu_present_set);
2029 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002030 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002031
Len Brownc98d5d92012-06-04 00:56:40 -04002032 CPU_FREE(cpu_affinity_set);
2033 cpu_affinity_set = NULL;
2034 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002035
Len Brownc98d5d92012-06-04 00:56:40 -04002036 free(thread_even);
2037 free(core_even);
2038 free(package_even);
2039
2040 thread_even = NULL;
2041 core_even = NULL;
2042 package_even = NULL;
2043
2044 free(thread_odd);
2045 free(core_odd);
2046 free(package_odd);
2047
2048 thread_odd = NULL;
2049 core_odd = NULL;
2050 package_odd = NULL;
2051
2052 free(output_buffer);
2053 output_buffer = NULL;
2054 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002055
2056 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002057
2058 free(irq_column_2_cpu);
2059 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002060}
2061
Josh Triplett57a42a32013-08-20 17:20:17 -07002062
2063/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002064 * Parse a file containing a single int.
2065 */
2066int parse_int_file(const char *fmt, ...)
2067{
2068 va_list args;
2069 char path[PATH_MAX];
2070 FILE *filep;
2071 int value;
2072
2073 va_start(args, fmt);
2074 vsnprintf(path, sizeof(path), fmt, args);
2075 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002076 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002077 if (fscanf(filep, "%d", &value) != 1)
2078 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002079 fclose(filep);
2080 return value;
2081}
2082
2083/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002084 * get_cpu_position_in_core(cpu)
2085 * return the position of the CPU among its HT siblings in the core
2086 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002087 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002088int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002089{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002090 char path[64];
2091 FILE *filep;
2092 int this_cpu;
2093 char character;
2094 int i;
2095
2096 sprintf(path,
2097 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2098 cpu);
2099 filep = fopen(path, "r");
2100 if (filep == NULL) {
2101 perror(path);
2102 exit(1);
2103 }
2104
2105 for (i = 0; i < topo.num_threads_per_core; i++) {
2106 fscanf(filep, "%d", &this_cpu);
2107 if (this_cpu == cpu) {
2108 fclose(filep);
2109 return i;
2110 }
2111
2112 /* Account for no separator after last thread*/
2113 if (i != (topo.num_threads_per_core - 1))
2114 fscanf(filep, "%c", &character);
2115 }
2116
2117 fclose(filep);
2118 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002119}
2120
Len Brownc98d5d92012-06-04 00:56:40 -04002121/*
2122 * cpu_is_first_core_in_package(cpu)
2123 * return 1 if given CPU is 1st core in package
2124 */
2125int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002126{
Josh Triplett95aebc42013-08-20 17:20:16 -07002127 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002128}
2129
2130int get_physical_package_id(int cpu)
2131{
Josh Triplett95aebc42013-08-20 17:20:16 -07002132 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002133}
2134
2135int get_core_id(int cpu)
2136{
Josh Triplett95aebc42013-08-20 17:20:16 -07002137 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002138}
2139
Len Brownc98d5d92012-06-04 00:56:40 -04002140int get_num_ht_siblings(int cpu)
2141{
2142 char path[80];
2143 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002144 int sib1;
2145 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002146 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002147 char str[100];
2148 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002149
2150 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002151 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002152
Len Brownc98d5d92012-06-04 00:56:40 -04002153 /*
2154 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002155 * A ',' separated or '-' separated set of numbers
2156 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002157 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002158 fscanf(filep, "%d%c\n", &sib1, &character);
2159 fseek(filep, 0, SEEK_SET);
2160 fgets(str, 100, filep);
2161 ch = strchr(str, character);
2162 while (ch != NULL) {
2163 matches++;
2164 ch = strchr(ch+1, character);
2165 }
Len Brownc98d5d92012-06-04 00:56:40 -04002166
2167 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002168 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002169}
2170
Len Brown103a8fe2010-10-22 23:53:03 -04002171/*
Len Brownc98d5d92012-06-04 00:56:40 -04002172 * run func(thread, core, package) in topology order
2173 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002174 */
2175
Len Brownc98d5d92012-06-04 00:56:40 -04002176int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2177 struct pkg_data *, struct thread_data *, struct core_data *,
2178 struct pkg_data *), struct thread_data *thread_base,
2179 struct core_data *core_base, struct pkg_data *pkg_base,
2180 struct thread_data *thread_base2, struct core_data *core_base2,
2181 struct pkg_data *pkg_base2)
2182{
2183 int retval, pkg_no, core_no, thread_no;
2184
2185 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2186 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2187 for (thread_no = 0; thread_no <
2188 topo.num_threads_per_core; ++thread_no) {
2189 struct thread_data *t, *t2;
2190 struct core_data *c, *c2;
2191 struct pkg_data *p, *p2;
2192
2193 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2194
2195 if (cpu_is_not_present(t->cpu_id))
2196 continue;
2197
2198 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2199
2200 c = GET_CORE(core_base, core_no, pkg_no);
2201 c2 = GET_CORE(core_base2, core_no, pkg_no);
2202
2203 p = GET_PKG(pkg_base, pkg_no);
2204 p2 = GET_PKG(pkg_base2, pkg_no);
2205
2206 retval = func(t, c, p, t2, c2, p2);
2207 if (retval)
2208 return retval;
2209 }
2210 }
2211 }
2212 return 0;
2213}
2214
2215/*
2216 * run func(cpu) on every cpu in /proc/stat
2217 * return max_cpu number
2218 */
2219int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002220{
2221 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002222 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002223 int retval;
2224
Josh Triplett57a42a32013-08-20 17:20:17 -07002225 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002226
2227 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002228 if (retval != 0)
2229 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002230
Len Brownc98d5d92012-06-04 00:56:40 -04002231 while (1) {
2232 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 -04002233 if (retval != 1)
2234 break;
2235
Len Brownc98d5d92012-06-04 00:56:40 -04002236 retval = func(cpu_num);
2237 if (retval) {
2238 fclose(fp);
2239 return(retval);
2240 }
Len Brown103a8fe2010-10-22 23:53:03 -04002241 }
2242 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002243 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002244}
2245
2246void re_initialize(void)
2247{
Len Brownc98d5d92012-06-04 00:56:40 -04002248 free_all_buffers();
2249 setup_all_buffers();
2250 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002251}
2252
Len Brownc98d5d92012-06-04 00:56:40 -04002253
Len Brown103a8fe2010-10-22 23:53:03 -04002254/*
Len Brownc98d5d92012-06-04 00:56:40 -04002255 * count_cpus()
2256 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002257 */
Len Brownc98d5d92012-06-04 00:56:40 -04002258int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002259{
Len Brownc98d5d92012-06-04 00:56:40 -04002260 if (topo.max_cpu_num < cpu)
2261 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002262
Len Brownc98d5d92012-06-04 00:56:40 -04002263 topo.num_cpus += 1;
2264 return 0;
2265}
2266int mark_cpu_present(int cpu)
2267{
2268 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002269 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002270}
2271
Len Brown562a2d32016-02-26 23:48:05 -05002272/*
2273 * snapshot_proc_interrupts()
2274 *
2275 * read and record summary of /proc/interrupts
2276 *
2277 * return 1 if config change requires a restart, else return 0
2278 */
2279int snapshot_proc_interrupts(void)
2280{
2281 static FILE *fp;
2282 int column, retval;
2283
2284 if (fp == NULL)
2285 fp = fopen_or_die("/proc/interrupts", "r");
2286 else
2287 rewind(fp);
2288
2289 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2290 for (column = 0; column < topo.num_cpus; ++column) {
2291 int cpu_number;
2292
2293 retval = fscanf(fp, " CPU%d", &cpu_number);
2294 if (retval != 1)
2295 break;
2296
2297 if (cpu_number > topo.max_cpu_num) {
2298 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2299 return 1;
2300 }
2301
2302 irq_column_2_cpu[column] = cpu_number;
2303 irqs_per_cpu[cpu_number] = 0;
2304 }
2305
2306 /* read /proc/interrupt count lines and sum up irqs per cpu */
2307 while (1) {
2308 int column;
2309 char buf[64];
2310
2311 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2312 if (retval != 1)
2313 break;
2314
2315 /* read the count per cpu */
2316 for (column = 0; column < topo.num_cpus; ++column) {
2317
2318 int cpu_number, irq_count;
2319
2320 retval = fscanf(fp, " %d", &irq_count);
2321 if (retval != 1)
2322 break;
2323
2324 cpu_number = irq_column_2_cpu[column];
2325 irqs_per_cpu[cpu_number] += irq_count;
2326
2327 }
2328
2329 while (getc(fp) != '\n')
2330 ; /* flush interrupt description */
2331
2332 }
2333 return 0;
2334}
Len Brown27d47352016-02-27 00:37:54 -05002335/*
Len Brownfdf676e2016-02-27 01:28:12 -05002336 * snapshot_gfx_rc6_ms()
2337 *
2338 * record snapshot of
2339 * /sys/class/drm/card0/power/rc6_residency_ms
2340 *
2341 * return 1 if config change requires a restart, else return 0
2342 */
2343int snapshot_gfx_rc6_ms(void)
2344{
2345 FILE *fp;
2346 int retval;
2347
2348 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2349
2350 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2351 if (retval != 1)
2352 err(1, "GFX rc6");
2353
2354 fclose(fp);
2355
2356 return 0;
2357}
2358/*
Len Brown27d47352016-02-27 00:37:54 -05002359 * snapshot_gfx_mhz()
2360 *
2361 * record snapshot of
2362 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2363 *
2364 * return 1 if config change requires a restart, else return 0
2365 */
2366int snapshot_gfx_mhz(void)
2367{
2368 static FILE *fp;
2369 int retval;
2370
2371 if (fp == NULL)
2372 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2373 else
2374 rewind(fp);
2375
2376 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2377 if (retval != 1)
2378 err(1, "GFX MHz");
2379
2380 return 0;
2381}
Len Brown562a2d32016-02-26 23:48:05 -05002382
2383/*
2384 * snapshot /proc and /sys files
2385 *
2386 * return 1 if configuration restart needed, else return 0
2387 */
2388int snapshot_proc_sysfs_files(void)
2389{
2390 if (snapshot_proc_interrupts())
2391 return 1;
2392
Len Brown812db3f2017-02-10 00:25:41 -05002393 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002394 snapshot_gfx_rc6_ms();
2395
Len Brown812db3f2017-02-10 00:25:41 -05002396 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002397 snapshot_gfx_mhz();
2398
Len Brown562a2d32016-02-26 23:48:05 -05002399 return 0;
2400}
2401
Len Brown103a8fe2010-10-22 23:53:03 -04002402void turbostat_loop()
2403{
Len Brownc98d5d92012-06-04 00:56:40 -04002404 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002405 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002406
Len Brown103a8fe2010-10-22 23:53:03 -04002407restart:
Len Browne52966c2012-11-08 22:38:05 -05002408 restarted++;
2409
Len Brown562a2d32016-02-26 23:48:05 -05002410 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002411 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002412 if (retval < -1) {
2413 exit(retval);
2414 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002415 if (restarted > 1) {
2416 exit(retval);
2417 }
Len Brownc98d5d92012-06-04 00:56:40 -04002418 re_initialize();
2419 goto restart;
2420 }
Len Browne52966c2012-11-08 22:38:05 -05002421 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002422 gettimeofday(&tv_even, (struct timezone *)NULL);
2423
2424 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002425 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002426 re_initialize();
2427 goto restart;
2428 }
Len Brown2a0609c2016-02-12 22:44:48 -05002429 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002430 if (snapshot_proc_sysfs_files())
2431 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002432 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002433 if (retval < -1) {
2434 exit(retval);
2435 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002436 re_initialize();
2437 goto restart;
2438 }
Len Brown103a8fe2010-10-22 23:53:03 -04002439 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002440 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002441 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2442 re_initialize();
2443 goto restart;
2444 }
Len Brownc98d5d92012-06-04 00:56:40 -04002445 compute_average(EVEN_COUNTERS);
2446 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002447 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002448 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002449 if (snapshot_proc_sysfs_files())
2450 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002451 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002452 if (retval < -1) {
2453 exit(retval);
2454 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002455 re_initialize();
2456 goto restart;
2457 }
Len Brown103a8fe2010-10-22 23:53:03 -04002458 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002459 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002460 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2461 re_initialize();
2462 goto restart;
2463 }
Len Brownc98d5d92012-06-04 00:56:40 -04002464 compute_average(ODD_COUNTERS);
2465 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002466 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002467 }
2468}
2469
2470void check_dev_msr()
2471{
2472 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002473 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002474
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002475 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2476 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002477 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2478 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002479}
2480
Len Brown98481e72014-08-15 00:36:50 -04002481void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002482{
Len Brown98481e72014-08-15 00:36:50 -04002483 struct __user_cap_header_struct cap_header_data;
2484 cap_user_header_t cap_header = &cap_header_data;
2485 struct __user_cap_data_struct cap_data_data;
2486 cap_user_data_t cap_data = &cap_data_data;
2487 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2488 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002489 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002490
2491 /* check for CAP_SYS_RAWIO */
2492 cap_header->pid = getpid();
2493 cap_header->version = _LINUX_CAPABILITY_VERSION;
2494 if (capget(cap_header, cap_data) < 0)
2495 err(-6, "capget(2) failed");
2496
2497 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2498 do_exit++;
2499 warnx("capget(CAP_SYS_RAWIO) failed,"
2500 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2501 }
2502
2503 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002504 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2505 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002506 do_exit++;
2507 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2508 }
2509
2510 /* if all else fails, thell them to be root */
2511 if (do_exit)
2512 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002513 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002514
2515 if (do_exit)
2516 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002517}
2518
Len Brownd7899442015-01-23 00:12:33 -05002519/*
2520 * NHM adds support for additional MSRs:
2521 *
2522 * MSR_SMI_COUNT 0x00000034
2523 *
Len Brownec0adc52015-11-12 02:42:31 -05002524 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002525 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002526 *
Len Browncf4cbe52017-01-01 13:08:33 -05002527 * MSR_MISC_PWR_MGMT 0x000001aa
2528 *
Len Brownd7899442015-01-23 00:12:33 -05002529 * MSR_PKG_C3_RESIDENCY 0x000003f8
2530 * MSR_PKG_C6_RESIDENCY 0x000003f9
2531 * MSR_CORE_C3_RESIDENCY 0x000003fc
2532 * MSR_CORE_C6_RESIDENCY 0x000003fd
2533 *
Len Brownee7e38e2015-02-09 23:39:45 -05002534 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002535 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002536 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002537 */
Len Brownee7e38e2015-02-09 23:39:45 -05002538int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002539{
Len Brownee7e38e2015-02-09 23:39:45 -05002540 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002541 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002542 int *pkg_cstate_limits;
2543
Len Brown103a8fe2010-10-22 23:53:03 -04002544 if (!genuine_intel)
2545 return 0;
2546
2547 if (family != 6)
2548 return 0;
2549
Len Brown21ed5572015-10-19 22:37:40 -04002550 bclk = discover_bclk(family, model);
2551
Len Brown103a8fe2010-10-22 23:53:03 -04002552 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002553 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2554 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002555 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002556 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2557 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2558 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2559 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002560 pkg_cstate_limits = nhm_pkg_cstate_limits;
2561 break;
Len Brown869ce692016-06-16 23:22:37 -04002562 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2563 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2564 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2565 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002566 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002567 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002568 break;
Len Brown869ce692016-06-16 23:22:37 -04002569 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2570 case INTEL_FAM6_HASWELL_X: /* HSX */
2571 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2572 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2573 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2574 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2575 case INTEL_FAM6_BROADWELL_X: /* BDX */
2576 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2577 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2578 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2579 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2580 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002581 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002582 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002583 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002584 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2585 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002586 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002587 break;
Len Brown869ce692016-06-16 23:22:37 -04002588 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002589 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002590 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002591 pkg_cstate_limits = slv_pkg_cstate_limits;
2592 break;
Len Brown869ce692016-06-16 23:22:37 -04002593 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002594 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002595 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002596 break;
Len Brown869ce692016-06-16 23:22:37 -04002597 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002598 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002599 pkg_cstate_limits = phi_pkg_cstate_limits;
2600 break;
Len Brown869ce692016-06-16 23:22:37 -04002601 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002602 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002603 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002604 pkg_cstate_limits = bxt_pkg_cstate_limits;
2605 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002606 default:
2607 return 0;
2608 }
Len Brown1df2e552017-01-07 23:24:57 -05002609 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002610 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002611
Len Brownec0adc52015-11-12 02:42:31 -05002612 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002613 base_ratio = (msr >> 8) & 0xFF;
2614
2615 base_hz = base_ratio * bclk * 1000000;
2616 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002617 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002618}
Len Brown0f7887c2017-01-12 23:49:18 -05002619/*
Len Brown495c76542017-02-08 02:41:51 -05002620 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002621 *
2622 * MSR_CC6_DEMOTION_POLICY_CONFIG
2623 * MSR_MC6_DEMOTION_POLICY_CONFIG
2624 */
2625
2626int has_slv_msrs(unsigned int family, unsigned int model)
2627{
2628 if (!genuine_intel)
2629 return 0;
2630
2631 switch (model) {
2632 case INTEL_FAM6_ATOM_SILVERMONT1:
2633 case INTEL_FAM6_ATOM_MERRIFIELD:
2634 case INTEL_FAM6_ATOM_MOOREFIELD:
2635 return 1;
2636 }
2637 return 0;
2638}
Len Brown7170a372017-01-27 02:13:27 -05002639int is_dnv(unsigned int family, unsigned int model)
2640{
2641
2642 if (!genuine_intel)
2643 return 0;
2644
2645 switch (model) {
2646 case INTEL_FAM6_ATOM_DENVERTON:
2647 return 1;
2648 }
2649 return 0;
2650}
Len Brownade0eba2017-02-10 01:56:47 -05002651int is_bdx(unsigned int family, unsigned int model)
2652{
2653
2654 if (!genuine_intel)
2655 return 0;
2656
2657 switch (model) {
2658 case INTEL_FAM6_BROADWELL_X:
2659 case INTEL_FAM6_BROADWELL_XEON_D:
2660 return 1;
2661 }
2662 return 0;
2663}
Len Brown34c761972017-01-27 02:36:41 -05002664int is_skx(unsigned int family, unsigned int model)
2665{
2666
2667 if (!genuine_intel)
2668 return 0;
2669
2670 switch (model) {
2671 case INTEL_FAM6_SKYLAKE_X:
2672 return 1;
2673 }
2674 return 0;
2675}
Len Brown0f7887c2017-01-12 23:49:18 -05002676
Len Brown31e07522017-01-31 23:07:49 -05002677int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002678{
Len Brown0f7887c2017-01-12 23:49:18 -05002679 if (has_slv_msrs(family, model))
2680 return 0;
2681
Len Brownd7899442015-01-23 00:12:33 -05002682 switch (model) {
2683 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002684 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2685 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2686 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002687 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002688 return 0;
2689 default:
2690 return 1;
2691 }
2692}
Len Brown0f7887c2017-01-12 23:49:18 -05002693int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2694{
2695 if (has_slv_msrs(family, model))
2696 return 1;
2697
2698 return 0;
2699}
Len Brown6574a5d2012-09-21 00:01:31 -04002700int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2701{
2702 if (!genuine_intel)
2703 return 0;
2704
2705 if (family != 6)
2706 return 0;
2707
2708 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002709 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2710 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002711 return 1;
2712 default:
2713 return 0;
2714 }
2715}
Len Brownfcd17212015-03-23 20:29:09 -04002716int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2717{
2718 if (!genuine_intel)
2719 return 0;
2720
2721 if (family != 6)
2722 return 0;
2723
2724 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002725 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002726 return 1;
2727 default:
2728 return 0;
2729 }
2730}
2731
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002732int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2733{
2734 if (!genuine_intel)
2735 return 0;
2736
2737 if (family != 6)
2738 return 0;
2739
2740 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002741 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002742 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002743 return 1;
2744 default:
2745 return 0;
2746 }
2747}
Len Brown31e07522017-01-31 23:07:49 -05002748int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
2749{
2750 if (!genuine_intel)
2751 return 0;
2752
2753 if (family != 6)
2754 return 0;
2755
2756 switch (model) {
2757 case INTEL_FAM6_ATOM_GOLDMONT:
2758 case INTEL_FAM6_SKYLAKE_X:
2759 return 1;
2760 default:
2761 return 0;
2762 }
2763}
Len Brown6fb31432015-06-17 16:23:45 -04002764int has_config_tdp(unsigned int family, unsigned int model)
2765{
2766 if (!genuine_intel)
2767 return 0;
2768
2769 if (family != 6)
2770 return 0;
2771
2772 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002773 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2774 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2775 case INTEL_FAM6_HASWELL_X: /* HSX */
2776 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2777 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2778 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2779 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2780 case INTEL_FAM6_BROADWELL_X: /* BDX */
2781 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2782 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2783 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2784 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2785 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2786 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002787
Len Brown869ce692016-06-16 23:22:37 -04002788 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002789 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002790 return 1;
2791 default:
2792 return 0;
2793 }
2794}
2795
Len Brownfcd17212015-03-23 20:29:09 -04002796static void
Colin Ian King1b693172016-03-02 13:50:25 +00002797dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002798{
2799 if (!do_nhm_platform_info)
2800 return;
2801
2802 dump_nhm_platform_info();
2803
2804 if (has_hsw_turbo_ratio_limit(family, model))
2805 dump_hsw_turbo_ratio_limits();
2806
2807 if (has_ivt_turbo_ratio_limit(family, model))
2808 dump_ivt_turbo_ratio_limits();
2809
Len Brown31e07522017-01-31 23:07:49 -05002810 if (has_turbo_ratio_limit(family, model))
2811 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04002812
Len Brown0f7887c2017-01-12 23:49:18 -05002813 if (has_atom_turbo_ratio_limit(family, model))
2814 dump_atom_turbo_ratio_limits();
2815
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002816 if (has_knl_turbo_ratio_limit(family, model))
2817 dump_knl_turbo_ratio_limits();
2818
Len Brown6fb31432015-06-17 16:23:45 -04002819 if (has_config_tdp(family, model))
2820 dump_config_tdp();
2821
Len Brownfcd17212015-03-23 20:29:09 -04002822 dump_nhm_cst_cfg();
2823}
2824
Len Brown6574a5d2012-09-21 00:01:31 -04002825
Len Brown889facb2012-11-08 00:48:57 -05002826/*
2827 * print_epb()
2828 * Decode the ENERGY_PERF_BIAS MSR
2829 */
2830int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2831{
2832 unsigned long long msr;
2833 char *epb_string;
2834 int cpu;
2835
2836 if (!has_epb)
2837 return 0;
2838
2839 cpu = t->cpu_id;
2840
2841 /* EPB is per-package */
2842 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2843 return 0;
2844
2845 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002846 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002847 return -1;
2848 }
2849
2850 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2851 return 0;
2852
Len Browne9be7dd2015-05-26 12:19:37 -04002853 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002854 case ENERGY_PERF_BIAS_PERFORMANCE:
2855 epb_string = "performance";
2856 break;
2857 case ENERGY_PERF_BIAS_NORMAL:
2858 epb_string = "balanced";
2859 break;
2860 case ENERGY_PERF_BIAS_POWERSAVE:
2861 epb_string = "powersave";
2862 break;
2863 default:
2864 epb_string = "custom";
2865 break;
2866 }
Len Brownb7d8c142016-02-13 23:36:17 -05002867 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002868
2869 return 0;
2870}
Len Brown7f5c2582015-12-01 01:36:39 -05002871/*
2872 * print_hwp()
2873 * Decode the MSR_HWP_CAPABILITIES
2874 */
2875int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2876{
2877 unsigned long long msr;
2878 int cpu;
2879
2880 if (!has_hwp)
2881 return 0;
2882
2883 cpu = t->cpu_id;
2884
2885 /* MSR_HWP_CAPABILITIES is per-package */
2886 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2887 return 0;
2888
2889 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002890 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002891 return -1;
2892 }
2893
2894 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2895 return 0;
2896
Len Brownb7d8c142016-02-13 23:36:17 -05002897 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002898 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2899
2900 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2901 if ((msr & (1 << 0)) == 0)
2902 return 0;
2903
2904 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2905 return 0;
2906
Len Brownb7d8c142016-02-13 23:36:17 -05002907 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002908 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2909 cpu, msr,
2910 (unsigned int)HWP_HIGHEST_PERF(msr),
2911 (unsigned int)HWP_GUARANTEED_PERF(msr),
2912 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2913 (unsigned int)HWP_LOWEST_PERF(msr));
2914
2915 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2916 return 0;
2917
Len Brownb7d8c142016-02-13 23:36:17 -05002918 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002919 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2920 cpu, msr,
2921 (unsigned int)(((msr) >> 0) & 0xff),
2922 (unsigned int)(((msr) >> 8) & 0xff),
2923 (unsigned int)(((msr) >> 16) & 0xff),
2924 (unsigned int)(((msr) >> 24) & 0xff),
2925 (unsigned int)(((msr) >> 32) & 0xff3),
2926 (unsigned int)(((msr) >> 42) & 0x1));
2927
2928 if (has_hwp_pkg) {
2929 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2930 return 0;
2931
Len Brownb7d8c142016-02-13 23:36:17 -05002932 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002933 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2934 cpu, msr,
2935 (unsigned int)(((msr) >> 0) & 0xff),
2936 (unsigned int)(((msr) >> 8) & 0xff),
2937 (unsigned int)(((msr) >> 16) & 0xff),
2938 (unsigned int)(((msr) >> 24) & 0xff),
2939 (unsigned int)(((msr) >> 32) & 0xff3));
2940 }
2941 if (has_hwp_notify) {
2942 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2943 return 0;
2944
Len Brownb7d8c142016-02-13 23:36:17 -05002945 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002946 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2947 cpu, msr,
2948 ((msr) & 0x1) ? "EN" : "Dis",
2949 ((msr) & 0x2) ? "EN" : "Dis");
2950 }
2951 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2952 return 0;
2953
Len Brownb7d8c142016-02-13 23:36:17 -05002954 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002955 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2956 cpu, msr,
2957 ((msr) & 0x1) ? "" : "No-",
2958 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05002959
2960 return 0;
2961}
2962
Len Brown3a9a9412014-08-15 02:39:52 -04002963/*
2964 * print_perf_limit()
2965 */
2966int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2967{
2968 unsigned long long msr;
2969 int cpu;
2970
2971 cpu = t->cpu_id;
2972
2973 /* per-package */
2974 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2975 return 0;
2976
2977 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002978 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002979 return -1;
2980 }
2981
2982 if (do_core_perf_limit_reasons) {
2983 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002984 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2985 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002986 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002987 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002988 (msr & 1 << 13) ? "Transitions, " : "",
2989 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2990 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2991 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2992 (msr & 1 << 9) ? "CorePwr, " : "",
2993 (msr & 1 << 8) ? "Amps, " : "",
2994 (msr & 1 << 6) ? "VR-Therm, " : "",
2995 (msr & 1 << 5) ? "Auto-HWP, " : "",
2996 (msr & 1 << 4) ? "Graphics, " : "",
2997 (msr & 1 << 2) ? "bit2, " : "",
2998 (msr & 1 << 1) ? "ThermStatus, " : "",
2999 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003000 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 -04003001 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003002 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003003 (msr & 1 << 29) ? "Transitions, " : "",
3004 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3005 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3006 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3007 (msr & 1 << 25) ? "CorePwr, " : "",
3008 (msr & 1 << 24) ? "Amps, " : "",
3009 (msr & 1 << 22) ? "VR-Therm, " : "",
3010 (msr & 1 << 21) ? "Auto-HWP, " : "",
3011 (msr & 1 << 20) ? "Graphics, " : "",
3012 (msr & 1 << 18) ? "bit18, " : "",
3013 (msr & 1 << 17) ? "ThermStatus, " : "",
3014 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003015
3016 }
3017 if (do_gfx_perf_limit_reasons) {
3018 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003019 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3020 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003021 (msr & 1 << 0) ? "PROCHOT, " : "",
3022 (msr & 1 << 1) ? "ThermStatus, " : "",
3023 (msr & 1 << 4) ? "Graphics, " : "",
3024 (msr & 1 << 6) ? "VR-Therm, " : "",
3025 (msr & 1 << 8) ? "Amps, " : "",
3026 (msr & 1 << 9) ? "GFXPwr, " : "",
3027 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3028 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003029 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003030 (msr & 1 << 16) ? "PROCHOT, " : "",
3031 (msr & 1 << 17) ? "ThermStatus, " : "",
3032 (msr & 1 << 20) ? "Graphics, " : "",
3033 (msr & 1 << 22) ? "VR-Therm, " : "",
3034 (msr & 1 << 24) ? "Amps, " : "",
3035 (msr & 1 << 25) ? "GFXPwr, " : "",
3036 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3037 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3038 }
3039 if (do_ring_perf_limit_reasons) {
3040 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003041 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3042 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003043 (msr & 1 << 0) ? "PROCHOT, " : "",
3044 (msr & 1 << 1) ? "ThermStatus, " : "",
3045 (msr & 1 << 6) ? "VR-Therm, " : "",
3046 (msr & 1 << 8) ? "Amps, " : "",
3047 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3048 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003049 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003050 (msr & 1 << 16) ? "PROCHOT, " : "",
3051 (msr & 1 << 17) ? "ThermStatus, " : "",
3052 (msr & 1 << 22) ? "VR-Therm, " : "",
3053 (msr & 1 << 24) ? "Amps, " : "",
3054 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3055 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3056 }
3057 return 0;
3058}
3059
Len Brown889facb2012-11-08 00:48:57 -05003060#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3061#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3062
Colin Ian King1b693172016-03-02 13:50:25 +00003063double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003064{
3065 unsigned long long msr;
3066
3067 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003068 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003069 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3070
3071 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003072 case INTEL_FAM6_ATOM_SILVERMONT1:
3073 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003074 return 30.0;
3075 default:
3076 return 135.0;
3077 }
3078}
3079
Andrey Semin40ee8e32014-12-05 00:07:00 -05003080/*
3081 * rapl_dram_energy_units_probe()
3082 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3083 */
3084static double
3085rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3086{
3087 /* only called for genuine_intel, family 6 */
3088
3089 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003090 case INTEL_FAM6_HASWELL_X: /* HSX */
3091 case INTEL_FAM6_BROADWELL_X: /* BDX */
3092 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3093 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003094 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003095 return (rapl_dram_energy_units = 15.3 / 1000000);
3096 default:
3097 return (rapl_energy_units);
3098 }
3099}
3100
Len Brown144b44b2013-11-09 00:30:16 -05003101
Len Brown889facb2012-11-08 00:48:57 -05003102/*
3103 * rapl_probe()
3104 *
Len Brown144b44b2013-11-09 00:30:16 -05003105 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003106 */
3107void rapl_probe(unsigned int family, unsigned int model)
3108{
3109 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003110 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003111 double tdp;
3112
3113 if (!genuine_intel)
3114 return;
3115
3116 if (family != 6)
3117 return;
3118
3119 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003120 case INTEL_FAM6_SANDYBRIDGE:
3121 case INTEL_FAM6_IVYBRIDGE:
3122 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3123 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3124 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3125 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3126 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003127 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003128 if (rapl_joules) {
3129 BIC_PRESENT(BIC_Pkg_J);
3130 BIC_PRESENT(BIC_Cor_J);
3131 BIC_PRESENT(BIC_GFX_J);
3132 } else {
3133 BIC_PRESENT(BIC_PkgWatt);
3134 BIC_PRESENT(BIC_CorWatt);
3135 BIC_PRESENT(BIC_GFXWatt);
3136 }
Len Brown889facb2012-11-08 00:48:57 -05003137 break;
Len Brown869ce692016-06-16 23:22:37 -04003138 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003139 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003140 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003141 if (rapl_joules)
3142 BIC_PRESENT(BIC_Pkg_J);
3143 else
3144 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003145 break;
Len Brown869ce692016-06-16 23:22:37 -04003146 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3147 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3148 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3149 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003150 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003151 BIC_PRESENT(BIC_PKG__);
3152 BIC_PRESENT(BIC_RAM__);
3153 if (rapl_joules) {
3154 BIC_PRESENT(BIC_Pkg_J);
3155 BIC_PRESENT(BIC_Cor_J);
3156 BIC_PRESENT(BIC_RAM_J);
3157 } else {
3158 BIC_PRESENT(BIC_PkgWatt);
3159 BIC_PRESENT(BIC_CorWatt);
3160 BIC_PRESENT(BIC_RAMWatt);
3161 }
Len Brown0b2bb692015-03-26 00:50:30 -04003162 break;
Len Brown869ce692016-06-16 23:22:37 -04003163 case INTEL_FAM6_HASWELL_X: /* HSX */
3164 case INTEL_FAM6_BROADWELL_X: /* BDX */
3165 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3166 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3167 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003168 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003169 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003170 BIC_PRESENT(BIC_PKG__);
3171 BIC_PRESENT(BIC_RAM__);
3172 if (rapl_joules) {
3173 BIC_PRESENT(BIC_Pkg_J);
3174 BIC_PRESENT(BIC_RAM_J);
3175 } else {
3176 BIC_PRESENT(BIC_PkgWatt);
3177 BIC_PRESENT(BIC_RAMWatt);
3178 }
Len Browne6f9bb32013-12-03 02:19:19 -05003179 break;
Len Brown869ce692016-06-16 23:22:37 -04003180 case INTEL_FAM6_SANDYBRIDGE_X:
3181 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003182 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 Brown812db3f2017-02-10 00:25:41 -05003183 BIC_PRESENT(BIC_PKG__);
3184 BIC_PRESENT(BIC_RAM__);
3185 if (rapl_joules) {
3186 BIC_PRESENT(BIC_Pkg_J);
3187 BIC_PRESENT(BIC_Cor_J);
3188 BIC_PRESENT(BIC_RAM_J);
3189 } else {
3190 BIC_PRESENT(BIC_PkgWatt);
3191 BIC_PRESENT(BIC_CorWatt);
3192 BIC_PRESENT(BIC_RAMWatt);
3193 }
Len Brown144b44b2013-11-09 00:30:16 -05003194 break;
Len Brown869ce692016-06-16 23:22:37 -04003195 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3196 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003197 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003198 if (rapl_joules) {
3199 BIC_PRESENT(BIC_Pkg_J);
3200 BIC_PRESENT(BIC_Cor_J);
3201 } else {
3202 BIC_PRESENT(BIC_PkgWatt);
3203 BIC_PRESENT(BIC_CorWatt);
3204 }
Len Brown889facb2012-11-08 00:48:57 -05003205 break;
Len Brown869ce692016-06-16 23:22:37 -04003206 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003207 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
Len Brown812db3f2017-02-10 00:25:41 -05003208 BIC_PRESENT(BIC_PKG__);
3209 BIC_PRESENT(BIC_RAM__);
3210 if (rapl_joules) {
3211 BIC_PRESENT(BIC_Pkg_J);
3212 BIC_PRESENT(BIC_Cor_J);
3213 BIC_PRESENT(BIC_RAM_J);
3214 } else {
3215 BIC_PRESENT(BIC_PkgWatt);
3216 BIC_PRESENT(BIC_CorWatt);
3217 BIC_PRESENT(BIC_RAMWatt);
3218 }
Jacob Pan0f644902016-06-16 09:48:22 -07003219 break;
Len Brown889facb2012-11-08 00:48:57 -05003220 default:
3221 return;
3222 }
3223
3224 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003225 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003226 return;
3227
3228 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003229 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003230 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3231 else
3232 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003233
Andrey Semin40ee8e32014-12-05 00:07:00 -05003234 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3235
Len Brown144b44b2013-11-09 00:30:16 -05003236 time_unit = msr >> 16 & 0xF;
3237 if (time_unit == 0)
3238 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003239
Len Brown144b44b2013-11-09 00:30:16 -05003240 rapl_time_units = 1.0 / (1 << (time_unit));
3241
3242 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003243
3244 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003245 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003246 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003247
3248 return;
3249}
3250
Colin Ian King1b693172016-03-02 13:50:25 +00003251void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003252{
3253 if (!genuine_intel)
3254 return;
3255
3256 if (family != 6)
3257 return;
3258
3259 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003260 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3261 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3262 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003263 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003264 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003265 do_core_perf_limit_reasons = 1;
3266 do_ring_perf_limit_reasons = 1;
3267 default:
3268 return;
3269 }
3270}
3271
Len Brown889facb2012-11-08 00:48:57 -05003272int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3273{
3274 unsigned long long msr;
3275 unsigned int dts;
3276 int cpu;
3277
3278 if (!(do_dts || do_ptm))
3279 return 0;
3280
3281 cpu = t->cpu_id;
3282
3283 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003284 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003285 return 0;
3286
3287 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003288 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003289 return -1;
3290 }
3291
3292 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3293 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3294 return 0;
3295
3296 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003297 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003298 cpu, msr, tcc_activation_temp - dts);
3299
3300#ifdef THERM_DEBUG
3301 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3302 return 0;
3303
3304 dts = (msr >> 16) & 0x7F;
3305 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003306 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003307 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3308#endif
3309 }
3310
3311
3312 if (do_dts) {
3313 unsigned int resolution;
3314
3315 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3316 return 0;
3317
3318 dts = (msr >> 16) & 0x7F;
3319 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003320 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003321 cpu, msr, tcc_activation_temp - dts, resolution);
3322
3323#ifdef THERM_DEBUG
3324 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3325 return 0;
3326
3327 dts = (msr >> 16) & 0x7F;
3328 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003329 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003330 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3331#endif
3332 }
3333
3334 return 0;
3335}
Len Brown36229892016-02-26 20:51:02 -05003336
Len Brown889facb2012-11-08 00:48:57 -05003337void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3338{
Len Brownb7d8c142016-02-13 23:36:17 -05003339 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003340 cpu, label,
3341 ((msr >> 15) & 1) ? "EN" : "DIS",
3342 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3343 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3344 (((msr >> 16) & 1) ? "EN" : "DIS"));
3345
3346 return;
3347}
3348
3349int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3350{
3351 unsigned long long msr;
3352 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003353
3354 if (!do_rapl)
3355 return 0;
3356
3357 /* RAPL counters are per package, so print only for 1st thread/package */
3358 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3359 return 0;
3360
3361 cpu = t->cpu_id;
3362 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003363 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003364 return -1;
3365 }
3366
3367 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3368 return -1;
3369
Len Brown96e47152017-01-21 02:26:00 -05003370 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3371 rapl_power_units, rapl_energy_units, rapl_time_units);
3372
Len Brown144b44b2013-11-09 00:30:16 -05003373 if (do_rapl & RAPL_PKG_POWER_INFO) {
3374
Len Brown889facb2012-11-08 00:48:57 -05003375 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3376 return -5;
3377
3378
Len Brownb7d8c142016-02-13 23:36:17 -05003379 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 -05003380 cpu, msr,
3381 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3382 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3383 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3384 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3385
Len Brown144b44b2013-11-09 00:30:16 -05003386 }
3387 if (do_rapl & RAPL_PKG) {
3388
Len Brown889facb2012-11-08 00:48:57 -05003389 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3390 return -9;
3391
Len Brownb7d8c142016-02-13 23:36:17 -05003392 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003393 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003394
3395 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003396 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003397 cpu,
3398 ((msr >> 47) & 1) ? "EN" : "DIS",
3399 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3400 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3401 ((msr >> 48) & 1) ? "EN" : "DIS");
3402 }
3403
Len Brown0b2bb692015-03-26 00:50:30 -04003404 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003405 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3406 return -6;
3407
Len Brownb7d8c142016-02-13 23:36:17 -05003408 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 -05003409 cpu, msr,
3410 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3411 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3412 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3413 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003414 }
3415 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003416 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3417 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003418 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003419 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003420
3421 print_power_limit_msr(cpu, msr, "DRAM Limit");
3422 }
Len Brown144b44b2013-11-09 00:30:16 -05003423 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003424 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3425 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003426
Len Brown96e47152017-01-21 02:26:00 -05003427 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003428 }
Jacob Pan91484942016-06-16 09:48:20 -07003429 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003430 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3431 return -9;
3432 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3433 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3434 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003435 }
3436 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003437 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3438 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003439
Len Brown96e47152017-01-21 02:26:00 -05003440 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003441
Len Brown96e47152017-01-21 02:26:00 -05003442 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3443 return -9;
3444 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3445 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3446 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003447 }
3448 return 0;
3449}
3450
Len Brownd7899442015-01-23 00:12:33 -05003451/*
3452 * SNB adds support for additional MSRs:
3453 *
3454 * MSR_PKG_C7_RESIDENCY 0x000003fa
3455 * MSR_CORE_C7_RESIDENCY 0x000003fe
3456 * MSR_PKG_C2_RESIDENCY 0x0000060d
3457 */
Len Brown103a8fe2010-10-22 23:53:03 -04003458
Len Brownd7899442015-01-23 00:12:33 -05003459int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003460{
3461 if (!genuine_intel)
3462 return 0;
3463
3464 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003465 case INTEL_FAM6_SANDYBRIDGE:
3466 case INTEL_FAM6_SANDYBRIDGE_X:
3467 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3468 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3469 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3470 case INTEL_FAM6_HASWELL_X: /* HSW */
3471 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3472 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3473 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3474 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3475 case INTEL_FAM6_BROADWELL_X: /* BDX */
3476 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3477 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3478 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3479 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3480 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3481 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3482 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003483 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003484 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003485 return 1;
3486 }
3487 return 0;
3488}
3489
Len Brownd7899442015-01-23 00:12:33 -05003490/*
3491 * HSW adds support for additional MSRs:
3492 *
Len Brown5a634262016-04-06 17:15:55 -04003493 * MSR_PKG_C8_RESIDENCY 0x00000630
3494 * MSR_PKG_C9_RESIDENCY 0x00000631
3495 * MSR_PKG_C10_RESIDENCY 0x00000632
3496 *
3497 * MSR_PKGC8_IRTL 0x00000633
3498 * MSR_PKGC9_IRTL 0x00000634
3499 * MSR_PKGC10_IRTL 0x00000635
3500 *
Len Brownd7899442015-01-23 00:12:33 -05003501 */
3502int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003503{
3504 if (!genuine_intel)
3505 return 0;
3506
3507 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003508 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3509 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3510 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3511 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3512 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3513 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3514 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003515 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003516 return 1;
3517 }
3518 return 0;
3519}
3520
Len Brown0b2bb692015-03-26 00:50:30 -04003521/*
3522 * SKL adds support for additional MSRS:
3523 *
3524 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3525 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3526 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3527 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3528 */
3529int has_skl_msrs(unsigned int family, unsigned int model)
3530{
3531 if (!genuine_intel)
3532 return 0;
3533
3534 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003535 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3536 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3537 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3538 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003539 return 1;
3540 }
3541 return 0;
3542}
3543
Len Brown144b44b2013-11-09 00:30:16 -05003544int is_slm(unsigned int family, unsigned int model)
3545{
3546 if (!genuine_intel)
3547 return 0;
3548 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003549 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3550 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003551 return 1;
3552 }
3553 return 0;
3554}
3555
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003556int is_knl(unsigned int family, unsigned int model)
3557{
3558 if (!genuine_intel)
3559 return 0;
3560 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003561 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003562 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003563 return 1;
3564 }
3565 return 0;
3566}
3567
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003568unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3569{
3570 if (is_knl(family, model))
3571 return 1024;
3572 return 1;
3573}
3574
Len Brown144b44b2013-11-09 00:30:16 -05003575#define SLM_BCLK_FREQS 5
3576double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3577
3578double slm_bclk(void)
3579{
3580 unsigned long long msr = 3;
3581 unsigned int i;
3582 double freq;
3583
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003584 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003585 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003586
3587 i = msr & 0xf;
3588 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003589 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003590 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003591 }
3592 freq = slm_freq_table[i];
3593
Len Brown96e47152017-01-21 02:26:00 -05003594 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003595 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003596
3597 return freq;
3598}
3599
Len Brown103a8fe2010-10-22 23:53:03 -04003600double discover_bclk(unsigned int family, unsigned int model)
3601{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003602 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003603 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003604 else if (is_slm(family, model))
3605 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003606 else
3607 return 133.33;
3608}
3609
Len Brown889facb2012-11-08 00:48:57 -05003610/*
3611 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3612 * the Thermal Control Circuit (TCC) activates.
3613 * This is usually equal to tjMax.
3614 *
3615 * Older processors do not have this MSR, so there we guess,
3616 * but also allow cmdline over-ride with -T.
3617 *
3618 * Several MSR temperature values are in units of degrees-C
3619 * below this value, including the Digital Thermal Sensor (DTS),
3620 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3621 */
3622int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3623{
3624 unsigned long long msr;
3625 unsigned int target_c_local;
3626 int cpu;
3627
3628 /* tcc_activation_temp is used only for dts or ptm */
3629 if (!(do_dts || do_ptm))
3630 return 0;
3631
3632 /* this is a per-package concept */
3633 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3634 return 0;
3635
3636 cpu = t->cpu_id;
3637 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003638 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003639 return -1;
3640 }
3641
3642 if (tcc_activation_temp_override != 0) {
3643 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003644 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003645 cpu, tcc_activation_temp);
3646 return 0;
3647 }
3648
3649 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003650 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003651 goto guess;
3652
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003653 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003654 goto guess;
3655
Jean Delvare34821242014-05-01 11:40:19 +02003656 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003657
Len Brown96e47152017-01-21 02:26:00 -05003658 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003659 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003660 cpu, msr, target_c_local);
3661
Jean Delvare34821242014-05-01 11:40:19 +02003662 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003663 goto guess;
3664
3665 tcc_activation_temp = target_c_local;
3666
3667 return 0;
3668
3669guess:
3670 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003671 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003672 cpu, tcc_activation_temp);
3673
3674 return 0;
3675}
Len Brown69807a62015-11-21 12:22:47 -05003676
Len Brownaa8d8cc2016-03-11 13:26:03 -05003677void decode_feature_control_msr(void)
3678{
3679 unsigned long long msr;
3680
3681 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3682 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3683 base_cpu, msr,
3684 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3685 msr & (1 << 18) ? "SGX" : "");
3686}
3687
Len Brown69807a62015-11-21 12:22:47 -05003688void decode_misc_enable_msr(void)
3689{
3690 unsigned long long msr;
3691
3692 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003693 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003694 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003695 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3696 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3697 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3698 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3699 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003700}
3701
Len Brown33148d62017-01-21 01:26:16 -05003702void decode_misc_feature_control(void)
3703{
3704 unsigned long long msr;
3705
3706 if (!has_misc_feature_control)
3707 return;
3708
3709 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
3710 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
3711 base_cpu, msr,
3712 msr & (0 << 0) ? "No-" : "",
3713 msr & (1 << 0) ? "No-" : "",
3714 msr & (2 << 0) ? "No-" : "",
3715 msr & (3 << 0) ? "No-" : "");
3716}
Len Brownf0057312015-12-03 01:35:36 -05003717/*
3718 * Decode MSR_MISC_PWR_MGMT
3719 *
3720 * Decode the bits according to the Nehalem documentation
3721 * bit[0] seems to continue to have same meaning going forward
3722 * bit[1] less so...
3723 */
3724void decode_misc_pwr_mgmt_msr(void)
3725{
3726 unsigned long long msr;
3727
3728 if (!do_nhm_platform_info)
3729 return;
3730
Len Browncf4cbe52017-01-01 13:08:33 -05003731 if (no_MSR_MISC_PWR_MGMT)
3732 return;
3733
Len Brownf0057312015-12-03 01:35:36 -05003734 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003735 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
Len Brownf0057312015-12-03 01:35:36 -05003736 base_cpu, msr,
3737 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003738 msr & (1 << 1) ? "EN" : "DIS",
3739 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003740}
Len Brown71616c82017-01-07 22:37:48 -05003741/*
3742 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3743 *
3744 * This MSRs are present on Silvermont processors,
3745 * Intel Atom processor E3000 series (Baytrail), and friends.
3746 */
3747void decode_c6_demotion_policy_msr(void)
3748{
3749 unsigned long long msr;
3750
3751 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3752 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3753 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3754
3755 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3756 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3757 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3758}
Len Brown7f5c2582015-12-01 01:36:39 -05003759
Len Brownfcd17212015-03-23 20:29:09 -04003760void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003761{
Len Brown61a87ba2015-11-23 02:30:51 -05003762 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003763 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003764 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003765
3766 eax = ebx = ecx = edx = 0;
3767
Len Brown5aea2f72016-03-13 03:14:35 -04003768 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003769
3770 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3771 genuine_intel = 1;
3772
Len Brown96e47152017-01-21 02:26:00 -05003773 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003774 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003775 (char *)&ebx, (char *)&edx, (char *)&ecx);
3776
Len Brown5aea2f72016-03-13 03:14:35 -04003777 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003778 family = (fms >> 8) & 0xf;
3779 model = (fms >> 4) & 0xf;
3780 stepping = fms & 0xf;
3781 if (family == 6 || family == 0xf)
3782 model += ((fms >> 16) & 0xf) << 4;
3783
Len Brown96e47152017-01-21 02:26:00 -05003784 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05003785 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003786 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003787 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003788 ecx & (1 << 0) ? "SSE3" : "-",
3789 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003790 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003791 ecx & (1 << 7) ? "EIST" : "-",
3792 ecx & (1 << 8) ? "TM2" : "-",
3793 edx & (1 << 4) ? "TSC" : "-",
3794 edx & (1 << 5) ? "MSR" : "-",
3795 edx & (1 << 22) ? "ACPI-TM" : "-",
3796 edx & (1 << 29) ? "TM" : "-");
3797 }
Len Brown103a8fe2010-10-22 23:53:03 -04003798
Josh Triplettb2c95d92013-08-20 17:20:18 -07003799 if (!(edx & (1 << 5)))
3800 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003801
3802 /*
3803 * check max extended function levels of CPUID.
3804 * This is needed to check for invariant TSC.
3805 * This check is valid for both Intel and AMD.
3806 */
3807 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003808 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003809
Len Brown61a87ba2015-11-23 02:30:51 -05003810 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003811
Len Brownd7899442015-01-23 00:12:33 -05003812 /*
3813 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3814 * this check is valid for both Intel and AMD
3815 */
Len Brown5aea2f72016-03-13 03:14:35 -04003816 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003817 has_invariant_tsc = edx & (1 << 8);
3818 }
Len Brown103a8fe2010-10-22 23:53:03 -04003819
3820 /*
3821 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3822 * this check is valid for both Intel and AMD
3823 */
3824
Len Brown5aea2f72016-03-13 03:14:35 -04003825 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003826 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003827 if (has_aperf) {
3828 BIC_PRESENT(BIC_Avg_MHz);
3829 BIC_PRESENT(BIC_Busy);
3830 BIC_PRESENT(BIC_Bzy_MHz);
3831 }
Len Brown889facb2012-11-08 00:48:57 -05003832 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003833 if (do_dts)
3834 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05003835 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05003836 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003837 if (do_ptm)
3838 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003839 has_hwp = eax & (1 << 7);
3840 has_hwp_notify = eax & (1 << 8);
3841 has_hwp_activity_window = eax & (1 << 9);
3842 has_hwp_epp = eax & (1 << 10);
3843 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003844 has_epb = ecx & (1 << 3);
3845
Len Brown96e47152017-01-21 02:26:00 -05003846 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05003847 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003848 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3849 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05003850 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05003851 do_dts ? "" : "No-",
3852 do_ptm ? "" : "No-",
3853 has_hwp ? "" : "No-",
3854 has_hwp_notify ? "" : "No-",
3855 has_hwp_activity_window ? "" : "No-",
3856 has_hwp_epp ? "" : "No-",
3857 has_hwp_pkg ? "" : "No-",
3858 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003859
Len Brown96e47152017-01-21 02:26:00 -05003860 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05003861 decode_misc_enable_msr();
3862
Len Brown33148d62017-01-21 01:26:16 -05003863
Len Brown96e47152017-01-21 02:26:00 -05003864 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003865 int has_sgx;
3866
3867 ecx = 0;
3868
3869 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3870
3871 has_sgx = ebx & (1 << 2);
3872 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3873
3874 if (has_sgx)
3875 decode_feature_control_msr();
3876 }
3877
Len Brown61a87ba2015-11-23 02:30:51 -05003878 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003879 unsigned int eax_crystal;
3880 unsigned int ebx_tsc;
3881
3882 /*
3883 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3884 */
3885 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003886 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003887
3888 if (ebx_tsc != 0) {
3889
Len Brown96e47152017-01-21 02:26:00 -05003890 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003891 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003892 eax_crystal, ebx_tsc, crystal_hz);
3893
3894 if (crystal_hz == 0)
3895 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04003896 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3897 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3898 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3899 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003900 crystal_hz = 24000000; /* 24.0 MHz */
3901 break;
Len Brown869ce692016-06-16 23:22:37 -04003902 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003903 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003904 crystal_hz = 25000000; /* 25.0 MHz */
3905 break;
Len Brown869ce692016-06-16 23:22:37 -04003906 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003907 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04003908 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003909 break;
3910 default:
3911 crystal_hz = 0;
3912 }
3913
3914 if (crystal_hz) {
3915 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05003916 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003917 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003918 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3919 }
3920 }
3921 }
Len Brown61a87ba2015-11-23 02:30:51 -05003922 if (max_level >= 0x16) {
3923 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3924
3925 /*
3926 * CPUID 16H Base MHz, Max MHz, Bus MHz
3927 */
3928 base_mhz = max_mhz = bus_mhz = edx = 0;
3929
Len Brown5aea2f72016-03-13 03:14:35 -04003930 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05003931 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003932 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003933 base_mhz, max_mhz, bus_mhz);
3934 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003935
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003936 if (has_aperf)
3937 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3938
Len Brown812db3f2017-02-10 00:25:41 -05003939 BIC_PRESENT(BIC_IRQ);
3940 BIC_PRESENT(BIC_TSC_MHz);
3941
3942 if (probe_nhm_msrs(family, model)) {
3943 do_nhm_platform_info = 1;
3944 BIC_PRESENT(BIC_CPU_c1);
3945 BIC_PRESENT(BIC_CPU_c3);
3946 BIC_PRESENT(BIC_CPU_c6);
3947 BIC_PRESENT(BIC_SMI);
3948 }
Len Brownd7899442015-01-23 00:12:33 -05003949 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05003950
3951 if (do_snb_cstates)
3952 BIC_PRESENT(BIC_CPU_c7);
3953
Len Brown5a634262016-04-06 17:15:55 -04003954 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05003955 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
3956 BIC_PRESENT(BIC_Pkgpc2);
3957 if (pkg_cstate_limit >= PCL__3)
3958 BIC_PRESENT(BIC_Pkgpc3);
3959 if (pkg_cstate_limit >= PCL__6)
3960 BIC_PRESENT(BIC_Pkgpc6);
3961 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
3962 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05003963 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05003964 BIC_NOT_PRESENT(BIC_Pkgpc2);
3965 BIC_NOT_PRESENT(BIC_Pkgpc3);
3966 BIC_PRESENT(BIC_Pkgpc6);
3967 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05003968 BIC_PRESENT(BIC_Mod_c6);
3969 use_c1_residency_msr = 1;
3970 }
Len Brown7170a372017-01-27 02:13:27 -05003971 if (is_dnv(family, model)) {
3972 BIC_PRESENT(BIC_CPU_c1);
3973 BIC_NOT_PRESENT(BIC_CPU_c3);
3974 BIC_NOT_PRESENT(BIC_Pkgpc3);
3975 BIC_NOT_PRESENT(BIC_CPU_c7);
3976 BIC_NOT_PRESENT(BIC_Pkgpc7);
3977 use_c1_residency_msr = 1;
3978 }
Len Brown34c761972017-01-27 02:36:41 -05003979 if (is_skx(family, model)) {
3980 BIC_NOT_PRESENT(BIC_CPU_c3);
3981 BIC_NOT_PRESENT(BIC_Pkgpc3);
3982 BIC_NOT_PRESENT(BIC_CPU_c7);
3983 BIC_NOT_PRESENT(BIC_Pkgpc7);
3984 }
Len Brownade0eba2017-02-10 01:56:47 -05003985 if (is_bdx(family, model)) {
3986 BIC_NOT_PRESENT(BIC_CPU_c7);
3987 BIC_NOT_PRESENT(BIC_Pkgpc7);
3988 }
Len Brown0f47c082017-01-27 00:50:45 -05003989 if (has_hsw_msrs(family, model)) {
3990 BIC_PRESENT(BIC_Pkgpc8);
3991 BIC_PRESENT(BIC_Pkgpc9);
3992 BIC_PRESENT(BIC_Pkgpc10);
3993 }
Len Brown5a634262016-04-06 17:15:55 -04003994 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04003995 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05003996 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003997 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04003998
Len Brown96e47152017-01-21 02:26:00 -05003999 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004000 decode_misc_pwr_mgmt_msr();
4001
Len Brown96e47152017-01-21 02:26:00 -05004002 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004003 decode_c6_demotion_policy_msr();
4004
Len Brown889facb2012-11-08 00:48:57 -05004005 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004006 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004007
Len Brown96e47152017-01-21 02:26:00 -05004008 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004009 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004010
Len Browna2b7b742015-09-26 00:12:38 -04004011 if (has_skl_msrs(family, model))
4012 calculate_tsc_tweak();
4013
Len Brown812db3f2017-02-10 00:25:41 -05004014 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4015 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004016
Len Brown812db3f2017-02-10 00:25:41 -05004017 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4018 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004019
Len Brown96e47152017-01-21 02:26:00 -05004020 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004021 decode_misc_feature_control();
4022
Len Brown889facb2012-11-08 00:48:57 -05004023 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004024}
4025
Len Brownd8af6f52015-02-10 01:56:38 -05004026void help()
Len Brown103a8fe2010-10-22 23:53:03 -04004027{
Len Brownb7d8c142016-02-13 23:36:17 -05004028 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05004029 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
4030 "\n"
4031 "Turbostat forks the specified COMMAND and prints statistics\n"
4032 "when COMMAND completes.\n"
4033 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
4034 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05004035 "--add add a counter\n"
4036 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brown96e47152017-01-21 02:26:00 -05004037 "--quiet skip decoding system configuration header\n"
Len Brownd8af6f52015-02-10 01:56:38 -05004038 "--interval sec Override default 5-second measurement interval\n"
4039 "--help print this help message\n"
Len Brownb7d8c142016-02-13 23:36:17 -05004040 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05004041 "--version print version information\n"
4042 "\n"
4043 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04004044}
4045
4046
4047/*
4048 * in /dev/cpu/ return success for names that are numbers
4049 * ie. filter out ".", "..", "microcode".
4050 */
4051int dir_filter(const struct dirent *dirp)
4052{
4053 if (isdigit(dirp->d_name[0]))
4054 return 1;
4055 else
4056 return 0;
4057}
4058
4059int open_dev_cpu_msr(int dummy1)
4060{
4061 return 0;
4062}
4063
Len Brownc98d5d92012-06-04 00:56:40 -04004064void topology_probe()
4065{
4066 int i;
4067 int max_core_id = 0;
4068 int max_package_id = 0;
4069 int max_siblings = 0;
4070 struct cpu_topology {
4071 int core_id;
4072 int physical_package_id;
4073 } *cpus;
4074
4075 /* Initialize num_cpus, max_cpu_num */
4076 topo.num_cpus = 0;
4077 topo.max_cpu_num = 0;
4078 for_all_proc_cpus(count_cpus);
4079 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004080 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004081
Len Brownd8af6f52015-02-10 01:56:38 -05004082 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004083 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004084
4085 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004086 if (cpus == NULL)
4087 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004088
4089 /*
4090 * Allocate and initialize cpu_present_set
4091 */
4092 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004093 if (cpu_present_set == NULL)
4094 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004095 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4096 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4097 for_all_proc_cpus(mark_cpu_present);
4098
4099 /*
4100 * Allocate and initialize cpu_affinity_set
4101 */
4102 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004103 if (cpu_affinity_set == NULL)
4104 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004105 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4106 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4107
4108
4109 /*
4110 * For online cpus
4111 * find max_core_id, max_package_id
4112 */
4113 for (i = 0; i <= topo.max_cpu_num; ++i) {
4114 int siblings;
4115
4116 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004117 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004118 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004119 continue;
4120 }
4121 cpus[i].core_id = get_core_id(i);
4122 if (cpus[i].core_id > max_core_id)
4123 max_core_id = cpus[i].core_id;
4124
4125 cpus[i].physical_package_id = get_physical_package_id(i);
4126 if (cpus[i].physical_package_id > max_package_id)
4127 max_package_id = cpus[i].physical_package_id;
4128
4129 siblings = get_num_ht_siblings(i);
4130 if (siblings > max_siblings)
4131 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004132 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004133 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004134 i, cpus[i].physical_package_id, cpus[i].core_id);
4135 }
4136 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004137 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004138 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004139 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004140 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004141 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004142
4143 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004144 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004145 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004146 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05004147 if (debug && !summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004148 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004149
4150 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004151 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004152 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004153
4154 free(cpus);
4155}
4156
4157void
4158allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4159{
4160 int i;
4161
4162 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004163 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004164 if (*t == NULL)
4165 goto error;
4166
4167 for (i = 0; i < topo.num_threads_per_core *
4168 topo.num_cores_per_pkg * topo.num_packages; i++)
4169 (*t)[i].cpu_id = -1;
4170
4171 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004172 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004173 if (*c == NULL)
4174 goto error;
4175
4176 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4177 (*c)[i].core_id = -1;
4178
Len Brown678a3bd2017-02-09 22:22:13 -05004179 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004180 if (*p == NULL)
4181 goto error;
4182
4183 for (i = 0; i < topo.num_packages; i++)
4184 (*p)[i].package_id = i;
4185
4186 return;
4187error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004188 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004189}
4190/*
4191 * init_counter()
4192 *
4193 * set cpu_id, core_num, pkg_num
4194 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4195 *
4196 * increment topo.num_cores when 1st core in pkg seen
4197 */
4198void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4199 struct pkg_data *pkg_base, int thread_num, int core_num,
4200 int pkg_num, int cpu_id)
4201{
4202 struct thread_data *t;
4203 struct core_data *c;
4204 struct pkg_data *p;
4205
4206 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4207 c = GET_CORE(core_base, core_num, pkg_num);
4208 p = GET_PKG(pkg_base, pkg_num);
4209
4210 t->cpu_id = cpu_id;
4211 if (thread_num == 0) {
4212 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4213 if (cpu_is_first_core_in_package(cpu_id))
4214 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4215 }
4216
4217 c->core_id = core_num;
4218 p->package_id = pkg_num;
4219}
4220
4221
4222int initialize_counters(int cpu_id)
4223{
4224 int my_thread_id, my_core_id, my_package_id;
4225
4226 my_package_id = get_physical_package_id(cpu_id);
4227 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004228 my_thread_id = get_cpu_position_in_core(cpu_id);
4229 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004230 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004231
4232 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4233 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4234 return 0;
4235}
4236
4237void allocate_output_buffer()
4238{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004239 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004240 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004241 if (outp == NULL)
4242 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004243}
Len Brown36229892016-02-26 20:51:02 -05004244void allocate_fd_percpu(void)
4245{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004246 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004247 if (fd_percpu == NULL)
4248 err(-1, "calloc fd_percpu");
4249}
Len Brown562a2d32016-02-26 23:48:05 -05004250void allocate_irq_buffers(void)
4251{
4252 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4253 if (irq_column_2_cpu == NULL)
4254 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004255
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004256 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004257 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004258 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004259}
Len Brownc98d5d92012-06-04 00:56:40 -04004260void setup_all_buffers(void)
4261{
4262 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004263 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004264 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004265 allocate_counters(&thread_even, &core_even, &package_even);
4266 allocate_counters(&thread_odd, &core_odd, &package_odd);
4267 allocate_output_buffer();
4268 for_all_proc_cpus(initialize_counters);
4269}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004270
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004271void set_base_cpu(void)
4272{
4273 base_cpu = sched_getcpu();
4274 if (base_cpu < 0)
4275 err(-ENODEV, "No valid cpus found");
4276
4277 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004278 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004279}
4280
Len Brown103a8fe2010-10-22 23:53:03 -04004281void turbostat_init()
4282{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004283 setup_all_buffers();
4284 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004285 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004286 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004287 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004288
Len Brown103a8fe2010-10-22 23:53:03 -04004289
Len Brown96e47152017-01-21 02:26:00 -05004290 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004291 for_all_cpus(print_hwp, ODD_COUNTERS);
4292
Len Brown96e47152017-01-21 02:26:00 -05004293 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004294 for_all_cpus(print_epb, ODD_COUNTERS);
4295
Len Brown96e47152017-01-21 02:26:00 -05004296 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004297 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4298
Len Brown96e47152017-01-21 02:26:00 -05004299 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004300 for_all_cpus(print_rapl, ODD_COUNTERS);
4301
4302 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4303
Len Brown96e47152017-01-21 02:26:00 -05004304 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004305 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004306
Len Brown96e47152017-01-21 02:26:00 -05004307 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004308 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004309}
4310
4311int fork_it(char **argv)
4312{
Len Brown103a8fe2010-10-22 23:53:03 -04004313 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004314 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004315
Len Brownd91bb172012-11-01 00:08:19 -04004316 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4317 if (status)
4318 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004319 /* clear affinity side-effect of get_counters() */
4320 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004321 gettimeofday(&tv_even, (struct timezone *)NULL);
4322
4323 child_pid = fork();
4324 if (!child_pid) {
4325 /* child */
4326 execvp(argv[0], argv);
4327 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004328
4329 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004330 if (child_pid == -1)
4331 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004332
4333 signal(SIGINT, SIG_IGN);
4334 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004335 if (waitpid(child_pid, &status, 0) == -1)
4336 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004337 }
Len Brownc98d5d92012-06-04 00:56:40 -04004338 /*
4339 * n.b. fork_it() does not check for errors from for_all_cpus()
4340 * because re-starting is problematic when forking
4341 */
4342 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004343 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004344 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004345 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4346 fprintf(outf, "%s: Counter reset detected\n", progname);
4347 else {
4348 compute_average(EVEN_COUNTERS);
4349 format_all_counters(EVEN_COUNTERS);
4350 }
Len Brown103a8fe2010-10-22 23:53:03 -04004351
Len Brownb7d8c142016-02-13 23:36:17 -05004352 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4353
4354 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004355
Len Brownd91bb172012-11-01 00:08:19 -04004356 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004357}
4358
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004359int get_and_dump_counters(void)
4360{
4361 int status;
4362
4363 status = for_all_cpus(get_counters, ODD_COUNTERS);
4364 if (status)
4365 return status;
4366
4367 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4368 if (status)
4369 return status;
4370
Len Brownb7d8c142016-02-13 23:36:17 -05004371 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004372
4373 return status;
4374}
4375
Len Brownd8af6f52015-02-10 01:56:38 -05004376void print_version() {
Len Brown0539ba12017-02-10 00:27:20 -05004377 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004378 " - Len Brown <lenb@kernel.org>\n");
4379}
4380
Len Brown495c76542017-02-08 02:41:51 -05004381int add_counter(unsigned int msr_num, char *path, char *name,
4382 unsigned int width, enum counter_scope scope,
4383 enum counter_type type, enum counter_format format)
Len Brown388e9c82016-12-22 23:57:55 -05004384{
4385 struct msr_counter *msrp;
4386
4387 msrp = calloc(1, sizeof(struct msr_counter));
4388 if (msrp == NULL) {
4389 perror("calloc");
4390 exit(1);
4391 }
4392
4393 msrp->msr_num = msr_num;
4394 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004395 if (path)
4396 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004397 msrp->width = width;
4398 msrp->type = type;
4399 msrp->format = format;
4400
4401 switch (scope) {
4402
4403 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004404 msrp->next = sys.tp;
4405 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004406 sys.added_thread_counters++;
4407 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4408 fprintf(stderr, "exceeded max %d added thread counters\n",
4409 MAX_ADDED_COUNTERS);
4410 exit(-1);
4411 }
Len Brown388e9c82016-12-22 23:57:55 -05004412 break;
4413
4414 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004415 msrp->next = sys.cp;
4416 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004417 sys.added_core_counters++;
4418 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4419 fprintf(stderr, "exceeded max %d added core counters\n",
4420 MAX_ADDED_COUNTERS);
4421 exit(-1);
4422 }
Len Brown388e9c82016-12-22 23:57:55 -05004423 break;
4424
4425 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004426 msrp->next = sys.pp;
4427 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004428 sys.added_package_counters++;
4429 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4430 fprintf(stderr, "exceeded max %d added package counters\n",
4431 MAX_ADDED_COUNTERS);
4432 exit(-1);
4433 }
Len Brown388e9c82016-12-22 23:57:55 -05004434 break;
4435 }
4436
4437 return 0;
4438}
4439
4440void parse_add_command(char *add_command)
4441{
4442 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004443 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004444 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004445 int width = 64;
4446 int fail = 0;
4447 enum counter_scope scope = SCOPE_CPU;
4448 enum counter_type type = COUNTER_CYCLES;
4449 enum counter_format format = FORMAT_DELTA;
4450
4451 while (add_command) {
4452
4453 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4454 goto next;
4455
4456 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4457 goto next;
4458
Len Brown495c76542017-02-08 02:41:51 -05004459 if (*add_command == '/') {
4460 path = add_command;
4461 goto next;
4462 }
4463
Len Brown388e9c82016-12-22 23:57:55 -05004464 if (sscanf(add_command, "u%d", &width) == 1) {
4465 if ((width == 32) || (width == 64))
4466 goto next;
4467 width = 64;
4468 }
4469 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4470 scope = SCOPE_CPU;
4471 goto next;
4472 }
4473 if (!strncmp(add_command, "core", strlen("core"))) {
4474 scope = SCOPE_CORE;
4475 goto next;
4476 }
4477 if (!strncmp(add_command, "package", strlen("package"))) {
4478 scope = SCOPE_PACKAGE;
4479 goto next;
4480 }
4481 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4482 type = COUNTER_CYCLES;
4483 goto next;
4484 }
4485 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4486 type = COUNTER_SECONDS;
4487 goto next;
4488 }
4489 if (!strncmp(add_command, "raw", strlen("raw"))) {
4490 format = FORMAT_RAW;
4491 goto next;
4492 }
4493 if (!strncmp(add_command, "delta", strlen("delta"))) {
4494 format = FORMAT_DELTA;
4495 goto next;
4496 }
4497 if (!strncmp(add_command, "percent", strlen("percent"))) {
4498 format = FORMAT_PERCENT;
4499 goto next;
4500 }
4501
4502 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4503 char *eos;
4504
4505 eos = strchr(name_buffer, ',');
4506 if (eos)
4507 *eos = '\0';
4508 goto next;
4509 }
4510
4511next:
4512 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004513 if (add_command) {
4514 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004515 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004516 }
Len Brown388e9c82016-12-22 23:57:55 -05004517
4518 }
Len Brown495c76542017-02-08 02:41:51 -05004519 if ((msr_num == 0) && (path == NULL)) {
4520 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004521 fail++;
4522 }
4523
4524 /* generate default column header */
4525 if (*name_buffer == '\0') {
4526 if (format == FORMAT_RAW) {
4527 if (width == 32)
4528 sprintf(name_buffer, "msr%d", msr_num);
4529 else
4530 sprintf(name_buffer, "MSR%d", msr_num);
4531 } else if (format == FORMAT_DELTA) {
4532 if (width == 32)
4533 sprintf(name_buffer, "cnt%d", msr_num);
4534 else
4535 sprintf(name_buffer, "CNT%d", msr_num);
4536 } else if (format == FORMAT_PERCENT) {
4537 if (width == 32)
4538 sprintf(name_buffer, "msr%d%%", msr_num);
4539 else
4540 sprintf(name_buffer, "MSR%d%%", msr_num);
4541 }
4542 }
4543
Len Brown495c76542017-02-08 02:41:51 -05004544 if (add_counter(msr_num, path, name_buffer, width, scope, type, format))
Len Brown388e9c82016-12-22 23:57:55 -05004545 fail++;
4546
4547 if (fail) {
4548 help();
4549 exit(1);
4550 }
4551}
Len Brown812db3f2017-02-10 00:25:41 -05004552/*
4553 * HIDE_LIST - hide this list of counters, show the rest [default]
4554 * SHOW_LIST - show this list of counters, hide the rest
4555 */
4556enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4557
4558int shown;
4559/*
4560 * parse_show_hide() - process cmdline to set default counter action
4561 */
4562void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4563{
4564 /*
4565 * --show: show only those specified
4566 * The 1st invocation will clear and replace the enabled mask
4567 * subsequent invocations can add to it.
4568 */
4569 if (new_mode == SHOW_LIST) {
4570 if (shown == 0)
4571 bic_enabled = bic_lookup(optarg);
4572 else
4573 bic_enabled |= bic_lookup(optarg);
4574 shown = 1;
4575
4576 return;
4577 }
4578
4579 /*
4580 * --hide: do not show those specified
4581 * multiple invocations simply clear more bits in enabled mask
4582 */
4583 bic_enabled &= ~bic_lookup(optarg);
4584}
4585
Len Brown103a8fe2010-10-22 23:53:03 -04004586void cmdline(int argc, char **argv)
4587{
4588 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004589 int option_index = 0;
4590 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004591 {"add", required_argument, 0, 'a'},
Len Brownd8af6f52015-02-10 01:56:38 -05004592 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05004593 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brownd8af6f52015-02-10 01:56:38 -05004594 {"interval", required_argument, 0, 'i'},
4595 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004596 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004597 {"Joules", no_argument, 0, 'J'},
Len Brownb7d8c142016-02-13 23:36:17 -05004598 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004599 {"Package", no_argument, 0, 'p'},
4600 {"processor", no_argument, 0, 'p'},
Len Brown96e47152017-01-21 02:26:00 -05004601 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05004602 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004603 {"Summary", no_argument, 0, 'S'},
4604 {"TCC", required_argument, 0, 'T'},
4605 {"version", no_argument, 0, 'v' },
4606 {0, 0, 0, 0 }
4607 };
Len Brown103a8fe2010-10-22 23:53:03 -04004608
4609 progname = argv[0];
4610
Len Brown96e47152017-01-21 02:26:00 -05004611 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpqST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004612 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004613 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004614 case 'a':
4615 parse_add_command(optarg);
4616 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004617 case 'D':
4618 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004619 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004620 case 'd':
4621 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004622 break;
Len Brown812db3f2017-02-10 00:25:41 -05004623 case 'H':
4624 parse_show_hide(optarg, HIDE_LIST);
4625 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004626 case 'h':
4627 default:
4628 help();
4629 exit(1);
4630 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004631 {
4632 double interval = strtod(optarg, NULL);
4633
4634 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004635 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004636 interval);
4637 exit(2);
4638 }
4639
4640 interval_ts.tv_sec = interval;
4641 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4642 }
Len Brown889facb2012-11-08 00:48:57 -05004643 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004644 case 'J':
4645 rapl_joules++;
4646 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004647 case 'o':
4648 outf = fopen_or_die(optarg, "w");
4649 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004650 case 'P':
4651 show_pkg_only++;
4652 break;
4653 case 'p':
4654 show_core_only++;
4655 break;
Len Brown96e47152017-01-21 02:26:00 -05004656 case 'q':
4657 quiet = 1;
4658 break;
Len Brown812db3f2017-02-10 00:25:41 -05004659 case 's':
4660 parse_show_hide(optarg, SHOW_LIST);
4661 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004662 case 'S':
4663 summary_only++;
4664 break;
4665 case 'T':
4666 tcc_activation_temp_override = atoi(optarg);
4667 break;
4668 case 'v':
4669 print_version();
4670 exit(0);
4671 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004672 }
4673 }
4674}
4675
4676int main(int argc, char **argv)
4677{
Len Brownb7d8c142016-02-13 23:36:17 -05004678 outf = stderr;
4679
Len Brown103a8fe2010-10-22 23:53:03 -04004680 cmdline(argc, argv);
4681
Len Brown96e47152017-01-21 02:26:00 -05004682 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05004683 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004684
4685 turbostat_init();
4686
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004687 /* dump counters and exit */
4688 if (dump_only)
4689 return get_and_dump_counters();
4690
Len Brown103a8fe2010-10-22 23:53:03 -04004691 /*
4692 * if any params left, it must be a command to fork
4693 */
4694 if (argc - optind)
4695 return fork_it(argv + optind);
4696 else
4697 turbostat_loop();
4698
4699 return 0;
4700}