blob: 2c674ad5ab803950a8ce1380560a5c7a23a7c843 [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
Len Browndd778a52017-02-21 23:21:13 -0500437#define MAX_DEFERRED 16
438char *deferred_skip_names[MAX_DEFERRED];
439int deferred_skip_index;
440
441/*
442 * HIDE_LIST - hide this list of counters, show the rest [default]
443 * SHOW_LIST - show this list of counters, hide the rest
444 */
445enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
446
447void help(void)
448{
449 fprintf(outf,
450 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
451 "\n"
452 "Turbostat forks the specified COMMAND and prints statistics\n"
453 "when COMMAND completes.\n"
454 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
455 "to print statistics, until interrupted.\n"
456 "--add add a counter\n"
457 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
458 "--cpu cpu-set limit output to summary plus cpu-set:\n"
459 " {core | package | j,k,l..m,n-p }\n"
460 "--quiet skip decoding system configuration header\n"
461 "--interval sec Override default 5-second measurement interval\n"
462 "--help print this help message\n"
463 "--list list column headers only\n"
464 "--out file create or truncate \"file\" for all output\n"
465 "--version print version information\n"
466 "\n"
467 "For more help, run \"man turbostat\"\n");
468}
469
Len Brown812db3f2017-02-10 00:25:41 -0500470/*
471 * bic_lookup
472 * for all the strings in comma separate name_list,
473 * set the approprate bit in return value.
474 */
Len Browndd778a52017-02-21 23:21:13 -0500475unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500476{
477 int i;
478 unsigned long long retval = 0;
479
480 while (name_list) {
481 char *comma;
482
483 comma = strchr(name_list, ',');
484
485 if (comma)
486 *comma = '\0';
487
488 for (i = 0; i < MAX_BIC; ++i) {
489 if (!strcmp(name_list, bic[i].name)) {
490 retval |= (1ULL << i);
491 break;
492 }
493 }
494 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500495 if (mode == SHOW_LIST) {
496 fprintf(stderr, "Invalid counter name: %s\n", name_list);
497 exit(-1);
498 }
499 deferred_skip_names[deferred_skip_index++] = name_list;
500 if (debug)
501 fprintf(stderr, "deferred \"%s\"\n", name_list);
502 if (deferred_skip_index >= MAX_DEFERRED) {
503 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
504 MAX_DEFERRED, name_list);
505 help();
506 exit(1);
507 }
Len Brown812db3f2017-02-10 00:25:41 -0500508 }
509
510 name_list = comma;
511 if (name_list)
512 name_list++;
513
514 }
515 return retval;
516}
Len Brownfc04cc62014-02-06 00:55:19 -0500517
Len Browndd778a52017-02-21 23:21:13 -0500518
Len Brownc8ade362017-02-15 17:15:11 -0500519void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400520{
Len Brown388e9c82016-12-22 23:57:55 -0500521 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500522 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500523
Len Brown812db3f2017-02-10 00:25:41 -0500524 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500525 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500526 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500527 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500528 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500529 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500530 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500531 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500532 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500533 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500534 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500535 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500536 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500537 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500538
Len Brown0de6c0d2017-02-15 21:45:40 -0500539 if (DO_BIC(BIC_IRQ)) {
540 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500541 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500542 else
Len Brown6168c2e2017-02-16 23:07:51 -0500543 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500544 }
545
Len Brown812db3f2017-02-10 00:25:41 -0500546 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500547 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500548
Len Brown388e9c82016-12-22 23:57:55 -0500549 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500550
Len Brown388e9c82016-12-22 23:57:55 -0500551 if (mp->format == FORMAT_RAW) {
552 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500553 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500554 else
Len Browndd778a52017-02-21 23:21:13 -0500555 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500556 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500557 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500558 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500559 else
Len Browndd778a52017-02-21 23:21:13 -0500560 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500561 }
562 }
563
Len Brown41618e62017-02-09 18:25:22 -0500564 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500565 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500566 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500567 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500568 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500569 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500570 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500571 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500572
Len Brown0539ba12017-02-10 00:27:20 -0500573 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500574 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500575
Len Brown812db3f2017-02-10 00:25:41 -0500576 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500577 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500578
579 for (mp = sys.cp; mp; mp = mp->next) {
580 if (mp->format == FORMAT_RAW) {
581 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500582 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500583 else
Len Brownc8ade362017-02-15 17:15:11 -0500584 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500585 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500586 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
587 outp += sprintf(outp, "%s%8s", delim, mp->name);
588 else
589 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500590 }
591 }
592
Len Brown812db3f2017-02-10 00:25:41 -0500593 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500594 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500595
Len Brown812db3f2017-02-10 00:25:41 -0500596 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500597 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500598
Len Brown812db3f2017-02-10 00:25:41 -0500599 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500600 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500601
Len Brown0b2bb692015-03-26 00:50:30 -0400602 if (do_skl_residency) {
Len Brown6168c2e2017-02-16 23:07:51 -0500603 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
604 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
605 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
606 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400607 }
608
Len Brown0f47c082017-01-27 00:50:45 -0500609 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500610 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500611 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500612 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500613 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500614 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500615 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500616 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500617 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500618 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500619 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500620 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500621 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500622 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400623
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800624 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500625 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500626 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500627 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500628 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500629 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500630 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500631 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500632 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500633 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500634 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500635 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500636 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500637 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500638 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500639 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500640 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500641 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500642 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500643 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500644 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500645 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500646 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500647 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500648 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500649 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800650 }
Len Brown388e9c82016-12-22 23:57:55 -0500651 for (mp = sys.pp; mp; mp = mp->next) {
652 if (mp->format == FORMAT_RAW) {
653 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500654 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500655 else
Len Brownc8ade362017-02-15 17:15:11 -0500656 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500657 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500658 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
659 outp += sprintf(outp, "%s%8s", delim, mp->name);
660 else
661 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500662 }
663 }
664
Len Brownc98d5d92012-06-04 00:56:40 -0400665 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400666}
667
Len Brownc98d5d92012-06-04 00:56:40 -0400668int dump_counters(struct thread_data *t, struct core_data *c,
669 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400670{
Len Brown388e9c82016-12-22 23:57:55 -0500671 int i;
672 struct msr_counter *mp;
673
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200674 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400675
Len Brownc98d5d92012-06-04 00:56:40 -0400676 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200677 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
678 t->cpu_id, t->flags);
679 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
680 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
681 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
682 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500683
Len Brown812db3f2017-02-10 00:25:41 -0500684 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500685 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500686 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500687 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500688
689 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
690 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
691 i, mp->msr_num, t->counter[i]);
692 }
Len Brownc98d5d92012-06-04 00:56:40 -0400693 }
Len Brown103a8fe2010-10-22 23:53:03 -0400694
Len Brownc98d5d92012-06-04 00:56:40 -0400695 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200696 outp += sprintf(outp, "core: %d\n", c->core_id);
697 outp += sprintf(outp, "c3: %016llX\n", c->c3);
698 outp += sprintf(outp, "c6: %016llX\n", c->c6);
699 outp += sprintf(outp, "c7: %016llX\n", c->c7);
700 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500701
702 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
703 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
704 i, mp->msr_num, c->counter[i]);
705 }
Len Brown0539ba12017-02-10 00:27:20 -0500706 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400707 }
708
709 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200710 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400711
712 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
713 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
714 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
715 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
716
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200717 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500718 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500719 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500720 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500721 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500722 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500723 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200724 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
725 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
726 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
727 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
728 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
729 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
730 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
731 outp += sprintf(outp, "Throttle PKG: %0X\n",
732 p->rapl_pkg_perf_status);
733 outp += sprintf(outp, "Throttle RAM: %0X\n",
734 p->rapl_dram_perf_status);
735 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500736
737 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
738 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
739 i, mp->msr_num, p->counter[i]);
740 }
Len Brownc98d5d92012-06-04 00:56:40 -0400741 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200742
743 outp += sprintf(outp, "\n");
744
Len Brownc98d5d92012-06-04 00:56:40 -0400745 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400746}
747
Len Browne23da032012-02-06 18:37:16 -0500748/*
749 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500750 */
Len Brownc98d5d92012-06-04 00:56:40 -0400751int format_counters(struct thread_data *t, struct core_data *c,
752 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400753{
Len Brown008d396e2017-02-10 00:29:51 -0500754 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500755 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500756 int i;
757 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500758 char *delim = "\t";
759 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400760
Len Brownc98d5d92012-06-04 00:56:40 -0400761 /* if showing only 1st thread in core and this isn't one, bail out */
762 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
763 return 0;
764
765 /* if showing only 1st thread in pkg and this isn't one, bail out */
766 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
767 return 0;
768
Len Brown1ef7d212017-02-10 23:54:15 -0500769 /*if not summary line and --cpu is used */
770 if ((t != &average.threads) &&
771 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
772 return 0;
773
Len Brown103a8fe2010-10-22 23:53:03 -0400774 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
775
Len Brown008d396e2017-02-10 00:29:51 -0500776 tsc = t->tsc * tsc_tweak;
777
Len Brownc98d5d92012-06-04 00:56:40 -0400778 /* topo columns, print blanks on 1st (average) line */
779 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500780 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500781 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500782 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500783 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500784 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500785 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400786 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500787 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400788 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500789 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400790 else
Len Brown6168c2e2017-02-16 23:07:51 -0500791 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400792 }
Len Brown812db3f2017-02-10 00:25:41 -0500793 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400794 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500795 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400796 else
Len Brown6168c2e2017-02-16 23:07:51 -0500797 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400798 }
Len Brown812db3f2017-02-10 00:25:41 -0500799 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500800 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400801 }
Len Brownfc04cc62014-02-06 00:55:19 -0500802
Len Brown812db3f2017-02-10 00:25:41 -0500803 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500804 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500805 1.0 / units * t->aperf / interval_float);
806
Len Brown812db3f2017-02-10 00:25:41 -0500807 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500808 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400809
Len Brown812db3f2017-02-10 00:25:41 -0500810 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400811 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500812 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400813 else
Len Brown6168c2e2017-02-16 23:07:51 -0500814 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500815 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400816 }
Len Brown103a8fe2010-10-22 23:53:03 -0400817
Len Brown812db3f2017-02-10 00:25:41 -0500818 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500819 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400820
Len Brown562a2d32016-02-26 23:48:05 -0500821 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500822 if (DO_BIC(BIC_IRQ)) {
823 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500824 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500825 else
Len Brown6168c2e2017-02-16 23:07:51 -0500826 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500827 }
Len Brown562a2d32016-02-26 23:48:05 -0500828
Len Brown1cc21f72015-02-23 00:34:57 -0500829 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500830 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500831 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500832
Len Brown678a3bd2017-02-09 22:22:13 -0500833 /* Added counters */
834 for (i = 0, mp = sys.tp; 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) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500838 else
Len Brown6168c2e2017-02-16 23:07:51 -0500839 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -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 : ""), t->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 : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500845 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500846 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500847 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500848 else
Len Brown6168c2e2017-02-16 23:07:51 -0500849 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500850 }
851 }
852
Len Brown41618e62017-02-09 18:25:22 -0500853 /* C1 */
854 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500855 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500856
857
Len Brownc98d5d92012-06-04 00:56:40 -0400858 /* print per-core data only for 1st thread in core */
859 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
860 goto done;
861
Len Brown812db3f2017-02-10 00:25:41 -0500862 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500863 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500864 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500865 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500866 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500867 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400868
Len Brown0539ba12017-02-10 00:27:20 -0500869 /* Mod%c6 */
870 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500871 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500872
Len Brown812db3f2017-02-10 00:25:41 -0500873 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500874 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500875
Len Brown388e9c82016-12-22 23:57:55 -0500876 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
877 if (mp->format == FORMAT_RAW) {
878 if (mp->width == 32)
Len Brown6168c2e2017-02-16 23:07:51 -0500879 outp += sprintf(outp, "%s0x%08lx", (printed++ ? delim : ""), (unsigned long) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500880 else
Len Brown6168c2e2017-02-16 23:07:51 -0500881 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500882 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500883 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500884 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500885 else
Len Brown6168c2e2017-02-16 23:07:51 -0500886 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500887 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500888 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500889 }
890 }
891
Len Brownc98d5d92012-06-04 00:56:40 -0400892 /* print per-package data only for 1st core in package */
893 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
894 goto done;
895
Len Brown0b2bb692015-03-26 00:50:30 -0400896 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500897 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500898 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500899
Len Brownfdf676e2016-02-27 01:28:12 -0500900 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500901 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400902 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500903 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400904 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500905 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400906 p->gfx_rc6_ms / 10.0 / interval_float);
907 }
908 }
Len Brownfdf676e2016-02-27 01:28:12 -0500909
Len Brown27d47352016-02-27 00:37:54 -0500910 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500911 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500912 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500913
Len Brown0b2bb692015-03-26 00:50:30 -0400914 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
915 if (do_skl_residency) {
Len Brown6168c2e2017-02-16 23:07:51 -0500916 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
917 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
918 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
919 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400920 }
921
Len Brown0f47c082017-01-27 00:50:45 -0500922 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500923 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500924 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500925 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500926 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500927 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500928 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500929 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500930 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500931 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500932 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500933 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500934 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500935 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500936
937 /*
938 * If measurement interval exceeds minimum RAPL Joule Counter range,
939 * indicate that results are suspect by printing "**" in fraction place.
940 */
Len Brownfc04cc62014-02-06 00:55:19 -0500941 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -0500942 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500943 else
Len Browne975db52016-04-06 23:56:02 -0400944 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500945
Len Brown812db3f2017-02-10 00:25:41 -0500946 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500947 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500948 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500949 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500950 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500951 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500952 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500953 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500954 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500955 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500956 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500957 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500958 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500959 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500960 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500961 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500962 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500963 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 -0500964 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500965 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 -0500966
Len Brown388e9c82016-12-22 23:57:55 -0500967 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
968 if (mp->format == FORMAT_RAW) {
969 if (mp->width == 32)
Len Brown6168c2e2017-02-16 23:07:51 -0500970 outp += sprintf(outp, "%s0x%08lx", (printed++ ? delim : ""), (unsigned long) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500971 else
Len Brown6168c2e2017-02-16 23:07:51 -0500972 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500973 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500974 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500975 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500976 else
Len Brown6168c2e2017-02-16 23:07:51 -0500977 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500978 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500979 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500980 }
981 }
982
Len Brownc98d5d92012-06-04 00:56:40 -0400983done:
Len Brownc98d5d92012-06-04 00:56:40 -0400984 outp += sprintf(outp, "\n");
985
986 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400987}
988
Len Brownb7d8c142016-02-13 23:36:17 -0500989void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400990{
Len Brownb7d8c142016-02-13 23:36:17 -0500991 FILE *filep;
992
993 if (outf == stderr)
994 filep = stdout;
995 else
996 filep = outf;
997
998 fputs(output_buffer, filep);
999 fflush(filep);
1000
Len Brownc98d5d92012-06-04 00:56:40 -04001001 outp = output_buffer;
1002}
Len Brownb7d8c142016-02-13 23:36:17 -05001003void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001004{
Len Brownb7d8c142016-02-13 23:36:17 -05001005 fputs(output_buffer, outf);
1006 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001007 outp = output_buffer;
1008}
1009void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1010{
Len Browne23da032012-02-06 18:37:16 -05001011 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001012
Len Browne23da032012-02-06 18:37:16 -05001013 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001014 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001015
Len Brownc98d5d92012-06-04 00:56:40 -04001016 if (topo.num_cpus > 1)
1017 format_counters(&average.threads, &average.cores,
1018 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001019
Len Browne23da032012-02-06 18:37:16 -05001020 printed = 1;
1021
1022 if (summary_only)
1023 return;
1024
Len Brownc98d5d92012-06-04 00:56:40 -04001025 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001026}
1027
Len Brown889facb2012-11-08 00:48:57 -05001028#define DELTA_WRAP32(new, old) \
1029 if (new > old) { \
1030 old = new - old; \
1031 } else { \
1032 old = 0x100000000 + new - old; \
1033 }
1034
Len Brownba3dec92016-04-22 20:31:46 -04001035int
Len Brownc98d5d92012-06-04 00:56:40 -04001036delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001037{
Len Brown388e9c82016-12-22 23:57:55 -05001038 int i;
1039 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001040
1041 if (do_skl_residency) {
1042 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1043 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1044 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1045 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1046 }
Len Brownc98d5d92012-06-04 00:56:40 -04001047 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001048 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001049 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001050 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001051 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001052 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001053 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001054 old->pc8 = new->pc8 - old->pc8;
1055 old->pc9 = new->pc9 - old->pc9;
1056 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -05001057 old->pkg_temp_c = new->pkg_temp_c;
1058
Len Brown9185e982016-04-06 17:16:00 -04001059 /* flag an error when rc6 counter resets/wraps */
1060 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1061 old->gfx_rc6_ms = -1;
1062 else
1063 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1064
Len Brown27d47352016-02-27 00:37:54 -05001065 old->gfx_mhz = new->gfx_mhz;
1066
Len Brown889facb2012-11-08 00:48:57 -05001067 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1068 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1069 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1070 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1071 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1072 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001073
Len Brown388e9c82016-12-22 23:57:55 -05001074 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1075 if (mp->format == FORMAT_RAW)
1076 old->counter[i] = new->counter[i];
1077 else
1078 old->counter[i] = new->counter[i] - old->counter[i];
1079 }
1080
Len Brownba3dec92016-04-22 20:31:46 -04001081 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001082}
Len Brown103a8fe2010-10-22 23:53:03 -04001083
Len Brownc98d5d92012-06-04 00:56:40 -04001084void
1085delta_core(struct core_data *new, struct core_data *old)
1086{
Len Brown388e9c82016-12-22 23:57:55 -05001087 int i;
1088 struct msr_counter *mp;
1089
Len Brownc98d5d92012-06-04 00:56:40 -04001090 old->c3 = new->c3 - old->c3;
1091 old->c6 = new->c6 - old->c6;
1092 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001093 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001094 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001095
1096 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1097 if (mp->format == FORMAT_RAW)
1098 old->counter[i] = new->counter[i];
1099 else
1100 old->counter[i] = new->counter[i] - old->counter[i];
1101 }
Len Brownc98d5d92012-06-04 00:56:40 -04001102}
Len Brown103a8fe2010-10-22 23:53:03 -04001103
Len Brownc3ae3312012-06-13 21:31:46 -04001104/*
1105 * old = new - old
1106 */
Len Brownba3dec92016-04-22 20:31:46 -04001107int
Len Brownc98d5d92012-06-04 00:56:40 -04001108delta_thread(struct thread_data *new, struct thread_data *old,
1109 struct core_data *core_delta)
1110{
Len Brown388e9c82016-12-22 23:57:55 -05001111 int i;
1112 struct msr_counter *mp;
1113
Len Brownc98d5d92012-06-04 00:56:40 -04001114 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001115
Len Brownc98d5d92012-06-04 00:56:40 -04001116 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001117 if (old->tsc < (1000 * 1000))
1118 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1119 "You can disable all c-states by booting with \"idle=poll\"\n"
1120 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001121
Len Brownc98d5d92012-06-04 00:56:40 -04001122 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001123
Len Brown812db3f2017-02-10 00:25:41 -05001124 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001125 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1126 old->aperf = new->aperf - old->aperf;
1127 old->mperf = new->mperf - old->mperf;
1128 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001129 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001130 }
Len Brownc98d5d92012-06-04 00:56:40 -04001131 }
Len Brown103a8fe2010-10-22 23:53:03 -04001132
Len Brown103a8fe2010-10-22 23:53:03 -04001133
Len Brown144b44b2013-11-09 00:30:16 -05001134 if (use_c1_residency_msr) {
1135 /*
1136 * Some models have a dedicated C1 residency MSR,
1137 * which should be more accurate than the derivation below.
1138 */
1139 } else {
1140 /*
1141 * As counter collection is not atomic,
1142 * it is possible for mperf's non-halted cycles + idle states
1143 * to exceed TSC's all cycles: show c1 = 0% in that case.
1144 */
1145 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1146 old->c1 = 0;
1147 else {
1148 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001149 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001150 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001151 }
Len Brownc98d5d92012-06-04 00:56:40 -04001152 }
Len Brownc3ae3312012-06-13 21:31:46 -04001153
Len Brownc98d5d92012-06-04 00:56:40 -04001154 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001155 if (debug > 1)
1156 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001157 old->mperf = 1; /* divide by 0 protection */
1158 }
1159
Len Brown812db3f2017-02-10 00:25:41 -05001160 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001161 old->irq_count = new->irq_count - old->irq_count;
1162
Len Brown812db3f2017-02-10 00:25:41 -05001163 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001164 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001165
Len Brown388e9c82016-12-22 23:57:55 -05001166 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1167 if (mp->format == FORMAT_RAW)
1168 old->counter[i] = new->counter[i];
1169 else
1170 old->counter[i] = new->counter[i] - old->counter[i];
1171 }
Len Brownba3dec92016-04-22 20:31:46 -04001172 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001173}
1174
1175int delta_cpu(struct thread_data *t, struct core_data *c,
1176 struct pkg_data *p, struct thread_data *t2,
1177 struct core_data *c2, struct pkg_data *p2)
1178{
Len Brownba3dec92016-04-22 20:31:46 -04001179 int retval = 0;
1180
Len Brownc98d5d92012-06-04 00:56:40 -04001181 /* calculate core delta only for 1st thread in core */
1182 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1183 delta_core(c, c2);
1184
1185 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001186 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1187 if (retval)
1188 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001189
1190 /* calculate package delta only for 1st core in package */
1191 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001192 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001193
Len Brownba3dec92016-04-22 20:31:46 -04001194 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001195}
1196
1197void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1198{
Len Brown388e9c82016-12-22 23:57:55 -05001199 int i;
1200 struct msr_counter *mp;
1201
Len Brownc98d5d92012-06-04 00:56:40 -04001202 t->tsc = 0;
1203 t->aperf = 0;
1204 t->mperf = 0;
1205 t->c1 = 0;
1206
Len Brown562a2d32016-02-26 23:48:05 -05001207 t->irq_count = 0;
1208 t->smi_count = 0;
1209
Len Brownc98d5d92012-06-04 00:56:40 -04001210 /* tells format_counters to dump all fields from this set */
1211 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1212
1213 c->c3 = 0;
1214 c->c6 = 0;
1215 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001216 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001217 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001218
Len Brown0b2bb692015-03-26 00:50:30 -04001219 p->pkg_wtd_core_c0 = 0;
1220 p->pkg_any_core_c0 = 0;
1221 p->pkg_any_gfxe_c0 = 0;
1222 p->pkg_both_core_gfxe_c0 = 0;
1223
Len Brownc98d5d92012-06-04 00:56:40 -04001224 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001225 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001226 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001227 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001228 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001229 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001230 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001231 p->pc8 = 0;
1232 p->pc9 = 0;
1233 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001234
1235 p->energy_pkg = 0;
1236 p->energy_dram = 0;
1237 p->energy_cores = 0;
1238 p->energy_gfx = 0;
1239 p->rapl_pkg_perf_status = 0;
1240 p->rapl_dram_perf_status = 0;
1241 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001242
Len Brownfdf676e2016-02-27 01:28:12 -05001243 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001244 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001245 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1246 t->counter[i] = 0;
1247
1248 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1249 c->counter[i] = 0;
1250
1251 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1252 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001253}
1254int sum_counters(struct thread_data *t, struct core_data *c,
1255 struct pkg_data *p)
1256{
Len Brown388e9c82016-12-22 23:57:55 -05001257 int i;
1258 struct msr_counter *mp;
1259
Len Brownc98d5d92012-06-04 00:56:40 -04001260 average.threads.tsc += t->tsc;
1261 average.threads.aperf += t->aperf;
1262 average.threads.mperf += t->mperf;
1263 average.threads.c1 += t->c1;
1264
Len Brown562a2d32016-02-26 23:48:05 -05001265 average.threads.irq_count += t->irq_count;
1266 average.threads.smi_count += t->smi_count;
1267
Len Brown388e9c82016-12-22 23:57:55 -05001268 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1269 if (mp->format == FORMAT_RAW)
1270 continue;
1271 average.threads.counter[i] += t->counter[i];
1272 }
1273
Len Brownc98d5d92012-06-04 00:56:40 -04001274 /* sum per-core values only for 1st thread in core */
1275 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1276 return 0;
1277
1278 average.cores.c3 += c->c3;
1279 average.cores.c6 += c->c6;
1280 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001281 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001282
Len Brown889facb2012-11-08 00:48:57 -05001283 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1284
Len Brown388e9c82016-12-22 23:57:55 -05001285 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1286 if (mp->format == FORMAT_RAW)
1287 continue;
1288 average.cores.counter[i] += c->counter[i];
1289 }
1290
Len Brownc98d5d92012-06-04 00:56:40 -04001291 /* sum per-pkg values only for 1st core in pkg */
1292 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1293 return 0;
1294
Len Brown0b2bb692015-03-26 00:50:30 -04001295 if (do_skl_residency) {
1296 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1297 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1298 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1299 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1300 }
1301
Len Brownc98d5d92012-06-04 00:56:40 -04001302 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001303 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001304 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001305 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001306 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001307 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001308 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001309 average.packages.pc8 += p->pc8;
1310 average.packages.pc9 += p->pc9;
1311 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001312
Len Brown889facb2012-11-08 00:48:57 -05001313 average.packages.energy_pkg += p->energy_pkg;
1314 average.packages.energy_dram += p->energy_dram;
1315 average.packages.energy_cores += p->energy_cores;
1316 average.packages.energy_gfx += p->energy_gfx;
1317
Len Brownfdf676e2016-02-27 01:28:12 -05001318 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001319 average.packages.gfx_mhz = p->gfx_mhz;
1320
Len Brown889facb2012-11-08 00:48:57 -05001321 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1322
1323 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1324 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001325
1326 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1327 if (mp->format == FORMAT_RAW)
1328 continue;
1329 average.packages.counter[i] += p->counter[i];
1330 }
Len Brownc98d5d92012-06-04 00:56:40 -04001331 return 0;
1332}
1333/*
1334 * sum the counters for all cpus in the system
1335 * compute the weighted average
1336 */
1337void compute_average(struct thread_data *t, struct core_data *c,
1338 struct pkg_data *p)
1339{
Len Brown388e9c82016-12-22 23:57:55 -05001340 int i;
1341 struct msr_counter *mp;
1342
Len Brownc98d5d92012-06-04 00:56:40 -04001343 clear_counters(&average.threads, &average.cores, &average.packages);
1344
1345 for_all_cpus(sum_counters, t, c, p);
1346
1347 average.threads.tsc /= topo.num_cpus;
1348 average.threads.aperf /= topo.num_cpus;
1349 average.threads.mperf /= topo.num_cpus;
1350 average.threads.c1 /= topo.num_cpus;
1351
Len Brown0de6c0d2017-02-15 21:45:40 -05001352 if (average.threads.irq_count > 9999999)
1353 sums_need_wide_columns = 1;
1354
Len Brownc98d5d92012-06-04 00:56:40 -04001355 average.cores.c3 /= topo.num_cores;
1356 average.cores.c6 /= topo.num_cores;
1357 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001358 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001359
Len Brown0b2bb692015-03-26 00:50:30 -04001360 if (do_skl_residency) {
1361 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1362 average.packages.pkg_any_core_c0 /= topo.num_packages;
1363 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1364 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1365 }
1366
Len Brownc98d5d92012-06-04 00:56:40 -04001367 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001368 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001369 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001370 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001371 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001372 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001373 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001374
1375 average.packages.pc8 /= topo.num_packages;
1376 average.packages.pc9 /= topo.num_packages;
1377 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001378
1379 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1380 if (mp->format == FORMAT_RAW)
1381 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001382 if (mp->type == COUNTER_ITEMS) {
1383 if (average.threads.counter[i] > 9999999)
1384 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001385 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001386 }
Len Brown388e9c82016-12-22 23:57:55 -05001387 average.threads.counter[i] /= topo.num_cpus;
1388 }
1389 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1390 if (mp->format == FORMAT_RAW)
1391 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001392 if (mp->type == COUNTER_ITEMS) {
1393 if (average.cores.counter[i] > 9999999)
1394 sums_need_wide_columns = 1;
1395 }
Len Brown388e9c82016-12-22 23:57:55 -05001396 average.cores.counter[i] /= topo.num_cores;
1397 }
1398 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1399 if (mp->format == FORMAT_RAW)
1400 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001401 if (mp->type == COUNTER_ITEMS) {
1402 if (average.packages.counter[i] > 9999999)
1403 sums_need_wide_columns = 1;
1404 }
Len Brown388e9c82016-12-22 23:57:55 -05001405 average.packages.counter[i] /= topo.num_packages;
1406 }
Len Brownc98d5d92012-06-04 00:56:40 -04001407}
1408
1409static unsigned long long rdtsc(void)
1410{
1411 unsigned int low, high;
1412
1413 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1414
1415 return low | ((unsigned long long)high) << 32;
1416}
1417
Len Brownc98d5d92012-06-04 00:56:40 -04001418/*
Len Brown495c76542017-02-08 02:41:51 -05001419 * Open a file, and exit on failure
1420 */
1421FILE *fopen_or_die(const char *path, const char *mode)
1422{
1423 FILE *filep = fopen(path, mode);
1424
1425 if (!filep)
1426 err(1, "%s: open failed", path);
1427 return filep;
1428}
1429/*
1430 * snapshot_sysfs_counter()
1431 *
1432 * return snapshot of given counter
1433 */
1434unsigned long long snapshot_sysfs_counter(char *path)
1435{
1436 FILE *fp;
1437 int retval;
1438 unsigned long long counter;
1439
1440 fp = fopen_or_die(path, "r");
1441
1442 retval = fscanf(fp, "%lld", &counter);
1443 if (retval != 1)
1444 err(1, "snapshot_sysfs_counter(%s)", path);
1445
1446 fclose(fp);
1447
1448 return counter;
1449}
1450
1451int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1452{
1453 if (mp->msr_num != 0) {
1454 if (get_msr(cpu, mp->msr_num, counterp))
1455 return -1;
1456 } else {
Len Brown41618e62017-02-09 18:25:22 -05001457 char path[128];
1458
1459 if (mp->flags & SYSFS_PERCPU) {
1460 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1461 cpu, mp->path);
1462
1463 *counterp = snapshot_sysfs_counter(path);
1464 } else {
1465 *counterp = snapshot_sysfs_counter(mp->path);
1466 }
Len Brown495c76542017-02-08 02:41:51 -05001467 }
1468
1469 return 0;
1470}
1471
1472/*
Len Brownc98d5d92012-06-04 00:56:40 -04001473 * get_counters(...)
1474 * migrate to cpu
1475 * acquire and record local counters for that cpu
1476 */
1477int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1478{
1479 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001480 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001481 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001482 struct msr_counter *mp;
1483 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001484
Len Browne52966c2012-11-08 22:38:05 -05001485 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001486 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001487 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001488 }
Len Brownc98d5d92012-06-04 00:56:40 -04001489
Len Brown0102b062016-02-27 03:11:29 -05001490retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001491 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1492
Len Brown812db3f2017-02-10 00:25:41 -05001493 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001494 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1495
1496 /*
1497 * The TSC, APERF and MPERF must be read together for
1498 * APERF/MPERF and MPERF/TSC to give accurate results.
1499 *
1500 * Unfortunately, APERF and MPERF are read by
1501 * individual system call, so delays may occur
1502 * between them. If the time to read them
1503 * varies by a large amount, we re-read them.
1504 */
1505
1506 /*
1507 * This initial dummy APERF read has been seen to
1508 * reduce jitter in the subsequent reads.
1509 */
1510
Len Brown9c63a652012-10-31 01:29:52 -04001511 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001512 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001513
1514 t->tsc = rdtsc(); /* re-read close to APERF */
1515
1516 tsc_before = t->tsc;
1517
1518 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1519 return -3;
1520
1521 tsc_between = rdtsc();
1522
Len Brown9c63a652012-10-31 01:29:52 -04001523 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001524 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001525
1526 tsc_after = rdtsc();
1527
1528 aperf_time = tsc_between - tsc_before;
1529 mperf_time = tsc_after - tsc_between;
1530
1531 /*
1532 * If the system call latency to read APERF and MPERF
1533 * differ by more than 2x, then try again.
1534 */
1535 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1536 aperf_mperf_retry_count++;
1537 if (aperf_mperf_retry_count < 5)
1538 goto retry;
1539 else
1540 warnx("cpu%d jitter %lld %lld",
1541 cpu, aperf_time, mperf_time);
1542 }
1543 aperf_mperf_retry_count = 0;
1544
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001545 t->aperf = t->aperf * aperf_mperf_multiplier;
1546 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001547 }
1548
Len Brown812db3f2017-02-10 00:25:41 -05001549 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001550 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001551 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001552 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1553 return -5;
1554 t->smi_count = msr & 0xFFFFFFFF;
1555 }
Len Brown0539ba12017-02-10 00:27:20 -05001556 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001557 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1558 return -6;
1559 }
1560
Len Brown388e9c82016-12-22 23:57:55 -05001561 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001562 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001563 return -10;
1564 }
1565
Len Brownc98d5d92012-06-04 00:56:40 -04001566 /* collect core counters only for 1st thread in core */
1567 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1568 return 0;
1569
Len Brown812db3f2017-02-10 00:25:41 -05001570 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001571 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1572 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001573 }
1574
Len Brown812db3f2017-02-10 00:25:41 -05001575 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001576 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1577 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001578 } else if (do_knl_cstates) {
1579 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1580 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001581 }
1582
Len Brown812db3f2017-02-10 00:25:41 -05001583 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001584 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1585 return -8;
1586
Len Brown0539ba12017-02-10 00:27:20 -05001587 if (DO_BIC(BIC_Mod_c6))
1588 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1589 return -8;
1590
Len Brown812db3f2017-02-10 00:25:41 -05001591 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001592 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1593 return -9;
1594 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1595 }
1596
Len Brown388e9c82016-12-22 23:57:55 -05001597 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001598 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001599 return -10;
1600 }
Len Brown889facb2012-11-08 00:48:57 -05001601
Len Brownc98d5d92012-06-04 00:56:40 -04001602 /* collect package counters only for 1st core in package */
1603 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1604 return 0;
1605
Len Brown0b2bb692015-03-26 00:50:30 -04001606 if (do_skl_residency) {
1607 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1608 return -10;
1609 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1610 return -11;
1611 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1612 return -12;
1613 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1614 return -13;
1615 }
Len Brown0f47c082017-01-27 00:50:45 -05001616 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001617 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1618 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001619 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001620 if (do_slm_cstates) {
1621 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1622 return -10;
1623 } else {
1624 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1625 return -10;
1626 }
1627 }
1628
Len Brown0f47c082017-01-27 00:50:45 -05001629 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001630 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1631 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001632 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001633 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1634 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001635 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001636 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1637 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001638 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001639 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1640 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001641 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001642 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1643 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001644
Len Brown889facb2012-11-08 00:48:57 -05001645 if (do_rapl & RAPL_PKG) {
1646 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1647 return -13;
1648 p->energy_pkg = msr & 0xFFFFFFFF;
1649 }
Jacob Pan91484942016-06-16 09:48:20 -07001650 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001651 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1652 return -14;
1653 p->energy_cores = msr & 0xFFFFFFFF;
1654 }
1655 if (do_rapl & RAPL_DRAM) {
1656 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1657 return -15;
1658 p->energy_dram = msr & 0xFFFFFFFF;
1659 }
1660 if (do_rapl & RAPL_GFX) {
1661 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1662 return -16;
1663 p->energy_gfx = msr & 0xFFFFFFFF;
1664 }
1665 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1666 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1667 return -16;
1668 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1669 }
1670 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1671 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1672 return -16;
1673 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1674 }
Len Brown812db3f2017-02-10 00:25:41 -05001675 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001676 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1677 return -17;
1678 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1679 }
Len Brownfdf676e2016-02-27 01:28:12 -05001680
Len Brown812db3f2017-02-10 00:25:41 -05001681 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001682 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1683
Len Brown812db3f2017-02-10 00:25:41 -05001684 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001685 p->gfx_mhz = gfx_cur_mhz;
1686
Len Brown388e9c82016-12-22 23:57:55 -05001687 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001688 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001689 return -10;
1690 }
1691
Len Brown103a8fe2010-10-22 23:53:03 -04001692 return 0;
1693}
1694
Len Brownee7e38e2015-02-09 23:39:45 -05001695/*
1696 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1697 * If you change the values, note they are used both in comparisons
1698 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1699 */
1700
1701#define PCLUKN 0 /* Unknown */
1702#define PCLRSV 1 /* Reserved */
1703#define PCL__0 2 /* PC0 */
1704#define PCL__1 3 /* PC1 */
1705#define PCL__2 4 /* PC2 */
1706#define PCL__3 5 /* PC3 */
1707#define PCL__4 6 /* PC4 */
1708#define PCL__6 7 /* PC6 */
1709#define PCL_6N 8 /* PC6 No Retention */
1710#define PCL_6R 9 /* PC6 Retention */
1711#define PCL__7 10 /* PC7 */
1712#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001713#define PCL__8 12 /* PC8 */
1714#define PCL__9 13 /* PC9 */
1715#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001716
1717int pkg_cstate_limit = PCLUKN;
1718char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001719 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001720
Len Browne9257f52015-04-01 21:02:57 -04001721int 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};
1722int 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};
1723int 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 -05001724int 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 -05001725int 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 -04001726int 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 -04001727int 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 -05001728int 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 -05001729
Len Browna2b7b742015-09-26 00:12:38 -04001730
1731static void
1732calculate_tsc_tweak()
1733{
Len Browna2b7b742015-09-26 00:12:38 -04001734 tsc_tweak = base_hz / tsc_hz;
1735}
1736
Len Brownfcd17212015-03-23 20:29:09 -04001737static void
1738dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001739{
1740 unsigned long long msr;
1741 unsigned int ratio;
1742
Len Brownec0adc52015-11-12 02:42:31 -05001743 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001744
Len Brownb7d8c142016-02-13 23:36:17 -05001745 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001746
Len Brown103a8fe2010-10-22 23:53:03 -04001747 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001748 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001749 ratio, bclk, ratio * bclk);
1750
1751 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001752 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001753 ratio, bclk, ratio * bclk);
1754
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001755 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001756 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001757 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001758
Len Brownfcd17212015-03-23 20:29:09 -04001759 return;
1760}
1761
1762static void
1763dump_hsw_turbo_ratio_limits(void)
1764{
1765 unsigned long long msr;
1766 unsigned int ratio;
1767
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001768 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001769
Len Brownb7d8c142016-02-13 23:36:17 -05001770 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001771
1772 ratio = (msr >> 8) & 0xFF;
1773 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001774 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001775 ratio, bclk, ratio * bclk);
1776
1777 ratio = (msr >> 0) & 0xFF;
1778 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001779 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001780 ratio, bclk, ratio * bclk);
1781 return;
1782}
1783
1784static void
1785dump_ivt_turbo_ratio_limits(void)
1786{
1787 unsigned long long msr;
1788 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001789
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001790 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001791
Len Brownb7d8c142016-02-13 23:36:17 -05001792 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001793
1794 ratio = (msr >> 56) & 0xFF;
1795 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001796 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001797 ratio, bclk, ratio * bclk);
1798
1799 ratio = (msr >> 48) & 0xFF;
1800 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001801 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001802 ratio, bclk, ratio * bclk);
1803
1804 ratio = (msr >> 40) & 0xFF;
1805 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001806 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001807 ratio, bclk, ratio * bclk);
1808
1809 ratio = (msr >> 32) & 0xFF;
1810 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001811 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001812 ratio, bclk, ratio * bclk);
1813
1814 ratio = (msr >> 24) & 0xFF;
1815 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001816 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001817 ratio, bclk, ratio * bclk);
1818
1819 ratio = (msr >> 16) & 0xFF;
1820 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001821 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001822 ratio, bclk, ratio * bclk);
1823
1824 ratio = (msr >> 8) & 0xFF;
1825 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001826 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001827 ratio, bclk, ratio * bclk);
1828
1829 ratio = (msr >> 0) & 0xFF;
1830 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001831 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001832 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001833 return;
1834}
Len Brown31e07522017-01-31 23:07:49 -05001835int has_turbo_ratio_group_limits(int family, int model)
1836{
1837
1838 if (!genuine_intel)
1839 return 0;
1840
1841 switch (model) {
1842 case INTEL_FAM6_ATOM_GOLDMONT:
1843 case INTEL_FAM6_SKYLAKE_X:
1844 case INTEL_FAM6_ATOM_DENVERTON:
1845 return 1;
1846 }
1847 return 0;
1848}
Len Brown6574a5d2012-09-21 00:01:31 -04001849
Len Brownfcd17212015-03-23 20:29:09 -04001850static void
Len Brown31e07522017-01-31 23:07:49 -05001851dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001852{
Len Brown31e07522017-01-31 23:07:49 -05001853 unsigned long long msr, core_counts;
1854 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001855
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001856 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001857 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001858
Len Brown31e07522017-01-31 23:07:49 -05001859 if (has_turbo_ratio_group_limits(family, model)) {
1860 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1861 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1862 } else {
1863 core_counts = 0x0807060504030201;
1864 }
1865
Len Brown6574a5d2012-09-21 00:01:31 -04001866 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001867 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -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 Brown6574a5d2012-09-21 00:01:31 -04001871
1872 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001873 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001874 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001875 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1876 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001877
1878 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001879 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001880 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001881 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1882 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001883
1884 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001885 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001886 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001887 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1888 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001889
Len Brown103a8fe2010-10-22 23:53:03 -04001890 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001891 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001892 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001893 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1894 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001895
1896 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001897 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001898 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001899 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1900 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001901
1902 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001903 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001904 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001905 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1906 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001907
1908 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001909 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001910 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001911 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1912 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04001913 return;
1914}
Len Brown3a9a9412014-08-15 02:39:52 -04001915
Len Brownfcd17212015-03-23 20:29:09 -04001916static void
Len Brown0f7887c2017-01-12 23:49:18 -05001917dump_atom_turbo_ratio_limits(void)
1918{
1919 unsigned long long msr;
1920 unsigned int ratio;
1921
1922 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1923 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1924
1925 ratio = (msr >> 0) & 0x3F;
1926 if (ratio)
1927 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1928 ratio, bclk, ratio * bclk);
1929
1930 ratio = (msr >> 8) & 0x3F;
1931 if (ratio)
1932 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1933 ratio, bclk, ratio * bclk);
1934
1935 ratio = (msr >> 16) & 0x3F;
1936 if (ratio)
1937 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1938 ratio, bclk, ratio * bclk);
1939
1940 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1941 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1942
1943 ratio = (msr >> 24) & 0x3F;
1944 if (ratio)
1945 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1946 ratio, bclk, ratio * bclk);
1947
1948 ratio = (msr >> 16) & 0x3F;
1949 if (ratio)
1950 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1951 ratio, bclk, ratio * bclk);
1952
1953 ratio = (msr >> 8) & 0x3F;
1954 if (ratio)
1955 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1956 ratio, bclk, ratio * bclk);
1957
1958 ratio = (msr >> 0) & 0x3F;
1959 if (ratio)
1960 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1961 ratio, bclk, ratio * bclk);
1962}
1963
1964static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001965dump_knl_turbo_ratio_limits(void)
1966{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001967 const unsigned int buckets_no = 7;
1968
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001969 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001970 int delta_cores, delta_ratio;
1971 int i, b_nr;
1972 unsigned int cores[buckets_no];
1973 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001974
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001975 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001976
Len Brownb7d8c142016-02-13 23:36:17 -05001977 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001978 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001979
1980 /**
1981 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001982 * [0] -- Reserved
1983 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001984 * [15:8] -- Base value of freq ratio of bucket 1.
1985 * [20:16] -- +ve delta of number of active cores of bucket 2.
1986 * i.e. active cores of bucket 2 =
1987 * active cores of bucket 1 + delta
1988 * [23:21] -- Negative delta of freq ratio of bucket 2.
1989 * i.e. freq ratio of bucket 2 =
1990 * freq ratio of bucket 1 - delta
1991 * [28:24]-- +ve delta of number of active cores of bucket 3.
1992 * [31:29]-- -ve delta of freq ratio of bucket 3.
1993 * [36:32]-- +ve delta of number of active cores of bucket 4.
1994 * [39:37]-- -ve delta of freq ratio of bucket 4.
1995 * [44:40]-- +ve delta of number of active cores of bucket 5.
1996 * [47:45]-- -ve delta of freq ratio of bucket 5.
1997 * [52:48]-- +ve delta of number of active cores of bucket 6.
1998 * [55:53]-- -ve delta of freq ratio of bucket 6.
1999 * [60:56]-- +ve delta of number of active cores of bucket 7.
2000 * [63:61]-- -ve delta of freq ratio of bucket 7.
2001 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002002
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002003 b_nr = 0;
2004 cores[b_nr] = (msr & 0xFF) >> 1;
2005 ratio[b_nr] = (msr >> 8) & 0xFF;
2006
2007 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002008 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002009 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002010
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002011 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2012 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2013 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002014 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002015
2016 for (i = buckets_no - 1; i >= 0; i--)
2017 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002018 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002019 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002020 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002021}
2022
2023static void
Len Brownfcd17212015-03-23 20:29:09 -04002024dump_nhm_cst_cfg(void)
2025{
2026 unsigned long long msr;
2027
Len Brown1df2e552017-01-07 23:24:57 -05002028 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002029
2030#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
2031#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
2032
Len Brown1df2e552017-01-07 23:24:57 -05002033 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002034
Len Brownb7d8c142016-02-13 23:36:17 -05002035 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04002036 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2037 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2038 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2039 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2040 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002041 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002042 pkg_cstate_limit_strings[pkg_cstate_limit]);
2043 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002044}
2045
Len Brown6fb31432015-06-17 16:23:45 -04002046static void
2047dump_config_tdp(void)
2048{
2049 unsigned long long msr;
2050
2051 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002052 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002053 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002054
2055 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002056 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002057 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002058 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2059 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2060 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2061 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002062 }
Len Brownb7d8c142016-02-13 23:36:17 -05002063 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002064
2065 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002066 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002067 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002068 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2069 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2070 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2071 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002072 }
Len Brownb7d8c142016-02-13 23:36:17 -05002073 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002074
2075 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002076 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002077 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002078 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2079 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2080 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002081
Len Brown6fb31432015-06-17 16:23:45 -04002082 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002083 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002084 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002085 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2086 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002087}
Len Brown5a634262016-04-06 17:15:55 -04002088
2089unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2090
2091void print_irtl(void)
2092{
2093 unsigned long long msr;
2094
2095 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2096 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2097 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2098 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2099
2100 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2101 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2102 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2103 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2104
2105 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2106 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2107 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2108 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2109
2110 if (!do_irtl_hsw)
2111 return;
2112
2113 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2114 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2115 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2116 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2117
2118 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2119 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2120 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2121 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2122
2123 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2124 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2125 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2126 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2127
2128}
Len Brown36229892016-02-26 20:51:02 -05002129void free_fd_percpu(void)
2130{
2131 int i;
2132
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002133 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002134 if (fd_percpu[i] != 0)
2135 close(fd_percpu[i]);
2136 }
2137
2138 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002139}
2140
Len Brownc98d5d92012-06-04 00:56:40 -04002141void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002142{
Len Brownc98d5d92012-06-04 00:56:40 -04002143 CPU_FREE(cpu_present_set);
2144 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002145 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002146
Len Brownc98d5d92012-06-04 00:56:40 -04002147 CPU_FREE(cpu_affinity_set);
2148 cpu_affinity_set = NULL;
2149 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002150
Len Brownc98d5d92012-06-04 00:56:40 -04002151 free(thread_even);
2152 free(core_even);
2153 free(package_even);
2154
2155 thread_even = NULL;
2156 core_even = NULL;
2157 package_even = NULL;
2158
2159 free(thread_odd);
2160 free(core_odd);
2161 free(package_odd);
2162
2163 thread_odd = NULL;
2164 core_odd = NULL;
2165 package_odd = NULL;
2166
2167 free(output_buffer);
2168 output_buffer = NULL;
2169 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002170
2171 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002172
2173 free(irq_column_2_cpu);
2174 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002175}
2176
Josh Triplett57a42a32013-08-20 17:20:17 -07002177
2178/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002179 * Parse a file containing a single int.
2180 */
2181int parse_int_file(const char *fmt, ...)
2182{
2183 va_list args;
2184 char path[PATH_MAX];
2185 FILE *filep;
2186 int value;
2187
2188 va_start(args, fmt);
2189 vsnprintf(path, sizeof(path), fmt, args);
2190 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002191 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002192 if (fscanf(filep, "%d", &value) != 1)
2193 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002194 fclose(filep);
2195 return value;
2196}
2197
2198/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002199 * get_cpu_position_in_core(cpu)
2200 * return the position of the CPU among its HT siblings in the core
2201 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002202 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002203int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002204{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002205 char path[64];
2206 FILE *filep;
2207 int this_cpu;
2208 char character;
2209 int i;
2210
2211 sprintf(path,
2212 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2213 cpu);
2214 filep = fopen(path, "r");
2215 if (filep == NULL) {
2216 perror(path);
2217 exit(1);
2218 }
2219
2220 for (i = 0; i < topo.num_threads_per_core; i++) {
2221 fscanf(filep, "%d", &this_cpu);
2222 if (this_cpu == cpu) {
2223 fclose(filep);
2224 return i;
2225 }
2226
2227 /* Account for no separator after last thread*/
2228 if (i != (topo.num_threads_per_core - 1))
2229 fscanf(filep, "%c", &character);
2230 }
2231
2232 fclose(filep);
2233 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002234}
2235
Len Brownc98d5d92012-06-04 00:56:40 -04002236/*
2237 * cpu_is_first_core_in_package(cpu)
2238 * return 1 if given CPU is 1st core in package
2239 */
2240int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002241{
Josh Triplett95aebc42013-08-20 17:20:16 -07002242 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002243}
2244
2245int get_physical_package_id(int cpu)
2246{
Josh Triplett95aebc42013-08-20 17:20:16 -07002247 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002248}
2249
2250int get_core_id(int cpu)
2251{
Josh Triplett95aebc42013-08-20 17:20:16 -07002252 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002253}
2254
Len Brownc98d5d92012-06-04 00:56:40 -04002255int get_num_ht_siblings(int cpu)
2256{
2257 char path[80];
2258 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002259 int sib1;
2260 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002261 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002262 char str[100];
2263 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002264
2265 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002266 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002267
Len Brownc98d5d92012-06-04 00:56:40 -04002268 /*
2269 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002270 * A ',' separated or '-' separated set of numbers
2271 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002272 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002273 fscanf(filep, "%d%c\n", &sib1, &character);
2274 fseek(filep, 0, SEEK_SET);
2275 fgets(str, 100, filep);
2276 ch = strchr(str, character);
2277 while (ch != NULL) {
2278 matches++;
2279 ch = strchr(ch+1, character);
2280 }
Len Brownc98d5d92012-06-04 00:56:40 -04002281
2282 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002283 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002284}
2285
Len Brown103a8fe2010-10-22 23:53:03 -04002286/*
Len Brownc98d5d92012-06-04 00:56:40 -04002287 * run func(thread, core, package) in topology order
2288 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002289 */
2290
Len Brownc98d5d92012-06-04 00:56:40 -04002291int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2292 struct pkg_data *, struct thread_data *, struct core_data *,
2293 struct pkg_data *), struct thread_data *thread_base,
2294 struct core_data *core_base, struct pkg_data *pkg_base,
2295 struct thread_data *thread_base2, struct core_data *core_base2,
2296 struct pkg_data *pkg_base2)
2297{
2298 int retval, pkg_no, core_no, thread_no;
2299
2300 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2301 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2302 for (thread_no = 0; thread_no <
2303 topo.num_threads_per_core; ++thread_no) {
2304 struct thread_data *t, *t2;
2305 struct core_data *c, *c2;
2306 struct pkg_data *p, *p2;
2307
2308 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2309
2310 if (cpu_is_not_present(t->cpu_id))
2311 continue;
2312
2313 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2314
2315 c = GET_CORE(core_base, core_no, pkg_no);
2316 c2 = GET_CORE(core_base2, core_no, pkg_no);
2317
2318 p = GET_PKG(pkg_base, pkg_no);
2319 p2 = GET_PKG(pkg_base2, pkg_no);
2320
2321 retval = func(t, c, p, t2, c2, p2);
2322 if (retval)
2323 return retval;
2324 }
2325 }
2326 }
2327 return 0;
2328}
2329
2330/*
2331 * run func(cpu) on every cpu in /proc/stat
2332 * return max_cpu number
2333 */
2334int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002335{
2336 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002337 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002338 int retval;
2339
Josh Triplett57a42a32013-08-20 17:20:17 -07002340 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002341
2342 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002343 if (retval != 0)
2344 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002345
Len Brownc98d5d92012-06-04 00:56:40 -04002346 while (1) {
2347 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 -04002348 if (retval != 1)
2349 break;
2350
Len Brownc98d5d92012-06-04 00:56:40 -04002351 retval = func(cpu_num);
2352 if (retval) {
2353 fclose(fp);
2354 return(retval);
2355 }
Len Brown103a8fe2010-10-22 23:53:03 -04002356 }
2357 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002358 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002359}
2360
2361void re_initialize(void)
2362{
Len Brownc98d5d92012-06-04 00:56:40 -04002363 free_all_buffers();
2364 setup_all_buffers();
2365 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002366}
2367
Len Brownc98d5d92012-06-04 00:56:40 -04002368
Len Brown103a8fe2010-10-22 23:53:03 -04002369/*
Len Brownc98d5d92012-06-04 00:56:40 -04002370 * count_cpus()
2371 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002372 */
Len Brownc98d5d92012-06-04 00:56:40 -04002373int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002374{
Len Brownc98d5d92012-06-04 00:56:40 -04002375 if (topo.max_cpu_num < cpu)
2376 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002377
Len Brownc98d5d92012-06-04 00:56:40 -04002378 topo.num_cpus += 1;
2379 return 0;
2380}
2381int mark_cpu_present(int cpu)
2382{
2383 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002384 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002385}
2386
Len Brown562a2d32016-02-26 23:48:05 -05002387/*
2388 * snapshot_proc_interrupts()
2389 *
2390 * read and record summary of /proc/interrupts
2391 *
2392 * return 1 if config change requires a restart, else return 0
2393 */
2394int snapshot_proc_interrupts(void)
2395{
2396 static FILE *fp;
2397 int column, retval;
2398
2399 if (fp == NULL)
2400 fp = fopen_or_die("/proc/interrupts", "r");
2401 else
2402 rewind(fp);
2403
2404 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2405 for (column = 0; column < topo.num_cpus; ++column) {
2406 int cpu_number;
2407
2408 retval = fscanf(fp, " CPU%d", &cpu_number);
2409 if (retval != 1)
2410 break;
2411
2412 if (cpu_number > topo.max_cpu_num) {
2413 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2414 return 1;
2415 }
2416
2417 irq_column_2_cpu[column] = cpu_number;
2418 irqs_per_cpu[cpu_number] = 0;
2419 }
2420
2421 /* read /proc/interrupt count lines and sum up irqs per cpu */
2422 while (1) {
2423 int column;
2424 char buf[64];
2425
2426 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2427 if (retval != 1)
2428 break;
2429
2430 /* read the count per cpu */
2431 for (column = 0; column < topo.num_cpus; ++column) {
2432
2433 int cpu_number, irq_count;
2434
2435 retval = fscanf(fp, " %d", &irq_count);
2436 if (retval != 1)
2437 break;
2438
2439 cpu_number = irq_column_2_cpu[column];
2440 irqs_per_cpu[cpu_number] += irq_count;
2441
2442 }
2443
2444 while (getc(fp) != '\n')
2445 ; /* flush interrupt description */
2446
2447 }
2448 return 0;
2449}
Len Brown27d47352016-02-27 00:37:54 -05002450/*
Len Brownfdf676e2016-02-27 01:28:12 -05002451 * snapshot_gfx_rc6_ms()
2452 *
2453 * record snapshot of
2454 * /sys/class/drm/card0/power/rc6_residency_ms
2455 *
2456 * return 1 if config change requires a restart, else return 0
2457 */
2458int snapshot_gfx_rc6_ms(void)
2459{
2460 FILE *fp;
2461 int retval;
2462
2463 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2464
2465 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2466 if (retval != 1)
2467 err(1, "GFX rc6");
2468
2469 fclose(fp);
2470
2471 return 0;
2472}
2473/*
Len Brown27d47352016-02-27 00:37:54 -05002474 * snapshot_gfx_mhz()
2475 *
2476 * record snapshot of
2477 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2478 *
2479 * return 1 if config change requires a restart, else return 0
2480 */
2481int snapshot_gfx_mhz(void)
2482{
2483 static FILE *fp;
2484 int retval;
2485
2486 if (fp == NULL)
2487 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2488 else
2489 rewind(fp);
2490
2491 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2492 if (retval != 1)
2493 err(1, "GFX MHz");
2494
2495 return 0;
2496}
Len Brown562a2d32016-02-26 23:48:05 -05002497
2498/*
2499 * snapshot /proc and /sys files
2500 *
2501 * return 1 if configuration restart needed, else return 0
2502 */
2503int snapshot_proc_sysfs_files(void)
2504{
Len Brown218f0e82017-02-14 22:07:52 -05002505 if (DO_BIC(BIC_IRQ))
2506 if (snapshot_proc_interrupts())
2507 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002508
Len Brown812db3f2017-02-10 00:25:41 -05002509 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002510 snapshot_gfx_rc6_ms();
2511
Len Brown812db3f2017-02-10 00:25:41 -05002512 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002513 snapshot_gfx_mhz();
2514
Len Brown562a2d32016-02-26 23:48:05 -05002515 return 0;
2516}
2517
Len Brown103a8fe2010-10-22 23:53:03 -04002518void turbostat_loop()
2519{
Len Brownc98d5d92012-06-04 00:56:40 -04002520 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002521 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002522
Len Brown103a8fe2010-10-22 23:53:03 -04002523restart:
Len Browne52966c2012-11-08 22:38:05 -05002524 restarted++;
2525
Len Brown562a2d32016-02-26 23:48:05 -05002526 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002527 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002528 if (retval < -1) {
2529 exit(retval);
2530 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002531 if (restarted > 1) {
2532 exit(retval);
2533 }
Len Brownc98d5d92012-06-04 00:56:40 -04002534 re_initialize();
2535 goto restart;
2536 }
Len Browne52966c2012-11-08 22:38:05 -05002537 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002538 gettimeofday(&tv_even, (struct timezone *)NULL);
2539
2540 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002541 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002542 re_initialize();
2543 goto restart;
2544 }
Len Brown2a0609c2016-02-12 22:44:48 -05002545 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002546 if (snapshot_proc_sysfs_files())
2547 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002548 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002549 if (retval < -1) {
2550 exit(retval);
2551 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002552 re_initialize();
2553 goto restart;
2554 }
Len Brown103a8fe2010-10-22 23:53:03 -04002555 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002556 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002557 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2558 re_initialize();
2559 goto restart;
2560 }
Len Brownc98d5d92012-06-04 00:56:40 -04002561 compute_average(EVEN_COUNTERS);
2562 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002563 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002564 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002565 if (snapshot_proc_sysfs_files())
2566 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002567 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002568 if (retval < -1) {
2569 exit(retval);
2570 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002571 re_initialize();
2572 goto restart;
2573 }
Len Brown103a8fe2010-10-22 23:53:03 -04002574 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002575 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002576 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2577 re_initialize();
2578 goto restart;
2579 }
Len Brownc98d5d92012-06-04 00:56:40 -04002580 compute_average(ODD_COUNTERS);
2581 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002582 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002583 }
2584}
2585
2586void check_dev_msr()
2587{
2588 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002589 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002590
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002591 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2592 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002593 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2594 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002595}
2596
Len Brown98481e72014-08-15 00:36:50 -04002597void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002598{
Len Brown98481e72014-08-15 00:36:50 -04002599 struct __user_cap_header_struct cap_header_data;
2600 cap_user_header_t cap_header = &cap_header_data;
2601 struct __user_cap_data_struct cap_data_data;
2602 cap_user_data_t cap_data = &cap_data_data;
2603 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2604 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002605 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002606
2607 /* check for CAP_SYS_RAWIO */
2608 cap_header->pid = getpid();
2609 cap_header->version = _LINUX_CAPABILITY_VERSION;
2610 if (capget(cap_header, cap_data) < 0)
2611 err(-6, "capget(2) failed");
2612
2613 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2614 do_exit++;
2615 warnx("capget(CAP_SYS_RAWIO) failed,"
2616 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2617 }
2618
2619 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002620 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2621 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002622 do_exit++;
2623 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2624 }
2625
2626 /* if all else fails, thell them to be root */
2627 if (do_exit)
2628 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002629 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002630
2631 if (do_exit)
2632 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002633}
2634
Len Brownd7899442015-01-23 00:12:33 -05002635/*
2636 * NHM adds support for additional MSRs:
2637 *
2638 * MSR_SMI_COUNT 0x00000034
2639 *
Len Brownec0adc52015-11-12 02:42:31 -05002640 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002641 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002642 *
Len Browncf4cbe52017-01-01 13:08:33 -05002643 * MSR_MISC_PWR_MGMT 0x000001aa
2644 *
Len Brownd7899442015-01-23 00:12:33 -05002645 * MSR_PKG_C3_RESIDENCY 0x000003f8
2646 * MSR_PKG_C6_RESIDENCY 0x000003f9
2647 * MSR_CORE_C3_RESIDENCY 0x000003fc
2648 * MSR_CORE_C6_RESIDENCY 0x000003fd
2649 *
Len Brownee7e38e2015-02-09 23:39:45 -05002650 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002651 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002652 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002653 */
Len Brownee7e38e2015-02-09 23:39:45 -05002654int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002655{
Len Brownee7e38e2015-02-09 23:39:45 -05002656 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002657 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002658 int *pkg_cstate_limits;
2659
Len Brown103a8fe2010-10-22 23:53:03 -04002660 if (!genuine_intel)
2661 return 0;
2662
2663 if (family != 6)
2664 return 0;
2665
Len Brown21ed5572015-10-19 22:37:40 -04002666 bclk = discover_bclk(family, model);
2667
Len Brown103a8fe2010-10-22 23:53:03 -04002668 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002669 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2670 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002671 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002672 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2673 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2674 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2675 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002676 pkg_cstate_limits = nhm_pkg_cstate_limits;
2677 break;
Len Brown869ce692016-06-16 23:22:37 -04002678 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2679 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2680 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2681 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002682 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002683 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002684 break;
Len Brown869ce692016-06-16 23:22:37 -04002685 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2686 case INTEL_FAM6_HASWELL_X: /* HSX */
2687 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2688 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2689 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2690 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2691 case INTEL_FAM6_BROADWELL_X: /* BDX */
2692 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2693 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2694 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2695 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2696 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002697 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002698 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002699 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002700 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2701 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002702 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002703 break;
Len Brown869ce692016-06-16 23:22:37 -04002704 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002705 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002706 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002707 pkg_cstate_limits = slv_pkg_cstate_limits;
2708 break;
Len Brown869ce692016-06-16 23:22:37 -04002709 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002710 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002711 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002712 break;
Len Brown869ce692016-06-16 23:22:37 -04002713 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002714 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002715 pkg_cstate_limits = phi_pkg_cstate_limits;
2716 break;
Len Brown869ce692016-06-16 23:22:37 -04002717 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002718 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002719 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002720 pkg_cstate_limits = bxt_pkg_cstate_limits;
2721 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002722 default:
2723 return 0;
2724 }
Len Brown1df2e552017-01-07 23:24:57 -05002725 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002726 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002727
Len Brownec0adc52015-11-12 02:42:31 -05002728 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002729 base_ratio = (msr >> 8) & 0xFF;
2730
2731 base_hz = base_ratio * bclk * 1000000;
2732 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002733 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002734}
Len Brown0f7887c2017-01-12 23:49:18 -05002735/*
Len Brown495c76542017-02-08 02:41:51 -05002736 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002737 *
2738 * MSR_CC6_DEMOTION_POLICY_CONFIG
2739 * MSR_MC6_DEMOTION_POLICY_CONFIG
2740 */
2741
2742int has_slv_msrs(unsigned int family, unsigned int model)
2743{
2744 if (!genuine_intel)
2745 return 0;
2746
2747 switch (model) {
2748 case INTEL_FAM6_ATOM_SILVERMONT1:
2749 case INTEL_FAM6_ATOM_MERRIFIELD:
2750 case INTEL_FAM6_ATOM_MOOREFIELD:
2751 return 1;
2752 }
2753 return 0;
2754}
Len Brown7170a372017-01-27 02:13:27 -05002755int is_dnv(unsigned int family, unsigned int model)
2756{
2757
2758 if (!genuine_intel)
2759 return 0;
2760
2761 switch (model) {
2762 case INTEL_FAM6_ATOM_DENVERTON:
2763 return 1;
2764 }
2765 return 0;
2766}
Len Brownade0eba2017-02-10 01:56:47 -05002767int is_bdx(unsigned int family, unsigned int model)
2768{
2769
2770 if (!genuine_intel)
2771 return 0;
2772
2773 switch (model) {
2774 case INTEL_FAM6_BROADWELL_X:
2775 case INTEL_FAM6_BROADWELL_XEON_D:
2776 return 1;
2777 }
2778 return 0;
2779}
Len Brown34c761972017-01-27 02:36:41 -05002780int is_skx(unsigned int family, unsigned int model)
2781{
2782
2783 if (!genuine_intel)
2784 return 0;
2785
2786 switch (model) {
2787 case INTEL_FAM6_SKYLAKE_X:
2788 return 1;
2789 }
2790 return 0;
2791}
Len Brown0f7887c2017-01-12 23:49:18 -05002792
Len Brown31e07522017-01-31 23:07:49 -05002793int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002794{
Len Brown0f7887c2017-01-12 23:49:18 -05002795 if (has_slv_msrs(family, model))
2796 return 0;
2797
Len Brownd7899442015-01-23 00:12:33 -05002798 switch (model) {
2799 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002800 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2801 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2802 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002803 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002804 return 0;
2805 default:
2806 return 1;
2807 }
2808}
Len Brown0f7887c2017-01-12 23:49:18 -05002809int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2810{
2811 if (has_slv_msrs(family, model))
2812 return 1;
2813
2814 return 0;
2815}
Len Brown6574a5d2012-09-21 00:01:31 -04002816int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2817{
2818 if (!genuine_intel)
2819 return 0;
2820
2821 if (family != 6)
2822 return 0;
2823
2824 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002825 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2826 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002827 return 1;
2828 default:
2829 return 0;
2830 }
2831}
Len Brownfcd17212015-03-23 20:29:09 -04002832int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2833{
2834 if (!genuine_intel)
2835 return 0;
2836
2837 if (family != 6)
2838 return 0;
2839
2840 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002841 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002842 return 1;
2843 default:
2844 return 0;
2845 }
2846}
2847
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002848int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2849{
2850 if (!genuine_intel)
2851 return 0;
2852
2853 if (family != 6)
2854 return 0;
2855
2856 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002857 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002858 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002859 return 1;
2860 default:
2861 return 0;
2862 }
2863}
Len Brown31e07522017-01-31 23:07:49 -05002864int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
2865{
2866 if (!genuine_intel)
2867 return 0;
2868
2869 if (family != 6)
2870 return 0;
2871
2872 switch (model) {
2873 case INTEL_FAM6_ATOM_GOLDMONT:
2874 case INTEL_FAM6_SKYLAKE_X:
2875 return 1;
2876 default:
2877 return 0;
2878 }
2879}
Len Brown6fb31432015-06-17 16:23:45 -04002880int has_config_tdp(unsigned int family, unsigned int model)
2881{
2882 if (!genuine_intel)
2883 return 0;
2884
2885 if (family != 6)
2886 return 0;
2887
2888 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002889 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2890 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2891 case INTEL_FAM6_HASWELL_X: /* HSX */
2892 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2893 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2894 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2895 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2896 case INTEL_FAM6_BROADWELL_X: /* BDX */
2897 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2898 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2899 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2900 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2901 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2902 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002903
Len Brown869ce692016-06-16 23:22:37 -04002904 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002905 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002906 return 1;
2907 default:
2908 return 0;
2909 }
2910}
2911
Len Brownfcd17212015-03-23 20:29:09 -04002912static void
Colin Ian King1b693172016-03-02 13:50:25 +00002913dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002914{
2915 if (!do_nhm_platform_info)
2916 return;
2917
2918 dump_nhm_platform_info();
2919
2920 if (has_hsw_turbo_ratio_limit(family, model))
2921 dump_hsw_turbo_ratio_limits();
2922
2923 if (has_ivt_turbo_ratio_limit(family, model))
2924 dump_ivt_turbo_ratio_limits();
2925
Len Brown31e07522017-01-31 23:07:49 -05002926 if (has_turbo_ratio_limit(family, model))
2927 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04002928
Len Brown0f7887c2017-01-12 23:49:18 -05002929 if (has_atom_turbo_ratio_limit(family, model))
2930 dump_atom_turbo_ratio_limits();
2931
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002932 if (has_knl_turbo_ratio_limit(family, model))
2933 dump_knl_turbo_ratio_limits();
2934
Len Brown6fb31432015-06-17 16:23:45 -04002935 if (has_config_tdp(family, model))
2936 dump_config_tdp();
2937
Len Brownfcd17212015-03-23 20:29:09 -04002938 dump_nhm_cst_cfg();
2939}
2940
Len Brown41618e62017-02-09 18:25:22 -05002941static void
2942dump_sysfs_cstate_config(void)
2943{
2944 char path[64];
2945 char name_buf[16];
2946 char desc[64];
2947 FILE *input;
2948 int state;
2949 char *sp;
2950
2951 if (!DO_BIC(BIC_sysfs))
2952 return;
2953
2954 for (state = 0; state < 10; ++state) {
2955
2956 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
2957 base_cpu, state);
2958 input = fopen(path, "r");
2959 if (input == NULL)
2960 continue;
2961 fgets(name_buf, sizeof(name_buf), input);
2962
2963 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
2964 sp = strchr(name_buf, '-');
2965 if (!sp)
2966 sp = strchrnul(name_buf, '\n');
2967 *sp = '\0';
2968
2969 fclose(input);
2970
2971 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
2972 base_cpu, state);
2973 input = fopen(path, "r");
2974 if (input == NULL)
2975 continue;
2976 fgets(desc, sizeof(desc), input);
2977
2978 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
2979 fclose(input);
2980 }
2981}
2982
Len Brown6574a5d2012-09-21 00:01:31 -04002983
Len Brown889facb2012-11-08 00:48:57 -05002984/*
2985 * print_epb()
2986 * Decode the ENERGY_PERF_BIAS MSR
2987 */
2988int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2989{
2990 unsigned long long msr;
2991 char *epb_string;
2992 int cpu;
2993
2994 if (!has_epb)
2995 return 0;
2996
2997 cpu = t->cpu_id;
2998
2999 /* EPB is per-package */
3000 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3001 return 0;
3002
3003 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003004 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003005 return -1;
3006 }
3007
3008 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3009 return 0;
3010
Len Browne9be7dd2015-05-26 12:19:37 -04003011 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003012 case ENERGY_PERF_BIAS_PERFORMANCE:
3013 epb_string = "performance";
3014 break;
3015 case ENERGY_PERF_BIAS_NORMAL:
3016 epb_string = "balanced";
3017 break;
3018 case ENERGY_PERF_BIAS_POWERSAVE:
3019 epb_string = "powersave";
3020 break;
3021 default:
3022 epb_string = "custom";
3023 break;
3024 }
Len Brownb7d8c142016-02-13 23:36:17 -05003025 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003026
3027 return 0;
3028}
Len Brown7f5c2582015-12-01 01:36:39 -05003029/*
3030 * print_hwp()
3031 * Decode the MSR_HWP_CAPABILITIES
3032 */
3033int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3034{
3035 unsigned long long msr;
3036 int cpu;
3037
3038 if (!has_hwp)
3039 return 0;
3040
3041 cpu = t->cpu_id;
3042
3043 /* MSR_HWP_CAPABILITIES is per-package */
3044 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3045 return 0;
3046
3047 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003048 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003049 return -1;
3050 }
3051
3052 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3053 return 0;
3054
Len Brownb7d8c142016-02-13 23:36:17 -05003055 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003056 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3057
3058 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3059 if ((msr & (1 << 0)) == 0)
3060 return 0;
3061
3062 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3063 return 0;
3064
Len Brownb7d8c142016-02-13 23:36:17 -05003065 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003066 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
3067 cpu, msr,
3068 (unsigned int)HWP_HIGHEST_PERF(msr),
3069 (unsigned int)HWP_GUARANTEED_PERF(msr),
3070 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3071 (unsigned int)HWP_LOWEST_PERF(msr));
3072
3073 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3074 return 0;
3075
Len Brownb7d8c142016-02-13 23:36:17 -05003076 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003077 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
3078 cpu, msr,
3079 (unsigned int)(((msr) >> 0) & 0xff),
3080 (unsigned int)(((msr) >> 8) & 0xff),
3081 (unsigned int)(((msr) >> 16) & 0xff),
3082 (unsigned int)(((msr) >> 24) & 0xff),
3083 (unsigned int)(((msr) >> 32) & 0xff3),
3084 (unsigned int)(((msr) >> 42) & 0x1));
3085
3086 if (has_hwp_pkg) {
3087 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3088 return 0;
3089
Len Brownb7d8c142016-02-13 23:36:17 -05003090 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003091 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
3092 cpu, msr,
3093 (unsigned int)(((msr) >> 0) & 0xff),
3094 (unsigned int)(((msr) >> 8) & 0xff),
3095 (unsigned int)(((msr) >> 16) & 0xff),
3096 (unsigned int)(((msr) >> 24) & 0xff),
3097 (unsigned int)(((msr) >> 32) & 0xff3));
3098 }
3099 if (has_hwp_notify) {
3100 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3101 return 0;
3102
Len Brownb7d8c142016-02-13 23:36:17 -05003103 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003104 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3105 cpu, msr,
3106 ((msr) & 0x1) ? "EN" : "Dis",
3107 ((msr) & 0x2) ? "EN" : "Dis");
3108 }
3109 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3110 return 0;
3111
Len Brownb7d8c142016-02-13 23:36:17 -05003112 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003113 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3114 cpu, msr,
3115 ((msr) & 0x1) ? "" : "No-",
3116 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003117
3118 return 0;
3119}
3120
Len Brown3a9a9412014-08-15 02:39:52 -04003121/*
3122 * print_perf_limit()
3123 */
3124int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3125{
3126 unsigned long long msr;
3127 int cpu;
3128
3129 cpu = t->cpu_id;
3130
3131 /* per-package */
3132 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3133 return 0;
3134
3135 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003136 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003137 return -1;
3138 }
3139
3140 if (do_core_perf_limit_reasons) {
3141 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003142 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3143 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003144 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003145 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003146 (msr & 1 << 13) ? "Transitions, " : "",
3147 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3148 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3149 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3150 (msr & 1 << 9) ? "CorePwr, " : "",
3151 (msr & 1 << 8) ? "Amps, " : "",
3152 (msr & 1 << 6) ? "VR-Therm, " : "",
3153 (msr & 1 << 5) ? "Auto-HWP, " : "",
3154 (msr & 1 << 4) ? "Graphics, " : "",
3155 (msr & 1 << 2) ? "bit2, " : "",
3156 (msr & 1 << 1) ? "ThermStatus, " : "",
3157 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003158 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 -04003159 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003160 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003161 (msr & 1 << 29) ? "Transitions, " : "",
3162 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3163 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3164 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3165 (msr & 1 << 25) ? "CorePwr, " : "",
3166 (msr & 1 << 24) ? "Amps, " : "",
3167 (msr & 1 << 22) ? "VR-Therm, " : "",
3168 (msr & 1 << 21) ? "Auto-HWP, " : "",
3169 (msr & 1 << 20) ? "Graphics, " : "",
3170 (msr & 1 << 18) ? "bit18, " : "",
3171 (msr & 1 << 17) ? "ThermStatus, " : "",
3172 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003173
3174 }
3175 if (do_gfx_perf_limit_reasons) {
3176 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003177 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3178 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003179 (msr & 1 << 0) ? "PROCHOT, " : "",
3180 (msr & 1 << 1) ? "ThermStatus, " : "",
3181 (msr & 1 << 4) ? "Graphics, " : "",
3182 (msr & 1 << 6) ? "VR-Therm, " : "",
3183 (msr & 1 << 8) ? "Amps, " : "",
3184 (msr & 1 << 9) ? "GFXPwr, " : "",
3185 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3186 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003187 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003188 (msr & 1 << 16) ? "PROCHOT, " : "",
3189 (msr & 1 << 17) ? "ThermStatus, " : "",
3190 (msr & 1 << 20) ? "Graphics, " : "",
3191 (msr & 1 << 22) ? "VR-Therm, " : "",
3192 (msr & 1 << 24) ? "Amps, " : "",
3193 (msr & 1 << 25) ? "GFXPwr, " : "",
3194 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3195 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3196 }
3197 if (do_ring_perf_limit_reasons) {
3198 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003199 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3200 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003201 (msr & 1 << 0) ? "PROCHOT, " : "",
3202 (msr & 1 << 1) ? "ThermStatus, " : "",
3203 (msr & 1 << 6) ? "VR-Therm, " : "",
3204 (msr & 1 << 8) ? "Amps, " : "",
3205 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3206 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003207 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003208 (msr & 1 << 16) ? "PROCHOT, " : "",
3209 (msr & 1 << 17) ? "ThermStatus, " : "",
3210 (msr & 1 << 22) ? "VR-Therm, " : "",
3211 (msr & 1 << 24) ? "Amps, " : "",
3212 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3213 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3214 }
3215 return 0;
3216}
3217
Len Brown889facb2012-11-08 00:48:57 -05003218#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3219#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3220
Colin Ian King1b693172016-03-02 13:50:25 +00003221double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003222{
3223 unsigned long long msr;
3224
3225 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003226 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003227 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3228
3229 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003230 case INTEL_FAM6_ATOM_SILVERMONT1:
3231 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003232 return 30.0;
3233 default:
3234 return 135.0;
3235 }
3236}
3237
Andrey Semin40ee8e32014-12-05 00:07:00 -05003238/*
3239 * rapl_dram_energy_units_probe()
3240 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3241 */
3242static double
3243rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3244{
3245 /* only called for genuine_intel, family 6 */
3246
3247 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003248 case INTEL_FAM6_HASWELL_X: /* HSX */
3249 case INTEL_FAM6_BROADWELL_X: /* BDX */
3250 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3251 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003252 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003253 return (rapl_dram_energy_units = 15.3 / 1000000);
3254 default:
3255 return (rapl_energy_units);
3256 }
3257}
3258
Len Brown144b44b2013-11-09 00:30:16 -05003259
Len Brown889facb2012-11-08 00:48:57 -05003260/*
3261 * rapl_probe()
3262 *
Len Brown144b44b2013-11-09 00:30:16 -05003263 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003264 */
3265void rapl_probe(unsigned int family, unsigned int model)
3266{
3267 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003268 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003269 double tdp;
3270
3271 if (!genuine_intel)
3272 return;
3273
3274 if (family != 6)
3275 return;
3276
3277 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003278 case INTEL_FAM6_SANDYBRIDGE:
3279 case INTEL_FAM6_IVYBRIDGE:
3280 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3281 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3282 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3283 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3284 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003285 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003286 if (rapl_joules) {
3287 BIC_PRESENT(BIC_Pkg_J);
3288 BIC_PRESENT(BIC_Cor_J);
3289 BIC_PRESENT(BIC_GFX_J);
3290 } else {
3291 BIC_PRESENT(BIC_PkgWatt);
3292 BIC_PRESENT(BIC_CorWatt);
3293 BIC_PRESENT(BIC_GFXWatt);
3294 }
Len Brown889facb2012-11-08 00:48:57 -05003295 break;
Len Brown869ce692016-06-16 23:22:37 -04003296 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003297 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003298 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003299 if (rapl_joules)
3300 BIC_PRESENT(BIC_Pkg_J);
3301 else
3302 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003303 break;
Len Brown869ce692016-06-16 23:22:37 -04003304 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3305 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3306 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3307 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003308 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 -05003309 BIC_PRESENT(BIC_PKG__);
3310 BIC_PRESENT(BIC_RAM__);
3311 if (rapl_joules) {
3312 BIC_PRESENT(BIC_Pkg_J);
3313 BIC_PRESENT(BIC_Cor_J);
3314 BIC_PRESENT(BIC_RAM_J);
3315 } else {
3316 BIC_PRESENT(BIC_PkgWatt);
3317 BIC_PRESENT(BIC_CorWatt);
3318 BIC_PRESENT(BIC_RAMWatt);
3319 }
Len Brown0b2bb692015-03-26 00:50:30 -04003320 break;
Len Brown869ce692016-06-16 23:22:37 -04003321 case INTEL_FAM6_HASWELL_X: /* HSX */
3322 case INTEL_FAM6_BROADWELL_X: /* BDX */
3323 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3324 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3325 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003326 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003327 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 -05003328 BIC_PRESENT(BIC_PKG__);
3329 BIC_PRESENT(BIC_RAM__);
3330 if (rapl_joules) {
3331 BIC_PRESENT(BIC_Pkg_J);
3332 BIC_PRESENT(BIC_RAM_J);
3333 } else {
3334 BIC_PRESENT(BIC_PkgWatt);
3335 BIC_PRESENT(BIC_RAMWatt);
3336 }
Len Browne6f9bb32013-12-03 02:19:19 -05003337 break;
Len Brown869ce692016-06-16 23:22:37 -04003338 case INTEL_FAM6_SANDYBRIDGE_X:
3339 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003340 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 -05003341 BIC_PRESENT(BIC_PKG__);
3342 BIC_PRESENT(BIC_RAM__);
3343 if (rapl_joules) {
3344 BIC_PRESENT(BIC_Pkg_J);
3345 BIC_PRESENT(BIC_Cor_J);
3346 BIC_PRESENT(BIC_RAM_J);
3347 } else {
3348 BIC_PRESENT(BIC_PkgWatt);
3349 BIC_PRESENT(BIC_CorWatt);
3350 BIC_PRESENT(BIC_RAMWatt);
3351 }
Len Brown144b44b2013-11-09 00:30:16 -05003352 break;
Len Brown869ce692016-06-16 23:22:37 -04003353 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3354 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003355 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003356 if (rapl_joules) {
3357 BIC_PRESENT(BIC_Pkg_J);
3358 BIC_PRESENT(BIC_Cor_J);
3359 } else {
3360 BIC_PRESENT(BIC_PkgWatt);
3361 BIC_PRESENT(BIC_CorWatt);
3362 }
Len Brown889facb2012-11-08 00:48:57 -05003363 break;
Len Brown869ce692016-06-16 23:22:37 -04003364 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003365 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 -05003366 BIC_PRESENT(BIC_PKG__);
3367 BIC_PRESENT(BIC_RAM__);
3368 if (rapl_joules) {
3369 BIC_PRESENT(BIC_Pkg_J);
3370 BIC_PRESENT(BIC_Cor_J);
3371 BIC_PRESENT(BIC_RAM_J);
3372 } else {
3373 BIC_PRESENT(BIC_PkgWatt);
3374 BIC_PRESENT(BIC_CorWatt);
3375 BIC_PRESENT(BIC_RAMWatt);
3376 }
Jacob Pan0f644902016-06-16 09:48:22 -07003377 break;
Len Brown889facb2012-11-08 00:48:57 -05003378 default:
3379 return;
3380 }
3381
3382 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003383 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003384 return;
3385
3386 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003387 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003388 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3389 else
3390 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003391
Andrey Semin40ee8e32014-12-05 00:07:00 -05003392 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3393
Len Brown144b44b2013-11-09 00:30:16 -05003394 time_unit = msr >> 16 & 0xF;
3395 if (time_unit == 0)
3396 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003397
Len Brown144b44b2013-11-09 00:30:16 -05003398 rapl_time_units = 1.0 / (1 << (time_unit));
3399
3400 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003401
3402 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003403 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003404 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003405
3406 return;
3407}
3408
Colin Ian King1b693172016-03-02 13:50:25 +00003409void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003410{
3411 if (!genuine_intel)
3412 return;
3413
3414 if (family != 6)
3415 return;
3416
3417 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003418 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3419 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3420 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003421 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003422 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003423 do_core_perf_limit_reasons = 1;
3424 do_ring_perf_limit_reasons = 1;
3425 default:
3426 return;
3427 }
3428}
3429
Len Brown889facb2012-11-08 00:48:57 -05003430int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3431{
3432 unsigned long long msr;
3433 unsigned int dts;
3434 int cpu;
3435
3436 if (!(do_dts || do_ptm))
3437 return 0;
3438
3439 cpu = t->cpu_id;
3440
3441 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003442 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003443 return 0;
3444
3445 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003446 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003447 return -1;
3448 }
3449
3450 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3451 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3452 return 0;
3453
3454 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003455 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003456 cpu, msr, tcc_activation_temp - dts);
3457
3458#ifdef THERM_DEBUG
3459 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3460 return 0;
3461
3462 dts = (msr >> 16) & 0x7F;
3463 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003464 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003465 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3466#endif
3467 }
3468
3469
3470 if (do_dts) {
3471 unsigned int resolution;
3472
3473 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3474 return 0;
3475
3476 dts = (msr >> 16) & 0x7F;
3477 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003478 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003479 cpu, msr, tcc_activation_temp - dts, resolution);
3480
3481#ifdef THERM_DEBUG
3482 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3483 return 0;
3484
3485 dts = (msr >> 16) & 0x7F;
3486 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003487 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003488 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3489#endif
3490 }
3491
3492 return 0;
3493}
Len Brown36229892016-02-26 20:51:02 -05003494
Len Brown889facb2012-11-08 00:48:57 -05003495void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3496{
Len Brownb7d8c142016-02-13 23:36:17 -05003497 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003498 cpu, label,
3499 ((msr >> 15) & 1) ? "EN" : "DIS",
3500 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3501 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3502 (((msr >> 16) & 1) ? "EN" : "DIS"));
3503
3504 return;
3505}
3506
3507int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3508{
3509 unsigned long long msr;
3510 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003511
3512 if (!do_rapl)
3513 return 0;
3514
3515 /* RAPL counters are per package, so print only for 1st thread/package */
3516 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3517 return 0;
3518
3519 cpu = t->cpu_id;
3520 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003521 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003522 return -1;
3523 }
3524
3525 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3526 return -1;
3527
Len Brown96e47152017-01-21 02:26:00 -05003528 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3529 rapl_power_units, rapl_energy_units, rapl_time_units);
3530
Len Brown144b44b2013-11-09 00:30:16 -05003531 if (do_rapl & RAPL_PKG_POWER_INFO) {
3532
Len Brown889facb2012-11-08 00:48:57 -05003533 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3534 return -5;
3535
3536
Len Brownb7d8c142016-02-13 23:36:17 -05003537 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 -05003538 cpu, msr,
3539 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3540 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3541 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3542 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3543
Len Brown144b44b2013-11-09 00:30:16 -05003544 }
3545 if (do_rapl & RAPL_PKG) {
3546
Len Brown889facb2012-11-08 00:48:57 -05003547 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3548 return -9;
3549
Len Brownb7d8c142016-02-13 23:36:17 -05003550 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003551 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003552
3553 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003554 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003555 cpu,
3556 ((msr >> 47) & 1) ? "EN" : "DIS",
3557 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3558 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3559 ((msr >> 48) & 1) ? "EN" : "DIS");
3560 }
3561
Len Brown0b2bb692015-03-26 00:50:30 -04003562 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003563 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3564 return -6;
3565
Len Brownb7d8c142016-02-13 23:36:17 -05003566 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 -05003567 cpu, msr,
3568 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3569 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3570 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3571 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003572 }
3573 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003574 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3575 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003576 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003577 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003578
3579 print_power_limit_msr(cpu, msr, "DRAM Limit");
3580 }
Len Brown144b44b2013-11-09 00:30:16 -05003581 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003582 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3583 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003584
Len Brown96e47152017-01-21 02:26:00 -05003585 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003586 }
Jacob Pan91484942016-06-16 09:48:20 -07003587 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003588 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3589 return -9;
3590 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3591 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3592 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003593 }
3594 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003595 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3596 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003597
Len Brown96e47152017-01-21 02:26:00 -05003598 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003599
Len Brown96e47152017-01-21 02:26:00 -05003600 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3601 return -9;
3602 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3603 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3604 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003605 }
3606 return 0;
3607}
3608
Len Brownd7899442015-01-23 00:12:33 -05003609/*
3610 * SNB adds support for additional MSRs:
3611 *
3612 * MSR_PKG_C7_RESIDENCY 0x000003fa
3613 * MSR_CORE_C7_RESIDENCY 0x000003fe
3614 * MSR_PKG_C2_RESIDENCY 0x0000060d
3615 */
Len Brown103a8fe2010-10-22 23:53:03 -04003616
Len Brownd7899442015-01-23 00:12:33 -05003617int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003618{
3619 if (!genuine_intel)
3620 return 0;
3621
3622 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003623 case INTEL_FAM6_SANDYBRIDGE:
3624 case INTEL_FAM6_SANDYBRIDGE_X:
3625 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3626 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3627 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3628 case INTEL_FAM6_HASWELL_X: /* HSW */
3629 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3630 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3631 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3632 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3633 case INTEL_FAM6_BROADWELL_X: /* BDX */
3634 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3635 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3636 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3637 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3638 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3639 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3640 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003641 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003642 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003643 return 1;
3644 }
3645 return 0;
3646}
3647
Len Brownd7899442015-01-23 00:12:33 -05003648/*
3649 * HSW adds support for additional MSRs:
3650 *
Len Brown5a634262016-04-06 17:15:55 -04003651 * MSR_PKG_C8_RESIDENCY 0x00000630
3652 * MSR_PKG_C9_RESIDENCY 0x00000631
3653 * MSR_PKG_C10_RESIDENCY 0x00000632
3654 *
3655 * MSR_PKGC8_IRTL 0x00000633
3656 * MSR_PKGC9_IRTL 0x00000634
3657 * MSR_PKGC10_IRTL 0x00000635
3658 *
Len Brownd7899442015-01-23 00:12:33 -05003659 */
3660int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003661{
3662 if (!genuine_intel)
3663 return 0;
3664
3665 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003666 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3667 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3668 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3669 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3670 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3671 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3672 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003673 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003674 return 1;
3675 }
3676 return 0;
3677}
3678
Len Brown0b2bb692015-03-26 00:50:30 -04003679/*
3680 * SKL adds support for additional MSRS:
3681 *
3682 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3683 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3684 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3685 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3686 */
3687int has_skl_msrs(unsigned int family, unsigned int model)
3688{
3689 if (!genuine_intel)
3690 return 0;
3691
3692 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003693 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3694 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3695 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3696 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003697 return 1;
3698 }
3699 return 0;
3700}
3701
Len Brown144b44b2013-11-09 00:30:16 -05003702int is_slm(unsigned int family, unsigned int model)
3703{
3704 if (!genuine_intel)
3705 return 0;
3706 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003707 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3708 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003709 return 1;
3710 }
3711 return 0;
3712}
3713
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003714int is_knl(unsigned int family, unsigned int model)
3715{
3716 if (!genuine_intel)
3717 return 0;
3718 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003719 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003720 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003721 return 1;
3722 }
3723 return 0;
3724}
3725
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003726unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3727{
3728 if (is_knl(family, model))
3729 return 1024;
3730 return 1;
3731}
3732
Len Brown144b44b2013-11-09 00:30:16 -05003733#define SLM_BCLK_FREQS 5
3734double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3735
3736double slm_bclk(void)
3737{
3738 unsigned long long msr = 3;
3739 unsigned int i;
3740 double freq;
3741
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003742 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003743 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003744
3745 i = msr & 0xf;
3746 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003747 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003748 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003749 }
3750 freq = slm_freq_table[i];
3751
Len Brown96e47152017-01-21 02:26:00 -05003752 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003753 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003754
3755 return freq;
3756}
3757
Len Brown103a8fe2010-10-22 23:53:03 -04003758double discover_bclk(unsigned int family, unsigned int model)
3759{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003760 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003761 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003762 else if (is_slm(family, model))
3763 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003764 else
3765 return 133.33;
3766}
3767
Len Brown889facb2012-11-08 00:48:57 -05003768/*
3769 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3770 * the Thermal Control Circuit (TCC) activates.
3771 * This is usually equal to tjMax.
3772 *
3773 * Older processors do not have this MSR, so there we guess,
3774 * but also allow cmdline over-ride with -T.
3775 *
3776 * Several MSR temperature values are in units of degrees-C
3777 * below this value, including the Digital Thermal Sensor (DTS),
3778 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3779 */
3780int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3781{
3782 unsigned long long msr;
3783 unsigned int target_c_local;
3784 int cpu;
3785
3786 /* tcc_activation_temp is used only for dts or ptm */
3787 if (!(do_dts || do_ptm))
3788 return 0;
3789
3790 /* this is a per-package concept */
3791 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3792 return 0;
3793
3794 cpu = t->cpu_id;
3795 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003796 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003797 return -1;
3798 }
3799
3800 if (tcc_activation_temp_override != 0) {
3801 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003802 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003803 cpu, tcc_activation_temp);
3804 return 0;
3805 }
3806
3807 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003808 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003809 goto guess;
3810
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003811 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003812 goto guess;
3813
Jean Delvare34821242014-05-01 11:40:19 +02003814 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003815
Len Brown96e47152017-01-21 02:26:00 -05003816 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003817 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003818 cpu, msr, target_c_local);
3819
Jean Delvare34821242014-05-01 11:40:19 +02003820 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003821 goto guess;
3822
3823 tcc_activation_temp = target_c_local;
3824
3825 return 0;
3826
3827guess:
3828 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003829 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003830 cpu, tcc_activation_temp);
3831
3832 return 0;
3833}
Len Brown69807a62015-11-21 12:22:47 -05003834
Len Brownaa8d8cc2016-03-11 13:26:03 -05003835void decode_feature_control_msr(void)
3836{
3837 unsigned long long msr;
3838
3839 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3840 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3841 base_cpu, msr,
3842 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3843 msr & (1 << 18) ? "SGX" : "");
3844}
3845
Len Brown69807a62015-11-21 12:22:47 -05003846void decode_misc_enable_msr(void)
3847{
3848 unsigned long long msr;
3849
3850 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003851 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003852 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003853 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3854 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3855 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3856 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3857 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003858}
3859
Len Brown33148d62017-01-21 01:26:16 -05003860void decode_misc_feature_control(void)
3861{
3862 unsigned long long msr;
3863
3864 if (!has_misc_feature_control)
3865 return;
3866
3867 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
3868 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
3869 base_cpu, msr,
3870 msr & (0 << 0) ? "No-" : "",
3871 msr & (1 << 0) ? "No-" : "",
3872 msr & (2 << 0) ? "No-" : "",
3873 msr & (3 << 0) ? "No-" : "");
3874}
Len Brownf0057312015-12-03 01:35:36 -05003875/*
3876 * Decode MSR_MISC_PWR_MGMT
3877 *
3878 * Decode the bits according to the Nehalem documentation
3879 * bit[0] seems to continue to have same meaning going forward
3880 * bit[1] less so...
3881 */
3882void decode_misc_pwr_mgmt_msr(void)
3883{
3884 unsigned long long msr;
3885
3886 if (!do_nhm_platform_info)
3887 return;
3888
Len Browncf4cbe52017-01-01 13:08:33 -05003889 if (no_MSR_MISC_PWR_MGMT)
3890 return;
3891
Len Brownf0057312015-12-03 01:35:36 -05003892 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003893 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 -05003894 base_cpu, msr,
3895 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003896 msr & (1 << 1) ? "EN" : "DIS",
3897 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003898}
Len Brown71616c82017-01-07 22:37:48 -05003899/*
3900 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3901 *
3902 * This MSRs are present on Silvermont processors,
3903 * Intel Atom processor E3000 series (Baytrail), and friends.
3904 */
3905void decode_c6_demotion_policy_msr(void)
3906{
3907 unsigned long long msr;
3908
3909 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3910 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3911 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3912
3913 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3914 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3915 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3916}
Len Brown7f5c2582015-12-01 01:36:39 -05003917
Len Brownfcd17212015-03-23 20:29:09 -04003918void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003919{
Len Brown61a87ba2015-11-23 02:30:51 -05003920 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003921 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003922 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003923
3924 eax = ebx = ecx = edx = 0;
3925
Len Brown5aea2f72016-03-13 03:14:35 -04003926 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003927
3928 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3929 genuine_intel = 1;
3930
Len Brown96e47152017-01-21 02:26:00 -05003931 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003932 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003933 (char *)&ebx, (char *)&edx, (char *)&ecx);
3934
Len Brown5aea2f72016-03-13 03:14:35 -04003935 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003936 family = (fms >> 8) & 0xf;
3937 model = (fms >> 4) & 0xf;
3938 stepping = fms & 0xf;
3939 if (family == 6 || family == 0xf)
3940 model += ((fms >> 16) & 0xf) << 4;
3941
Len Brown96e47152017-01-21 02:26:00 -05003942 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05003943 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003944 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003945 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003946 ecx & (1 << 0) ? "SSE3" : "-",
3947 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003948 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003949 ecx & (1 << 7) ? "EIST" : "-",
3950 ecx & (1 << 8) ? "TM2" : "-",
3951 edx & (1 << 4) ? "TSC" : "-",
3952 edx & (1 << 5) ? "MSR" : "-",
3953 edx & (1 << 22) ? "ACPI-TM" : "-",
3954 edx & (1 << 29) ? "TM" : "-");
3955 }
Len Brown103a8fe2010-10-22 23:53:03 -04003956
Josh Triplettb2c95d92013-08-20 17:20:18 -07003957 if (!(edx & (1 << 5)))
3958 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003959
3960 /*
3961 * check max extended function levels of CPUID.
3962 * This is needed to check for invariant TSC.
3963 * This check is valid for both Intel and AMD.
3964 */
3965 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003966 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003967
Len Brown61a87ba2015-11-23 02:30:51 -05003968 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003969
Len Brownd7899442015-01-23 00:12:33 -05003970 /*
3971 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3972 * this check is valid for both Intel and AMD
3973 */
Len Brown5aea2f72016-03-13 03:14:35 -04003974 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003975 has_invariant_tsc = edx & (1 << 8);
3976 }
Len Brown103a8fe2010-10-22 23:53:03 -04003977
3978 /*
3979 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3980 * this check is valid for both Intel and AMD
3981 */
3982
Len Brown5aea2f72016-03-13 03:14:35 -04003983 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003984 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003985 if (has_aperf) {
3986 BIC_PRESENT(BIC_Avg_MHz);
3987 BIC_PRESENT(BIC_Busy);
3988 BIC_PRESENT(BIC_Bzy_MHz);
3989 }
Len Brown889facb2012-11-08 00:48:57 -05003990 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003991 if (do_dts)
3992 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05003993 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05003994 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003995 if (do_ptm)
3996 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003997 has_hwp = eax & (1 << 7);
3998 has_hwp_notify = eax & (1 << 8);
3999 has_hwp_activity_window = eax & (1 << 9);
4000 has_hwp_epp = eax & (1 << 10);
4001 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004002 has_epb = ecx & (1 << 3);
4003
Len Brown96e47152017-01-21 02:26:00 -05004004 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004005 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004006 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4007 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004008 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004009 do_dts ? "" : "No-",
4010 do_ptm ? "" : "No-",
4011 has_hwp ? "" : "No-",
4012 has_hwp_notify ? "" : "No-",
4013 has_hwp_activity_window ? "" : "No-",
4014 has_hwp_epp ? "" : "No-",
4015 has_hwp_pkg ? "" : "No-",
4016 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004017
Len Brown96e47152017-01-21 02:26:00 -05004018 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004019 decode_misc_enable_msr();
4020
Len Brown33148d62017-01-21 01:26:16 -05004021
Len Brown96e47152017-01-21 02:26:00 -05004022 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004023 int has_sgx;
4024
4025 ecx = 0;
4026
4027 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4028
4029 has_sgx = ebx & (1 << 2);
4030 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4031
4032 if (has_sgx)
4033 decode_feature_control_msr();
4034 }
4035
Len Brown61a87ba2015-11-23 02:30:51 -05004036 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004037 unsigned int eax_crystal;
4038 unsigned int ebx_tsc;
4039
4040 /*
4041 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4042 */
4043 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004044 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004045
4046 if (ebx_tsc != 0) {
4047
Len Brown96e47152017-01-21 02:26:00 -05004048 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004049 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004050 eax_crystal, ebx_tsc, crystal_hz);
4051
4052 if (crystal_hz == 0)
4053 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004054 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4055 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4056 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4057 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004058 crystal_hz = 24000000; /* 24.0 MHz */
4059 break;
Len Brown869ce692016-06-16 23:22:37 -04004060 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05004061 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004062 crystal_hz = 25000000; /* 25.0 MHz */
4063 break;
Len Brown869ce692016-06-16 23:22:37 -04004064 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004065 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004066 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004067 break;
4068 default:
4069 crystal_hz = 0;
4070 }
4071
4072 if (crystal_hz) {
4073 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004074 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004075 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004076 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4077 }
4078 }
4079 }
Len Brown61a87ba2015-11-23 02:30:51 -05004080 if (max_level >= 0x16) {
4081 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4082
4083 /*
4084 * CPUID 16H Base MHz, Max MHz, Bus MHz
4085 */
4086 base_mhz = max_mhz = bus_mhz = edx = 0;
4087
Len Brown5aea2f72016-03-13 03:14:35 -04004088 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004089 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004090 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004091 base_mhz, max_mhz, bus_mhz);
4092 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004093
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004094 if (has_aperf)
4095 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4096
Len Brown812db3f2017-02-10 00:25:41 -05004097 BIC_PRESENT(BIC_IRQ);
4098 BIC_PRESENT(BIC_TSC_MHz);
4099
4100 if (probe_nhm_msrs(family, model)) {
4101 do_nhm_platform_info = 1;
4102 BIC_PRESENT(BIC_CPU_c1);
4103 BIC_PRESENT(BIC_CPU_c3);
4104 BIC_PRESENT(BIC_CPU_c6);
4105 BIC_PRESENT(BIC_SMI);
4106 }
Len Brownd7899442015-01-23 00:12:33 -05004107 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004108
4109 if (do_snb_cstates)
4110 BIC_PRESENT(BIC_CPU_c7);
4111
Len Brown5a634262016-04-06 17:15:55 -04004112 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004113 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4114 BIC_PRESENT(BIC_Pkgpc2);
4115 if (pkg_cstate_limit >= PCL__3)
4116 BIC_PRESENT(BIC_Pkgpc3);
4117 if (pkg_cstate_limit >= PCL__6)
4118 BIC_PRESENT(BIC_Pkgpc6);
4119 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4120 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004121 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004122 BIC_NOT_PRESENT(BIC_Pkgpc2);
4123 BIC_NOT_PRESENT(BIC_Pkgpc3);
4124 BIC_PRESENT(BIC_Pkgpc6);
4125 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004126 BIC_PRESENT(BIC_Mod_c6);
4127 use_c1_residency_msr = 1;
4128 }
Len Brown7170a372017-01-27 02:13:27 -05004129 if (is_dnv(family, model)) {
4130 BIC_PRESENT(BIC_CPU_c1);
4131 BIC_NOT_PRESENT(BIC_CPU_c3);
4132 BIC_NOT_PRESENT(BIC_Pkgpc3);
4133 BIC_NOT_PRESENT(BIC_CPU_c7);
4134 BIC_NOT_PRESENT(BIC_Pkgpc7);
4135 use_c1_residency_msr = 1;
4136 }
Len Brown34c761972017-01-27 02:36:41 -05004137 if (is_skx(family, model)) {
4138 BIC_NOT_PRESENT(BIC_CPU_c3);
4139 BIC_NOT_PRESENT(BIC_Pkgpc3);
4140 BIC_NOT_PRESENT(BIC_CPU_c7);
4141 BIC_NOT_PRESENT(BIC_Pkgpc7);
4142 }
Len Brownade0eba2017-02-10 01:56:47 -05004143 if (is_bdx(family, model)) {
4144 BIC_NOT_PRESENT(BIC_CPU_c7);
4145 BIC_NOT_PRESENT(BIC_Pkgpc7);
4146 }
Len Brown0f47c082017-01-27 00:50:45 -05004147 if (has_hsw_msrs(family, model)) {
4148 BIC_PRESENT(BIC_Pkgpc8);
4149 BIC_PRESENT(BIC_Pkgpc9);
4150 BIC_PRESENT(BIC_Pkgpc10);
4151 }
Len Brown5a634262016-04-06 17:15:55 -04004152 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04004153 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05004154 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004155 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004156
Len Brown96e47152017-01-21 02:26:00 -05004157 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004158 decode_misc_pwr_mgmt_msr();
4159
Len Brown96e47152017-01-21 02:26:00 -05004160 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004161 decode_c6_demotion_policy_msr();
4162
Len Brown889facb2012-11-08 00:48:57 -05004163 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004164 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004165
Len Brown96e47152017-01-21 02:26:00 -05004166 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004167 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004168
Len Brown41618e62017-02-09 18:25:22 -05004169 if (!quiet)
4170 dump_sysfs_cstate_config();
4171
Len Browna2b7b742015-09-26 00:12:38 -04004172 if (has_skl_msrs(family, model))
4173 calculate_tsc_tweak();
4174
Len Brown812db3f2017-02-10 00:25:41 -05004175 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4176 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004177
Len Brown812db3f2017-02-10 00:25:41 -05004178 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4179 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004180
Len Brown96e47152017-01-21 02:26:00 -05004181 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004182 decode_misc_feature_control();
4183
Len Brown889facb2012-11-08 00:48:57 -05004184 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004185}
4186
Len Brown103a8fe2010-10-22 23:53:03 -04004187
4188/*
4189 * in /dev/cpu/ return success for names that are numbers
4190 * ie. filter out ".", "..", "microcode".
4191 */
4192int dir_filter(const struct dirent *dirp)
4193{
4194 if (isdigit(dirp->d_name[0]))
4195 return 1;
4196 else
4197 return 0;
4198}
4199
4200int open_dev_cpu_msr(int dummy1)
4201{
4202 return 0;
4203}
4204
Len Brownc98d5d92012-06-04 00:56:40 -04004205void topology_probe()
4206{
4207 int i;
4208 int max_core_id = 0;
4209 int max_package_id = 0;
4210 int max_siblings = 0;
4211 struct cpu_topology {
4212 int core_id;
4213 int physical_package_id;
4214 } *cpus;
4215
4216 /* Initialize num_cpus, max_cpu_num */
4217 topo.num_cpus = 0;
4218 topo.max_cpu_num = 0;
4219 for_all_proc_cpus(count_cpus);
4220 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004221 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004222
Len Brownd8af6f52015-02-10 01:56:38 -05004223 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004224 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004225
4226 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004227 if (cpus == NULL)
4228 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004229
4230 /*
4231 * Allocate and initialize cpu_present_set
4232 */
4233 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004234 if (cpu_present_set == NULL)
4235 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004236 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4237 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4238 for_all_proc_cpus(mark_cpu_present);
4239
4240 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004241 * Validate that all cpus in cpu_subset are also in cpu_present_set
4242 */
4243 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4244 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4245 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4246 err(1, "cpu%d not present", i);
4247 }
4248
4249 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004250 * Allocate and initialize cpu_affinity_set
4251 */
4252 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004253 if (cpu_affinity_set == NULL)
4254 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004255 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4256 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4257
4258
4259 /*
4260 * For online cpus
4261 * find max_core_id, max_package_id
4262 */
4263 for (i = 0; i <= topo.max_cpu_num; ++i) {
4264 int siblings;
4265
4266 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004267 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004268 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004269 continue;
4270 }
4271 cpus[i].core_id = get_core_id(i);
4272 if (cpus[i].core_id > max_core_id)
4273 max_core_id = cpus[i].core_id;
4274
4275 cpus[i].physical_package_id = get_physical_package_id(i);
4276 if (cpus[i].physical_package_id > max_package_id)
4277 max_package_id = cpus[i].physical_package_id;
4278
4279 siblings = get_num_ht_siblings(i);
4280 if (siblings > max_siblings)
4281 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004282 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004283 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004284 i, cpus[i].physical_package_id, cpus[i].core_id);
4285 }
4286 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004287 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004288 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004289 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004290 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004291 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004292
4293 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004294 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004295 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004296 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004297 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004298 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004299
4300 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004301 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004302 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004303
4304 free(cpus);
4305}
4306
4307void
4308allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4309{
4310 int i;
4311
4312 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004313 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004314 if (*t == NULL)
4315 goto error;
4316
4317 for (i = 0; i < topo.num_threads_per_core *
4318 topo.num_cores_per_pkg * topo.num_packages; i++)
4319 (*t)[i].cpu_id = -1;
4320
4321 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004322 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004323 if (*c == NULL)
4324 goto error;
4325
4326 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4327 (*c)[i].core_id = -1;
4328
Len Brown678a3bd2017-02-09 22:22:13 -05004329 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004330 if (*p == NULL)
4331 goto error;
4332
4333 for (i = 0; i < topo.num_packages; i++)
4334 (*p)[i].package_id = i;
4335
4336 return;
4337error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004338 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004339}
4340/*
4341 * init_counter()
4342 *
4343 * set cpu_id, core_num, pkg_num
4344 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4345 *
4346 * increment topo.num_cores when 1st core in pkg seen
4347 */
4348void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4349 struct pkg_data *pkg_base, int thread_num, int core_num,
4350 int pkg_num, int cpu_id)
4351{
4352 struct thread_data *t;
4353 struct core_data *c;
4354 struct pkg_data *p;
4355
4356 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4357 c = GET_CORE(core_base, core_num, pkg_num);
4358 p = GET_PKG(pkg_base, pkg_num);
4359
4360 t->cpu_id = cpu_id;
4361 if (thread_num == 0) {
4362 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4363 if (cpu_is_first_core_in_package(cpu_id))
4364 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4365 }
4366
4367 c->core_id = core_num;
4368 p->package_id = pkg_num;
4369}
4370
4371
4372int initialize_counters(int cpu_id)
4373{
4374 int my_thread_id, my_core_id, my_package_id;
4375
4376 my_package_id = get_physical_package_id(cpu_id);
4377 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004378 my_thread_id = get_cpu_position_in_core(cpu_id);
4379 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004380 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004381
4382 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4383 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4384 return 0;
4385}
4386
4387void allocate_output_buffer()
4388{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004389 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004390 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004391 if (outp == NULL)
4392 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004393}
Len Brown36229892016-02-26 20:51:02 -05004394void allocate_fd_percpu(void)
4395{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004396 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004397 if (fd_percpu == NULL)
4398 err(-1, "calloc fd_percpu");
4399}
Len Brown562a2d32016-02-26 23:48:05 -05004400void allocate_irq_buffers(void)
4401{
4402 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4403 if (irq_column_2_cpu == NULL)
4404 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004405
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004406 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004407 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004408 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004409}
Len Brownc98d5d92012-06-04 00:56:40 -04004410void setup_all_buffers(void)
4411{
4412 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004413 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004414 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004415 allocate_counters(&thread_even, &core_even, &package_even);
4416 allocate_counters(&thread_odd, &core_odd, &package_odd);
4417 allocate_output_buffer();
4418 for_all_proc_cpus(initialize_counters);
4419}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004420
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004421void set_base_cpu(void)
4422{
4423 base_cpu = sched_getcpu();
4424 if (base_cpu < 0)
4425 err(-ENODEV, "No valid cpus found");
4426
4427 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004428 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004429}
4430
Len Brown103a8fe2010-10-22 23:53:03 -04004431void turbostat_init()
4432{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004433 setup_all_buffers();
4434 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004435 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004436 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004437 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004438
Len Brown103a8fe2010-10-22 23:53:03 -04004439
Len Brown96e47152017-01-21 02:26:00 -05004440 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004441 for_all_cpus(print_hwp, ODD_COUNTERS);
4442
Len Brown96e47152017-01-21 02:26:00 -05004443 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004444 for_all_cpus(print_epb, ODD_COUNTERS);
4445
Len Brown96e47152017-01-21 02:26:00 -05004446 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004447 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4448
Len Brown96e47152017-01-21 02:26:00 -05004449 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004450 for_all_cpus(print_rapl, ODD_COUNTERS);
4451
4452 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4453
Len Brown96e47152017-01-21 02:26:00 -05004454 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004455 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004456
Len Brown96e47152017-01-21 02:26:00 -05004457 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004458 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004459}
4460
4461int fork_it(char **argv)
4462{
Len Brown103a8fe2010-10-22 23:53:03 -04004463 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004464 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004465
Len Brown218f0e82017-02-14 22:07:52 -05004466 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004467 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4468 if (status)
4469 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004470 /* clear affinity side-effect of get_counters() */
4471 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004472 gettimeofday(&tv_even, (struct timezone *)NULL);
4473
4474 child_pid = fork();
4475 if (!child_pid) {
4476 /* child */
4477 execvp(argv[0], argv);
4478 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004479
4480 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004481 if (child_pid == -1)
4482 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004483
4484 signal(SIGINT, SIG_IGN);
4485 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004486 if (waitpid(child_pid, &status, 0) == -1)
4487 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004488 }
Len Brownc98d5d92012-06-04 00:56:40 -04004489 /*
4490 * n.b. fork_it() does not check for errors from for_all_cpus()
4491 * because re-starting is problematic when forking
4492 */
Len Brown218f0e82017-02-14 22:07:52 -05004493 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004494 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004495 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004496 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004497 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4498 fprintf(outf, "%s: Counter reset detected\n", progname);
4499 else {
4500 compute_average(EVEN_COUNTERS);
4501 format_all_counters(EVEN_COUNTERS);
4502 }
Len Brown103a8fe2010-10-22 23:53:03 -04004503
Len Brownb7d8c142016-02-13 23:36:17 -05004504 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4505
4506 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004507
Len Brownd91bb172012-11-01 00:08:19 -04004508 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004509}
4510
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004511int get_and_dump_counters(void)
4512{
4513 int status;
4514
Len Brown218f0e82017-02-14 22:07:52 -05004515 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004516 status = for_all_cpus(get_counters, ODD_COUNTERS);
4517 if (status)
4518 return status;
4519
4520 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4521 if (status)
4522 return status;
4523
Len Brownb7d8c142016-02-13 23:36:17 -05004524 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004525
4526 return status;
4527}
4528
Len Brownd8af6f52015-02-10 01:56:38 -05004529void print_version() {
Len Brown0539ba12017-02-10 00:27:20 -05004530 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004531 " - Len Brown <lenb@kernel.org>\n");
4532}
4533
Len Brown495c76542017-02-08 02:41:51 -05004534int add_counter(unsigned int msr_num, char *path, char *name,
4535 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004536 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004537{
4538 struct msr_counter *msrp;
4539
4540 msrp = calloc(1, sizeof(struct msr_counter));
4541 if (msrp == NULL) {
4542 perror("calloc");
4543 exit(1);
4544 }
4545
4546 msrp->msr_num = msr_num;
4547 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004548 if (path)
4549 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004550 msrp->width = width;
4551 msrp->type = type;
4552 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004553 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004554
4555 switch (scope) {
4556
4557 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004558 msrp->next = sys.tp;
4559 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004560 sys.added_thread_counters++;
4561 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4562 fprintf(stderr, "exceeded max %d added thread counters\n",
4563 MAX_ADDED_COUNTERS);
4564 exit(-1);
4565 }
Len Brown388e9c82016-12-22 23:57:55 -05004566 break;
4567
4568 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004569 msrp->next = sys.cp;
4570 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004571 sys.added_core_counters++;
4572 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4573 fprintf(stderr, "exceeded max %d added core counters\n",
4574 MAX_ADDED_COUNTERS);
4575 exit(-1);
4576 }
Len Brown388e9c82016-12-22 23:57:55 -05004577 break;
4578
4579 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004580 msrp->next = sys.pp;
4581 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004582 sys.added_package_counters++;
4583 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4584 fprintf(stderr, "exceeded max %d added package counters\n",
4585 MAX_ADDED_COUNTERS);
4586 exit(-1);
4587 }
Len Brown388e9c82016-12-22 23:57:55 -05004588 break;
4589 }
4590
4591 return 0;
4592}
4593
4594void parse_add_command(char *add_command)
4595{
4596 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004597 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004598 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004599 int width = 64;
4600 int fail = 0;
4601 enum counter_scope scope = SCOPE_CPU;
4602 enum counter_type type = COUNTER_CYCLES;
4603 enum counter_format format = FORMAT_DELTA;
4604
4605 while (add_command) {
4606
4607 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4608 goto next;
4609
4610 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4611 goto next;
4612
Len Brown495c76542017-02-08 02:41:51 -05004613 if (*add_command == '/') {
4614 path = add_command;
4615 goto next;
4616 }
4617
Len Brown388e9c82016-12-22 23:57:55 -05004618 if (sscanf(add_command, "u%d", &width) == 1) {
4619 if ((width == 32) || (width == 64))
4620 goto next;
4621 width = 64;
4622 }
4623 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4624 scope = SCOPE_CPU;
4625 goto next;
4626 }
4627 if (!strncmp(add_command, "core", strlen("core"))) {
4628 scope = SCOPE_CORE;
4629 goto next;
4630 }
4631 if (!strncmp(add_command, "package", strlen("package"))) {
4632 scope = SCOPE_PACKAGE;
4633 goto next;
4634 }
4635 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4636 type = COUNTER_CYCLES;
4637 goto next;
4638 }
4639 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4640 type = COUNTER_SECONDS;
4641 goto next;
4642 }
Len Brown41618e62017-02-09 18:25:22 -05004643 if (!strncmp(add_command, "usec", strlen("usec"))) {
4644 type = COUNTER_USEC;
4645 goto next;
4646 }
Len Brown388e9c82016-12-22 23:57:55 -05004647 if (!strncmp(add_command, "raw", strlen("raw"))) {
4648 format = FORMAT_RAW;
4649 goto next;
4650 }
4651 if (!strncmp(add_command, "delta", strlen("delta"))) {
4652 format = FORMAT_DELTA;
4653 goto next;
4654 }
4655 if (!strncmp(add_command, "percent", strlen("percent"))) {
4656 format = FORMAT_PERCENT;
4657 goto next;
4658 }
4659
4660 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4661 char *eos;
4662
4663 eos = strchr(name_buffer, ',');
4664 if (eos)
4665 *eos = '\0';
4666 goto next;
4667 }
4668
4669next:
4670 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004671 if (add_command) {
4672 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004673 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004674 }
Len Brown388e9c82016-12-22 23:57:55 -05004675
4676 }
Len Brown495c76542017-02-08 02:41:51 -05004677 if ((msr_num == 0) && (path == NULL)) {
4678 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004679 fail++;
4680 }
4681
4682 /* generate default column header */
4683 if (*name_buffer == '\0') {
4684 if (format == FORMAT_RAW) {
4685 if (width == 32)
4686 sprintf(name_buffer, "msr%d", msr_num);
4687 else
4688 sprintf(name_buffer, "MSR%d", msr_num);
4689 } else if (format == FORMAT_DELTA) {
4690 if (width == 32)
4691 sprintf(name_buffer, "cnt%d", msr_num);
4692 else
4693 sprintf(name_buffer, "CNT%d", msr_num);
4694 } else if (format == FORMAT_PERCENT) {
4695 if (width == 32)
4696 sprintf(name_buffer, "msr%d%%", msr_num);
4697 else
4698 sprintf(name_buffer, "MSR%d%%", msr_num);
4699 }
4700 }
4701
Len Brown41618e62017-02-09 18:25:22 -05004702 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05004703 fail++;
4704
4705 if (fail) {
4706 help();
4707 exit(1);
4708 }
4709}
Len Brown41618e62017-02-09 18:25:22 -05004710
Len Browndd778a52017-02-21 23:21:13 -05004711int is_deferred_skip(char *name)
4712{
4713 int i;
4714
4715 for (i = 0; i < deferred_skip_index; ++i)
4716 if (!strcmp(name, deferred_skip_names[i]))
4717 return 1;
4718 return 0;
4719}
4720
Len Brown41618e62017-02-09 18:25:22 -05004721void probe_sysfs(void)
4722{
4723 char path[64];
4724 char name_buf[16];
4725 FILE *input;
4726 int state;
4727 char *sp;
4728
4729 if (!DO_BIC(BIC_sysfs))
4730 return;
4731
4732 for (state = 10; state > 0; --state) {
4733
4734 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4735 base_cpu, state);
4736 input = fopen(path, "r");
4737 if (input == NULL)
4738 continue;
4739 fgets(name_buf, sizeof(name_buf), input);
4740
4741 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4742 sp = strchr(name_buf, '-');
4743 if (!sp)
4744 sp = strchrnul(name_buf, '\n');
4745 *sp = '%';
4746 *(sp + 1) = '\0';
4747
4748 fclose(input);
4749
4750 sprintf(path, "cpuidle/state%d/time", state);
4751
Len Browndd778a52017-02-21 23:21:13 -05004752 if (is_deferred_skip(name_buf))
4753 continue;
4754
Len Brown41618e62017-02-09 18:25:22 -05004755 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4756 FORMAT_PERCENT, SYSFS_PERCPU);
4757 }
4758
4759 for (state = 10; state > 0; --state) {
4760
4761 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4762 base_cpu, state);
4763 input = fopen(path, "r");
4764 if (input == NULL)
4765 continue;
4766 fgets(name_buf, sizeof(name_buf), input);
4767 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4768 sp = strchr(name_buf, '-');
4769 if (!sp)
4770 sp = strchrnul(name_buf, '\n');
4771 *sp = '\0';
4772 fclose(input);
4773
4774 sprintf(path, "cpuidle/state%d/usage", state);
4775
Len Browndd778a52017-02-21 23:21:13 -05004776 if (is_deferred_skip(name_buf))
4777 continue;
4778
Len Brown41618e62017-02-09 18:25:22 -05004779 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4780 FORMAT_DELTA, SYSFS_PERCPU);
4781 }
4782
4783}
4784
Len Brown1ef7d212017-02-10 23:54:15 -05004785
4786/*
4787 * parse cpuset with following syntax
4788 * 1,2,4..6,8-10 and set bits in cpu_subset
4789 */
4790void parse_cpu_command(char *optarg)
4791{
4792 unsigned int start, end;
4793 char *next;
4794
Len Brown4e4e1e72017-02-21 22:33:42 -05004795 if (!strcmp(optarg, "core")) {
4796 if (cpu_subset)
4797 goto error;
4798 show_core_only++;
4799 return;
4800 }
4801 if (!strcmp(optarg, "package")) {
4802 if (cpu_subset)
4803 goto error;
4804 show_pkg_only++;
4805 return;
4806 }
4807 if (show_core_only || show_pkg_only)
4808 goto error;
4809
Len Brown1ef7d212017-02-10 23:54:15 -05004810 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
4811 if (cpu_subset == NULL)
4812 err(3, "CPU_ALLOC");
4813 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
4814
4815 CPU_ZERO_S(cpu_subset_size, cpu_subset);
4816
4817 next = optarg;
4818
4819 while (next && *next) {
4820
4821 if (*next == '-') /* no negative cpu numbers */
4822 goto error;
4823
4824 start = strtoul(next, &next, 10);
4825
4826 if (start >= CPU_SUBSET_MAXCPUS)
4827 goto error;
4828 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4829
4830 if (*next == '\0')
4831 break;
4832
4833 if (*next == ',') {
4834 next += 1;
4835 continue;
4836 }
4837
4838 if (*next == '-') {
4839 next += 1; /* start range */
4840 } else if (*next == '.') {
4841 next += 1;
4842 if (*next == '.')
4843 next += 1; /* start range */
4844 else
4845 goto error;
4846 }
4847
4848 end = strtoul(next, &next, 10);
4849 if (end <= start)
4850 goto error;
4851
4852 while (++start <= end) {
4853 if (start >= CPU_SUBSET_MAXCPUS)
4854 goto error;
4855 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4856 }
4857
4858 if (*next == ',')
4859 next += 1;
4860 else if (*next != '\0')
4861 goto error;
4862 }
4863
4864 return;
4865
4866error:
Len Brown4e4e1e72017-02-21 22:33:42 -05004867 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
4868 help();
Len Brown1ef7d212017-02-10 23:54:15 -05004869 exit(-1);
4870}
4871
Len Brown812db3f2017-02-10 00:25:41 -05004872int shown;
4873/*
4874 * parse_show_hide() - process cmdline to set default counter action
4875 */
4876void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4877{
4878 /*
4879 * --show: show only those specified
4880 * The 1st invocation will clear and replace the enabled mask
4881 * subsequent invocations can add to it.
4882 */
4883 if (new_mode == SHOW_LIST) {
4884 if (shown == 0)
Len Browndd778a52017-02-21 23:21:13 -05004885 bic_enabled = bic_lookup(optarg, new_mode);
Len Brown812db3f2017-02-10 00:25:41 -05004886 else
Len Browndd778a52017-02-21 23:21:13 -05004887 bic_enabled |= bic_lookup(optarg, new_mode);
Len Brown812db3f2017-02-10 00:25:41 -05004888 shown = 1;
4889
4890 return;
4891 }
4892
4893 /*
4894 * --hide: do not show those specified
4895 * multiple invocations simply clear more bits in enabled mask
4896 */
Len Browndd778a52017-02-21 23:21:13 -05004897 bic_enabled &= ~bic_lookup(optarg, new_mode);
Len Brown41618e62017-02-09 18:25:22 -05004898
Len Brown812db3f2017-02-10 00:25:41 -05004899}
4900
Len Brown103a8fe2010-10-22 23:53:03 -04004901void cmdline(int argc, char **argv)
4902{
4903 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004904 int option_index = 0;
4905 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004906 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05004907 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05004908 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05004909 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brownd8af6f52015-02-10 01:56:38 -05004910 {"interval", required_argument, 0, 'i'},
4911 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004912 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004913 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05004914 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05004915 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05004916 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05004917 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004918 {"Summary", no_argument, 0, 'S'},
4919 {"TCC", required_argument, 0, 'T'},
4920 {"version", no_argument, 0, 'v' },
4921 {0, 0, 0, 0 }
4922 };
Len Brown103a8fe2010-10-22 23:53:03 -04004923
4924 progname = argv[0];
4925
Len Brown4e4e1e72017-02-21 22:33:42 -05004926 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004927 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004928 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004929 case 'a':
4930 parse_add_command(optarg);
4931 break;
Len Brown1ef7d212017-02-10 23:54:15 -05004932 case 'c':
4933 parse_cpu_command(optarg);
4934 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004935 case 'D':
4936 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004937 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004938 case 'd':
4939 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004940 break;
Len Brown812db3f2017-02-10 00:25:41 -05004941 case 'H':
4942 parse_show_hide(optarg, HIDE_LIST);
4943 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004944 case 'h':
4945 default:
4946 help();
4947 exit(1);
4948 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004949 {
4950 double interval = strtod(optarg, NULL);
4951
4952 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004953 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004954 interval);
4955 exit(2);
4956 }
4957
4958 interval_ts.tv_sec = interval;
4959 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4960 }
Len Brown889facb2012-11-08 00:48:57 -05004961 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004962 case 'J':
4963 rapl_joules++;
4964 break;
Len Brownc8ade362017-02-15 17:15:11 -05004965 case 'l':
4966 list_header_only++;
4967 quiet++;
4968 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004969 case 'o':
4970 outf = fopen_or_die(optarg, "w");
4971 break;
Len Brown96e47152017-01-21 02:26:00 -05004972 case 'q':
4973 quiet = 1;
4974 break;
Len Brown812db3f2017-02-10 00:25:41 -05004975 case 's':
4976 parse_show_hide(optarg, SHOW_LIST);
4977 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004978 case 'S':
4979 summary_only++;
4980 break;
4981 case 'T':
4982 tcc_activation_temp_override = atoi(optarg);
4983 break;
4984 case 'v':
4985 print_version();
4986 exit(0);
4987 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004988 }
4989 }
4990}
4991
4992int main(int argc, char **argv)
4993{
Len Brownb7d8c142016-02-13 23:36:17 -05004994 outf = stderr;
4995
Len Brown103a8fe2010-10-22 23:53:03 -04004996 cmdline(argc, argv);
4997
Len Brown96e47152017-01-21 02:26:00 -05004998 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05004999 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005000
Len Brown41618e62017-02-09 18:25:22 -05005001 probe_sysfs();
5002
Len Brown103a8fe2010-10-22 23:53:03 -04005003 turbostat_init();
5004
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005005 /* dump counters and exit */
5006 if (dump_only)
5007 return get_and_dump_counters();
5008
Len Brownc8ade362017-02-15 17:15:11 -05005009 /* list header and exit */
5010 if (list_header_only) {
5011 print_header(",");
5012 flush_output_stdout();
5013 return 0;
5014 }
5015
Len Brown103a8fe2010-10-22 23:53:03 -04005016 /*
5017 * if any params left, it must be a command to fork
5018 */
5019 if (argc - optind)
5020 return fork_it(argv + optind);
5021 else
5022 turbostat_loop();
5023
5024 return 0;
5025}