blob: 7b02fbb6589316117ba5e9ec2b3b4c602f42c86b [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
32#include <sys/resource.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <sys/time.h>
36#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050037#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038#include <dirent.h>
39#include <string.h>
40#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040041#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050042#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070043#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040044#include <linux/capability.h>
45#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040046
Len Brown103a8fe2010-10-22 23:53:03 -040047char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050048FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050049int *fd_percpu;
Len Brown2a0609c2016-02-12 22:44:48 -050050struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050051unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050052unsigned int quiet;
Len Brownd8af6f52015-02-10 01:56:38 -050053unsigned int rapl_joules;
54unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050055unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050056unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040057unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070058unsigned int do_knl_cstates;
Len Brown0b2bb692015-03-26 00:50:30 -040059unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050060unsigned int do_slm_cstates;
61unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040062unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050063unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040064unsigned int do_irtl_snb;
65unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050066unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040067unsigned int genuine_intel;
68unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050069unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050070unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020071unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040072double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040073double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040074unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040075double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040076unsigned int show_pkg_only;
77unsigned int show_core_only;
78char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050079unsigned int do_rapl;
80unsigned int do_dts;
81unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050082unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050083unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050084unsigned int tcc_activation_temp;
85unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050086double rapl_power_units, rapl_time_units;
87double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050088double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040089unsigned int do_core_perf_limit_reasons;
90unsigned int do_gfx_perf_limit_reasons;
91unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040092unsigned int crystal_hz;
93unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040094int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040095double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050096unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
97 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
98unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
99unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
100unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
101unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500102unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500103
Len Browne6f9bb32013-12-03 02:19:19 -0500104#define RAPL_PKG (1 << 0)
105 /* 0x610 MSR_PKG_POWER_LIMIT */
106 /* 0x611 MSR_PKG_ENERGY_STATUS */
107#define RAPL_PKG_PERF_STATUS (1 << 1)
108 /* 0x613 MSR_PKG_PERF_STATUS */
109#define RAPL_PKG_POWER_INFO (1 << 2)
110 /* 0x614 MSR_PKG_POWER_INFO */
111
112#define RAPL_DRAM (1 << 3)
113 /* 0x618 MSR_DRAM_POWER_LIMIT */
114 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500115#define RAPL_DRAM_PERF_STATUS (1 << 4)
116 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400117#define RAPL_DRAM_POWER_INFO (1 << 5)
118 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500119
Jacob Pan91484942016-06-16 09:48:20 -0700120#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500121 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400122#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500123 /* 0x63a MSR_PP0_POLICY */
124
Len Brown0b2bb692015-03-26 00:50:30 -0400125#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500126 /* 0x640 MSR_PP1_POWER_LIMIT */
127 /* 0x641 MSR_PP1_ENERGY_STATUS */
128 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700129
130#define RAPL_CORES_ENERGY_STATUS (1 << 9)
131 /* 0x639 MSR_PP0_ENERGY_STATUS */
132#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500133#define TJMAX_DEFAULT 100
134
135#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400136
Len Brown388e9c82016-12-22 23:57:55 -0500137/*
138 * buffer size used by sscanf() for added column names
139 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
140 */
141#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500142#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500143
Len Brown103a8fe2010-10-22 23:53:03 -0400144int backwards_count;
145char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400146
Len Brown1ef7d212017-02-10 23:54:15 -0500147#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
148cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
149size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown678a3bd2017-02-09 22:22:13 -0500150#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400151
Len Brownc98d5d92012-06-04 00:56:40 -0400152struct thread_data {
153 unsigned long long tsc;
154 unsigned long long aperf;
155 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500156 unsigned long long c1;
Len Brown562a2d32016-02-26 23:48:05 -0500157 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500158 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400159 unsigned int cpu_id;
160 unsigned int flags;
161#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
162#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500163 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400164} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400165
Len Brownc98d5d92012-06-04 00:56:40 -0400166struct core_data {
167 unsigned long long c3;
168 unsigned long long c6;
169 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500170 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500171 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400172 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500173 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400174} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400175
Len Brownc98d5d92012-06-04 00:56:40 -0400176struct pkg_data {
177 unsigned long long pc2;
178 unsigned long long pc3;
179 unsigned long long pc6;
180 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800181 unsigned long long pc8;
182 unsigned long long pc9;
183 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400184 unsigned long long pkg_wtd_core_c0;
185 unsigned long long pkg_any_core_c0;
186 unsigned long long pkg_any_gfxe_c0;
187 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400188 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500189 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400190 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500191 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
192 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
193 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
194 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
195 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
196 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
197 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500198 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400199} *package_even, *package_odd;
200
201#define ODD_COUNTERS thread_odd, core_odd, package_odd
202#define EVEN_COUNTERS thread_even, core_even, package_even
203
204#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
205 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
206 topo.num_threads_per_core + \
207 (core_no) * topo.num_threads_per_core + (thread_no))
208#define GET_CORE(core_base, core_no, pkg_no) \
209 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
210#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
211
Len Brown388e9c82016-12-22 23:57:55 -0500212enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500213enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500214enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
215
216struct msr_counter {
217 unsigned int msr_num;
218 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500219 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500220 unsigned int width;
221 enum counter_type type;
222 enum counter_format format;
223 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500224 unsigned int flags;
225#define FLAGS_HIDE (1 << 0)
226#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500227#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500228};
229
230struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500231 unsigned int added_thread_counters;
232 unsigned int added_core_counters;
233 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500234 struct msr_counter *tp;
235 struct msr_counter *cp;
236 struct msr_counter *pp;
237} sys;
238
Len Brownc98d5d92012-06-04 00:56:40 -0400239struct system_summary {
240 struct thread_data threads;
241 struct core_data cores;
242 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500243} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400244
245
246struct topo_params {
247 int num_packages;
248 int num_cpus;
249 int num_cores;
250 int max_cpu_num;
251 int num_cores_per_pkg;
252 int num_threads_per_core;
253} topo;
254
255struct timeval tv_even, tv_odd, tv_delta;
256
Len Brown562a2d32016-02-26 23:48:05 -0500257int *irq_column_2_cpu; /* /proc/interrupts column numbers */
258int *irqs_per_cpu; /* indexed by cpu_num */
259
Len Brownc98d5d92012-06-04 00:56:40 -0400260void setup_all_buffers(void);
261
262int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400263{
Len Brownc98d5d92012-06-04 00:56:40 -0400264 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400265}
Len Brown88c32812012-03-29 21:44:40 -0400266/*
Len Brownc98d5d92012-06-04 00:56:40 -0400267 * run func(thread, core, package) in topology order
268 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400269 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400270
Len Brownc98d5d92012-06-04 00:56:40 -0400271int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
272 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400273{
Len Brownc98d5d92012-06-04 00:56:40 -0400274 int retval, pkg_no, core_no, thread_no;
275
276 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
277 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
278 for (thread_no = 0; thread_no <
279 topo.num_threads_per_core; ++thread_no) {
280 struct thread_data *t;
281 struct core_data *c;
282 struct pkg_data *p;
283
284 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
285
286 if (cpu_is_not_present(t->cpu_id))
287 continue;
288
289 c = GET_CORE(core_base, core_no, pkg_no);
290 p = GET_PKG(pkg_base, pkg_no);
291
292 retval = func(t, c, p);
293 if (retval)
294 return retval;
295 }
296 }
297 }
298 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400299}
300
301int cpu_migrate(int cpu)
302{
Len Brownc98d5d92012-06-04 00:56:40 -0400303 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
304 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
305 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400306 return -1;
307 else
308 return 0;
309}
Len Brown36229892016-02-26 20:51:02 -0500310int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400311{
Len Brown103a8fe2010-10-22 23:53:03 -0400312 char pathname[32];
313 int fd;
314
Len Brown36229892016-02-26 20:51:02 -0500315 fd = fd_percpu[cpu];
316
317 if (fd)
318 return fd;
319
Len Brown103a8fe2010-10-22 23:53:03 -0400320 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
321 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400322 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400323 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 -0400324
Len Brown36229892016-02-26 20:51:02 -0500325 fd_percpu[cpu] = fd;
326
327 return fd;
328}
329
330int get_msr(int cpu, off_t offset, unsigned long long *msr)
331{
332 ssize_t retval;
333
334 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400335
Len Brown98481e72014-08-15 00:36:50 -0400336 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500337 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400338
339 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400340}
341
Len Brownfc04cc62014-02-06 00:55:19 -0500342/*
Len Brown812db3f2017-02-10 00:25:41 -0500343 * Each string in this array is compared in --show and --hide cmdline.
344 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500345 */
Len Brown812db3f2017-02-10 00:25:41 -0500346struct msr_counter bic[] = {
347 { 0x0, "Package" },
348 { 0x0, "Avg_MHz" },
349 { 0x0, "Bzy_MHz" },
350 { 0x0, "TSC_MHz" },
351 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500352 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500353 { 0x0, "Busy%" },
354 { 0x0, "CPU%c1" },
355 { 0x0, "CPU%c3" },
356 { 0x0, "CPU%c6" },
357 { 0x0, "CPU%c7" },
358 { 0x0, "ThreadC" },
359 { 0x0, "CoreTmp" },
360 { 0x0, "CoreCnt" },
361 { 0x0, "PkgTmp" },
362 { 0x0, "GFX%rc6" },
363 { 0x0, "GFXMHz" },
364 { 0x0, "Pkg%pc2" },
365 { 0x0, "Pkg%pc3" },
366 { 0x0, "Pkg%pc6" },
367 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500368 { 0x0, "Pkg%pc8" },
369 { 0x0, "Pkg%pc9" },
370 { 0x0, "Pkg%pc10" },
Len Brown812db3f2017-02-10 00:25:41 -0500371 { 0x0, "PkgWatt" },
372 { 0x0, "CorWatt" },
373 { 0x0, "GFXWatt" },
374 { 0x0, "PkgCnt" },
375 { 0x0, "RAMWatt" },
376 { 0x0, "PKG_%" },
377 { 0x0, "RAM_%" },
378 { 0x0, "Pkg_J" },
379 { 0x0, "Cor_J" },
380 { 0x0, "GFX_J" },
381 { 0x0, "RAM_J" },
382 { 0x0, "Core" },
383 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500384 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500385 { 0x0, "sysfs" },
Len Brown812db3f2017-02-10 00:25:41 -0500386};
387
388#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
389#define BIC_Package (1ULL << 0)
390#define BIC_Avg_MHz (1ULL << 1)
391#define BIC_Bzy_MHz (1ULL << 2)
392#define BIC_TSC_MHz (1ULL << 3)
393#define BIC_IRQ (1ULL << 4)
394#define BIC_SMI (1ULL << 5)
395#define BIC_Busy (1ULL << 6)
396#define BIC_CPU_c1 (1ULL << 7)
397#define BIC_CPU_c3 (1ULL << 8)
398#define BIC_CPU_c6 (1ULL << 9)
399#define BIC_CPU_c7 (1ULL << 10)
400#define BIC_ThreadC (1ULL << 11)
401#define BIC_CoreTmp (1ULL << 12)
402#define BIC_CoreCnt (1ULL << 13)
403#define BIC_PkgTmp (1ULL << 14)
404#define BIC_GFX_rc6 (1ULL << 15)
405#define BIC_GFXMHz (1ULL << 16)
406#define BIC_Pkgpc2 (1ULL << 17)
407#define BIC_Pkgpc3 (1ULL << 18)
408#define BIC_Pkgpc6 (1ULL << 19)
409#define BIC_Pkgpc7 (1ULL << 20)
Len Brown0f47c082017-01-27 00:50:45 -0500410#define BIC_Pkgpc8 (1ULL << 21)
411#define BIC_Pkgpc9 (1ULL << 22)
412#define BIC_Pkgpc10 (1ULL << 23)
413#define BIC_PkgWatt (1ULL << 24)
414#define BIC_CorWatt (1ULL << 25)
415#define BIC_GFXWatt (1ULL << 26)
416#define BIC_PkgCnt (1ULL << 27)
417#define BIC_RAMWatt (1ULL << 28)
418#define BIC_PKG__ (1ULL << 29)
419#define BIC_RAM__ (1ULL << 30)
420#define BIC_Pkg_J (1ULL << 31)
421#define BIC_Cor_J (1ULL << 32)
422#define BIC_GFX_J (1ULL << 33)
423#define BIC_RAM_J (1ULL << 34)
424#define BIC_Core (1ULL << 35)
425#define BIC_CPU (1ULL << 36)
426#define BIC_Mod_c6 (1ULL << 37)
Len Brown41618e62017-02-09 18:25:22 -0500427#define BIC_sysfs (1ULL << 38)
Len Brown812db3f2017-02-10 00:25:41 -0500428
429unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
Len Brown41618e62017-02-09 18:25:22 -0500430unsigned long long bic_present = BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500431
432#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
433#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500434#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500435
436/*
437 * bic_lookup
438 * for all the strings in comma separate name_list,
439 * set the approprate bit in return value.
440 */
441unsigned long long bic_lookup(char *name_list)
442{
443 int i;
444 unsigned long long retval = 0;
445
446 while (name_list) {
447 char *comma;
448
449 comma = strchr(name_list, ',');
450
451 if (comma)
452 *comma = '\0';
453
454 for (i = 0; i < MAX_BIC; ++i) {
455 if (!strcmp(name_list, bic[i].name)) {
456 retval |= (1ULL << i);
457 break;
458 }
459 }
460 if (i == MAX_BIC) {
461 fprintf(stderr, "Invalid counter name: %s\n", name_list);
462 exit(-1);
463 }
464
465 name_list = comma;
466 if (name_list)
467 name_list++;
468
469 }
470 return retval;
471}
Len Brownfc04cc62014-02-06 00:55:19 -0500472
Len Brownc8ade362017-02-15 17:15:11 -0500473void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400474{
Len Brown388e9c82016-12-22 23:57:55 -0500475 struct msr_counter *mp;
476
Len Brown812db3f2017-02-10 00:25:41 -0500477 if (DO_BIC(BIC_Package))
Len Brownc8ade362017-02-15 17:15:11 -0500478 outp += sprintf(outp, "%sPackage", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500479 if (DO_BIC(BIC_Core))
Len Brownc8ade362017-02-15 17:15:11 -0500480 outp += sprintf(outp, "%sCore", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500481 if (DO_BIC(BIC_CPU))
Len Brownc8ade362017-02-15 17:15:11 -0500482 outp += sprintf(outp, "%sCPU", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500483 if (DO_BIC(BIC_Avg_MHz))
Len Brownc8ade362017-02-15 17:15:11 -0500484 outp += sprintf(outp, "%sAvg_MHz", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500485 if (DO_BIC(BIC_Busy))
Len Brownc8ade362017-02-15 17:15:11 -0500486 outp += sprintf(outp, "%sBusy%%", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500487 if (DO_BIC(BIC_Bzy_MHz))
Len Brownc8ade362017-02-15 17:15:11 -0500488 outp += sprintf(outp, "%sBzy_MHz", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500489 if (DO_BIC(BIC_TSC_MHz))
Len Brownc8ade362017-02-15 17:15:11 -0500490 outp += sprintf(outp, "%sTSC_MHz", delim);
Len Brown1cc21f72015-02-23 00:34:57 -0500491
Len Brown812db3f2017-02-10 00:25:41 -0500492 if (DO_BIC(BIC_IRQ))
Len Brownc8ade362017-02-15 17:15:11 -0500493 outp += sprintf(outp, "%sIRQ", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500494 if (DO_BIC(BIC_SMI))
Len Brownc8ade362017-02-15 17:15:11 -0500495 outp += sprintf(outp, "%sSMI", delim);
Len Brown1cc21f72015-02-23 00:34:57 -0500496
Len Brown388e9c82016-12-22 23:57:55 -0500497 for (mp = sys.tp; mp; mp = mp->next) {
498 if (mp->format == FORMAT_RAW) {
499 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500500 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500501 else
Len Brownc8ade362017-02-15 17:15:11 -0500502 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500503 } else {
Len Brownc8ade362017-02-15 17:15:11 -0500504 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500505 }
506 }
507
Len Brown41618e62017-02-09 18:25:22 -0500508 if (DO_BIC(BIC_CPU_c1))
Len Brownc8ade362017-02-15 17:15:11 -0500509 outp += sprintf(outp, "%sCPU%%c1", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500510 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brownc8ade362017-02-15 17:15:11 -0500511 outp += sprintf(outp, "%sCPU%%c3", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500512 if (DO_BIC(BIC_CPU_c6))
Len Brownc8ade362017-02-15 17:15:11 -0500513 outp += sprintf(outp, "%sCPU%%c6", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500514 if (DO_BIC(BIC_CPU_c7))
Len Brownc8ade362017-02-15 17:15:11 -0500515 outp += sprintf(outp, "%sCPU%%c7", delim);
Len Brown678a3bd2017-02-09 22:22:13 -0500516
Len Brown0539ba12017-02-10 00:27:20 -0500517 if (DO_BIC(BIC_Mod_c6))
Len Brownc8ade362017-02-15 17:15:11 -0500518 outp += sprintf(outp, "%sMod%%c6", delim);
Len Brown678a3bd2017-02-09 22:22:13 -0500519
Len Brown812db3f2017-02-10 00:25:41 -0500520 if (DO_BIC(BIC_CoreTmp))
Len Brownc8ade362017-02-15 17:15:11 -0500521 outp += sprintf(outp, "%sCoreTmp", delim);
Len Brown388e9c82016-12-22 23:57:55 -0500522
523 for (mp = sys.cp; mp; mp = mp->next) {
524 if (mp->format == FORMAT_RAW) {
525 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500526 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500527 else
Len Brownc8ade362017-02-15 17:15:11 -0500528 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500529 } else {
Len Brownc8ade362017-02-15 17:15:11 -0500530 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500531 }
532 }
533
Len Brown812db3f2017-02-10 00:25:41 -0500534 if (DO_BIC(BIC_PkgTmp))
Len Brownc8ade362017-02-15 17:15:11 -0500535 outp += sprintf(outp, "%sPkgTmp", delim);
Len Brown889facb2012-11-08 00:48:57 -0500536
Len Brown812db3f2017-02-10 00:25:41 -0500537 if (DO_BIC(BIC_GFX_rc6))
Len Brownc8ade362017-02-15 17:15:11 -0500538 outp += sprintf(outp, "%sGFX%%rc6", delim);
Len Brownfdf676e2016-02-27 01:28:12 -0500539
Len Brown812db3f2017-02-10 00:25:41 -0500540 if (DO_BIC(BIC_GFXMHz))
Len Brownc8ade362017-02-15 17:15:11 -0500541 outp += sprintf(outp, "%sGFXMHz", delim);
Len Brown27d47352016-02-27 00:37:54 -0500542
Len Brown0b2bb692015-03-26 00:50:30 -0400543 if (do_skl_residency) {
Len Brownc8ade362017-02-15 17:15:11 -0500544 outp += sprintf(outp, "%sTotl%%C0", delim);
545 outp += sprintf(outp, "%sAny%%C0", delim);
546 outp += sprintf(outp, "%sGFX%%C0", delim);
547 outp += sprintf(outp, "%sCPUGFX%%", delim);
Len Brown0b2bb692015-03-26 00:50:30 -0400548 }
549
Len Brown0f47c082017-01-27 00:50:45 -0500550 if (DO_BIC(BIC_Pkgpc2))
Len Brownc8ade362017-02-15 17:15:11 -0500551 outp += sprintf(outp, "%sPkg%%pc2", delim);
Len Brown0f47c082017-01-27 00:50:45 -0500552 if (DO_BIC(BIC_Pkgpc3))
Len Brownc8ade362017-02-15 17:15:11 -0500553 outp += sprintf(outp, "%sPkg%%pc3", delim);
Len Brown0f47c082017-01-27 00:50:45 -0500554 if (DO_BIC(BIC_Pkgpc6))
Len Brownc8ade362017-02-15 17:15:11 -0500555 outp += sprintf(outp, "%sPkg%%pc6", delim);
Len Brown0f47c082017-01-27 00:50:45 -0500556 if (DO_BIC(BIC_Pkgpc7))
Len Brownc8ade362017-02-15 17:15:11 -0500557 outp += sprintf(outp, "%sPkg%%pc7", delim);
Len Brown0f47c082017-01-27 00:50:45 -0500558 if (DO_BIC(BIC_Pkgpc8))
Len Brownc8ade362017-02-15 17:15:11 -0500559 outp += sprintf(outp, "%sPkg%%pc8", delim);
Len Brown0f47c082017-01-27 00:50:45 -0500560 if (DO_BIC(BIC_Pkgpc9))
Len Brownc8ade362017-02-15 17:15:11 -0500561 outp += sprintf(outp, "%sPkg%%pc9", delim);
Len Brown0f47c082017-01-27 00:50:45 -0500562 if (DO_BIC(BIC_Pkgpc10))
Len Brownc8ade362017-02-15 17:15:11 -0500563 outp += sprintf(outp, "%sPk%%pc10", delim);
Len Brown103a8fe2010-10-22 23:53:03 -0400564
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800565 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500566 if (DO_BIC(BIC_PkgWatt))
Len Brownc8ade362017-02-15 17:15:11 -0500567 outp += sprintf(outp, "%sPkgWatt", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500568 if (DO_BIC(BIC_CorWatt))
Len Brownc8ade362017-02-15 17:15:11 -0500569 outp += sprintf(outp, "%sCorWatt", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500570 if (DO_BIC(BIC_GFXWatt))
Len Brownc8ade362017-02-15 17:15:11 -0500571 outp += sprintf(outp, "%sGFXWatt", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500572 if (DO_BIC(BIC_RAMWatt))
Len Brownc8ade362017-02-15 17:15:11 -0500573 outp += sprintf(outp, "%sRAMWatt", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500574 if (DO_BIC(BIC_PKG__))
Len Brownc8ade362017-02-15 17:15:11 -0500575 outp += sprintf(outp, "%sPKG_%%", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500576 if (DO_BIC(BIC_RAM__))
Len Brownc8ade362017-02-15 17:15:11 -0500577 outp += sprintf(outp, "%sRAM_%%", delim);
Len Brownd7899442015-01-23 00:12:33 -0500578 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500579 if (DO_BIC(BIC_Pkg_J))
Len Brownc8ade362017-02-15 17:15:11 -0500580 outp += sprintf(outp, "%sPkg_J", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500581 if (DO_BIC(BIC_Cor_J))
Len Brownc8ade362017-02-15 17:15:11 -0500582 outp += sprintf(outp, "%sCor_J", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500583 if (DO_BIC(BIC_GFX_J))
Len Brownc8ade362017-02-15 17:15:11 -0500584 outp += sprintf(outp, "%sGFX_J", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500585 if (DO_BIC(BIC_RAM_J))
Len Brownc8ade362017-02-15 17:15:11 -0500586 outp += sprintf(outp, "%sRAM_J", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500587 if (DO_BIC(BIC_PKG__))
Len Brownc8ade362017-02-15 17:15:11 -0500588 outp += sprintf(outp, "%sPKG_%%", delim);
Len Brown812db3f2017-02-10 00:25:41 -0500589 if (DO_BIC(BIC_RAM__))
Len Brownc8ade362017-02-15 17:15:11 -0500590 outp += sprintf(outp, "%sRAM_%%", delim);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800591 }
Len Brown388e9c82016-12-22 23:57:55 -0500592 for (mp = sys.pp; mp; mp = mp->next) {
593 if (mp->format == FORMAT_RAW) {
594 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500595 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500596 else
Len Brownc8ade362017-02-15 17:15:11 -0500597 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500598 } else {
Len Brownc8ade362017-02-15 17:15:11 -0500599 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500600 }
601 }
602
Len Brownc98d5d92012-06-04 00:56:40 -0400603 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400604}
605
Len Brownc98d5d92012-06-04 00:56:40 -0400606int dump_counters(struct thread_data *t, struct core_data *c,
607 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400608{
Len Brown388e9c82016-12-22 23:57:55 -0500609 int i;
610 struct msr_counter *mp;
611
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200612 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400613
Len Brownc98d5d92012-06-04 00:56:40 -0400614 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200615 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
616 t->cpu_id, t->flags);
617 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
618 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
619 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
620 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500621
Len Brown812db3f2017-02-10 00:25:41 -0500622 if (DO_BIC(BIC_IRQ))
Len Brown218f0e82017-02-14 22:07:52 -0500623 outp += sprintf(outp, "IRQ: %d\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500624 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500625 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500626
627 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
628 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
629 i, mp->msr_num, t->counter[i]);
630 }
Len Brownc98d5d92012-06-04 00:56:40 -0400631 }
Len Brown103a8fe2010-10-22 23:53:03 -0400632
Len Brownc98d5d92012-06-04 00:56:40 -0400633 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200634 outp += sprintf(outp, "core: %d\n", c->core_id);
635 outp += sprintf(outp, "c3: %016llX\n", c->c3);
636 outp += sprintf(outp, "c6: %016llX\n", c->c6);
637 outp += sprintf(outp, "c7: %016llX\n", c->c7);
638 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500639
640 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
641 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
642 i, mp->msr_num, c->counter[i]);
643 }
Len Brown0539ba12017-02-10 00:27:20 -0500644 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400645 }
646
647 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200648 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400649
650 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
651 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
652 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
653 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
654
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200655 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500656 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500657 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500658 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500659 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500660 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500661 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200662 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
663 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
664 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
665 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
666 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
667 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
668 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
669 outp += sprintf(outp, "Throttle PKG: %0X\n",
670 p->rapl_pkg_perf_status);
671 outp += sprintf(outp, "Throttle RAM: %0X\n",
672 p->rapl_dram_perf_status);
673 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500674
675 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
676 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
677 i, mp->msr_num, p->counter[i]);
678 }
Len Brownc98d5d92012-06-04 00:56:40 -0400679 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200680
681 outp += sprintf(outp, "\n");
682
Len Brownc98d5d92012-06-04 00:56:40 -0400683 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400684}
685
Len Browne23da032012-02-06 18:37:16 -0500686/*
687 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500688 */
Len Brownc98d5d92012-06-04 00:56:40 -0400689int format_counters(struct thread_data *t, struct core_data *c,
690 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400691{
Len Brown008d396e2017-02-10 00:29:51 -0500692 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500693 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500694 int i;
695 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400696
Len Brownc98d5d92012-06-04 00:56:40 -0400697 /* if showing only 1st thread in core and this isn't one, bail out */
698 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
699 return 0;
700
701 /* if showing only 1st thread in pkg and this isn't one, bail out */
702 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
703 return 0;
704
Len Brown1ef7d212017-02-10 23:54:15 -0500705 /*if not summary line and --cpu is used */
706 if ((t != &average.threads) &&
707 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
708 return 0;
709
Len Brown103a8fe2010-10-22 23:53:03 -0400710 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
711
Len Brown008d396e2017-02-10 00:29:51 -0500712 tsc = t->tsc * tsc_tweak;
713
Len Brownc98d5d92012-06-04 00:56:40 -0400714 /* topo columns, print blanks on 1st (average) line */
715 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500716 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400717 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500718 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400719 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500720 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400721 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400722 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500723 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400724 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400725 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400726 else
Len Brown3d109de2016-04-22 23:24:27 -0400727 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400728 }
Len Brown812db3f2017-02-10 00:25:41 -0500729 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400730 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400731 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400732 else
Len Brown3d109de2016-04-22 23:24:27 -0400733 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400734 }
Len Brown812db3f2017-02-10 00:25:41 -0500735 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400736 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400737 }
Len Brownfc04cc62014-02-06 00:55:19 -0500738
Len Brown812db3f2017-02-10 00:25:41 -0500739 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400740 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500741 1.0 / units * t->aperf / interval_float);
742
Len Brown812db3f2017-02-10 00:25:41 -0500743 if (DO_BIC(BIC_Busy))
Len Brown008d396e2017-02-10 00:29:51 -0500744 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400745
Len Brown812db3f2017-02-10 00:25:41 -0500746 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400747 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400748 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400749 else
Len Brown3d109de2016-04-22 23:24:27 -0400750 outp += sprintf(outp, "\t%.0f",
Len Brown008d396e2017-02-10 00:29:51 -0500751 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400752 }
Len Brown103a8fe2010-10-22 23:53:03 -0400753
Len Brown812db3f2017-02-10 00:25:41 -0500754 if (DO_BIC(BIC_TSC_MHz))
755 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400756
Len Brown562a2d32016-02-26 23:48:05 -0500757 /* IRQ */
Len Brown812db3f2017-02-10 00:25:41 -0500758 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400759 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500760
Len Brown1cc21f72015-02-23 00:34:57 -0500761 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500762 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400763 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500764
Len Brown678a3bd2017-02-09 22:22:13 -0500765 /* Added counters */
766 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
767 if (mp->format == FORMAT_RAW) {
768 if (mp->width == 32)
769 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
770 else
771 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
772 } else if (mp->format == FORMAT_DELTA) {
773 outp += sprintf(outp, "\t%lld", t->counter[i]);
774 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500775 if (mp->type == COUNTER_USEC)
776 outp += sprintf(outp, "\t%.2f", t->counter[i]/interval_float/10000);
777 else
778 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500779 }
780 }
781
Len Brown41618e62017-02-09 18:25:22 -0500782 /* C1 */
783 if (DO_BIC(BIC_CPU_c1))
784 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/tsc);
785
786
Len Brownc98d5d92012-06-04 00:56:40 -0400787 /* print per-core data only for 1st thread in core */
788 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
789 goto done;
790
Len Brown812db3f2017-02-10 00:25:41 -0500791 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown008d396e2017-02-10 00:29:51 -0500792 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500793 if (DO_BIC(BIC_CPU_c6))
Len Brown008d396e2017-02-10 00:29:51 -0500794 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500795 if (DO_BIC(BIC_CPU_c7))
Len Brown008d396e2017-02-10 00:29:51 -0500796 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400797
Len Brown0539ba12017-02-10 00:27:20 -0500798 /* Mod%c6 */
799 if (DO_BIC(BIC_Mod_c6))
Len Brown008d396e2017-02-10 00:29:51 -0500800 outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500801
Len Brown812db3f2017-02-10 00:25:41 -0500802 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400803 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500804
Len Brown388e9c82016-12-22 23:57:55 -0500805 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
806 if (mp->format == FORMAT_RAW) {
807 if (mp->width == 32)
808 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
809 else
810 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
811 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500812 outp += sprintf(outp, "\t%lld", c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500813 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500814 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500815 }
816 }
817
Len Brownc98d5d92012-06-04 00:56:40 -0400818 /* print per-package data only for 1st core in package */
819 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
820 goto done;
821
Len Brown0b2bb692015-03-26 00:50:30 -0400822 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500823 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400824 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500825
Len Brownfdf676e2016-02-27 01:28:12 -0500826 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500827 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400828 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400829 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400830 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400831 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400832 p->gfx_rc6_ms / 10.0 / interval_float);
833 }
834 }
Len Brownfdf676e2016-02-27 01:28:12 -0500835
Len Brown27d47352016-02-27 00:37:54 -0500836 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500837 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400838 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500839
Len Brown0b2bb692015-03-26 00:50:30 -0400840 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
841 if (do_skl_residency) {
Len Brown008d396e2017-02-10 00:29:51 -0500842 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/tsc);
843 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/tsc);
844 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/tsc);
845 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400846 }
847
Len Brown0f47c082017-01-27 00:50:45 -0500848 if (DO_BIC(BIC_Pkgpc2))
Len Brown008d396e2017-02-10 00:29:51 -0500849 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500850 if (DO_BIC(BIC_Pkgpc3))
Len Brown008d396e2017-02-10 00:29:51 -0500851 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500852 if (DO_BIC(BIC_Pkgpc6))
Len Brown008d396e2017-02-10 00:29:51 -0500853 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500854 if (DO_BIC(BIC_Pkgpc7))
Len Brown008d396e2017-02-10 00:29:51 -0500855 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500856 if (DO_BIC(BIC_Pkgpc8))
Len Brown008d396e2017-02-10 00:29:51 -0500857 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500858 if (DO_BIC(BIC_Pkgpc9))
Len Brown008d396e2017-02-10 00:29:51 -0500859 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500860 if (DO_BIC(BIC_Pkgpc10))
Len Brown008d396e2017-02-10 00:29:51 -0500861 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500862
863 /*
864 * If measurement interval exceeds minimum RAPL Joule Counter range,
865 * indicate that results are suspect by printing "**" in fraction place.
866 */
Len Brownfc04cc62014-02-06 00:55:19 -0500867 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400868 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500869 else
Len Browne975db52016-04-06 23:56:02 -0400870 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500871
Len Brown812db3f2017-02-10 00:25:41 -0500872 if (DO_BIC(BIC_PkgWatt))
873 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
874 if (DO_BIC(BIC_CorWatt))
875 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
876 if (DO_BIC(BIC_GFXWatt))
877 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
878 if (DO_BIC(BIC_RAMWatt))
879 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
880 if (DO_BIC(BIC_Pkg_J))
881 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
882 if (DO_BIC(BIC_Cor_J))
883 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
884 if (DO_BIC(BIC_GFX_J))
885 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
886 if (DO_BIC(BIC_RAM_J))
887 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
888 if (DO_BIC(BIC_PKG__))
889 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
890 if (DO_BIC(BIC_RAM__))
891 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
892
Len Brown388e9c82016-12-22 23:57:55 -0500893 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
894 if (mp->format == FORMAT_RAW) {
895 if (mp->width == 32)
896 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
897 else
898 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
899 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500900 outp += sprintf(outp, "\t%lld", p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500901 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500902 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500903 }
904 }
905
Len Brownc98d5d92012-06-04 00:56:40 -0400906done:
Len Brownc98d5d92012-06-04 00:56:40 -0400907 outp += sprintf(outp, "\n");
908
909 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400910}
911
Len Brownb7d8c142016-02-13 23:36:17 -0500912void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400913{
Len Brownb7d8c142016-02-13 23:36:17 -0500914 FILE *filep;
915
916 if (outf == stderr)
917 filep = stdout;
918 else
919 filep = outf;
920
921 fputs(output_buffer, filep);
922 fflush(filep);
923
Len Brownc98d5d92012-06-04 00:56:40 -0400924 outp = output_buffer;
925}
Len Brownb7d8c142016-02-13 23:36:17 -0500926void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400927{
Len Brownb7d8c142016-02-13 23:36:17 -0500928 fputs(output_buffer, outf);
929 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400930 outp = output_buffer;
931}
932void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
933{
Len Browne23da032012-02-06 18:37:16 -0500934 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400935
Len Browne23da032012-02-06 18:37:16 -0500936 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -0500937 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -0400938
Len Brownc98d5d92012-06-04 00:56:40 -0400939 if (topo.num_cpus > 1)
940 format_counters(&average.threads, &average.cores,
941 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400942
Len Browne23da032012-02-06 18:37:16 -0500943 printed = 1;
944
945 if (summary_only)
946 return;
947
Len Brownc98d5d92012-06-04 00:56:40 -0400948 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400949}
950
Len Brown889facb2012-11-08 00:48:57 -0500951#define DELTA_WRAP32(new, old) \
952 if (new > old) { \
953 old = new - old; \
954 } else { \
955 old = 0x100000000 + new - old; \
956 }
957
Len Brownba3dec92016-04-22 20:31:46 -0400958int
Len Brownc98d5d92012-06-04 00:56:40 -0400959delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400960{
Len Brown388e9c82016-12-22 23:57:55 -0500961 int i;
962 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400963
964 if (do_skl_residency) {
965 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
966 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
967 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
968 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
969 }
Len Brownc98d5d92012-06-04 00:56:40 -0400970 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -0500971 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500972 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -0500973 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500974 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -0500975 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500976 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800977 old->pc8 = new->pc8 - old->pc8;
978 old->pc9 = new->pc9 - old->pc9;
979 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500980 old->pkg_temp_c = new->pkg_temp_c;
981
Len Brown9185e982016-04-06 17:16:00 -0400982 /* flag an error when rc6 counter resets/wraps */
983 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
984 old->gfx_rc6_ms = -1;
985 else
986 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
987
Len Brown27d47352016-02-27 00:37:54 -0500988 old->gfx_mhz = new->gfx_mhz;
989
Len Brown889facb2012-11-08 00:48:57 -0500990 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
991 DELTA_WRAP32(new->energy_cores, old->energy_cores);
992 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
993 DELTA_WRAP32(new->energy_dram, old->energy_dram);
994 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
995 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400996
Len Brown388e9c82016-12-22 23:57:55 -0500997 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
998 if (mp->format == FORMAT_RAW)
999 old->counter[i] = new->counter[i];
1000 else
1001 old->counter[i] = new->counter[i] - old->counter[i];
1002 }
1003
Len Brownba3dec92016-04-22 20:31:46 -04001004 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001005}
Len Brown103a8fe2010-10-22 23:53:03 -04001006
Len Brownc98d5d92012-06-04 00:56:40 -04001007void
1008delta_core(struct core_data *new, struct core_data *old)
1009{
Len Brown388e9c82016-12-22 23:57:55 -05001010 int i;
1011 struct msr_counter *mp;
1012
Len Brownc98d5d92012-06-04 00:56:40 -04001013 old->c3 = new->c3 - old->c3;
1014 old->c6 = new->c6 - old->c6;
1015 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001016 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001017 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001018
1019 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1020 if (mp->format == FORMAT_RAW)
1021 old->counter[i] = new->counter[i];
1022 else
1023 old->counter[i] = new->counter[i] - old->counter[i];
1024 }
Len Brownc98d5d92012-06-04 00:56:40 -04001025}
Len Brown103a8fe2010-10-22 23:53:03 -04001026
Len Brownc3ae3312012-06-13 21:31:46 -04001027/*
1028 * old = new - old
1029 */
Len Brownba3dec92016-04-22 20:31:46 -04001030int
Len Brownc98d5d92012-06-04 00:56:40 -04001031delta_thread(struct thread_data *new, struct thread_data *old,
1032 struct core_data *core_delta)
1033{
Len Brown388e9c82016-12-22 23:57:55 -05001034 int i;
1035 struct msr_counter *mp;
1036
Len Brownc98d5d92012-06-04 00:56:40 -04001037 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001038
Len Brownc98d5d92012-06-04 00:56:40 -04001039 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001040 if (old->tsc < (1000 * 1000))
1041 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1042 "You can disable all c-states by booting with \"idle=poll\"\n"
1043 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001044
Len Brownc98d5d92012-06-04 00:56:40 -04001045 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001046
Len Brown812db3f2017-02-10 00:25:41 -05001047 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001048 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1049 old->aperf = new->aperf - old->aperf;
1050 old->mperf = new->mperf - old->mperf;
1051 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001052 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001053 }
Len Brownc98d5d92012-06-04 00:56:40 -04001054 }
Len Brown103a8fe2010-10-22 23:53:03 -04001055
Len Brown103a8fe2010-10-22 23:53:03 -04001056
Len Brown144b44b2013-11-09 00:30:16 -05001057 if (use_c1_residency_msr) {
1058 /*
1059 * Some models have a dedicated C1 residency MSR,
1060 * which should be more accurate than the derivation below.
1061 */
1062 } else {
1063 /*
1064 * As counter collection is not atomic,
1065 * it is possible for mperf's non-halted cycles + idle states
1066 * to exceed TSC's all cycles: show c1 = 0% in that case.
1067 */
1068 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1069 old->c1 = 0;
1070 else {
1071 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001072 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001073 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001074 }
Len Brownc98d5d92012-06-04 00:56:40 -04001075 }
Len Brownc3ae3312012-06-13 21:31:46 -04001076
Len Brownc98d5d92012-06-04 00:56:40 -04001077 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001078 if (debug > 1)
1079 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001080 old->mperf = 1; /* divide by 0 protection */
1081 }
1082
Len Brown812db3f2017-02-10 00:25:41 -05001083 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001084 old->irq_count = new->irq_count - old->irq_count;
1085
Len Brown812db3f2017-02-10 00:25:41 -05001086 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001087 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001088
Len Brown388e9c82016-12-22 23:57:55 -05001089 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1090 if (mp->format == FORMAT_RAW)
1091 old->counter[i] = new->counter[i];
1092 else
1093 old->counter[i] = new->counter[i] - old->counter[i];
1094 }
Len Brownba3dec92016-04-22 20:31:46 -04001095 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001096}
1097
1098int delta_cpu(struct thread_data *t, struct core_data *c,
1099 struct pkg_data *p, struct thread_data *t2,
1100 struct core_data *c2, struct pkg_data *p2)
1101{
Len Brownba3dec92016-04-22 20:31:46 -04001102 int retval = 0;
1103
Len Brownc98d5d92012-06-04 00:56:40 -04001104 /* calculate core delta only for 1st thread in core */
1105 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1106 delta_core(c, c2);
1107
1108 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001109 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1110 if (retval)
1111 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001112
1113 /* calculate package delta only for 1st core in package */
1114 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001115 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001116
Len Brownba3dec92016-04-22 20:31:46 -04001117 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001118}
1119
1120void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1121{
Len Brown388e9c82016-12-22 23:57:55 -05001122 int i;
1123 struct msr_counter *mp;
1124
Len Brownc98d5d92012-06-04 00:56:40 -04001125 t->tsc = 0;
1126 t->aperf = 0;
1127 t->mperf = 0;
1128 t->c1 = 0;
1129
Len Brown562a2d32016-02-26 23:48:05 -05001130 t->irq_count = 0;
1131 t->smi_count = 0;
1132
Len Brownc98d5d92012-06-04 00:56:40 -04001133 /* tells format_counters to dump all fields from this set */
1134 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1135
1136 c->c3 = 0;
1137 c->c6 = 0;
1138 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001139 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001140 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001141
Len Brown0b2bb692015-03-26 00:50:30 -04001142 p->pkg_wtd_core_c0 = 0;
1143 p->pkg_any_core_c0 = 0;
1144 p->pkg_any_gfxe_c0 = 0;
1145 p->pkg_both_core_gfxe_c0 = 0;
1146
Len Brownc98d5d92012-06-04 00:56:40 -04001147 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001148 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001149 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001150 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001151 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001152 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001153 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001154 p->pc8 = 0;
1155 p->pc9 = 0;
1156 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001157
1158 p->energy_pkg = 0;
1159 p->energy_dram = 0;
1160 p->energy_cores = 0;
1161 p->energy_gfx = 0;
1162 p->rapl_pkg_perf_status = 0;
1163 p->rapl_dram_perf_status = 0;
1164 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001165
Len Brownfdf676e2016-02-27 01:28:12 -05001166 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001167 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001168 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1169 t->counter[i] = 0;
1170
1171 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1172 c->counter[i] = 0;
1173
1174 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1175 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001176}
1177int sum_counters(struct thread_data *t, struct core_data *c,
1178 struct pkg_data *p)
1179{
Len Brown388e9c82016-12-22 23:57:55 -05001180 int i;
1181 struct msr_counter *mp;
1182
Len Brownc98d5d92012-06-04 00:56:40 -04001183 average.threads.tsc += t->tsc;
1184 average.threads.aperf += t->aperf;
1185 average.threads.mperf += t->mperf;
1186 average.threads.c1 += t->c1;
1187
Len Brown562a2d32016-02-26 23:48:05 -05001188 average.threads.irq_count += t->irq_count;
1189 average.threads.smi_count += t->smi_count;
1190
Len Brown388e9c82016-12-22 23:57:55 -05001191 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1192 if (mp->format == FORMAT_RAW)
1193 continue;
1194 average.threads.counter[i] += t->counter[i];
1195 }
1196
Len Brownc98d5d92012-06-04 00:56:40 -04001197 /* sum per-core values only for 1st thread in core */
1198 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1199 return 0;
1200
1201 average.cores.c3 += c->c3;
1202 average.cores.c6 += c->c6;
1203 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001204 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001205
Len Brown889facb2012-11-08 00:48:57 -05001206 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1207
Len Brown388e9c82016-12-22 23:57:55 -05001208 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1209 if (mp->format == FORMAT_RAW)
1210 continue;
1211 average.cores.counter[i] += c->counter[i];
1212 }
1213
Len Brownc98d5d92012-06-04 00:56:40 -04001214 /* sum per-pkg values only for 1st core in pkg */
1215 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1216 return 0;
1217
Len Brown0b2bb692015-03-26 00:50:30 -04001218 if (do_skl_residency) {
1219 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1220 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1221 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1222 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1223 }
1224
Len Brownc98d5d92012-06-04 00:56:40 -04001225 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001226 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001227 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001228 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001229 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001230 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001231 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001232 average.packages.pc8 += p->pc8;
1233 average.packages.pc9 += p->pc9;
1234 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001235
Len Brown889facb2012-11-08 00:48:57 -05001236 average.packages.energy_pkg += p->energy_pkg;
1237 average.packages.energy_dram += p->energy_dram;
1238 average.packages.energy_cores += p->energy_cores;
1239 average.packages.energy_gfx += p->energy_gfx;
1240
Len Brownfdf676e2016-02-27 01:28:12 -05001241 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001242 average.packages.gfx_mhz = p->gfx_mhz;
1243
Len Brown889facb2012-11-08 00:48:57 -05001244 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1245
1246 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1247 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001248
1249 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1250 if (mp->format == FORMAT_RAW)
1251 continue;
1252 average.packages.counter[i] += p->counter[i];
1253 }
Len Brownc98d5d92012-06-04 00:56:40 -04001254 return 0;
1255}
1256/*
1257 * sum the counters for all cpus in the system
1258 * compute the weighted average
1259 */
1260void compute_average(struct thread_data *t, struct core_data *c,
1261 struct pkg_data *p)
1262{
Len Brown388e9c82016-12-22 23:57:55 -05001263 int i;
1264 struct msr_counter *mp;
1265
Len Brownc98d5d92012-06-04 00:56:40 -04001266 clear_counters(&average.threads, &average.cores, &average.packages);
1267
1268 for_all_cpus(sum_counters, t, c, p);
1269
1270 average.threads.tsc /= topo.num_cpus;
1271 average.threads.aperf /= topo.num_cpus;
1272 average.threads.mperf /= topo.num_cpus;
1273 average.threads.c1 /= topo.num_cpus;
1274
1275 average.cores.c3 /= topo.num_cores;
1276 average.cores.c6 /= topo.num_cores;
1277 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001278 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001279
Len Brown0b2bb692015-03-26 00:50:30 -04001280 if (do_skl_residency) {
1281 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1282 average.packages.pkg_any_core_c0 /= topo.num_packages;
1283 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1284 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1285 }
1286
Len Brownc98d5d92012-06-04 00:56:40 -04001287 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001288 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001289 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001290 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001291 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001292 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001293 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001294
1295 average.packages.pc8 /= topo.num_packages;
1296 average.packages.pc9 /= topo.num_packages;
1297 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001298
1299 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1300 if (mp->format == FORMAT_RAW)
1301 continue;
Len Brown41618e62017-02-09 18:25:22 -05001302 if (mp->flags & SYSFS_PERCPU && mp->type == COUNTER_ITEMS)
1303 continue;
Len Brown388e9c82016-12-22 23:57:55 -05001304 average.threads.counter[i] /= topo.num_cpus;
1305 }
1306 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1307 if (mp->format == FORMAT_RAW)
1308 continue;
1309 average.cores.counter[i] /= topo.num_cores;
1310 }
1311 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1312 if (mp->format == FORMAT_RAW)
1313 continue;
1314 average.packages.counter[i] /= topo.num_packages;
1315 }
Len Brownc98d5d92012-06-04 00:56:40 -04001316}
1317
1318static unsigned long long rdtsc(void)
1319{
1320 unsigned int low, high;
1321
1322 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1323
1324 return low | ((unsigned long long)high) << 32;
1325}
1326
Len Brownc98d5d92012-06-04 00:56:40 -04001327/*
Len Brown495c76542017-02-08 02:41:51 -05001328 * Open a file, and exit on failure
1329 */
1330FILE *fopen_or_die(const char *path, const char *mode)
1331{
1332 FILE *filep = fopen(path, mode);
1333
1334 if (!filep)
1335 err(1, "%s: open failed", path);
1336 return filep;
1337}
1338/*
1339 * snapshot_sysfs_counter()
1340 *
1341 * return snapshot of given counter
1342 */
1343unsigned long long snapshot_sysfs_counter(char *path)
1344{
1345 FILE *fp;
1346 int retval;
1347 unsigned long long counter;
1348
1349 fp = fopen_or_die(path, "r");
1350
1351 retval = fscanf(fp, "%lld", &counter);
1352 if (retval != 1)
1353 err(1, "snapshot_sysfs_counter(%s)", path);
1354
1355 fclose(fp);
1356
1357 return counter;
1358}
1359
1360int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1361{
1362 if (mp->msr_num != 0) {
1363 if (get_msr(cpu, mp->msr_num, counterp))
1364 return -1;
1365 } else {
Len Brown41618e62017-02-09 18:25:22 -05001366 char path[128];
1367
1368 if (mp->flags & SYSFS_PERCPU) {
1369 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1370 cpu, mp->path);
1371
1372 *counterp = snapshot_sysfs_counter(path);
1373 } else {
1374 *counterp = snapshot_sysfs_counter(mp->path);
1375 }
Len Brown495c76542017-02-08 02:41:51 -05001376 }
1377
1378 return 0;
1379}
1380
1381/*
Len Brownc98d5d92012-06-04 00:56:40 -04001382 * get_counters(...)
1383 * migrate to cpu
1384 * acquire and record local counters for that cpu
1385 */
1386int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1387{
1388 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001389 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001390 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001391 struct msr_counter *mp;
1392 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001393
Len Browne52966c2012-11-08 22:38:05 -05001394 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001395 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001396 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001397 }
Len Brownc98d5d92012-06-04 00:56:40 -04001398
Len Brown0102b062016-02-27 03:11:29 -05001399retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001400 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1401
Len Brown812db3f2017-02-10 00:25:41 -05001402 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001403 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1404
1405 /*
1406 * The TSC, APERF and MPERF must be read together for
1407 * APERF/MPERF and MPERF/TSC to give accurate results.
1408 *
1409 * Unfortunately, APERF and MPERF are read by
1410 * individual system call, so delays may occur
1411 * between them. If the time to read them
1412 * varies by a large amount, we re-read them.
1413 */
1414
1415 /*
1416 * This initial dummy APERF read has been seen to
1417 * reduce jitter in the subsequent reads.
1418 */
1419
Len Brown9c63a652012-10-31 01:29:52 -04001420 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001421 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001422
1423 t->tsc = rdtsc(); /* re-read close to APERF */
1424
1425 tsc_before = t->tsc;
1426
1427 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1428 return -3;
1429
1430 tsc_between = rdtsc();
1431
Len Brown9c63a652012-10-31 01:29:52 -04001432 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001433 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001434
1435 tsc_after = rdtsc();
1436
1437 aperf_time = tsc_between - tsc_before;
1438 mperf_time = tsc_after - tsc_between;
1439
1440 /*
1441 * If the system call latency to read APERF and MPERF
1442 * differ by more than 2x, then try again.
1443 */
1444 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1445 aperf_mperf_retry_count++;
1446 if (aperf_mperf_retry_count < 5)
1447 goto retry;
1448 else
1449 warnx("cpu%d jitter %lld %lld",
1450 cpu, aperf_time, mperf_time);
1451 }
1452 aperf_mperf_retry_count = 0;
1453
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001454 t->aperf = t->aperf * aperf_mperf_multiplier;
1455 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001456 }
1457
Len Brown812db3f2017-02-10 00:25:41 -05001458 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001459 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001460 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001461 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1462 return -5;
1463 t->smi_count = msr & 0xFFFFFFFF;
1464 }
Len Brown0539ba12017-02-10 00:27:20 -05001465 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001466 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1467 return -6;
1468 }
1469
Len Brown388e9c82016-12-22 23:57:55 -05001470 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001471 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001472 return -10;
1473 }
1474
Len Brownc98d5d92012-06-04 00:56:40 -04001475 /* collect core counters only for 1st thread in core */
1476 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1477 return 0;
1478
Len Brown812db3f2017-02-10 00:25:41 -05001479 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001480 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1481 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001482 }
1483
Len Brown812db3f2017-02-10 00:25:41 -05001484 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001485 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1486 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001487 } else if (do_knl_cstates) {
1488 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1489 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001490 }
1491
Len Brown812db3f2017-02-10 00:25:41 -05001492 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001493 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1494 return -8;
1495
Len Brown0539ba12017-02-10 00:27:20 -05001496 if (DO_BIC(BIC_Mod_c6))
1497 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1498 return -8;
1499
Len Brown812db3f2017-02-10 00:25:41 -05001500 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001501 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1502 return -9;
1503 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1504 }
1505
Len Brown388e9c82016-12-22 23:57:55 -05001506 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001507 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001508 return -10;
1509 }
Len Brown889facb2012-11-08 00:48:57 -05001510
Len Brownc98d5d92012-06-04 00:56:40 -04001511 /* collect package counters only for 1st core in package */
1512 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1513 return 0;
1514
Len Brown0b2bb692015-03-26 00:50:30 -04001515 if (do_skl_residency) {
1516 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1517 return -10;
1518 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1519 return -11;
1520 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1521 return -12;
1522 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1523 return -13;
1524 }
Len Brown0f47c082017-01-27 00:50:45 -05001525 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001526 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1527 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001528 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001529 if (do_slm_cstates) {
1530 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1531 return -10;
1532 } else {
1533 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1534 return -10;
1535 }
1536 }
1537
Len Brown0f47c082017-01-27 00:50:45 -05001538 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001539 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1540 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001541 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001542 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1543 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001544 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001545 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1546 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001547 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001548 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1549 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001550 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001551 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1552 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001553
Len Brown889facb2012-11-08 00:48:57 -05001554 if (do_rapl & RAPL_PKG) {
1555 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1556 return -13;
1557 p->energy_pkg = msr & 0xFFFFFFFF;
1558 }
Jacob Pan91484942016-06-16 09:48:20 -07001559 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001560 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1561 return -14;
1562 p->energy_cores = msr & 0xFFFFFFFF;
1563 }
1564 if (do_rapl & RAPL_DRAM) {
1565 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1566 return -15;
1567 p->energy_dram = msr & 0xFFFFFFFF;
1568 }
1569 if (do_rapl & RAPL_GFX) {
1570 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1571 return -16;
1572 p->energy_gfx = msr & 0xFFFFFFFF;
1573 }
1574 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1575 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1576 return -16;
1577 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1578 }
1579 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1580 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1581 return -16;
1582 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1583 }
Len Brown812db3f2017-02-10 00:25:41 -05001584 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001585 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1586 return -17;
1587 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1588 }
Len Brownfdf676e2016-02-27 01:28:12 -05001589
Len Brown812db3f2017-02-10 00:25:41 -05001590 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001591 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1592
Len Brown812db3f2017-02-10 00:25:41 -05001593 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001594 p->gfx_mhz = gfx_cur_mhz;
1595
Len Brown388e9c82016-12-22 23:57:55 -05001596 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001597 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001598 return -10;
1599 }
1600
Len Brown103a8fe2010-10-22 23:53:03 -04001601 return 0;
1602}
1603
Len Brownee7e38e2015-02-09 23:39:45 -05001604/*
1605 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1606 * If you change the values, note they are used both in comparisons
1607 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1608 */
1609
1610#define PCLUKN 0 /* Unknown */
1611#define PCLRSV 1 /* Reserved */
1612#define PCL__0 2 /* PC0 */
1613#define PCL__1 3 /* PC1 */
1614#define PCL__2 4 /* PC2 */
1615#define PCL__3 5 /* PC3 */
1616#define PCL__4 6 /* PC4 */
1617#define PCL__6 7 /* PC6 */
1618#define PCL_6N 8 /* PC6 No Retention */
1619#define PCL_6R 9 /* PC6 Retention */
1620#define PCL__7 10 /* PC7 */
1621#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001622#define PCL__8 12 /* PC8 */
1623#define PCL__9 13 /* PC9 */
1624#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001625
1626int pkg_cstate_limit = PCLUKN;
1627char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001628 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001629
Len Browne9257f52015-04-01 21:02:57 -04001630int 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};
1631int 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};
1632int 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 -05001633int 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 -05001634int 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 -04001635int 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 -04001636int 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 -05001637int 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 -05001638
Len Browna2b7b742015-09-26 00:12:38 -04001639
1640static void
1641calculate_tsc_tweak()
1642{
Len Browna2b7b742015-09-26 00:12:38 -04001643 tsc_tweak = base_hz / tsc_hz;
1644}
1645
Len Brownfcd17212015-03-23 20:29:09 -04001646static void
1647dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001648{
1649 unsigned long long msr;
1650 unsigned int ratio;
1651
Len Brownec0adc52015-11-12 02:42:31 -05001652 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001653
Len Brownb7d8c142016-02-13 23:36:17 -05001654 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001655
Len Brown103a8fe2010-10-22 23:53:03 -04001656 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001657 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001658 ratio, bclk, ratio * bclk);
1659
1660 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001661 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001662 ratio, bclk, ratio * bclk);
1663
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001664 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001665 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001666 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001667
Len Brownfcd17212015-03-23 20:29:09 -04001668 return;
1669}
1670
1671static void
1672dump_hsw_turbo_ratio_limits(void)
1673{
1674 unsigned long long msr;
1675 unsigned int ratio;
1676
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001677 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001678
Len Brownb7d8c142016-02-13 23:36:17 -05001679 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001680
1681 ratio = (msr >> 8) & 0xFF;
1682 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001683 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001684 ratio, bclk, ratio * bclk);
1685
1686 ratio = (msr >> 0) & 0xFF;
1687 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001688 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001689 ratio, bclk, ratio * bclk);
1690 return;
1691}
1692
1693static void
1694dump_ivt_turbo_ratio_limits(void)
1695{
1696 unsigned long long msr;
1697 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001698
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001699 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001700
Len Brownb7d8c142016-02-13 23:36:17 -05001701 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001702
1703 ratio = (msr >> 56) & 0xFF;
1704 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001705 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001706 ratio, bclk, ratio * bclk);
1707
1708 ratio = (msr >> 48) & 0xFF;
1709 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001710 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001711 ratio, bclk, ratio * bclk);
1712
1713 ratio = (msr >> 40) & 0xFF;
1714 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001715 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001716 ratio, bclk, ratio * bclk);
1717
1718 ratio = (msr >> 32) & 0xFF;
1719 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001720 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001721 ratio, bclk, ratio * bclk);
1722
1723 ratio = (msr >> 24) & 0xFF;
1724 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001725 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001726 ratio, bclk, ratio * bclk);
1727
1728 ratio = (msr >> 16) & 0xFF;
1729 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001730 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001731 ratio, bclk, ratio * bclk);
1732
1733 ratio = (msr >> 8) & 0xFF;
1734 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001735 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001736 ratio, bclk, ratio * bclk);
1737
1738 ratio = (msr >> 0) & 0xFF;
1739 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001740 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001741 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001742 return;
1743}
Len Brown31e07522017-01-31 23:07:49 -05001744int has_turbo_ratio_group_limits(int family, int model)
1745{
1746
1747 if (!genuine_intel)
1748 return 0;
1749
1750 switch (model) {
1751 case INTEL_FAM6_ATOM_GOLDMONT:
1752 case INTEL_FAM6_SKYLAKE_X:
1753 case INTEL_FAM6_ATOM_DENVERTON:
1754 return 1;
1755 }
1756 return 0;
1757}
Len Brown6574a5d2012-09-21 00:01:31 -04001758
Len Brownfcd17212015-03-23 20:29:09 -04001759static void
Len Brown31e07522017-01-31 23:07:49 -05001760dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001761{
Len Brown31e07522017-01-31 23:07:49 -05001762 unsigned long long msr, core_counts;
1763 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001764
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001765 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001766 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001767
Len Brown31e07522017-01-31 23:07:49 -05001768 if (has_turbo_ratio_group_limits(family, model)) {
1769 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1770 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1771 } else {
1772 core_counts = 0x0807060504030201;
1773 }
1774
Len Brown6574a5d2012-09-21 00:01:31 -04001775 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001776 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001777 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001778 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1779 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001780
1781 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001782 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001783 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001784 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1785 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001786
1787 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001788 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001789 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001790 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1791 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001792
1793 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001794 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001795 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001796 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1797 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001798
Len Brown103a8fe2010-10-22 23:53:03 -04001799 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001800 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001801 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001802 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1803 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001804
1805 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001806 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001807 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001808 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1809 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001810
1811 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001812 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001813 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001814 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1815 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001816
1817 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001818 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001819 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001820 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1821 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04001822 return;
1823}
Len Brown3a9a9412014-08-15 02:39:52 -04001824
Len Brownfcd17212015-03-23 20:29:09 -04001825static void
Len Brown0f7887c2017-01-12 23:49:18 -05001826dump_atom_turbo_ratio_limits(void)
1827{
1828 unsigned long long msr;
1829 unsigned int ratio;
1830
1831 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1832 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1833
1834 ratio = (msr >> 0) & 0x3F;
1835 if (ratio)
1836 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1837 ratio, bclk, ratio * bclk);
1838
1839 ratio = (msr >> 8) & 0x3F;
1840 if (ratio)
1841 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1842 ratio, bclk, ratio * bclk);
1843
1844 ratio = (msr >> 16) & 0x3F;
1845 if (ratio)
1846 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1847 ratio, bclk, ratio * bclk);
1848
1849 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1850 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1851
1852 ratio = (msr >> 24) & 0x3F;
1853 if (ratio)
1854 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1855 ratio, bclk, ratio * bclk);
1856
1857 ratio = (msr >> 16) & 0x3F;
1858 if (ratio)
1859 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1860 ratio, bclk, ratio * bclk);
1861
1862 ratio = (msr >> 8) & 0x3F;
1863 if (ratio)
1864 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1865 ratio, bclk, ratio * bclk);
1866
1867 ratio = (msr >> 0) & 0x3F;
1868 if (ratio)
1869 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1870 ratio, bclk, ratio * bclk);
1871}
1872
1873static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001874dump_knl_turbo_ratio_limits(void)
1875{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001876 const unsigned int buckets_no = 7;
1877
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001878 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001879 int delta_cores, delta_ratio;
1880 int i, b_nr;
1881 unsigned int cores[buckets_no];
1882 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001883
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001884 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001885
Len Brownb7d8c142016-02-13 23:36:17 -05001886 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001887 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001888
1889 /**
1890 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001891 * [0] -- Reserved
1892 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001893 * [15:8] -- Base value of freq ratio of bucket 1.
1894 * [20:16] -- +ve delta of number of active cores of bucket 2.
1895 * i.e. active cores of bucket 2 =
1896 * active cores of bucket 1 + delta
1897 * [23:21] -- Negative delta of freq ratio of bucket 2.
1898 * i.e. freq ratio of bucket 2 =
1899 * freq ratio of bucket 1 - delta
1900 * [28:24]-- +ve delta of number of active cores of bucket 3.
1901 * [31:29]-- -ve delta of freq ratio of bucket 3.
1902 * [36:32]-- +ve delta of number of active cores of bucket 4.
1903 * [39:37]-- -ve delta of freq ratio of bucket 4.
1904 * [44:40]-- +ve delta of number of active cores of bucket 5.
1905 * [47:45]-- -ve delta of freq ratio of bucket 5.
1906 * [52:48]-- +ve delta of number of active cores of bucket 6.
1907 * [55:53]-- -ve delta of freq ratio of bucket 6.
1908 * [60:56]-- +ve delta of number of active cores of bucket 7.
1909 * [63:61]-- -ve delta of freq ratio of bucket 7.
1910 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001911
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001912 b_nr = 0;
1913 cores[b_nr] = (msr & 0xFF) >> 1;
1914 ratio[b_nr] = (msr >> 8) & 0xFF;
1915
1916 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001917 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001918 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001919
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001920 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1921 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1922 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001923 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001924
1925 for (i = buckets_no - 1; i >= 0; i--)
1926 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001927 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05001928 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001929 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001930}
1931
1932static void
Len Brownfcd17212015-03-23 20:29:09 -04001933dump_nhm_cst_cfg(void)
1934{
1935 unsigned long long msr;
1936
Len Brown1df2e552017-01-07 23:24:57 -05001937 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001938
1939#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1940#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1941
Len Brown1df2e552017-01-07 23:24:57 -05001942 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001943
Len Brownb7d8c142016-02-13 23:36:17 -05001944 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001945 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1946 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1947 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1948 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1949 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001950 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001951 pkg_cstate_limit_strings[pkg_cstate_limit]);
1952 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001953}
1954
Len Brown6fb31432015-06-17 16:23:45 -04001955static void
1956dump_config_tdp(void)
1957{
1958 unsigned long long msr;
1959
1960 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001961 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001962 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001963
1964 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001965 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001966 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001967 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1968 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1969 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1970 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001971 }
Len Brownb7d8c142016-02-13 23:36:17 -05001972 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001973
1974 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001975 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001976 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001977 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1978 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1979 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1980 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001981 }
Len Brownb7d8c142016-02-13 23:36:17 -05001982 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001983
1984 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001985 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001986 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001987 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1988 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1989 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001990
Len Brown6fb31432015-06-17 16:23:45 -04001991 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001992 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001993 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001994 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1995 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001996}
Len Brown5a634262016-04-06 17:15:55 -04001997
1998unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1999
2000void print_irtl(void)
2001{
2002 unsigned long long msr;
2003
2004 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2005 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2006 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2007 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2008
2009 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2010 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2011 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2012 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2013
2014 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2015 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2016 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2017 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2018
2019 if (!do_irtl_hsw)
2020 return;
2021
2022 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2023 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2024 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2025 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2026
2027 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2028 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2029 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2030 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2031
2032 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2033 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2034 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2035 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2036
2037}
Len Brown36229892016-02-26 20:51:02 -05002038void free_fd_percpu(void)
2039{
2040 int i;
2041
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002042 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002043 if (fd_percpu[i] != 0)
2044 close(fd_percpu[i]);
2045 }
2046
2047 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002048}
2049
Len Brownc98d5d92012-06-04 00:56:40 -04002050void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002051{
Len Brownc98d5d92012-06-04 00:56:40 -04002052 CPU_FREE(cpu_present_set);
2053 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002054 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002055
Len Brownc98d5d92012-06-04 00:56:40 -04002056 CPU_FREE(cpu_affinity_set);
2057 cpu_affinity_set = NULL;
2058 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002059
Len Brownc98d5d92012-06-04 00:56:40 -04002060 free(thread_even);
2061 free(core_even);
2062 free(package_even);
2063
2064 thread_even = NULL;
2065 core_even = NULL;
2066 package_even = NULL;
2067
2068 free(thread_odd);
2069 free(core_odd);
2070 free(package_odd);
2071
2072 thread_odd = NULL;
2073 core_odd = NULL;
2074 package_odd = NULL;
2075
2076 free(output_buffer);
2077 output_buffer = NULL;
2078 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002079
2080 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002081
2082 free(irq_column_2_cpu);
2083 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002084}
2085
Josh Triplett57a42a32013-08-20 17:20:17 -07002086
2087/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002088 * Parse a file containing a single int.
2089 */
2090int parse_int_file(const char *fmt, ...)
2091{
2092 va_list args;
2093 char path[PATH_MAX];
2094 FILE *filep;
2095 int value;
2096
2097 va_start(args, fmt);
2098 vsnprintf(path, sizeof(path), fmt, args);
2099 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002100 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002101 if (fscanf(filep, "%d", &value) != 1)
2102 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002103 fclose(filep);
2104 return value;
2105}
2106
2107/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002108 * get_cpu_position_in_core(cpu)
2109 * return the position of the CPU among its HT siblings in the core
2110 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002111 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002112int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002113{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002114 char path[64];
2115 FILE *filep;
2116 int this_cpu;
2117 char character;
2118 int i;
2119
2120 sprintf(path,
2121 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2122 cpu);
2123 filep = fopen(path, "r");
2124 if (filep == NULL) {
2125 perror(path);
2126 exit(1);
2127 }
2128
2129 for (i = 0; i < topo.num_threads_per_core; i++) {
2130 fscanf(filep, "%d", &this_cpu);
2131 if (this_cpu == cpu) {
2132 fclose(filep);
2133 return i;
2134 }
2135
2136 /* Account for no separator after last thread*/
2137 if (i != (topo.num_threads_per_core - 1))
2138 fscanf(filep, "%c", &character);
2139 }
2140
2141 fclose(filep);
2142 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002143}
2144
Len Brownc98d5d92012-06-04 00:56:40 -04002145/*
2146 * cpu_is_first_core_in_package(cpu)
2147 * return 1 if given CPU is 1st core in package
2148 */
2149int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002150{
Josh Triplett95aebc42013-08-20 17:20:16 -07002151 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002152}
2153
2154int get_physical_package_id(int cpu)
2155{
Josh Triplett95aebc42013-08-20 17:20:16 -07002156 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002157}
2158
2159int get_core_id(int cpu)
2160{
Josh Triplett95aebc42013-08-20 17:20:16 -07002161 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002162}
2163
Len Brownc98d5d92012-06-04 00:56:40 -04002164int get_num_ht_siblings(int cpu)
2165{
2166 char path[80];
2167 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002168 int sib1;
2169 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002170 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002171 char str[100];
2172 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002173
2174 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002175 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002176
Len Brownc98d5d92012-06-04 00:56:40 -04002177 /*
2178 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002179 * A ',' separated or '-' separated set of numbers
2180 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002181 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002182 fscanf(filep, "%d%c\n", &sib1, &character);
2183 fseek(filep, 0, SEEK_SET);
2184 fgets(str, 100, filep);
2185 ch = strchr(str, character);
2186 while (ch != NULL) {
2187 matches++;
2188 ch = strchr(ch+1, character);
2189 }
Len Brownc98d5d92012-06-04 00:56:40 -04002190
2191 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002192 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002193}
2194
Len Brown103a8fe2010-10-22 23:53:03 -04002195/*
Len Brownc98d5d92012-06-04 00:56:40 -04002196 * run func(thread, core, package) in topology order
2197 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002198 */
2199
Len Brownc98d5d92012-06-04 00:56:40 -04002200int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2201 struct pkg_data *, struct thread_data *, struct core_data *,
2202 struct pkg_data *), struct thread_data *thread_base,
2203 struct core_data *core_base, struct pkg_data *pkg_base,
2204 struct thread_data *thread_base2, struct core_data *core_base2,
2205 struct pkg_data *pkg_base2)
2206{
2207 int retval, pkg_no, core_no, thread_no;
2208
2209 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2210 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2211 for (thread_no = 0; thread_no <
2212 topo.num_threads_per_core; ++thread_no) {
2213 struct thread_data *t, *t2;
2214 struct core_data *c, *c2;
2215 struct pkg_data *p, *p2;
2216
2217 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2218
2219 if (cpu_is_not_present(t->cpu_id))
2220 continue;
2221
2222 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2223
2224 c = GET_CORE(core_base, core_no, pkg_no);
2225 c2 = GET_CORE(core_base2, core_no, pkg_no);
2226
2227 p = GET_PKG(pkg_base, pkg_no);
2228 p2 = GET_PKG(pkg_base2, pkg_no);
2229
2230 retval = func(t, c, p, t2, c2, p2);
2231 if (retval)
2232 return retval;
2233 }
2234 }
2235 }
2236 return 0;
2237}
2238
2239/*
2240 * run func(cpu) on every cpu in /proc/stat
2241 * return max_cpu number
2242 */
2243int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002244{
2245 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002246 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002247 int retval;
2248
Josh Triplett57a42a32013-08-20 17:20:17 -07002249 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002250
2251 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002252 if (retval != 0)
2253 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002254
Len Brownc98d5d92012-06-04 00:56:40 -04002255 while (1) {
2256 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 -04002257 if (retval != 1)
2258 break;
2259
Len Brownc98d5d92012-06-04 00:56:40 -04002260 retval = func(cpu_num);
2261 if (retval) {
2262 fclose(fp);
2263 return(retval);
2264 }
Len Brown103a8fe2010-10-22 23:53:03 -04002265 }
2266 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002267 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002268}
2269
2270void re_initialize(void)
2271{
Len Brownc98d5d92012-06-04 00:56:40 -04002272 free_all_buffers();
2273 setup_all_buffers();
2274 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002275}
2276
Len Brownc98d5d92012-06-04 00:56:40 -04002277
Len Brown103a8fe2010-10-22 23:53:03 -04002278/*
Len Brownc98d5d92012-06-04 00:56:40 -04002279 * count_cpus()
2280 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002281 */
Len Brownc98d5d92012-06-04 00:56:40 -04002282int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002283{
Len Brownc98d5d92012-06-04 00:56:40 -04002284 if (topo.max_cpu_num < cpu)
2285 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002286
Len Brownc98d5d92012-06-04 00:56:40 -04002287 topo.num_cpus += 1;
2288 return 0;
2289}
2290int mark_cpu_present(int cpu)
2291{
2292 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002293 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002294}
2295
Len Brown562a2d32016-02-26 23:48:05 -05002296/*
2297 * snapshot_proc_interrupts()
2298 *
2299 * read and record summary of /proc/interrupts
2300 *
2301 * return 1 if config change requires a restart, else return 0
2302 */
2303int snapshot_proc_interrupts(void)
2304{
2305 static FILE *fp;
2306 int column, retval;
2307
2308 if (fp == NULL)
2309 fp = fopen_or_die("/proc/interrupts", "r");
2310 else
2311 rewind(fp);
2312
2313 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2314 for (column = 0; column < topo.num_cpus; ++column) {
2315 int cpu_number;
2316
2317 retval = fscanf(fp, " CPU%d", &cpu_number);
2318 if (retval != 1)
2319 break;
2320
2321 if (cpu_number > topo.max_cpu_num) {
2322 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2323 return 1;
2324 }
2325
2326 irq_column_2_cpu[column] = cpu_number;
2327 irqs_per_cpu[cpu_number] = 0;
2328 }
2329
2330 /* read /proc/interrupt count lines and sum up irqs per cpu */
2331 while (1) {
2332 int column;
2333 char buf[64];
2334
2335 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2336 if (retval != 1)
2337 break;
2338
2339 /* read the count per cpu */
2340 for (column = 0; column < topo.num_cpus; ++column) {
2341
2342 int cpu_number, irq_count;
2343
2344 retval = fscanf(fp, " %d", &irq_count);
2345 if (retval != 1)
2346 break;
2347
2348 cpu_number = irq_column_2_cpu[column];
2349 irqs_per_cpu[cpu_number] += irq_count;
2350
2351 }
2352
2353 while (getc(fp) != '\n')
2354 ; /* flush interrupt description */
2355
2356 }
2357 return 0;
2358}
Len Brown27d47352016-02-27 00:37:54 -05002359/*
Len Brownfdf676e2016-02-27 01:28:12 -05002360 * snapshot_gfx_rc6_ms()
2361 *
2362 * record snapshot of
2363 * /sys/class/drm/card0/power/rc6_residency_ms
2364 *
2365 * return 1 if config change requires a restart, else return 0
2366 */
2367int snapshot_gfx_rc6_ms(void)
2368{
2369 FILE *fp;
2370 int retval;
2371
2372 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2373
2374 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2375 if (retval != 1)
2376 err(1, "GFX rc6");
2377
2378 fclose(fp);
2379
2380 return 0;
2381}
2382/*
Len Brown27d47352016-02-27 00:37:54 -05002383 * snapshot_gfx_mhz()
2384 *
2385 * record snapshot of
2386 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2387 *
2388 * return 1 if config change requires a restart, else return 0
2389 */
2390int snapshot_gfx_mhz(void)
2391{
2392 static FILE *fp;
2393 int retval;
2394
2395 if (fp == NULL)
2396 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2397 else
2398 rewind(fp);
2399
2400 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2401 if (retval != 1)
2402 err(1, "GFX MHz");
2403
2404 return 0;
2405}
Len Brown562a2d32016-02-26 23:48:05 -05002406
2407/*
2408 * snapshot /proc and /sys files
2409 *
2410 * return 1 if configuration restart needed, else return 0
2411 */
2412int snapshot_proc_sysfs_files(void)
2413{
Len Brown218f0e82017-02-14 22:07:52 -05002414 if (DO_BIC(BIC_IRQ))
2415 if (snapshot_proc_interrupts())
2416 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002417
Len Brown812db3f2017-02-10 00:25:41 -05002418 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002419 snapshot_gfx_rc6_ms();
2420
Len Brown812db3f2017-02-10 00:25:41 -05002421 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002422 snapshot_gfx_mhz();
2423
Len Brown562a2d32016-02-26 23:48:05 -05002424 return 0;
2425}
2426
Len Brown103a8fe2010-10-22 23:53:03 -04002427void turbostat_loop()
2428{
Len Brownc98d5d92012-06-04 00:56:40 -04002429 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002430 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002431
Len Brown103a8fe2010-10-22 23:53:03 -04002432restart:
Len Browne52966c2012-11-08 22:38:05 -05002433 restarted++;
2434
Len Brown562a2d32016-02-26 23:48:05 -05002435 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002436 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002437 if (retval < -1) {
2438 exit(retval);
2439 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002440 if (restarted > 1) {
2441 exit(retval);
2442 }
Len Brownc98d5d92012-06-04 00:56:40 -04002443 re_initialize();
2444 goto restart;
2445 }
Len Browne52966c2012-11-08 22:38:05 -05002446 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002447 gettimeofday(&tv_even, (struct timezone *)NULL);
2448
2449 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002450 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002451 re_initialize();
2452 goto restart;
2453 }
Len Brown2a0609c2016-02-12 22:44:48 -05002454 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002455 if (snapshot_proc_sysfs_files())
2456 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002457 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002458 if (retval < -1) {
2459 exit(retval);
2460 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002461 re_initialize();
2462 goto restart;
2463 }
Len Brown103a8fe2010-10-22 23:53:03 -04002464 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002465 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002466 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2467 re_initialize();
2468 goto restart;
2469 }
Len Brownc98d5d92012-06-04 00:56:40 -04002470 compute_average(EVEN_COUNTERS);
2471 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002472 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002473 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002474 if (snapshot_proc_sysfs_files())
2475 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002476 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002477 if (retval < -1) {
2478 exit(retval);
2479 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002480 re_initialize();
2481 goto restart;
2482 }
Len Brown103a8fe2010-10-22 23:53:03 -04002483 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002484 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002485 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2486 re_initialize();
2487 goto restart;
2488 }
Len Brownc98d5d92012-06-04 00:56:40 -04002489 compute_average(ODD_COUNTERS);
2490 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002491 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002492 }
2493}
2494
2495void check_dev_msr()
2496{
2497 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002498 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002499
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002500 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2501 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002502 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2503 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002504}
2505
Len Brown98481e72014-08-15 00:36:50 -04002506void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002507{
Len Brown98481e72014-08-15 00:36:50 -04002508 struct __user_cap_header_struct cap_header_data;
2509 cap_user_header_t cap_header = &cap_header_data;
2510 struct __user_cap_data_struct cap_data_data;
2511 cap_user_data_t cap_data = &cap_data_data;
2512 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2513 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002514 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002515
2516 /* check for CAP_SYS_RAWIO */
2517 cap_header->pid = getpid();
2518 cap_header->version = _LINUX_CAPABILITY_VERSION;
2519 if (capget(cap_header, cap_data) < 0)
2520 err(-6, "capget(2) failed");
2521
2522 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2523 do_exit++;
2524 warnx("capget(CAP_SYS_RAWIO) failed,"
2525 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2526 }
2527
2528 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002529 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2530 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002531 do_exit++;
2532 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2533 }
2534
2535 /* if all else fails, thell them to be root */
2536 if (do_exit)
2537 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002538 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002539
2540 if (do_exit)
2541 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002542}
2543
Len Brownd7899442015-01-23 00:12:33 -05002544/*
2545 * NHM adds support for additional MSRs:
2546 *
2547 * MSR_SMI_COUNT 0x00000034
2548 *
Len Brownec0adc52015-11-12 02:42:31 -05002549 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002550 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002551 *
Len Browncf4cbe52017-01-01 13:08:33 -05002552 * MSR_MISC_PWR_MGMT 0x000001aa
2553 *
Len Brownd7899442015-01-23 00:12:33 -05002554 * MSR_PKG_C3_RESIDENCY 0x000003f8
2555 * MSR_PKG_C6_RESIDENCY 0x000003f9
2556 * MSR_CORE_C3_RESIDENCY 0x000003fc
2557 * MSR_CORE_C6_RESIDENCY 0x000003fd
2558 *
Len Brownee7e38e2015-02-09 23:39:45 -05002559 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002560 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002561 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002562 */
Len Brownee7e38e2015-02-09 23:39:45 -05002563int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002564{
Len Brownee7e38e2015-02-09 23:39:45 -05002565 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002566 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002567 int *pkg_cstate_limits;
2568
Len Brown103a8fe2010-10-22 23:53:03 -04002569 if (!genuine_intel)
2570 return 0;
2571
2572 if (family != 6)
2573 return 0;
2574
Len Brown21ed5572015-10-19 22:37:40 -04002575 bclk = discover_bclk(family, model);
2576
Len Brown103a8fe2010-10-22 23:53:03 -04002577 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002578 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2579 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002580 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002581 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2582 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2583 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2584 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002585 pkg_cstate_limits = nhm_pkg_cstate_limits;
2586 break;
Len Brown869ce692016-06-16 23:22:37 -04002587 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2588 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2589 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2590 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002591 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002592 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002593 break;
Len Brown869ce692016-06-16 23:22:37 -04002594 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2595 case INTEL_FAM6_HASWELL_X: /* HSX */
2596 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2597 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2598 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2599 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2600 case INTEL_FAM6_BROADWELL_X: /* BDX */
2601 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2602 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2603 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2604 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2605 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002606 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002607 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002608 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002609 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2610 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002611 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002612 break;
Len Brown869ce692016-06-16 23:22:37 -04002613 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002614 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002615 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002616 pkg_cstate_limits = slv_pkg_cstate_limits;
2617 break;
Len Brown869ce692016-06-16 23:22:37 -04002618 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002619 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002620 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002621 break;
Len Brown869ce692016-06-16 23:22:37 -04002622 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002623 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002624 pkg_cstate_limits = phi_pkg_cstate_limits;
2625 break;
Len Brown869ce692016-06-16 23:22:37 -04002626 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002627 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002628 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002629 pkg_cstate_limits = bxt_pkg_cstate_limits;
2630 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002631 default:
2632 return 0;
2633 }
Len Brown1df2e552017-01-07 23:24:57 -05002634 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002635 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002636
Len Brownec0adc52015-11-12 02:42:31 -05002637 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002638 base_ratio = (msr >> 8) & 0xFF;
2639
2640 base_hz = base_ratio * bclk * 1000000;
2641 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002642 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002643}
Len Brown0f7887c2017-01-12 23:49:18 -05002644/*
Len Brown495c76542017-02-08 02:41:51 -05002645 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002646 *
2647 * MSR_CC6_DEMOTION_POLICY_CONFIG
2648 * MSR_MC6_DEMOTION_POLICY_CONFIG
2649 */
2650
2651int has_slv_msrs(unsigned int family, unsigned int model)
2652{
2653 if (!genuine_intel)
2654 return 0;
2655
2656 switch (model) {
2657 case INTEL_FAM6_ATOM_SILVERMONT1:
2658 case INTEL_FAM6_ATOM_MERRIFIELD:
2659 case INTEL_FAM6_ATOM_MOOREFIELD:
2660 return 1;
2661 }
2662 return 0;
2663}
Len Brown7170a372017-01-27 02:13:27 -05002664int is_dnv(unsigned int family, unsigned int model)
2665{
2666
2667 if (!genuine_intel)
2668 return 0;
2669
2670 switch (model) {
2671 case INTEL_FAM6_ATOM_DENVERTON:
2672 return 1;
2673 }
2674 return 0;
2675}
Len Brownade0eba2017-02-10 01:56:47 -05002676int is_bdx(unsigned int family, unsigned int model)
2677{
2678
2679 if (!genuine_intel)
2680 return 0;
2681
2682 switch (model) {
2683 case INTEL_FAM6_BROADWELL_X:
2684 case INTEL_FAM6_BROADWELL_XEON_D:
2685 return 1;
2686 }
2687 return 0;
2688}
Len Brown34c761972017-01-27 02:36:41 -05002689int is_skx(unsigned int family, unsigned int model)
2690{
2691
2692 if (!genuine_intel)
2693 return 0;
2694
2695 switch (model) {
2696 case INTEL_FAM6_SKYLAKE_X:
2697 return 1;
2698 }
2699 return 0;
2700}
Len Brown0f7887c2017-01-12 23:49:18 -05002701
Len Brown31e07522017-01-31 23:07:49 -05002702int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002703{
Len Brown0f7887c2017-01-12 23:49:18 -05002704 if (has_slv_msrs(family, model))
2705 return 0;
2706
Len Brownd7899442015-01-23 00:12:33 -05002707 switch (model) {
2708 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002709 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2710 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2711 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002712 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002713 return 0;
2714 default:
2715 return 1;
2716 }
2717}
Len Brown0f7887c2017-01-12 23:49:18 -05002718int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2719{
2720 if (has_slv_msrs(family, model))
2721 return 1;
2722
2723 return 0;
2724}
Len Brown6574a5d2012-09-21 00:01:31 -04002725int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2726{
2727 if (!genuine_intel)
2728 return 0;
2729
2730 if (family != 6)
2731 return 0;
2732
2733 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002734 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2735 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002736 return 1;
2737 default:
2738 return 0;
2739 }
2740}
Len Brownfcd17212015-03-23 20:29:09 -04002741int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2742{
2743 if (!genuine_intel)
2744 return 0;
2745
2746 if (family != 6)
2747 return 0;
2748
2749 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002750 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002751 return 1;
2752 default:
2753 return 0;
2754 }
2755}
2756
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002757int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2758{
2759 if (!genuine_intel)
2760 return 0;
2761
2762 if (family != 6)
2763 return 0;
2764
2765 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002766 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002767 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002768 return 1;
2769 default:
2770 return 0;
2771 }
2772}
Len Brown31e07522017-01-31 23:07:49 -05002773int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
2774{
2775 if (!genuine_intel)
2776 return 0;
2777
2778 if (family != 6)
2779 return 0;
2780
2781 switch (model) {
2782 case INTEL_FAM6_ATOM_GOLDMONT:
2783 case INTEL_FAM6_SKYLAKE_X:
2784 return 1;
2785 default:
2786 return 0;
2787 }
2788}
Len Brown6fb31432015-06-17 16:23:45 -04002789int has_config_tdp(unsigned int family, unsigned int model)
2790{
2791 if (!genuine_intel)
2792 return 0;
2793
2794 if (family != 6)
2795 return 0;
2796
2797 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002798 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2799 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2800 case INTEL_FAM6_HASWELL_X: /* HSX */
2801 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2802 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2803 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2804 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2805 case INTEL_FAM6_BROADWELL_X: /* BDX */
2806 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2807 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2808 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2809 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2810 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2811 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002812
Len Brown869ce692016-06-16 23:22:37 -04002813 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002814 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002815 return 1;
2816 default:
2817 return 0;
2818 }
2819}
2820
Len Brownfcd17212015-03-23 20:29:09 -04002821static void
Colin Ian King1b693172016-03-02 13:50:25 +00002822dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002823{
2824 if (!do_nhm_platform_info)
2825 return;
2826
2827 dump_nhm_platform_info();
2828
2829 if (has_hsw_turbo_ratio_limit(family, model))
2830 dump_hsw_turbo_ratio_limits();
2831
2832 if (has_ivt_turbo_ratio_limit(family, model))
2833 dump_ivt_turbo_ratio_limits();
2834
Len Brown31e07522017-01-31 23:07:49 -05002835 if (has_turbo_ratio_limit(family, model))
2836 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04002837
Len Brown0f7887c2017-01-12 23:49:18 -05002838 if (has_atom_turbo_ratio_limit(family, model))
2839 dump_atom_turbo_ratio_limits();
2840
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002841 if (has_knl_turbo_ratio_limit(family, model))
2842 dump_knl_turbo_ratio_limits();
2843
Len Brown6fb31432015-06-17 16:23:45 -04002844 if (has_config_tdp(family, model))
2845 dump_config_tdp();
2846
Len Brownfcd17212015-03-23 20:29:09 -04002847 dump_nhm_cst_cfg();
2848}
2849
Len Brown41618e62017-02-09 18:25:22 -05002850static void
2851dump_sysfs_cstate_config(void)
2852{
2853 char path[64];
2854 char name_buf[16];
2855 char desc[64];
2856 FILE *input;
2857 int state;
2858 char *sp;
2859
2860 if (!DO_BIC(BIC_sysfs))
2861 return;
2862
2863 for (state = 0; state < 10; ++state) {
2864
2865 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
2866 base_cpu, state);
2867 input = fopen(path, "r");
2868 if (input == NULL)
2869 continue;
2870 fgets(name_buf, sizeof(name_buf), input);
2871
2872 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
2873 sp = strchr(name_buf, '-');
2874 if (!sp)
2875 sp = strchrnul(name_buf, '\n');
2876 *sp = '\0';
2877
2878 fclose(input);
2879
2880 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
2881 base_cpu, state);
2882 input = fopen(path, "r");
2883 if (input == NULL)
2884 continue;
2885 fgets(desc, sizeof(desc), input);
2886
2887 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
2888 fclose(input);
2889 }
2890}
2891
Len Brown6574a5d2012-09-21 00:01:31 -04002892
Len Brown889facb2012-11-08 00:48:57 -05002893/*
2894 * print_epb()
2895 * Decode the ENERGY_PERF_BIAS MSR
2896 */
2897int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2898{
2899 unsigned long long msr;
2900 char *epb_string;
2901 int cpu;
2902
2903 if (!has_epb)
2904 return 0;
2905
2906 cpu = t->cpu_id;
2907
2908 /* EPB is per-package */
2909 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2910 return 0;
2911
2912 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002913 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002914 return -1;
2915 }
2916
2917 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2918 return 0;
2919
Len Browne9be7dd2015-05-26 12:19:37 -04002920 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002921 case ENERGY_PERF_BIAS_PERFORMANCE:
2922 epb_string = "performance";
2923 break;
2924 case ENERGY_PERF_BIAS_NORMAL:
2925 epb_string = "balanced";
2926 break;
2927 case ENERGY_PERF_BIAS_POWERSAVE:
2928 epb_string = "powersave";
2929 break;
2930 default:
2931 epb_string = "custom";
2932 break;
2933 }
Len Brownb7d8c142016-02-13 23:36:17 -05002934 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002935
2936 return 0;
2937}
Len Brown7f5c2582015-12-01 01:36:39 -05002938/*
2939 * print_hwp()
2940 * Decode the MSR_HWP_CAPABILITIES
2941 */
2942int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2943{
2944 unsigned long long msr;
2945 int cpu;
2946
2947 if (!has_hwp)
2948 return 0;
2949
2950 cpu = t->cpu_id;
2951
2952 /* MSR_HWP_CAPABILITIES is per-package */
2953 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2954 return 0;
2955
2956 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002957 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002958 return -1;
2959 }
2960
2961 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2962 return 0;
2963
Len Brownb7d8c142016-02-13 23:36:17 -05002964 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002965 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2966
2967 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2968 if ((msr & (1 << 0)) == 0)
2969 return 0;
2970
2971 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2972 return 0;
2973
Len Brownb7d8c142016-02-13 23:36:17 -05002974 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002975 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2976 cpu, msr,
2977 (unsigned int)HWP_HIGHEST_PERF(msr),
2978 (unsigned int)HWP_GUARANTEED_PERF(msr),
2979 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2980 (unsigned int)HWP_LOWEST_PERF(msr));
2981
2982 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2983 return 0;
2984
Len Brownb7d8c142016-02-13 23:36:17 -05002985 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002986 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2987 cpu, msr,
2988 (unsigned int)(((msr) >> 0) & 0xff),
2989 (unsigned int)(((msr) >> 8) & 0xff),
2990 (unsigned int)(((msr) >> 16) & 0xff),
2991 (unsigned int)(((msr) >> 24) & 0xff),
2992 (unsigned int)(((msr) >> 32) & 0xff3),
2993 (unsigned int)(((msr) >> 42) & 0x1));
2994
2995 if (has_hwp_pkg) {
2996 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2997 return 0;
2998
Len Brownb7d8c142016-02-13 23:36:17 -05002999 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003000 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
3001 cpu, msr,
3002 (unsigned int)(((msr) >> 0) & 0xff),
3003 (unsigned int)(((msr) >> 8) & 0xff),
3004 (unsigned int)(((msr) >> 16) & 0xff),
3005 (unsigned int)(((msr) >> 24) & 0xff),
3006 (unsigned int)(((msr) >> 32) & 0xff3));
3007 }
3008 if (has_hwp_notify) {
3009 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3010 return 0;
3011
Len Brownb7d8c142016-02-13 23:36:17 -05003012 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003013 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3014 cpu, msr,
3015 ((msr) & 0x1) ? "EN" : "Dis",
3016 ((msr) & 0x2) ? "EN" : "Dis");
3017 }
3018 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3019 return 0;
3020
Len Brownb7d8c142016-02-13 23:36:17 -05003021 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003022 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3023 cpu, msr,
3024 ((msr) & 0x1) ? "" : "No-",
3025 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003026
3027 return 0;
3028}
3029
Len Brown3a9a9412014-08-15 02:39:52 -04003030/*
3031 * print_perf_limit()
3032 */
3033int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3034{
3035 unsigned long long msr;
3036 int cpu;
3037
3038 cpu = t->cpu_id;
3039
3040 /* per-package */
3041 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3042 return 0;
3043
3044 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003045 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003046 return -1;
3047 }
3048
3049 if (do_core_perf_limit_reasons) {
3050 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003051 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3052 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003053 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003054 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003055 (msr & 1 << 13) ? "Transitions, " : "",
3056 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3057 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3058 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3059 (msr & 1 << 9) ? "CorePwr, " : "",
3060 (msr & 1 << 8) ? "Amps, " : "",
3061 (msr & 1 << 6) ? "VR-Therm, " : "",
3062 (msr & 1 << 5) ? "Auto-HWP, " : "",
3063 (msr & 1 << 4) ? "Graphics, " : "",
3064 (msr & 1 << 2) ? "bit2, " : "",
3065 (msr & 1 << 1) ? "ThermStatus, " : "",
3066 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003067 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 -04003068 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003069 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003070 (msr & 1 << 29) ? "Transitions, " : "",
3071 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3072 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3073 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3074 (msr & 1 << 25) ? "CorePwr, " : "",
3075 (msr & 1 << 24) ? "Amps, " : "",
3076 (msr & 1 << 22) ? "VR-Therm, " : "",
3077 (msr & 1 << 21) ? "Auto-HWP, " : "",
3078 (msr & 1 << 20) ? "Graphics, " : "",
3079 (msr & 1 << 18) ? "bit18, " : "",
3080 (msr & 1 << 17) ? "ThermStatus, " : "",
3081 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003082
3083 }
3084 if (do_gfx_perf_limit_reasons) {
3085 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003086 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3087 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003088 (msr & 1 << 0) ? "PROCHOT, " : "",
3089 (msr & 1 << 1) ? "ThermStatus, " : "",
3090 (msr & 1 << 4) ? "Graphics, " : "",
3091 (msr & 1 << 6) ? "VR-Therm, " : "",
3092 (msr & 1 << 8) ? "Amps, " : "",
3093 (msr & 1 << 9) ? "GFXPwr, " : "",
3094 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3095 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003096 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003097 (msr & 1 << 16) ? "PROCHOT, " : "",
3098 (msr & 1 << 17) ? "ThermStatus, " : "",
3099 (msr & 1 << 20) ? "Graphics, " : "",
3100 (msr & 1 << 22) ? "VR-Therm, " : "",
3101 (msr & 1 << 24) ? "Amps, " : "",
3102 (msr & 1 << 25) ? "GFXPwr, " : "",
3103 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3104 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3105 }
3106 if (do_ring_perf_limit_reasons) {
3107 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003108 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3109 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003110 (msr & 1 << 0) ? "PROCHOT, " : "",
3111 (msr & 1 << 1) ? "ThermStatus, " : "",
3112 (msr & 1 << 6) ? "VR-Therm, " : "",
3113 (msr & 1 << 8) ? "Amps, " : "",
3114 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3115 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003116 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003117 (msr & 1 << 16) ? "PROCHOT, " : "",
3118 (msr & 1 << 17) ? "ThermStatus, " : "",
3119 (msr & 1 << 22) ? "VR-Therm, " : "",
3120 (msr & 1 << 24) ? "Amps, " : "",
3121 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3122 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3123 }
3124 return 0;
3125}
3126
Len Brown889facb2012-11-08 00:48:57 -05003127#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3128#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3129
Colin Ian King1b693172016-03-02 13:50:25 +00003130double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003131{
3132 unsigned long long msr;
3133
3134 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003135 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003136 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3137
3138 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003139 case INTEL_FAM6_ATOM_SILVERMONT1:
3140 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003141 return 30.0;
3142 default:
3143 return 135.0;
3144 }
3145}
3146
Andrey Semin40ee8e32014-12-05 00:07:00 -05003147/*
3148 * rapl_dram_energy_units_probe()
3149 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3150 */
3151static double
3152rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3153{
3154 /* only called for genuine_intel, family 6 */
3155
3156 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003157 case INTEL_FAM6_HASWELL_X: /* HSX */
3158 case INTEL_FAM6_BROADWELL_X: /* BDX */
3159 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3160 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003161 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003162 return (rapl_dram_energy_units = 15.3 / 1000000);
3163 default:
3164 return (rapl_energy_units);
3165 }
3166}
3167
Len Brown144b44b2013-11-09 00:30:16 -05003168
Len Brown889facb2012-11-08 00:48:57 -05003169/*
3170 * rapl_probe()
3171 *
Len Brown144b44b2013-11-09 00:30:16 -05003172 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003173 */
3174void rapl_probe(unsigned int family, unsigned int model)
3175{
3176 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003177 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003178 double tdp;
3179
3180 if (!genuine_intel)
3181 return;
3182
3183 if (family != 6)
3184 return;
3185
3186 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003187 case INTEL_FAM6_SANDYBRIDGE:
3188 case INTEL_FAM6_IVYBRIDGE:
3189 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3190 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3191 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3192 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3193 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003194 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003195 if (rapl_joules) {
3196 BIC_PRESENT(BIC_Pkg_J);
3197 BIC_PRESENT(BIC_Cor_J);
3198 BIC_PRESENT(BIC_GFX_J);
3199 } else {
3200 BIC_PRESENT(BIC_PkgWatt);
3201 BIC_PRESENT(BIC_CorWatt);
3202 BIC_PRESENT(BIC_GFXWatt);
3203 }
Len Brown889facb2012-11-08 00:48:57 -05003204 break;
Len Brown869ce692016-06-16 23:22:37 -04003205 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003206 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003207 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003208 if (rapl_joules)
3209 BIC_PRESENT(BIC_Pkg_J);
3210 else
3211 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003212 break;
Len Brown869ce692016-06-16 23:22:37 -04003213 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3214 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3215 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3216 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003217 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 -05003218 BIC_PRESENT(BIC_PKG__);
3219 BIC_PRESENT(BIC_RAM__);
3220 if (rapl_joules) {
3221 BIC_PRESENT(BIC_Pkg_J);
3222 BIC_PRESENT(BIC_Cor_J);
3223 BIC_PRESENT(BIC_RAM_J);
3224 } else {
3225 BIC_PRESENT(BIC_PkgWatt);
3226 BIC_PRESENT(BIC_CorWatt);
3227 BIC_PRESENT(BIC_RAMWatt);
3228 }
Len Brown0b2bb692015-03-26 00:50:30 -04003229 break;
Len Brown869ce692016-06-16 23:22:37 -04003230 case INTEL_FAM6_HASWELL_X: /* HSX */
3231 case INTEL_FAM6_BROADWELL_X: /* BDX */
3232 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3233 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3234 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003235 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003236 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 -05003237 BIC_PRESENT(BIC_PKG__);
3238 BIC_PRESENT(BIC_RAM__);
3239 if (rapl_joules) {
3240 BIC_PRESENT(BIC_Pkg_J);
3241 BIC_PRESENT(BIC_RAM_J);
3242 } else {
3243 BIC_PRESENT(BIC_PkgWatt);
3244 BIC_PRESENT(BIC_RAMWatt);
3245 }
Len Browne6f9bb32013-12-03 02:19:19 -05003246 break;
Len Brown869ce692016-06-16 23:22:37 -04003247 case INTEL_FAM6_SANDYBRIDGE_X:
3248 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003249 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 -05003250 BIC_PRESENT(BIC_PKG__);
3251 BIC_PRESENT(BIC_RAM__);
3252 if (rapl_joules) {
3253 BIC_PRESENT(BIC_Pkg_J);
3254 BIC_PRESENT(BIC_Cor_J);
3255 BIC_PRESENT(BIC_RAM_J);
3256 } else {
3257 BIC_PRESENT(BIC_PkgWatt);
3258 BIC_PRESENT(BIC_CorWatt);
3259 BIC_PRESENT(BIC_RAMWatt);
3260 }
Len Brown144b44b2013-11-09 00:30:16 -05003261 break;
Len Brown869ce692016-06-16 23:22:37 -04003262 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3263 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003264 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003265 if (rapl_joules) {
3266 BIC_PRESENT(BIC_Pkg_J);
3267 BIC_PRESENT(BIC_Cor_J);
3268 } else {
3269 BIC_PRESENT(BIC_PkgWatt);
3270 BIC_PRESENT(BIC_CorWatt);
3271 }
Len Brown889facb2012-11-08 00:48:57 -05003272 break;
Len Brown869ce692016-06-16 23:22:37 -04003273 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003274 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 -05003275 BIC_PRESENT(BIC_PKG__);
3276 BIC_PRESENT(BIC_RAM__);
3277 if (rapl_joules) {
3278 BIC_PRESENT(BIC_Pkg_J);
3279 BIC_PRESENT(BIC_Cor_J);
3280 BIC_PRESENT(BIC_RAM_J);
3281 } else {
3282 BIC_PRESENT(BIC_PkgWatt);
3283 BIC_PRESENT(BIC_CorWatt);
3284 BIC_PRESENT(BIC_RAMWatt);
3285 }
Jacob Pan0f644902016-06-16 09:48:22 -07003286 break;
Len Brown889facb2012-11-08 00:48:57 -05003287 default:
3288 return;
3289 }
3290
3291 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003292 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003293 return;
3294
3295 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003296 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003297 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3298 else
3299 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003300
Andrey Semin40ee8e32014-12-05 00:07:00 -05003301 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3302
Len Brown144b44b2013-11-09 00:30:16 -05003303 time_unit = msr >> 16 & 0xF;
3304 if (time_unit == 0)
3305 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003306
Len Brown144b44b2013-11-09 00:30:16 -05003307 rapl_time_units = 1.0 / (1 << (time_unit));
3308
3309 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003310
3311 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003312 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003313 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003314
3315 return;
3316}
3317
Colin Ian King1b693172016-03-02 13:50:25 +00003318void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003319{
3320 if (!genuine_intel)
3321 return;
3322
3323 if (family != 6)
3324 return;
3325
3326 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003327 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3328 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3329 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003330 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003331 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003332 do_core_perf_limit_reasons = 1;
3333 do_ring_perf_limit_reasons = 1;
3334 default:
3335 return;
3336 }
3337}
3338
Len Brown889facb2012-11-08 00:48:57 -05003339int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3340{
3341 unsigned long long msr;
3342 unsigned int dts;
3343 int cpu;
3344
3345 if (!(do_dts || do_ptm))
3346 return 0;
3347
3348 cpu = t->cpu_id;
3349
3350 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003351 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003352 return 0;
3353
3354 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003355 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003356 return -1;
3357 }
3358
3359 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3360 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3361 return 0;
3362
3363 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003364 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003365 cpu, msr, tcc_activation_temp - dts);
3366
3367#ifdef THERM_DEBUG
3368 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3369 return 0;
3370
3371 dts = (msr >> 16) & 0x7F;
3372 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003373 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003374 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3375#endif
3376 }
3377
3378
3379 if (do_dts) {
3380 unsigned int resolution;
3381
3382 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3383 return 0;
3384
3385 dts = (msr >> 16) & 0x7F;
3386 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003387 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003388 cpu, msr, tcc_activation_temp - dts, resolution);
3389
3390#ifdef THERM_DEBUG
3391 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3392 return 0;
3393
3394 dts = (msr >> 16) & 0x7F;
3395 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003396 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003397 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3398#endif
3399 }
3400
3401 return 0;
3402}
Len Brown36229892016-02-26 20:51:02 -05003403
Len Brown889facb2012-11-08 00:48:57 -05003404void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3405{
Len Brownb7d8c142016-02-13 23:36:17 -05003406 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003407 cpu, label,
3408 ((msr >> 15) & 1) ? "EN" : "DIS",
3409 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3410 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3411 (((msr >> 16) & 1) ? "EN" : "DIS"));
3412
3413 return;
3414}
3415
3416int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3417{
3418 unsigned long long msr;
3419 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003420
3421 if (!do_rapl)
3422 return 0;
3423
3424 /* RAPL counters are per package, so print only for 1st thread/package */
3425 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3426 return 0;
3427
3428 cpu = t->cpu_id;
3429 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003430 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003431 return -1;
3432 }
3433
3434 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3435 return -1;
3436
Len Brown96e47152017-01-21 02:26:00 -05003437 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3438 rapl_power_units, rapl_energy_units, rapl_time_units);
3439
Len Brown144b44b2013-11-09 00:30:16 -05003440 if (do_rapl & RAPL_PKG_POWER_INFO) {
3441
Len Brown889facb2012-11-08 00:48:57 -05003442 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3443 return -5;
3444
3445
Len Brownb7d8c142016-02-13 23:36:17 -05003446 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 -05003447 cpu, msr,
3448 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3449 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3450 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3451 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3452
Len Brown144b44b2013-11-09 00:30:16 -05003453 }
3454 if (do_rapl & RAPL_PKG) {
3455
Len Brown889facb2012-11-08 00:48:57 -05003456 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3457 return -9;
3458
Len Brownb7d8c142016-02-13 23:36:17 -05003459 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003460 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003461
3462 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003463 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003464 cpu,
3465 ((msr >> 47) & 1) ? "EN" : "DIS",
3466 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3467 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3468 ((msr >> 48) & 1) ? "EN" : "DIS");
3469 }
3470
Len Brown0b2bb692015-03-26 00:50:30 -04003471 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003472 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3473 return -6;
3474
Len Brownb7d8c142016-02-13 23:36:17 -05003475 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 -05003476 cpu, msr,
3477 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3478 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3479 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3480 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003481 }
3482 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003483 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3484 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003485 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003486 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003487
3488 print_power_limit_msr(cpu, msr, "DRAM Limit");
3489 }
Len Brown144b44b2013-11-09 00:30:16 -05003490 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003491 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3492 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003493
Len Brown96e47152017-01-21 02:26:00 -05003494 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003495 }
Jacob Pan91484942016-06-16 09:48:20 -07003496 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003497 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3498 return -9;
3499 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3500 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3501 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003502 }
3503 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003504 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3505 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003506
Len Brown96e47152017-01-21 02:26:00 -05003507 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003508
Len Brown96e47152017-01-21 02:26:00 -05003509 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3510 return -9;
3511 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3512 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3513 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003514 }
3515 return 0;
3516}
3517
Len Brownd7899442015-01-23 00:12:33 -05003518/*
3519 * SNB adds support for additional MSRs:
3520 *
3521 * MSR_PKG_C7_RESIDENCY 0x000003fa
3522 * MSR_CORE_C7_RESIDENCY 0x000003fe
3523 * MSR_PKG_C2_RESIDENCY 0x0000060d
3524 */
Len Brown103a8fe2010-10-22 23:53:03 -04003525
Len Brownd7899442015-01-23 00:12:33 -05003526int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003527{
3528 if (!genuine_intel)
3529 return 0;
3530
3531 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003532 case INTEL_FAM6_SANDYBRIDGE:
3533 case INTEL_FAM6_SANDYBRIDGE_X:
3534 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3535 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3536 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3537 case INTEL_FAM6_HASWELL_X: /* HSW */
3538 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3539 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3540 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3541 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3542 case INTEL_FAM6_BROADWELL_X: /* BDX */
3543 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3544 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3545 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3546 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3547 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3548 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3549 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003550 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003551 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003552 return 1;
3553 }
3554 return 0;
3555}
3556
Len Brownd7899442015-01-23 00:12:33 -05003557/*
3558 * HSW adds support for additional MSRs:
3559 *
Len Brown5a634262016-04-06 17:15:55 -04003560 * MSR_PKG_C8_RESIDENCY 0x00000630
3561 * MSR_PKG_C9_RESIDENCY 0x00000631
3562 * MSR_PKG_C10_RESIDENCY 0x00000632
3563 *
3564 * MSR_PKGC8_IRTL 0x00000633
3565 * MSR_PKGC9_IRTL 0x00000634
3566 * MSR_PKGC10_IRTL 0x00000635
3567 *
Len Brownd7899442015-01-23 00:12:33 -05003568 */
3569int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003570{
3571 if (!genuine_intel)
3572 return 0;
3573
3574 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003575 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3576 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3577 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3578 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3579 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3580 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3581 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003582 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003583 return 1;
3584 }
3585 return 0;
3586}
3587
Len Brown0b2bb692015-03-26 00:50:30 -04003588/*
3589 * SKL adds support for additional MSRS:
3590 *
3591 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3592 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3593 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3594 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3595 */
3596int has_skl_msrs(unsigned int family, unsigned int model)
3597{
3598 if (!genuine_intel)
3599 return 0;
3600
3601 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003602 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3603 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3604 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3605 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003606 return 1;
3607 }
3608 return 0;
3609}
3610
Len Brown144b44b2013-11-09 00:30:16 -05003611int is_slm(unsigned int family, unsigned int model)
3612{
3613 if (!genuine_intel)
3614 return 0;
3615 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003616 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3617 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003618 return 1;
3619 }
3620 return 0;
3621}
3622
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003623int is_knl(unsigned int family, unsigned int model)
3624{
3625 if (!genuine_intel)
3626 return 0;
3627 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003628 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003629 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003630 return 1;
3631 }
3632 return 0;
3633}
3634
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003635unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3636{
3637 if (is_knl(family, model))
3638 return 1024;
3639 return 1;
3640}
3641
Len Brown144b44b2013-11-09 00:30:16 -05003642#define SLM_BCLK_FREQS 5
3643double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3644
3645double slm_bclk(void)
3646{
3647 unsigned long long msr = 3;
3648 unsigned int i;
3649 double freq;
3650
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003651 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003652 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003653
3654 i = msr & 0xf;
3655 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003656 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003657 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003658 }
3659 freq = slm_freq_table[i];
3660
Len Brown96e47152017-01-21 02:26:00 -05003661 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003662 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003663
3664 return freq;
3665}
3666
Len Brown103a8fe2010-10-22 23:53:03 -04003667double discover_bclk(unsigned int family, unsigned int model)
3668{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003669 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003670 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003671 else if (is_slm(family, model))
3672 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003673 else
3674 return 133.33;
3675}
3676
Len Brown889facb2012-11-08 00:48:57 -05003677/*
3678 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3679 * the Thermal Control Circuit (TCC) activates.
3680 * This is usually equal to tjMax.
3681 *
3682 * Older processors do not have this MSR, so there we guess,
3683 * but also allow cmdline over-ride with -T.
3684 *
3685 * Several MSR temperature values are in units of degrees-C
3686 * below this value, including the Digital Thermal Sensor (DTS),
3687 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3688 */
3689int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3690{
3691 unsigned long long msr;
3692 unsigned int target_c_local;
3693 int cpu;
3694
3695 /* tcc_activation_temp is used only for dts or ptm */
3696 if (!(do_dts || do_ptm))
3697 return 0;
3698
3699 /* this is a per-package concept */
3700 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3701 return 0;
3702
3703 cpu = t->cpu_id;
3704 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003705 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003706 return -1;
3707 }
3708
3709 if (tcc_activation_temp_override != 0) {
3710 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003711 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003712 cpu, tcc_activation_temp);
3713 return 0;
3714 }
3715
3716 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003717 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003718 goto guess;
3719
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003720 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003721 goto guess;
3722
Jean Delvare34821242014-05-01 11:40:19 +02003723 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003724
Len Brown96e47152017-01-21 02:26:00 -05003725 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003726 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003727 cpu, msr, target_c_local);
3728
Jean Delvare34821242014-05-01 11:40:19 +02003729 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003730 goto guess;
3731
3732 tcc_activation_temp = target_c_local;
3733
3734 return 0;
3735
3736guess:
3737 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003738 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003739 cpu, tcc_activation_temp);
3740
3741 return 0;
3742}
Len Brown69807a62015-11-21 12:22:47 -05003743
Len Brownaa8d8cc2016-03-11 13:26:03 -05003744void decode_feature_control_msr(void)
3745{
3746 unsigned long long msr;
3747
3748 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3749 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3750 base_cpu, msr,
3751 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3752 msr & (1 << 18) ? "SGX" : "");
3753}
3754
Len Brown69807a62015-11-21 12:22:47 -05003755void decode_misc_enable_msr(void)
3756{
3757 unsigned long long msr;
3758
3759 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003760 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003761 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003762 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3763 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3764 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3765 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3766 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003767}
3768
Len Brown33148d62017-01-21 01:26:16 -05003769void decode_misc_feature_control(void)
3770{
3771 unsigned long long msr;
3772
3773 if (!has_misc_feature_control)
3774 return;
3775
3776 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
3777 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
3778 base_cpu, msr,
3779 msr & (0 << 0) ? "No-" : "",
3780 msr & (1 << 0) ? "No-" : "",
3781 msr & (2 << 0) ? "No-" : "",
3782 msr & (3 << 0) ? "No-" : "");
3783}
Len Brownf0057312015-12-03 01:35:36 -05003784/*
3785 * Decode MSR_MISC_PWR_MGMT
3786 *
3787 * Decode the bits according to the Nehalem documentation
3788 * bit[0] seems to continue to have same meaning going forward
3789 * bit[1] less so...
3790 */
3791void decode_misc_pwr_mgmt_msr(void)
3792{
3793 unsigned long long msr;
3794
3795 if (!do_nhm_platform_info)
3796 return;
3797
Len Browncf4cbe52017-01-01 13:08:33 -05003798 if (no_MSR_MISC_PWR_MGMT)
3799 return;
3800
Len Brownf0057312015-12-03 01:35:36 -05003801 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003802 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 -05003803 base_cpu, msr,
3804 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003805 msr & (1 << 1) ? "EN" : "DIS",
3806 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003807}
Len Brown71616c82017-01-07 22:37:48 -05003808/*
3809 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3810 *
3811 * This MSRs are present on Silvermont processors,
3812 * Intel Atom processor E3000 series (Baytrail), and friends.
3813 */
3814void decode_c6_demotion_policy_msr(void)
3815{
3816 unsigned long long msr;
3817
3818 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3819 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3820 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3821
3822 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3823 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3824 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3825}
Len Brown7f5c2582015-12-01 01:36:39 -05003826
Len Brownfcd17212015-03-23 20:29:09 -04003827void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003828{
Len Brown61a87ba2015-11-23 02:30:51 -05003829 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003830 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003831 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003832
3833 eax = ebx = ecx = edx = 0;
3834
Len Brown5aea2f72016-03-13 03:14:35 -04003835 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003836
3837 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3838 genuine_intel = 1;
3839
Len Brown96e47152017-01-21 02:26:00 -05003840 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003841 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003842 (char *)&ebx, (char *)&edx, (char *)&ecx);
3843
Len Brown5aea2f72016-03-13 03:14:35 -04003844 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003845 family = (fms >> 8) & 0xf;
3846 model = (fms >> 4) & 0xf;
3847 stepping = fms & 0xf;
3848 if (family == 6 || family == 0xf)
3849 model += ((fms >> 16) & 0xf) << 4;
3850
Len Brown96e47152017-01-21 02:26:00 -05003851 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05003852 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003853 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003854 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003855 ecx & (1 << 0) ? "SSE3" : "-",
3856 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003857 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003858 ecx & (1 << 7) ? "EIST" : "-",
3859 ecx & (1 << 8) ? "TM2" : "-",
3860 edx & (1 << 4) ? "TSC" : "-",
3861 edx & (1 << 5) ? "MSR" : "-",
3862 edx & (1 << 22) ? "ACPI-TM" : "-",
3863 edx & (1 << 29) ? "TM" : "-");
3864 }
Len Brown103a8fe2010-10-22 23:53:03 -04003865
Josh Triplettb2c95d92013-08-20 17:20:18 -07003866 if (!(edx & (1 << 5)))
3867 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003868
3869 /*
3870 * check max extended function levels of CPUID.
3871 * This is needed to check for invariant TSC.
3872 * This check is valid for both Intel and AMD.
3873 */
3874 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003875 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003876
Len Brown61a87ba2015-11-23 02:30:51 -05003877 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003878
Len Brownd7899442015-01-23 00:12:33 -05003879 /*
3880 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3881 * this check is valid for both Intel and AMD
3882 */
Len Brown5aea2f72016-03-13 03:14:35 -04003883 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003884 has_invariant_tsc = edx & (1 << 8);
3885 }
Len Brown103a8fe2010-10-22 23:53:03 -04003886
3887 /*
3888 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3889 * this check is valid for both Intel and AMD
3890 */
3891
Len Brown5aea2f72016-03-13 03:14:35 -04003892 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003893 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003894 if (has_aperf) {
3895 BIC_PRESENT(BIC_Avg_MHz);
3896 BIC_PRESENT(BIC_Busy);
3897 BIC_PRESENT(BIC_Bzy_MHz);
3898 }
Len Brown889facb2012-11-08 00:48:57 -05003899 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003900 if (do_dts)
3901 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05003902 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05003903 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003904 if (do_ptm)
3905 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003906 has_hwp = eax & (1 << 7);
3907 has_hwp_notify = eax & (1 << 8);
3908 has_hwp_activity_window = eax & (1 << 9);
3909 has_hwp_epp = eax & (1 << 10);
3910 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003911 has_epb = ecx & (1 << 3);
3912
Len Brown96e47152017-01-21 02:26:00 -05003913 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05003914 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003915 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3916 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05003917 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05003918 do_dts ? "" : "No-",
3919 do_ptm ? "" : "No-",
3920 has_hwp ? "" : "No-",
3921 has_hwp_notify ? "" : "No-",
3922 has_hwp_activity_window ? "" : "No-",
3923 has_hwp_epp ? "" : "No-",
3924 has_hwp_pkg ? "" : "No-",
3925 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003926
Len Brown96e47152017-01-21 02:26:00 -05003927 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05003928 decode_misc_enable_msr();
3929
Len Brown33148d62017-01-21 01:26:16 -05003930
Len Brown96e47152017-01-21 02:26:00 -05003931 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003932 int has_sgx;
3933
3934 ecx = 0;
3935
3936 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3937
3938 has_sgx = ebx & (1 << 2);
3939 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3940
3941 if (has_sgx)
3942 decode_feature_control_msr();
3943 }
3944
Len Brown61a87ba2015-11-23 02:30:51 -05003945 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003946 unsigned int eax_crystal;
3947 unsigned int ebx_tsc;
3948
3949 /*
3950 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3951 */
3952 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003953 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003954
3955 if (ebx_tsc != 0) {
3956
Len Brown96e47152017-01-21 02:26:00 -05003957 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003958 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003959 eax_crystal, ebx_tsc, crystal_hz);
3960
3961 if (crystal_hz == 0)
3962 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04003963 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3964 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3965 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3966 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003967 crystal_hz = 24000000; /* 24.0 MHz */
3968 break;
Len Brown869ce692016-06-16 23:22:37 -04003969 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003970 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003971 crystal_hz = 25000000; /* 25.0 MHz */
3972 break;
Len Brown869ce692016-06-16 23:22:37 -04003973 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003974 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04003975 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003976 break;
3977 default:
3978 crystal_hz = 0;
3979 }
3980
3981 if (crystal_hz) {
3982 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05003983 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003984 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003985 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3986 }
3987 }
3988 }
Len Brown61a87ba2015-11-23 02:30:51 -05003989 if (max_level >= 0x16) {
3990 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3991
3992 /*
3993 * CPUID 16H Base MHz, Max MHz, Bus MHz
3994 */
3995 base_mhz = max_mhz = bus_mhz = edx = 0;
3996
Len Brown5aea2f72016-03-13 03:14:35 -04003997 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05003998 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003999 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004000 base_mhz, max_mhz, bus_mhz);
4001 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004002
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004003 if (has_aperf)
4004 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4005
Len Brown812db3f2017-02-10 00:25:41 -05004006 BIC_PRESENT(BIC_IRQ);
4007 BIC_PRESENT(BIC_TSC_MHz);
4008
4009 if (probe_nhm_msrs(family, model)) {
4010 do_nhm_platform_info = 1;
4011 BIC_PRESENT(BIC_CPU_c1);
4012 BIC_PRESENT(BIC_CPU_c3);
4013 BIC_PRESENT(BIC_CPU_c6);
4014 BIC_PRESENT(BIC_SMI);
4015 }
Len Brownd7899442015-01-23 00:12:33 -05004016 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004017
4018 if (do_snb_cstates)
4019 BIC_PRESENT(BIC_CPU_c7);
4020
Len Brown5a634262016-04-06 17:15:55 -04004021 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004022 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4023 BIC_PRESENT(BIC_Pkgpc2);
4024 if (pkg_cstate_limit >= PCL__3)
4025 BIC_PRESENT(BIC_Pkgpc3);
4026 if (pkg_cstate_limit >= PCL__6)
4027 BIC_PRESENT(BIC_Pkgpc6);
4028 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4029 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004030 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004031 BIC_NOT_PRESENT(BIC_Pkgpc2);
4032 BIC_NOT_PRESENT(BIC_Pkgpc3);
4033 BIC_PRESENT(BIC_Pkgpc6);
4034 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004035 BIC_PRESENT(BIC_Mod_c6);
4036 use_c1_residency_msr = 1;
4037 }
Len Brown7170a372017-01-27 02:13:27 -05004038 if (is_dnv(family, model)) {
4039 BIC_PRESENT(BIC_CPU_c1);
4040 BIC_NOT_PRESENT(BIC_CPU_c3);
4041 BIC_NOT_PRESENT(BIC_Pkgpc3);
4042 BIC_NOT_PRESENT(BIC_CPU_c7);
4043 BIC_NOT_PRESENT(BIC_Pkgpc7);
4044 use_c1_residency_msr = 1;
4045 }
Len Brown34c761972017-01-27 02:36:41 -05004046 if (is_skx(family, model)) {
4047 BIC_NOT_PRESENT(BIC_CPU_c3);
4048 BIC_NOT_PRESENT(BIC_Pkgpc3);
4049 BIC_NOT_PRESENT(BIC_CPU_c7);
4050 BIC_NOT_PRESENT(BIC_Pkgpc7);
4051 }
Len Brownade0eba2017-02-10 01:56:47 -05004052 if (is_bdx(family, model)) {
4053 BIC_NOT_PRESENT(BIC_CPU_c7);
4054 BIC_NOT_PRESENT(BIC_Pkgpc7);
4055 }
Len Brown0f47c082017-01-27 00:50:45 -05004056 if (has_hsw_msrs(family, model)) {
4057 BIC_PRESENT(BIC_Pkgpc8);
4058 BIC_PRESENT(BIC_Pkgpc9);
4059 BIC_PRESENT(BIC_Pkgpc10);
4060 }
Len Brown5a634262016-04-06 17:15:55 -04004061 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04004062 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05004063 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004064 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004065
Len Brown96e47152017-01-21 02:26:00 -05004066 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004067 decode_misc_pwr_mgmt_msr();
4068
Len Brown96e47152017-01-21 02:26:00 -05004069 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004070 decode_c6_demotion_policy_msr();
4071
Len Brown889facb2012-11-08 00:48:57 -05004072 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004073 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004074
Len Brown96e47152017-01-21 02:26:00 -05004075 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004076 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004077
Len Brown41618e62017-02-09 18:25:22 -05004078 if (!quiet)
4079 dump_sysfs_cstate_config();
4080
Len Browna2b7b742015-09-26 00:12:38 -04004081 if (has_skl_msrs(family, model))
4082 calculate_tsc_tweak();
4083
Len Brown812db3f2017-02-10 00:25:41 -05004084 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4085 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004086
Len Brown812db3f2017-02-10 00:25:41 -05004087 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4088 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004089
Len Brown96e47152017-01-21 02:26:00 -05004090 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004091 decode_misc_feature_control();
4092
Len Brown889facb2012-11-08 00:48:57 -05004093 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004094}
4095
Len Brownd8af6f52015-02-10 01:56:38 -05004096void help()
Len Brown103a8fe2010-10-22 23:53:03 -04004097{
Len Brownb7d8c142016-02-13 23:36:17 -05004098 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05004099 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
4100 "\n"
4101 "Turbostat forks the specified COMMAND and prints statistics\n"
4102 "when COMMAND completes.\n"
4103 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
4104 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05004105 "--add add a counter\n"
4106 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brown1ef7d212017-02-10 23:54:15 -05004107 "--cpu cpu-set limit output to summary plus cpu-set cpu-set\n"
Len Brown96e47152017-01-21 02:26:00 -05004108 "--quiet skip decoding system configuration header\n"
Len Brownd8af6f52015-02-10 01:56:38 -05004109 "--interval sec Override default 5-second measurement interval\n"
4110 "--help print this help message\n"
Len Brownc8ade362017-02-15 17:15:11 -05004111 "--list list column headers only\n"
Len Brownb7d8c142016-02-13 23:36:17 -05004112 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05004113 "--version print version information\n"
4114 "\n"
4115 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04004116}
4117
4118
4119/*
4120 * in /dev/cpu/ return success for names that are numbers
4121 * ie. filter out ".", "..", "microcode".
4122 */
4123int dir_filter(const struct dirent *dirp)
4124{
4125 if (isdigit(dirp->d_name[0]))
4126 return 1;
4127 else
4128 return 0;
4129}
4130
4131int open_dev_cpu_msr(int dummy1)
4132{
4133 return 0;
4134}
4135
Len Brownc98d5d92012-06-04 00:56:40 -04004136void topology_probe()
4137{
4138 int i;
4139 int max_core_id = 0;
4140 int max_package_id = 0;
4141 int max_siblings = 0;
4142 struct cpu_topology {
4143 int core_id;
4144 int physical_package_id;
4145 } *cpus;
4146
4147 /* Initialize num_cpus, max_cpu_num */
4148 topo.num_cpus = 0;
4149 topo.max_cpu_num = 0;
4150 for_all_proc_cpus(count_cpus);
4151 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004152 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004153
Len Brownd8af6f52015-02-10 01:56:38 -05004154 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004155 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004156
4157 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004158 if (cpus == NULL)
4159 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004160
4161 /*
4162 * Allocate and initialize cpu_present_set
4163 */
4164 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004165 if (cpu_present_set == NULL)
4166 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004167 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4168 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4169 for_all_proc_cpus(mark_cpu_present);
4170
4171 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004172 * Validate that all cpus in cpu_subset are also in cpu_present_set
4173 */
4174 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4175 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4176 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4177 err(1, "cpu%d not present", i);
4178 }
4179
4180 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004181 * Allocate and initialize cpu_affinity_set
4182 */
4183 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004184 if (cpu_affinity_set == NULL)
4185 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004186 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4187 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4188
4189
4190 /*
4191 * For online cpus
4192 * find max_core_id, max_package_id
4193 */
4194 for (i = 0; i <= topo.max_cpu_num; ++i) {
4195 int siblings;
4196
4197 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004198 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004199 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004200 continue;
4201 }
4202 cpus[i].core_id = get_core_id(i);
4203 if (cpus[i].core_id > max_core_id)
4204 max_core_id = cpus[i].core_id;
4205
4206 cpus[i].physical_package_id = get_physical_package_id(i);
4207 if (cpus[i].physical_package_id > max_package_id)
4208 max_package_id = cpus[i].physical_package_id;
4209
4210 siblings = get_num_ht_siblings(i);
4211 if (siblings > max_siblings)
4212 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004213 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004214 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004215 i, cpus[i].physical_package_id, cpus[i].core_id);
4216 }
4217 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004218 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004219 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004220 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004221 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004222 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004223
4224 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004225 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004226 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004227 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05004228 if (debug && !summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004229 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004230
4231 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004232 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004233 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004234
4235 free(cpus);
4236}
4237
4238void
4239allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4240{
4241 int i;
4242
4243 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004244 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004245 if (*t == NULL)
4246 goto error;
4247
4248 for (i = 0; i < topo.num_threads_per_core *
4249 topo.num_cores_per_pkg * topo.num_packages; i++)
4250 (*t)[i].cpu_id = -1;
4251
4252 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004253 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004254 if (*c == NULL)
4255 goto error;
4256
4257 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4258 (*c)[i].core_id = -1;
4259
Len Brown678a3bd2017-02-09 22:22:13 -05004260 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004261 if (*p == NULL)
4262 goto error;
4263
4264 for (i = 0; i < topo.num_packages; i++)
4265 (*p)[i].package_id = i;
4266
4267 return;
4268error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004269 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004270}
4271/*
4272 * init_counter()
4273 *
4274 * set cpu_id, core_num, pkg_num
4275 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4276 *
4277 * increment topo.num_cores when 1st core in pkg seen
4278 */
4279void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4280 struct pkg_data *pkg_base, int thread_num, int core_num,
4281 int pkg_num, int cpu_id)
4282{
4283 struct thread_data *t;
4284 struct core_data *c;
4285 struct pkg_data *p;
4286
4287 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4288 c = GET_CORE(core_base, core_num, pkg_num);
4289 p = GET_PKG(pkg_base, pkg_num);
4290
4291 t->cpu_id = cpu_id;
4292 if (thread_num == 0) {
4293 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4294 if (cpu_is_first_core_in_package(cpu_id))
4295 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4296 }
4297
4298 c->core_id = core_num;
4299 p->package_id = pkg_num;
4300}
4301
4302
4303int initialize_counters(int cpu_id)
4304{
4305 int my_thread_id, my_core_id, my_package_id;
4306
4307 my_package_id = get_physical_package_id(cpu_id);
4308 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004309 my_thread_id = get_cpu_position_in_core(cpu_id);
4310 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004311 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004312
4313 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4314 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4315 return 0;
4316}
4317
4318void allocate_output_buffer()
4319{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004320 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004321 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004322 if (outp == NULL)
4323 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004324}
Len Brown36229892016-02-26 20:51:02 -05004325void allocate_fd_percpu(void)
4326{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004327 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004328 if (fd_percpu == NULL)
4329 err(-1, "calloc fd_percpu");
4330}
Len Brown562a2d32016-02-26 23:48:05 -05004331void allocate_irq_buffers(void)
4332{
4333 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4334 if (irq_column_2_cpu == NULL)
4335 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004336
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004337 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004338 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004339 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004340}
Len Brownc98d5d92012-06-04 00:56:40 -04004341void setup_all_buffers(void)
4342{
4343 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004344 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004345 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004346 allocate_counters(&thread_even, &core_even, &package_even);
4347 allocate_counters(&thread_odd, &core_odd, &package_odd);
4348 allocate_output_buffer();
4349 for_all_proc_cpus(initialize_counters);
4350}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004351
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004352void set_base_cpu(void)
4353{
4354 base_cpu = sched_getcpu();
4355 if (base_cpu < 0)
4356 err(-ENODEV, "No valid cpus found");
4357
4358 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004359 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004360}
4361
Len Brown103a8fe2010-10-22 23:53:03 -04004362void turbostat_init()
4363{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004364 setup_all_buffers();
4365 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004366 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004367 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004368 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004369
Len Brown103a8fe2010-10-22 23:53:03 -04004370
Len Brown96e47152017-01-21 02:26:00 -05004371 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004372 for_all_cpus(print_hwp, ODD_COUNTERS);
4373
Len Brown96e47152017-01-21 02:26:00 -05004374 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004375 for_all_cpus(print_epb, ODD_COUNTERS);
4376
Len Brown96e47152017-01-21 02:26:00 -05004377 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004378 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4379
Len Brown96e47152017-01-21 02:26:00 -05004380 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004381 for_all_cpus(print_rapl, ODD_COUNTERS);
4382
4383 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4384
Len Brown96e47152017-01-21 02:26:00 -05004385 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004386 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004387
Len Brown96e47152017-01-21 02:26:00 -05004388 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004389 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004390}
4391
4392int fork_it(char **argv)
4393{
Len Brown103a8fe2010-10-22 23:53:03 -04004394 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004395 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004396
Len Brown218f0e82017-02-14 22:07:52 -05004397 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004398 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4399 if (status)
4400 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004401 /* clear affinity side-effect of get_counters() */
4402 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004403 gettimeofday(&tv_even, (struct timezone *)NULL);
4404
4405 child_pid = fork();
4406 if (!child_pid) {
4407 /* child */
4408 execvp(argv[0], argv);
4409 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004410
4411 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004412 if (child_pid == -1)
4413 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004414
4415 signal(SIGINT, SIG_IGN);
4416 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004417 if (waitpid(child_pid, &status, 0) == -1)
4418 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004419 }
Len Brownc98d5d92012-06-04 00:56:40 -04004420 /*
4421 * n.b. fork_it() does not check for errors from for_all_cpus()
4422 * because re-starting is problematic when forking
4423 */
Len Brown218f0e82017-02-14 22:07:52 -05004424 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004425 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004426 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004427 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004428 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4429 fprintf(outf, "%s: Counter reset detected\n", progname);
4430 else {
4431 compute_average(EVEN_COUNTERS);
4432 format_all_counters(EVEN_COUNTERS);
4433 }
Len Brown103a8fe2010-10-22 23:53:03 -04004434
Len Brownb7d8c142016-02-13 23:36:17 -05004435 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4436
4437 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004438
Len Brownd91bb172012-11-01 00:08:19 -04004439 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004440}
4441
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004442int get_and_dump_counters(void)
4443{
4444 int status;
4445
Len Brown218f0e82017-02-14 22:07:52 -05004446 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004447 status = for_all_cpus(get_counters, ODD_COUNTERS);
4448 if (status)
4449 return status;
4450
4451 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4452 if (status)
4453 return status;
4454
Len Brownb7d8c142016-02-13 23:36:17 -05004455 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004456
4457 return status;
4458}
4459
Len Brownd8af6f52015-02-10 01:56:38 -05004460void print_version() {
Len Brown0539ba12017-02-10 00:27:20 -05004461 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004462 " - Len Brown <lenb@kernel.org>\n");
4463}
4464
Len Brown495c76542017-02-08 02:41:51 -05004465int add_counter(unsigned int msr_num, char *path, char *name,
4466 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004467 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004468{
4469 struct msr_counter *msrp;
4470
4471 msrp = calloc(1, sizeof(struct msr_counter));
4472 if (msrp == NULL) {
4473 perror("calloc");
4474 exit(1);
4475 }
4476
4477 msrp->msr_num = msr_num;
4478 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004479 if (path)
4480 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004481 msrp->width = width;
4482 msrp->type = type;
4483 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004484 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004485
4486 switch (scope) {
4487
4488 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004489 msrp->next = sys.tp;
4490 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004491 sys.added_thread_counters++;
4492 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4493 fprintf(stderr, "exceeded max %d added thread counters\n",
4494 MAX_ADDED_COUNTERS);
4495 exit(-1);
4496 }
Len Brown388e9c82016-12-22 23:57:55 -05004497 break;
4498
4499 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004500 msrp->next = sys.cp;
4501 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004502 sys.added_core_counters++;
4503 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4504 fprintf(stderr, "exceeded max %d added core counters\n",
4505 MAX_ADDED_COUNTERS);
4506 exit(-1);
4507 }
Len Brown388e9c82016-12-22 23:57:55 -05004508 break;
4509
4510 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004511 msrp->next = sys.pp;
4512 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004513 sys.added_package_counters++;
4514 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4515 fprintf(stderr, "exceeded max %d added package counters\n",
4516 MAX_ADDED_COUNTERS);
4517 exit(-1);
4518 }
Len Brown388e9c82016-12-22 23:57:55 -05004519 break;
4520 }
4521
4522 return 0;
4523}
4524
4525void parse_add_command(char *add_command)
4526{
4527 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004528 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004529 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004530 int width = 64;
4531 int fail = 0;
4532 enum counter_scope scope = SCOPE_CPU;
4533 enum counter_type type = COUNTER_CYCLES;
4534 enum counter_format format = FORMAT_DELTA;
4535
4536 while (add_command) {
4537
4538 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4539 goto next;
4540
4541 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4542 goto next;
4543
Len Brown495c76542017-02-08 02:41:51 -05004544 if (*add_command == '/') {
4545 path = add_command;
4546 goto next;
4547 }
4548
Len Brown388e9c82016-12-22 23:57:55 -05004549 if (sscanf(add_command, "u%d", &width) == 1) {
4550 if ((width == 32) || (width == 64))
4551 goto next;
4552 width = 64;
4553 }
4554 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4555 scope = SCOPE_CPU;
4556 goto next;
4557 }
4558 if (!strncmp(add_command, "core", strlen("core"))) {
4559 scope = SCOPE_CORE;
4560 goto next;
4561 }
4562 if (!strncmp(add_command, "package", strlen("package"))) {
4563 scope = SCOPE_PACKAGE;
4564 goto next;
4565 }
4566 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4567 type = COUNTER_CYCLES;
4568 goto next;
4569 }
4570 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4571 type = COUNTER_SECONDS;
4572 goto next;
4573 }
Len Brown41618e62017-02-09 18:25:22 -05004574 if (!strncmp(add_command, "usec", strlen("usec"))) {
4575 type = COUNTER_USEC;
4576 goto next;
4577 }
Len Brown388e9c82016-12-22 23:57:55 -05004578 if (!strncmp(add_command, "raw", strlen("raw"))) {
4579 format = FORMAT_RAW;
4580 goto next;
4581 }
4582 if (!strncmp(add_command, "delta", strlen("delta"))) {
4583 format = FORMAT_DELTA;
4584 goto next;
4585 }
4586 if (!strncmp(add_command, "percent", strlen("percent"))) {
4587 format = FORMAT_PERCENT;
4588 goto next;
4589 }
4590
4591 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4592 char *eos;
4593
4594 eos = strchr(name_buffer, ',');
4595 if (eos)
4596 *eos = '\0';
4597 goto next;
4598 }
4599
4600next:
4601 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004602 if (add_command) {
4603 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004604 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004605 }
Len Brown388e9c82016-12-22 23:57:55 -05004606
4607 }
Len Brown495c76542017-02-08 02:41:51 -05004608 if ((msr_num == 0) && (path == NULL)) {
4609 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004610 fail++;
4611 }
4612
4613 /* generate default column header */
4614 if (*name_buffer == '\0') {
4615 if (format == FORMAT_RAW) {
4616 if (width == 32)
4617 sprintf(name_buffer, "msr%d", msr_num);
4618 else
4619 sprintf(name_buffer, "MSR%d", msr_num);
4620 } else if (format == FORMAT_DELTA) {
4621 if (width == 32)
4622 sprintf(name_buffer, "cnt%d", msr_num);
4623 else
4624 sprintf(name_buffer, "CNT%d", msr_num);
4625 } else if (format == FORMAT_PERCENT) {
4626 if (width == 32)
4627 sprintf(name_buffer, "msr%d%%", msr_num);
4628 else
4629 sprintf(name_buffer, "MSR%d%%", msr_num);
4630 }
4631 }
4632
Len Brown41618e62017-02-09 18:25:22 -05004633 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05004634 fail++;
4635
4636 if (fail) {
4637 help();
4638 exit(1);
4639 }
4640}
Len Brown41618e62017-02-09 18:25:22 -05004641
4642void probe_sysfs(void)
4643{
4644 char path[64];
4645 char name_buf[16];
4646 FILE *input;
4647 int state;
4648 char *sp;
4649
4650 if (!DO_BIC(BIC_sysfs))
4651 return;
4652
4653 for (state = 10; state > 0; --state) {
4654
4655 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4656 base_cpu, state);
4657 input = fopen(path, "r");
4658 if (input == NULL)
4659 continue;
4660 fgets(name_buf, sizeof(name_buf), input);
4661
4662 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4663 sp = strchr(name_buf, '-');
4664 if (!sp)
4665 sp = strchrnul(name_buf, '\n');
4666 *sp = '%';
4667 *(sp + 1) = '\0';
4668
4669 fclose(input);
4670
4671 sprintf(path, "cpuidle/state%d/time", state);
4672
4673 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4674 FORMAT_PERCENT, SYSFS_PERCPU);
4675 }
4676
4677 for (state = 10; state > 0; --state) {
4678
4679 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4680 base_cpu, state);
4681 input = fopen(path, "r");
4682 if (input == NULL)
4683 continue;
4684 fgets(name_buf, sizeof(name_buf), input);
4685 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4686 sp = strchr(name_buf, '-');
4687 if (!sp)
4688 sp = strchrnul(name_buf, '\n');
4689 *sp = '\0';
4690 fclose(input);
4691
4692 sprintf(path, "cpuidle/state%d/usage", state);
4693
4694 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4695 FORMAT_DELTA, SYSFS_PERCPU);
4696 }
4697
4698}
4699
Len Brown1ef7d212017-02-10 23:54:15 -05004700
4701/*
4702 * parse cpuset with following syntax
4703 * 1,2,4..6,8-10 and set bits in cpu_subset
4704 */
4705void parse_cpu_command(char *optarg)
4706{
4707 unsigned int start, end;
4708 char *next;
4709
4710 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
4711 if (cpu_subset == NULL)
4712 err(3, "CPU_ALLOC");
4713 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
4714
4715 CPU_ZERO_S(cpu_subset_size, cpu_subset);
4716
4717 next = optarg;
4718
4719 while (next && *next) {
4720
4721 if (*next == '-') /* no negative cpu numbers */
4722 goto error;
4723
4724 start = strtoul(next, &next, 10);
4725
4726 if (start >= CPU_SUBSET_MAXCPUS)
4727 goto error;
4728 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4729
4730 if (*next == '\0')
4731 break;
4732
4733 if (*next == ',') {
4734 next += 1;
4735 continue;
4736 }
4737
4738 if (*next == '-') {
4739 next += 1; /* start range */
4740 } else if (*next == '.') {
4741 next += 1;
4742 if (*next == '.')
4743 next += 1; /* start range */
4744 else
4745 goto error;
4746 }
4747
4748 end = strtoul(next, &next, 10);
4749 if (end <= start)
4750 goto error;
4751
4752 while (++start <= end) {
4753 if (start >= CPU_SUBSET_MAXCPUS)
4754 goto error;
4755 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4756 }
4757
4758 if (*next == ',')
4759 next += 1;
4760 else if (*next != '\0')
4761 goto error;
4762 }
4763
4764 return;
4765
4766error:
4767 fprintf(stderr, "'--cpu %s' malformed\n", optarg);
4768 exit(-1);
4769}
4770
Len Brown812db3f2017-02-10 00:25:41 -05004771/*
4772 * HIDE_LIST - hide this list of counters, show the rest [default]
4773 * SHOW_LIST - show this list of counters, hide the rest
4774 */
4775enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4776
4777int shown;
4778/*
4779 * parse_show_hide() - process cmdline to set default counter action
4780 */
4781void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4782{
4783 /*
4784 * --show: show only those specified
4785 * The 1st invocation will clear and replace the enabled mask
4786 * subsequent invocations can add to it.
4787 */
4788 if (new_mode == SHOW_LIST) {
4789 if (shown == 0)
4790 bic_enabled = bic_lookup(optarg);
4791 else
4792 bic_enabled |= bic_lookup(optarg);
4793 shown = 1;
4794
4795 return;
4796 }
4797
4798 /*
4799 * --hide: do not show those specified
4800 * multiple invocations simply clear more bits in enabled mask
4801 */
4802 bic_enabled &= ~bic_lookup(optarg);
Len Brown41618e62017-02-09 18:25:22 -05004803
Len Brown812db3f2017-02-10 00:25:41 -05004804}
4805
Len Brown103a8fe2010-10-22 23:53:03 -04004806void cmdline(int argc, char **argv)
4807{
4808 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004809 int option_index = 0;
4810 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004811 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05004812 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05004813 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05004814 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brownd8af6f52015-02-10 01:56:38 -05004815 {"interval", required_argument, 0, 'i'},
4816 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004817 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004818 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05004819 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05004820 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004821 {"Package", no_argument, 0, 'p'},
4822 {"processor", no_argument, 0, 'p'},
Len Brown96e47152017-01-21 02:26:00 -05004823 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05004824 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004825 {"Summary", no_argument, 0, 'S'},
4826 {"TCC", required_argument, 0, 'T'},
4827 {"version", no_argument, 0, 'v' },
4828 {0, 0, 0, 0 }
4829 };
Len Brown103a8fe2010-10-22 23:53:03 -04004830
4831 progname = argv[0];
4832
Len Brown96e47152017-01-21 02:26:00 -05004833 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpqST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004834 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004835 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004836 case 'a':
4837 parse_add_command(optarg);
4838 break;
Len Brown1ef7d212017-02-10 23:54:15 -05004839 case 'c':
4840 parse_cpu_command(optarg);
4841 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004842 case 'D':
4843 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004844 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004845 case 'd':
4846 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004847 break;
Len Brown812db3f2017-02-10 00:25:41 -05004848 case 'H':
4849 parse_show_hide(optarg, HIDE_LIST);
4850 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004851 case 'h':
4852 default:
4853 help();
4854 exit(1);
4855 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004856 {
4857 double interval = strtod(optarg, NULL);
4858
4859 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004860 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004861 interval);
4862 exit(2);
4863 }
4864
4865 interval_ts.tv_sec = interval;
4866 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4867 }
Len Brown889facb2012-11-08 00:48:57 -05004868 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004869 case 'J':
4870 rapl_joules++;
4871 break;
Len Brownc8ade362017-02-15 17:15:11 -05004872 case 'l':
4873 list_header_only++;
4874 quiet++;
4875 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004876 case 'o':
4877 outf = fopen_or_die(optarg, "w");
4878 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004879 case 'P':
4880 show_pkg_only++;
4881 break;
4882 case 'p':
4883 show_core_only++;
4884 break;
Len Brown96e47152017-01-21 02:26:00 -05004885 case 'q':
4886 quiet = 1;
4887 break;
Len Brown812db3f2017-02-10 00:25:41 -05004888 case 's':
4889 parse_show_hide(optarg, SHOW_LIST);
4890 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004891 case 'S':
4892 summary_only++;
4893 break;
4894 case 'T':
4895 tcc_activation_temp_override = atoi(optarg);
4896 break;
4897 case 'v':
4898 print_version();
4899 exit(0);
4900 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004901 }
4902 }
4903}
4904
4905int main(int argc, char **argv)
4906{
Len Brownb7d8c142016-02-13 23:36:17 -05004907 outf = stderr;
4908
Len Brown103a8fe2010-10-22 23:53:03 -04004909 cmdline(argc, argv);
4910
Len Brown96e47152017-01-21 02:26:00 -05004911 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05004912 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004913
Len Brown41618e62017-02-09 18:25:22 -05004914 probe_sysfs();
4915
Len Brown103a8fe2010-10-22 23:53:03 -04004916 turbostat_init();
4917
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004918 /* dump counters and exit */
4919 if (dump_only)
4920 return get_and_dump_counters();
4921
Len Brownc8ade362017-02-15 17:15:11 -05004922 /* list header and exit */
4923 if (list_header_only) {
4924 print_header(",");
4925 flush_output_stdout();
4926 return 0;
4927 }
4928
Len Brown103a8fe2010-10-22 23:53:03 -04004929 /*
4930 * if any params left, it must be a command to fork
4931 */
4932 if (argc - optind)
4933 return fork_it(argv + optind);
4934 else
4935 turbostat_loop();
4936
4937 return 0;
4938}