blob: dd9b2efbbb2ad0bdef553ed7e67d5aa4b942030a [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 Brown3f44a5c2017-10-17 15:42:56 -040053unsigned int shown;
Len Brown0de6c0d2017-02-15 21:45:40 -050054unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050055unsigned int rapl_joules;
56unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050057unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050058unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040059unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070060unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050061unsigned int do_slm_cstates;
62unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040063unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050064unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040065unsigned int do_irtl_snb;
66unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050067unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040068unsigned int genuine_intel;
69unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050070unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050071unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020072unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040073double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040074double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040075unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040076double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040077unsigned int show_pkg_only;
78unsigned int show_core_only;
79char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050080unsigned int do_rapl;
81unsigned int do_dts;
82unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050083unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050084unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050085unsigned int tcc_activation_temp;
86unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050087double rapl_power_units, rapl_time_units;
88double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050089double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040090unsigned int do_core_perf_limit_reasons;
91unsigned int do_gfx_perf_limit_reasons;
92unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040093unsigned int crystal_hz;
94unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040095int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040096double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050097unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
98 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
99unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
100unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
101unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
102unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500103unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500104
Len Browne6f9bb32013-12-03 02:19:19 -0500105#define RAPL_PKG (1 << 0)
106 /* 0x610 MSR_PKG_POWER_LIMIT */
107 /* 0x611 MSR_PKG_ENERGY_STATUS */
108#define RAPL_PKG_PERF_STATUS (1 << 1)
109 /* 0x613 MSR_PKG_PERF_STATUS */
110#define RAPL_PKG_POWER_INFO (1 << 2)
111 /* 0x614 MSR_PKG_POWER_INFO */
112
113#define RAPL_DRAM (1 << 3)
114 /* 0x618 MSR_DRAM_POWER_LIMIT */
115 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500116#define RAPL_DRAM_PERF_STATUS (1 << 4)
117 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400118#define RAPL_DRAM_POWER_INFO (1 << 5)
119 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500120
Jacob Pan91484942016-06-16 09:48:20 -0700121#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500122 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400123#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500124 /* 0x63a MSR_PP0_POLICY */
125
Len Brown0b2bb692015-03-26 00:50:30 -0400126#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500127 /* 0x640 MSR_PP1_POWER_LIMIT */
128 /* 0x641 MSR_PP1_ENERGY_STATUS */
129 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700130
131#define RAPL_CORES_ENERGY_STATUS (1 << 9)
132 /* 0x639 MSR_PP0_ENERGY_STATUS */
133#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500134#define TJMAX_DEFAULT 100
135
136#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400137
Len Brown388e9c82016-12-22 23:57:55 -0500138/*
139 * buffer size used by sscanf() for added column names
140 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
141 */
142#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500143#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500144
Len Brown103a8fe2010-10-22 23:53:03 -0400145int backwards_count;
146char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400147
Len Brown1ef7d212017-02-10 23:54:15 -0500148#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
149cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
150size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown678a3bd2017-02-09 22:22:13 -0500151#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400152
Len Brownc98d5d92012-06-04 00:56:40 -0400153struct thread_data {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700154 struct timeval tv_begin;
155 struct timeval tv_end;
Len Brownc98d5d92012-06-04 00:56:40 -0400156 unsigned long long tsc;
157 unsigned long long aperf;
158 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500159 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500160 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500161 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400162 unsigned int cpu_id;
163 unsigned int flags;
164#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
165#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500166 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400167} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400168
Len Brownc98d5d92012-06-04 00:56:40 -0400169struct core_data {
170 unsigned long long c3;
171 unsigned long long c6;
172 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500173 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500174 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400175 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500176 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400177} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400178
Len Brownc98d5d92012-06-04 00:56:40 -0400179struct pkg_data {
180 unsigned long long pc2;
181 unsigned long long pc3;
182 unsigned long long pc6;
183 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800184 unsigned long long pc8;
185 unsigned long long pc9;
186 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400187 unsigned long long pkg_wtd_core_c0;
188 unsigned long long pkg_any_core_c0;
189 unsigned long long pkg_any_gfxe_c0;
190 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400191 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500192 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400193 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500194 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
195 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
196 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
197 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
198 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
199 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
200 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500201 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400202} *package_even, *package_odd;
203
204#define ODD_COUNTERS thread_odd, core_odd, package_odd
205#define EVEN_COUNTERS thread_even, core_even, package_even
206
207#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
208 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
209 topo.num_threads_per_core + \
210 (core_no) * topo.num_threads_per_core + (thread_no))
211#define GET_CORE(core_base, core_no, pkg_no) \
212 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
213#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
214
Len Brown388e9c82016-12-22 23:57:55 -0500215enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500216enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500217enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
218
219struct msr_counter {
220 unsigned int msr_num;
221 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500222 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500223 unsigned int width;
224 enum counter_type type;
225 enum counter_format format;
226 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500227 unsigned int flags;
228#define FLAGS_HIDE (1 << 0)
229#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500230#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500231};
232
233struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500234 unsigned int added_thread_counters;
235 unsigned int added_core_counters;
236 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500237 struct msr_counter *tp;
238 struct msr_counter *cp;
239 struct msr_counter *pp;
240} sys;
241
Len Brownc98d5d92012-06-04 00:56:40 -0400242struct system_summary {
243 struct thread_data threads;
244 struct core_data cores;
245 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500246} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400247
248
249struct topo_params {
250 int num_packages;
251 int num_cpus;
252 int num_cores;
253 int max_cpu_num;
254 int num_cores_per_pkg;
255 int num_threads_per_core;
256} topo;
257
258struct timeval tv_even, tv_odd, tv_delta;
259
Len Brown562a2d32016-02-26 23:48:05 -0500260int *irq_column_2_cpu; /* /proc/interrupts column numbers */
261int *irqs_per_cpu; /* indexed by cpu_num */
262
Len Brownc98d5d92012-06-04 00:56:40 -0400263void setup_all_buffers(void);
264
265int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400266{
Len Brownc98d5d92012-06-04 00:56:40 -0400267 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400268}
Len Brown88c32812012-03-29 21:44:40 -0400269/*
Len Brownc98d5d92012-06-04 00:56:40 -0400270 * run func(thread, core, package) in topology order
271 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400272 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400273
Len Brownc98d5d92012-06-04 00:56:40 -0400274int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
275 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400276{
Len Brownc98d5d92012-06-04 00:56:40 -0400277 int retval, pkg_no, core_no, thread_no;
278
279 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
280 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
281 for (thread_no = 0; thread_no <
282 topo.num_threads_per_core; ++thread_no) {
283 struct thread_data *t;
284 struct core_data *c;
285 struct pkg_data *p;
286
287 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
288
289 if (cpu_is_not_present(t->cpu_id))
290 continue;
291
292 c = GET_CORE(core_base, core_no, pkg_no);
293 p = GET_PKG(pkg_base, pkg_no);
294
295 retval = func(t, c, p);
296 if (retval)
297 return retval;
298 }
299 }
300 }
301 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400302}
303
304int cpu_migrate(int cpu)
305{
Len Brownc98d5d92012-06-04 00:56:40 -0400306 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
307 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
308 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400309 return -1;
310 else
311 return 0;
312}
Len Brown36229892016-02-26 20:51:02 -0500313int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400314{
Len Brown103a8fe2010-10-22 23:53:03 -0400315 char pathname[32];
316 int fd;
317
Len Brown36229892016-02-26 20:51:02 -0500318 fd = fd_percpu[cpu];
319
320 if (fd)
321 return fd;
322
Len Brown103a8fe2010-10-22 23:53:03 -0400323 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
324 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400325 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400326 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 -0400327
Len Brown36229892016-02-26 20:51:02 -0500328 fd_percpu[cpu] = fd;
329
330 return fd;
331}
332
333int get_msr(int cpu, off_t offset, unsigned long long *msr)
334{
335 ssize_t retval;
336
337 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400338
Len Brown98481e72014-08-15 00:36:50 -0400339 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500340 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400341
342 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400343}
344
Len Brownfc04cc62014-02-06 00:55:19 -0500345/*
Len Brown812db3f2017-02-10 00:25:41 -0500346 * Each string in this array is compared in --show and --hide cmdline.
347 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500348 */
Len Brown812db3f2017-02-10 00:25:41 -0500349struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400350 { 0x0, "usec" },
351 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500352 { 0x0, "Package" },
353 { 0x0, "Avg_MHz" },
354 { 0x0, "Bzy_MHz" },
355 { 0x0, "TSC_MHz" },
356 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500357 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500358 { 0x0, "Busy%" },
359 { 0x0, "CPU%c1" },
360 { 0x0, "CPU%c3" },
361 { 0x0, "CPU%c6" },
362 { 0x0, "CPU%c7" },
363 { 0x0, "ThreadC" },
364 { 0x0, "CoreTmp" },
365 { 0x0, "CoreCnt" },
366 { 0x0, "PkgTmp" },
367 { 0x0, "GFX%rc6" },
368 { 0x0, "GFXMHz" },
369 { 0x0, "Pkg%pc2" },
370 { 0x0, "Pkg%pc3" },
371 { 0x0, "Pkg%pc6" },
372 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500373 { 0x0, "Pkg%pc8" },
374 { 0x0, "Pkg%pc9" },
375 { 0x0, "Pkg%pc10" },
Len Brown812db3f2017-02-10 00:25:41 -0500376 { 0x0, "PkgWatt" },
377 { 0x0, "CorWatt" },
378 { 0x0, "GFXWatt" },
379 { 0x0, "PkgCnt" },
380 { 0x0, "RAMWatt" },
381 { 0x0, "PKG_%" },
382 { 0x0, "RAM_%" },
383 { 0x0, "Pkg_J" },
384 { 0x0, "Cor_J" },
385 { 0x0, "GFX_J" },
386 { 0x0, "RAM_J" },
387 { 0x0, "Core" },
388 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500389 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500390 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400391 { 0x0, "Totl%C0" },
392 { 0x0, "Any%C0" },
393 { 0x0, "GFX%C0" },
394 { 0x0, "CPUGFX%" },
Len Brown812db3f2017-02-10 00:25:41 -0500395};
396
Len Browna99d8732017-05-20 20:11:55 -0400397
398
Len Brown812db3f2017-02-10 00:25:41 -0500399#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400400#define BIC_USEC (1ULL << 0)
401#define BIC_TOD (1ULL << 1)
402#define BIC_Package (1ULL << 2)
403#define BIC_Avg_MHz (1ULL << 3)
404#define BIC_Bzy_MHz (1ULL << 4)
405#define BIC_TSC_MHz (1ULL << 5)
406#define BIC_IRQ (1ULL << 6)
407#define BIC_SMI (1ULL << 7)
408#define BIC_Busy (1ULL << 8)
409#define BIC_CPU_c1 (1ULL << 9)
410#define BIC_CPU_c3 (1ULL << 10)
411#define BIC_CPU_c6 (1ULL << 11)
412#define BIC_CPU_c7 (1ULL << 12)
413#define BIC_ThreadC (1ULL << 13)
414#define BIC_CoreTmp (1ULL << 14)
415#define BIC_CoreCnt (1ULL << 15)
416#define BIC_PkgTmp (1ULL << 16)
417#define BIC_GFX_rc6 (1ULL << 17)
418#define BIC_GFXMHz (1ULL << 18)
419#define BIC_Pkgpc2 (1ULL << 19)
420#define BIC_Pkgpc3 (1ULL << 20)
421#define BIC_Pkgpc6 (1ULL << 21)
422#define BIC_Pkgpc7 (1ULL << 22)
423#define BIC_Pkgpc8 (1ULL << 23)
424#define BIC_Pkgpc9 (1ULL << 24)
425#define BIC_Pkgpc10 (1ULL << 25)
426#define BIC_PkgWatt (1ULL << 26)
427#define BIC_CorWatt (1ULL << 27)
428#define BIC_GFXWatt (1ULL << 28)
429#define BIC_PkgCnt (1ULL << 29)
430#define BIC_RAMWatt (1ULL << 30)
431#define BIC_PKG__ (1ULL << 31)
432#define BIC_RAM__ (1ULL << 32)
433#define BIC_Pkg_J (1ULL << 33)
434#define BIC_Cor_J (1ULL << 34)
435#define BIC_GFX_J (1ULL << 35)
436#define BIC_RAM_J (1ULL << 36)
437#define BIC_Core (1ULL << 37)
438#define BIC_CPU (1ULL << 38)
439#define BIC_Mod_c6 (1ULL << 39)
440#define BIC_sysfs (1ULL << 40)
441#define BIC_Totl_c0 (1ULL << 41)
442#define BIC_Any_c0 (1ULL << 42)
443#define BIC_GFX_c0 (1ULL << 43)
444#define BIC_CPUGFX (1ULL << 44)
Len Brown812db3f2017-02-10 00:25:41 -0500445
Len Brown3f44a5c2017-10-17 15:42:56 -0400446#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
447
448unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
449unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500450
451#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400452#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500453#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500454#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500455
Len Brown3f44a5c2017-10-17 15:42:56 -0400456
Len Browndd778a52017-02-21 23:21:13 -0500457#define MAX_DEFERRED 16
458char *deferred_skip_names[MAX_DEFERRED];
459int deferred_skip_index;
460
461/*
462 * HIDE_LIST - hide this list of counters, show the rest [default]
463 * SHOW_LIST - show this list of counters, hide the rest
464 */
465enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
466
467void help(void)
468{
469 fprintf(outf,
470 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
471 "\n"
472 "Turbostat forks the specified COMMAND and prints statistics\n"
473 "when COMMAND completes.\n"
474 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
475 "to print statistics, until interrupted.\n"
476 "--add add a counter\n"
477 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
478 "--cpu cpu-set limit output to summary plus cpu-set:\n"
479 " {core | package | j,k,l..m,n-p }\n"
480 "--quiet skip decoding system configuration header\n"
481 "--interval sec Override default 5-second measurement interval\n"
482 "--help print this help message\n"
483 "--list list column headers only\n"
484 "--out file create or truncate \"file\" for all output\n"
485 "--version print version information\n"
486 "\n"
487 "For more help, run \"man turbostat\"\n");
488}
489
Len Brown812db3f2017-02-10 00:25:41 -0500490/*
491 * bic_lookup
492 * for all the strings in comma separate name_list,
493 * set the approprate bit in return value.
494 */
Len Browndd778a52017-02-21 23:21:13 -0500495unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500496{
497 int i;
498 unsigned long long retval = 0;
499
500 while (name_list) {
501 char *comma;
502
503 comma = strchr(name_list, ',');
504
505 if (comma)
506 *comma = '\0';
507
Len Brown3f44a5c2017-10-17 15:42:56 -0400508 if (!strcmp(name_list, "all"))
509 return ~0;
510
Len Brown812db3f2017-02-10 00:25:41 -0500511 for (i = 0; i < MAX_BIC; ++i) {
512 if (!strcmp(name_list, bic[i].name)) {
513 retval |= (1ULL << i);
514 break;
515 }
516 }
517 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500518 if (mode == SHOW_LIST) {
519 fprintf(stderr, "Invalid counter name: %s\n", name_list);
520 exit(-1);
521 }
522 deferred_skip_names[deferred_skip_index++] = name_list;
523 if (debug)
524 fprintf(stderr, "deferred \"%s\"\n", name_list);
525 if (deferred_skip_index >= MAX_DEFERRED) {
526 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
527 MAX_DEFERRED, name_list);
528 help();
529 exit(1);
530 }
Len Brown812db3f2017-02-10 00:25:41 -0500531 }
532
533 name_list = comma;
534 if (name_list)
535 name_list++;
536
537 }
538 return retval;
539}
Len Brownfc04cc62014-02-06 00:55:19 -0500540
Len Browndd778a52017-02-21 23:21:13 -0500541
Len Brownc8ade362017-02-15 17:15:11 -0500542void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400543{
Len Brown388e9c82016-12-22 23:57:55 -0500544 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500545 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500546
Len Brown3f44a5c2017-10-17 15:42:56 -0400547 if (DO_BIC(BIC_USEC))
548 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
549 if (DO_BIC(BIC_TOD))
550 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500551 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500552 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500553 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500554 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500555 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500556 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500557 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500558 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500559 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500560 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500561 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500562 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500563 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500564 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500565
Len Brown0de6c0d2017-02-15 21:45:40 -0500566 if (DO_BIC(BIC_IRQ)) {
567 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500568 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500569 else
Len Brown6168c2e2017-02-16 23:07:51 -0500570 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500571 }
572
Len Brown812db3f2017-02-10 00:25:41 -0500573 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500574 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500575
Len Brown388e9c82016-12-22 23:57:55 -0500576 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500577
Len Brown388e9c82016-12-22 23:57:55 -0500578 if (mp->format == FORMAT_RAW) {
579 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500580 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500581 else
Len Browndd778a52017-02-21 23:21:13 -0500582 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500583 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500584 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500585 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500586 else
Len Browndd778a52017-02-21 23:21:13 -0500587 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500588 }
589 }
590
Len Brown41618e62017-02-09 18:25:22 -0500591 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500592 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500593 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500594 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500595 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500596 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500597 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500598 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500599
Len Brown0539ba12017-02-10 00:27:20 -0500600 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500601 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500602
Len Brown812db3f2017-02-10 00:25:41 -0500603 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500604 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500605
606 for (mp = sys.cp; mp; mp = mp->next) {
607 if (mp->format == FORMAT_RAW) {
608 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500609 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500610 else
Len Brownc8ade362017-02-15 17:15:11 -0500611 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500612 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500613 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
614 outp += sprintf(outp, "%s%8s", delim, mp->name);
615 else
616 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500617 }
618 }
619
Len Brown812db3f2017-02-10 00:25:41 -0500620 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500621 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500622
Len Brown812db3f2017-02-10 00:25:41 -0500623 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500624 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500625
Len Brown812db3f2017-02-10 00:25:41 -0500626 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500627 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500628
Len Browna99d8732017-05-20 20:11:55 -0400629 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500630 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400631 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500632 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400633 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500634 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400635 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500636 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400637
Len Brown0f47c082017-01-27 00:50:45 -0500638 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500639 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500640 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500641 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500642 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500643 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500644 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500645 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500646 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500647 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500648 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500649 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500650 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500651 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400652
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800653 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500654 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500655 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500656 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500657 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500658 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500659 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500660 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500661 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500662 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500663 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500664 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500665 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500666 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500667 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500668 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500669 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500670 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500671 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500672 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500673 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500674 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500675 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500676 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500677 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500678 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800679 }
Len Brown388e9c82016-12-22 23:57:55 -0500680 for (mp = sys.pp; mp; mp = mp->next) {
681 if (mp->format == FORMAT_RAW) {
682 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500683 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500684 else
Len Brownc8ade362017-02-15 17:15:11 -0500685 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500686 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500687 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
688 outp += sprintf(outp, "%s%8s", delim, mp->name);
689 else
690 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500691 }
692 }
693
Len Brownc98d5d92012-06-04 00:56:40 -0400694 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400695}
696
Len Brownc98d5d92012-06-04 00:56:40 -0400697int dump_counters(struct thread_data *t, struct core_data *c,
698 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400699{
Len Brown388e9c82016-12-22 23:57:55 -0500700 int i;
701 struct msr_counter *mp;
702
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200703 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400704
Len Brownc98d5d92012-06-04 00:56:40 -0400705 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200706 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
707 t->cpu_id, t->flags);
708 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
709 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
710 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
711 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500712
Len Brown812db3f2017-02-10 00:25:41 -0500713 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500714 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500715 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500716 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500717
718 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
719 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
720 i, mp->msr_num, t->counter[i]);
721 }
Len Brownc98d5d92012-06-04 00:56:40 -0400722 }
Len Brown103a8fe2010-10-22 23:53:03 -0400723
Len Brownc98d5d92012-06-04 00:56:40 -0400724 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200725 outp += sprintf(outp, "core: %d\n", c->core_id);
726 outp += sprintf(outp, "c3: %016llX\n", c->c3);
727 outp += sprintf(outp, "c6: %016llX\n", c->c6);
728 outp += sprintf(outp, "c7: %016llX\n", c->c7);
729 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500730
731 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
732 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
733 i, mp->msr_num, c->counter[i]);
734 }
Len Brown0539ba12017-02-10 00:27:20 -0500735 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400736 }
737
738 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200739 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400740
741 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
742 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
743 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
744 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
745
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200746 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500747 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500748 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500749 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500750 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500751 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500752 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200753 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
754 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
755 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
756 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
757 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
758 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
759 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
760 outp += sprintf(outp, "Throttle PKG: %0X\n",
761 p->rapl_pkg_perf_status);
762 outp += sprintf(outp, "Throttle RAM: %0X\n",
763 p->rapl_dram_perf_status);
764 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500765
766 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
767 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
768 i, mp->msr_num, p->counter[i]);
769 }
Len Brownc98d5d92012-06-04 00:56:40 -0400770 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200771
772 outp += sprintf(outp, "\n");
773
Len Brownc98d5d92012-06-04 00:56:40 -0400774 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400775}
776
Len Browne23da032012-02-06 18:37:16 -0500777/*
778 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500779 */
Len Brownc98d5d92012-06-04 00:56:40 -0400780int format_counters(struct thread_data *t, struct core_data *c,
781 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400782{
Len Brown008d396e2017-02-10 00:29:51 -0500783 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500784 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500785 int i;
786 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500787 char *delim = "\t";
788 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400789
Len Brownc98d5d92012-06-04 00:56:40 -0400790 /* if showing only 1st thread in core and this isn't one, bail out */
791 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
792 return 0;
793
794 /* if showing only 1st thread in pkg and this isn't one, bail out */
795 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
796 return 0;
797
Len Brown1ef7d212017-02-10 23:54:15 -0500798 /*if not summary line and --cpu is used */
799 if ((t != &average.threads) &&
800 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
801 return 0;
802
Len Brown3f44a5c2017-10-17 15:42:56 -0400803 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700804 /* on each row, print how many usec each timestamp took to gather */
805 struct timeval tv;
806
807 timersub(&t->tv_end, &t->tv_begin, &tv);
808 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
809 }
810
Len Brown3f44a5c2017-10-17 15:42:56 -0400811 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
812 if (DO_BIC(BIC_TOD))
813 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
814
Len Brown103a8fe2010-10-22 23:53:03 -0400815 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
816
Len Brown008d396e2017-02-10 00:29:51 -0500817 tsc = t->tsc * tsc_tweak;
818
Len Brownc98d5d92012-06-04 00:56:40 -0400819 /* topo columns, print blanks on 1st (average) line */
820 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500821 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500822 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500823 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500824 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500825 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500826 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400827 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500828 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400829 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500830 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400831 else
Len Brown6168c2e2017-02-16 23:07:51 -0500832 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400833 }
Len Brown812db3f2017-02-10 00:25:41 -0500834 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400835 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500836 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400837 else
Len Brown6168c2e2017-02-16 23:07:51 -0500838 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400839 }
Len Brown812db3f2017-02-10 00:25:41 -0500840 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500841 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400842 }
Len Brownfc04cc62014-02-06 00:55:19 -0500843
Len Brown812db3f2017-02-10 00:25:41 -0500844 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500845 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500846 1.0 / units * t->aperf / interval_float);
847
Len Brown812db3f2017-02-10 00:25:41 -0500848 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500849 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400850
Len Brown812db3f2017-02-10 00:25:41 -0500851 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400852 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500853 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400854 else
Len Brown6168c2e2017-02-16 23:07:51 -0500855 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500856 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400857 }
Len Brown103a8fe2010-10-22 23:53:03 -0400858
Len Brown812db3f2017-02-10 00:25:41 -0500859 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500860 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400861
Len Brown562a2d32016-02-26 23:48:05 -0500862 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500863 if (DO_BIC(BIC_IRQ)) {
864 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500865 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500866 else
Len Brown6168c2e2017-02-16 23:07:51 -0500867 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500868 }
Len Brown562a2d32016-02-26 23:48:05 -0500869
Len Brown1cc21f72015-02-23 00:34:57 -0500870 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500871 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500872 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500873
Len Brown678a3bd2017-02-09 22:22:13 -0500874 /* Added counters */
875 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
876 if (mp->format == FORMAT_RAW) {
877 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500878 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500879 else
Len Brown6168c2e2017-02-16 23:07:51 -0500880 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500881 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500882 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500883 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500884 else
Len Brown6168c2e2017-02-16 23:07:51 -0500885 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500886 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500887 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500888 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500889 else
Len Brown6168c2e2017-02-16 23:07:51 -0500890 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500891 }
892 }
893
Len Brown41618e62017-02-09 18:25:22 -0500894 /* C1 */
895 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500896 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500897
898
Len Brownc98d5d92012-06-04 00:56:40 -0400899 /* print per-core data only for 1st thread in core */
900 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
901 goto done;
902
Len Brown812db3f2017-02-10 00:25:41 -0500903 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500904 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500905 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500906 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500907 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500908 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400909
Len Brown0539ba12017-02-10 00:27:20 -0500910 /* Mod%c6 */
911 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500912 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500913
Len Brown812db3f2017-02-10 00:25:41 -0500914 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500915 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500916
Len Brown388e9c82016-12-22 23:57:55 -0500917 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
918 if (mp->format == FORMAT_RAW) {
919 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500920 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500921 else
Len Brown6168c2e2017-02-16 23:07:51 -0500922 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500923 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500924 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500925 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500926 else
Len Brown6168c2e2017-02-16 23:07:51 -0500927 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500928 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500929 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500930 }
931 }
932
Len Brownc98d5d92012-06-04 00:56:40 -0400933 /* print per-package data only for 1st core in package */
934 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
935 goto done;
936
Len Brown0b2bb692015-03-26 00:50:30 -0400937 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500938 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500939 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500940
Len Brownfdf676e2016-02-27 01:28:12 -0500941 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500942 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400943 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500944 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400945 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500946 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400947 p->gfx_rc6_ms / 10.0 / interval_float);
948 }
949 }
Len Brownfdf676e2016-02-27 01:28:12 -0500950
Len Brown27d47352016-02-27 00:37:54 -0500951 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500952 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500953 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500954
Len Brown0b2bb692015-03-26 00:50:30 -0400955 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -0400956 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500957 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400958 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500959 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400960 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500961 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400962 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500963 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400964
Len Brown0f47c082017-01-27 00:50:45 -0500965 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500966 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500967 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500968 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500969 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500970 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500971 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500972 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500973 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500974 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500975 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500976 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500977 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500978 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500979
980 /*
981 * If measurement interval exceeds minimum RAPL Joule Counter range,
982 * indicate that results are suspect by printing "**" in fraction place.
983 */
Len Brownfc04cc62014-02-06 00:55:19 -0500984 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -0500985 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500986 else
Len Browne975db52016-04-06 23:56:02 -0400987 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500988
Len Brown812db3f2017-02-10 00:25:41 -0500989 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500990 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500991 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500992 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500993 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500994 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500995 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500996 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500997 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500998 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500999 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001000 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001001 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001002 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001003 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001004 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001005 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001006 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001007 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001008 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001009
Len Brown388e9c82016-12-22 23:57:55 -05001010 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1011 if (mp->format == FORMAT_RAW) {
1012 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001013 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001014 else
Len Brown6168c2e2017-02-16 23:07:51 -05001015 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001016 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001017 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001018 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001019 else
Len Brown6168c2e2017-02-16 23:07:51 -05001020 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001021 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001022 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001023 }
1024 }
1025
Len Brownc98d5d92012-06-04 00:56:40 -04001026done:
Len Brownc98d5d92012-06-04 00:56:40 -04001027 outp += sprintf(outp, "\n");
1028
1029 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001030}
1031
Len Brownb7d8c142016-02-13 23:36:17 -05001032void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001033{
Len Brownb7d8c142016-02-13 23:36:17 -05001034 FILE *filep;
1035
1036 if (outf == stderr)
1037 filep = stdout;
1038 else
1039 filep = outf;
1040
1041 fputs(output_buffer, filep);
1042 fflush(filep);
1043
Len Brownc98d5d92012-06-04 00:56:40 -04001044 outp = output_buffer;
1045}
Len Brownb7d8c142016-02-13 23:36:17 -05001046void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001047{
Len Brownb7d8c142016-02-13 23:36:17 -05001048 fputs(output_buffer, outf);
1049 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001050 outp = output_buffer;
1051}
1052void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1053{
Len Browne23da032012-02-06 18:37:16 -05001054 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001055
Len Browne23da032012-02-06 18:37:16 -05001056 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001057 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001058
Len Brownc98d5d92012-06-04 00:56:40 -04001059 if (topo.num_cpus > 1)
1060 format_counters(&average.threads, &average.cores,
1061 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001062
Len Browne23da032012-02-06 18:37:16 -05001063 printed = 1;
1064
1065 if (summary_only)
1066 return;
1067
Len Brownc98d5d92012-06-04 00:56:40 -04001068 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001069}
1070
Len Brown889facb2012-11-08 00:48:57 -05001071#define DELTA_WRAP32(new, old) \
1072 if (new > old) { \
1073 old = new - old; \
1074 } else { \
1075 old = 0x100000000 + new - old; \
1076 }
1077
Len Brownba3dec92016-04-22 20:31:46 -04001078int
Len Brownc98d5d92012-06-04 00:56:40 -04001079delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001080{
Len Brown388e9c82016-12-22 23:57:55 -05001081 int i;
1082 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001083
Len Browna99d8732017-05-20 20:11:55 -04001084
1085 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001086 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001087 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001088 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001089 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001090 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001091 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001092 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001093
Len Brownc98d5d92012-06-04 00:56:40 -04001094 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001095 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001096 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001097 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001098 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001099 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001100 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001101 old->pc8 = new->pc8 - old->pc8;
1102 old->pc9 = new->pc9 - old->pc9;
1103 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -05001104 old->pkg_temp_c = new->pkg_temp_c;
1105
Len Brown9185e982016-04-06 17:16:00 -04001106 /* flag an error when rc6 counter resets/wraps */
1107 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1108 old->gfx_rc6_ms = -1;
1109 else
1110 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1111
Len Brown27d47352016-02-27 00:37:54 -05001112 old->gfx_mhz = new->gfx_mhz;
1113
Len Brown889facb2012-11-08 00:48:57 -05001114 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1115 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1116 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1117 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1118 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1119 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001120
Len Brown388e9c82016-12-22 23:57:55 -05001121 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1122 if (mp->format == FORMAT_RAW)
1123 old->counter[i] = new->counter[i];
1124 else
1125 old->counter[i] = new->counter[i] - old->counter[i];
1126 }
1127
Len Brownba3dec92016-04-22 20:31:46 -04001128 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001129}
Len Brown103a8fe2010-10-22 23:53:03 -04001130
Len Brownc98d5d92012-06-04 00:56:40 -04001131void
1132delta_core(struct core_data *new, struct core_data *old)
1133{
Len Brown388e9c82016-12-22 23:57:55 -05001134 int i;
1135 struct msr_counter *mp;
1136
Len Brownc98d5d92012-06-04 00:56:40 -04001137 old->c3 = new->c3 - old->c3;
1138 old->c6 = new->c6 - old->c6;
1139 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001140 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001141 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001142
1143 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1144 if (mp->format == FORMAT_RAW)
1145 old->counter[i] = new->counter[i];
1146 else
1147 old->counter[i] = new->counter[i] - old->counter[i];
1148 }
Len Brownc98d5d92012-06-04 00:56:40 -04001149}
Len Brown103a8fe2010-10-22 23:53:03 -04001150
Len Brownc3ae3312012-06-13 21:31:46 -04001151/*
1152 * old = new - old
1153 */
Len Brownba3dec92016-04-22 20:31:46 -04001154int
Len Brownc98d5d92012-06-04 00:56:40 -04001155delta_thread(struct thread_data *new, struct thread_data *old,
1156 struct core_data *core_delta)
1157{
Len Brown388e9c82016-12-22 23:57:55 -05001158 int i;
1159 struct msr_counter *mp;
1160
Len Brown3f44a5c2017-10-17 15:42:56 -04001161 /*
1162 * the timestamps from start of measurement interval are in "old"
1163 * the timestamp from end of measurement interval are in "new"
1164 * over-write old w/ new so we can print end of interval values
1165 */
1166
1167 old->tv_begin = new->tv_begin;
1168 old->tv_end = new->tv_end;
1169
Len Brownc98d5d92012-06-04 00:56:40 -04001170 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001171
Len Brownc98d5d92012-06-04 00:56:40 -04001172 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001173 if (old->tsc < (1000 * 1000))
1174 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1175 "You can disable all c-states by booting with \"idle=poll\"\n"
1176 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001177
Len Brownc98d5d92012-06-04 00:56:40 -04001178 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001179
Len Brown812db3f2017-02-10 00:25:41 -05001180 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001181 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1182 old->aperf = new->aperf - old->aperf;
1183 old->mperf = new->mperf - old->mperf;
1184 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001185 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001186 }
Len Brownc98d5d92012-06-04 00:56:40 -04001187 }
Len Brown103a8fe2010-10-22 23:53:03 -04001188
Len Brown103a8fe2010-10-22 23:53:03 -04001189
Len Brown144b44b2013-11-09 00:30:16 -05001190 if (use_c1_residency_msr) {
1191 /*
1192 * Some models have a dedicated C1 residency MSR,
1193 * which should be more accurate than the derivation below.
1194 */
1195 } else {
1196 /*
1197 * As counter collection is not atomic,
1198 * it is possible for mperf's non-halted cycles + idle states
1199 * to exceed TSC's all cycles: show c1 = 0% in that case.
1200 */
Len Brown95149362017-04-12 19:44:51 -04001201 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001202 old->c1 = 0;
1203 else {
1204 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001205 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001206 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001207 }
Len Brownc98d5d92012-06-04 00:56:40 -04001208 }
Len Brownc3ae3312012-06-13 21:31:46 -04001209
Len Brownc98d5d92012-06-04 00:56:40 -04001210 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001211 if (debug > 1)
1212 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001213 old->mperf = 1; /* divide by 0 protection */
1214 }
1215
Len Brown812db3f2017-02-10 00:25:41 -05001216 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001217 old->irq_count = new->irq_count - old->irq_count;
1218
Len Brown812db3f2017-02-10 00:25:41 -05001219 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001220 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001221
Len Brown388e9c82016-12-22 23:57:55 -05001222 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1223 if (mp->format == FORMAT_RAW)
1224 old->counter[i] = new->counter[i];
1225 else
1226 old->counter[i] = new->counter[i] - old->counter[i];
1227 }
Len Brownba3dec92016-04-22 20:31:46 -04001228 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001229}
1230
1231int delta_cpu(struct thread_data *t, struct core_data *c,
1232 struct pkg_data *p, struct thread_data *t2,
1233 struct core_data *c2, struct pkg_data *p2)
1234{
Len Brownba3dec92016-04-22 20:31:46 -04001235 int retval = 0;
1236
Len Brownc98d5d92012-06-04 00:56:40 -04001237 /* calculate core delta only for 1st thread in core */
1238 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1239 delta_core(c, c2);
1240
1241 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001242 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1243 if (retval)
1244 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001245
1246 /* calculate package delta only for 1st core in package */
1247 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001248 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001249
Len Brownba3dec92016-04-22 20:31:46 -04001250 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001251}
1252
1253void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1254{
Len Brown388e9c82016-12-22 23:57:55 -05001255 int i;
1256 struct msr_counter *mp;
1257
Len Brown3f44a5c2017-10-17 15:42:56 -04001258 t->tv_begin.tv_sec = 0;
1259 t->tv_begin.tv_usec = 0;
1260 t->tv_end.tv_sec = 0;
1261 t->tv_end.tv_usec = 0;
1262
Len Brownc98d5d92012-06-04 00:56:40 -04001263 t->tsc = 0;
1264 t->aperf = 0;
1265 t->mperf = 0;
1266 t->c1 = 0;
1267
Len Brown562a2d32016-02-26 23:48:05 -05001268 t->irq_count = 0;
1269 t->smi_count = 0;
1270
Len Brownc98d5d92012-06-04 00:56:40 -04001271 /* tells format_counters to dump all fields from this set */
1272 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1273
1274 c->c3 = 0;
1275 c->c6 = 0;
1276 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001277 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001278 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001279
Len Brown0b2bb692015-03-26 00:50:30 -04001280 p->pkg_wtd_core_c0 = 0;
1281 p->pkg_any_core_c0 = 0;
1282 p->pkg_any_gfxe_c0 = 0;
1283 p->pkg_both_core_gfxe_c0 = 0;
1284
Len Brownc98d5d92012-06-04 00:56:40 -04001285 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001286 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001287 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001288 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001289 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001290 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001291 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001292 p->pc8 = 0;
1293 p->pc9 = 0;
1294 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001295
1296 p->energy_pkg = 0;
1297 p->energy_dram = 0;
1298 p->energy_cores = 0;
1299 p->energy_gfx = 0;
1300 p->rapl_pkg_perf_status = 0;
1301 p->rapl_dram_perf_status = 0;
1302 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001303
Len Brownfdf676e2016-02-27 01:28:12 -05001304 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001305 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001306 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1307 t->counter[i] = 0;
1308
1309 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1310 c->counter[i] = 0;
1311
1312 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1313 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001314}
1315int sum_counters(struct thread_data *t, struct core_data *c,
1316 struct pkg_data *p)
1317{
Len Brown388e9c82016-12-22 23:57:55 -05001318 int i;
1319 struct msr_counter *mp;
1320
Len Brown3f44a5c2017-10-17 15:42:56 -04001321 /* remember first tv_begin */
1322 if (average.threads.tv_begin.tv_sec == 0)
1323 average.threads.tv_begin = t->tv_begin;
1324
1325 /* remember last tv_end */
1326 average.threads.tv_end = t->tv_end;
1327
Len Brownc98d5d92012-06-04 00:56:40 -04001328 average.threads.tsc += t->tsc;
1329 average.threads.aperf += t->aperf;
1330 average.threads.mperf += t->mperf;
1331 average.threads.c1 += t->c1;
1332
Len Brown562a2d32016-02-26 23:48:05 -05001333 average.threads.irq_count += t->irq_count;
1334 average.threads.smi_count += t->smi_count;
1335
Len Brown388e9c82016-12-22 23:57:55 -05001336 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1337 if (mp->format == FORMAT_RAW)
1338 continue;
1339 average.threads.counter[i] += t->counter[i];
1340 }
1341
Len Brownc98d5d92012-06-04 00:56:40 -04001342 /* sum per-core values only for 1st thread in core */
1343 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1344 return 0;
1345
1346 average.cores.c3 += c->c3;
1347 average.cores.c6 += c->c6;
1348 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001349 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001350
Len Brown889facb2012-11-08 00:48:57 -05001351 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1352
Len Brown388e9c82016-12-22 23:57:55 -05001353 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1354 if (mp->format == FORMAT_RAW)
1355 continue;
1356 average.cores.counter[i] += c->counter[i];
1357 }
1358
Len Brownc98d5d92012-06-04 00:56:40 -04001359 /* sum per-pkg values only for 1st core in pkg */
1360 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1361 return 0;
1362
Len Browna99d8732017-05-20 20:11:55 -04001363 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001364 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001365 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001366 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001367 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001368 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001369 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001370 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001371
Len Brownc98d5d92012-06-04 00:56:40 -04001372 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001373 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001374 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001375 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001376 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001377 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001378 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001379 average.packages.pc8 += p->pc8;
1380 average.packages.pc9 += p->pc9;
1381 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001382
Len Brown889facb2012-11-08 00:48:57 -05001383 average.packages.energy_pkg += p->energy_pkg;
1384 average.packages.energy_dram += p->energy_dram;
1385 average.packages.energy_cores += p->energy_cores;
1386 average.packages.energy_gfx += p->energy_gfx;
1387
Len Brownfdf676e2016-02-27 01:28:12 -05001388 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001389 average.packages.gfx_mhz = p->gfx_mhz;
1390
Len Brown889facb2012-11-08 00:48:57 -05001391 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1392
1393 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1394 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001395
1396 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1397 if (mp->format == FORMAT_RAW)
1398 continue;
1399 average.packages.counter[i] += p->counter[i];
1400 }
Len Brownc98d5d92012-06-04 00:56:40 -04001401 return 0;
1402}
1403/*
1404 * sum the counters for all cpus in the system
1405 * compute the weighted average
1406 */
1407void compute_average(struct thread_data *t, struct core_data *c,
1408 struct pkg_data *p)
1409{
Len Brown388e9c82016-12-22 23:57:55 -05001410 int i;
1411 struct msr_counter *mp;
1412
Len Brownc98d5d92012-06-04 00:56:40 -04001413 clear_counters(&average.threads, &average.cores, &average.packages);
1414
1415 for_all_cpus(sum_counters, t, c, p);
1416
1417 average.threads.tsc /= topo.num_cpus;
1418 average.threads.aperf /= topo.num_cpus;
1419 average.threads.mperf /= topo.num_cpus;
1420 average.threads.c1 /= topo.num_cpus;
1421
Len Brown0de6c0d2017-02-15 21:45:40 -05001422 if (average.threads.irq_count > 9999999)
1423 sums_need_wide_columns = 1;
1424
Len Brownc98d5d92012-06-04 00:56:40 -04001425 average.cores.c3 /= topo.num_cores;
1426 average.cores.c6 /= topo.num_cores;
1427 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001428 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001429
Len Browna99d8732017-05-20 20:11:55 -04001430 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001431 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001432 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001433 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001434 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001435 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001436 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001437 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001438
Len Brownc98d5d92012-06-04 00:56:40 -04001439 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001440 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001441 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001442 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001443 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001444 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001445 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001446
1447 average.packages.pc8 /= topo.num_packages;
1448 average.packages.pc9 /= topo.num_packages;
1449 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001450
1451 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1452 if (mp->format == FORMAT_RAW)
1453 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001454 if (mp->type == COUNTER_ITEMS) {
1455 if (average.threads.counter[i] > 9999999)
1456 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001457 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001458 }
Len Brown388e9c82016-12-22 23:57:55 -05001459 average.threads.counter[i] /= topo.num_cpus;
1460 }
1461 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1462 if (mp->format == FORMAT_RAW)
1463 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001464 if (mp->type == COUNTER_ITEMS) {
1465 if (average.cores.counter[i] > 9999999)
1466 sums_need_wide_columns = 1;
1467 }
Len Brown388e9c82016-12-22 23:57:55 -05001468 average.cores.counter[i] /= topo.num_cores;
1469 }
1470 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1471 if (mp->format == FORMAT_RAW)
1472 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001473 if (mp->type == COUNTER_ITEMS) {
1474 if (average.packages.counter[i] > 9999999)
1475 sums_need_wide_columns = 1;
1476 }
Len Brown388e9c82016-12-22 23:57:55 -05001477 average.packages.counter[i] /= topo.num_packages;
1478 }
Len Brownc98d5d92012-06-04 00:56:40 -04001479}
1480
1481static unsigned long long rdtsc(void)
1482{
1483 unsigned int low, high;
1484
1485 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1486
1487 return low | ((unsigned long long)high) << 32;
1488}
1489
Len Brownc98d5d92012-06-04 00:56:40 -04001490/*
Len Brown495c76542017-02-08 02:41:51 -05001491 * Open a file, and exit on failure
1492 */
1493FILE *fopen_or_die(const char *path, const char *mode)
1494{
1495 FILE *filep = fopen(path, mode);
1496
1497 if (!filep)
1498 err(1, "%s: open failed", path);
1499 return filep;
1500}
1501/*
1502 * snapshot_sysfs_counter()
1503 *
1504 * return snapshot of given counter
1505 */
1506unsigned long long snapshot_sysfs_counter(char *path)
1507{
1508 FILE *fp;
1509 int retval;
1510 unsigned long long counter;
1511
1512 fp = fopen_or_die(path, "r");
1513
1514 retval = fscanf(fp, "%lld", &counter);
1515 if (retval != 1)
1516 err(1, "snapshot_sysfs_counter(%s)", path);
1517
1518 fclose(fp);
1519
1520 return counter;
1521}
1522
1523int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1524{
1525 if (mp->msr_num != 0) {
1526 if (get_msr(cpu, mp->msr_num, counterp))
1527 return -1;
1528 } else {
Len Brown41618e62017-02-09 18:25:22 -05001529 char path[128];
1530
1531 if (mp->flags & SYSFS_PERCPU) {
1532 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1533 cpu, mp->path);
1534
1535 *counterp = snapshot_sysfs_counter(path);
1536 } else {
1537 *counterp = snapshot_sysfs_counter(mp->path);
1538 }
Len Brown495c76542017-02-08 02:41:51 -05001539 }
1540
1541 return 0;
1542}
1543
1544/*
Len Brownc98d5d92012-06-04 00:56:40 -04001545 * get_counters(...)
1546 * migrate to cpu
1547 * acquire and record local counters for that cpu
1548 */
1549int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1550{
1551 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001552 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001553 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001554 struct msr_counter *mp;
1555 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001556
Len Brownf4fdf2b2017-05-27 21:06:55 -07001557
1558 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1559
Len Browne52966c2012-11-08 22:38:05 -05001560 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001561 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001562 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001563 }
Len Brownc98d5d92012-06-04 00:56:40 -04001564
Len Brown0102b062016-02-27 03:11:29 -05001565retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001566 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1567
Len Brown812db3f2017-02-10 00:25:41 -05001568 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001569 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1570
1571 /*
1572 * The TSC, APERF and MPERF must be read together for
1573 * APERF/MPERF and MPERF/TSC to give accurate results.
1574 *
1575 * Unfortunately, APERF and MPERF are read by
1576 * individual system call, so delays may occur
1577 * between them. If the time to read them
1578 * varies by a large amount, we re-read them.
1579 */
1580
1581 /*
1582 * This initial dummy APERF read has been seen to
1583 * reduce jitter in the subsequent reads.
1584 */
1585
Len Brown9c63a652012-10-31 01:29:52 -04001586 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001587 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001588
1589 t->tsc = rdtsc(); /* re-read close to APERF */
1590
1591 tsc_before = t->tsc;
1592
1593 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1594 return -3;
1595
1596 tsc_between = rdtsc();
1597
Len Brown9c63a652012-10-31 01:29:52 -04001598 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001599 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001600
1601 tsc_after = rdtsc();
1602
1603 aperf_time = tsc_between - tsc_before;
1604 mperf_time = tsc_after - tsc_between;
1605
1606 /*
1607 * If the system call latency to read APERF and MPERF
1608 * differ by more than 2x, then try again.
1609 */
1610 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1611 aperf_mperf_retry_count++;
1612 if (aperf_mperf_retry_count < 5)
1613 goto retry;
1614 else
1615 warnx("cpu%d jitter %lld %lld",
1616 cpu, aperf_time, mperf_time);
1617 }
1618 aperf_mperf_retry_count = 0;
1619
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001620 t->aperf = t->aperf * aperf_mperf_multiplier;
1621 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001622 }
1623
Len Brown812db3f2017-02-10 00:25:41 -05001624 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001625 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001626 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001627 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1628 return -5;
1629 t->smi_count = msr & 0xFFFFFFFF;
1630 }
Len Brown0539ba12017-02-10 00:27:20 -05001631 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001632 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1633 return -6;
1634 }
1635
Len Brown388e9c82016-12-22 23:57:55 -05001636 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001637 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001638 return -10;
1639 }
1640
Len Brownc98d5d92012-06-04 00:56:40 -04001641 /* collect core counters only for 1st thread in core */
1642 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001643 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001644
Len Brown812db3f2017-02-10 00:25:41 -05001645 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001646 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1647 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001648 }
1649
Len Brown812db3f2017-02-10 00:25:41 -05001650 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001651 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1652 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001653 } else if (do_knl_cstates) {
1654 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1655 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001656 }
1657
Len Brown812db3f2017-02-10 00:25:41 -05001658 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001659 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1660 return -8;
1661
Len Brown0539ba12017-02-10 00:27:20 -05001662 if (DO_BIC(BIC_Mod_c6))
1663 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1664 return -8;
1665
Len Brown812db3f2017-02-10 00:25:41 -05001666 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001667 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1668 return -9;
1669 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1670 }
1671
Len Brown388e9c82016-12-22 23:57:55 -05001672 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001673 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001674 return -10;
1675 }
Len Brown889facb2012-11-08 00:48:57 -05001676
Len Brownc98d5d92012-06-04 00:56:40 -04001677 /* collect package counters only for 1st core in package */
1678 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001679 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001680
Len Browna99d8732017-05-20 20:11:55 -04001681 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001682 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1683 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001684 }
1685 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001686 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1687 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001688 }
1689 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001690 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1691 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001692 }
1693 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001694 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1695 return -13;
1696 }
Len Brown0f47c082017-01-27 00:50:45 -05001697 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001698 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1699 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001700 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001701 if (do_slm_cstates) {
1702 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1703 return -10;
1704 } else {
1705 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1706 return -10;
1707 }
1708 }
1709
Len Brown0f47c082017-01-27 00:50:45 -05001710 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001711 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1712 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001713 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001714 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1715 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001716 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001717 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1718 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001719 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001720 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1721 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001722 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001723 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1724 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001725
Len Brown889facb2012-11-08 00:48:57 -05001726 if (do_rapl & RAPL_PKG) {
1727 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1728 return -13;
1729 p->energy_pkg = msr & 0xFFFFFFFF;
1730 }
Jacob Pan91484942016-06-16 09:48:20 -07001731 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001732 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1733 return -14;
1734 p->energy_cores = msr & 0xFFFFFFFF;
1735 }
1736 if (do_rapl & RAPL_DRAM) {
1737 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1738 return -15;
1739 p->energy_dram = msr & 0xFFFFFFFF;
1740 }
1741 if (do_rapl & RAPL_GFX) {
1742 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1743 return -16;
1744 p->energy_gfx = msr & 0xFFFFFFFF;
1745 }
1746 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1747 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1748 return -16;
1749 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1750 }
1751 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1752 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1753 return -16;
1754 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1755 }
Len Brown812db3f2017-02-10 00:25:41 -05001756 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001757 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1758 return -17;
1759 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1760 }
Len Brownfdf676e2016-02-27 01:28:12 -05001761
Len Brown812db3f2017-02-10 00:25:41 -05001762 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001763 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1764
Len Brown812db3f2017-02-10 00:25:41 -05001765 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001766 p->gfx_mhz = gfx_cur_mhz;
1767
Len Brown388e9c82016-12-22 23:57:55 -05001768 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001769 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001770 return -10;
1771 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001772done:
1773 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001774
Len Brown103a8fe2010-10-22 23:53:03 -04001775 return 0;
1776}
1777
Len Brownee7e38e2015-02-09 23:39:45 -05001778/*
1779 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1780 * If you change the values, note they are used both in comparisons
1781 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1782 */
1783
1784#define PCLUKN 0 /* Unknown */
1785#define PCLRSV 1 /* Reserved */
1786#define PCL__0 2 /* PC0 */
1787#define PCL__1 3 /* PC1 */
1788#define PCL__2 4 /* PC2 */
1789#define PCL__3 5 /* PC3 */
1790#define PCL__4 6 /* PC4 */
1791#define PCL__6 7 /* PC6 */
1792#define PCL_6N 8 /* PC6 No Retention */
1793#define PCL_6R 9 /* PC6 Retention */
1794#define PCL__7 10 /* PC7 */
1795#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001796#define PCL__8 12 /* PC8 */
1797#define PCL__9 13 /* PC9 */
1798#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001799
1800int pkg_cstate_limit = PCLUKN;
1801char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001802 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001803
Len Browne9257f52015-04-01 21:02:57 -04001804int 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};
1805int 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};
1806int 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 -05001807int 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 -05001808int 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 -04001809int 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 -04001810int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Artem Bityutskiy2085e122017-08-04 18:42:23 +03001811int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brownee7e38e2015-02-09 23:39:45 -05001812
Len Browna2b7b742015-09-26 00:12:38 -04001813
1814static void
1815calculate_tsc_tweak()
1816{
Len Browna2b7b742015-09-26 00:12:38 -04001817 tsc_tweak = base_hz / tsc_hz;
1818}
1819
Len Brownfcd17212015-03-23 20:29:09 -04001820static void
1821dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001822{
1823 unsigned long long msr;
1824 unsigned int ratio;
1825
Len Brownec0adc52015-11-12 02:42:31 -05001826 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001827
Len Brownb7d8c142016-02-13 23:36:17 -05001828 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001829
Len Brown103a8fe2010-10-22 23:53:03 -04001830 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001831 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001832 ratio, bclk, ratio * bclk);
1833
1834 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001835 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001836 ratio, bclk, ratio * bclk);
1837
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001838 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001839 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001840 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001841
Len Brownfcd17212015-03-23 20:29:09 -04001842 return;
1843}
1844
1845static void
1846dump_hsw_turbo_ratio_limits(void)
1847{
1848 unsigned long long msr;
1849 unsigned int ratio;
1850
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001851 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001852
Len Brownb7d8c142016-02-13 23:36:17 -05001853 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001854
1855 ratio = (msr >> 8) & 0xFF;
1856 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001857 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001858 ratio, bclk, ratio * bclk);
1859
1860 ratio = (msr >> 0) & 0xFF;
1861 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001862 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001863 ratio, bclk, ratio * bclk);
1864 return;
1865}
1866
1867static void
1868dump_ivt_turbo_ratio_limits(void)
1869{
1870 unsigned long long msr;
1871 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001872
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001873 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001874
Len Brownb7d8c142016-02-13 23:36:17 -05001875 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001876
1877 ratio = (msr >> 56) & 0xFF;
1878 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001879 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001880 ratio, bclk, ratio * bclk);
1881
1882 ratio = (msr >> 48) & 0xFF;
1883 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001884 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001885 ratio, bclk, ratio * bclk);
1886
1887 ratio = (msr >> 40) & 0xFF;
1888 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001889 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001890 ratio, bclk, ratio * bclk);
1891
1892 ratio = (msr >> 32) & 0xFF;
1893 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001894 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001895 ratio, bclk, ratio * bclk);
1896
1897 ratio = (msr >> 24) & 0xFF;
1898 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001899 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001900 ratio, bclk, ratio * bclk);
1901
1902 ratio = (msr >> 16) & 0xFF;
1903 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001904 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001905 ratio, bclk, ratio * bclk);
1906
1907 ratio = (msr >> 8) & 0xFF;
1908 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001909 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001910 ratio, bclk, ratio * bclk);
1911
1912 ratio = (msr >> 0) & 0xFF;
1913 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001914 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001915 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001916 return;
1917}
Len Brown31e07522017-01-31 23:07:49 -05001918int has_turbo_ratio_group_limits(int family, int model)
1919{
1920
1921 if (!genuine_intel)
1922 return 0;
1923
1924 switch (model) {
1925 case INTEL_FAM6_ATOM_GOLDMONT:
1926 case INTEL_FAM6_SKYLAKE_X:
1927 case INTEL_FAM6_ATOM_DENVERTON:
1928 return 1;
1929 }
1930 return 0;
1931}
Len Brown6574a5d2012-09-21 00:01:31 -04001932
Len Brownfcd17212015-03-23 20:29:09 -04001933static void
Len Brown31e07522017-01-31 23:07:49 -05001934dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001935{
Len Brown31e07522017-01-31 23:07:49 -05001936 unsigned long long msr, core_counts;
1937 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001938
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001939 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001940 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001941
Len Brown31e07522017-01-31 23:07:49 -05001942 if (has_turbo_ratio_group_limits(family, model)) {
1943 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1944 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1945 } else {
1946 core_counts = 0x0807060504030201;
1947 }
1948
Len Brown6574a5d2012-09-21 00:01:31 -04001949 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001950 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001951 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001952 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1953 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001954
1955 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001956 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001957 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001958 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1959 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001960
1961 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001962 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001963 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001964 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1965 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001966
1967 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001968 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001969 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001970 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1971 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001972
Len Brown103a8fe2010-10-22 23:53:03 -04001973 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001974 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001975 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001976 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1977 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001978
1979 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001980 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001981 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001982 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1983 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001984
1985 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001986 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001987 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001988 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1989 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001990
1991 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001992 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001993 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001994 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1995 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04001996 return;
1997}
Len Brown3a9a9412014-08-15 02:39:52 -04001998
Len Brownfcd17212015-03-23 20:29:09 -04001999static void
Len Brown0f7887c2017-01-12 23:49:18 -05002000dump_atom_turbo_ratio_limits(void)
2001{
2002 unsigned long long msr;
2003 unsigned int ratio;
2004
2005 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2006 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2007
2008 ratio = (msr >> 0) & 0x3F;
2009 if (ratio)
2010 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2011 ratio, bclk, ratio * bclk);
2012
2013 ratio = (msr >> 8) & 0x3F;
2014 if (ratio)
2015 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2016 ratio, bclk, ratio * bclk);
2017
2018 ratio = (msr >> 16) & 0x3F;
2019 if (ratio)
2020 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2021 ratio, bclk, ratio * bclk);
2022
2023 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2024 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2025
2026 ratio = (msr >> 24) & 0x3F;
2027 if (ratio)
2028 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2029 ratio, bclk, ratio * bclk);
2030
2031 ratio = (msr >> 16) & 0x3F;
2032 if (ratio)
2033 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2034 ratio, bclk, ratio * bclk);
2035
2036 ratio = (msr >> 8) & 0x3F;
2037 if (ratio)
2038 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2039 ratio, bclk, ratio * bclk);
2040
2041 ratio = (msr >> 0) & 0x3F;
2042 if (ratio)
2043 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2044 ratio, bclk, ratio * bclk);
2045}
2046
2047static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002048dump_knl_turbo_ratio_limits(void)
2049{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002050 const unsigned int buckets_no = 7;
2051
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002052 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002053 int delta_cores, delta_ratio;
2054 int i, b_nr;
2055 unsigned int cores[buckets_no];
2056 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002057
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002058 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002059
Len Brownb7d8c142016-02-13 23:36:17 -05002060 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002061 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002062
2063 /**
2064 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002065 * [0] -- Reserved
2066 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002067 * [15:8] -- Base value of freq ratio of bucket 1.
2068 * [20:16] -- +ve delta of number of active cores of bucket 2.
2069 * i.e. active cores of bucket 2 =
2070 * active cores of bucket 1 + delta
2071 * [23:21] -- Negative delta of freq ratio of bucket 2.
2072 * i.e. freq ratio of bucket 2 =
2073 * freq ratio of bucket 1 - delta
2074 * [28:24]-- +ve delta of number of active cores of bucket 3.
2075 * [31:29]-- -ve delta of freq ratio of bucket 3.
2076 * [36:32]-- +ve delta of number of active cores of bucket 4.
2077 * [39:37]-- -ve delta of freq ratio of bucket 4.
2078 * [44:40]-- +ve delta of number of active cores of bucket 5.
2079 * [47:45]-- -ve delta of freq ratio of bucket 5.
2080 * [52:48]-- +ve delta of number of active cores of bucket 6.
2081 * [55:53]-- -ve delta of freq ratio of bucket 6.
2082 * [60:56]-- +ve delta of number of active cores of bucket 7.
2083 * [63:61]-- -ve delta of freq ratio of bucket 7.
2084 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002085
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002086 b_nr = 0;
2087 cores[b_nr] = (msr & 0xFF) >> 1;
2088 ratio[b_nr] = (msr >> 8) & 0xFF;
2089
2090 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002091 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002092 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002093
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002094 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2095 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2096 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002097 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002098
2099 for (i = buckets_no - 1; i >= 0; i--)
2100 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002101 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002102 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002103 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002104}
2105
2106static void
Len Brownfcd17212015-03-23 20:29:09 -04002107dump_nhm_cst_cfg(void)
2108{
2109 unsigned long long msr;
2110
Len Brown1df2e552017-01-07 23:24:57 -05002111 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002112
2113#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
2114#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
2115
Len Brown1df2e552017-01-07 23:24:57 -05002116 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002117
Len Brownb7d8c142016-02-13 23:36:17 -05002118 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04002119 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2120 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2121 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2122 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2123 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002124 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002125 pkg_cstate_limit_strings[pkg_cstate_limit]);
2126 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002127}
2128
Len Brown6fb31432015-06-17 16:23:45 -04002129static void
2130dump_config_tdp(void)
2131{
2132 unsigned long long msr;
2133
2134 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002135 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002136 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002137
2138 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002139 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002140 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002141 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2142 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2143 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2144 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002145 }
Len Brownb7d8c142016-02-13 23:36:17 -05002146 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002147
2148 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002149 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002150 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002151 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2152 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2153 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2154 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002155 }
Len Brownb7d8c142016-02-13 23:36:17 -05002156 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002157
2158 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002159 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002160 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002161 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2162 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2163 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002164
Len Brown6fb31432015-06-17 16:23:45 -04002165 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002166 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002167 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002168 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2169 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002170}
Len Brown5a634262016-04-06 17:15:55 -04002171
2172unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2173
2174void print_irtl(void)
2175{
2176 unsigned long long msr;
2177
2178 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2179 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2180 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2181 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2182
2183 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2184 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2185 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2186 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2187
2188 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2189 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2190 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2191 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2192
2193 if (!do_irtl_hsw)
2194 return;
2195
2196 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2197 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2198 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2199 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2200
2201 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2202 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2203 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2204 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2205
2206 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2207 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2208 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2209 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2210
2211}
Len Brown36229892016-02-26 20:51:02 -05002212void free_fd_percpu(void)
2213{
2214 int i;
2215
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002216 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002217 if (fd_percpu[i] != 0)
2218 close(fd_percpu[i]);
2219 }
2220
2221 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002222}
2223
Len Brownc98d5d92012-06-04 00:56:40 -04002224void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002225{
Len Brownc98d5d92012-06-04 00:56:40 -04002226 CPU_FREE(cpu_present_set);
2227 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002228 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002229
Len Brownc98d5d92012-06-04 00:56:40 -04002230 CPU_FREE(cpu_affinity_set);
2231 cpu_affinity_set = NULL;
2232 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002233
Len Brownc98d5d92012-06-04 00:56:40 -04002234 free(thread_even);
2235 free(core_even);
2236 free(package_even);
2237
2238 thread_even = NULL;
2239 core_even = NULL;
2240 package_even = NULL;
2241
2242 free(thread_odd);
2243 free(core_odd);
2244 free(package_odd);
2245
2246 thread_odd = NULL;
2247 core_odd = NULL;
2248 package_odd = NULL;
2249
2250 free(output_buffer);
2251 output_buffer = NULL;
2252 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002253
2254 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002255
2256 free(irq_column_2_cpu);
2257 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002258}
2259
Josh Triplett57a42a32013-08-20 17:20:17 -07002260
2261/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002262 * Parse a file containing a single int.
2263 */
2264int parse_int_file(const char *fmt, ...)
2265{
2266 va_list args;
2267 char path[PATH_MAX];
2268 FILE *filep;
2269 int value;
2270
2271 va_start(args, fmt);
2272 vsnprintf(path, sizeof(path), fmt, args);
2273 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002274 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002275 if (fscanf(filep, "%d", &value) != 1)
2276 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002277 fclose(filep);
2278 return value;
2279}
2280
2281/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002282 * get_cpu_position_in_core(cpu)
2283 * return the position of the CPU among its HT siblings in the core
2284 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002285 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002286int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002287{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002288 char path[64];
2289 FILE *filep;
2290 int this_cpu;
2291 char character;
2292 int i;
2293
2294 sprintf(path,
2295 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2296 cpu);
2297 filep = fopen(path, "r");
2298 if (filep == NULL) {
2299 perror(path);
2300 exit(1);
2301 }
2302
2303 for (i = 0; i < topo.num_threads_per_core; i++) {
2304 fscanf(filep, "%d", &this_cpu);
2305 if (this_cpu == cpu) {
2306 fclose(filep);
2307 return i;
2308 }
2309
2310 /* Account for no separator after last thread*/
2311 if (i != (topo.num_threads_per_core - 1))
2312 fscanf(filep, "%c", &character);
2313 }
2314
2315 fclose(filep);
2316 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002317}
2318
Len Brownc98d5d92012-06-04 00:56:40 -04002319/*
2320 * cpu_is_first_core_in_package(cpu)
2321 * return 1 if given CPU is 1st core in package
2322 */
2323int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002324{
Josh Triplett95aebc42013-08-20 17:20:16 -07002325 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002326}
2327
2328int get_physical_package_id(int cpu)
2329{
Josh Triplett95aebc42013-08-20 17:20:16 -07002330 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002331}
2332
2333int get_core_id(int cpu)
2334{
Josh Triplett95aebc42013-08-20 17:20:16 -07002335 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002336}
2337
Len Brownc98d5d92012-06-04 00:56:40 -04002338int get_num_ht_siblings(int cpu)
2339{
2340 char path[80];
2341 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002342 int sib1;
2343 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002344 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002345 char str[100];
2346 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002347
2348 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002349 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002350
Len Brownc98d5d92012-06-04 00:56:40 -04002351 /*
2352 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002353 * A ',' separated or '-' separated set of numbers
2354 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002355 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002356 fscanf(filep, "%d%c\n", &sib1, &character);
2357 fseek(filep, 0, SEEK_SET);
2358 fgets(str, 100, filep);
2359 ch = strchr(str, character);
2360 while (ch != NULL) {
2361 matches++;
2362 ch = strchr(ch+1, character);
2363 }
Len Brownc98d5d92012-06-04 00:56:40 -04002364
2365 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002366 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002367}
2368
Len Brown103a8fe2010-10-22 23:53:03 -04002369/*
Len Brownc98d5d92012-06-04 00:56:40 -04002370 * run func(thread, core, package) in topology order
2371 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002372 */
2373
Len Brownc98d5d92012-06-04 00:56:40 -04002374int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2375 struct pkg_data *, struct thread_data *, struct core_data *,
2376 struct pkg_data *), struct thread_data *thread_base,
2377 struct core_data *core_base, struct pkg_data *pkg_base,
2378 struct thread_data *thread_base2, struct core_data *core_base2,
2379 struct pkg_data *pkg_base2)
2380{
2381 int retval, pkg_no, core_no, thread_no;
2382
2383 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2384 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2385 for (thread_no = 0; thread_no <
2386 topo.num_threads_per_core; ++thread_no) {
2387 struct thread_data *t, *t2;
2388 struct core_data *c, *c2;
2389 struct pkg_data *p, *p2;
2390
2391 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2392
2393 if (cpu_is_not_present(t->cpu_id))
2394 continue;
2395
2396 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2397
2398 c = GET_CORE(core_base, core_no, pkg_no);
2399 c2 = GET_CORE(core_base2, core_no, pkg_no);
2400
2401 p = GET_PKG(pkg_base, pkg_no);
2402 p2 = GET_PKG(pkg_base2, pkg_no);
2403
2404 retval = func(t, c, p, t2, c2, p2);
2405 if (retval)
2406 return retval;
2407 }
2408 }
2409 }
2410 return 0;
2411}
2412
2413/*
2414 * run func(cpu) on every cpu in /proc/stat
2415 * return max_cpu number
2416 */
2417int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002418{
2419 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002420 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002421 int retval;
2422
Josh Triplett57a42a32013-08-20 17:20:17 -07002423 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002424
2425 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002426 if (retval != 0)
2427 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002428
Len Brownc98d5d92012-06-04 00:56:40 -04002429 while (1) {
2430 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 -04002431 if (retval != 1)
2432 break;
2433
Len Brownc98d5d92012-06-04 00:56:40 -04002434 retval = func(cpu_num);
2435 if (retval) {
2436 fclose(fp);
2437 return(retval);
2438 }
Len Brown103a8fe2010-10-22 23:53:03 -04002439 }
2440 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002441 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002442}
2443
2444void re_initialize(void)
2445{
Len Brownc98d5d92012-06-04 00:56:40 -04002446 free_all_buffers();
2447 setup_all_buffers();
2448 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002449}
2450
Len Brownc98d5d92012-06-04 00:56:40 -04002451
Len Brown103a8fe2010-10-22 23:53:03 -04002452/*
Len Brownc98d5d92012-06-04 00:56:40 -04002453 * count_cpus()
2454 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002455 */
Len Brownc98d5d92012-06-04 00:56:40 -04002456int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002457{
Len Brownc98d5d92012-06-04 00:56:40 -04002458 if (topo.max_cpu_num < cpu)
2459 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002460
Len Brownc98d5d92012-06-04 00:56:40 -04002461 topo.num_cpus += 1;
2462 return 0;
2463}
2464int mark_cpu_present(int cpu)
2465{
2466 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002467 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002468}
2469
Len Brown562a2d32016-02-26 23:48:05 -05002470/*
2471 * snapshot_proc_interrupts()
2472 *
2473 * read and record summary of /proc/interrupts
2474 *
2475 * return 1 if config change requires a restart, else return 0
2476 */
2477int snapshot_proc_interrupts(void)
2478{
2479 static FILE *fp;
2480 int column, retval;
2481
2482 if (fp == NULL)
2483 fp = fopen_or_die("/proc/interrupts", "r");
2484 else
2485 rewind(fp);
2486
2487 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2488 for (column = 0; column < topo.num_cpus; ++column) {
2489 int cpu_number;
2490
2491 retval = fscanf(fp, " CPU%d", &cpu_number);
2492 if (retval != 1)
2493 break;
2494
2495 if (cpu_number > topo.max_cpu_num) {
2496 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2497 return 1;
2498 }
2499
2500 irq_column_2_cpu[column] = cpu_number;
2501 irqs_per_cpu[cpu_number] = 0;
2502 }
2503
2504 /* read /proc/interrupt count lines and sum up irqs per cpu */
2505 while (1) {
2506 int column;
2507 char buf[64];
2508
2509 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2510 if (retval != 1)
2511 break;
2512
2513 /* read the count per cpu */
2514 for (column = 0; column < topo.num_cpus; ++column) {
2515
2516 int cpu_number, irq_count;
2517
2518 retval = fscanf(fp, " %d", &irq_count);
2519 if (retval != 1)
2520 break;
2521
2522 cpu_number = irq_column_2_cpu[column];
2523 irqs_per_cpu[cpu_number] += irq_count;
2524
2525 }
2526
2527 while (getc(fp) != '\n')
2528 ; /* flush interrupt description */
2529
2530 }
2531 return 0;
2532}
Len Brown27d47352016-02-27 00:37:54 -05002533/*
Len Brownfdf676e2016-02-27 01:28:12 -05002534 * snapshot_gfx_rc6_ms()
2535 *
2536 * record snapshot of
2537 * /sys/class/drm/card0/power/rc6_residency_ms
2538 *
2539 * return 1 if config change requires a restart, else return 0
2540 */
2541int snapshot_gfx_rc6_ms(void)
2542{
2543 FILE *fp;
2544 int retval;
2545
2546 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2547
2548 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2549 if (retval != 1)
2550 err(1, "GFX rc6");
2551
2552 fclose(fp);
2553
2554 return 0;
2555}
2556/*
Len Brown27d47352016-02-27 00:37:54 -05002557 * snapshot_gfx_mhz()
2558 *
2559 * record snapshot of
2560 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2561 *
2562 * return 1 if config change requires a restart, else return 0
2563 */
2564int snapshot_gfx_mhz(void)
2565{
2566 static FILE *fp;
2567 int retval;
2568
2569 if (fp == NULL)
2570 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002571 else {
Len Brown27d47352016-02-27 00:37:54 -05002572 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002573 fflush(fp);
2574 }
Len Brown27d47352016-02-27 00:37:54 -05002575
2576 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2577 if (retval != 1)
2578 err(1, "GFX MHz");
2579
2580 return 0;
2581}
Len Brown562a2d32016-02-26 23:48:05 -05002582
2583/*
2584 * snapshot /proc and /sys files
2585 *
2586 * return 1 if configuration restart needed, else return 0
2587 */
2588int snapshot_proc_sysfs_files(void)
2589{
Len Brown218f0e82017-02-14 22:07:52 -05002590 if (DO_BIC(BIC_IRQ))
2591 if (snapshot_proc_interrupts())
2592 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002593
Len Brown812db3f2017-02-10 00:25:41 -05002594 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002595 snapshot_gfx_rc6_ms();
2596
Len Brown812db3f2017-02-10 00:25:41 -05002597 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002598 snapshot_gfx_mhz();
2599
Len Brown562a2d32016-02-26 23:48:05 -05002600 return 0;
2601}
2602
Len Brown8aa2ed02017-07-15 14:57:37 -04002603int exit_requested;
2604
2605static void signal_handler (int signal)
2606{
2607 switch (signal) {
2608 case SIGINT:
2609 exit_requested = 1;
2610 if (debug)
2611 fprintf(stderr, " SIGINT\n");
2612 break;
Len Brown07211962017-07-15 15:51:26 -04002613 case SIGUSR1:
2614 if (debug > 1)
2615 fprintf(stderr, "SIGUSR1\n");
2616 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002617 }
2618}
2619
2620void setup_signal_handler(void)
2621{
2622 struct sigaction sa;
2623
2624 memset(&sa, 0, sizeof(sa));
2625
2626 sa.sa_handler = &signal_handler;
2627
2628 if (sigaction(SIGINT, &sa, NULL) < 0)
2629 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002630 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2631 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002632}
Len Brown103a8fe2010-10-22 23:53:03 -04002633void turbostat_loop()
2634{
Len Brownc98d5d92012-06-04 00:56:40 -04002635 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002636 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002637
Len Brown8aa2ed02017-07-15 14:57:37 -04002638 setup_signal_handler();
2639
Len Brown103a8fe2010-10-22 23:53:03 -04002640restart:
Len Browne52966c2012-11-08 22:38:05 -05002641 restarted++;
2642
Len Brown562a2d32016-02-26 23:48:05 -05002643 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002644 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002645 if (retval < -1) {
2646 exit(retval);
2647 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002648 if (restarted > 1) {
2649 exit(retval);
2650 }
Len Brownc98d5d92012-06-04 00:56:40 -04002651 re_initialize();
2652 goto restart;
2653 }
Len Browne52966c2012-11-08 22:38:05 -05002654 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002655 gettimeofday(&tv_even, (struct timezone *)NULL);
2656
2657 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002658 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002659 re_initialize();
2660 goto restart;
2661 }
Len Brown2a0609c2016-02-12 22:44:48 -05002662 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002663 if (snapshot_proc_sysfs_files())
2664 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002665 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002666 if (retval < -1) {
2667 exit(retval);
2668 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002669 re_initialize();
2670 goto restart;
2671 }
Len Brown103a8fe2010-10-22 23:53:03 -04002672 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002673 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002674 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2675 re_initialize();
2676 goto restart;
2677 }
Len Brownc98d5d92012-06-04 00:56:40 -04002678 compute_average(EVEN_COUNTERS);
2679 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002680 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002681 if (exit_requested)
2682 break;
Len Brown2a0609c2016-02-12 22:44:48 -05002683 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002684 if (snapshot_proc_sysfs_files())
2685 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002686 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002687 if (retval < -1) {
2688 exit(retval);
2689 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002690 re_initialize();
2691 goto restart;
2692 }
Len Brown103a8fe2010-10-22 23:53:03 -04002693 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002694 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002695 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2696 re_initialize();
2697 goto restart;
2698 }
Len Brownc98d5d92012-06-04 00:56:40 -04002699 compute_average(ODD_COUNTERS);
2700 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002701 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002702 if (exit_requested)
2703 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002704 }
2705}
2706
2707void check_dev_msr()
2708{
2709 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002710 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002711
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002712 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2713 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002714 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2715 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002716}
2717
Len Brown98481e72014-08-15 00:36:50 -04002718void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002719{
Len Brown98481e72014-08-15 00:36:50 -04002720 struct __user_cap_header_struct cap_header_data;
2721 cap_user_header_t cap_header = &cap_header_data;
2722 struct __user_cap_data_struct cap_data_data;
2723 cap_user_data_t cap_data = &cap_data_data;
2724 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2725 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002726 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002727
2728 /* check for CAP_SYS_RAWIO */
2729 cap_header->pid = getpid();
2730 cap_header->version = _LINUX_CAPABILITY_VERSION;
2731 if (capget(cap_header, cap_data) < 0)
2732 err(-6, "capget(2) failed");
2733
2734 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2735 do_exit++;
2736 warnx("capget(CAP_SYS_RAWIO) failed,"
2737 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2738 }
2739
2740 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002741 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2742 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002743 do_exit++;
2744 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2745 }
2746
2747 /* if all else fails, thell them to be root */
2748 if (do_exit)
2749 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002750 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002751
2752 if (do_exit)
2753 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002754}
2755
Len Brownd7899442015-01-23 00:12:33 -05002756/*
2757 * NHM adds support for additional MSRs:
2758 *
2759 * MSR_SMI_COUNT 0x00000034
2760 *
Len Brownec0adc52015-11-12 02:42:31 -05002761 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002762 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002763 *
Len Browncf4cbe52017-01-01 13:08:33 -05002764 * MSR_MISC_PWR_MGMT 0x000001aa
2765 *
Len Brownd7899442015-01-23 00:12:33 -05002766 * MSR_PKG_C3_RESIDENCY 0x000003f8
2767 * MSR_PKG_C6_RESIDENCY 0x000003f9
2768 * MSR_CORE_C3_RESIDENCY 0x000003fc
2769 * MSR_CORE_C6_RESIDENCY 0x000003fd
2770 *
Len Brownee7e38e2015-02-09 23:39:45 -05002771 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002772 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002773 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002774 */
Len Brownee7e38e2015-02-09 23:39:45 -05002775int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002776{
Len Brownee7e38e2015-02-09 23:39:45 -05002777 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002778 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002779 int *pkg_cstate_limits;
2780
Len Brown103a8fe2010-10-22 23:53:03 -04002781 if (!genuine_intel)
2782 return 0;
2783
2784 if (family != 6)
2785 return 0;
2786
Len Brown21ed5572015-10-19 22:37:40 -04002787 bclk = discover_bclk(family, model);
2788
Len Brown103a8fe2010-10-22 23:53:03 -04002789 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002790 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2791 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002792 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002793 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2794 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2795 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2796 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002797 pkg_cstate_limits = nhm_pkg_cstate_limits;
2798 break;
Len Brown869ce692016-06-16 23:22:37 -04002799 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2800 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2801 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2802 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002803 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002804 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002805 break;
Len Brown869ce692016-06-16 23:22:37 -04002806 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2807 case INTEL_FAM6_HASWELL_X: /* HSX */
2808 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2809 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2810 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2811 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2812 case INTEL_FAM6_BROADWELL_X: /* BDX */
2813 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2814 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2815 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2816 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2817 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002818 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002819 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002820 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002821 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2822 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002823 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002824 break;
Len Brown869ce692016-06-16 23:22:37 -04002825 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002826 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002827 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002828 pkg_cstate_limits = slv_pkg_cstate_limits;
2829 break;
Len Brown869ce692016-06-16 23:22:37 -04002830 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002831 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002832 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002833 break;
Len Brown869ce692016-06-16 23:22:37 -04002834 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002835 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002836 pkg_cstate_limits = phi_pkg_cstate_limits;
2837 break;
Len Brown869ce692016-06-16 23:22:37 -04002838 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002839 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002840 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002841 pkg_cstate_limits = bxt_pkg_cstate_limits;
2842 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002843 default:
2844 return 0;
2845 }
Len Brown1df2e552017-01-07 23:24:57 -05002846 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002847 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002848
Len Brownec0adc52015-11-12 02:42:31 -05002849 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002850 base_ratio = (msr >> 8) & 0xFF;
2851
2852 base_hz = base_ratio * bclk * 1000000;
2853 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002854 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002855}
Len Brown0f7887c2017-01-12 23:49:18 -05002856/*
Len Brown495c76542017-02-08 02:41:51 -05002857 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002858 *
2859 * MSR_CC6_DEMOTION_POLICY_CONFIG
2860 * MSR_MC6_DEMOTION_POLICY_CONFIG
2861 */
2862
2863int has_slv_msrs(unsigned int family, unsigned int model)
2864{
2865 if (!genuine_intel)
2866 return 0;
2867
2868 switch (model) {
2869 case INTEL_FAM6_ATOM_SILVERMONT1:
2870 case INTEL_FAM6_ATOM_MERRIFIELD:
2871 case INTEL_FAM6_ATOM_MOOREFIELD:
2872 return 1;
2873 }
2874 return 0;
2875}
Len Brown7170a372017-01-27 02:13:27 -05002876int is_dnv(unsigned int family, unsigned int model)
2877{
2878
2879 if (!genuine_intel)
2880 return 0;
2881
2882 switch (model) {
2883 case INTEL_FAM6_ATOM_DENVERTON:
2884 return 1;
2885 }
2886 return 0;
2887}
Len Brownade0eba2017-02-10 01:56:47 -05002888int is_bdx(unsigned int family, unsigned int model)
2889{
2890
2891 if (!genuine_intel)
2892 return 0;
2893
2894 switch (model) {
2895 case INTEL_FAM6_BROADWELL_X:
2896 case INTEL_FAM6_BROADWELL_XEON_D:
2897 return 1;
2898 }
2899 return 0;
2900}
Len Brown34c761972017-01-27 02:36:41 -05002901int is_skx(unsigned int family, unsigned int model)
2902{
2903
2904 if (!genuine_intel)
2905 return 0;
2906
2907 switch (model) {
2908 case INTEL_FAM6_SKYLAKE_X:
2909 return 1;
2910 }
2911 return 0;
2912}
Len Brown0f7887c2017-01-12 23:49:18 -05002913
Len Brown31e07522017-01-31 23:07:49 -05002914int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002915{
Len Brown0f7887c2017-01-12 23:49:18 -05002916 if (has_slv_msrs(family, model))
2917 return 0;
2918
Len Brownd7899442015-01-23 00:12:33 -05002919 switch (model) {
2920 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002921 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2922 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2923 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002924 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002925 return 0;
2926 default:
2927 return 1;
2928 }
2929}
Len Brown0f7887c2017-01-12 23:49:18 -05002930int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2931{
2932 if (has_slv_msrs(family, model))
2933 return 1;
2934
2935 return 0;
2936}
Len Brown6574a5d2012-09-21 00:01:31 -04002937int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2938{
2939 if (!genuine_intel)
2940 return 0;
2941
2942 if (family != 6)
2943 return 0;
2944
2945 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002946 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2947 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002948 return 1;
2949 default:
2950 return 0;
2951 }
2952}
Len Brownfcd17212015-03-23 20:29:09 -04002953int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2954{
2955 if (!genuine_intel)
2956 return 0;
2957
2958 if (family != 6)
2959 return 0;
2960
2961 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002962 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002963 return 1;
2964 default:
2965 return 0;
2966 }
2967}
2968
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002969int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2970{
2971 if (!genuine_intel)
2972 return 0;
2973
2974 if (family != 6)
2975 return 0;
2976
2977 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002978 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002979 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002980 return 1;
2981 default:
2982 return 0;
2983 }
2984}
Len Brown31e07522017-01-31 23:07:49 -05002985int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
2986{
2987 if (!genuine_intel)
2988 return 0;
2989
2990 if (family != 6)
2991 return 0;
2992
2993 switch (model) {
2994 case INTEL_FAM6_ATOM_GOLDMONT:
2995 case INTEL_FAM6_SKYLAKE_X:
2996 return 1;
2997 default:
2998 return 0;
2999 }
3000}
Len Brown6fb31432015-06-17 16:23:45 -04003001int has_config_tdp(unsigned int family, unsigned int model)
3002{
3003 if (!genuine_intel)
3004 return 0;
3005
3006 if (family != 6)
3007 return 0;
3008
3009 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003010 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3011 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3012 case INTEL_FAM6_HASWELL_X: /* HSX */
3013 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3014 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3015 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3016 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3017 case INTEL_FAM6_BROADWELL_X: /* BDX */
3018 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3019 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3020 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3021 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3022 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3023 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003024
Len Brown869ce692016-06-16 23:22:37 -04003025 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003026 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003027 return 1;
3028 default:
3029 return 0;
3030 }
3031}
3032
Len Brownfcd17212015-03-23 20:29:09 -04003033static void
Colin Ian King1b693172016-03-02 13:50:25 +00003034dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003035{
3036 if (!do_nhm_platform_info)
3037 return;
3038
3039 dump_nhm_platform_info();
3040
3041 if (has_hsw_turbo_ratio_limit(family, model))
3042 dump_hsw_turbo_ratio_limits();
3043
3044 if (has_ivt_turbo_ratio_limit(family, model))
3045 dump_ivt_turbo_ratio_limits();
3046
Len Brown31e07522017-01-31 23:07:49 -05003047 if (has_turbo_ratio_limit(family, model))
3048 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003049
Len Brown0f7887c2017-01-12 23:49:18 -05003050 if (has_atom_turbo_ratio_limit(family, model))
3051 dump_atom_turbo_ratio_limits();
3052
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003053 if (has_knl_turbo_ratio_limit(family, model))
3054 dump_knl_turbo_ratio_limits();
3055
Len Brown6fb31432015-06-17 16:23:45 -04003056 if (has_config_tdp(family, model))
3057 dump_config_tdp();
3058
Len Brownfcd17212015-03-23 20:29:09 -04003059 dump_nhm_cst_cfg();
3060}
3061
Len Brown41618e62017-02-09 18:25:22 -05003062static void
3063dump_sysfs_cstate_config(void)
3064{
3065 char path[64];
3066 char name_buf[16];
3067 char desc[64];
3068 FILE *input;
3069 int state;
3070 char *sp;
3071
3072 if (!DO_BIC(BIC_sysfs))
3073 return;
3074
3075 for (state = 0; state < 10; ++state) {
3076
3077 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3078 base_cpu, state);
3079 input = fopen(path, "r");
3080 if (input == NULL)
3081 continue;
3082 fgets(name_buf, sizeof(name_buf), input);
3083
3084 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3085 sp = strchr(name_buf, '-');
3086 if (!sp)
3087 sp = strchrnul(name_buf, '\n');
3088 *sp = '\0';
3089
3090 fclose(input);
3091
3092 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3093 base_cpu, state);
3094 input = fopen(path, "r");
3095 if (input == NULL)
3096 continue;
3097 fgets(desc, sizeof(desc), input);
3098
3099 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3100 fclose(input);
3101 }
3102}
Len Brown7293fcc2017-02-22 00:11:12 -05003103static void
3104dump_sysfs_pstate_config(void)
3105{
3106 char path[64];
3107 char driver_buf[64];
3108 char governor_buf[64];
3109 FILE *input;
3110 int turbo;
3111
3112 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3113 base_cpu);
3114 input = fopen(path, "r");
3115 if (input == NULL) {
3116 fprintf(stderr, "NSFOD %s\n", path);
3117 return;
3118 }
3119 fgets(driver_buf, sizeof(driver_buf), input);
3120 fclose(input);
3121
3122 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3123 base_cpu);
3124 input = fopen(path, "r");
3125 if (input == NULL) {
3126 fprintf(stderr, "NSFOD %s\n", path);
3127 return;
3128 }
3129 fgets(governor_buf, sizeof(governor_buf), input);
3130 fclose(input);
3131
3132 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3133 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3134
3135 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3136 input = fopen(path, "r");
3137 if (input != NULL) {
3138 fscanf(input, "%d", &turbo);
3139 fprintf(outf, "cpufreq boost: %d\n", turbo);
3140 fclose(input);
3141 }
3142
3143 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3144 input = fopen(path, "r");
3145 if (input != NULL) {
3146 fscanf(input, "%d", &turbo);
3147 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3148 fclose(input);
3149 }
3150}
Len Brown41618e62017-02-09 18:25:22 -05003151
Len Brown6574a5d2012-09-21 00:01:31 -04003152
Len Brown889facb2012-11-08 00:48:57 -05003153/*
3154 * print_epb()
3155 * Decode the ENERGY_PERF_BIAS MSR
3156 */
3157int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3158{
3159 unsigned long long msr;
3160 char *epb_string;
3161 int cpu;
3162
3163 if (!has_epb)
3164 return 0;
3165
3166 cpu = t->cpu_id;
3167
3168 /* EPB is per-package */
3169 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3170 return 0;
3171
3172 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003173 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003174 return -1;
3175 }
3176
3177 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3178 return 0;
3179
Len Browne9be7dd2015-05-26 12:19:37 -04003180 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003181 case ENERGY_PERF_BIAS_PERFORMANCE:
3182 epb_string = "performance";
3183 break;
3184 case ENERGY_PERF_BIAS_NORMAL:
3185 epb_string = "balanced";
3186 break;
3187 case ENERGY_PERF_BIAS_POWERSAVE:
3188 epb_string = "powersave";
3189 break;
3190 default:
3191 epb_string = "custom";
3192 break;
3193 }
Len Brownb7d8c142016-02-13 23:36:17 -05003194 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003195
3196 return 0;
3197}
Len Brown7f5c2582015-12-01 01:36:39 -05003198/*
3199 * print_hwp()
3200 * Decode the MSR_HWP_CAPABILITIES
3201 */
3202int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3203{
3204 unsigned long long msr;
3205 int cpu;
3206
3207 if (!has_hwp)
3208 return 0;
3209
3210 cpu = t->cpu_id;
3211
3212 /* MSR_HWP_CAPABILITIES is per-package */
3213 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3214 return 0;
3215
3216 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003217 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003218 return -1;
3219 }
3220
3221 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3222 return 0;
3223
Len Brownb7d8c142016-02-13 23:36:17 -05003224 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003225 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3226
3227 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3228 if ((msr & (1 << 0)) == 0)
3229 return 0;
3230
3231 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3232 return 0;
3233
Len Brownb7d8c142016-02-13 23:36:17 -05003234 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003235 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003236 cpu, msr,
3237 (unsigned int)HWP_HIGHEST_PERF(msr),
3238 (unsigned int)HWP_GUARANTEED_PERF(msr),
3239 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3240 (unsigned int)HWP_LOWEST_PERF(msr));
3241
3242 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3243 return 0;
3244
Len Brownb7d8c142016-02-13 23:36:17 -05003245 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003246 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003247 cpu, msr,
3248 (unsigned int)(((msr) >> 0) & 0xff),
3249 (unsigned int)(((msr) >> 8) & 0xff),
3250 (unsigned int)(((msr) >> 16) & 0xff),
3251 (unsigned int)(((msr) >> 24) & 0xff),
3252 (unsigned int)(((msr) >> 32) & 0xff3),
3253 (unsigned int)(((msr) >> 42) & 0x1));
3254
3255 if (has_hwp_pkg) {
3256 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3257 return 0;
3258
Len Brownb7d8c142016-02-13 23:36:17 -05003259 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003260 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003261 cpu, msr,
3262 (unsigned int)(((msr) >> 0) & 0xff),
3263 (unsigned int)(((msr) >> 8) & 0xff),
3264 (unsigned int)(((msr) >> 16) & 0xff),
3265 (unsigned int)(((msr) >> 24) & 0xff),
3266 (unsigned int)(((msr) >> 32) & 0xff3));
3267 }
3268 if (has_hwp_notify) {
3269 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3270 return 0;
3271
Len Brownb7d8c142016-02-13 23:36:17 -05003272 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003273 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3274 cpu, msr,
3275 ((msr) & 0x1) ? "EN" : "Dis",
3276 ((msr) & 0x2) ? "EN" : "Dis");
3277 }
3278 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3279 return 0;
3280
Len Brownb7d8c142016-02-13 23:36:17 -05003281 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003282 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3283 cpu, msr,
3284 ((msr) & 0x1) ? "" : "No-",
3285 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003286
3287 return 0;
3288}
3289
Len Brown3a9a9412014-08-15 02:39:52 -04003290/*
3291 * print_perf_limit()
3292 */
3293int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3294{
3295 unsigned long long msr;
3296 int cpu;
3297
3298 cpu = t->cpu_id;
3299
3300 /* per-package */
3301 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3302 return 0;
3303
3304 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003305 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003306 return -1;
3307 }
3308
3309 if (do_core_perf_limit_reasons) {
3310 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003311 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3312 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003313 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003314 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003315 (msr & 1 << 13) ? "Transitions, " : "",
3316 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3317 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3318 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3319 (msr & 1 << 9) ? "CorePwr, " : "",
3320 (msr & 1 << 8) ? "Amps, " : "",
3321 (msr & 1 << 6) ? "VR-Therm, " : "",
3322 (msr & 1 << 5) ? "Auto-HWP, " : "",
3323 (msr & 1 << 4) ? "Graphics, " : "",
3324 (msr & 1 << 2) ? "bit2, " : "",
3325 (msr & 1 << 1) ? "ThermStatus, " : "",
3326 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003327 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 -04003328 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003329 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003330 (msr & 1 << 29) ? "Transitions, " : "",
3331 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3332 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3333 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3334 (msr & 1 << 25) ? "CorePwr, " : "",
3335 (msr & 1 << 24) ? "Amps, " : "",
3336 (msr & 1 << 22) ? "VR-Therm, " : "",
3337 (msr & 1 << 21) ? "Auto-HWP, " : "",
3338 (msr & 1 << 20) ? "Graphics, " : "",
3339 (msr & 1 << 18) ? "bit18, " : "",
3340 (msr & 1 << 17) ? "ThermStatus, " : "",
3341 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003342
3343 }
3344 if (do_gfx_perf_limit_reasons) {
3345 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003346 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3347 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003348 (msr & 1 << 0) ? "PROCHOT, " : "",
3349 (msr & 1 << 1) ? "ThermStatus, " : "",
3350 (msr & 1 << 4) ? "Graphics, " : "",
3351 (msr & 1 << 6) ? "VR-Therm, " : "",
3352 (msr & 1 << 8) ? "Amps, " : "",
3353 (msr & 1 << 9) ? "GFXPwr, " : "",
3354 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3355 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003356 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003357 (msr & 1 << 16) ? "PROCHOT, " : "",
3358 (msr & 1 << 17) ? "ThermStatus, " : "",
3359 (msr & 1 << 20) ? "Graphics, " : "",
3360 (msr & 1 << 22) ? "VR-Therm, " : "",
3361 (msr & 1 << 24) ? "Amps, " : "",
3362 (msr & 1 << 25) ? "GFXPwr, " : "",
3363 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3364 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3365 }
3366 if (do_ring_perf_limit_reasons) {
3367 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003368 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3369 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003370 (msr & 1 << 0) ? "PROCHOT, " : "",
3371 (msr & 1 << 1) ? "ThermStatus, " : "",
3372 (msr & 1 << 6) ? "VR-Therm, " : "",
3373 (msr & 1 << 8) ? "Amps, " : "",
3374 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3375 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003376 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003377 (msr & 1 << 16) ? "PROCHOT, " : "",
3378 (msr & 1 << 17) ? "ThermStatus, " : "",
3379 (msr & 1 << 22) ? "VR-Therm, " : "",
3380 (msr & 1 << 24) ? "Amps, " : "",
3381 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3382 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3383 }
3384 return 0;
3385}
3386
Len Brown889facb2012-11-08 00:48:57 -05003387#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3388#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3389
Colin Ian King1b693172016-03-02 13:50:25 +00003390double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003391{
3392 unsigned long long msr;
3393
3394 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003395 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003396 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3397
3398 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003399 case INTEL_FAM6_ATOM_SILVERMONT1:
3400 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003401 return 30.0;
3402 default:
3403 return 135.0;
3404 }
3405}
3406
Andrey Semin40ee8e32014-12-05 00:07:00 -05003407/*
3408 * rapl_dram_energy_units_probe()
3409 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3410 */
3411static double
3412rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3413{
3414 /* only called for genuine_intel, family 6 */
3415
3416 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003417 case INTEL_FAM6_HASWELL_X: /* HSX */
3418 case INTEL_FAM6_BROADWELL_X: /* BDX */
3419 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3420 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003421 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003422 return (rapl_dram_energy_units = 15.3 / 1000000);
3423 default:
3424 return (rapl_energy_units);
3425 }
3426}
3427
Len Brown144b44b2013-11-09 00:30:16 -05003428
Len Brown889facb2012-11-08 00:48:57 -05003429/*
3430 * rapl_probe()
3431 *
Len Brown144b44b2013-11-09 00:30:16 -05003432 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003433 */
3434void rapl_probe(unsigned int family, unsigned int model)
3435{
3436 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003437 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003438 double tdp;
3439
3440 if (!genuine_intel)
3441 return;
3442
3443 if (family != 6)
3444 return;
3445
3446 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003447 case INTEL_FAM6_SANDYBRIDGE:
3448 case INTEL_FAM6_IVYBRIDGE:
3449 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3450 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3451 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3452 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3453 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003454 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003455 if (rapl_joules) {
3456 BIC_PRESENT(BIC_Pkg_J);
3457 BIC_PRESENT(BIC_Cor_J);
3458 BIC_PRESENT(BIC_GFX_J);
3459 } else {
3460 BIC_PRESENT(BIC_PkgWatt);
3461 BIC_PRESENT(BIC_CorWatt);
3462 BIC_PRESENT(BIC_GFXWatt);
3463 }
Len Brown889facb2012-11-08 00:48:57 -05003464 break;
Len Brown869ce692016-06-16 23:22:37 -04003465 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003466 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003467 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003468 if (rapl_joules)
3469 BIC_PRESENT(BIC_Pkg_J);
3470 else
3471 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003472 break;
Len Brown869ce692016-06-16 23:22:37 -04003473 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3474 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3475 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3476 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown81824922017-03-04 17:23:07 -05003477 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003478 BIC_PRESENT(BIC_PKG__);
3479 BIC_PRESENT(BIC_RAM__);
3480 if (rapl_joules) {
3481 BIC_PRESENT(BIC_Pkg_J);
3482 BIC_PRESENT(BIC_Cor_J);
3483 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003484 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003485 } else {
3486 BIC_PRESENT(BIC_PkgWatt);
3487 BIC_PRESENT(BIC_CorWatt);
3488 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003489 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003490 }
Len Brown0b2bb692015-03-26 00:50:30 -04003491 break;
Len Brown869ce692016-06-16 23:22:37 -04003492 case INTEL_FAM6_HASWELL_X: /* HSX */
3493 case INTEL_FAM6_BROADWELL_X: /* BDX */
3494 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3495 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3496 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003497 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003498 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 -05003499 BIC_PRESENT(BIC_PKG__);
3500 BIC_PRESENT(BIC_RAM__);
3501 if (rapl_joules) {
3502 BIC_PRESENT(BIC_Pkg_J);
3503 BIC_PRESENT(BIC_RAM_J);
3504 } else {
3505 BIC_PRESENT(BIC_PkgWatt);
3506 BIC_PRESENT(BIC_RAMWatt);
3507 }
Len Browne6f9bb32013-12-03 02:19:19 -05003508 break;
Len Brown869ce692016-06-16 23:22:37 -04003509 case INTEL_FAM6_SANDYBRIDGE_X:
3510 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003511 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 -05003512 BIC_PRESENT(BIC_PKG__);
3513 BIC_PRESENT(BIC_RAM__);
3514 if (rapl_joules) {
3515 BIC_PRESENT(BIC_Pkg_J);
3516 BIC_PRESENT(BIC_Cor_J);
3517 BIC_PRESENT(BIC_RAM_J);
3518 } else {
3519 BIC_PRESENT(BIC_PkgWatt);
3520 BIC_PRESENT(BIC_CorWatt);
3521 BIC_PRESENT(BIC_RAMWatt);
3522 }
Len Brown144b44b2013-11-09 00:30:16 -05003523 break;
Len Brown869ce692016-06-16 23:22:37 -04003524 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3525 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003526 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003527 if (rapl_joules) {
3528 BIC_PRESENT(BIC_Pkg_J);
3529 BIC_PRESENT(BIC_Cor_J);
3530 } else {
3531 BIC_PRESENT(BIC_PkgWatt);
3532 BIC_PRESENT(BIC_CorWatt);
3533 }
Len Brown889facb2012-11-08 00:48:57 -05003534 break;
Len Brown869ce692016-06-16 23:22:37 -04003535 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003536 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 -05003537 BIC_PRESENT(BIC_PKG__);
3538 BIC_PRESENT(BIC_RAM__);
3539 if (rapl_joules) {
3540 BIC_PRESENT(BIC_Pkg_J);
3541 BIC_PRESENT(BIC_Cor_J);
3542 BIC_PRESENT(BIC_RAM_J);
3543 } else {
3544 BIC_PRESENT(BIC_PkgWatt);
3545 BIC_PRESENT(BIC_CorWatt);
3546 BIC_PRESENT(BIC_RAMWatt);
3547 }
Jacob Pan0f644902016-06-16 09:48:22 -07003548 break;
Len Brown889facb2012-11-08 00:48:57 -05003549 default:
3550 return;
3551 }
3552
3553 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003554 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003555 return;
3556
3557 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003558 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003559 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3560 else
3561 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003562
Andrey Semin40ee8e32014-12-05 00:07:00 -05003563 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3564
Len Brown144b44b2013-11-09 00:30:16 -05003565 time_unit = msr >> 16 & 0xF;
3566 if (time_unit == 0)
3567 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003568
Len Brown144b44b2013-11-09 00:30:16 -05003569 rapl_time_units = 1.0 / (1 << (time_unit));
3570
3571 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003572
3573 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003574 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003575 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003576
3577 return;
3578}
3579
Colin Ian King1b693172016-03-02 13:50:25 +00003580void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003581{
3582 if (!genuine_intel)
3583 return;
3584
3585 if (family != 6)
3586 return;
3587
3588 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003589 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3590 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3591 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003592 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003593 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003594 do_core_perf_limit_reasons = 1;
3595 do_ring_perf_limit_reasons = 1;
3596 default:
3597 return;
3598 }
3599}
3600
Len Brown889facb2012-11-08 00:48:57 -05003601int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3602{
3603 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003604 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003605 int cpu;
3606
3607 if (!(do_dts || do_ptm))
3608 return 0;
3609
3610 cpu = t->cpu_id;
3611
3612 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003613 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003614 return 0;
3615
3616 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003617 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003618 return -1;
3619 }
3620
3621 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3622 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3623 return 0;
3624
3625 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003626 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003627 cpu, msr, tcc_activation_temp - dts);
3628
Len Brown889facb2012-11-08 00:48:57 -05003629 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3630 return 0;
3631
3632 dts = (msr >> 16) & 0x7F;
3633 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003634 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003635 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003636 }
3637
3638
Len Brownf4896fa52017-03-04 18:10:45 -05003639 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003640 unsigned int resolution;
3641
3642 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3643 return 0;
3644
3645 dts = (msr >> 16) & 0x7F;
3646 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003647 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003648 cpu, msr, tcc_activation_temp - dts, resolution);
3649
Len Brown889facb2012-11-08 00:48:57 -05003650 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3651 return 0;
3652
3653 dts = (msr >> 16) & 0x7F;
3654 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003655 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003656 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003657 }
3658
3659 return 0;
3660}
Len Brown36229892016-02-26 20:51:02 -05003661
Len Brown889facb2012-11-08 00:48:57 -05003662void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3663{
Len Brownb7d8c142016-02-13 23:36:17 -05003664 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003665 cpu, label,
3666 ((msr >> 15) & 1) ? "EN" : "DIS",
3667 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3668 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3669 (((msr >> 16) & 1) ? "EN" : "DIS"));
3670
3671 return;
3672}
3673
3674int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3675{
3676 unsigned long long msr;
3677 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003678
3679 if (!do_rapl)
3680 return 0;
3681
3682 /* RAPL counters are per package, so print only for 1st thread/package */
3683 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3684 return 0;
3685
3686 cpu = t->cpu_id;
3687 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003688 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003689 return -1;
3690 }
3691
3692 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3693 return -1;
3694
Len Brown96e47152017-01-21 02:26:00 -05003695 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3696 rapl_power_units, rapl_energy_units, rapl_time_units);
3697
Len Brown144b44b2013-11-09 00:30:16 -05003698 if (do_rapl & RAPL_PKG_POWER_INFO) {
3699
Len Brown889facb2012-11-08 00:48:57 -05003700 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3701 return -5;
3702
3703
Len Brownb7d8c142016-02-13 23:36:17 -05003704 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 -05003705 cpu, msr,
3706 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3707 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3708 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3709 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3710
Len Brown144b44b2013-11-09 00:30:16 -05003711 }
3712 if (do_rapl & RAPL_PKG) {
3713
Len Brown889facb2012-11-08 00:48:57 -05003714 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3715 return -9;
3716
Len Brownb7d8c142016-02-13 23:36:17 -05003717 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003718 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003719
3720 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003721 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003722 cpu,
3723 ((msr >> 47) & 1) ? "EN" : "DIS",
3724 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3725 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3726 ((msr >> 48) & 1) ? "EN" : "DIS");
3727 }
3728
Len Brown0b2bb692015-03-26 00:50:30 -04003729 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003730 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3731 return -6;
3732
Len Brownb7d8c142016-02-13 23:36:17 -05003733 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 -05003734 cpu, msr,
3735 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3736 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3737 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3738 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003739 }
3740 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003741 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3742 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003743 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003744 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003745
3746 print_power_limit_msr(cpu, msr, "DRAM Limit");
3747 }
Len Brown144b44b2013-11-09 00:30:16 -05003748 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003749 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3750 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003751
Len Brown96e47152017-01-21 02:26:00 -05003752 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003753 }
Jacob Pan91484942016-06-16 09:48:20 -07003754 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003755 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3756 return -9;
3757 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3758 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3759 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003760 }
3761 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003762 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3763 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003764
Len Brown96e47152017-01-21 02:26:00 -05003765 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003766
Len Brown96e47152017-01-21 02:26:00 -05003767 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3768 return -9;
3769 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3770 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3771 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003772 }
3773 return 0;
3774}
3775
Len Brownd7899442015-01-23 00:12:33 -05003776/*
3777 * SNB adds support for additional MSRs:
3778 *
3779 * MSR_PKG_C7_RESIDENCY 0x000003fa
3780 * MSR_CORE_C7_RESIDENCY 0x000003fe
3781 * MSR_PKG_C2_RESIDENCY 0x0000060d
3782 */
Len Brown103a8fe2010-10-22 23:53:03 -04003783
Len Brownd7899442015-01-23 00:12:33 -05003784int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003785{
3786 if (!genuine_intel)
3787 return 0;
3788
3789 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003790 case INTEL_FAM6_SANDYBRIDGE:
3791 case INTEL_FAM6_SANDYBRIDGE_X:
3792 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3793 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3794 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3795 case INTEL_FAM6_HASWELL_X: /* HSW */
3796 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3797 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3798 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3799 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3800 case INTEL_FAM6_BROADWELL_X: /* BDX */
3801 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3802 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3803 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3804 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3805 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3806 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3807 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003808 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003809 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003810 return 1;
3811 }
3812 return 0;
3813}
3814
Len Brownd7899442015-01-23 00:12:33 -05003815/*
3816 * HSW adds support for additional MSRs:
3817 *
Len Brown5a634262016-04-06 17:15:55 -04003818 * MSR_PKG_C8_RESIDENCY 0x00000630
3819 * MSR_PKG_C9_RESIDENCY 0x00000631
3820 * MSR_PKG_C10_RESIDENCY 0x00000632
3821 *
3822 * MSR_PKGC8_IRTL 0x00000633
3823 * MSR_PKGC9_IRTL 0x00000634
3824 * MSR_PKGC10_IRTL 0x00000635
3825 *
Len Brownd7899442015-01-23 00:12:33 -05003826 */
3827int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003828{
3829 if (!genuine_intel)
3830 return 0;
3831
3832 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003833 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3834 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3835 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3836 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3837 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3838 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3839 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003840 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003841 return 1;
3842 }
3843 return 0;
3844}
3845
Len Brown0b2bb692015-03-26 00:50:30 -04003846/*
3847 * SKL adds support for additional MSRS:
3848 *
3849 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3850 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3851 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3852 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3853 */
3854int has_skl_msrs(unsigned int family, unsigned int model)
3855{
3856 if (!genuine_intel)
3857 return 0;
3858
3859 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003860 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3861 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3862 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3863 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003864 return 1;
3865 }
3866 return 0;
3867}
3868
Len Brown144b44b2013-11-09 00:30:16 -05003869int is_slm(unsigned int family, unsigned int model)
3870{
3871 if (!genuine_intel)
3872 return 0;
3873 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003874 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3875 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003876 return 1;
3877 }
3878 return 0;
3879}
3880
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003881int is_knl(unsigned int family, unsigned int model)
3882{
3883 if (!genuine_intel)
3884 return 0;
3885 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003886 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003887 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003888 return 1;
3889 }
3890 return 0;
3891}
3892
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003893unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3894{
3895 if (is_knl(family, model))
3896 return 1024;
3897 return 1;
3898}
3899
Len Brown144b44b2013-11-09 00:30:16 -05003900#define SLM_BCLK_FREQS 5
3901double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3902
3903double slm_bclk(void)
3904{
3905 unsigned long long msr = 3;
3906 unsigned int i;
3907 double freq;
3908
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003909 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003910 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003911
3912 i = msr & 0xf;
3913 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003914 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003915 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003916 }
3917 freq = slm_freq_table[i];
3918
Len Brown96e47152017-01-21 02:26:00 -05003919 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003920 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003921
3922 return freq;
3923}
3924
Len Brown103a8fe2010-10-22 23:53:03 -04003925double discover_bclk(unsigned int family, unsigned int model)
3926{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003927 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003928 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003929 else if (is_slm(family, model))
3930 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003931 else
3932 return 133.33;
3933}
3934
Len Brown889facb2012-11-08 00:48:57 -05003935/*
3936 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3937 * the Thermal Control Circuit (TCC) activates.
3938 * This is usually equal to tjMax.
3939 *
3940 * Older processors do not have this MSR, so there we guess,
3941 * but also allow cmdline over-ride with -T.
3942 *
3943 * Several MSR temperature values are in units of degrees-C
3944 * below this value, including the Digital Thermal Sensor (DTS),
3945 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3946 */
3947int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3948{
3949 unsigned long long msr;
3950 unsigned int target_c_local;
3951 int cpu;
3952
3953 /* tcc_activation_temp is used only for dts or ptm */
3954 if (!(do_dts || do_ptm))
3955 return 0;
3956
3957 /* this is a per-package concept */
3958 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3959 return 0;
3960
3961 cpu = t->cpu_id;
3962 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003963 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003964 return -1;
3965 }
3966
3967 if (tcc_activation_temp_override != 0) {
3968 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003969 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003970 cpu, tcc_activation_temp);
3971 return 0;
3972 }
3973
3974 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003975 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003976 goto guess;
3977
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003978 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003979 goto guess;
3980
Jean Delvare34821242014-05-01 11:40:19 +02003981 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003982
Len Brown96e47152017-01-21 02:26:00 -05003983 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003984 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003985 cpu, msr, target_c_local);
3986
Jean Delvare34821242014-05-01 11:40:19 +02003987 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003988 goto guess;
3989
3990 tcc_activation_temp = target_c_local;
3991
3992 return 0;
3993
3994guess:
3995 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003996 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003997 cpu, tcc_activation_temp);
3998
3999 return 0;
4000}
Len Brown69807a62015-11-21 12:22:47 -05004001
Len Brownaa8d8cc2016-03-11 13:26:03 -05004002void decode_feature_control_msr(void)
4003{
4004 unsigned long long msr;
4005
4006 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4007 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4008 base_cpu, msr,
4009 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4010 msr & (1 << 18) ? "SGX" : "");
4011}
4012
Len Brown69807a62015-11-21 12:22:47 -05004013void decode_misc_enable_msr(void)
4014{
4015 unsigned long long msr;
4016
Len Brownf26b1512017-06-23 20:45:54 -07004017 if (!genuine_intel)
4018 return;
4019
Len Brown69807a62015-11-21 12:22:47 -05004020 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004021 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004022 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004023 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4024 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4025 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
4026 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4027 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004028}
4029
Len Brown33148d62017-01-21 01:26:16 -05004030void decode_misc_feature_control(void)
4031{
4032 unsigned long long msr;
4033
4034 if (!has_misc_feature_control)
4035 return;
4036
4037 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4038 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4039 base_cpu, msr,
4040 msr & (0 << 0) ? "No-" : "",
4041 msr & (1 << 0) ? "No-" : "",
4042 msr & (2 << 0) ? "No-" : "",
4043 msr & (3 << 0) ? "No-" : "");
4044}
Len Brownf0057312015-12-03 01:35:36 -05004045/*
4046 * Decode MSR_MISC_PWR_MGMT
4047 *
4048 * Decode the bits according to the Nehalem documentation
4049 * bit[0] seems to continue to have same meaning going forward
4050 * bit[1] less so...
4051 */
4052void decode_misc_pwr_mgmt_msr(void)
4053{
4054 unsigned long long msr;
4055
4056 if (!do_nhm_platform_info)
4057 return;
4058
Len Browncf4cbe52017-01-01 13:08:33 -05004059 if (no_MSR_MISC_PWR_MGMT)
4060 return;
4061
Len Brownf0057312015-12-03 01:35:36 -05004062 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004063 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 -05004064 base_cpu, msr,
4065 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004066 msr & (1 << 1) ? "EN" : "DIS",
4067 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004068}
Len Brown71616c82017-01-07 22:37:48 -05004069/*
4070 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4071 *
4072 * This MSRs are present on Silvermont processors,
4073 * Intel Atom processor E3000 series (Baytrail), and friends.
4074 */
4075void decode_c6_demotion_policy_msr(void)
4076{
4077 unsigned long long msr;
4078
4079 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4080 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4081 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4082
4083 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4084 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4085 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4086}
Len Brown7f5c2582015-12-01 01:36:39 -05004087
Len Brownfcd17212015-03-23 20:29:09 -04004088void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004089{
Len Brown61a87ba2015-11-23 02:30:51 -05004090 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004091 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004092 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004093
4094 eax = ebx = ecx = edx = 0;
4095
Len Brown5aea2f72016-03-13 03:14:35 -04004096 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004097
4098 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4099 genuine_intel = 1;
4100
Len Brown96e47152017-01-21 02:26:00 -05004101 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004102 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004103 (char *)&ebx, (char *)&edx, (char *)&ecx);
4104
Len Brown5aea2f72016-03-13 03:14:35 -04004105 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004106 family = (fms >> 8) & 0xf;
4107 model = (fms >> 4) & 0xf;
4108 stepping = fms & 0xf;
4109 if (family == 6 || family == 0xf)
4110 model += ((fms >> 16) & 0xf) << 4;
4111
Len Brown96e47152017-01-21 02:26:00 -05004112 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004113 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004114 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004115 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004116 ecx & (1 << 0) ? "SSE3" : "-",
4117 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004118 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004119 ecx & (1 << 7) ? "EIST" : "-",
4120 ecx & (1 << 8) ? "TM2" : "-",
4121 edx & (1 << 4) ? "TSC" : "-",
4122 edx & (1 << 5) ? "MSR" : "-",
4123 edx & (1 << 22) ? "ACPI-TM" : "-",
4124 edx & (1 << 29) ? "TM" : "-");
4125 }
Len Brown103a8fe2010-10-22 23:53:03 -04004126
Josh Triplettb2c95d92013-08-20 17:20:18 -07004127 if (!(edx & (1 << 5)))
4128 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004129
4130 /*
4131 * check max extended function levels of CPUID.
4132 * This is needed to check for invariant TSC.
4133 * This check is valid for both Intel and AMD.
4134 */
4135 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004136 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004137
Len Brown61a87ba2015-11-23 02:30:51 -05004138 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004139
Len Brownd7899442015-01-23 00:12:33 -05004140 /*
4141 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4142 * this check is valid for both Intel and AMD
4143 */
Len Brown5aea2f72016-03-13 03:14:35 -04004144 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004145 has_invariant_tsc = edx & (1 << 8);
4146 }
Len Brown103a8fe2010-10-22 23:53:03 -04004147
4148 /*
4149 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4150 * this check is valid for both Intel and AMD
4151 */
4152
Len Brown5aea2f72016-03-13 03:14:35 -04004153 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004154 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004155 if (has_aperf) {
4156 BIC_PRESENT(BIC_Avg_MHz);
4157 BIC_PRESENT(BIC_Busy);
4158 BIC_PRESENT(BIC_Bzy_MHz);
4159 }
Len Brown889facb2012-11-08 00:48:57 -05004160 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004161 if (do_dts)
4162 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004163 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004164 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004165 if (do_ptm)
4166 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004167 has_hwp = eax & (1 << 7);
4168 has_hwp_notify = eax & (1 << 8);
4169 has_hwp_activity_window = eax & (1 << 9);
4170 has_hwp_epp = eax & (1 << 10);
4171 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004172 has_epb = ecx & (1 << 3);
4173
Len Brown96e47152017-01-21 02:26:00 -05004174 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004175 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004176 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4177 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004178 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004179 do_dts ? "" : "No-",
4180 do_ptm ? "" : "No-",
4181 has_hwp ? "" : "No-",
4182 has_hwp_notify ? "" : "No-",
4183 has_hwp_activity_window ? "" : "No-",
4184 has_hwp_epp ? "" : "No-",
4185 has_hwp_pkg ? "" : "No-",
4186 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004187
Len Brown96e47152017-01-21 02:26:00 -05004188 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004189 decode_misc_enable_msr();
4190
Len Brown33148d62017-01-21 01:26:16 -05004191
Len Brown96e47152017-01-21 02:26:00 -05004192 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004193 int has_sgx;
4194
4195 ecx = 0;
4196
4197 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4198
4199 has_sgx = ebx & (1 << 2);
4200 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4201
4202 if (has_sgx)
4203 decode_feature_control_msr();
4204 }
4205
Len Brown61a87ba2015-11-23 02:30:51 -05004206 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004207 unsigned int eax_crystal;
4208 unsigned int ebx_tsc;
4209
4210 /*
4211 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4212 */
4213 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004214 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004215
4216 if (ebx_tsc != 0) {
4217
Len Brown96e47152017-01-21 02:26:00 -05004218 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004219 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004220 eax_crystal, ebx_tsc, crystal_hz);
4221
4222 if (crystal_hz == 0)
4223 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004224 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4225 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4226 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4227 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004228 crystal_hz = 24000000; /* 24.0 MHz */
4229 break;
Len Brown869ce692016-06-16 23:22:37 -04004230 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05004231 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004232 crystal_hz = 25000000; /* 25.0 MHz */
4233 break;
Len Brown869ce692016-06-16 23:22:37 -04004234 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004235 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004236 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004237 break;
4238 default:
4239 crystal_hz = 0;
4240 }
4241
4242 if (crystal_hz) {
4243 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004244 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004245 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004246 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4247 }
4248 }
4249 }
Len Brown61a87ba2015-11-23 02:30:51 -05004250 if (max_level >= 0x16) {
4251 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4252
4253 /*
4254 * CPUID 16H Base MHz, Max MHz, Bus MHz
4255 */
4256 base_mhz = max_mhz = bus_mhz = edx = 0;
4257
Len Brown5aea2f72016-03-13 03:14:35 -04004258 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004259 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004260 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004261 base_mhz, max_mhz, bus_mhz);
4262 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004263
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004264 if (has_aperf)
4265 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4266
Len Brown812db3f2017-02-10 00:25:41 -05004267 BIC_PRESENT(BIC_IRQ);
4268 BIC_PRESENT(BIC_TSC_MHz);
4269
4270 if (probe_nhm_msrs(family, model)) {
4271 do_nhm_platform_info = 1;
4272 BIC_PRESENT(BIC_CPU_c1);
4273 BIC_PRESENT(BIC_CPU_c3);
4274 BIC_PRESENT(BIC_CPU_c6);
4275 BIC_PRESENT(BIC_SMI);
4276 }
Len Brownd7899442015-01-23 00:12:33 -05004277 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004278
4279 if (do_snb_cstates)
4280 BIC_PRESENT(BIC_CPU_c7);
4281
Len Brown5a634262016-04-06 17:15:55 -04004282 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004283 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4284 BIC_PRESENT(BIC_Pkgpc2);
4285 if (pkg_cstate_limit >= PCL__3)
4286 BIC_PRESENT(BIC_Pkgpc3);
4287 if (pkg_cstate_limit >= PCL__6)
4288 BIC_PRESENT(BIC_Pkgpc6);
4289 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4290 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004291 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004292 BIC_NOT_PRESENT(BIC_Pkgpc2);
4293 BIC_NOT_PRESENT(BIC_Pkgpc3);
4294 BIC_PRESENT(BIC_Pkgpc6);
4295 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004296 BIC_PRESENT(BIC_Mod_c6);
4297 use_c1_residency_msr = 1;
4298 }
Len Brown7170a372017-01-27 02:13:27 -05004299 if (is_dnv(family, model)) {
4300 BIC_PRESENT(BIC_CPU_c1);
4301 BIC_NOT_PRESENT(BIC_CPU_c3);
4302 BIC_NOT_PRESENT(BIC_Pkgpc3);
4303 BIC_NOT_PRESENT(BIC_CPU_c7);
4304 BIC_NOT_PRESENT(BIC_Pkgpc7);
4305 use_c1_residency_msr = 1;
4306 }
Len Brown34c761972017-01-27 02:36:41 -05004307 if (is_skx(family, model)) {
4308 BIC_NOT_PRESENT(BIC_CPU_c3);
4309 BIC_NOT_PRESENT(BIC_Pkgpc3);
4310 BIC_NOT_PRESENT(BIC_CPU_c7);
4311 BIC_NOT_PRESENT(BIC_Pkgpc7);
4312 }
Len Brownade0eba2017-02-10 01:56:47 -05004313 if (is_bdx(family, model)) {
4314 BIC_NOT_PRESENT(BIC_CPU_c7);
4315 BIC_NOT_PRESENT(BIC_Pkgpc7);
4316 }
Len Brown0f47c082017-01-27 00:50:45 -05004317 if (has_hsw_msrs(family, model)) {
4318 BIC_PRESENT(BIC_Pkgpc8);
4319 BIC_PRESENT(BIC_Pkgpc9);
4320 BIC_PRESENT(BIC_Pkgpc10);
4321 }
Len Brown5a634262016-04-06 17:15:55 -04004322 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004323 if (has_skl_msrs(family, model)) {
4324 BIC_PRESENT(BIC_Totl_c0);
4325 BIC_PRESENT(BIC_Any_c0);
4326 BIC_PRESENT(BIC_GFX_c0);
4327 BIC_PRESENT(BIC_CPUGFX);
4328 }
Len Brown144b44b2013-11-09 00:30:16 -05004329 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004330 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004331
Len Brown96e47152017-01-21 02:26:00 -05004332 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004333 decode_misc_pwr_mgmt_msr();
4334
Len Brown96e47152017-01-21 02:26:00 -05004335 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004336 decode_c6_demotion_policy_msr();
4337
Len Brown889facb2012-11-08 00:48:57 -05004338 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004339 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004340
Len Brown96e47152017-01-21 02:26:00 -05004341 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004342 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004343
Len Brown41618e62017-02-09 18:25:22 -05004344 if (!quiet)
4345 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004346 if (!quiet)
4347 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004348
Len Browna2b7b742015-09-26 00:12:38 -04004349 if (has_skl_msrs(family, model))
4350 calculate_tsc_tweak();
4351
Len Brown812db3f2017-02-10 00:25:41 -05004352 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4353 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004354
Len Brown812db3f2017-02-10 00:25:41 -05004355 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4356 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004357
Len Brown96e47152017-01-21 02:26:00 -05004358 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004359 decode_misc_feature_control();
4360
Len Brown889facb2012-11-08 00:48:57 -05004361 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004362}
4363
Len Brown103a8fe2010-10-22 23:53:03 -04004364
4365/*
4366 * in /dev/cpu/ return success for names that are numbers
4367 * ie. filter out ".", "..", "microcode".
4368 */
4369int dir_filter(const struct dirent *dirp)
4370{
4371 if (isdigit(dirp->d_name[0]))
4372 return 1;
4373 else
4374 return 0;
4375}
4376
4377int open_dev_cpu_msr(int dummy1)
4378{
4379 return 0;
4380}
4381
Len Brownc98d5d92012-06-04 00:56:40 -04004382void topology_probe()
4383{
4384 int i;
4385 int max_core_id = 0;
4386 int max_package_id = 0;
4387 int max_siblings = 0;
4388 struct cpu_topology {
4389 int core_id;
4390 int physical_package_id;
4391 } *cpus;
4392
4393 /* Initialize num_cpus, max_cpu_num */
4394 topo.num_cpus = 0;
4395 topo.max_cpu_num = 0;
4396 for_all_proc_cpus(count_cpus);
4397 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004398 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004399
Len Brownd8af6f52015-02-10 01:56:38 -05004400 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004401 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004402
4403 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004404 if (cpus == NULL)
4405 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004406
4407 /*
4408 * Allocate and initialize cpu_present_set
4409 */
4410 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004411 if (cpu_present_set == NULL)
4412 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004413 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4414 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4415 for_all_proc_cpus(mark_cpu_present);
4416
4417 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004418 * Validate that all cpus in cpu_subset are also in cpu_present_set
4419 */
4420 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4421 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4422 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4423 err(1, "cpu%d not present", i);
4424 }
4425
4426 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004427 * Allocate and initialize cpu_affinity_set
4428 */
4429 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004430 if (cpu_affinity_set == NULL)
4431 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004432 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4433 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4434
4435
4436 /*
4437 * For online cpus
4438 * find max_core_id, max_package_id
4439 */
4440 for (i = 0; i <= topo.max_cpu_num; ++i) {
4441 int siblings;
4442
4443 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004444 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004445 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004446 continue;
4447 }
4448 cpus[i].core_id = get_core_id(i);
4449 if (cpus[i].core_id > max_core_id)
4450 max_core_id = cpus[i].core_id;
4451
4452 cpus[i].physical_package_id = get_physical_package_id(i);
4453 if (cpus[i].physical_package_id > max_package_id)
4454 max_package_id = cpus[i].physical_package_id;
4455
4456 siblings = get_num_ht_siblings(i);
4457 if (siblings > max_siblings)
4458 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004459 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004460 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004461 i, cpus[i].physical_package_id, cpus[i].core_id);
4462 }
4463 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004464 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004465 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004466 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004467 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004468 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004469
4470 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004471 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004472 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004473 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004474 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004475 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004476
4477 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004478 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004479 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004480
4481 free(cpus);
4482}
4483
4484void
4485allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4486{
4487 int i;
4488
4489 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004490 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004491 if (*t == NULL)
4492 goto error;
4493
4494 for (i = 0; i < topo.num_threads_per_core *
4495 topo.num_cores_per_pkg * topo.num_packages; i++)
4496 (*t)[i].cpu_id = -1;
4497
4498 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004499 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004500 if (*c == NULL)
4501 goto error;
4502
4503 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4504 (*c)[i].core_id = -1;
4505
Len Brown678a3bd2017-02-09 22:22:13 -05004506 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004507 if (*p == NULL)
4508 goto error;
4509
4510 for (i = 0; i < topo.num_packages; i++)
4511 (*p)[i].package_id = i;
4512
4513 return;
4514error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004515 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004516}
4517/*
4518 * init_counter()
4519 *
4520 * set cpu_id, core_num, pkg_num
4521 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4522 *
4523 * increment topo.num_cores when 1st core in pkg seen
4524 */
4525void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4526 struct pkg_data *pkg_base, int thread_num, int core_num,
4527 int pkg_num, int cpu_id)
4528{
4529 struct thread_data *t;
4530 struct core_data *c;
4531 struct pkg_data *p;
4532
4533 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4534 c = GET_CORE(core_base, core_num, pkg_num);
4535 p = GET_PKG(pkg_base, pkg_num);
4536
4537 t->cpu_id = cpu_id;
4538 if (thread_num == 0) {
4539 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4540 if (cpu_is_first_core_in_package(cpu_id))
4541 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4542 }
4543
4544 c->core_id = core_num;
4545 p->package_id = pkg_num;
4546}
4547
4548
4549int initialize_counters(int cpu_id)
4550{
4551 int my_thread_id, my_core_id, my_package_id;
4552
4553 my_package_id = get_physical_package_id(cpu_id);
4554 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004555 my_thread_id = get_cpu_position_in_core(cpu_id);
4556 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004557 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004558
4559 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4560 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4561 return 0;
4562}
4563
4564void allocate_output_buffer()
4565{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004566 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004567 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004568 if (outp == NULL)
4569 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004570}
Len Brown36229892016-02-26 20:51:02 -05004571void allocate_fd_percpu(void)
4572{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004573 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004574 if (fd_percpu == NULL)
4575 err(-1, "calloc fd_percpu");
4576}
Len Brown562a2d32016-02-26 23:48:05 -05004577void allocate_irq_buffers(void)
4578{
4579 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4580 if (irq_column_2_cpu == NULL)
4581 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004582
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004583 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004584 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004585 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004586}
Len Brownc98d5d92012-06-04 00:56:40 -04004587void setup_all_buffers(void)
4588{
4589 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004590 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004591 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004592 allocate_counters(&thread_even, &core_even, &package_even);
4593 allocate_counters(&thread_odd, &core_odd, &package_odd);
4594 allocate_output_buffer();
4595 for_all_proc_cpus(initialize_counters);
4596}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004597
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004598void set_base_cpu(void)
4599{
4600 base_cpu = sched_getcpu();
4601 if (base_cpu < 0)
4602 err(-ENODEV, "No valid cpus found");
4603
4604 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004605 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004606}
4607
Len Brown103a8fe2010-10-22 23:53:03 -04004608void turbostat_init()
4609{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004610 setup_all_buffers();
4611 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004612 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004613 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004614 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004615
Len Brown103a8fe2010-10-22 23:53:03 -04004616
Len Brown96e47152017-01-21 02:26:00 -05004617 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004618 for_all_cpus(print_hwp, ODD_COUNTERS);
4619
Len Brown96e47152017-01-21 02:26:00 -05004620 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004621 for_all_cpus(print_epb, ODD_COUNTERS);
4622
Len Brown96e47152017-01-21 02:26:00 -05004623 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004624 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4625
Len Brown96e47152017-01-21 02:26:00 -05004626 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004627 for_all_cpus(print_rapl, ODD_COUNTERS);
4628
4629 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4630
Len Brown96e47152017-01-21 02:26:00 -05004631 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004632 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004633
Len Brown96e47152017-01-21 02:26:00 -05004634 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004635 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004636}
4637
4638int fork_it(char **argv)
4639{
Len Brown103a8fe2010-10-22 23:53:03 -04004640 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004641 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004642
Len Brown218f0e82017-02-14 22:07:52 -05004643 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004644 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4645 if (status)
4646 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004647 /* clear affinity side-effect of get_counters() */
4648 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004649 gettimeofday(&tv_even, (struct timezone *)NULL);
4650
4651 child_pid = fork();
4652 if (!child_pid) {
4653 /* child */
4654 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004655 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004656 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004657
4658 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004659 if (child_pid == -1)
4660 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004661
4662 signal(SIGINT, SIG_IGN);
4663 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004664 if (waitpid(child_pid, &status, 0) == -1)
4665 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004666 }
Len Brownc98d5d92012-06-04 00:56:40 -04004667 /*
4668 * n.b. fork_it() does not check for errors from for_all_cpus()
4669 * because re-starting is problematic when forking
4670 */
Len Brown218f0e82017-02-14 22:07:52 -05004671 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004672 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004673 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004674 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004675 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4676 fprintf(outf, "%s: Counter reset detected\n", progname);
4677 else {
4678 compute_average(EVEN_COUNTERS);
4679 format_all_counters(EVEN_COUNTERS);
4680 }
Len Brown103a8fe2010-10-22 23:53:03 -04004681
Len Brownb7d8c142016-02-13 23:36:17 -05004682 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4683
4684 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004685
Len Brownd91bb172012-11-01 00:08:19 -04004686 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004687}
4688
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004689int get_and_dump_counters(void)
4690{
4691 int status;
4692
Len Brown218f0e82017-02-14 22:07:52 -05004693 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004694 status = for_all_cpus(get_counters, ODD_COUNTERS);
4695 if (status)
4696 return status;
4697
4698 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4699 if (status)
4700 return status;
4701
Len Brownb7d8c142016-02-13 23:36:17 -05004702 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004703
4704 return status;
4705}
4706
Len Brownd8af6f52015-02-10 01:56:38 -05004707void print_version() {
Len Brownf7d44a82017-05-27 21:24:58 -07004708 fprintf(outf, "turbostat version 17.06.23"
Len Brownd8af6f52015-02-10 01:56:38 -05004709 " - Len Brown <lenb@kernel.org>\n");
4710}
4711
Len Brown495c76542017-02-08 02:41:51 -05004712int add_counter(unsigned int msr_num, char *path, char *name,
4713 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004714 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004715{
4716 struct msr_counter *msrp;
4717
4718 msrp = calloc(1, sizeof(struct msr_counter));
4719 if (msrp == NULL) {
4720 perror("calloc");
4721 exit(1);
4722 }
4723
4724 msrp->msr_num = msr_num;
4725 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004726 if (path)
4727 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004728 msrp->width = width;
4729 msrp->type = type;
4730 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004731 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004732
4733 switch (scope) {
4734
4735 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004736 msrp->next = sys.tp;
4737 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004738 sys.added_thread_counters++;
4739 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4740 fprintf(stderr, "exceeded max %d added thread counters\n",
4741 MAX_ADDED_COUNTERS);
4742 exit(-1);
4743 }
Len Brown388e9c82016-12-22 23:57:55 -05004744 break;
4745
4746 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004747 msrp->next = sys.cp;
4748 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004749 sys.added_core_counters++;
4750 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4751 fprintf(stderr, "exceeded max %d added core counters\n",
4752 MAX_ADDED_COUNTERS);
4753 exit(-1);
4754 }
Len Brown388e9c82016-12-22 23:57:55 -05004755 break;
4756
4757 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004758 msrp->next = sys.pp;
4759 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004760 sys.added_package_counters++;
4761 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4762 fprintf(stderr, "exceeded max %d added package counters\n",
4763 MAX_ADDED_COUNTERS);
4764 exit(-1);
4765 }
Len Brown388e9c82016-12-22 23:57:55 -05004766 break;
4767 }
4768
4769 return 0;
4770}
4771
4772void parse_add_command(char *add_command)
4773{
4774 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004775 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004776 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004777 int width = 64;
4778 int fail = 0;
4779 enum counter_scope scope = SCOPE_CPU;
4780 enum counter_type type = COUNTER_CYCLES;
4781 enum counter_format format = FORMAT_DELTA;
4782
4783 while (add_command) {
4784
4785 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4786 goto next;
4787
4788 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4789 goto next;
4790
Len Brown495c76542017-02-08 02:41:51 -05004791 if (*add_command == '/') {
4792 path = add_command;
4793 goto next;
4794 }
4795
Len Brown388e9c82016-12-22 23:57:55 -05004796 if (sscanf(add_command, "u%d", &width) == 1) {
4797 if ((width == 32) || (width == 64))
4798 goto next;
4799 width = 64;
4800 }
4801 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4802 scope = SCOPE_CPU;
4803 goto next;
4804 }
4805 if (!strncmp(add_command, "core", strlen("core"))) {
4806 scope = SCOPE_CORE;
4807 goto next;
4808 }
4809 if (!strncmp(add_command, "package", strlen("package"))) {
4810 scope = SCOPE_PACKAGE;
4811 goto next;
4812 }
4813 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4814 type = COUNTER_CYCLES;
4815 goto next;
4816 }
4817 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4818 type = COUNTER_SECONDS;
4819 goto next;
4820 }
Len Brown41618e62017-02-09 18:25:22 -05004821 if (!strncmp(add_command, "usec", strlen("usec"))) {
4822 type = COUNTER_USEC;
4823 goto next;
4824 }
Len Brown388e9c82016-12-22 23:57:55 -05004825 if (!strncmp(add_command, "raw", strlen("raw"))) {
4826 format = FORMAT_RAW;
4827 goto next;
4828 }
4829 if (!strncmp(add_command, "delta", strlen("delta"))) {
4830 format = FORMAT_DELTA;
4831 goto next;
4832 }
4833 if (!strncmp(add_command, "percent", strlen("percent"))) {
4834 format = FORMAT_PERCENT;
4835 goto next;
4836 }
4837
4838 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4839 char *eos;
4840
4841 eos = strchr(name_buffer, ',');
4842 if (eos)
4843 *eos = '\0';
4844 goto next;
4845 }
4846
4847next:
4848 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004849 if (add_command) {
4850 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004851 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004852 }
Len Brown388e9c82016-12-22 23:57:55 -05004853
4854 }
Len Brown495c76542017-02-08 02:41:51 -05004855 if ((msr_num == 0) && (path == NULL)) {
4856 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004857 fail++;
4858 }
4859
4860 /* generate default column header */
4861 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05004862 if (width == 32)
4863 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4864 else
4865 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05004866 }
4867
Len Brown41618e62017-02-09 18:25:22 -05004868 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05004869 fail++;
4870
4871 if (fail) {
4872 help();
4873 exit(1);
4874 }
4875}
Len Brown41618e62017-02-09 18:25:22 -05004876
Len Browndd778a52017-02-21 23:21:13 -05004877int is_deferred_skip(char *name)
4878{
4879 int i;
4880
4881 for (i = 0; i < deferred_skip_index; ++i)
4882 if (!strcmp(name, deferred_skip_names[i]))
4883 return 1;
4884 return 0;
4885}
4886
Len Brown41618e62017-02-09 18:25:22 -05004887void probe_sysfs(void)
4888{
4889 char path[64];
4890 char name_buf[16];
4891 FILE *input;
4892 int state;
4893 char *sp;
4894
4895 if (!DO_BIC(BIC_sysfs))
4896 return;
4897
4898 for (state = 10; state > 0; --state) {
4899
4900 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4901 base_cpu, state);
4902 input = fopen(path, "r");
4903 if (input == NULL)
4904 continue;
4905 fgets(name_buf, sizeof(name_buf), input);
4906
4907 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4908 sp = strchr(name_buf, '-');
4909 if (!sp)
4910 sp = strchrnul(name_buf, '\n');
4911 *sp = '%';
4912 *(sp + 1) = '\0';
4913
4914 fclose(input);
4915
4916 sprintf(path, "cpuidle/state%d/time", state);
4917
Len Browndd778a52017-02-21 23:21:13 -05004918 if (is_deferred_skip(name_buf))
4919 continue;
4920
Len Brown41618e62017-02-09 18:25:22 -05004921 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4922 FORMAT_PERCENT, SYSFS_PERCPU);
4923 }
4924
4925 for (state = 10; state > 0; --state) {
4926
4927 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4928 base_cpu, state);
4929 input = fopen(path, "r");
4930 if (input == NULL)
4931 continue;
4932 fgets(name_buf, sizeof(name_buf), input);
4933 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4934 sp = strchr(name_buf, '-');
4935 if (!sp)
4936 sp = strchrnul(name_buf, '\n');
4937 *sp = '\0';
4938 fclose(input);
4939
4940 sprintf(path, "cpuidle/state%d/usage", state);
4941
Len Browndd778a52017-02-21 23:21:13 -05004942 if (is_deferred_skip(name_buf))
4943 continue;
4944
Len Brown41618e62017-02-09 18:25:22 -05004945 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4946 FORMAT_DELTA, SYSFS_PERCPU);
4947 }
4948
4949}
4950
Len Brown1ef7d212017-02-10 23:54:15 -05004951
4952/*
4953 * parse cpuset with following syntax
4954 * 1,2,4..6,8-10 and set bits in cpu_subset
4955 */
4956void parse_cpu_command(char *optarg)
4957{
4958 unsigned int start, end;
4959 char *next;
4960
Len Brown4e4e1e72017-02-21 22:33:42 -05004961 if (!strcmp(optarg, "core")) {
4962 if (cpu_subset)
4963 goto error;
4964 show_core_only++;
4965 return;
4966 }
4967 if (!strcmp(optarg, "package")) {
4968 if (cpu_subset)
4969 goto error;
4970 show_pkg_only++;
4971 return;
4972 }
4973 if (show_core_only || show_pkg_only)
4974 goto error;
4975
Len Brown1ef7d212017-02-10 23:54:15 -05004976 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
4977 if (cpu_subset == NULL)
4978 err(3, "CPU_ALLOC");
4979 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
4980
4981 CPU_ZERO_S(cpu_subset_size, cpu_subset);
4982
4983 next = optarg;
4984
4985 while (next && *next) {
4986
4987 if (*next == '-') /* no negative cpu numbers */
4988 goto error;
4989
4990 start = strtoul(next, &next, 10);
4991
4992 if (start >= CPU_SUBSET_MAXCPUS)
4993 goto error;
4994 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4995
4996 if (*next == '\0')
4997 break;
4998
4999 if (*next == ',') {
5000 next += 1;
5001 continue;
5002 }
5003
5004 if (*next == '-') {
5005 next += 1; /* start range */
5006 } else if (*next == '.') {
5007 next += 1;
5008 if (*next == '.')
5009 next += 1; /* start range */
5010 else
5011 goto error;
5012 }
5013
5014 end = strtoul(next, &next, 10);
5015 if (end <= start)
5016 goto error;
5017
5018 while (++start <= end) {
5019 if (start >= CPU_SUBSET_MAXCPUS)
5020 goto error;
5021 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5022 }
5023
5024 if (*next == ',')
5025 next += 1;
5026 else if (*next != '\0')
5027 goto error;
5028 }
5029
5030 return;
5031
5032error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005033 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5034 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005035 exit(-1);
5036}
5037
Len Brown812db3f2017-02-10 00:25:41 -05005038
Len Brown103a8fe2010-10-22 23:53:03 -04005039void cmdline(int argc, char **argv)
5040{
5041 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005042 int option_index = 0;
5043 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005044 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005045 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005046 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005047 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005048 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005049 {"interval", required_argument, 0, 'i'},
5050 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005051 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005052 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005053 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005054 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005055 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005056 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005057 {"Summary", no_argument, 0, 'S'},
5058 {"TCC", required_argument, 0, 'T'},
5059 {"version", no_argument, 0, 'v' },
5060 {0, 0, 0, 0 }
5061 };
Len Brown103a8fe2010-10-22 23:53:03 -04005062
5063 progname = argv[0];
5064
Len Brown3f44a5c2017-10-17 15:42:56 -04005065 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jo:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005066 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005067 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005068 case 'a':
5069 parse_add_command(optarg);
5070 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005071 case 'c':
5072 parse_cpu_command(optarg);
5073 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005074 case 'D':
5075 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005076 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005077 case 'e':
5078 /* --enable specified counter */
5079 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5080 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005081 case 'd':
5082 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005083 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005084 break;
Len Brown812db3f2017-02-10 00:25:41 -05005085 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005086 /*
5087 * --hide: do not show those specified
5088 * multiple invocations simply clear more bits in enabled mask
5089 */
5090 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005091 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005092 case 'h':
5093 default:
5094 help();
5095 exit(1);
5096 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005097 {
5098 double interval = strtod(optarg, NULL);
5099
5100 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005101 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005102 interval);
5103 exit(2);
5104 }
5105
5106 interval_ts.tv_sec = interval;
5107 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
5108 }
Len Brown889facb2012-11-08 00:48:57 -05005109 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005110 case 'J':
5111 rapl_joules++;
5112 break;
Len Brownc8ade362017-02-15 17:15:11 -05005113 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005114 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005115 list_header_only++;
5116 quiet++;
5117 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005118 case 'o':
5119 outf = fopen_or_die(optarg, "w");
5120 break;
Len Brown96e47152017-01-21 02:26:00 -05005121 case 'q':
5122 quiet = 1;
5123 break;
Len Brown812db3f2017-02-10 00:25:41 -05005124 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005125 /*
5126 * --show: show only those specified
5127 * The 1st invocation will clear and replace the enabled mask
5128 * subsequent invocations can add to it.
5129 */
5130 if (shown == 0)
5131 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5132 else
5133 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5134 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005135 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005136 case 'S':
5137 summary_only++;
5138 break;
5139 case 'T':
5140 tcc_activation_temp_override = atoi(optarg);
5141 break;
5142 case 'v':
5143 print_version();
5144 exit(0);
5145 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005146 }
5147 }
5148}
5149
5150int main(int argc, char **argv)
5151{
Len Brownb7d8c142016-02-13 23:36:17 -05005152 outf = stderr;
5153
Len Brown103a8fe2010-10-22 23:53:03 -04005154 cmdline(argc, argv);
5155
Len Brown96e47152017-01-21 02:26:00 -05005156 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005157 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005158
Len Brown41618e62017-02-09 18:25:22 -05005159 probe_sysfs();
5160
Len Brown103a8fe2010-10-22 23:53:03 -04005161 turbostat_init();
5162
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005163 /* dump counters and exit */
5164 if (dump_only)
5165 return get_and_dump_counters();
5166
Len Brownc8ade362017-02-15 17:15:11 -05005167 /* list header and exit */
5168 if (list_header_only) {
5169 print_header(",");
5170 flush_output_stdout();
5171 return 0;
5172 }
5173
Len Brown103a8fe2010-10-22 23:53:03 -04005174 /*
5175 * if any params left, it must be a command to fork
5176 */
5177 if (argc - optind)
5178 return fork_it(argv + optind);
5179 else
5180 turbostat_loop();
5181
5182 return 0;
5183}