blob: 851eaf06f3589b4ddfaca19b40de2a8a9e0abe81 [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 Brown0de6c0d2017-02-15 21:45:40 -050053unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050054unsigned int rapl_joules;
55unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050056unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050057unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040058unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070059unsigned int do_knl_cstates;
Len Brown0b2bb692015-03-26 00:50:30 -040060unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050061unsigned int do_slm_cstates;
62unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040063unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050064unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040065unsigned int do_irtl_snb;
66unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050067unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040068unsigned int genuine_intel;
69unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050070unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050071unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020072unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040073double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040074double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040075unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040076double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040077unsigned int show_pkg_only;
78unsigned int show_core_only;
79char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050080unsigned int do_rapl;
81unsigned int do_dts;
82unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050083unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050084unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050085unsigned int tcc_activation_temp;
86unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050087double rapl_power_units, rapl_time_units;
88double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050089double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040090unsigned int do_core_perf_limit_reasons;
91unsigned int do_gfx_perf_limit_reasons;
92unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040093unsigned int crystal_hz;
94unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040095int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040096double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050097unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
98 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
99unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
100unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
101unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
102unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500103unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500104
Len Browne6f9bb32013-12-03 02:19:19 -0500105#define RAPL_PKG (1 << 0)
106 /* 0x610 MSR_PKG_POWER_LIMIT */
107 /* 0x611 MSR_PKG_ENERGY_STATUS */
108#define RAPL_PKG_PERF_STATUS (1 << 1)
109 /* 0x613 MSR_PKG_PERF_STATUS */
110#define RAPL_PKG_POWER_INFO (1 << 2)
111 /* 0x614 MSR_PKG_POWER_INFO */
112
113#define RAPL_DRAM (1 << 3)
114 /* 0x618 MSR_DRAM_POWER_LIMIT */
115 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500116#define RAPL_DRAM_PERF_STATUS (1 << 4)
117 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400118#define RAPL_DRAM_POWER_INFO (1 << 5)
119 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500120
Jacob Pan91484942016-06-16 09:48:20 -0700121#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500122 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400123#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500124 /* 0x63a MSR_PP0_POLICY */
125
Len Brown0b2bb692015-03-26 00:50:30 -0400126#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500127 /* 0x640 MSR_PP1_POWER_LIMIT */
128 /* 0x641 MSR_PP1_ENERGY_STATUS */
129 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700130
131#define RAPL_CORES_ENERGY_STATUS (1 << 9)
132 /* 0x639 MSR_PP0_ENERGY_STATUS */
133#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500134#define TJMAX_DEFAULT 100
135
136#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400137
Len Brown388e9c82016-12-22 23:57:55 -0500138/*
139 * buffer size used by sscanf() for added column names
140 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
141 */
142#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500143#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500144
Len Brown103a8fe2010-10-22 23:53:03 -0400145int backwards_count;
146char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400147
Len Brown1ef7d212017-02-10 23:54:15 -0500148#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
149cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
150size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown678a3bd2017-02-09 22:22:13 -0500151#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400152
Len Brownc98d5d92012-06-04 00:56:40 -0400153struct thread_data {
154 unsigned long long tsc;
155 unsigned long long aperf;
156 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500157 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500158 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500159 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400160 unsigned int cpu_id;
161 unsigned int flags;
162#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
163#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500164 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400165} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400166
Len Brownc98d5d92012-06-04 00:56:40 -0400167struct core_data {
168 unsigned long long c3;
169 unsigned long long c6;
170 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500171 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500172 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400173 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500174 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400175} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400176
Len Brownc98d5d92012-06-04 00:56:40 -0400177struct pkg_data {
178 unsigned long long pc2;
179 unsigned long long pc3;
180 unsigned long long pc6;
181 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800182 unsigned long long pc8;
183 unsigned long long pc9;
184 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400185 unsigned long long pkg_wtd_core_c0;
186 unsigned long long pkg_any_core_c0;
187 unsigned long long pkg_any_gfxe_c0;
188 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400189 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500190 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400191 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500192 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
193 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
194 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
195 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
196 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
197 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
198 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500199 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400200} *package_even, *package_odd;
201
202#define ODD_COUNTERS thread_odd, core_odd, package_odd
203#define EVEN_COUNTERS thread_even, core_even, package_even
204
205#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
206 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
207 topo.num_threads_per_core + \
208 (core_no) * topo.num_threads_per_core + (thread_no))
209#define GET_CORE(core_base, core_no, pkg_no) \
210 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
211#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
212
Len Brown388e9c82016-12-22 23:57:55 -0500213enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500214enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500215enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
216
217struct msr_counter {
218 unsigned int msr_num;
219 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500220 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500221 unsigned int width;
222 enum counter_type type;
223 enum counter_format format;
224 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500225 unsigned int flags;
226#define FLAGS_HIDE (1 << 0)
227#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500228#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500229};
230
231struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500232 unsigned int added_thread_counters;
233 unsigned int added_core_counters;
234 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500235 struct msr_counter *tp;
236 struct msr_counter *cp;
237 struct msr_counter *pp;
238} sys;
239
Len Brownc98d5d92012-06-04 00:56:40 -0400240struct system_summary {
241 struct thread_data threads;
242 struct core_data cores;
243 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500244} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400245
246
247struct topo_params {
248 int num_packages;
249 int num_cpus;
250 int num_cores;
251 int max_cpu_num;
252 int num_cores_per_pkg;
253 int num_threads_per_core;
254} topo;
255
256struct timeval tv_even, tv_odd, tv_delta;
257
Len Brown562a2d32016-02-26 23:48:05 -0500258int *irq_column_2_cpu; /* /proc/interrupts column numbers */
259int *irqs_per_cpu; /* indexed by cpu_num */
260
Len Brownc98d5d92012-06-04 00:56:40 -0400261void setup_all_buffers(void);
262
263int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400264{
Len Brownc98d5d92012-06-04 00:56:40 -0400265 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400266}
Len Brown88c32812012-03-29 21:44:40 -0400267/*
Len Brownc98d5d92012-06-04 00:56:40 -0400268 * run func(thread, core, package) in topology order
269 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400270 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400271
Len Brownc98d5d92012-06-04 00:56:40 -0400272int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
273 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400274{
Len Brownc98d5d92012-06-04 00:56:40 -0400275 int retval, pkg_no, core_no, thread_no;
276
277 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
278 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
279 for (thread_no = 0; thread_no <
280 topo.num_threads_per_core; ++thread_no) {
281 struct thread_data *t;
282 struct core_data *c;
283 struct pkg_data *p;
284
285 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
286
287 if (cpu_is_not_present(t->cpu_id))
288 continue;
289
290 c = GET_CORE(core_base, core_no, pkg_no);
291 p = GET_PKG(pkg_base, pkg_no);
292
293 retval = func(t, c, p);
294 if (retval)
295 return retval;
296 }
297 }
298 }
299 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400300}
301
302int cpu_migrate(int cpu)
303{
Len Brownc98d5d92012-06-04 00:56:40 -0400304 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
305 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
306 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400307 return -1;
308 else
309 return 0;
310}
Len Brown36229892016-02-26 20:51:02 -0500311int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400312{
Len Brown103a8fe2010-10-22 23:53:03 -0400313 char pathname[32];
314 int fd;
315
Len Brown36229892016-02-26 20:51:02 -0500316 fd = fd_percpu[cpu];
317
318 if (fd)
319 return fd;
320
Len Brown103a8fe2010-10-22 23:53:03 -0400321 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
322 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400323 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400324 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 -0400325
Len Brown36229892016-02-26 20:51:02 -0500326 fd_percpu[cpu] = fd;
327
328 return fd;
329}
330
331int get_msr(int cpu, off_t offset, unsigned long long *msr)
332{
333 ssize_t retval;
334
335 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400336
Len Brown98481e72014-08-15 00:36:50 -0400337 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500338 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400339
340 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400341}
342
Len Brownfc04cc62014-02-06 00:55:19 -0500343/*
Len Brown812db3f2017-02-10 00:25:41 -0500344 * Each string in this array is compared in --show and --hide cmdline.
345 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500346 */
Len Brown812db3f2017-02-10 00:25:41 -0500347struct msr_counter bic[] = {
348 { 0x0, "Package" },
349 { 0x0, "Avg_MHz" },
350 { 0x0, "Bzy_MHz" },
351 { 0x0, "TSC_MHz" },
352 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500353 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500354 { 0x0, "Busy%" },
355 { 0x0, "CPU%c1" },
356 { 0x0, "CPU%c3" },
357 { 0x0, "CPU%c6" },
358 { 0x0, "CPU%c7" },
359 { 0x0, "ThreadC" },
360 { 0x0, "CoreTmp" },
361 { 0x0, "CoreCnt" },
362 { 0x0, "PkgTmp" },
363 { 0x0, "GFX%rc6" },
364 { 0x0, "GFXMHz" },
365 { 0x0, "Pkg%pc2" },
366 { 0x0, "Pkg%pc3" },
367 { 0x0, "Pkg%pc6" },
368 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500369 { 0x0, "Pkg%pc8" },
370 { 0x0, "Pkg%pc9" },
371 { 0x0, "Pkg%pc10" },
Len Brown812db3f2017-02-10 00:25:41 -0500372 { 0x0, "PkgWatt" },
373 { 0x0, "CorWatt" },
374 { 0x0, "GFXWatt" },
375 { 0x0, "PkgCnt" },
376 { 0x0, "RAMWatt" },
377 { 0x0, "PKG_%" },
378 { 0x0, "RAM_%" },
379 { 0x0, "Pkg_J" },
380 { 0x0, "Cor_J" },
381 { 0x0, "GFX_J" },
382 { 0x0, "RAM_J" },
383 { 0x0, "Core" },
384 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500385 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500386 { 0x0, "sysfs" },
Len Brown812db3f2017-02-10 00:25:41 -0500387};
388
389#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
390#define BIC_Package (1ULL << 0)
391#define BIC_Avg_MHz (1ULL << 1)
392#define BIC_Bzy_MHz (1ULL << 2)
393#define BIC_TSC_MHz (1ULL << 3)
394#define BIC_IRQ (1ULL << 4)
395#define BIC_SMI (1ULL << 5)
396#define BIC_Busy (1ULL << 6)
397#define BIC_CPU_c1 (1ULL << 7)
398#define BIC_CPU_c3 (1ULL << 8)
399#define BIC_CPU_c6 (1ULL << 9)
400#define BIC_CPU_c7 (1ULL << 10)
401#define BIC_ThreadC (1ULL << 11)
402#define BIC_CoreTmp (1ULL << 12)
403#define BIC_CoreCnt (1ULL << 13)
404#define BIC_PkgTmp (1ULL << 14)
405#define BIC_GFX_rc6 (1ULL << 15)
406#define BIC_GFXMHz (1ULL << 16)
407#define BIC_Pkgpc2 (1ULL << 17)
408#define BIC_Pkgpc3 (1ULL << 18)
409#define BIC_Pkgpc6 (1ULL << 19)
410#define BIC_Pkgpc7 (1ULL << 20)
Len Brown0f47c082017-01-27 00:50:45 -0500411#define BIC_Pkgpc8 (1ULL << 21)
412#define BIC_Pkgpc9 (1ULL << 22)
413#define BIC_Pkgpc10 (1ULL << 23)
414#define BIC_PkgWatt (1ULL << 24)
415#define BIC_CorWatt (1ULL << 25)
416#define BIC_GFXWatt (1ULL << 26)
417#define BIC_PkgCnt (1ULL << 27)
418#define BIC_RAMWatt (1ULL << 28)
419#define BIC_PKG__ (1ULL << 29)
420#define BIC_RAM__ (1ULL << 30)
421#define BIC_Pkg_J (1ULL << 31)
422#define BIC_Cor_J (1ULL << 32)
423#define BIC_GFX_J (1ULL << 33)
424#define BIC_RAM_J (1ULL << 34)
425#define BIC_Core (1ULL << 35)
426#define BIC_CPU (1ULL << 36)
427#define BIC_Mod_c6 (1ULL << 37)
Len Brown41618e62017-02-09 18:25:22 -0500428#define BIC_sysfs (1ULL << 38)
Len Brown812db3f2017-02-10 00:25:41 -0500429
430unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
Len Brown41618e62017-02-09 18:25:22 -0500431unsigned long long bic_present = BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500432
433#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
434#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500435#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500436
437/*
438 * bic_lookup
439 * for all the strings in comma separate name_list,
440 * set the approprate bit in return value.
441 */
442unsigned long long bic_lookup(char *name_list)
443{
444 int i;
445 unsigned long long retval = 0;
446
447 while (name_list) {
448 char *comma;
449
450 comma = strchr(name_list, ',');
451
452 if (comma)
453 *comma = '\0';
454
455 for (i = 0; i < MAX_BIC; ++i) {
456 if (!strcmp(name_list, bic[i].name)) {
457 retval |= (1ULL << i);
458 break;
459 }
460 }
461 if (i == MAX_BIC) {
462 fprintf(stderr, "Invalid counter name: %s\n", name_list);
463 exit(-1);
464 }
465
466 name_list = comma;
467 if (name_list)
468 name_list++;
469
470 }
471 return retval;
472}
Len Brownfc04cc62014-02-06 00:55:19 -0500473
Len Brownc8ade362017-02-15 17:15:11 -0500474void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400475{
Len Brown388e9c82016-12-22 23:57:55 -0500476 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500477 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500478
Len Brown812db3f2017-02-10 00:25:41 -0500479 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500480 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500481 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500482 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500483 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500484 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500485 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500486 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500487 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500488 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500489 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500490 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500491 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500492 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500493
Len Brown0de6c0d2017-02-15 21:45:40 -0500494 if (DO_BIC(BIC_IRQ)) {
495 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500496 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500497 else
Len Brown6168c2e2017-02-16 23:07:51 -0500498 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500499 }
500
Len Brown812db3f2017-02-10 00:25:41 -0500501 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500502 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500503
Len Brown388e9c82016-12-22 23:57:55 -0500504 for (mp = sys.tp; mp; mp = mp->next) {
Len Brown6168c2e2017-02-16 23:07:51 -0500505 if (*delim == ',') {
506 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), "sysfs");
507 break;
508 }
Len Brown388e9c82016-12-22 23:57:55 -0500509 if (mp->format == FORMAT_RAW) {
510 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500511 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500512 else
Len Brownc8ade362017-02-15 17:15:11 -0500513 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500514 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500515 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
516 outp += sprintf(outp, "%s%8s", delim, mp->name);
517 else
518 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500519 }
520 }
521
Len Brown41618e62017-02-09 18:25:22 -0500522 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500523 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500524 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500525 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500526 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500527 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500528 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500529 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500530
Len Brown0539ba12017-02-10 00:27:20 -0500531 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500532 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500533
Len Brown812db3f2017-02-10 00:25:41 -0500534 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500535 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500536
537 for (mp = sys.cp; mp; mp = mp->next) {
538 if (mp->format == FORMAT_RAW) {
539 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500540 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500541 else
Len Brownc8ade362017-02-15 17:15:11 -0500542 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500543 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500544 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
545 outp += sprintf(outp, "%s%8s", delim, mp->name);
546 else
547 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500548 }
549 }
550
Len Brown812db3f2017-02-10 00:25:41 -0500551 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500552 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500553
Len Brown812db3f2017-02-10 00:25:41 -0500554 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500555 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500556
Len Brown812db3f2017-02-10 00:25:41 -0500557 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500558 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500559
Len Brown0b2bb692015-03-26 00:50:30 -0400560 if (do_skl_residency) {
Len Brown6168c2e2017-02-16 23:07:51 -0500561 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
562 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
563 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
564 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400565 }
566
Len Brown0f47c082017-01-27 00:50:45 -0500567 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500568 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500569 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500570 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500571 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500572 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500573 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500574 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500575 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500576 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500577 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500578 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500579 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500580 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400581
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800582 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500583 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500584 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500585 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500586 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500587 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500588 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500589 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500590 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500591 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500592 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500593 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500594 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500595 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500596 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500597 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500598 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500599 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500600 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500601 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500602 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500603 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500604 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500605 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500606 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500607 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800608 }
Len Brown388e9c82016-12-22 23:57:55 -0500609 for (mp = sys.pp; mp; mp = mp->next) {
610 if (mp->format == FORMAT_RAW) {
611 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500612 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500613 else
Len Brownc8ade362017-02-15 17:15:11 -0500614 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500615 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500616 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
617 outp += sprintf(outp, "%s%8s", delim, mp->name);
618 else
619 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500620 }
621 }
622
Len Brownc98d5d92012-06-04 00:56:40 -0400623 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400624}
625
Len Brownc98d5d92012-06-04 00:56:40 -0400626int dump_counters(struct thread_data *t, struct core_data *c,
627 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400628{
Len Brown388e9c82016-12-22 23:57:55 -0500629 int i;
630 struct msr_counter *mp;
631
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200632 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400633
Len Brownc98d5d92012-06-04 00:56:40 -0400634 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200635 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
636 t->cpu_id, t->flags);
637 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
638 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
639 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
640 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500641
Len Brown812db3f2017-02-10 00:25:41 -0500642 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500643 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500644 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500645 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500646
647 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
648 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
649 i, mp->msr_num, t->counter[i]);
650 }
Len Brownc98d5d92012-06-04 00:56:40 -0400651 }
Len Brown103a8fe2010-10-22 23:53:03 -0400652
Len Brownc98d5d92012-06-04 00:56:40 -0400653 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200654 outp += sprintf(outp, "core: %d\n", c->core_id);
655 outp += sprintf(outp, "c3: %016llX\n", c->c3);
656 outp += sprintf(outp, "c6: %016llX\n", c->c6);
657 outp += sprintf(outp, "c7: %016llX\n", c->c7);
658 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500659
660 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
661 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
662 i, mp->msr_num, c->counter[i]);
663 }
Len Brown0539ba12017-02-10 00:27:20 -0500664 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400665 }
666
667 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200668 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400669
670 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
671 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
672 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
673 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
674
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200675 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500676 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500677 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500678 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500679 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500680 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500681 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200682 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
683 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
684 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
685 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
686 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
687 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
688 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
689 outp += sprintf(outp, "Throttle PKG: %0X\n",
690 p->rapl_pkg_perf_status);
691 outp += sprintf(outp, "Throttle RAM: %0X\n",
692 p->rapl_dram_perf_status);
693 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500694
695 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
696 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
697 i, mp->msr_num, p->counter[i]);
698 }
Len Brownc98d5d92012-06-04 00:56:40 -0400699 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200700
701 outp += sprintf(outp, "\n");
702
Len Brownc98d5d92012-06-04 00:56:40 -0400703 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400704}
705
Len Browne23da032012-02-06 18:37:16 -0500706/*
707 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500708 */
Len Brownc98d5d92012-06-04 00:56:40 -0400709int format_counters(struct thread_data *t, struct core_data *c,
710 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400711{
Len Brown008d396e2017-02-10 00:29:51 -0500712 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500713 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500714 int i;
715 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500716 char *delim = "\t";
717 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400718
Len Brownc98d5d92012-06-04 00:56:40 -0400719 /* if showing only 1st thread in core and this isn't one, bail out */
720 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
721 return 0;
722
723 /* if showing only 1st thread in pkg and this isn't one, bail out */
724 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
725 return 0;
726
Len Brown1ef7d212017-02-10 23:54:15 -0500727 /*if not summary line and --cpu is used */
728 if ((t != &average.threads) &&
729 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
730 return 0;
731
Len Brown103a8fe2010-10-22 23:53:03 -0400732 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
733
Len Brown008d396e2017-02-10 00:29:51 -0500734 tsc = t->tsc * tsc_tweak;
735
Len Brownc98d5d92012-06-04 00:56:40 -0400736 /* topo columns, print blanks on 1st (average) line */
737 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500738 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500739 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500740 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500741 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500742 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500743 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400744 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500745 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400746 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500747 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400748 else
Len Brown6168c2e2017-02-16 23:07:51 -0500749 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400750 }
Len Brown812db3f2017-02-10 00:25:41 -0500751 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400752 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500753 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400754 else
Len Brown6168c2e2017-02-16 23:07:51 -0500755 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400756 }
Len Brown812db3f2017-02-10 00:25:41 -0500757 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500758 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400759 }
Len Brownfc04cc62014-02-06 00:55:19 -0500760
Len Brown812db3f2017-02-10 00:25:41 -0500761 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500762 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500763 1.0 / units * t->aperf / interval_float);
764
Len Brown812db3f2017-02-10 00:25:41 -0500765 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500766 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400767
Len Brown812db3f2017-02-10 00:25:41 -0500768 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400769 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500770 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400771 else
Len Brown6168c2e2017-02-16 23:07:51 -0500772 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500773 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400774 }
Len Brown103a8fe2010-10-22 23:53:03 -0400775
Len Brown812db3f2017-02-10 00:25:41 -0500776 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500777 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400778
Len Brown562a2d32016-02-26 23:48:05 -0500779 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500780 if (DO_BIC(BIC_IRQ)) {
781 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500782 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500783 else
Len Brown6168c2e2017-02-16 23:07:51 -0500784 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500785 }
Len Brown562a2d32016-02-26 23:48:05 -0500786
Len Brown1cc21f72015-02-23 00:34:57 -0500787 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500788 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500789 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500790
Len Brown678a3bd2017-02-09 22:22:13 -0500791 /* Added counters */
792 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
793 if (mp->format == FORMAT_RAW) {
794 if (mp->width == 32)
Len Brown6168c2e2017-02-16 23:07:51 -0500795 outp += sprintf(outp, "%s0x%08lx", (printed++ ? delim : ""), (unsigned long) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500796 else
Len Brown6168c2e2017-02-16 23:07:51 -0500797 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500798 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500799 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500800 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500801 else
Len Brown6168c2e2017-02-16 23:07:51 -0500802 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500803 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500804 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500805 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500806 else
Len Brown6168c2e2017-02-16 23:07:51 -0500807 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500808 }
809 }
810
Len Brown41618e62017-02-09 18:25:22 -0500811 /* C1 */
812 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500813 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500814
815
Len Brownc98d5d92012-06-04 00:56:40 -0400816 /* print per-core data only for 1st thread in core */
817 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
818 goto done;
819
Len Brown812db3f2017-02-10 00:25:41 -0500820 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500821 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500822 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500823 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500824 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500825 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400826
Len Brown0539ba12017-02-10 00:27:20 -0500827 /* Mod%c6 */
828 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500829 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500830
Len Brown812db3f2017-02-10 00:25:41 -0500831 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500832 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500833
Len Brown388e9c82016-12-22 23:57:55 -0500834 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
835 if (mp->format == FORMAT_RAW) {
836 if (mp->width == 32)
Len Brown6168c2e2017-02-16 23:07:51 -0500837 outp += sprintf(outp, "%s0x%08lx", (printed++ ? delim : ""), (unsigned long) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500838 else
Len Brown6168c2e2017-02-16 23:07:51 -0500839 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500840 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500841 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500842 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500843 else
Len Brown6168c2e2017-02-16 23:07:51 -0500844 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500845 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500846 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500847 }
848 }
849
Len Brownc98d5d92012-06-04 00:56:40 -0400850 /* print per-package data only for 1st core in package */
851 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
852 goto done;
853
Len Brown0b2bb692015-03-26 00:50:30 -0400854 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500855 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500856 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500857
Len Brownfdf676e2016-02-27 01:28:12 -0500858 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500859 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400860 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500861 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400862 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500863 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400864 p->gfx_rc6_ms / 10.0 / interval_float);
865 }
866 }
Len Brownfdf676e2016-02-27 01:28:12 -0500867
Len Brown27d47352016-02-27 00:37:54 -0500868 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500869 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500870 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500871
Len Brown0b2bb692015-03-26 00:50:30 -0400872 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
873 if (do_skl_residency) {
Len Brown6168c2e2017-02-16 23:07:51 -0500874 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
875 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
876 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
877 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400878 }
879
Len Brown0f47c082017-01-27 00:50:45 -0500880 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500881 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500882 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500883 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500884 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500885 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500886 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500887 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500888 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500889 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500890 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500891 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500892 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500893 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500894
895 /*
896 * If measurement interval exceeds minimum RAPL Joule Counter range,
897 * indicate that results are suspect by printing "**" in fraction place.
898 */
Len Brownfc04cc62014-02-06 00:55:19 -0500899 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -0500900 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500901 else
Len Browne975db52016-04-06 23:56:02 -0400902 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500903
Len Brown812db3f2017-02-10 00:25:41 -0500904 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500905 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500906 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500907 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500908 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500909 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500910 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500911 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500912 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500913 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500914 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500915 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500916 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500917 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500918 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500919 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500920 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500921 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500922 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500923 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500924
Len Brown388e9c82016-12-22 23:57:55 -0500925 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
926 if (mp->format == FORMAT_RAW) {
927 if (mp->width == 32)
Len Brown6168c2e2017-02-16 23:07:51 -0500928 outp += sprintf(outp, "%s0x%08lx", (printed++ ? delim : ""), (unsigned long) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500929 else
Len Brown6168c2e2017-02-16 23:07:51 -0500930 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500931 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500932 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500933 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500934 else
Len Brown6168c2e2017-02-16 23:07:51 -0500935 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500936 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500937 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500938 }
939 }
940
Len Brownc98d5d92012-06-04 00:56:40 -0400941done:
Len Brownc98d5d92012-06-04 00:56:40 -0400942 outp += sprintf(outp, "\n");
943
944 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400945}
946
Len Brownb7d8c142016-02-13 23:36:17 -0500947void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400948{
Len Brownb7d8c142016-02-13 23:36:17 -0500949 FILE *filep;
950
951 if (outf == stderr)
952 filep = stdout;
953 else
954 filep = outf;
955
956 fputs(output_buffer, filep);
957 fflush(filep);
958
Len Brownc98d5d92012-06-04 00:56:40 -0400959 outp = output_buffer;
960}
Len Brownb7d8c142016-02-13 23:36:17 -0500961void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400962{
Len Brownb7d8c142016-02-13 23:36:17 -0500963 fputs(output_buffer, outf);
964 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400965 outp = output_buffer;
966}
967void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
968{
Len Browne23da032012-02-06 18:37:16 -0500969 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400970
Len Browne23da032012-02-06 18:37:16 -0500971 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -0500972 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -0400973
Len Brownc98d5d92012-06-04 00:56:40 -0400974 if (topo.num_cpus > 1)
975 format_counters(&average.threads, &average.cores,
976 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400977
Len Browne23da032012-02-06 18:37:16 -0500978 printed = 1;
979
980 if (summary_only)
981 return;
982
Len Brownc98d5d92012-06-04 00:56:40 -0400983 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400984}
985
Len Brown889facb2012-11-08 00:48:57 -0500986#define DELTA_WRAP32(new, old) \
987 if (new > old) { \
988 old = new - old; \
989 } else { \
990 old = 0x100000000 + new - old; \
991 }
992
Len Brownba3dec92016-04-22 20:31:46 -0400993int
Len Brownc98d5d92012-06-04 00:56:40 -0400994delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400995{
Len Brown388e9c82016-12-22 23:57:55 -0500996 int i;
997 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400998
999 if (do_skl_residency) {
1000 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1001 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1002 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1003 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1004 }
Len Brownc98d5d92012-06-04 00:56:40 -04001005 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001006 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001007 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001008 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001009 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001010 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001011 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001012 old->pc8 = new->pc8 - old->pc8;
1013 old->pc9 = new->pc9 - old->pc9;
1014 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -05001015 old->pkg_temp_c = new->pkg_temp_c;
1016
Len Brown9185e982016-04-06 17:16:00 -04001017 /* flag an error when rc6 counter resets/wraps */
1018 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1019 old->gfx_rc6_ms = -1;
1020 else
1021 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1022
Len Brown27d47352016-02-27 00:37:54 -05001023 old->gfx_mhz = new->gfx_mhz;
1024
Len Brown889facb2012-11-08 00:48:57 -05001025 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1026 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1027 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1028 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1029 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1030 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001031
Len Brown388e9c82016-12-22 23:57:55 -05001032 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1033 if (mp->format == FORMAT_RAW)
1034 old->counter[i] = new->counter[i];
1035 else
1036 old->counter[i] = new->counter[i] - old->counter[i];
1037 }
1038
Len Brownba3dec92016-04-22 20:31:46 -04001039 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001040}
Len Brown103a8fe2010-10-22 23:53:03 -04001041
Len Brownc98d5d92012-06-04 00:56:40 -04001042void
1043delta_core(struct core_data *new, struct core_data *old)
1044{
Len Brown388e9c82016-12-22 23:57:55 -05001045 int i;
1046 struct msr_counter *mp;
1047
Len Brownc98d5d92012-06-04 00:56:40 -04001048 old->c3 = new->c3 - old->c3;
1049 old->c6 = new->c6 - old->c6;
1050 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001051 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001052 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001053
1054 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1055 if (mp->format == FORMAT_RAW)
1056 old->counter[i] = new->counter[i];
1057 else
1058 old->counter[i] = new->counter[i] - old->counter[i];
1059 }
Len Brownc98d5d92012-06-04 00:56:40 -04001060}
Len Brown103a8fe2010-10-22 23:53:03 -04001061
Len Brownc3ae3312012-06-13 21:31:46 -04001062/*
1063 * old = new - old
1064 */
Len Brownba3dec92016-04-22 20:31:46 -04001065int
Len Brownc98d5d92012-06-04 00:56:40 -04001066delta_thread(struct thread_data *new, struct thread_data *old,
1067 struct core_data *core_delta)
1068{
Len Brown388e9c82016-12-22 23:57:55 -05001069 int i;
1070 struct msr_counter *mp;
1071
Len Brownc98d5d92012-06-04 00:56:40 -04001072 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001073
Len Brownc98d5d92012-06-04 00:56:40 -04001074 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001075 if (old->tsc < (1000 * 1000))
1076 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1077 "You can disable all c-states by booting with \"idle=poll\"\n"
1078 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001079
Len Brownc98d5d92012-06-04 00:56:40 -04001080 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001081
Len Brown812db3f2017-02-10 00:25:41 -05001082 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001083 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1084 old->aperf = new->aperf - old->aperf;
1085 old->mperf = new->mperf - old->mperf;
1086 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001087 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001088 }
Len Brownc98d5d92012-06-04 00:56:40 -04001089 }
Len Brown103a8fe2010-10-22 23:53:03 -04001090
Len Brown103a8fe2010-10-22 23:53:03 -04001091
Len Brown144b44b2013-11-09 00:30:16 -05001092 if (use_c1_residency_msr) {
1093 /*
1094 * Some models have a dedicated C1 residency MSR,
1095 * which should be more accurate than the derivation below.
1096 */
1097 } else {
1098 /*
1099 * As counter collection is not atomic,
1100 * it is possible for mperf's non-halted cycles + idle states
1101 * to exceed TSC's all cycles: show c1 = 0% in that case.
1102 */
1103 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1104 old->c1 = 0;
1105 else {
1106 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001107 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001108 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001109 }
Len Brownc98d5d92012-06-04 00:56:40 -04001110 }
Len Brownc3ae3312012-06-13 21:31:46 -04001111
Len Brownc98d5d92012-06-04 00:56:40 -04001112 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001113 if (debug > 1)
1114 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001115 old->mperf = 1; /* divide by 0 protection */
1116 }
1117
Len Brown812db3f2017-02-10 00:25:41 -05001118 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001119 old->irq_count = new->irq_count - old->irq_count;
1120
Len Brown812db3f2017-02-10 00:25:41 -05001121 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001122 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001123
Len Brown388e9c82016-12-22 23:57:55 -05001124 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1125 if (mp->format == FORMAT_RAW)
1126 old->counter[i] = new->counter[i];
1127 else
1128 old->counter[i] = new->counter[i] - old->counter[i];
1129 }
Len Brownba3dec92016-04-22 20:31:46 -04001130 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001131}
1132
1133int delta_cpu(struct thread_data *t, struct core_data *c,
1134 struct pkg_data *p, struct thread_data *t2,
1135 struct core_data *c2, struct pkg_data *p2)
1136{
Len Brownba3dec92016-04-22 20:31:46 -04001137 int retval = 0;
1138
Len Brownc98d5d92012-06-04 00:56:40 -04001139 /* calculate core delta only for 1st thread in core */
1140 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1141 delta_core(c, c2);
1142
1143 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001144 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1145 if (retval)
1146 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001147
1148 /* calculate package delta only for 1st core in package */
1149 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001150 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001151
Len Brownba3dec92016-04-22 20:31:46 -04001152 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001153}
1154
1155void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1156{
Len Brown388e9c82016-12-22 23:57:55 -05001157 int i;
1158 struct msr_counter *mp;
1159
Len Brownc98d5d92012-06-04 00:56:40 -04001160 t->tsc = 0;
1161 t->aperf = 0;
1162 t->mperf = 0;
1163 t->c1 = 0;
1164
Len Brown562a2d32016-02-26 23:48:05 -05001165 t->irq_count = 0;
1166 t->smi_count = 0;
1167
Len Brownc98d5d92012-06-04 00:56:40 -04001168 /* tells format_counters to dump all fields from this set */
1169 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1170
1171 c->c3 = 0;
1172 c->c6 = 0;
1173 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001174 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001175 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001176
Len Brown0b2bb692015-03-26 00:50:30 -04001177 p->pkg_wtd_core_c0 = 0;
1178 p->pkg_any_core_c0 = 0;
1179 p->pkg_any_gfxe_c0 = 0;
1180 p->pkg_both_core_gfxe_c0 = 0;
1181
Len Brownc98d5d92012-06-04 00:56:40 -04001182 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001183 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001184 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001185 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001186 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001187 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001188 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001189 p->pc8 = 0;
1190 p->pc9 = 0;
1191 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001192
1193 p->energy_pkg = 0;
1194 p->energy_dram = 0;
1195 p->energy_cores = 0;
1196 p->energy_gfx = 0;
1197 p->rapl_pkg_perf_status = 0;
1198 p->rapl_dram_perf_status = 0;
1199 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001200
Len Brownfdf676e2016-02-27 01:28:12 -05001201 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001202 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001203 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1204 t->counter[i] = 0;
1205
1206 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1207 c->counter[i] = 0;
1208
1209 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1210 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001211}
1212int sum_counters(struct thread_data *t, struct core_data *c,
1213 struct pkg_data *p)
1214{
Len Brown388e9c82016-12-22 23:57:55 -05001215 int i;
1216 struct msr_counter *mp;
1217
Len Brownc98d5d92012-06-04 00:56:40 -04001218 average.threads.tsc += t->tsc;
1219 average.threads.aperf += t->aperf;
1220 average.threads.mperf += t->mperf;
1221 average.threads.c1 += t->c1;
1222
Len Brown562a2d32016-02-26 23:48:05 -05001223 average.threads.irq_count += t->irq_count;
1224 average.threads.smi_count += t->smi_count;
1225
Len Brown388e9c82016-12-22 23:57:55 -05001226 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1227 if (mp->format == FORMAT_RAW)
1228 continue;
1229 average.threads.counter[i] += t->counter[i];
1230 }
1231
Len Brownc98d5d92012-06-04 00:56:40 -04001232 /* sum per-core values only for 1st thread in core */
1233 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1234 return 0;
1235
1236 average.cores.c3 += c->c3;
1237 average.cores.c6 += c->c6;
1238 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001239 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001240
Len Brown889facb2012-11-08 00:48:57 -05001241 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1242
Len Brown388e9c82016-12-22 23:57:55 -05001243 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1244 if (mp->format == FORMAT_RAW)
1245 continue;
1246 average.cores.counter[i] += c->counter[i];
1247 }
1248
Len Brownc98d5d92012-06-04 00:56:40 -04001249 /* sum per-pkg values only for 1st core in pkg */
1250 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1251 return 0;
1252
Len Brown0b2bb692015-03-26 00:50:30 -04001253 if (do_skl_residency) {
1254 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1255 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1256 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1257 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1258 }
1259
Len Brownc98d5d92012-06-04 00:56:40 -04001260 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001261 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001262 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001263 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001264 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001265 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001266 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001267 average.packages.pc8 += p->pc8;
1268 average.packages.pc9 += p->pc9;
1269 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001270
Len Brown889facb2012-11-08 00:48:57 -05001271 average.packages.energy_pkg += p->energy_pkg;
1272 average.packages.energy_dram += p->energy_dram;
1273 average.packages.energy_cores += p->energy_cores;
1274 average.packages.energy_gfx += p->energy_gfx;
1275
Len Brownfdf676e2016-02-27 01:28:12 -05001276 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001277 average.packages.gfx_mhz = p->gfx_mhz;
1278
Len Brown889facb2012-11-08 00:48:57 -05001279 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1280
1281 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1282 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001283
1284 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1285 if (mp->format == FORMAT_RAW)
1286 continue;
1287 average.packages.counter[i] += p->counter[i];
1288 }
Len Brownc98d5d92012-06-04 00:56:40 -04001289 return 0;
1290}
1291/*
1292 * sum the counters for all cpus in the system
1293 * compute the weighted average
1294 */
1295void compute_average(struct thread_data *t, struct core_data *c,
1296 struct pkg_data *p)
1297{
Len Brown388e9c82016-12-22 23:57:55 -05001298 int i;
1299 struct msr_counter *mp;
1300
Len Brownc98d5d92012-06-04 00:56:40 -04001301 clear_counters(&average.threads, &average.cores, &average.packages);
1302
1303 for_all_cpus(sum_counters, t, c, p);
1304
1305 average.threads.tsc /= topo.num_cpus;
1306 average.threads.aperf /= topo.num_cpus;
1307 average.threads.mperf /= topo.num_cpus;
1308 average.threads.c1 /= topo.num_cpus;
1309
Len Brown0de6c0d2017-02-15 21:45:40 -05001310 if (average.threads.irq_count > 9999999)
1311 sums_need_wide_columns = 1;
1312
Len Brownc98d5d92012-06-04 00:56:40 -04001313 average.cores.c3 /= topo.num_cores;
1314 average.cores.c6 /= topo.num_cores;
1315 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001316 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001317
Len Brown0b2bb692015-03-26 00:50:30 -04001318 if (do_skl_residency) {
1319 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1320 average.packages.pkg_any_core_c0 /= topo.num_packages;
1321 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1322 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1323 }
1324
Len Brownc98d5d92012-06-04 00:56:40 -04001325 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001326 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001327 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001328 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001329 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001330 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001331 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001332
1333 average.packages.pc8 /= topo.num_packages;
1334 average.packages.pc9 /= topo.num_packages;
1335 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001336
1337 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1338 if (mp->format == FORMAT_RAW)
1339 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001340 if (mp->type == COUNTER_ITEMS) {
1341 if (average.threads.counter[i] > 9999999)
1342 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001343 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001344 }
Len Brown388e9c82016-12-22 23:57:55 -05001345 average.threads.counter[i] /= topo.num_cpus;
1346 }
1347 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1348 if (mp->format == FORMAT_RAW)
1349 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001350 if (mp->type == COUNTER_ITEMS) {
1351 if (average.cores.counter[i] > 9999999)
1352 sums_need_wide_columns = 1;
1353 }
Len Brown388e9c82016-12-22 23:57:55 -05001354 average.cores.counter[i] /= topo.num_cores;
1355 }
1356 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1357 if (mp->format == FORMAT_RAW)
1358 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001359 if (mp->type == COUNTER_ITEMS) {
1360 if (average.packages.counter[i] > 9999999)
1361 sums_need_wide_columns = 1;
1362 }
Len Brown388e9c82016-12-22 23:57:55 -05001363 average.packages.counter[i] /= topo.num_packages;
1364 }
Len Brownc98d5d92012-06-04 00:56:40 -04001365}
1366
1367static unsigned long long rdtsc(void)
1368{
1369 unsigned int low, high;
1370
1371 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1372
1373 return low | ((unsigned long long)high) << 32;
1374}
1375
Len Brownc98d5d92012-06-04 00:56:40 -04001376/*
Len Brown495c76542017-02-08 02:41:51 -05001377 * Open a file, and exit on failure
1378 */
1379FILE *fopen_or_die(const char *path, const char *mode)
1380{
1381 FILE *filep = fopen(path, mode);
1382
1383 if (!filep)
1384 err(1, "%s: open failed", path);
1385 return filep;
1386}
1387/*
1388 * snapshot_sysfs_counter()
1389 *
1390 * return snapshot of given counter
1391 */
1392unsigned long long snapshot_sysfs_counter(char *path)
1393{
1394 FILE *fp;
1395 int retval;
1396 unsigned long long counter;
1397
1398 fp = fopen_or_die(path, "r");
1399
1400 retval = fscanf(fp, "%lld", &counter);
1401 if (retval != 1)
1402 err(1, "snapshot_sysfs_counter(%s)", path);
1403
1404 fclose(fp);
1405
1406 return counter;
1407}
1408
1409int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1410{
1411 if (mp->msr_num != 0) {
1412 if (get_msr(cpu, mp->msr_num, counterp))
1413 return -1;
1414 } else {
Len Brown41618e62017-02-09 18:25:22 -05001415 char path[128];
1416
1417 if (mp->flags & SYSFS_PERCPU) {
1418 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1419 cpu, mp->path);
1420
1421 *counterp = snapshot_sysfs_counter(path);
1422 } else {
1423 *counterp = snapshot_sysfs_counter(mp->path);
1424 }
Len Brown495c76542017-02-08 02:41:51 -05001425 }
1426
1427 return 0;
1428}
1429
1430/*
Len Brownc98d5d92012-06-04 00:56:40 -04001431 * get_counters(...)
1432 * migrate to cpu
1433 * acquire and record local counters for that cpu
1434 */
1435int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1436{
1437 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001438 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001439 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001440 struct msr_counter *mp;
1441 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001442
Len Browne52966c2012-11-08 22:38:05 -05001443 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001444 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001445 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001446 }
Len Brownc98d5d92012-06-04 00:56:40 -04001447
Len Brown0102b062016-02-27 03:11:29 -05001448retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001449 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1450
Len Brown812db3f2017-02-10 00:25:41 -05001451 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001452 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1453
1454 /*
1455 * The TSC, APERF and MPERF must be read together for
1456 * APERF/MPERF and MPERF/TSC to give accurate results.
1457 *
1458 * Unfortunately, APERF and MPERF are read by
1459 * individual system call, so delays may occur
1460 * between them. If the time to read them
1461 * varies by a large amount, we re-read them.
1462 */
1463
1464 /*
1465 * This initial dummy APERF read has been seen to
1466 * reduce jitter in the subsequent reads.
1467 */
1468
Len Brown9c63a652012-10-31 01:29:52 -04001469 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001470 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001471
1472 t->tsc = rdtsc(); /* re-read close to APERF */
1473
1474 tsc_before = t->tsc;
1475
1476 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1477 return -3;
1478
1479 tsc_between = rdtsc();
1480
Len Brown9c63a652012-10-31 01:29:52 -04001481 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001482 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001483
1484 tsc_after = rdtsc();
1485
1486 aperf_time = tsc_between - tsc_before;
1487 mperf_time = tsc_after - tsc_between;
1488
1489 /*
1490 * If the system call latency to read APERF and MPERF
1491 * differ by more than 2x, then try again.
1492 */
1493 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1494 aperf_mperf_retry_count++;
1495 if (aperf_mperf_retry_count < 5)
1496 goto retry;
1497 else
1498 warnx("cpu%d jitter %lld %lld",
1499 cpu, aperf_time, mperf_time);
1500 }
1501 aperf_mperf_retry_count = 0;
1502
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001503 t->aperf = t->aperf * aperf_mperf_multiplier;
1504 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001505 }
1506
Len Brown812db3f2017-02-10 00:25:41 -05001507 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001508 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001509 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001510 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1511 return -5;
1512 t->smi_count = msr & 0xFFFFFFFF;
1513 }
Len Brown0539ba12017-02-10 00:27:20 -05001514 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001515 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1516 return -6;
1517 }
1518
Len Brown388e9c82016-12-22 23:57:55 -05001519 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001520 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001521 return -10;
1522 }
1523
Len Brownc98d5d92012-06-04 00:56:40 -04001524 /* collect core counters only for 1st thread in core */
1525 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1526 return 0;
1527
Len Brown812db3f2017-02-10 00:25:41 -05001528 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001529 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1530 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001531 }
1532
Len Brown812db3f2017-02-10 00:25:41 -05001533 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001534 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1535 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001536 } else if (do_knl_cstates) {
1537 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1538 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001539 }
1540
Len Brown812db3f2017-02-10 00:25:41 -05001541 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001542 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1543 return -8;
1544
Len Brown0539ba12017-02-10 00:27:20 -05001545 if (DO_BIC(BIC_Mod_c6))
1546 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1547 return -8;
1548
Len Brown812db3f2017-02-10 00:25:41 -05001549 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001550 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1551 return -9;
1552 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1553 }
1554
Len Brown388e9c82016-12-22 23:57:55 -05001555 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001556 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001557 return -10;
1558 }
Len Brown889facb2012-11-08 00:48:57 -05001559
Len Brownc98d5d92012-06-04 00:56:40 -04001560 /* collect package counters only for 1st core in package */
1561 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1562 return 0;
1563
Len Brown0b2bb692015-03-26 00:50:30 -04001564 if (do_skl_residency) {
1565 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1566 return -10;
1567 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1568 return -11;
1569 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1570 return -12;
1571 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1572 return -13;
1573 }
Len Brown0f47c082017-01-27 00:50:45 -05001574 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001575 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1576 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001577 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001578 if (do_slm_cstates) {
1579 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1580 return -10;
1581 } else {
1582 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1583 return -10;
1584 }
1585 }
1586
Len Brown0f47c082017-01-27 00:50:45 -05001587 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001588 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1589 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001590 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001591 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1592 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001593 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001594 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1595 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001596 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001597 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1598 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001599 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001600 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1601 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001602
Len Brown889facb2012-11-08 00:48:57 -05001603 if (do_rapl & RAPL_PKG) {
1604 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1605 return -13;
1606 p->energy_pkg = msr & 0xFFFFFFFF;
1607 }
Jacob Pan91484942016-06-16 09:48:20 -07001608 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001609 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1610 return -14;
1611 p->energy_cores = msr & 0xFFFFFFFF;
1612 }
1613 if (do_rapl & RAPL_DRAM) {
1614 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1615 return -15;
1616 p->energy_dram = msr & 0xFFFFFFFF;
1617 }
1618 if (do_rapl & RAPL_GFX) {
1619 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1620 return -16;
1621 p->energy_gfx = msr & 0xFFFFFFFF;
1622 }
1623 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1624 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1625 return -16;
1626 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1627 }
1628 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1629 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1630 return -16;
1631 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1632 }
Len Brown812db3f2017-02-10 00:25:41 -05001633 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001634 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1635 return -17;
1636 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1637 }
Len Brownfdf676e2016-02-27 01:28:12 -05001638
Len Brown812db3f2017-02-10 00:25:41 -05001639 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001640 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1641
Len Brown812db3f2017-02-10 00:25:41 -05001642 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001643 p->gfx_mhz = gfx_cur_mhz;
1644
Len Brown388e9c82016-12-22 23:57:55 -05001645 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001646 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001647 return -10;
1648 }
1649
Len Brown103a8fe2010-10-22 23:53:03 -04001650 return 0;
1651}
1652
Len Brownee7e38e2015-02-09 23:39:45 -05001653/*
1654 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1655 * If you change the values, note they are used both in comparisons
1656 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1657 */
1658
1659#define PCLUKN 0 /* Unknown */
1660#define PCLRSV 1 /* Reserved */
1661#define PCL__0 2 /* PC0 */
1662#define PCL__1 3 /* PC1 */
1663#define PCL__2 4 /* PC2 */
1664#define PCL__3 5 /* PC3 */
1665#define PCL__4 6 /* PC4 */
1666#define PCL__6 7 /* PC6 */
1667#define PCL_6N 8 /* PC6 No Retention */
1668#define PCL_6R 9 /* PC6 Retention */
1669#define PCL__7 10 /* PC7 */
1670#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001671#define PCL__8 12 /* PC8 */
1672#define PCL__9 13 /* PC9 */
1673#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001674
1675int pkg_cstate_limit = PCLUKN;
1676char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001677 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001678
Len Browne9257f52015-04-01 21:02:57 -04001679int 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};
1680int 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};
1681int 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 -05001682int 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 -05001683int 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 -04001684int 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 -04001685int 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 -05001686int 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 -05001687
Len Browna2b7b742015-09-26 00:12:38 -04001688
1689static void
1690calculate_tsc_tweak()
1691{
Len Browna2b7b742015-09-26 00:12:38 -04001692 tsc_tweak = base_hz / tsc_hz;
1693}
1694
Len Brownfcd17212015-03-23 20:29:09 -04001695static void
1696dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001697{
1698 unsigned long long msr;
1699 unsigned int ratio;
1700
Len Brownec0adc52015-11-12 02:42:31 -05001701 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001702
Len Brownb7d8c142016-02-13 23:36:17 -05001703 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001704
Len Brown103a8fe2010-10-22 23:53:03 -04001705 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001706 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001707 ratio, bclk, ratio * bclk);
1708
1709 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001710 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001711 ratio, bclk, ratio * bclk);
1712
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001713 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001714 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001715 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001716
Len Brownfcd17212015-03-23 20:29:09 -04001717 return;
1718}
1719
1720static void
1721dump_hsw_turbo_ratio_limits(void)
1722{
1723 unsigned long long msr;
1724 unsigned int ratio;
1725
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001726 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001727
Len Brownb7d8c142016-02-13 23:36:17 -05001728 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001729
1730 ratio = (msr >> 8) & 0xFF;
1731 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001732 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001733 ratio, bclk, ratio * bclk);
1734
1735 ratio = (msr >> 0) & 0xFF;
1736 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001737 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001738 ratio, bclk, ratio * bclk);
1739 return;
1740}
1741
1742static void
1743dump_ivt_turbo_ratio_limits(void)
1744{
1745 unsigned long long msr;
1746 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001747
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001748 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001749
Len Brownb7d8c142016-02-13 23:36:17 -05001750 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001751
1752 ratio = (msr >> 56) & 0xFF;
1753 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001754 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001755 ratio, bclk, ratio * bclk);
1756
1757 ratio = (msr >> 48) & 0xFF;
1758 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001759 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001760 ratio, bclk, ratio * bclk);
1761
1762 ratio = (msr >> 40) & 0xFF;
1763 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001764 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001765 ratio, bclk, ratio * bclk);
1766
1767 ratio = (msr >> 32) & 0xFF;
1768 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001769 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001770 ratio, bclk, ratio * bclk);
1771
1772 ratio = (msr >> 24) & 0xFF;
1773 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001774 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001775 ratio, bclk, ratio * bclk);
1776
1777 ratio = (msr >> 16) & 0xFF;
1778 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001779 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001780 ratio, bclk, ratio * bclk);
1781
1782 ratio = (msr >> 8) & 0xFF;
1783 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001784 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001785 ratio, bclk, ratio * bclk);
1786
1787 ratio = (msr >> 0) & 0xFF;
1788 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001789 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001790 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001791 return;
1792}
Len Brown31e07522017-01-31 23:07:49 -05001793int has_turbo_ratio_group_limits(int family, int model)
1794{
1795
1796 if (!genuine_intel)
1797 return 0;
1798
1799 switch (model) {
1800 case INTEL_FAM6_ATOM_GOLDMONT:
1801 case INTEL_FAM6_SKYLAKE_X:
1802 case INTEL_FAM6_ATOM_DENVERTON:
1803 return 1;
1804 }
1805 return 0;
1806}
Len Brown6574a5d2012-09-21 00:01:31 -04001807
Len Brownfcd17212015-03-23 20:29:09 -04001808static void
Len Brown31e07522017-01-31 23:07:49 -05001809dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001810{
Len Brown31e07522017-01-31 23:07:49 -05001811 unsigned long long msr, core_counts;
1812 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001813
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001814 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001815 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001816
Len Brown31e07522017-01-31 23:07:49 -05001817 if (has_turbo_ratio_group_limits(family, model)) {
1818 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1819 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1820 } else {
1821 core_counts = 0x0807060504030201;
1822 }
1823
Len Brown6574a5d2012-09-21 00:01:31 -04001824 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001825 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001826 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001827 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1828 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001829
1830 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001831 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001832 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001833 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1834 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001835
1836 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001837 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001838 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001839 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1840 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001841
1842 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001843 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001844 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001845 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1846 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001847
Len Brown103a8fe2010-10-22 23:53:03 -04001848 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001849 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001850 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001851 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1852 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001853
1854 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001855 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001856 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001857 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1858 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001859
1860 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001861 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001862 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001863 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1864 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001865
1866 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001867 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001868 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001869 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1870 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04001871 return;
1872}
Len Brown3a9a9412014-08-15 02:39:52 -04001873
Len Brownfcd17212015-03-23 20:29:09 -04001874static void
Len Brown0f7887c2017-01-12 23:49:18 -05001875dump_atom_turbo_ratio_limits(void)
1876{
1877 unsigned long long msr;
1878 unsigned int ratio;
1879
1880 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1881 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1882
1883 ratio = (msr >> 0) & 0x3F;
1884 if (ratio)
1885 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1886 ratio, bclk, ratio * bclk);
1887
1888 ratio = (msr >> 8) & 0x3F;
1889 if (ratio)
1890 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1891 ratio, bclk, ratio * bclk);
1892
1893 ratio = (msr >> 16) & 0x3F;
1894 if (ratio)
1895 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1896 ratio, bclk, ratio * bclk);
1897
1898 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1899 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1900
1901 ratio = (msr >> 24) & 0x3F;
1902 if (ratio)
1903 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1904 ratio, bclk, ratio * bclk);
1905
1906 ratio = (msr >> 16) & 0x3F;
1907 if (ratio)
1908 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1909 ratio, bclk, ratio * bclk);
1910
1911 ratio = (msr >> 8) & 0x3F;
1912 if (ratio)
1913 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1914 ratio, bclk, ratio * bclk);
1915
1916 ratio = (msr >> 0) & 0x3F;
1917 if (ratio)
1918 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1919 ratio, bclk, ratio * bclk);
1920}
1921
1922static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001923dump_knl_turbo_ratio_limits(void)
1924{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001925 const unsigned int buckets_no = 7;
1926
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001927 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001928 int delta_cores, delta_ratio;
1929 int i, b_nr;
1930 unsigned int cores[buckets_no];
1931 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001932
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001933 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001934
Len Brownb7d8c142016-02-13 23:36:17 -05001935 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001936 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001937
1938 /**
1939 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001940 * [0] -- Reserved
1941 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001942 * [15:8] -- Base value of freq ratio of bucket 1.
1943 * [20:16] -- +ve delta of number of active cores of bucket 2.
1944 * i.e. active cores of bucket 2 =
1945 * active cores of bucket 1 + delta
1946 * [23:21] -- Negative delta of freq ratio of bucket 2.
1947 * i.e. freq ratio of bucket 2 =
1948 * freq ratio of bucket 1 - delta
1949 * [28:24]-- +ve delta of number of active cores of bucket 3.
1950 * [31:29]-- -ve delta of freq ratio of bucket 3.
1951 * [36:32]-- +ve delta of number of active cores of bucket 4.
1952 * [39:37]-- -ve delta of freq ratio of bucket 4.
1953 * [44:40]-- +ve delta of number of active cores of bucket 5.
1954 * [47:45]-- -ve delta of freq ratio of bucket 5.
1955 * [52:48]-- +ve delta of number of active cores of bucket 6.
1956 * [55:53]-- -ve delta of freq ratio of bucket 6.
1957 * [60:56]-- +ve delta of number of active cores of bucket 7.
1958 * [63:61]-- -ve delta of freq ratio of bucket 7.
1959 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001960
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001961 b_nr = 0;
1962 cores[b_nr] = (msr & 0xFF) >> 1;
1963 ratio[b_nr] = (msr >> 8) & 0xFF;
1964
1965 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001966 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001967 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001968
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001969 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1970 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1971 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001972 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001973
1974 for (i = buckets_no - 1; i >= 0; i--)
1975 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001976 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05001977 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001978 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001979}
1980
1981static void
Len Brownfcd17212015-03-23 20:29:09 -04001982dump_nhm_cst_cfg(void)
1983{
1984 unsigned long long msr;
1985
Len Brown1df2e552017-01-07 23:24:57 -05001986 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001987
1988#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1989#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1990
Len Brown1df2e552017-01-07 23:24:57 -05001991 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001992
Len Brownb7d8c142016-02-13 23:36:17 -05001993 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001994 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1995 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1996 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1997 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1998 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001999 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002000 pkg_cstate_limit_strings[pkg_cstate_limit]);
2001 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002002}
2003
Len Brown6fb31432015-06-17 16:23:45 -04002004static void
2005dump_config_tdp(void)
2006{
2007 unsigned long long msr;
2008
2009 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002010 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002011 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002012
2013 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002014 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002015 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002016 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2017 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2018 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2019 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002020 }
Len Brownb7d8c142016-02-13 23:36:17 -05002021 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002022
2023 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002024 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002025 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002026 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2027 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2028 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2029 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002030 }
Len Brownb7d8c142016-02-13 23:36:17 -05002031 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002032
2033 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002034 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002035 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002036 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2037 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2038 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002039
Len Brown6fb31432015-06-17 16:23:45 -04002040 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002041 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002042 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002043 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2044 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002045}
Len Brown5a634262016-04-06 17:15:55 -04002046
2047unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2048
2049void print_irtl(void)
2050{
2051 unsigned long long msr;
2052
2053 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2054 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2055 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2056 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2057
2058 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2059 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2060 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2061 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2062
2063 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2064 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2065 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2066 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2067
2068 if (!do_irtl_hsw)
2069 return;
2070
2071 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2072 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2073 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2074 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2075
2076 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2077 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2078 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2079 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2080
2081 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2082 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2083 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2084 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2085
2086}
Len Brown36229892016-02-26 20:51:02 -05002087void free_fd_percpu(void)
2088{
2089 int i;
2090
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002091 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002092 if (fd_percpu[i] != 0)
2093 close(fd_percpu[i]);
2094 }
2095
2096 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002097}
2098
Len Brownc98d5d92012-06-04 00:56:40 -04002099void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002100{
Len Brownc98d5d92012-06-04 00:56:40 -04002101 CPU_FREE(cpu_present_set);
2102 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002103 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002104
Len Brownc98d5d92012-06-04 00:56:40 -04002105 CPU_FREE(cpu_affinity_set);
2106 cpu_affinity_set = NULL;
2107 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002108
Len Brownc98d5d92012-06-04 00:56:40 -04002109 free(thread_even);
2110 free(core_even);
2111 free(package_even);
2112
2113 thread_even = NULL;
2114 core_even = NULL;
2115 package_even = NULL;
2116
2117 free(thread_odd);
2118 free(core_odd);
2119 free(package_odd);
2120
2121 thread_odd = NULL;
2122 core_odd = NULL;
2123 package_odd = NULL;
2124
2125 free(output_buffer);
2126 output_buffer = NULL;
2127 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002128
2129 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002130
2131 free(irq_column_2_cpu);
2132 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002133}
2134
Josh Triplett57a42a32013-08-20 17:20:17 -07002135
2136/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002137 * Parse a file containing a single int.
2138 */
2139int parse_int_file(const char *fmt, ...)
2140{
2141 va_list args;
2142 char path[PATH_MAX];
2143 FILE *filep;
2144 int value;
2145
2146 va_start(args, fmt);
2147 vsnprintf(path, sizeof(path), fmt, args);
2148 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002149 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002150 if (fscanf(filep, "%d", &value) != 1)
2151 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002152 fclose(filep);
2153 return value;
2154}
2155
2156/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002157 * get_cpu_position_in_core(cpu)
2158 * return the position of the CPU among its HT siblings in the core
2159 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002160 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002161int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002162{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002163 char path[64];
2164 FILE *filep;
2165 int this_cpu;
2166 char character;
2167 int i;
2168
2169 sprintf(path,
2170 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2171 cpu);
2172 filep = fopen(path, "r");
2173 if (filep == NULL) {
2174 perror(path);
2175 exit(1);
2176 }
2177
2178 for (i = 0; i < topo.num_threads_per_core; i++) {
2179 fscanf(filep, "%d", &this_cpu);
2180 if (this_cpu == cpu) {
2181 fclose(filep);
2182 return i;
2183 }
2184
2185 /* Account for no separator after last thread*/
2186 if (i != (topo.num_threads_per_core - 1))
2187 fscanf(filep, "%c", &character);
2188 }
2189
2190 fclose(filep);
2191 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002192}
2193
Len Brownc98d5d92012-06-04 00:56:40 -04002194/*
2195 * cpu_is_first_core_in_package(cpu)
2196 * return 1 if given CPU is 1st core in package
2197 */
2198int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002199{
Josh Triplett95aebc42013-08-20 17:20:16 -07002200 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002201}
2202
2203int get_physical_package_id(int cpu)
2204{
Josh Triplett95aebc42013-08-20 17:20:16 -07002205 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002206}
2207
2208int get_core_id(int cpu)
2209{
Josh Triplett95aebc42013-08-20 17:20:16 -07002210 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002211}
2212
Len Brownc98d5d92012-06-04 00:56:40 -04002213int get_num_ht_siblings(int cpu)
2214{
2215 char path[80];
2216 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002217 int sib1;
2218 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002219 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002220 char str[100];
2221 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002222
2223 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002224 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002225
Len Brownc98d5d92012-06-04 00:56:40 -04002226 /*
2227 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002228 * A ',' separated or '-' separated set of numbers
2229 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002230 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002231 fscanf(filep, "%d%c\n", &sib1, &character);
2232 fseek(filep, 0, SEEK_SET);
2233 fgets(str, 100, filep);
2234 ch = strchr(str, character);
2235 while (ch != NULL) {
2236 matches++;
2237 ch = strchr(ch+1, character);
2238 }
Len Brownc98d5d92012-06-04 00:56:40 -04002239
2240 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002241 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002242}
2243
Len Brown103a8fe2010-10-22 23:53:03 -04002244/*
Len Brownc98d5d92012-06-04 00:56:40 -04002245 * run func(thread, core, package) in topology order
2246 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002247 */
2248
Len Brownc98d5d92012-06-04 00:56:40 -04002249int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2250 struct pkg_data *, struct thread_data *, struct core_data *,
2251 struct pkg_data *), struct thread_data *thread_base,
2252 struct core_data *core_base, struct pkg_data *pkg_base,
2253 struct thread_data *thread_base2, struct core_data *core_base2,
2254 struct pkg_data *pkg_base2)
2255{
2256 int retval, pkg_no, core_no, thread_no;
2257
2258 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2259 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2260 for (thread_no = 0; thread_no <
2261 topo.num_threads_per_core; ++thread_no) {
2262 struct thread_data *t, *t2;
2263 struct core_data *c, *c2;
2264 struct pkg_data *p, *p2;
2265
2266 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2267
2268 if (cpu_is_not_present(t->cpu_id))
2269 continue;
2270
2271 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2272
2273 c = GET_CORE(core_base, core_no, pkg_no);
2274 c2 = GET_CORE(core_base2, core_no, pkg_no);
2275
2276 p = GET_PKG(pkg_base, pkg_no);
2277 p2 = GET_PKG(pkg_base2, pkg_no);
2278
2279 retval = func(t, c, p, t2, c2, p2);
2280 if (retval)
2281 return retval;
2282 }
2283 }
2284 }
2285 return 0;
2286}
2287
2288/*
2289 * run func(cpu) on every cpu in /proc/stat
2290 * return max_cpu number
2291 */
2292int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002293{
2294 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002295 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002296 int retval;
2297
Josh Triplett57a42a32013-08-20 17:20:17 -07002298 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002299
2300 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002301 if (retval != 0)
2302 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002303
Len Brownc98d5d92012-06-04 00:56:40 -04002304 while (1) {
2305 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 -04002306 if (retval != 1)
2307 break;
2308
Len Brownc98d5d92012-06-04 00:56:40 -04002309 retval = func(cpu_num);
2310 if (retval) {
2311 fclose(fp);
2312 return(retval);
2313 }
Len Brown103a8fe2010-10-22 23:53:03 -04002314 }
2315 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002316 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002317}
2318
2319void re_initialize(void)
2320{
Len Brownc98d5d92012-06-04 00:56:40 -04002321 free_all_buffers();
2322 setup_all_buffers();
2323 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002324}
2325
Len Brownc98d5d92012-06-04 00:56:40 -04002326
Len Brown103a8fe2010-10-22 23:53:03 -04002327/*
Len Brownc98d5d92012-06-04 00:56:40 -04002328 * count_cpus()
2329 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002330 */
Len Brownc98d5d92012-06-04 00:56:40 -04002331int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002332{
Len Brownc98d5d92012-06-04 00:56:40 -04002333 if (topo.max_cpu_num < cpu)
2334 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002335
Len Brownc98d5d92012-06-04 00:56:40 -04002336 topo.num_cpus += 1;
2337 return 0;
2338}
2339int mark_cpu_present(int cpu)
2340{
2341 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002342 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002343}
2344
Len Brown562a2d32016-02-26 23:48:05 -05002345/*
2346 * snapshot_proc_interrupts()
2347 *
2348 * read and record summary of /proc/interrupts
2349 *
2350 * return 1 if config change requires a restart, else return 0
2351 */
2352int snapshot_proc_interrupts(void)
2353{
2354 static FILE *fp;
2355 int column, retval;
2356
2357 if (fp == NULL)
2358 fp = fopen_or_die("/proc/interrupts", "r");
2359 else
2360 rewind(fp);
2361
2362 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2363 for (column = 0; column < topo.num_cpus; ++column) {
2364 int cpu_number;
2365
2366 retval = fscanf(fp, " CPU%d", &cpu_number);
2367 if (retval != 1)
2368 break;
2369
2370 if (cpu_number > topo.max_cpu_num) {
2371 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2372 return 1;
2373 }
2374
2375 irq_column_2_cpu[column] = cpu_number;
2376 irqs_per_cpu[cpu_number] = 0;
2377 }
2378
2379 /* read /proc/interrupt count lines and sum up irqs per cpu */
2380 while (1) {
2381 int column;
2382 char buf[64];
2383
2384 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2385 if (retval != 1)
2386 break;
2387
2388 /* read the count per cpu */
2389 for (column = 0; column < topo.num_cpus; ++column) {
2390
2391 int cpu_number, irq_count;
2392
2393 retval = fscanf(fp, " %d", &irq_count);
2394 if (retval != 1)
2395 break;
2396
2397 cpu_number = irq_column_2_cpu[column];
2398 irqs_per_cpu[cpu_number] += irq_count;
2399
2400 }
2401
2402 while (getc(fp) != '\n')
2403 ; /* flush interrupt description */
2404
2405 }
2406 return 0;
2407}
Len Brown27d47352016-02-27 00:37:54 -05002408/*
Len Brownfdf676e2016-02-27 01:28:12 -05002409 * snapshot_gfx_rc6_ms()
2410 *
2411 * record snapshot of
2412 * /sys/class/drm/card0/power/rc6_residency_ms
2413 *
2414 * return 1 if config change requires a restart, else return 0
2415 */
2416int snapshot_gfx_rc6_ms(void)
2417{
2418 FILE *fp;
2419 int retval;
2420
2421 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2422
2423 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2424 if (retval != 1)
2425 err(1, "GFX rc6");
2426
2427 fclose(fp);
2428
2429 return 0;
2430}
2431/*
Len Brown27d47352016-02-27 00:37:54 -05002432 * snapshot_gfx_mhz()
2433 *
2434 * record snapshot of
2435 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2436 *
2437 * return 1 if config change requires a restart, else return 0
2438 */
2439int snapshot_gfx_mhz(void)
2440{
2441 static FILE *fp;
2442 int retval;
2443
2444 if (fp == NULL)
2445 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2446 else
2447 rewind(fp);
2448
2449 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2450 if (retval != 1)
2451 err(1, "GFX MHz");
2452
2453 return 0;
2454}
Len Brown562a2d32016-02-26 23:48:05 -05002455
2456/*
2457 * snapshot /proc and /sys files
2458 *
2459 * return 1 if configuration restart needed, else return 0
2460 */
2461int snapshot_proc_sysfs_files(void)
2462{
Len Brown218f0e82017-02-14 22:07:52 -05002463 if (DO_BIC(BIC_IRQ))
2464 if (snapshot_proc_interrupts())
2465 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002466
Len Brown812db3f2017-02-10 00:25:41 -05002467 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002468 snapshot_gfx_rc6_ms();
2469
Len Brown812db3f2017-02-10 00:25:41 -05002470 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002471 snapshot_gfx_mhz();
2472
Len Brown562a2d32016-02-26 23:48:05 -05002473 return 0;
2474}
2475
Len Brown103a8fe2010-10-22 23:53:03 -04002476void turbostat_loop()
2477{
Len Brownc98d5d92012-06-04 00:56:40 -04002478 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002479 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002480
Len Brown103a8fe2010-10-22 23:53:03 -04002481restart:
Len Browne52966c2012-11-08 22:38:05 -05002482 restarted++;
2483
Len Brown562a2d32016-02-26 23:48:05 -05002484 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002485 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002486 if (retval < -1) {
2487 exit(retval);
2488 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002489 if (restarted > 1) {
2490 exit(retval);
2491 }
Len Brownc98d5d92012-06-04 00:56:40 -04002492 re_initialize();
2493 goto restart;
2494 }
Len Browne52966c2012-11-08 22:38:05 -05002495 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002496 gettimeofday(&tv_even, (struct timezone *)NULL);
2497
2498 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002499 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002500 re_initialize();
2501 goto restart;
2502 }
Len Brown2a0609c2016-02-12 22:44:48 -05002503 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002504 if (snapshot_proc_sysfs_files())
2505 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002506 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002507 if (retval < -1) {
2508 exit(retval);
2509 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002510 re_initialize();
2511 goto restart;
2512 }
Len Brown103a8fe2010-10-22 23:53:03 -04002513 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002514 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002515 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2516 re_initialize();
2517 goto restart;
2518 }
Len Brownc98d5d92012-06-04 00:56:40 -04002519 compute_average(EVEN_COUNTERS);
2520 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002521 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002522 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002523 if (snapshot_proc_sysfs_files())
2524 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002525 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002526 if (retval < -1) {
2527 exit(retval);
2528 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002529 re_initialize();
2530 goto restart;
2531 }
Len Brown103a8fe2010-10-22 23:53:03 -04002532 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002533 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002534 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2535 re_initialize();
2536 goto restart;
2537 }
Len Brownc98d5d92012-06-04 00:56:40 -04002538 compute_average(ODD_COUNTERS);
2539 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002540 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002541 }
2542}
2543
2544void check_dev_msr()
2545{
2546 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002547 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002548
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002549 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2550 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002551 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2552 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002553}
2554
Len Brown98481e72014-08-15 00:36:50 -04002555void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002556{
Len Brown98481e72014-08-15 00:36:50 -04002557 struct __user_cap_header_struct cap_header_data;
2558 cap_user_header_t cap_header = &cap_header_data;
2559 struct __user_cap_data_struct cap_data_data;
2560 cap_user_data_t cap_data = &cap_data_data;
2561 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2562 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002563 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002564
2565 /* check for CAP_SYS_RAWIO */
2566 cap_header->pid = getpid();
2567 cap_header->version = _LINUX_CAPABILITY_VERSION;
2568 if (capget(cap_header, cap_data) < 0)
2569 err(-6, "capget(2) failed");
2570
2571 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2572 do_exit++;
2573 warnx("capget(CAP_SYS_RAWIO) failed,"
2574 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2575 }
2576
2577 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002578 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2579 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002580 do_exit++;
2581 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2582 }
2583
2584 /* if all else fails, thell them to be root */
2585 if (do_exit)
2586 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002587 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002588
2589 if (do_exit)
2590 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002591}
2592
Len Brownd7899442015-01-23 00:12:33 -05002593/*
2594 * NHM adds support for additional MSRs:
2595 *
2596 * MSR_SMI_COUNT 0x00000034
2597 *
Len Brownec0adc52015-11-12 02:42:31 -05002598 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002599 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002600 *
Len Browncf4cbe52017-01-01 13:08:33 -05002601 * MSR_MISC_PWR_MGMT 0x000001aa
2602 *
Len Brownd7899442015-01-23 00:12:33 -05002603 * MSR_PKG_C3_RESIDENCY 0x000003f8
2604 * MSR_PKG_C6_RESIDENCY 0x000003f9
2605 * MSR_CORE_C3_RESIDENCY 0x000003fc
2606 * MSR_CORE_C6_RESIDENCY 0x000003fd
2607 *
Len Brownee7e38e2015-02-09 23:39:45 -05002608 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002609 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002610 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002611 */
Len Brownee7e38e2015-02-09 23:39:45 -05002612int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002613{
Len Brownee7e38e2015-02-09 23:39:45 -05002614 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002615 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002616 int *pkg_cstate_limits;
2617
Len Brown103a8fe2010-10-22 23:53:03 -04002618 if (!genuine_intel)
2619 return 0;
2620
2621 if (family != 6)
2622 return 0;
2623
Len Brown21ed5572015-10-19 22:37:40 -04002624 bclk = discover_bclk(family, model);
2625
Len Brown103a8fe2010-10-22 23:53:03 -04002626 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002627 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2628 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002629 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002630 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2631 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2632 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2633 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002634 pkg_cstate_limits = nhm_pkg_cstate_limits;
2635 break;
Len Brown869ce692016-06-16 23:22:37 -04002636 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2637 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2638 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2639 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002640 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002641 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002642 break;
Len Brown869ce692016-06-16 23:22:37 -04002643 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2644 case INTEL_FAM6_HASWELL_X: /* HSX */
2645 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2646 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2647 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2648 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2649 case INTEL_FAM6_BROADWELL_X: /* BDX */
2650 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2651 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2652 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2653 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2654 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002655 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002656 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002657 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002658 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2659 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002660 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002661 break;
Len Brown869ce692016-06-16 23:22:37 -04002662 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002663 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002664 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002665 pkg_cstate_limits = slv_pkg_cstate_limits;
2666 break;
Len Brown869ce692016-06-16 23:22:37 -04002667 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002668 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002669 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002670 break;
Len Brown869ce692016-06-16 23:22:37 -04002671 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002672 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002673 pkg_cstate_limits = phi_pkg_cstate_limits;
2674 break;
Len Brown869ce692016-06-16 23:22:37 -04002675 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002676 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002677 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002678 pkg_cstate_limits = bxt_pkg_cstate_limits;
2679 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002680 default:
2681 return 0;
2682 }
Len Brown1df2e552017-01-07 23:24:57 -05002683 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002684 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002685
Len Brownec0adc52015-11-12 02:42:31 -05002686 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002687 base_ratio = (msr >> 8) & 0xFF;
2688
2689 base_hz = base_ratio * bclk * 1000000;
2690 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002691 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002692}
Len Brown0f7887c2017-01-12 23:49:18 -05002693/*
Len Brown495c76542017-02-08 02:41:51 -05002694 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002695 *
2696 * MSR_CC6_DEMOTION_POLICY_CONFIG
2697 * MSR_MC6_DEMOTION_POLICY_CONFIG
2698 */
2699
2700int has_slv_msrs(unsigned int family, unsigned int model)
2701{
2702 if (!genuine_intel)
2703 return 0;
2704
2705 switch (model) {
2706 case INTEL_FAM6_ATOM_SILVERMONT1:
2707 case INTEL_FAM6_ATOM_MERRIFIELD:
2708 case INTEL_FAM6_ATOM_MOOREFIELD:
2709 return 1;
2710 }
2711 return 0;
2712}
Len Brown7170a372017-01-27 02:13:27 -05002713int is_dnv(unsigned int family, unsigned int model)
2714{
2715
2716 if (!genuine_intel)
2717 return 0;
2718
2719 switch (model) {
2720 case INTEL_FAM6_ATOM_DENVERTON:
2721 return 1;
2722 }
2723 return 0;
2724}
Len Brownade0eba2017-02-10 01:56:47 -05002725int is_bdx(unsigned int family, unsigned int model)
2726{
2727
2728 if (!genuine_intel)
2729 return 0;
2730
2731 switch (model) {
2732 case INTEL_FAM6_BROADWELL_X:
2733 case INTEL_FAM6_BROADWELL_XEON_D:
2734 return 1;
2735 }
2736 return 0;
2737}
Len Brown34c761972017-01-27 02:36:41 -05002738int is_skx(unsigned int family, unsigned int model)
2739{
2740
2741 if (!genuine_intel)
2742 return 0;
2743
2744 switch (model) {
2745 case INTEL_FAM6_SKYLAKE_X:
2746 return 1;
2747 }
2748 return 0;
2749}
Len Brown0f7887c2017-01-12 23:49:18 -05002750
Len Brown31e07522017-01-31 23:07:49 -05002751int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002752{
Len Brown0f7887c2017-01-12 23:49:18 -05002753 if (has_slv_msrs(family, model))
2754 return 0;
2755
Len Brownd7899442015-01-23 00:12:33 -05002756 switch (model) {
2757 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002758 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2759 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2760 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002761 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002762 return 0;
2763 default:
2764 return 1;
2765 }
2766}
Len Brown0f7887c2017-01-12 23:49:18 -05002767int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2768{
2769 if (has_slv_msrs(family, model))
2770 return 1;
2771
2772 return 0;
2773}
Len Brown6574a5d2012-09-21 00:01:31 -04002774int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2775{
2776 if (!genuine_intel)
2777 return 0;
2778
2779 if (family != 6)
2780 return 0;
2781
2782 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002783 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2784 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002785 return 1;
2786 default:
2787 return 0;
2788 }
2789}
Len Brownfcd17212015-03-23 20:29:09 -04002790int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2791{
2792 if (!genuine_intel)
2793 return 0;
2794
2795 if (family != 6)
2796 return 0;
2797
2798 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002799 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002800 return 1;
2801 default:
2802 return 0;
2803 }
2804}
2805
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002806int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2807{
2808 if (!genuine_intel)
2809 return 0;
2810
2811 if (family != 6)
2812 return 0;
2813
2814 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002815 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002816 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002817 return 1;
2818 default:
2819 return 0;
2820 }
2821}
Len Brown31e07522017-01-31 23:07:49 -05002822int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
2823{
2824 if (!genuine_intel)
2825 return 0;
2826
2827 if (family != 6)
2828 return 0;
2829
2830 switch (model) {
2831 case INTEL_FAM6_ATOM_GOLDMONT:
2832 case INTEL_FAM6_SKYLAKE_X:
2833 return 1;
2834 default:
2835 return 0;
2836 }
2837}
Len Brown6fb31432015-06-17 16:23:45 -04002838int has_config_tdp(unsigned int family, unsigned int model)
2839{
2840 if (!genuine_intel)
2841 return 0;
2842
2843 if (family != 6)
2844 return 0;
2845
2846 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002847 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2848 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2849 case INTEL_FAM6_HASWELL_X: /* HSX */
2850 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2851 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2852 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2853 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2854 case INTEL_FAM6_BROADWELL_X: /* BDX */
2855 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2856 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2857 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2858 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2859 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2860 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002861
Len Brown869ce692016-06-16 23:22:37 -04002862 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002863 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002864 return 1;
2865 default:
2866 return 0;
2867 }
2868}
2869
Len Brownfcd17212015-03-23 20:29:09 -04002870static void
Colin Ian King1b693172016-03-02 13:50:25 +00002871dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002872{
2873 if (!do_nhm_platform_info)
2874 return;
2875
2876 dump_nhm_platform_info();
2877
2878 if (has_hsw_turbo_ratio_limit(family, model))
2879 dump_hsw_turbo_ratio_limits();
2880
2881 if (has_ivt_turbo_ratio_limit(family, model))
2882 dump_ivt_turbo_ratio_limits();
2883
Len Brown31e07522017-01-31 23:07:49 -05002884 if (has_turbo_ratio_limit(family, model))
2885 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04002886
Len Brown0f7887c2017-01-12 23:49:18 -05002887 if (has_atom_turbo_ratio_limit(family, model))
2888 dump_atom_turbo_ratio_limits();
2889
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002890 if (has_knl_turbo_ratio_limit(family, model))
2891 dump_knl_turbo_ratio_limits();
2892
Len Brown6fb31432015-06-17 16:23:45 -04002893 if (has_config_tdp(family, model))
2894 dump_config_tdp();
2895
Len Brownfcd17212015-03-23 20:29:09 -04002896 dump_nhm_cst_cfg();
2897}
2898
Len Brown41618e62017-02-09 18:25:22 -05002899static void
2900dump_sysfs_cstate_config(void)
2901{
2902 char path[64];
2903 char name_buf[16];
2904 char desc[64];
2905 FILE *input;
2906 int state;
2907 char *sp;
2908
2909 if (!DO_BIC(BIC_sysfs))
2910 return;
2911
2912 for (state = 0; state < 10; ++state) {
2913
2914 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
2915 base_cpu, state);
2916 input = fopen(path, "r");
2917 if (input == NULL)
2918 continue;
2919 fgets(name_buf, sizeof(name_buf), input);
2920
2921 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
2922 sp = strchr(name_buf, '-');
2923 if (!sp)
2924 sp = strchrnul(name_buf, '\n');
2925 *sp = '\0';
2926
2927 fclose(input);
2928
2929 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
2930 base_cpu, state);
2931 input = fopen(path, "r");
2932 if (input == NULL)
2933 continue;
2934 fgets(desc, sizeof(desc), input);
2935
2936 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
2937 fclose(input);
2938 }
2939}
2940
Len Brown6574a5d2012-09-21 00:01:31 -04002941
Len Brown889facb2012-11-08 00:48:57 -05002942/*
2943 * print_epb()
2944 * Decode the ENERGY_PERF_BIAS MSR
2945 */
2946int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2947{
2948 unsigned long long msr;
2949 char *epb_string;
2950 int cpu;
2951
2952 if (!has_epb)
2953 return 0;
2954
2955 cpu = t->cpu_id;
2956
2957 /* EPB is per-package */
2958 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2959 return 0;
2960
2961 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002962 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002963 return -1;
2964 }
2965
2966 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2967 return 0;
2968
Len Browne9be7dd2015-05-26 12:19:37 -04002969 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002970 case ENERGY_PERF_BIAS_PERFORMANCE:
2971 epb_string = "performance";
2972 break;
2973 case ENERGY_PERF_BIAS_NORMAL:
2974 epb_string = "balanced";
2975 break;
2976 case ENERGY_PERF_BIAS_POWERSAVE:
2977 epb_string = "powersave";
2978 break;
2979 default:
2980 epb_string = "custom";
2981 break;
2982 }
Len Brownb7d8c142016-02-13 23:36:17 -05002983 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002984
2985 return 0;
2986}
Len Brown7f5c2582015-12-01 01:36:39 -05002987/*
2988 * print_hwp()
2989 * Decode the MSR_HWP_CAPABILITIES
2990 */
2991int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2992{
2993 unsigned long long msr;
2994 int cpu;
2995
2996 if (!has_hwp)
2997 return 0;
2998
2999 cpu = t->cpu_id;
3000
3001 /* MSR_HWP_CAPABILITIES is per-package */
3002 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3003 return 0;
3004
3005 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003006 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003007 return -1;
3008 }
3009
3010 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3011 return 0;
3012
Len Brownb7d8c142016-02-13 23:36:17 -05003013 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003014 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3015
3016 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3017 if ((msr & (1 << 0)) == 0)
3018 return 0;
3019
3020 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3021 return 0;
3022
Len Brownb7d8c142016-02-13 23:36:17 -05003023 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003024 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
3025 cpu, msr,
3026 (unsigned int)HWP_HIGHEST_PERF(msr),
3027 (unsigned int)HWP_GUARANTEED_PERF(msr),
3028 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3029 (unsigned int)HWP_LOWEST_PERF(msr));
3030
3031 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3032 return 0;
3033
Len Brownb7d8c142016-02-13 23:36:17 -05003034 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003035 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
3036 cpu, msr,
3037 (unsigned int)(((msr) >> 0) & 0xff),
3038 (unsigned int)(((msr) >> 8) & 0xff),
3039 (unsigned int)(((msr) >> 16) & 0xff),
3040 (unsigned int)(((msr) >> 24) & 0xff),
3041 (unsigned int)(((msr) >> 32) & 0xff3),
3042 (unsigned int)(((msr) >> 42) & 0x1));
3043
3044 if (has_hwp_pkg) {
3045 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3046 return 0;
3047
Len Brownb7d8c142016-02-13 23:36:17 -05003048 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003049 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
3050 cpu, msr,
3051 (unsigned int)(((msr) >> 0) & 0xff),
3052 (unsigned int)(((msr) >> 8) & 0xff),
3053 (unsigned int)(((msr) >> 16) & 0xff),
3054 (unsigned int)(((msr) >> 24) & 0xff),
3055 (unsigned int)(((msr) >> 32) & 0xff3));
3056 }
3057 if (has_hwp_notify) {
3058 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3059 return 0;
3060
Len Brownb7d8c142016-02-13 23:36:17 -05003061 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003062 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3063 cpu, msr,
3064 ((msr) & 0x1) ? "EN" : "Dis",
3065 ((msr) & 0x2) ? "EN" : "Dis");
3066 }
3067 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3068 return 0;
3069
Len Brownb7d8c142016-02-13 23:36:17 -05003070 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003071 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3072 cpu, msr,
3073 ((msr) & 0x1) ? "" : "No-",
3074 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003075
3076 return 0;
3077}
3078
Len Brown3a9a9412014-08-15 02:39:52 -04003079/*
3080 * print_perf_limit()
3081 */
3082int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3083{
3084 unsigned long long msr;
3085 int cpu;
3086
3087 cpu = t->cpu_id;
3088
3089 /* per-package */
3090 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3091 return 0;
3092
3093 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003094 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003095 return -1;
3096 }
3097
3098 if (do_core_perf_limit_reasons) {
3099 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003100 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3101 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003102 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003103 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003104 (msr & 1 << 13) ? "Transitions, " : "",
3105 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3106 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3107 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3108 (msr & 1 << 9) ? "CorePwr, " : "",
3109 (msr & 1 << 8) ? "Amps, " : "",
3110 (msr & 1 << 6) ? "VR-Therm, " : "",
3111 (msr & 1 << 5) ? "Auto-HWP, " : "",
3112 (msr & 1 << 4) ? "Graphics, " : "",
3113 (msr & 1 << 2) ? "bit2, " : "",
3114 (msr & 1 << 1) ? "ThermStatus, " : "",
3115 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003116 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 -04003117 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003118 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003119 (msr & 1 << 29) ? "Transitions, " : "",
3120 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3121 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3122 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3123 (msr & 1 << 25) ? "CorePwr, " : "",
3124 (msr & 1 << 24) ? "Amps, " : "",
3125 (msr & 1 << 22) ? "VR-Therm, " : "",
3126 (msr & 1 << 21) ? "Auto-HWP, " : "",
3127 (msr & 1 << 20) ? "Graphics, " : "",
3128 (msr & 1 << 18) ? "bit18, " : "",
3129 (msr & 1 << 17) ? "ThermStatus, " : "",
3130 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003131
3132 }
3133 if (do_gfx_perf_limit_reasons) {
3134 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003135 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3136 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003137 (msr & 1 << 0) ? "PROCHOT, " : "",
3138 (msr & 1 << 1) ? "ThermStatus, " : "",
3139 (msr & 1 << 4) ? "Graphics, " : "",
3140 (msr & 1 << 6) ? "VR-Therm, " : "",
3141 (msr & 1 << 8) ? "Amps, " : "",
3142 (msr & 1 << 9) ? "GFXPwr, " : "",
3143 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3144 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003145 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003146 (msr & 1 << 16) ? "PROCHOT, " : "",
3147 (msr & 1 << 17) ? "ThermStatus, " : "",
3148 (msr & 1 << 20) ? "Graphics, " : "",
3149 (msr & 1 << 22) ? "VR-Therm, " : "",
3150 (msr & 1 << 24) ? "Amps, " : "",
3151 (msr & 1 << 25) ? "GFXPwr, " : "",
3152 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3153 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3154 }
3155 if (do_ring_perf_limit_reasons) {
3156 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003157 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3158 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003159 (msr & 1 << 0) ? "PROCHOT, " : "",
3160 (msr & 1 << 1) ? "ThermStatus, " : "",
3161 (msr & 1 << 6) ? "VR-Therm, " : "",
3162 (msr & 1 << 8) ? "Amps, " : "",
3163 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3164 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003165 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003166 (msr & 1 << 16) ? "PROCHOT, " : "",
3167 (msr & 1 << 17) ? "ThermStatus, " : "",
3168 (msr & 1 << 22) ? "VR-Therm, " : "",
3169 (msr & 1 << 24) ? "Amps, " : "",
3170 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3171 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3172 }
3173 return 0;
3174}
3175
Len Brown889facb2012-11-08 00:48:57 -05003176#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3177#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3178
Colin Ian King1b693172016-03-02 13:50:25 +00003179double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003180{
3181 unsigned long long msr;
3182
3183 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003184 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003185 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3186
3187 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003188 case INTEL_FAM6_ATOM_SILVERMONT1:
3189 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003190 return 30.0;
3191 default:
3192 return 135.0;
3193 }
3194}
3195
Andrey Semin40ee8e32014-12-05 00:07:00 -05003196/*
3197 * rapl_dram_energy_units_probe()
3198 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3199 */
3200static double
3201rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3202{
3203 /* only called for genuine_intel, family 6 */
3204
3205 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003206 case INTEL_FAM6_HASWELL_X: /* HSX */
3207 case INTEL_FAM6_BROADWELL_X: /* BDX */
3208 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3209 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003210 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003211 return (rapl_dram_energy_units = 15.3 / 1000000);
3212 default:
3213 return (rapl_energy_units);
3214 }
3215}
3216
Len Brown144b44b2013-11-09 00:30:16 -05003217
Len Brown889facb2012-11-08 00:48:57 -05003218/*
3219 * rapl_probe()
3220 *
Len Brown144b44b2013-11-09 00:30:16 -05003221 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003222 */
3223void rapl_probe(unsigned int family, unsigned int model)
3224{
3225 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003226 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003227 double tdp;
3228
3229 if (!genuine_intel)
3230 return;
3231
3232 if (family != 6)
3233 return;
3234
3235 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003236 case INTEL_FAM6_SANDYBRIDGE:
3237 case INTEL_FAM6_IVYBRIDGE:
3238 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3239 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3240 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3241 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3242 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003243 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003244 if (rapl_joules) {
3245 BIC_PRESENT(BIC_Pkg_J);
3246 BIC_PRESENT(BIC_Cor_J);
3247 BIC_PRESENT(BIC_GFX_J);
3248 } else {
3249 BIC_PRESENT(BIC_PkgWatt);
3250 BIC_PRESENT(BIC_CorWatt);
3251 BIC_PRESENT(BIC_GFXWatt);
3252 }
Len Brown889facb2012-11-08 00:48:57 -05003253 break;
Len Brown869ce692016-06-16 23:22:37 -04003254 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003255 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003256 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003257 if (rapl_joules)
3258 BIC_PRESENT(BIC_Pkg_J);
3259 else
3260 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003261 break;
Len Brown869ce692016-06-16 23:22:37 -04003262 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3263 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3264 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3265 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003266 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 -05003267 BIC_PRESENT(BIC_PKG__);
3268 BIC_PRESENT(BIC_RAM__);
3269 if (rapl_joules) {
3270 BIC_PRESENT(BIC_Pkg_J);
3271 BIC_PRESENT(BIC_Cor_J);
3272 BIC_PRESENT(BIC_RAM_J);
3273 } else {
3274 BIC_PRESENT(BIC_PkgWatt);
3275 BIC_PRESENT(BIC_CorWatt);
3276 BIC_PRESENT(BIC_RAMWatt);
3277 }
Len Brown0b2bb692015-03-26 00:50:30 -04003278 break;
Len Brown869ce692016-06-16 23:22:37 -04003279 case INTEL_FAM6_HASWELL_X: /* HSX */
3280 case INTEL_FAM6_BROADWELL_X: /* BDX */
3281 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3282 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3283 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003284 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003285 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 -05003286 BIC_PRESENT(BIC_PKG__);
3287 BIC_PRESENT(BIC_RAM__);
3288 if (rapl_joules) {
3289 BIC_PRESENT(BIC_Pkg_J);
3290 BIC_PRESENT(BIC_RAM_J);
3291 } else {
3292 BIC_PRESENT(BIC_PkgWatt);
3293 BIC_PRESENT(BIC_RAMWatt);
3294 }
Len Browne6f9bb32013-12-03 02:19:19 -05003295 break;
Len Brown869ce692016-06-16 23:22:37 -04003296 case INTEL_FAM6_SANDYBRIDGE_X:
3297 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003298 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 -05003299 BIC_PRESENT(BIC_PKG__);
3300 BIC_PRESENT(BIC_RAM__);
3301 if (rapl_joules) {
3302 BIC_PRESENT(BIC_Pkg_J);
3303 BIC_PRESENT(BIC_Cor_J);
3304 BIC_PRESENT(BIC_RAM_J);
3305 } else {
3306 BIC_PRESENT(BIC_PkgWatt);
3307 BIC_PRESENT(BIC_CorWatt);
3308 BIC_PRESENT(BIC_RAMWatt);
3309 }
Len Brown144b44b2013-11-09 00:30:16 -05003310 break;
Len Brown869ce692016-06-16 23:22:37 -04003311 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3312 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003313 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003314 if (rapl_joules) {
3315 BIC_PRESENT(BIC_Pkg_J);
3316 BIC_PRESENT(BIC_Cor_J);
3317 } else {
3318 BIC_PRESENT(BIC_PkgWatt);
3319 BIC_PRESENT(BIC_CorWatt);
3320 }
Len Brown889facb2012-11-08 00:48:57 -05003321 break;
Len Brown869ce692016-06-16 23:22:37 -04003322 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003323 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 -05003324 BIC_PRESENT(BIC_PKG__);
3325 BIC_PRESENT(BIC_RAM__);
3326 if (rapl_joules) {
3327 BIC_PRESENT(BIC_Pkg_J);
3328 BIC_PRESENT(BIC_Cor_J);
3329 BIC_PRESENT(BIC_RAM_J);
3330 } else {
3331 BIC_PRESENT(BIC_PkgWatt);
3332 BIC_PRESENT(BIC_CorWatt);
3333 BIC_PRESENT(BIC_RAMWatt);
3334 }
Jacob Pan0f644902016-06-16 09:48:22 -07003335 break;
Len Brown889facb2012-11-08 00:48:57 -05003336 default:
3337 return;
3338 }
3339
3340 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003341 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003342 return;
3343
3344 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003345 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003346 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3347 else
3348 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003349
Andrey Semin40ee8e32014-12-05 00:07:00 -05003350 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3351
Len Brown144b44b2013-11-09 00:30:16 -05003352 time_unit = msr >> 16 & 0xF;
3353 if (time_unit == 0)
3354 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003355
Len Brown144b44b2013-11-09 00:30:16 -05003356 rapl_time_units = 1.0 / (1 << (time_unit));
3357
3358 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003359
3360 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003361 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003362 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003363
3364 return;
3365}
3366
Colin Ian King1b693172016-03-02 13:50:25 +00003367void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003368{
3369 if (!genuine_intel)
3370 return;
3371
3372 if (family != 6)
3373 return;
3374
3375 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003376 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3377 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3378 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003379 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003380 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003381 do_core_perf_limit_reasons = 1;
3382 do_ring_perf_limit_reasons = 1;
3383 default:
3384 return;
3385 }
3386}
3387
Len Brown889facb2012-11-08 00:48:57 -05003388int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3389{
3390 unsigned long long msr;
3391 unsigned int dts;
3392 int cpu;
3393
3394 if (!(do_dts || do_ptm))
3395 return 0;
3396
3397 cpu = t->cpu_id;
3398
3399 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003400 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003401 return 0;
3402
3403 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003404 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003405 return -1;
3406 }
3407
3408 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3409 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3410 return 0;
3411
3412 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003413 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003414 cpu, msr, tcc_activation_temp - dts);
3415
3416#ifdef THERM_DEBUG
3417 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3418 return 0;
3419
3420 dts = (msr >> 16) & 0x7F;
3421 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003422 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003423 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3424#endif
3425 }
3426
3427
3428 if (do_dts) {
3429 unsigned int resolution;
3430
3431 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3432 return 0;
3433
3434 dts = (msr >> 16) & 0x7F;
3435 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003436 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003437 cpu, msr, tcc_activation_temp - dts, resolution);
3438
3439#ifdef THERM_DEBUG
3440 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3441 return 0;
3442
3443 dts = (msr >> 16) & 0x7F;
3444 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003445 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003446 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3447#endif
3448 }
3449
3450 return 0;
3451}
Len Brown36229892016-02-26 20:51:02 -05003452
Len Brown889facb2012-11-08 00:48:57 -05003453void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3454{
Len Brownb7d8c142016-02-13 23:36:17 -05003455 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003456 cpu, label,
3457 ((msr >> 15) & 1) ? "EN" : "DIS",
3458 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3459 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3460 (((msr >> 16) & 1) ? "EN" : "DIS"));
3461
3462 return;
3463}
3464
3465int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3466{
3467 unsigned long long msr;
3468 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003469
3470 if (!do_rapl)
3471 return 0;
3472
3473 /* RAPL counters are per package, so print only for 1st thread/package */
3474 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3475 return 0;
3476
3477 cpu = t->cpu_id;
3478 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003479 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003480 return -1;
3481 }
3482
3483 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3484 return -1;
3485
Len Brown96e47152017-01-21 02:26:00 -05003486 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3487 rapl_power_units, rapl_energy_units, rapl_time_units);
3488
Len Brown144b44b2013-11-09 00:30:16 -05003489 if (do_rapl & RAPL_PKG_POWER_INFO) {
3490
Len Brown889facb2012-11-08 00:48:57 -05003491 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3492 return -5;
3493
3494
Len Brownb7d8c142016-02-13 23:36:17 -05003495 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 -05003496 cpu, msr,
3497 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3498 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3499 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3500 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3501
Len Brown144b44b2013-11-09 00:30:16 -05003502 }
3503 if (do_rapl & RAPL_PKG) {
3504
Len Brown889facb2012-11-08 00:48:57 -05003505 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3506 return -9;
3507
Len Brownb7d8c142016-02-13 23:36:17 -05003508 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003509 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003510
3511 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003512 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003513 cpu,
3514 ((msr >> 47) & 1) ? "EN" : "DIS",
3515 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3516 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3517 ((msr >> 48) & 1) ? "EN" : "DIS");
3518 }
3519
Len Brown0b2bb692015-03-26 00:50:30 -04003520 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003521 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3522 return -6;
3523
Len Brownb7d8c142016-02-13 23:36:17 -05003524 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 -05003525 cpu, msr,
3526 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3527 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3528 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3529 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003530 }
3531 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003532 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3533 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003534 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003535 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003536
3537 print_power_limit_msr(cpu, msr, "DRAM Limit");
3538 }
Len Brown144b44b2013-11-09 00:30:16 -05003539 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003540 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3541 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003542
Len Brown96e47152017-01-21 02:26:00 -05003543 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003544 }
Jacob Pan91484942016-06-16 09:48:20 -07003545 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003546 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3547 return -9;
3548 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3549 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3550 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003551 }
3552 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003553 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3554 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003555
Len Brown96e47152017-01-21 02:26:00 -05003556 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003557
Len Brown96e47152017-01-21 02:26:00 -05003558 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3559 return -9;
3560 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3561 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3562 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003563 }
3564 return 0;
3565}
3566
Len Brownd7899442015-01-23 00:12:33 -05003567/*
3568 * SNB adds support for additional MSRs:
3569 *
3570 * MSR_PKG_C7_RESIDENCY 0x000003fa
3571 * MSR_CORE_C7_RESIDENCY 0x000003fe
3572 * MSR_PKG_C2_RESIDENCY 0x0000060d
3573 */
Len Brown103a8fe2010-10-22 23:53:03 -04003574
Len Brownd7899442015-01-23 00:12:33 -05003575int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003576{
3577 if (!genuine_intel)
3578 return 0;
3579
3580 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003581 case INTEL_FAM6_SANDYBRIDGE:
3582 case INTEL_FAM6_SANDYBRIDGE_X:
3583 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3584 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3585 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3586 case INTEL_FAM6_HASWELL_X: /* HSW */
3587 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3588 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3589 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3590 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3591 case INTEL_FAM6_BROADWELL_X: /* BDX */
3592 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3593 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3594 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3595 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3596 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3597 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3598 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003599 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003600 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003601 return 1;
3602 }
3603 return 0;
3604}
3605
Len Brownd7899442015-01-23 00:12:33 -05003606/*
3607 * HSW adds support for additional MSRs:
3608 *
Len Brown5a634262016-04-06 17:15:55 -04003609 * MSR_PKG_C8_RESIDENCY 0x00000630
3610 * MSR_PKG_C9_RESIDENCY 0x00000631
3611 * MSR_PKG_C10_RESIDENCY 0x00000632
3612 *
3613 * MSR_PKGC8_IRTL 0x00000633
3614 * MSR_PKGC9_IRTL 0x00000634
3615 * MSR_PKGC10_IRTL 0x00000635
3616 *
Len Brownd7899442015-01-23 00:12:33 -05003617 */
3618int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003619{
3620 if (!genuine_intel)
3621 return 0;
3622
3623 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003624 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3625 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3626 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3627 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3628 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3629 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3630 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003631 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003632 return 1;
3633 }
3634 return 0;
3635}
3636
Len Brown0b2bb692015-03-26 00:50:30 -04003637/*
3638 * SKL adds support for additional MSRS:
3639 *
3640 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3641 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3642 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3643 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3644 */
3645int has_skl_msrs(unsigned int family, unsigned int model)
3646{
3647 if (!genuine_intel)
3648 return 0;
3649
3650 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003651 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3652 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3653 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3654 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003655 return 1;
3656 }
3657 return 0;
3658}
3659
Len Brown144b44b2013-11-09 00:30:16 -05003660int is_slm(unsigned int family, unsigned int model)
3661{
3662 if (!genuine_intel)
3663 return 0;
3664 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003665 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3666 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003667 return 1;
3668 }
3669 return 0;
3670}
3671
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003672int is_knl(unsigned int family, unsigned int model)
3673{
3674 if (!genuine_intel)
3675 return 0;
3676 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003677 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003678 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003679 return 1;
3680 }
3681 return 0;
3682}
3683
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003684unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3685{
3686 if (is_knl(family, model))
3687 return 1024;
3688 return 1;
3689}
3690
Len Brown144b44b2013-11-09 00:30:16 -05003691#define SLM_BCLK_FREQS 5
3692double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3693
3694double slm_bclk(void)
3695{
3696 unsigned long long msr = 3;
3697 unsigned int i;
3698 double freq;
3699
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003700 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003701 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003702
3703 i = msr & 0xf;
3704 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003705 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003706 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003707 }
3708 freq = slm_freq_table[i];
3709
Len Brown96e47152017-01-21 02:26:00 -05003710 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003711 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003712
3713 return freq;
3714}
3715
Len Brown103a8fe2010-10-22 23:53:03 -04003716double discover_bclk(unsigned int family, unsigned int model)
3717{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003718 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003719 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003720 else if (is_slm(family, model))
3721 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003722 else
3723 return 133.33;
3724}
3725
Len Brown889facb2012-11-08 00:48:57 -05003726/*
3727 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3728 * the Thermal Control Circuit (TCC) activates.
3729 * This is usually equal to tjMax.
3730 *
3731 * Older processors do not have this MSR, so there we guess,
3732 * but also allow cmdline over-ride with -T.
3733 *
3734 * Several MSR temperature values are in units of degrees-C
3735 * below this value, including the Digital Thermal Sensor (DTS),
3736 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3737 */
3738int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3739{
3740 unsigned long long msr;
3741 unsigned int target_c_local;
3742 int cpu;
3743
3744 /* tcc_activation_temp is used only for dts or ptm */
3745 if (!(do_dts || do_ptm))
3746 return 0;
3747
3748 /* this is a per-package concept */
3749 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3750 return 0;
3751
3752 cpu = t->cpu_id;
3753 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003754 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003755 return -1;
3756 }
3757
3758 if (tcc_activation_temp_override != 0) {
3759 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003760 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003761 cpu, tcc_activation_temp);
3762 return 0;
3763 }
3764
3765 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003766 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003767 goto guess;
3768
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003769 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003770 goto guess;
3771
Jean Delvare34821242014-05-01 11:40:19 +02003772 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003773
Len Brown96e47152017-01-21 02:26:00 -05003774 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003775 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003776 cpu, msr, target_c_local);
3777
Jean Delvare34821242014-05-01 11:40:19 +02003778 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003779 goto guess;
3780
3781 tcc_activation_temp = target_c_local;
3782
3783 return 0;
3784
3785guess:
3786 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003787 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003788 cpu, tcc_activation_temp);
3789
3790 return 0;
3791}
Len Brown69807a62015-11-21 12:22:47 -05003792
Len Brownaa8d8cc2016-03-11 13:26:03 -05003793void decode_feature_control_msr(void)
3794{
3795 unsigned long long msr;
3796
3797 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3798 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3799 base_cpu, msr,
3800 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3801 msr & (1 << 18) ? "SGX" : "");
3802}
3803
Len Brown69807a62015-11-21 12:22:47 -05003804void decode_misc_enable_msr(void)
3805{
3806 unsigned long long msr;
3807
3808 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003809 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003810 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003811 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3812 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3813 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3814 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3815 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003816}
3817
Len Brown33148d62017-01-21 01:26:16 -05003818void decode_misc_feature_control(void)
3819{
3820 unsigned long long msr;
3821
3822 if (!has_misc_feature_control)
3823 return;
3824
3825 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
3826 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
3827 base_cpu, msr,
3828 msr & (0 << 0) ? "No-" : "",
3829 msr & (1 << 0) ? "No-" : "",
3830 msr & (2 << 0) ? "No-" : "",
3831 msr & (3 << 0) ? "No-" : "");
3832}
Len Brownf0057312015-12-03 01:35:36 -05003833/*
3834 * Decode MSR_MISC_PWR_MGMT
3835 *
3836 * Decode the bits according to the Nehalem documentation
3837 * bit[0] seems to continue to have same meaning going forward
3838 * bit[1] less so...
3839 */
3840void decode_misc_pwr_mgmt_msr(void)
3841{
3842 unsigned long long msr;
3843
3844 if (!do_nhm_platform_info)
3845 return;
3846
Len Browncf4cbe52017-01-01 13:08:33 -05003847 if (no_MSR_MISC_PWR_MGMT)
3848 return;
3849
Len Brownf0057312015-12-03 01:35:36 -05003850 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003851 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 -05003852 base_cpu, msr,
3853 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003854 msr & (1 << 1) ? "EN" : "DIS",
3855 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003856}
Len Brown71616c82017-01-07 22:37:48 -05003857/*
3858 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3859 *
3860 * This MSRs are present on Silvermont processors,
3861 * Intel Atom processor E3000 series (Baytrail), and friends.
3862 */
3863void decode_c6_demotion_policy_msr(void)
3864{
3865 unsigned long long msr;
3866
3867 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3868 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3869 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3870
3871 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3872 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3873 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3874}
Len Brown7f5c2582015-12-01 01:36:39 -05003875
Len Brownfcd17212015-03-23 20:29:09 -04003876void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003877{
Len Brown61a87ba2015-11-23 02:30:51 -05003878 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003879 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003880 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003881
3882 eax = ebx = ecx = edx = 0;
3883
Len Brown5aea2f72016-03-13 03:14:35 -04003884 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003885
3886 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3887 genuine_intel = 1;
3888
Len Brown96e47152017-01-21 02:26:00 -05003889 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003890 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003891 (char *)&ebx, (char *)&edx, (char *)&ecx);
3892
Len Brown5aea2f72016-03-13 03:14:35 -04003893 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003894 family = (fms >> 8) & 0xf;
3895 model = (fms >> 4) & 0xf;
3896 stepping = fms & 0xf;
3897 if (family == 6 || family == 0xf)
3898 model += ((fms >> 16) & 0xf) << 4;
3899
Len Brown96e47152017-01-21 02:26:00 -05003900 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05003901 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003902 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003903 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003904 ecx & (1 << 0) ? "SSE3" : "-",
3905 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003906 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003907 ecx & (1 << 7) ? "EIST" : "-",
3908 ecx & (1 << 8) ? "TM2" : "-",
3909 edx & (1 << 4) ? "TSC" : "-",
3910 edx & (1 << 5) ? "MSR" : "-",
3911 edx & (1 << 22) ? "ACPI-TM" : "-",
3912 edx & (1 << 29) ? "TM" : "-");
3913 }
Len Brown103a8fe2010-10-22 23:53:03 -04003914
Josh Triplettb2c95d92013-08-20 17:20:18 -07003915 if (!(edx & (1 << 5)))
3916 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003917
3918 /*
3919 * check max extended function levels of CPUID.
3920 * This is needed to check for invariant TSC.
3921 * This check is valid for both Intel and AMD.
3922 */
3923 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003924 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003925
Len Brown61a87ba2015-11-23 02:30:51 -05003926 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003927
Len Brownd7899442015-01-23 00:12:33 -05003928 /*
3929 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3930 * this check is valid for both Intel and AMD
3931 */
Len Brown5aea2f72016-03-13 03:14:35 -04003932 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003933 has_invariant_tsc = edx & (1 << 8);
3934 }
Len Brown103a8fe2010-10-22 23:53:03 -04003935
3936 /*
3937 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3938 * this check is valid for both Intel and AMD
3939 */
3940
Len Brown5aea2f72016-03-13 03:14:35 -04003941 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003942 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003943 if (has_aperf) {
3944 BIC_PRESENT(BIC_Avg_MHz);
3945 BIC_PRESENT(BIC_Busy);
3946 BIC_PRESENT(BIC_Bzy_MHz);
3947 }
Len Brown889facb2012-11-08 00:48:57 -05003948 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003949 if (do_dts)
3950 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05003951 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05003952 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003953 if (do_ptm)
3954 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003955 has_hwp = eax & (1 << 7);
3956 has_hwp_notify = eax & (1 << 8);
3957 has_hwp_activity_window = eax & (1 << 9);
3958 has_hwp_epp = eax & (1 << 10);
3959 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003960 has_epb = ecx & (1 << 3);
3961
Len Brown96e47152017-01-21 02:26:00 -05003962 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05003963 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003964 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3965 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05003966 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05003967 do_dts ? "" : "No-",
3968 do_ptm ? "" : "No-",
3969 has_hwp ? "" : "No-",
3970 has_hwp_notify ? "" : "No-",
3971 has_hwp_activity_window ? "" : "No-",
3972 has_hwp_epp ? "" : "No-",
3973 has_hwp_pkg ? "" : "No-",
3974 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003975
Len Brown96e47152017-01-21 02:26:00 -05003976 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05003977 decode_misc_enable_msr();
3978
Len Brown33148d62017-01-21 01:26:16 -05003979
Len Brown96e47152017-01-21 02:26:00 -05003980 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003981 int has_sgx;
3982
3983 ecx = 0;
3984
3985 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3986
3987 has_sgx = ebx & (1 << 2);
3988 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3989
3990 if (has_sgx)
3991 decode_feature_control_msr();
3992 }
3993
Len Brown61a87ba2015-11-23 02:30:51 -05003994 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003995 unsigned int eax_crystal;
3996 unsigned int ebx_tsc;
3997
3998 /*
3999 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4000 */
4001 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004002 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004003
4004 if (ebx_tsc != 0) {
4005
Len Brown96e47152017-01-21 02:26:00 -05004006 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004007 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004008 eax_crystal, ebx_tsc, crystal_hz);
4009
4010 if (crystal_hz == 0)
4011 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004012 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4013 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4014 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4015 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004016 crystal_hz = 24000000; /* 24.0 MHz */
4017 break;
Len Brown869ce692016-06-16 23:22:37 -04004018 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05004019 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004020 crystal_hz = 25000000; /* 25.0 MHz */
4021 break;
Len Brown869ce692016-06-16 23:22:37 -04004022 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004023 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004024 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004025 break;
4026 default:
4027 crystal_hz = 0;
4028 }
4029
4030 if (crystal_hz) {
4031 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004032 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004033 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004034 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4035 }
4036 }
4037 }
Len Brown61a87ba2015-11-23 02:30:51 -05004038 if (max_level >= 0x16) {
4039 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4040
4041 /*
4042 * CPUID 16H Base MHz, Max MHz, Bus MHz
4043 */
4044 base_mhz = max_mhz = bus_mhz = edx = 0;
4045
Len Brown5aea2f72016-03-13 03:14:35 -04004046 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004047 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004048 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004049 base_mhz, max_mhz, bus_mhz);
4050 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004051
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004052 if (has_aperf)
4053 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4054
Len Brown812db3f2017-02-10 00:25:41 -05004055 BIC_PRESENT(BIC_IRQ);
4056 BIC_PRESENT(BIC_TSC_MHz);
4057
4058 if (probe_nhm_msrs(family, model)) {
4059 do_nhm_platform_info = 1;
4060 BIC_PRESENT(BIC_CPU_c1);
4061 BIC_PRESENT(BIC_CPU_c3);
4062 BIC_PRESENT(BIC_CPU_c6);
4063 BIC_PRESENT(BIC_SMI);
4064 }
Len Brownd7899442015-01-23 00:12:33 -05004065 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004066
4067 if (do_snb_cstates)
4068 BIC_PRESENT(BIC_CPU_c7);
4069
Len Brown5a634262016-04-06 17:15:55 -04004070 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004071 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4072 BIC_PRESENT(BIC_Pkgpc2);
4073 if (pkg_cstate_limit >= PCL__3)
4074 BIC_PRESENT(BIC_Pkgpc3);
4075 if (pkg_cstate_limit >= PCL__6)
4076 BIC_PRESENT(BIC_Pkgpc6);
4077 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4078 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004079 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004080 BIC_NOT_PRESENT(BIC_Pkgpc2);
4081 BIC_NOT_PRESENT(BIC_Pkgpc3);
4082 BIC_PRESENT(BIC_Pkgpc6);
4083 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004084 BIC_PRESENT(BIC_Mod_c6);
4085 use_c1_residency_msr = 1;
4086 }
Len Brown7170a372017-01-27 02:13:27 -05004087 if (is_dnv(family, model)) {
4088 BIC_PRESENT(BIC_CPU_c1);
4089 BIC_NOT_PRESENT(BIC_CPU_c3);
4090 BIC_NOT_PRESENT(BIC_Pkgpc3);
4091 BIC_NOT_PRESENT(BIC_CPU_c7);
4092 BIC_NOT_PRESENT(BIC_Pkgpc7);
4093 use_c1_residency_msr = 1;
4094 }
Len Brown34c761972017-01-27 02:36:41 -05004095 if (is_skx(family, model)) {
4096 BIC_NOT_PRESENT(BIC_CPU_c3);
4097 BIC_NOT_PRESENT(BIC_Pkgpc3);
4098 BIC_NOT_PRESENT(BIC_CPU_c7);
4099 BIC_NOT_PRESENT(BIC_Pkgpc7);
4100 }
Len Brownade0eba2017-02-10 01:56:47 -05004101 if (is_bdx(family, model)) {
4102 BIC_NOT_PRESENT(BIC_CPU_c7);
4103 BIC_NOT_PRESENT(BIC_Pkgpc7);
4104 }
Len Brown0f47c082017-01-27 00:50:45 -05004105 if (has_hsw_msrs(family, model)) {
4106 BIC_PRESENT(BIC_Pkgpc8);
4107 BIC_PRESENT(BIC_Pkgpc9);
4108 BIC_PRESENT(BIC_Pkgpc10);
4109 }
Len Brown5a634262016-04-06 17:15:55 -04004110 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04004111 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05004112 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004113 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004114
Len Brown96e47152017-01-21 02:26:00 -05004115 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004116 decode_misc_pwr_mgmt_msr();
4117
Len Brown96e47152017-01-21 02:26:00 -05004118 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004119 decode_c6_demotion_policy_msr();
4120
Len Brown889facb2012-11-08 00:48:57 -05004121 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004122 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004123
Len Brown96e47152017-01-21 02:26:00 -05004124 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004125 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004126
Len Brown41618e62017-02-09 18:25:22 -05004127 if (!quiet)
4128 dump_sysfs_cstate_config();
4129
Len Browna2b7b742015-09-26 00:12:38 -04004130 if (has_skl_msrs(family, model))
4131 calculate_tsc_tweak();
4132
Len Brown812db3f2017-02-10 00:25:41 -05004133 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4134 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004135
Len Brown812db3f2017-02-10 00:25:41 -05004136 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4137 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004138
Len Brown96e47152017-01-21 02:26:00 -05004139 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004140 decode_misc_feature_control();
4141
Len Brown889facb2012-11-08 00:48:57 -05004142 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004143}
4144
Len Brownd8af6f52015-02-10 01:56:38 -05004145void help()
Len Brown103a8fe2010-10-22 23:53:03 -04004146{
Len Brownb7d8c142016-02-13 23:36:17 -05004147 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05004148 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
4149 "\n"
4150 "Turbostat forks the specified COMMAND and prints statistics\n"
4151 "when COMMAND completes.\n"
4152 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
4153 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05004154 "--add add a counter\n"
4155 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brown1ef7d212017-02-10 23:54:15 -05004156 "--cpu cpu-set limit output to summary plus cpu-set cpu-set\n"
Len Brown96e47152017-01-21 02:26:00 -05004157 "--quiet skip decoding system configuration header\n"
Len Brownd8af6f52015-02-10 01:56:38 -05004158 "--interval sec Override default 5-second measurement interval\n"
4159 "--help print this help message\n"
Len Brownc8ade362017-02-15 17:15:11 -05004160 "--list list column headers only\n"
Len Brownb7d8c142016-02-13 23:36:17 -05004161 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05004162 "--version print version information\n"
4163 "\n"
4164 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04004165}
4166
4167
4168/*
4169 * in /dev/cpu/ return success for names that are numbers
4170 * ie. filter out ".", "..", "microcode".
4171 */
4172int dir_filter(const struct dirent *dirp)
4173{
4174 if (isdigit(dirp->d_name[0]))
4175 return 1;
4176 else
4177 return 0;
4178}
4179
4180int open_dev_cpu_msr(int dummy1)
4181{
4182 return 0;
4183}
4184
Len Brownc98d5d92012-06-04 00:56:40 -04004185void topology_probe()
4186{
4187 int i;
4188 int max_core_id = 0;
4189 int max_package_id = 0;
4190 int max_siblings = 0;
4191 struct cpu_topology {
4192 int core_id;
4193 int physical_package_id;
4194 } *cpus;
4195
4196 /* Initialize num_cpus, max_cpu_num */
4197 topo.num_cpus = 0;
4198 topo.max_cpu_num = 0;
4199 for_all_proc_cpus(count_cpus);
4200 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004201 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004202
Len Brownd8af6f52015-02-10 01:56:38 -05004203 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004204 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004205
4206 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004207 if (cpus == NULL)
4208 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004209
4210 /*
4211 * Allocate and initialize cpu_present_set
4212 */
4213 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004214 if (cpu_present_set == NULL)
4215 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004216 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4217 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4218 for_all_proc_cpus(mark_cpu_present);
4219
4220 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004221 * Validate that all cpus in cpu_subset are also in cpu_present_set
4222 */
4223 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4224 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4225 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4226 err(1, "cpu%d not present", i);
4227 }
4228
4229 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004230 * Allocate and initialize cpu_affinity_set
4231 */
4232 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004233 if (cpu_affinity_set == NULL)
4234 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004235 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4236 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4237
4238
4239 /*
4240 * For online cpus
4241 * find max_core_id, max_package_id
4242 */
4243 for (i = 0; i <= topo.max_cpu_num; ++i) {
4244 int siblings;
4245
4246 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004247 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004248 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004249 continue;
4250 }
4251 cpus[i].core_id = get_core_id(i);
4252 if (cpus[i].core_id > max_core_id)
4253 max_core_id = cpus[i].core_id;
4254
4255 cpus[i].physical_package_id = get_physical_package_id(i);
4256 if (cpus[i].physical_package_id > max_package_id)
4257 max_package_id = cpus[i].physical_package_id;
4258
4259 siblings = get_num_ht_siblings(i);
4260 if (siblings > max_siblings)
4261 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004262 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004263 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004264 i, cpus[i].physical_package_id, cpus[i].core_id);
4265 }
4266 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004267 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004268 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004269 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004270 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004271 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004272
4273 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004274 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004275 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004276 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05004277 if (debug && !summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004278 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004279
4280 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004281 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004282 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004283
4284 free(cpus);
4285}
4286
4287void
4288allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4289{
4290 int i;
4291
4292 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004293 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004294 if (*t == NULL)
4295 goto error;
4296
4297 for (i = 0; i < topo.num_threads_per_core *
4298 topo.num_cores_per_pkg * topo.num_packages; i++)
4299 (*t)[i].cpu_id = -1;
4300
4301 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004302 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004303 if (*c == NULL)
4304 goto error;
4305
4306 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4307 (*c)[i].core_id = -1;
4308
Len Brown678a3bd2017-02-09 22:22:13 -05004309 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004310 if (*p == NULL)
4311 goto error;
4312
4313 for (i = 0; i < topo.num_packages; i++)
4314 (*p)[i].package_id = i;
4315
4316 return;
4317error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004318 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004319}
4320/*
4321 * init_counter()
4322 *
4323 * set cpu_id, core_num, pkg_num
4324 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4325 *
4326 * increment topo.num_cores when 1st core in pkg seen
4327 */
4328void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4329 struct pkg_data *pkg_base, int thread_num, int core_num,
4330 int pkg_num, int cpu_id)
4331{
4332 struct thread_data *t;
4333 struct core_data *c;
4334 struct pkg_data *p;
4335
4336 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4337 c = GET_CORE(core_base, core_num, pkg_num);
4338 p = GET_PKG(pkg_base, pkg_num);
4339
4340 t->cpu_id = cpu_id;
4341 if (thread_num == 0) {
4342 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4343 if (cpu_is_first_core_in_package(cpu_id))
4344 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4345 }
4346
4347 c->core_id = core_num;
4348 p->package_id = pkg_num;
4349}
4350
4351
4352int initialize_counters(int cpu_id)
4353{
4354 int my_thread_id, my_core_id, my_package_id;
4355
4356 my_package_id = get_physical_package_id(cpu_id);
4357 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004358 my_thread_id = get_cpu_position_in_core(cpu_id);
4359 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004360 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004361
4362 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4363 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4364 return 0;
4365}
4366
4367void allocate_output_buffer()
4368{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004369 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004370 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004371 if (outp == NULL)
4372 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004373}
Len Brown36229892016-02-26 20:51:02 -05004374void allocate_fd_percpu(void)
4375{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004376 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004377 if (fd_percpu == NULL)
4378 err(-1, "calloc fd_percpu");
4379}
Len Brown562a2d32016-02-26 23:48:05 -05004380void allocate_irq_buffers(void)
4381{
4382 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4383 if (irq_column_2_cpu == NULL)
4384 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004385
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004386 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004387 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004388 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004389}
Len Brownc98d5d92012-06-04 00:56:40 -04004390void setup_all_buffers(void)
4391{
4392 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004393 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004394 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004395 allocate_counters(&thread_even, &core_even, &package_even);
4396 allocate_counters(&thread_odd, &core_odd, &package_odd);
4397 allocate_output_buffer();
4398 for_all_proc_cpus(initialize_counters);
4399}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004400
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004401void set_base_cpu(void)
4402{
4403 base_cpu = sched_getcpu();
4404 if (base_cpu < 0)
4405 err(-ENODEV, "No valid cpus found");
4406
4407 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004408 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004409}
4410
Len Brown103a8fe2010-10-22 23:53:03 -04004411void turbostat_init()
4412{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004413 setup_all_buffers();
4414 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004415 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004416 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004417 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004418
Len Brown103a8fe2010-10-22 23:53:03 -04004419
Len Brown96e47152017-01-21 02:26:00 -05004420 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004421 for_all_cpus(print_hwp, ODD_COUNTERS);
4422
Len Brown96e47152017-01-21 02:26:00 -05004423 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004424 for_all_cpus(print_epb, ODD_COUNTERS);
4425
Len Brown96e47152017-01-21 02:26:00 -05004426 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004427 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4428
Len Brown96e47152017-01-21 02:26:00 -05004429 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004430 for_all_cpus(print_rapl, ODD_COUNTERS);
4431
4432 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4433
Len Brown96e47152017-01-21 02:26:00 -05004434 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004435 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004436
Len Brown96e47152017-01-21 02:26:00 -05004437 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004438 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004439}
4440
4441int fork_it(char **argv)
4442{
Len Brown103a8fe2010-10-22 23:53:03 -04004443 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004444 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004445
Len Brown218f0e82017-02-14 22:07:52 -05004446 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004447 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4448 if (status)
4449 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004450 /* clear affinity side-effect of get_counters() */
4451 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004452 gettimeofday(&tv_even, (struct timezone *)NULL);
4453
4454 child_pid = fork();
4455 if (!child_pid) {
4456 /* child */
4457 execvp(argv[0], argv);
4458 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004459
4460 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004461 if (child_pid == -1)
4462 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004463
4464 signal(SIGINT, SIG_IGN);
4465 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004466 if (waitpid(child_pid, &status, 0) == -1)
4467 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004468 }
Len Brownc98d5d92012-06-04 00:56:40 -04004469 /*
4470 * n.b. fork_it() does not check for errors from for_all_cpus()
4471 * because re-starting is problematic when forking
4472 */
Len Brown218f0e82017-02-14 22:07:52 -05004473 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004474 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004475 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004476 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004477 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4478 fprintf(outf, "%s: Counter reset detected\n", progname);
4479 else {
4480 compute_average(EVEN_COUNTERS);
4481 format_all_counters(EVEN_COUNTERS);
4482 }
Len Brown103a8fe2010-10-22 23:53:03 -04004483
Len Brownb7d8c142016-02-13 23:36:17 -05004484 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4485
4486 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004487
Len Brownd91bb172012-11-01 00:08:19 -04004488 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004489}
4490
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004491int get_and_dump_counters(void)
4492{
4493 int status;
4494
Len Brown218f0e82017-02-14 22:07:52 -05004495 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004496 status = for_all_cpus(get_counters, ODD_COUNTERS);
4497 if (status)
4498 return status;
4499
4500 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4501 if (status)
4502 return status;
4503
Len Brownb7d8c142016-02-13 23:36:17 -05004504 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004505
4506 return status;
4507}
4508
Len Brownd8af6f52015-02-10 01:56:38 -05004509void print_version() {
Len Brown0539ba12017-02-10 00:27:20 -05004510 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004511 " - Len Brown <lenb@kernel.org>\n");
4512}
4513
Len Brown495c76542017-02-08 02:41:51 -05004514int add_counter(unsigned int msr_num, char *path, char *name,
4515 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004516 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004517{
4518 struct msr_counter *msrp;
4519
4520 msrp = calloc(1, sizeof(struct msr_counter));
4521 if (msrp == NULL) {
4522 perror("calloc");
4523 exit(1);
4524 }
4525
4526 msrp->msr_num = msr_num;
4527 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004528 if (path)
4529 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004530 msrp->width = width;
4531 msrp->type = type;
4532 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004533 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004534
4535 switch (scope) {
4536
4537 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004538 msrp->next = sys.tp;
4539 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004540 sys.added_thread_counters++;
4541 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4542 fprintf(stderr, "exceeded max %d added thread counters\n",
4543 MAX_ADDED_COUNTERS);
4544 exit(-1);
4545 }
Len Brown388e9c82016-12-22 23:57:55 -05004546 break;
4547
4548 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004549 msrp->next = sys.cp;
4550 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004551 sys.added_core_counters++;
4552 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4553 fprintf(stderr, "exceeded max %d added core counters\n",
4554 MAX_ADDED_COUNTERS);
4555 exit(-1);
4556 }
Len Brown388e9c82016-12-22 23:57:55 -05004557 break;
4558
4559 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004560 msrp->next = sys.pp;
4561 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004562 sys.added_package_counters++;
4563 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4564 fprintf(stderr, "exceeded max %d added package counters\n",
4565 MAX_ADDED_COUNTERS);
4566 exit(-1);
4567 }
Len Brown388e9c82016-12-22 23:57:55 -05004568 break;
4569 }
4570
4571 return 0;
4572}
4573
4574void parse_add_command(char *add_command)
4575{
4576 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004577 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004578 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004579 int width = 64;
4580 int fail = 0;
4581 enum counter_scope scope = SCOPE_CPU;
4582 enum counter_type type = COUNTER_CYCLES;
4583 enum counter_format format = FORMAT_DELTA;
4584
4585 while (add_command) {
4586
4587 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4588 goto next;
4589
4590 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4591 goto next;
4592
Len Brown495c76542017-02-08 02:41:51 -05004593 if (*add_command == '/') {
4594 path = add_command;
4595 goto next;
4596 }
4597
Len Brown388e9c82016-12-22 23:57:55 -05004598 if (sscanf(add_command, "u%d", &width) == 1) {
4599 if ((width == 32) || (width == 64))
4600 goto next;
4601 width = 64;
4602 }
4603 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4604 scope = SCOPE_CPU;
4605 goto next;
4606 }
4607 if (!strncmp(add_command, "core", strlen("core"))) {
4608 scope = SCOPE_CORE;
4609 goto next;
4610 }
4611 if (!strncmp(add_command, "package", strlen("package"))) {
4612 scope = SCOPE_PACKAGE;
4613 goto next;
4614 }
4615 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4616 type = COUNTER_CYCLES;
4617 goto next;
4618 }
4619 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4620 type = COUNTER_SECONDS;
4621 goto next;
4622 }
Len Brown41618e62017-02-09 18:25:22 -05004623 if (!strncmp(add_command, "usec", strlen("usec"))) {
4624 type = COUNTER_USEC;
4625 goto next;
4626 }
Len Brown388e9c82016-12-22 23:57:55 -05004627 if (!strncmp(add_command, "raw", strlen("raw"))) {
4628 format = FORMAT_RAW;
4629 goto next;
4630 }
4631 if (!strncmp(add_command, "delta", strlen("delta"))) {
4632 format = FORMAT_DELTA;
4633 goto next;
4634 }
4635 if (!strncmp(add_command, "percent", strlen("percent"))) {
4636 format = FORMAT_PERCENT;
4637 goto next;
4638 }
4639
4640 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4641 char *eos;
4642
4643 eos = strchr(name_buffer, ',');
4644 if (eos)
4645 *eos = '\0';
4646 goto next;
4647 }
4648
4649next:
4650 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004651 if (add_command) {
4652 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004653 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004654 }
Len Brown388e9c82016-12-22 23:57:55 -05004655
4656 }
Len Brown495c76542017-02-08 02:41:51 -05004657 if ((msr_num == 0) && (path == NULL)) {
4658 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004659 fail++;
4660 }
4661
4662 /* generate default column header */
4663 if (*name_buffer == '\0') {
4664 if (format == FORMAT_RAW) {
4665 if (width == 32)
4666 sprintf(name_buffer, "msr%d", msr_num);
4667 else
4668 sprintf(name_buffer, "MSR%d", msr_num);
4669 } else if (format == FORMAT_DELTA) {
4670 if (width == 32)
4671 sprintf(name_buffer, "cnt%d", msr_num);
4672 else
4673 sprintf(name_buffer, "CNT%d", msr_num);
4674 } else if (format == FORMAT_PERCENT) {
4675 if (width == 32)
4676 sprintf(name_buffer, "msr%d%%", msr_num);
4677 else
4678 sprintf(name_buffer, "MSR%d%%", msr_num);
4679 }
4680 }
4681
Len Brown41618e62017-02-09 18:25:22 -05004682 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05004683 fail++;
4684
4685 if (fail) {
4686 help();
4687 exit(1);
4688 }
4689}
Len Brown41618e62017-02-09 18:25:22 -05004690
4691void probe_sysfs(void)
4692{
4693 char path[64];
4694 char name_buf[16];
4695 FILE *input;
4696 int state;
4697 char *sp;
4698
4699 if (!DO_BIC(BIC_sysfs))
4700 return;
4701
4702 for (state = 10; state > 0; --state) {
4703
4704 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4705 base_cpu, state);
4706 input = fopen(path, "r");
4707 if (input == NULL)
4708 continue;
4709 fgets(name_buf, sizeof(name_buf), input);
4710
4711 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4712 sp = strchr(name_buf, '-');
4713 if (!sp)
4714 sp = strchrnul(name_buf, '\n');
4715 *sp = '%';
4716 *(sp + 1) = '\0';
4717
4718 fclose(input);
4719
4720 sprintf(path, "cpuidle/state%d/time", state);
4721
4722 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4723 FORMAT_PERCENT, SYSFS_PERCPU);
4724 }
4725
4726 for (state = 10; state > 0; --state) {
4727
4728 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4729 base_cpu, state);
4730 input = fopen(path, "r");
4731 if (input == NULL)
4732 continue;
4733 fgets(name_buf, sizeof(name_buf), input);
4734 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4735 sp = strchr(name_buf, '-');
4736 if (!sp)
4737 sp = strchrnul(name_buf, '\n');
4738 *sp = '\0';
4739 fclose(input);
4740
4741 sprintf(path, "cpuidle/state%d/usage", state);
4742
4743 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4744 FORMAT_DELTA, SYSFS_PERCPU);
4745 }
4746
4747}
4748
Len Brown1ef7d212017-02-10 23:54:15 -05004749
4750/*
4751 * parse cpuset with following syntax
4752 * 1,2,4..6,8-10 and set bits in cpu_subset
4753 */
4754void parse_cpu_command(char *optarg)
4755{
4756 unsigned int start, end;
4757 char *next;
4758
4759 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
4760 if (cpu_subset == NULL)
4761 err(3, "CPU_ALLOC");
4762 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
4763
4764 CPU_ZERO_S(cpu_subset_size, cpu_subset);
4765
4766 next = optarg;
4767
4768 while (next && *next) {
4769
4770 if (*next == '-') /* no negative cpu numbers */
4771 goto error;
4772
4773 start = strtoul(next, &next, 10);
4774
4775 if (start >= CPU_SUBSET_MAXCPUS)
4776 goto error;
4777 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4778
4779 if (*next == '\0')
4780 break;
4781
4782 if (*next == ',') {
4783 next += 1;
4784 continue;
4785 }
4786
4787 if (*next == '-') {
4788 next += 1; /* start range */
4789 } else if (*next == '.') {
4790 next += 1;
4791 if (*next == '.')
4792 next += 1; /* start range */
4793 else
4794 goto error;
4795 }
4796
4797 end = strtoul(next, &next, 10);
4798 if (end <= start)
4799 goto error;
4800
4801 while (++start <= end) {
4802 if (start >= CPU_SUBSET_MAXCPUS)
4803 goto error;
4804 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4805 }
4806
4807 if (*next == ',')
4808 next += 1;
4809 else if (*next != '\0')
4810 goto error;
4811 }
4812
4813 return;
4814
4815error:
4816 fprintf(stderr, "'--cpu %s' malformed\n", optarg);
4817 exit(-1);
4818}
4819
Len Brown812db3f2017-02-10 00:25:41 -05004820/*
4821 * HIDE_LIST - hide this list of counters, show the rest [default]
4822 * SHOW_LIST - show this list of counters, hide the rest
4823 */
4824enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4825
4826int shown;
4827/*
4828 * parse_show_hide() - process cmdline to set default counter action
4829 */
4830void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4831{
4832 /*
4833 * --show: show only those specified
4834 * The 1st invocation will clear and replace the enabled mask
4835 * subsequent invocations can add to it.
4836 */
4837 if (new_mode == SHOW_LIST) {
4838 if (shown == 0)
4839 bic_enabled = bic_lookup(optarg);
4840 else
4841 bic_enabled |= bic_lookup(optarg);
4842 shown = 1;
4843
4844 return;
4845 }
4846
4847 /*
4848 * --hide: do not show those specified
4849 * multiple invocations simply clear more bits in enabled mask
4850 */
4851 bic_enabled &= ~bic_lookup(optarg);
Len Brown41618e62017-02-09 18:25:22 -05004852
Len Brown812db3f2017-02-10 00:25:41 -05004853}
4854
Len Brown103a8fe2010-10-22 23:53:03 -04004855void cmdline(int argc, char **argv)
4856{
4857 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004858 int option_index = 0;
4859 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004860 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05004861 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05004862 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05004863 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brownd8af6f52015-02-10 01:56:38 -05004864 {"interval", required_argument, 0, 'i'},
4865 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004866 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004867 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05004868 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05004869 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004870 {"Package", no_argument, 0, 'p'},
4871 {"processor", no_argument, 0, 'p'},
Len Brown96e47152017-01-21 02:26:00 -05004872 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05004873 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004874 {"Summary", no_argument, 0, 'S'},
4875 {"TCC", required_argument, 0, 'T'},
4876 {"version", no_argument, 0, 'v' },
4877 {0, 0, 0, 0 }
4878 };
Len Brown103a8fe2010-10-22 23:53:03 -04004879
4880 progname = argv[0];
4881
Len Brown96e47152017-01-21 02:26:00 -05004882 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpqST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004883 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004884 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004885 case 'a':
4886 parse_add_command(optarg);
4887 break;
Len Brown1ef7d212017-02-10 23:54:15 -05004888 case 'c':
4889 parse_cpu_command(optarg);
4890 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004891 case 'D':
4892 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004893 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004894 case 'd':
4895 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004896 break;
Len Brown812db3f2017-02-10 00:25:41 -05004897 case 'H':
4898 parse_show_hide(optarg, HIDE_LIST);
4899 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004900 case 'h':
4901 default:
4902 help();
4903 exit(1);
4904 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004905 {
4906 double interval = strtod(optarg, NULL);
4907
4908 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004909 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004910 interval);
4911 exit(2);
4912 }
4913
4914 interval_ts.tv_sec = interval;
4915 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4916 }
Len Brown889facb2012-11-08 00:48:57 -05004917 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004918 case 'J':
4919 rapl_joules++;
4920 break;
Len Brownc8ade362017-02-15 17:15:11 -05004921 case 'l':
4922 list_header_only++;
4923 quiet++;
4924 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004925 case 'o':
4926 outf = fopen_or_die(optarg, "w");
4927 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004928 case 'P':
4929 show_pkg_only++;
4930 break;
4931 case 'p':
4932 show_core_only++;
4933 break;
Len Brown96e47152017-01-21 02:26:00 -05004934 case 'q':
4935 quiet = 1;
4936 break;
Len Brown812db3f2017-02-10 00:25:41 -05004937 case 's':
4938 parse_show_hide(optarg, SHOW_LIST);
4939 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004940 case 'S':
4941 summary_only++;
4942 break;
4943 case 'T':
4944 tcc_activation_temp_override = atoi(optarg);
4945 break;
4946 case 'v':
4947 print_version();
4948 exit(0);
4949 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004950 }
4951 }
4952}
4953
4954int main(int argc, char **argv)
4955{
Len Brownb7d8c142016-02-13 23:36:17 -05004956 outf = stderr;
4957
Len Brown103a8fe2010-10-22 23:53:03 -04004958 cmdline(argc, argv);
4959
Len Brown96e47152017-01-21 02:26:00 -05004960 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05004961 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004962
Len Brown41618e62017-02-09 18:25:22 -05004963 probe_sysfs();
4964
Len Brown103a8fe2010-10-22 23:53:03 -04004965 turbostat_init();
4966
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004967 /* dump counters and exit */
4968 if (dump_only)
4969 return get_and_dump_counters();
4970
Len Brownc8ade362017-02-15 17:15:11 -05004971 /* list header and exit */
4972 if (list_header_only) {
4973 print_header(",");
4974 flush_output_stdout();
4975 return 0;
4976 }
4977
Len Brown103a8fe2010-10-22 23:53:03 -04004978 /*
4979 * if any params left, it must be a command to fork
4980 */
4981 if (argc - optind)
4982 return fork_it(argv + optind);
4983 else
4984 turbostat_loop();
4985
4986 return 0;
4987}