blob: 1684f4ec627eeedbaa7e3be22e1f586d3ed4615c [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>
Len Brownb9ad8ee2017-07-19 19:28:37 -040032#include <sys/select.h>
Len Brown103a8fe2010-10-22 23:53:03 -040033#include <sys/resource.h>
34#include <fcntl.h>
35#include <signal.h>
36#include <sys/time.h>
37#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050038#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040039#include <dirent.h>
40#include <string.h>
41#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040042#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050043#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070044#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040045#include <linux/capability.h>
46#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040047
Len Brown103a8fe2010-10-22 23:53:03 -040048char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050049FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050050int *fd_percpu;
Len Brownb9ad8ee2017-07-19 19:28:37 -040051struct timeval interval_tv = {5, 0};
Artem Bityutskiy47936f92017-10-04 15:01:47 +030052struct timespec interval_ts = {5, 0};
Len Brownb9ad8ee2017-07-19 19:28:37 -040053struct timespec one_msec = {0, 1000000};
Chen Yu023fe0a2018-04-26 08:41:03 +080054unsigned int num_iterations;
Len Brownd8af6f52015-02-10 01:56:38 -050055unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050056unsigned int quiet;
Len Brown3f44a5c2017-10-17 15:42:56 -040057unsigned int shown;
Len Brown0de6c0d2017-02-15 21:45:40 -050058unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050059unsigned int rapl_joules;
60unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050061unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050062unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040063unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070064unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050065unsigned int do_slm_cstates;
Srinivas Pandruvada997e5392018-05-31 10:39:07 -070066unsigned int do_cnl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050067unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040068unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050069unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040070unsigned int do_irtl_snb;
71unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050072unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040073unsigned int genuine_intel;
74unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050075unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050076unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020077unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040078double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040079double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040080unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040081double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040082unsigned int show_pkg_only;
83unsigned int show_core_only;
84char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050085unsigned int do_rapl;
86unsigned int do_dts;
87unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050088unsigned long long gfx_cur_rc6_ms;
Len Brownbe0e54c2018-06-01 12:35:53 -040089unsigned long long cpuidle_cur_cpu_lpi_us;
90unsigned long long cpuidle_cur_sys_lpi_us;
Len Brown27d47352016-02-27 00:37:54 -050091unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050092unsigned int tcc_activation_temp;
93unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050094double rapl_power_units, rapl_time_units;
95double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050096double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040097unsigned int do_core_perf_limit_reasons;
Artem Bityutskiyac980e12017-09-05 15:14:08 +030098unsigned int has_automatic_cstate_conversion;
Len Brown3a9a9412014-08-15 02:39:52 -040099unsigned int do_gfx_perf_limit_reasons;
100unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -0400101unsigned int crystal_hz;
102unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400103int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400104double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500105unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
106 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
107unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
108unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
109unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
110unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500111unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500112
Len Browne6f9bb32013-12-03 02:19:19 -0500113#define RAPL_PKG (1 << 0)
114 /* 0x610 MSR_PKG_POWER_LIMIT */
115 /* 0x611 MSR_PKG_ENERGY_STATUS */
116#define RAPL_PKG_PERF_STATUS (1 << 1)
117 /* 0x613 MSR_PKG_PERF_STATUS */
118#define RAPL_PKG_POWER_INFO (1 << 2)
119 /* 0x614 MSR_PKG_POWER_INFO */
120
121#define RAPL_DRAM (1 << 3)
122 /* 0x618 MSR_DRAM_POWER_LIMIT */
123 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500124#define RAPL_DRAM_PERF_STATUS (1 << 4)
125 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400126#define RAPL_DRAM_POWER_INFO (1 << 5)
127 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500128
Jacob Pan91484942016-06-16 09:48:20 -0700129#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500130 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400131#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500132 /* 0x63a MSR_PP0_POLICY */
133
Len Brown0b2bb692015-03-26 00:50:30 -0400134#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500135 /* 0x640 MSR_PP1_POWER_LIMIT */
136 /* 0x641 MSR_PP1_ENERGY_STATUS */
137 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700138
139#define RAPL_CORES_ENERGY_STATUS (1 << 9)
140 /* 0x639 MSR_PP0_ENERGY_STATUS */
141#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500142#define TJMAX_DEFAULT 100
143
144#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brown388e9c82016-12-22 23:57:55 -0500146/*
147 * buffer size used by sscanf() for added column names
148 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
149 */
150#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500151#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500152
Len Brown103a8fe2010-10-22 23:53:03 -0400153int backwards_count;
154char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400155
Len Brown1ef7d212017-02-10 23:54:15 -0500156#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
157cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
158size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown0748eaf2018-06-01 12:38:29 -0400159#define MAX_ADDED_COUNTERS 8
160#define MAX_ADDED_THREAD_COUNTERS 24
Len Brown103a8fe2010-10-22 23:53:03 -0400161
Len Brownc98d5d92012-06-04 00:56:40 -0400162struct thread_data {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700163 struct timeval tv_begin;
164 struct timeval tv_end;
Len Brownc98d5d92012-06-04 00:56:40 -0400165 unsigned long long tsc;
166 unsigned long long aperf;
167 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500168 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500169 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500170 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400171 unsigned int cpu_id;
172 unsigned int flags;
173#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
174#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown0748eaf2018-06-01 12:38:29 -0400175 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400176} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400177
Len Brownc98d5d92012-06-04 00:56:40 -0400178struct core_data {
179 unsigned long long c3;
180 unsigned long long c6;
181 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500182 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500183 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400184 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500185 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400186} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400187
Len Brownc98d5d92012-06-04 00:56:40 -0400188struct pkg_data {
189 unsigned long long pc2;
190 unsigned long long pc3;
191 unsigned long long pc6;
192 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800193 unsigned long long pc8;
194 unsigned long long pc9;
195 unsigned long long pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -0400196 unsigned long long cpu_lpi;
197 unsigned long long sys_lpi;
Len Brown0b2bb692015-03-26 00:50:30 -0400198 unsigned long long pkg_wtd_core_c0;
199 unsigned long long pkg_any_core_c0;
200 unsigned long long pkg_any_gfxe_c0;
201 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400202 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500203 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400204 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500205 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
206 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
207 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
208 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
209 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
210 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
211 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500212 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400213} *package_even, *package_odd;
214
215#define ODD_COUNTERS thread_odd, core_odd, package_odd
216#define EVEN_COUNTERS thread_even, core_even, package_even
217
218#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
219 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
220 topo.num_threads_per_core + \
221 (core_no) * topo.num_threads_per_core + (thread_no))
222#define GET_CORE(core_base, core_no, pkg_no) \
223 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
224#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
225
Len Brown388e9c82016-12-22 23:57:55 -0500226enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500227enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500228enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
229
230struct msr_counter {
231 unsigned int msr_num;
232 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500233 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500234 unsigned int width;
235 enum counter_type type;
236 enum counter_format format;
237 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500238 unsigned int flags;
239#define FLAGS_HIDE (1 << 0)
240#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500241#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500242};
243
244struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500245 unsigned int added_thread_counters;
246 unsigned int added_core_counters;
247 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500248 struct msr_counter *tp;
249 struct msr_counter *cp;
250 struct msr_counter *pp;
251} sys;
252
Len Brownc98d5d92012-06-04 00:56:40 -0400253struct system_summary {
254 struct thread_data threads;
255 struct core_data cores;
256 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500257} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400258
259
260struct topo_params {
261 int num_packages;
262 int num_cpus;
263 int num_cores;
264 int max_cpu_num;
265 int num_cores_per_pkg;
266 int num_threads_per_core;
267} topo;
268
269struct timeval tv_even, tv_odd, tv_delta;
270
Len Brown562a2d32016-02-26 23:48:05 -0500271int *irq_column_2_cpu; /* /proc/interrupts column numbers */
272int *irqs_per_cpu; /* indexed by cpu_num */
273
Len Brownc98d5d92012-06-04 00:56:40 -0400274void setup_all_buffers(void);
275
276int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400277{
Len Brownc98d5d92012-06-04 00:56:40 -0400278 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400279}
Len Brown88c32812012-03-29 21:44:40 -0400280/*
Len Brownc98d5d92012-06-04 00:56:40 -0400281 * run func(thread, core, package) in topology order
282 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400283 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400284
Len Brownc98d5d92012-06-04 00:56:40 -0400285int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
286 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400287{
Len Brownc98d5d92012-06-04 00:56:40 -0400288 int retval, pkg_no, core_no, thread_no;
289
290 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
291 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
292 for (thread_no = 0; thread_no <
293 topo.num_threads_per_core; ++thread_no) {
294 struct thread_data *t;
295 struct core_data *c;
296 struct pkg_data *p;
297
298 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
299
300 if (cpu_is_not_present(t->cpu_id))
301 continue;
302
303 c = GET_CORE(core_base, core_no, pkg_no);
304 p = GET_PKG(pkg_base, pkg_no);
305
306 retval = func(t, c, p);
307 if (retval)
308 return retval;
309 }
310 }
311 }
312 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400313}
314
315int cpu_migrate(int cpu)
316{
Len Brownc98d5d92012-06-04 00:56:40 -0400317 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
318 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
319 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400320 return -1;
321 else
322 return 0;
323}
Len Brown36229892016-02-26 20:51:02 -0500324int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400325{
Len Brown103a8fe2010-10-22 23:53:03 -0400326 char pathname[32];
327 int fd;
328
Len Brown36229892016-02-26 20:51:02 -0500329 fd = fd_percpu[cpu];
330
331 if (fd)
332 return fd;
333
Len Brown103a8fe2010-10-22 23:53:03 -0400334 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
335 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400336 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400337 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 -0400338
Len Brown36229892016-02-26 20:51:02 -0500339 fd_percpu[cpu] = fd;
340
341 return fd;
342}
343
344int get_msr(int cpu, off_t offset, unsigned long long *msr)
345{
346 ssize_t retval;
347
348 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400349
Len Brown98481e72014-08-15 00:36:50 -0400350 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500351 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400352
353 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400354}
355
Len Brownfc04cc62014-02-06 00:55:19 -0500356/*
Len Brown812db3f2017-02-10 00:25:41 -0500357 * Each string in this array is compared in --show and --hide cmdline.
358 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500359 */
Len Brown812db3f2017-02-10 00:25:41 -0500360struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400361 { 0x0, "usec" },
362 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500363 { 0x0, "Package" },
364 { 0x0, "Avg_MHz" },
365 { 0x0, "Bzy_MHz" },
366 { 0x0, "TSC_MHz" },
367 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500368 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500369 { 0x0, "Busy%" },
370 { 0x0, "CPU%c1" },
371 { 0x0, "CPU%c3" },
372 { 0x0, "CPU%c6" },
373 { 0x0, "CPU%c7" },
374 { 0x0, "ThreadC" },
375 { 0x0, "CoreTmp" },
376 { 0x0, "CoreCnt" },
377 { 0x0, "PkgTmp" },
378 { 0x0, "GFX%rc6" },
379 { 0x0, "GFXMHz" },
380 { 0x0, "Pkg%pc2" },
381 { 0x0, "Pkg%pc3" },
382 { 0x0, "Pkg%pc6" },
383 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500384 { 0x0, "Pkg%pc8" },
385 { 0x0, "Pkg%pc9" },
Len Brown4bd1f8f2018-01-28 23:42:42 -0500386 { 0x0, "Pk%pc10" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400387 { 0x0, "CPU%LPI" },
388 { 0x0, "SYS%LPI" },
Len Brown812db3f2017-02-10 00:25:41 -0500389 { 0x0, "PkgWatt" },
390 { 0x0, "CorWatt" },
391 { 0x0, "GFXWatt" },
392 { 0x0, "PkgCnt" },
393 { 0x0, "RAMWatt" },
394 { 0x0, "PKG_%" },
395 { 0x0, "RAM_%" },
396 { 0x0, "Pkg_J" },
397 { 0x0, "Cor_J" },
398 { 0x0, "GFX_J" },
399 { 0x0, "RAM_J" },
400 { 0x0, "Core" },
401 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500402 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500403 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400404 { 0x0, "Totl%C0" },
405 { 0x0, "Any%C0" },
406 { 0x0, "GFX%C0" },
407 { 0x0, "CPUGFX%" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400408 { 0x0, "Node%" },
Len Brown812db3f2017-02-10 00:25:41 -0500409};
410
Len Browna99d8732017-05-20 20:11:55 -0400411
412
Len Brown812db3f2017-02-10 00:25:41 -0500413#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400414#define BIC_USEC (1ULL << 0)
415#define BIC_TOD (1ULL << 1)
416#define BIC_Package (1ULL << 2)
417#define BIC_Avg_MHz (1ULL << 3)
418#define BIC_Bzy_MHz (1ULL << 4)
419#define BIC_TSC_MHz (1ULL << 5)
420#define BIC_IRQ (1ULL << 6)
421#define BIC_SMI (1ULL << 7)
422#define BIC_Busy (1ULL << 8)
423#define BIC_CPU_c1 (1ULL << 9)
424#define BIC_CPU_c3 (1ULL << 10)
425#define BIC_CPU_c6 (1ULL << 11)
426#define BIC_CPU_c7 (1ULL << 12)
427#define BIC_ThreadC (1ULL << 13)
428#define BIC_CoreTmp (1ULL << 14)
429#define BIC_CoreCnt (1ULL << 15)
430#define BIC_PkgTmp (1ULL << 16)
431#define BIC_GFX_rc6 (1ULL << 17)
432#define BIC_GFXMHz (1ULL << 18)
433#define BIC_Pkgpc2 (1ULL << 19)
434#define BIC_Pkgpc3 (1ULL << 20)
435#define BIC_Pkgpc6 (1ULL << 21)
436#define BIC_Pkgpc7 (1ULL << 22)
437#define BIC_Pkgpc8 (1ULL << 23)
438#define BIC_Pkgpc9 (1ULL << 24)
439#define BIC_Pkgpc10 (1ULL << 25)
Len Brownbe0e54c2018-06-01 12:35:53 -0400440#define BIC_CPU_LPI (1ULL << 26)
441#define BIC_SYS_LPI (1ULL << 27)
Len Brown3f44a5c2017-10-17 15:42:56 -0400442#define BIC_PkgWatt (1ULL << 26)
443#define BIC_CorWatt (1ULL << 27)
444#define BIC_GFXWatt (1ULL << 28)
445#define BIC_PkgCnt (1ULL << 29)
446#define BIC_RAMWatt (1ULL << 30)
447#define BIC_PKG__ (1ULL << 31)
448#define BIC_RAM__ (1ULL << 32)
449#define BIC_Pkg_J (1ULL << 33)
450#define BIC_Cor_J (1ULL << 34)
451#define BIC_GFX_J (1ULL << 35)
452#define BIC_RAM_J (1ULL << 36)
453#define BIC_Core (1ULL << 37)
454#define BIC_CPU (1ULL << 38)
455#define BIC_Mod_c6 (1ULL << 39)
456#define BIC_sysfs (1ULL << 40)
457#define BIC_Totl_c0 (1ULL << 41)
458#define BIC_Any_c0 (1ULL << 42)
459#define BIC_GFX_c0 (1ULL << 43)
460#define BIC_CPUGFX (1ULL << 44)
Len Brown812db3f2017-02-10 00:25:41 -0500461
Len Brown3f44a5c2017-10-17 15:42:56 -0400462#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
463
464unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
465unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500466
467#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400468#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500469#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500470#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500471
Len Brown3f44a5c2017-10-17 15:42:56 -0400472
Len Browndd778a52017-02-21 23:21:13 -0500473#define MAX_DEFERRED 16
474char *deferred_skip_names[MAX_DEFERRED];
475int deferred_skip_index;
476
477/*
478 * HIDE_LIST - hide this list of counters, show the rest [default]
479 * SHOW_LIST - show this list of counters, hide the rest
480 */
481enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
482
483void help(void)
484{
485 fprintf(outf,
486 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
487 "\n"
488 "Turbostat forks the specified COMMAND and prints statistics\n"
489 "when COMMAND completes.\n"
490 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
491 "to print statistics, until interrupted.\n"
492 "--add add a counter\n"
493 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
494 "--cpu cpu-set limit output to summary plus cpu-set:\n"
495 " {core | package | j,k,l..m,n-p }\n"
496 "--quiet skip decoding system configuration header\n"
Len Brownb9ad8ee2017-07-19 19:28:37 -0400497 "--interval sec.subsec Override default 5-second measurement interval\n"
Len Browndd778a52017-02-21 23:21:13 -0500498 "--help print this help message\n"
499 "--list list column headers only\n"
Chen Yu023fe0a2018-04-26 08:41:03 +0800500 "--num_iterations num number of the measurement iterations\n"
Len Browndd778a52017-02-21 23:21:13 -0500501 "--out file create or truncate \"file\" for all output\n"
502 "--version print version information\n"
503 "\n"
504 "For more help, run \"man turbostat\"\n");
505}
506
Len Brown812db3f2017-02-10 00:25:41 -0500507/*
508 * bic_lookup
509 * for all the strings in comma separate name_list,
510 * set the approprate bit in return value.
511 */
Len Browndd778a52017-02-21 23:21:13 -0500512unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500513{
514 int i;
515 unsigned long long retval = 0;
516
517 while (name_list) {
518 char *comma;
519
520 comma = strchr(name_list, ',');
521
522 if (comma)
523 *comma = '\0';
524
Len Brown3f44a5c2017-10-17 15:42:56 -0400525 if (!strcmp(name_list, "all"))
526 return ~0;
527
Len Brown812db3f2017-02-10 00:25:41 -0500528 for (i = 0; i < MAX_BIC; ++i) {
529 if (!strcmp(name_list, bic[i].name)) {
530 retval |= (1ULL << i);
531 break;
532 }
533 }
534 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500535 if (mode == SHOW_LIST) {
536 fprintf(stderr, "Invalid counter name: %s\n", name_list);
537 exit(-1);
538 }
539 deferred_skip_names[deferred_skip_index++] = name_list;
540 if (debug)
541 fprintf(stderr, "deferred \"%s\"\n", name_list);
542 if (deferred_skip_index >= MAX_DEFERRED) {
543 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
544 MAX_DEFERRED, name_list);
545 help();
546 exit(1);
547 }
Len Brown812db3f2017-02-10 00:25:41 -0500548 }
549
550 name_list = comma;
551 if (name_list)
552 name_list++;
553
554 }
555 return retval;
556}
Len Brownfc04cc62014-02-06 00:55:19 -0500557
Len Browndd778a52017-02-21 23:21:13 -0500558
Len Brownc8ade362017-02-15 17:15:11 -0500559void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400560{
Len Brown388e9c82016-12-22 23:57:55 -0500561 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500562 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500563
Len Brown3f44a5c2017-10-17 15:42:56 -0400564 if (DO_BIC(BIC_USEC))
565 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
566 if (DO_BIC(BIC_TOD))
567 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500568 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500569 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500570 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500571 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500572 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500573 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500574 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500575 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500576 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500577 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500578 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500579 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500580 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500581 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500582
Len Brown0de6c0d2017-02-15 21:45:40 -0500583 if (DO_BIC(BIC_IRQ)) {
584 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500585 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500586 else
Len Brown6168c2e2017-02-16 23:07:51 -0500587 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500588 }
589
Len Brown812db3f2017-02-10 00:25:41 -0500590 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500591 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500592
Len Brown388e9c82016-12-22 23:57:55 -0500593 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500594
Len Brown388e9c82016-12-22 23:57:55 -0500595 if (mp->format == FORMAT_RAW) {
596 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500597 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500598 else
Len Browndd778a52017-02-21 23:21:13 -0500599 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500600 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500601 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500602 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500603 else
Len Browndd778a52017-02-21 23:21:13 -0500604 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500605 }
606 }
607
Len Brown41618e62017-02-09 18:25:22 -0500608 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500609 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700610 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500611 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500612 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500613 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500614 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500615 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500616
Len Brown0539ba12017-02-10 00:27:20 -0500617 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500618 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500619
Len Brown812db3f2017-02-10 00:25:41 -0500620 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500621 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500622
623 for (mp = sys.cp; mp; mp = mp->next) {
624 if (mp->format == FORMAT_RAW) {
625 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500626 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500627 else
Len Brownc8ade362017-02-15 17:15:11 -0500628 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500629 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500630 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
631 outp += sprintf(outp, "%s%8s", delim, mp->name);
632 else
633 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500634 }
635 }
636
Len Brown812db3f2017-02-10 00:25:41 -0500637 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500638 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500639
Len Brown812db3f2017-02-10 00:25:41 -0500640 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500641 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500642
Len Brown812db3f2017-02-10 00:25:41 -0500643 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500644 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500645
Len Browna99d8732017-05-20 20:11:55 -0400646 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500647 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400648 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500649 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400650 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500651 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400652 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500653 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400654
Len Brown0f47c082017-01-27 00:50:45 -0500655 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500656 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500657 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500658 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500659 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500660 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500661 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500662 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500663 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500664 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500665 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500666 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500667 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500668 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brownbe0e54c2018-06-01 12:35:53 -0400669 if (DO_BIC(BIC_CPU_LPI))
670 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
671 if (DO_BIC(BIC_SYS_LPI))
672 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400673
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800674 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500675 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500676 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500677 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500678 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500679 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500680 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500681 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500682 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500683 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500684 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500685 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500686 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500687 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500688 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500689 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500690 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500691 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500692 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500693 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500694 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500695 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500696 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500697 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500698 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500699 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800700 }
Len Brown388e9c82016-12-22 23:57:55 -0500701 for (mp = sys.pp; mp; mp = mp->next) {
702 if (mp->format == FORMAT_RAW) {
703 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500704 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500705 else
Len Brownc8ade362017-02-15 17:15:11 -0500706 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500707 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500708 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
709 outp += sprintf(outp, "%s%8s", delim, mp->name);
710 else
711 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500712 }
713 }
714
Len Brownc98d5d92012-06-04 00:56:40 -0400715 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400716}
717
Len Brownc98d5d92012-06-04 00:56:40 -0400718int dump_counters(struct thread_data *t, struct core_data *c,
719 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400720{
Len Brown388e9c82016-12-22 23:57:55 -0500721 int i;
722 struct msr_counter *mp;
723
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200724 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400725
Len Brownc98d5d92012-06-04 00:56:40 -0400726 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200727 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
728 t->cpu_id, t->flags);
729 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
730 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
731 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
732 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500733
Len Brown812db3f2017-02-10 00:25:41 -0500734 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500735 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500736 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500737 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500738
739 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
740 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
741 i, mp->msr_num, t->counter[i]);
742 }
Len Brownc98d5d92012-06-04 00:56:40 -0400743 }
Len Brown103a8fe2010-10-22 23:53:03 -0400744
Len Brownc98d5d92012-06-04 00:56:40 -0400745 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200746 outp += sprintf(outp, "core: %d\n", c->core_id);
747 outp += sprintf(outp, "c3: %016llX\n", c->c3);
748 outp += sprintf(outp, "c6: %016llX\n", c->c6);
749 outp += sprintf(outp, "c7: %016llX\n", c->c7);
750 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500751
752 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
753 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
754 i, mp->msr_num, c->counter[i]);
755 }
Len Brown0539ba12017-02-10 00:27:20 -0500756 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400757 }
758
759 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200760 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400761
762 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
763 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
764 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
765 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
766
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200767 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500768 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500769 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500770 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500771 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500772 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500773 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200774 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
775 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
776 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
Len Brownbe0e54c2018-06-01 12:35:53 -0400777 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
778 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
779 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200780 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
781 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
782 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
783 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
784 outp += sprintf(outp, "Throttle PKG: %0X\n",
785 p->rapl_pkg_perf_status);
786 outp += sprintf(outp, "Throttle RAM: %0X\n",
787 p->rapl_dram_perf_status);
788 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500789
790 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
791 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
792 i, mp->msr_num, p->counter[i]);
793 }
Len Brownc98d5d92012-06-04 00:56:40 -0400794 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200795
796 outp += sprintf(outp, "\n");
797
Len Brownc98d5d92012-06-04 00:56:40 -0400798 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400799}
800
Len Browne23da032012-02-06 18:37:16 -0500801/*
802 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500803 */
Len Brownc98d5d92012-06-04 00:56:40 -0400804int format_counters(struct thread_data *t, struct core_data *c,
805 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400806{
Len Brown008d396e2017-02-10 00:29:51 -0500807 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500808 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500809 int i;
810 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500811 char *delim = "\t";
812 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400813
Len Brownc98d5d92012-06-04 00:56:40 -0400814 /* if showing only 1st thread in core and this isn't one, bail out */
815 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
816 return 0;
817
818 /* if showing only 1st thread in pkg and this isn't one, bail out */
819 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
820 return 0;
821
Len Brown1ef7d212017-02-10 23:54:15 -0500822 /*if not summary line and --cpu is used */
823 if ((t != &average.threads) &&
824 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
825 return 0;
826
Len Brown3f44a5c2017-10-17 15:42:56 -0400827 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700828 /* on each row, print how many usec each timestamp took to gather */
829 struct timeval tv;
830
831 timersub(&t->tv_end, &t->tv_begin, &tv);
832 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
833 }
834
Len Brown3f44a5c2017-10-17 15:42:56 -0400835 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
836 if (DO_BIC(BIC_TOD))
837 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
838
Len Brown103a8fe2010-10-22 23:53:03 -0400839 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
840
Len Brown008d396e2017-02-10 00:29:51 -0500841 tsc = t->tsc * tsc_tweak;
842
Len Brownc98d5d92012-06-04 00:56:40 -0400843 /* topo columns, print blanks on 1st (average) line */
844 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500845 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500846 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500847 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500848 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500849 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500850 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400851 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500852 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400853 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500854 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400855 else
Len Brown6168c2e2017-02-16 23:07:51 -0500856 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400857 }
Len Brown812db3f2017-02-10 00:25:41 -0500858 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400859 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500860 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400861 else
Len Brown6168c2e2017-02-16 23:07:51 -0500862 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400863 }
Len Brown812db3f2017-02-10 00:25:41 -0500864 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500865 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400866 }
Len Brownfc04cc62014-02-06 00:55:19 -0500867
Len Brown812db3f2017-02-10 00:25:41 -0500868 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500869 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500870 1.0 / units * t->aperf / interval_float);
871
Len Brown812db3f2017-02-10 00:25:41 -0500872 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500873 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400874
Len Brown812db3f2017-02-10 00:25:41 -0500875 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400876 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500877 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400878 else
Len Brown6168c2e2017-02-16 23:07:51 -0500879 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500880 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400881 }
Len Brown103a8fe2010-10-22 23:53:03 -0400882
Len Brown812db3f2017-02-10 00:25:41 -0500883 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500884 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400885
Len Brown562a2d32016-02-26 23:48:05 -0500886 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500887 if (DO_BIC(BIC_IRQ)) {
888 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500889 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500890 else
Len Brown6168c2e2017-02-16 23:07:51 -0500891 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500892 }
Len Brown562a2d32016-02-26 23:48:05 -0500893
Len Brown1cc21f72015-02-23 00:34:57 -0500894 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500895 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500896 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500897
Len Brown678a3bd2017-02-09 22:22:13 -0500898 /* Added counters */
899 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
900 if (mp->format == FORMAT_RAW) {
901 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500902 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500903 else
Len Brown6168c2e2017-02-16 23:07:51 -0500904 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500905 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500906 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500907 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500908 else
Len Brown6168c2e2017-02-16 23:07:51 -0500909 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500910 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500911 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500912 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500913 else
Len Brown6168c2e2017-02-16 23:07:51 -0500914 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500915 }
916 }
917
Len Brown41618e62017-02-09 18:25:22 -0500918 /* C1 */
919 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500920 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500921
922
Len Brownc98d5d92012-06-04 00:56:40 -0400923 /* print per-core data only for 1st thread in core */
924 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
925 goto done;
926
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700927 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500928 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500929 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500930 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500931 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500932 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400933
Len Brown0539ba12017-02-10 00:27:20 -0500934 /* Mod%c6 */
935 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500936 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500937
Len Brown812db3f2017-02-10 00:25:41 -0500938 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500939 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500940
Len Brown388e9c82016-12-22 23:57:55 -0500941 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
942 if (mp->format == FORMAT_RAW) {
943 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500944 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500945 else
Len Brown6168c2e2017-02-16 23:07:51 -0500946 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500947 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500948 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500949 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500950 else
Len Brown6168c2e2017-02-16 23:07:51 -0500951 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500952 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500953 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500954 }
955 }
956
Len Brownc98d5d92012-06-04 00:56:40 -0400957 /* print per-package data only for 1st core in package */
958 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
959 goto done;
960
Len Brown0b2bb692015-03-26 00:50:30 -0400961 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500962 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500963 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500964
Len Brownfdf676e2016-02-27 01:28:12 -0500965 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500966 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400967 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500968 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400969 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500970 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400971 p->gfx_rc6_ms / 10.0 / interval_float);
972 }
973 }
Len Brownfdf676e2016-02-27 01:28:12 -0500974
Len Brown27d47352016-02-27 00:37:54 -0500975 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500976 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500977 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500978
Len Brown0b2bb692015-03-26 00:50:30 -0400979 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -0400980 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500981 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400982 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500983 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400984 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500985 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400986 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500987 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400988
Len Brown0f47c082017-01-27 00:50:45 -0500989 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500990 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500991 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500992 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500993 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500994 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500995 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500996 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500997 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500998 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500999 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -05001000 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001001 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -05001002 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -05001003
Len Brownbe0e54c2018-06-01 12:35:53 -04001004 if (DO_BIC(BIC_CPU_LPI))
1005 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1006 if (DO_BIC(BIC_SYS_LPI))
1007 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1008
Len Brown889facb2012-11-08 00:48:57 -05001009 /*
1010 * If measurement interval exceeds minimum RAPL Joule Counter range,
1011 * indicate that results are suspect by printing "**" in fraction place.
1012 */
Len Brownfc04cc62014-02-06 00:55:19 -05001013 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -05001014 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -05001015 else
Len Browne975db52016-04-06 23:56:02 -04001016 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -05001017
Len Brown812db3f2017-02-10 00:25:41 -05001018 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001019 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001020 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001021 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001022 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001023 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001024 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001025 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001026 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001027 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001028 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001029 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001030 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001031 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001032 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001033 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001034 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001035 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 -05001036 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001037 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 -05001038
Len Brown388e9c82016-12-22 23:57:55 -05001039 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1040 if (mp->format == FORMAT_RAW) {
1041 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001042 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001043 else
Len Brown6168c2e2017-02-16 23:07:51 -05001044 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001045 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001046 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001047 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001048 else
Len Brown6168c2e2017-02-16 23:07:51 -05001049 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001050 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001051 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001052 }
1053 }
1054
Len Brownc98d5d92012-06-04 00:56:40 -04001055done:
Len Brown94d6ab42018-01-27 22:39:21 -05001056 if (*(outp - 1) != '\n')
1057 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001058
1059 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001060}
1061
Len Brownb7d8c142016-02-13 23:36:17 -05001062void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001063{
Len Brownb7d8c142016-02-13 23:36:17 -05001064 FILE *filep;
1065
1066 if (outf == stderr)
1067 filep = stdout;
1068 else
1069 filep = outf;
1070
1071 fputs(output_buffer, filep);
1072 fflush(filep);
1073
Len Brownc98d5d92012-06-04 00:56:40 -04001074 outp = output_buffer;
1075}
Len Brownb7d8c142016-02-13 23:36:17 -05001076void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001077{
Len Brownb7d8c142016-02-13 23:36:17 -05001078 fputs(output_buffer, outf);
1079 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001080 outp = output_buffer;
1081}
1082void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1083{
Len Browne23da032012-02-06 18:37:16 -05001084 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001085
Len Browne23da032012-02-06 18:37:16 -05001086 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001087 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001088
Len Brownc98d5d92012-06-04 00:56:40 -04001089 if (topo.num_cpus > 1)
1090 format_counters(&average.threads, &average.cores,
1091 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001092
Len Browne23da032012-02-06 18:37:16 -05001093 printed = 1;
1094
1095 if (summary_only)
1096 return;
1097
Len Brownc98d5d92012-06-04 00:56:40 -04001098 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001099}
1100
Len Brown889facb2012-11-08 00:48:57 -05001101#define DELTA_WRAP32(new, old) \
1102 if (new > old) { \
1103 old = new - old; \
1104 } else { \
1105 old = 0x100000000 + new - old; \
1106 }
1107
Len Brownba3dec92016-04-22 20:31:46 -04001108int
Len Brownc98d5d92012-06-04 00:56:40 -04001109delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001110{
Len Brown388e9c82016-12-22 23:57:55 -05001111 int i;
1112 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001113
Len Browna99d8732017-05-20 20:11:55 -04001114
1115 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001116 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001117 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001118 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001119 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001120 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001121 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001122 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 -04001123
Len Brownc98d5d92012-06-04 00:56:40 -04001124 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001125 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001126 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001127 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001128 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001129 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001130 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001131 old->pc8 = new->pc8 - old->pc8;
1132 old->pc9 = new->pc9 - old->pc9;
1133 old->pc10 = new->pc10 - old->pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -04001134 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1135 old->sys_lpi = new->sys_lpi - old->sys_lpi;
Len Brown889facb2012-11-08 00:48:57 -05001136 old->pkg_temp_c = new->pkg_temp_c;
1137
Len Brown9185e982016-04-06 17:16:00 -04001138 /* flag an error when rc6 counter resets/wraps */
1139 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1140 old->gfx_rc6_ms = -1;
1141 else
1142 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1143
Len Brown27d47352016-02-27 00:37:54 -05001144 old->gfx_mhz = new->gfx_mhz;
1145
Len Brown889facb2012-11-08 00:48:57 -05001146 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1147 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1148 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1149 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1150 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1151 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001152
Len Brown388e9c82016-12-22 23:57:55 -05001153 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1154 if (mp->format == FORMAT_RAW)
1155 old->counter[i] = new->counter[i];
1156 else
1157 old->counter[i] = new->counter[i] - old->counter[i];
1158 }
1159
Len Brownba3dec92016-04-22 20:31:46 -04001160 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001161}
Len Brown103a8fe2010-10-22 23:53:03 -04001162
Len Brownc98d5d92012-06-04 00:56:40 -04001163void
1164delta_core(struct core_data *new, struct core_data *old)
1165{
Len Brown388e9c82016-12-22 23:57:55 -05001166 int i;
1167 struct msr_counter *mp;
1168
Len Brownc98d5d92012-06-04 00:56:40 -04001169 old->c3 = new->c3 - old->c3;
1170 old->c6 = new->c6 - old->c6;
1171 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001172 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001173 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001174
1175 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1176 if (mp->format == FORMAT_RAW)
1177 old->counter[i] = new->counter[i];
1178 else
1179 old->counter[i] = new->counter[i] - old->counter[i];
1180 }
Len Brownc98d5d92012-06-04 00:56:40 -04001181}
Len Brown103a8fe2010-10-22 23:53:03 -04001182
Len Brownc3ae3312012-06-13 21:31:46 -04001183/*
1184 * old = new - old
1185 */
Len Brownba3dec92016-04-22 20:31:46 -04001186int
Len Brownc98d5d92012-06-04 00:56:40 -04001187delta_thread(struct thread_data *new, struct thread_data *old,
1188 struct core_data *core_delta)
1189{
Len Brown388e9c82016-12-22 23:57:55 -05001190 int i;
1191 struct msr_counter *mp;
1192
Len Brown3f44a5c2017-10-17 15:42:56 -04001193 /*
1194 * the timestamps from start of measurement interval are in "old"
1195 * the timestamp from end of measurement interval are in "new"
1196 * over-write old w/ new so we can print end of interval values
1197 */
1198
1199 old->tv_begin = new->tv_begin;
1200 old->tv_end = new->tv_end;
1201
Len Brownc98d5d92012-06-04 00:56:40 -04001202 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001203
Len Brownc98d5d92012-06-04 00:56:40 -04001204 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001205 if (old->tsc < (1000 * 1000))
1206 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1207 "You can disable all c-states by booting with \"idle=poll\"\n"
1208 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001209
Len Brownc98d5d92012-06-04 00:56:40 -04001210 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001211
Len Brown812db3f2017-02-10 00:25:41 -05001212 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001213 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1214 old->aperf = new->aperf - old->aperf;
1215 old->mperf = new->mperf - old->mperf;
1216 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001217 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001218 }
Len Brownc98d5d92012-06-04 00:56:40 -04001219 }
Len Brown103a8fe2010-10-22 23:53:03 -04001220
Len Brown103a8fe2010-10-22 23:53:03 -04001221
Len Brown144b44b2013-11-09 00:30:16 -05001222 if (use_c1_residency_msr) {
1223 /*
1224 * Some models have a dedicated C1 residency MSR,
1225 * which should be more accurate than the derivation below.
1226 */
1227 } else {
1228 /*
1229 * As counter collection is not atomic,
1230 * it is possible for mperf's non-halted cycles + idle states
1231 * to exceed TSC's all cycles: show c1 = 0% in that case.
1232 */
Len Brown95149362017-04-12 19:44:51 -04001233 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001234 old->c1 = 0;
1235 else {
1236 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001237 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001238 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001239 }
Len Brownc98d5d92012-06-04 00:56:40 -04001240 }
Len Brownc3ae3312012-06-13 21:31:46 -04001241
Len Brownc98d5d92012-06-04 00:56:40 -04001242 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001243 if (debug > 1)
1244 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001245 old->mperf = 1; /* divide by 0 protection */
1246 }
1247
Len Brown812db3f2017-02-10 00:25:41 -05001248 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001249 old->irq_count = new->irq_count - old->irq_count;
1250
Len Brown812db3f2017-02-10 00:25:41 -05001251 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001252 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001253
Len Brown388e9c82016-12-22 23:57:55 -05001254 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1255 if (mp->format == FORMAT_RAW)
1256 old->counter[i] = new->counter[i];
1257 else
1258 old->counter[i] = new->counter[i] - old->counter[i];
1259 }
Len Brownba3dec92016-04-22 20:31:46 -04001260 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001261}
1262
1263int delta_cpu(struct thread_data *t, struct core_data *c,
1264 struct pkg_data *p, struct thread_data *t2,
1265 struct core_data *c2, struct pkg_data *p2)
1266{
Len Brownba3dec92016-04-22 20:31:46 -04001267 int retval = 0;
1268
Len Brownc98d5d92012-06-04 00:56:40 -04001269 /* calculate core delta only for 1st thread in core */
1270 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1271 delta_core(c, c2);
1272
1273 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001274 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1275 if (retval)
1276 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001277
1278 /* calculate package delta only for 1st core in package */
1279 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001280 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001281
Len Brownba3dec92016-04-22 20:31:46 -04001282 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001283}
1284
1285void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1286{
Len Brown388e9c82016-12-22 23:57:55 -05001287 int i;
1288 struct msr_counter *mp;
1289
Len Brown3f44a5c2017-10-17 15:42:56 -04001290 t->tv_begin.tv_sec = 0;
1291 t->tv_begin.tv_usec = 0;
1292 t->tv_end.tv_sec = 0;
1293 t->tv_end.tv_usec = 0;
1294
Len Brownc98d5d92012-06-04 00:56:40 -04001295 t->tsc = 0;
1296 t->aperf = 0;
1297 t->mperf = 0;
1298 t->c1 = 0;
1299
Len Brown562a2d32016-02-26 23:48:05 -05001300 t->irq_count = 0;
1301 t->smi_count = 0;
1302
Len Brownc98d5d92012-06-04 00:56:40 -04001303 /* tells format_counters to dump all fields from this set */
1304 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1305
1306 c->c3 = 0;
1307 c->c6 = 0;
1308 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001309 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001310 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001311
Len Brown0b2bb692015-03-26 00:50:30 -04001312 p->pkg_wtd_core_c0 = 0;
1313 p->pkg_any_core_c0 = 0;
1314 p->pkg_any_gfxe_c0 = 0;
1315 p->pkg_both_core_gfxe_c0 = 0;
1316
Len Brownc98d5d92012-06-04 00:56:40 -04001317 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001318 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001319 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001320 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001321 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001322 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001323 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001324 p->pc8 = 0;
1325 p->pc9 = 0;
1326 p->pc10 = 0;
Len Brownbe0e54c2018-06-01 12:35:53 -04001327 p->cpu_lpi = 0;
1328 p->sys_lpi = 0;
Len Brown889facb2012-11-08 00:48:57 -05001329
1330 p->energy_pkg = 0;
1331 p->energy_dram = 0;
1332 p->energy_cores = 0;
1333 p->energy_gfx = 0;
1334 p->rapl_pkg_perf_status = 0;
1335 p->rapl_dram_perf_status = 0;
1336 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001337
Len Brownfdf676e2016-02-27 01:28:12 -05001338 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001339 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001340 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1341 t->counter[i] = 0;
1342
1343 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1344 c->counter[i] = 0;
1345
1346 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1347 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001348}
1349int sum_counters(struct thread_data *t, struct core_data *c,
1350 struct pkg_data *p)
1351{
Len Brown388e9c82016-12-22 23:57:55 -05001352 int i;
1353 struct msr_counter *mp;
1354
Len Brown3f44a5c2017-10-17 15:42:56 -04001355 /* remember first tv_begin */
1356 if (average.threads.tv_begin.tv_sec == 0)
1357 average.threads.tv_begin = t->tv_begin;
1358
1359 /* remember last tv_end */
1360 average.threads.tv_end = t->tv_end;
1361
Len Brownc98d5d92012-06-04 00:56:40 -04001362 average.threads.tsc += t->tsc;
1363 average.threads.aperf += t->aperf;
1364 average.threads.mperf += t->mperf;
1365 average.threads.c1 += t->c1;
1366
Len Brown562a2d32016-02-26 23:48:05 -05001367 average.threads.irq_count += t->irq_count;
1368 average.threads.smi_count += t->smi_count;
1369
Len Brown388e9c82016-12-22 23:57:55 -05001370 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1371 if (mp->format == FORMAT_RAW)
1372 continue;
1373 average.threads.counter[i] += t->counter[i];
1374 }
1375
Len Brownc98d5d92012-06-04 00:56:40 -04001376 /* sum per-core values only for 1st thread in core */
1377 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1378 return 0;
1379
1380 average.cores.c3 += c->c3;
1381 average.cores.c6 += c->c6;
1382 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001383 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001384
Len Brown889facb2012-11-08 00:48:57 -05001385 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1386
Len Brown388e9c82016-12-22 23:57:55 -05001387 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1388 if (mp->format == FORMAT_RAW)
1389 continue;
1390 average.cores.counter[i] += c->counter[i];
1391 }
1392
Len Brownc98d5d92012-06-04 00:56:40 -04001393 /* sum per-pkg values only for 1st core in pkg */
1394 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1395 return 0;
1396
Len Browna99d8732017-05-20 20:11:55 -04001397 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001398 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001399 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001400 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001401 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001402 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001403 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001404 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001405
Len Brownc98d5d92012-06-04 00:56:40 -04001406 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001407 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001408 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001409 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001410 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001411 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001412 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001413 average.packages.pc8 += p->pc8;
1414 average.packages.pc9 += p->pc9;
1415 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001416
Len Brownbe0e54c2018-06-01 12:35:53 -04001417 average.packages.cpu_lpi = p->cpu_lpi;
1418 average.packages.sys_lpi = p->sys_lpi;
1419
Len Brown889facb2012-11-08 00:48:57 -05001420 average.packages.energy_pkg += p->energy_pkg;
1421 average.packages.energy_dram += p->energy_dram;
1422 average.packages.energy_cores += p->energy_cores;
1423 average.packages.energy_gfx += p->energy_gfx;
1424
Len Brownfdf676e2016-02-27 01:28:12 -05001425 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001426 average.packages.gfx_mhz = p->gfx_mhz;
1427
Len Brown889facb2012-11-08 00:48:57 -05001428 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1429
1430 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1431 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001432
1433 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1434 if (mp->format == FORMAT_RAW)
1435 continue;
1436 average.packages.counter[i] += p->counter[i];
1437 }
Len Brownc98d5d92012-06-04 00:56:40 -04001438 return 0;
1439}
1440/*
1441 * sum the counters for all cpus in the system
1442 * compute the weighted average
1443 */
1444void compute_average(struct thread_data *t, struct core_data *c,
1445 struct pkg_data *p)
1446{
Len Brown388e9c82016-12-22 23:57:55 -05001447 int i;
1448 struct msr_counter *mp;
1449
Len Brownc98d5d92012-06-04 00:56:40 -04001450 clear_counters(&average.threads, &average.cores, &average.packages);
1451
1452 for_all_cpus(sum_counters, t, c, p);
1453
1454 average.threads.tsc /= topo.num_cpus;
1455 average.threads.aperf /= topo.num_cpus;
1456 average.threads.mperf /= topo.num_cpus;
1457 average.threads.c1 /= topo.num_cpus;
1458
Len Brown0de6c0d2017-02-15 21:45:40 -05001459 if (average.threads.irq_count > 9999999)
1460 sums_need_wide_columns = 1;
1461
Len Brownc98d5d92012-06-04 00:56:40 -04001462 average.cores.c3 /= topo.num_cores;
1463 average.cores.c6 /= topo.num_cores;
1464 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001465 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001466
Len Browna99d8732017-05-20 20:11:55 -04001467 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001468 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001469 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001470 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001471 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001472 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001473 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001474 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001475
Len Brownc98d5d92012-06-04 00:56:40 -04001476 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001477 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001478 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001479 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001480 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001481 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001482 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001483
1484 average.packages.pc8 /= topo.num_packages;
1485 average.packages.pc9 /= topo.num_packages;
1486 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001487
1488 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1489 if (mp->format == FORMAT_RAW)
1490 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001491 if (mp->type == COUNTER_ITEMS) {
1492 if (average.threads.counter[i] > 9999999)
1493 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001494 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001495 }
Len Brown388e9c82016-12-22 23:57:55 -05001496 average.threads.counter[i] /= topo.num_cpus;
1497 }
1498 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1499 if (mp->format == FORMAT_RAW)
1500 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001501 if (mp->type == COUNTER_ITEMS) {
1502 if (average.cores.counter[i] > 9999999)
1503 sums_need_wide_columns = 1;
1504 }
Len Brown388e9c82016-12-22 23:57:55 -05001505 average.cores.counter[i] /= topo.num_cores;
1506 }
1507 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1508 if (mp->format == FORMAT_RAW)
1509 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001510 if (mp->type == COUNTER_ITEMS) {
1511 if (average.packages.counter[i] > 9999999)
1512 sums_need_wide_columns = 1;
1513 }
Len Brown388e9c82016-12-22 23:57:55 -05001514 average.packages.counter[i] /= topo.num_packages;
1515 }
Len Brownc98d5d92012-06-04 00:56:40 -04001516}
1517
1518static unsigned long long rdtsc(void)
1519{
1520 unsigned int low, high;
1521
1522 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1523
1524 return low | ((unsigned long long)high) << 32;
1525}
1526
Len Brownc98d5d92012-06-04 00:56:40 -04001527/*
Len Brown495c76542017-02-08 02:41:51 -05001528 * Open a file, and exit on failure
1529 */
1530FILE *fopen_or_die(const char *path, const char *mode)
1531{
1532 FILE *filep = fopen(path, mode);
1533
1534 if (!filep)
1535 err(1, "%s: open failed", path);
1536 return filep;
1537}
1538/*
1539 * snapshot_sysfs_counter()
1540 *
1541 * return snapshot of given counter
1542 */
1543unsigned long long snapshot_sysfs_counter(char *path)
1544{
1545 FILE *fp;
1546 int retval;
1547 unsigned long long counter;
1548
1549 fp = fopen_or_die(path, "r");
1550
1551 retval = fscanf(fp, "%lld", &counter);
1552 if (retval != 1)
1553 err(1, "snapshot_sysfs_counter(%s)", path);
1554
1555 fclose(fp);
1556
1557 return counter;
1558}
1559
1560int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1561{
1562 if (mp->msr_num != 0) {
1563 if (get_msr(cpu, mp->msr_num, counterp))
1564 return -1;
1565 } else {
Len Brown46c27972017-12-08 17:38:17 -05001566 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001567
1568 if (mp->flags & SYSFS_PERCPU) {
1569 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1570 cpu, mp->path);
1571
1572 *counterp = snapshot_sysfs_counter(path);
1573 } else {
1574 *counterp = snapshot_sysfs_counter(mp->path);
1575 }
Len Brown495c76542017-02-08 02:41:51 -05001576 }
1577
1578 return 0;
1579}
1580
1581/*
Len Brownc98d5d92012-06-04 00:56:40 -04001582 * get_counters(...)
1583 * migrate to cpu
1584 * acquire and record local counters for that cpu
1585 */
1586int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1587{
1588 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001589 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001590 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001591 struct msr_counter *mp;
1592 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001593
Len Brownf4fdf2b2017-05-27 21:06:55 -07001594
1595 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1596
Len Browne52966c2012-11-08 22:38:05 -05001597 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001598 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001599 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001600 }
Len Brownc98d5d92012-06-04 00:56:40 -04001601
Len Brown0102b062016-02-27 03:11:29 -05001602retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001603 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1604
Len Brown812db3f2017-02-10 00:25:41 -05001605 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001606 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1607
1608 /*
1609 * The TSC, APERF and MPERF must be read together for
1610 * APERF/MPERF and MPERF/TSC to give accurate results.
1611 *
1612 * Unfortunately, APERF and MPERF are read by
1613 * individual system call, so delays may occur
1614 * between them. If the time to read them
1615 * varies by a large amount, we re-read them.
1616 */
1617
1618 /*
1619 * This initial dummy APERF read has been seen to
1620 * reduce jitter in the subsequent reads.
1621 */
1622
Len Brown9c63a652012-10-31 01:29:52 -04001623 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001624 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001625
1626 t->tsc = rdtsc(); /* re-read close to APERF */
1627
1628 tsc_before = t->tsc;
1629
1630 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1631 return -3;
1632
1633 tsc_between = rdtsc();
1634
Len Brown9c63a652012-10-31 01:29:52 -04001635 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001636 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001637
1638 tsc_after = rdtsc();
1639
1640 aperf_time = tsc_between - tsc_before;
1641 mperf_time = tsc_after - tsc_between;
1642
1643 /*
1644 * If the system call latency to read APERF and MPERF
1645 * differ by more than 2x, then try again.
1646 */
1647 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1648 aperf_mperf_retry_count++;
1649 if (aperf_mperf_retry_count < 5)
1650 goto retry;
1651 else
1652 warnx("cpu%d jitter %lld %lld",
1653 cpu, aperf_time, mperf_time);
1654 }
1655 aperf_mperf_retry_count = 0;
1656
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001657 t->aperf = t->aperf * aperf_mperf_multiplier;
1658 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001659 }
1660
Len Brown812db3f2017-02-10 00:25:41 -05001661 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001662 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001663 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001664 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1665 return -5;
1666 t->smi_count = msr & 0xFFFFFFFF;
1667 }
Len Brown0539ba12017-02-10 00:27:20 -05001668 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001669 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1670 return -6;
1671 }
1672
Len Brown388e9c82016-12-22 23:57:55 -05001673 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001674 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001675 return -10;
1676 }
1677
Len Brownc98d5d92012-06-04 00:56:40 -04001678 /* collect core counters only for 1st thread in core */
1679 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001680 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001681
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07001682 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001683 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1684 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001685 }
1686
Len Brown812db3f2017-02-10 00:25:41 -05001687 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001688 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1689 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001690 } else if (do_knl_cstates) {
1691 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1692 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001693 }
1694
Len Brown812db3f2017-02-10 00:25:41 -05001695 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001696 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1697 return -8;
1698
Len Brown0539ba12017-02-10 00:27:20 -05001699 if (DO_BIC(BIC_Mod_c6))
1700 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1701 return -8;
1702
Len Brown812db3f2017-02-10 00:25:41 -05001703 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001704 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1705 return -9;
1706 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1707 }
1708
Len Brown388e9c82016-12-22 23:57:55 -05001709 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001710 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001711 return -10;
1712 }
Len Brown889facb2012-11-08 00:48:57 -05001713
Len Brownc98d5d92012-06-04 00:56:40 -04001714 /* collect package counters only for 1st core in package */
1715 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001716 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001717
Len Browna99d8732017-05-20 20:11:55 -04001718 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001719 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1720 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001721 }
1722 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001723 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1724 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001725 }
1726 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001727 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1728 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001729 }
1730 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001731 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1732 return -13;
1733 }
Len Brown0f47c082017-01-27 00:50:45 -05001734 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001735 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1736 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001737 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001738 if (do_slm_cstates) {
1739 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1740 return -10;
1741 } else {
1742 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1743 return -10;
1744 }
1745 }
1746
Len Brown0f47c082017-01-27 00:50:45 -05001747 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001748 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1749 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001750 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001751 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1752 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001753 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001754 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1755 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001756 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001757 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1758 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001759 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001760 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1761 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001762
Len Brownbe0e54c2018-06-01 12:35:53 -04001763 if (DO_BIC(BIC_CPU_LPI))
1764 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1765 if (DO_BIC(BIC_SYS_LPI))
1766 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1767
Len Brown889facb2012-11-08 00:48:57 -05001768 if (do_rapl & RAPL_PKG) {
1769 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1770 return -13;
1771 p->energy_pkg = msr & 0xFFFFFFFF;
1772 }
Jacob Pan91484942016-06-16 09:48:20 -07001773 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001774 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1775 return -14;
1776 p->energy_cores = msr & 0xFFFFFFFF;
1777 }
1778 if (do_rapl & RAPL_DRAM) {
1779 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1780 return -15;
1781 p->energy_dram = msr & 0xFFFFFFFF;
1782 }
1783 if (do_rapl & RAPL_GFX) {
1784 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1785 return -16;
1786 p->energy_gfx = msr & 0xFFFFFFFF;
1787 }
1788 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1789 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1790 return -16;
1791 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1792 }
1793 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1794 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1795 return -16;
1796 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1797 }
Len Brown812db3f2017-02-10 00:25:41 -05001798 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001799 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1800 return -17;
1801 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1802 }
Len Brownfdf676e2016-02-27 01:28:12 -05001803
Len Brown812db3f2017-02-10 00:25:41 -05001804 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001805 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1806
Len Brown812db3f2017-02-10 00:25:41 -05001807 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001808 p->gfx_mhz = gfx_cur_mhz;
1809
Len Brown388e9c82016-12-22 23:57:55 -05001810 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001811 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001812 return -10;
1813 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001814done:
1815 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001816
Len Brown103a8fe2010-10-22 23:53:03 -04001817 return 0;
1818}
1819
Len Brownee7e38e2015-02-09 23:39:45 -05001820/*
1821 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1822 * If you change the values, note they are used both in comparisons
1823 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1824 */
1825
1826#define PCLUKN 0 /* Unknown */
1827#define PCLRSV 1 /* Reserved */
1828#define PCL__0 2 /* PC0 */
1829#define PCL__1 3 /* PC1 */
1830#define PCL__2 4 /* PC2 */
1831#define PCL__3 5 /* PC3 */
1832#define PCL__4 6 /* PC4 */
1833#define PCL__6 7 /* PC6 */
1834#define PCL_6N 8 /* PC6 No Retention */
1835#define PCL_6R 9 /* PC6 Retention */
1836#define PCL__7 10 /* PC7 */
1837#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001838#define PCL__8 12 /* PC8 */
1839#define PCL__9 13 /* PC9 */
1840#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001841
1842int pkg_cstate_limit = PCLUKN;
1843char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001844 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001845
Len Browne9257f52015-04-01 21:02:57 -04001846int 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};
1847int 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};
1848int 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 -05001849int 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 -05001850int 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 -04001851int 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 -04001852int 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 +03001853int 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 -05001854
Len Browna2b7b742015-09-26 00:12:38 -04001855
1856static void
1857calculate_tsc_tweak()
1858{
Len Browna2b7b742015-09-26 00:12:38 -04001859 tsc_tweak = base_hz / tsc_hz;
1860}
1861
Len Brownfcd17212015-03-23 20:29:09 -04001862static void
1863dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001864{
1865 unsigned long long msr;
1866 unsigned int ratio;
1867
Len Brownec0adc52015-11-12 02:42:31 -05001868 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001869
Len Brownb7d8c142016-02-13 23:36:17 -05001870 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001871
Len Brown103a8fe2010-10-22 23:53:03 -04001872 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001873 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001874 ratio, bclk, ratio * bclk);
1875
1876 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001877 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001878 ratio, bclk, ratio * bclk);
1879
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001880 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001881 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001882 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001883
Len Brownfcd17212015-03-23 20:29:09 -04001884 return;
1885}
1886
1887static void
1888dump_hsw_turbo_ratio_limits(void)
1889{
1890 unsigned long long msr;
1891 unsigned int ratio;
1892
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001893 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001894
Len Brownb7d8c142016-02-13 23:36:17 -05001895 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001896
1897 ratio = (msr >> 8) & 0xFF;
1898 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001899 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001900 ratio, bclk, ratio * bclk);
1901
1902 ratio = (msr >> 0) & 0xFF;
1903 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001904 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001905 ratio, bclk, ratio * bclk);
1906 return;
1907}
1908
1909static void
1910dump_ivt_turbo_ratio_limits(void)
1911{
1912 unsigned long long msr;
1913 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001914
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001915 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001916
Len Brownb7d8c142016-02-13 23:36:17 -05001917 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001918
1919 ratio = (msr >> 56) & 0xFF;
1920 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001921 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001922 ratio, bclk, ratio * bclk);
1923
1924 ratio = (msr >> 48) & 0xFF;
1925 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001926 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001927 ratio, bclk, ratio * bclk);
1928
1929 ratio = (msr >> 40) & 0xFF;
1930 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001931 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001932 ratio, bclk, ratio * bclk);
1933
1934 ratio = (msr >> 32) & 0xFF;
1935 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001936 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001937 ratio, bclk, ratio * bclk);
1938
1939 ratio = (msr >> 24) & 0xFF;
1940 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001941 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001942 ratio, bclk, ratio * bclk);
1943
1944 ratio = (msr >> 16) & 0xFF;
1945 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001946 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001947 ratio, bclk, ratio * bclk);
1948
1949 ratio = (msr >> 8) & 0xFF;
1950 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001951 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001952 ratio, bclk, ratio * bclk);
1953
1954 ratio = (msr >> 0) & 0xFF;
1955 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001956 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001957 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001958 return;
1959}
Len Brown31e07522017-01-31 23:07:49 -05001960int has_turbo_ratio_group_limits(int family, int model)
1961{
1962
1963 if (!genuine_intel)
1964 return 0;
1965
1966 switch (model) {
1967 case INTEL_FAM6_ATOM_GOLDMONT:
1968 case INTEL_FAM6_SKYLAKE_X:
1969 case INTEL_FAM6_ATOM_DENVERTON:
1970 return 1;
1971 }
1972 return 0;
1973}
Len Brown6574a5d2012-09-21 00:01:31 -04001974
Len Brownfcd17212015-03-23 20:29:09 -04001975static void
Len Brown31e07522017-01-31 23:07:49 -05001976dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001977{
Len Brown31e07522017-01-31 23:07:49 -05001978 unsigned long long msr, core_counts;
1979 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001980
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001981 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001982 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001983
Len Brown31e07522017-01-31 23:07:49 -05001984 if (has_turbo_ratio_group_limits(family, model)) {
1985 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1986 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1987 } else {
1988 core_counts = 0x0807060504030201;
1989 }
1990
Len Brown6574a5d2012-09-21 00:01:31 -04001991 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001992 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -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 Brown6574a5d2012-09-21 00:01:31 -04001996
1997 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001998 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001999 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002000 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2001 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002002
2003 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002004 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002005 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002006 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2007 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002008
2009 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002010 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002011 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002012 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2013 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002014
Len Brown103a8fe2010-10-22 23:53:03 -04002015 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002016 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002017 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002018 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2019 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002020
2021 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002022 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002023 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002024 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2025 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002026
2027 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002028 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002029 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002030 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2031 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002032
2033 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002034 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002035 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002036 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2037 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002038 return;
2039}
Len Brown3a9a9412014-08-15 02:39:52 -04002040
Len Brownfcd17212015-03-23 20:29:09 -04002041static void
Len Brown0f7887c2017-01-12 23:49:18 -05002042dump_atom_turbo_ratio_limits(void)
2043{
2044 unsigned long long msr;
2045 unsigned int ratio;
2046
2047 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2048 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2049
2050 ratio = (msr >> 0) & 0x3F;
2051 if (ratio)
2052 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2053 ratio, bclk, ratio * bclk);
2054
2055 ratio = (msr >> 8) & 0x3F;
2056 if (ratio)
2057 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2058 ratio, bclk, ratio * bclk);
2059
2060 ratio = (msr >> 16) & 0x3F;
2061 if (ratio)
2062 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2063 ratio, bclk, ratio * bclk);
2064
2065 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2066 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2067
2068 ratio = (msr >> 24) & 0x3F;
2069 if (ratio)
2070 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2071 ratio, bclk, ratio * bclk);
2072
2073 ratio = (msr >> 16) & 0x3F;
2074 if (ratio)
2075 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2076 ratio, bclk, ratio * bclk);
2077
2078 ratio = (msr >> 8) & 0x3F;
2079 if (ratio)
2080 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2081 ratio, bclk, ratio * bclk);
2082
2083 ratio = (msr >> 0) & 0x3F;
2084 if (ratio)
2085 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2086 ratio, bclk, ratio * bclk);
2087}
2088
2089static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002090dump_knl_turbo_ratio_limits(void)
2091{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002092 const unsigned int buckets_no = 7;
2093
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002094 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002095 int delta_cores, delta_ratio;
2096 int i, b_nr;
2097 unsigned int cores[buckets_no];
2098 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002099
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002100 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002101
Len Brownb7d8c142016-02-13 23:36:17 -05002102 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002103 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002104
2105 /**
2106 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002107 * [0] -- Reserved
2108 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002109 * [15:8] -- Base value of freq ratio of bucket 1.
2110 * [20:16] -- +ve delta of number of active cores of bucket 2.
2111 * i.e. active cores of bucket 2 =
2112 * active cores of bucket 1 + delta
2113 * [23:21] -- Negative delta of freq ratio of bucket 2.
2114 * i.e. freq ratio of bucket 2 =
2115 * freq ratio of bucket 1 - delta
2116 * [28:24]-- +ve delta of number of active cores of bucket 3.
2117 * [31:29]-- -ve delta of freq ratio of bucket 3.
2118 * [36:32]-- +ve delta of number of active cores of bucket 4.
2119 * [39:37]-- -ve delta of freq ratio of bucket 4.
2120 * [44:40]-- +ve delta of number of active cores of bucket 5.
2121 * [47:45]-- -ve delta of freq ratio of bucket 5.
2122 * [52:48]-- +ve delta of number of active cores of bucket 6.
2123 * [55:53]-- -ve delta of freq ratio of bucket 6.
2124 * [60:56]-- +ve delta of number of active cores of bucket 7.
2125 * [63:61]-- -ve delta of freq ratio of bucket 7.
2126 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002127
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002128 b_nr = 0;
2129 cores[b_nr] = (msr & 0xFF) >> 1;
2130 ratio[b_nr] = (msr >> 8) & 0xFF;
2131
2132 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002133 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002134 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002135
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002136 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2137 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2138 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002139 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002140
2141 for (i = buckets_no - 1; i >= 0; i--)
2142 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002143 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002144 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002145 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002146}
2147
2148static void
Len Brownfcd17212015-03-23 20:29:09 -04002149dump_nhm_cst_cfg(void)
2150{
2151 unsigned long long msr;
2152
Len Brown1df2e552017-01-07 23:24:57 -05002153 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002154
Len Brown1df2e552017-01-07 23:24:57 -05002155 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002156
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002157 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002158 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2159 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2160 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2161 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2162 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002163 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002164 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002165
2166#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2167 if (has_automatic_cstate_conversion) {
2168 fprintf(outf, ", automatic c-state conversion=%s",
2169 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2170 }
2171
2172 fprintf(outf, ")\n");
2173
Len Brownfcd17212015-03-23 20:29:09 -04002174 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002175}
2176
Len Brown6fb31432015-06-17 16:23:45 -04002177static void
2178dump_config_tdp(void)
2179{
2180 unsigned long long msr;
2181
2182 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002183 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002184 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002185
2186 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002187 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002188 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002189 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2190 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2191 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2192 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002193 }
Len Brownb7d8c142016-02-13 23:36:17 -05002194 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002195
2196 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002197 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002198 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002199 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2200 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2201 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2202 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002203 }
Len Brownb7d8c142016-02-13 23:36:17 -05002204 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002205
2206 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002207 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002208 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002209 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2210 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2211 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002212
Len Brown6fb31432015-06-17 16:23:45 -04002213 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002214 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002215 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002216 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2217 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002218}
Len Brown5a634262016-04-06 17:15:55 -04002219
2220unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2221
2222void print_irtl(void)
2223{
2224 unsigned long long msr;
2225
2226 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2227 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2228 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2229 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2230
2231 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2232 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2233 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2234 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2235
2236 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2237 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2238 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2239 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2240
2241 if (!do_irtl_hsw)
2242 return;
2243
2244 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2245 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2246 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2247 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2248
2249 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2250 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2251 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2252 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2253
2254 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2255 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2256 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2257 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2258
2259}
Len Brown36229892016-02-26 20:51:02 -05002260void free_fd_percpu(void)
2261{
2262 int i;
2263
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002264 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002265 if (fd_percpu[i] != 0)
2266 close(fd_percpu[i]);
2267 }
2268
2269 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002270}
2271
Len Brownc98d5d92012-06-04 00:56:40 -04002272void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002273{
Len Brownc98d5d92012-06-04 00:56:40 -04002274 CPU_FREE(cpu_present_set);
2275 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002276 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002277
Len Brownc98d5d92012-06-04 00:56:40 -04002278 CPU_FREE(cpu_affinity_set);
2279 cpu_affinity_set = NULL;
2280 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002281
Len Brownc98d5d92012-06-04 00:56:40 -04002282 free(thread_even);
2283 free(core_even);
2284 free(package_even);
2285
2286 thread_even = NULL;
2287 core_even = NULL;
2288 package_even = NULL;
2289
2290 free(thread_odd);
2291 free(core_odd);
2292 free(package_odd);
2293
2294 thread_odd = NULL;
2295 core_odd = NULL;
2296 package_odd = NULL;
2297
2298 free(output_buffer);
2299 output_buffer = NULL;
2300 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002301
2302 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002303
2304 free(irq_column_2_cpu);
2305 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002306}
2307
Josh Triplett57a42a32013-08-20 17:20:17 -07002308
2309/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002310 * Parse a file containing a single int.
2311 */
2312int parse_int_file(const char *fmt, ...)
2313{
2314 va_list args;
2315 char path[PATH_MAX];
2316 FILE *filep;
2317 int value;
2318
2319 va_start(args, fmt);
2320 vsnprintf(path, sizeof(path), fmt, args);
2321 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002322 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002323 if (fscanf(filep, "%d", &value) != 1)
2324 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002325 fclose(filep);
2326 return value;
2327}
2328
2329/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002330 * get_cpu_position_in_core(cpu)
2331 * return the position of the CPU among its HT siblings in the core
2332 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002333 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002334int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002335{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002336 char path[64];
2337 FILE *filep;
2338 int this_cpu;
2339 char character;
2340 int i;
2341
2342 sprintf(path,
2343 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2344 cpu);
2345 filep = fopen(path, "r");
2346 if (filep == NULL) {
2347 perror(path);
2348 exit(1);
2349 }
2350
2351 for (i = 0; i < topo.num_threads_per_core; i++) {
2352 fscanf(filep, "%d", &this_cpu);
2353 if (this_cpu == cpu) {
2354 fclose(filep);
2355 return i;
2356 }
2357
2358 /* Account for no separator after last thread*/
2359 if (i != (topo.num_threads_per_core - 1))
2360 fscanf(filep, "%c", &character);
2361 }
2362
2363 fclose(filep);
2364 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002365}
2366
Len Brownc98d5d92012-06-04 00:56:40 -04002367/*
2368 * cpu_is_first_core_in_package(cpu)
2369 * return 1 if given CPU is 1st core in package
2370 */
2371int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002372{
Josh Triplett95aebc42013-08-20 17:20:16 -07002373 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002374}
2375
2376int get_physical_package_id(int cpu)
2377{
Josh Triplett95aebc42013-08-20 17:20:16 -07002378 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002379}
2380
2381int get_core_id(int cpu)
2382{
Josh Triplett95aebc42013-08-20 17:20:16 -07002383 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002384}
2385
Len Brownc98d5d92012-06-04 00:56:40 -04002386int get_num_ht_siblings(int cpu)
2387{
2388 char path[80];
2389 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002390 int sib1;
2391 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002392 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002393 char str[100];
2394 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002395
2396 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002397 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002398
Len Brownc98d5d92012-06-04 00:56:40 -04002399 /*
2400 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002401 * A ',' separated or '-' separated set of numbers
2402 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002403 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002404 fscanf(filep, "%d%c\n", &sib1, &character);
2405 fseek(filep, 0, SEEK_SET);
2406 fgets(str, 100, filep);
2407 ch = strchr(str, character);
2408 while (ch != NULL) {
2409 matches++;
2410 ch = strchr(ch+1, character);
2411 }
Len Brownc98d5d92012-06-04 00:56:40 -04002412
2413 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002414 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002415}
2416
Len Brown103a8fe2010-10-22 23:53:03 -04002417/*
Len Brownc98d5d92012-06-04 00:56:40 -04002418 * run func(thread, core, package) in topology order
2419 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002420 */
2421
Len Brownc98d5d92012-06-04 00:56:40 -04002422int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2423 struct pkg_data *, struct thread_data *, struct core_data *,
2424 struct pkg_data *), struct thread_data *thread_base,
2425 struct core_data *core_base, struct pkg_data *pkg_base,
2426 struct thread_data *thread_base2, struct core_data *core_base2,
2427 struct pkg_data *pkg_base2)
2428{
2429 int retval, pkg_no, core_no, thread_no;
2430
2431 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2432 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2433 for (thread_no = 0; thread_no <
2434 topo.num_threads_per_core; ++thread_no) {
2435 struct thread_data *t, *t2;
2436 struct core_data *c, *c2;
2437 struct pkg_data *p, *p2;
2438
2439 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2440
2441 if (cpu_is_not_present(t->cpu_id))
2442 continue;
2443
2444 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2445
2446 c = GET_CORE(core_base, core_no, pkg_no);
2447 c2 = GET_CORE(core_base2, core_no, pkg_no);
2448
2449 p = GET_PKG(pkg_base, pkg_no);
2450 p2 = GET_PKG(pkg_base2, pkg_no);
2451
2452 retval = func(t, c, p, t2, c2, p2);
2453 if (retval)
2454 return retval;
2455 }
2456 }
2457 }
2458 return 0;
2459}
2460
2461/*
2462 * run func(cpu) on every cpu in /proc/stat
2463 * return max_cpu number
2464 */
2465int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002466{
2467 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002468 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002469 int retval;
2470
Josh Triplett57a42a32013-08-20 17:20:17 -07002471 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002472
2473 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002474 if (retval != 0)
2475 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002476
Len Brownc98d5d92012-06-04 00:56:40 -04002477 while (1) {
2478 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 -04002479 if (retval != 1)
2480 break;
2481
Len Brownc98d5d92012-06-04 00:56:40 -04002482 retval = func(cpu_num);
2483 if (retval) {
2484 fclose(fp);
2485 return(retval);
2486 }
Len Brown103a8fe2010-10-22 23:53:03 -04002487 }
2488 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002489 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002490}
2491
2492void re_initialize(void)
2493{
Len Brownc98d5d92012-06-04 00:56:40 -04002494 free_all_buffers();
2495 setup_all_buffers();
2496 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002497}
2498
Prarit Bhargava843c5792018-06-01 10:04:28 -04002499void set_max_cpu_num(void)
2500{
2501 FILE *filep;
2502 unsigned long dummy;
2503
2504 topo.max_cpu_num = 0;
2505 filep = fopen_or_die(
2506 "/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2507 "r");
2508 while (fscanf(filep, "%lx,", &dummy) == 1)
2509 topo.max_cpu_num += 32;
2510 fclose(filep);
2511 topo.max_cpu_num--; /* 0 based */
2512}
Len Brownc98d5d92012-06-04 00:56:40 -04002513
Len Brown103a8fe2010-10-22 23:53:03 -04002514/*
Len Brownc98d5d92012-06-04 00:56:40 -04002515 * count_cpus()
2516 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002517 */
Len Brownc98d5d92012-06-04 00:56:40 -04002518int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002519{
Prarit Bhargava843c5792018-06-01 10:04:28 -04002520 topo.num_cpus++;
Len Brownc98d5d92012-06-04 00:56:40 -04002521 return 0;
2522}
2523int mark_cpu_present(int cpu)
2524{
2525 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002526 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002527}
2528
Len Brown562a2d32016-02-26 23:48:05 -05002529/*
2530 * snapshot_proc_interrupts()
2531 *
2532 * read and record summary of /proc/interrupts
2533 *
2534 * return 1 if config change requires a restart, else return 0
2535 */
2536int snapshot_proc_interrupts(void)
2537{
2538 static FILE *fp;
2539 int column, retval;
2540
2541 if (fp == NULL)
2542 fp = fopen_or_die("/proc/interrupts", "r");
2543 else
2544 rewind(fp);
2545
2546 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2547 for (column = 0; column < topo.num_cpus; ++column) {
2548 int cpu_number;
2549
2550 retval = fscanf(fp, " CPU%d", &cpu_number);
2551 if (retval != 1)
2552 break;
2553
2554 if (cpu_number > topo.max_cpu_num) {
2555 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2556 return 1;
2557 }
2558
2559 irq_column_2_cpu[column] = cpu_number;
2560 irqs_per_cpu[cpu_number] = 0;
2561 }
2562
2563 /* read /proc/interrupt count lines and sum up irqs per cpu */
2564 while (1) {
2565 int column;
2566 char buf[64];
2567
2568 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2569 if (retval != 1)
2570 break;
2571
2572 /* read the count per cpu */
2573 for (column = 0; column < topo.num_cpus; ++column) {
2574
2575 int cpu_number, irq_count;
2576
2577 retval = fscanf(fp, " %d", &irq_count);
2578 if (retval != 1)
2579 break;
2580
2581 cpu_number = irq_column_2_cpu[column];
2582 irqs_per_cpu[cpu_number] += irq_count;
2583
2584 }
2585
2586 while (getc(fp) != '\n')
2587 ; /* flush interrupt description */
2588
2589 }
2590 return 0;
2591}
Len Brown27d47352016-02-27 00:37:54 -05002592/*
Len Brownfdf676e2016-02-27 01:28:12 -05002593 * snapshot_gfx_rc6_ms()
2594 *
2595 * record snapshot of
2596 * /sys/class/drm/card0/power/rc6_residency_ms
2597 *
2598 * return 1 if config change requires a restart, else return 0
2599 */
2600int snapshot_gfx_rc6_ms(void)
2601{
2602 FILE *fp;
2603 int retval;
2604
2605 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2606
2607 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2608 if (retval != 1)
2609 err(1, "GFX rc6");
2610
2611 fclose(fp);
2612
2613 return 0;
2614}
2615/*
Len Brown27d47352016-02-27 00:37:54 -05002616 * snapshot_gfx_mhz()
2617 *
2618 * record snapshot of
2619 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2620 *
2621 * return 1 if config change requires a restart, else return 0
2622 */
2623int snapshot_gfx_mhz(void)
2624{
2625 static FILE *fp;
2626 int retval;
2627
2628 if (fp == NULL)
2629 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002630 else {
Len Brown27d47352016-02-27 00:37:54 -05002631 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002632 fflush(fp);
2633 }
Len Brown27d47352016-02-27 00:37:54 -05002634
2635 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2636 if (retval != 1)
2637 err(1, "GFX MHz");
2638
2639 return 0;
2640}
Len Brown562a2d32016-02-26 23:48:05 -05002641
2642/*
Len Brownbe0e54c2018-06-01 12:35:53 -04002643 * snapshot_cpu_lpi()
2644 *
2645 * record snapshot of
2646 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2647 *
2648 * return 1 if config change requires a restart, else return 0
2649 */
2650int snapshot_cpu_lpi_us(void)
2651{
2652 FILE *fp;
2653 int retval;
2654
2655 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2656
2657 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2658 if (retval != 1)
2659 err(1, "CPU LPI");
2660
2661 fclose(fp);
2662
2663 return 0;
2664}
2665/*
2666 * snapshot_sys_lpi()
2667 *
2668 * record snapshot of
2669 * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2670 *
2671 * return 1 if config change requires a restart, else return 0
2672 */
2673int snapshot_sys_lpi_us(void)
2674{
2675 FILE *fp;
2676 int retval;
2677
2678 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2679
2680 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2681 if (retval != 1)
2682 err(1, "SYS LPI");
2683
2684 fclose(fp);
2685
2686 return 0;
2687}
2688/*
Len Brown562a2d32016-02-26 23:48:05 -05002689 * snapshot /proc and /sys files
2690 *
2691 * return 1 if configuration restart needed, else return 0
2692 */
2693int snapshot_proc_sysfs_files(void)
2694{
Len Brown218f0e82017-02-14 22:07:52 -05002695 if (DO_BIC(BIC_IRQ))
2696 if (snapshot_proc_interrupts())
2697 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002698
Len Brown812db3f2017-02-10 00:25:41 -05002699 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002700 snapshot_gfx_rc6_ms();
2701
Len Brown812db3f2017-02-10 00:25:41 -05002702 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002703 snapshot_gfx_mhz();
2704
Len Brownbe0e54c2018-06-01 12:35:53 -04002705 if (DO_BIC(BIC_CPU_LPI))
2706 snapshot_cpu_lpi_us();
2707
2708 if (DO_BIC(BIC_SYS_LPI))
2709 snapshot_sys_lpi_us();
2710
Len Brown562a2d32016-02-26 23:48:05 -05002711 return 0;
2712}
2713
Len Brown8aa2ed02017-07-15 14:57:37 -04002714int exit_requested;
2715
2716static void signal_handler (int signal)
2717{
2718 switch (signal) {
2719 case SIGINT:
2720 exit_requested = 1;
2721 if (debug)
2722 fprintf(stderr, " SIGINT\n");
2723 break;
Len Brown07211962017-07-15 15:51:26 -04002724 case SIGUSR1:
2725 if (debug > 1)
2726 fprintf(stderr, "SIGUSR1\n");
2727 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002728 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002729 /* make sure this manually-invoked interval is at least 1ms long */
2730 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002731}
2732
2733void setup_signal_handler(void)
2734{
2735 struct sigaction sa;
2736
2737 memset(&sa, 0, sizeof(sa));
2738
2739 sa.sa_handler = &signal_handler;
2740
2741 if (sigaction(SIGINT, &sa, NULL) < 0)
2742 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002743 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2744 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002745}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002746
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002747void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002748{
2749 struct timeval select_timeout;
2750 fd_set readfds;
2751 int retval;
2752
2753 FD_ZERO(&readfds);
2754 FD_SET(0, &readfds);
2755
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002756 if (!isatty(fileno(stdin))) {
2757 nanosleep(&interval_ts, NULL);
2758 return;
2759 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002760
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002761 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002762 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2763
2764 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002765 switch (getc(stdin)) {
2766 case 'q':
2767 exit_requested = 1;
2768 break;
2769 }
2770 /* make sure this manually-invoked interval is at least 1ms long */
2771 nanosleep(&one_msec, NULL);
2772 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002773}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002774
Len Brown103a8fe2010-10-22 23:53:03 -04002775void turbostat_loop()
2776{
Len Brownc98d5d92012-06-04 00:56:40 -04002777 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002778 int restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002779 int done_iters = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002780
Len Brown8aa2ed02017-07-15 14:57:37 -04002781 setup_signal_handler();
2782
Len Brown103a8fe2010-10-22 23:53:03 -04002783restart:
Len Browne52966c2012-11-08 22:38:05 -05002784 restarted++;
2785
Len Brown562a2d32016-02-26 23:48:05 -05002786 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002787 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002788 if (retval < -1) {
2789 exit(retval);
2790 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002791 if (restarted > 1) {
2792 exit(retval);
2793 }
Len Brownc98d5d92012-06-04 00:56:40 -04002794 re_initialize();
2795 goto restart;
2796 }
Len Browne52966c2012-11-08 22:38:05 -05002797 restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002798 done_iters = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002799 gettimeofday(&tv_even, (struct timezone *)NULL);
2800
2801 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002802 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002803 re_initialize();
2804 goto restart;
2805 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002806 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002807 if (snapshot_proc_sysfs_files())
2808 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002809 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002810 if (retval < -1) {
2811 exit(retval);
2812 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002813 re_initialize();
2814 goto restart;
2815 }
Len Brown103a8fe2010-10-22 23:53:03 -04002816 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002817 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002818 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2819 re_initialize();
2820 goto restart;
2821 }
Len Brownc98d5d92012-06-04 00:56:40 -04002822 compute_average(EVEN_COUNTERS);
2823 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002824 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002825 if (exit_requested)
2826 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002827 if (num_iterations && ++done_iters >= num_iterations)
2828 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002829 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002830 if (snapshot_proc_sysfs_files())
2831 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002832 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002833 if (retval < -1) {
2834 exit(retval);
2835 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002836 re_initialize();
2837 goto restart;
2838 }
Len Brown103a8fe2010-10-22 23:53:03 -04002839 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002840 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002841 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2842 re_initialize();
2843 goto restart;
2844 }
Len Brownc98d5d92012-06-04 00:56:40 -04002845 compute_average(ODD_COUNTERS);
2846 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002847 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002848 if (exit_requested)
2849 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002850 if (num_iterations && ++done_iters >= num_iterations)
2851 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002852 }
2853}
2854
2855void check_dev_msr()
2856{
2857 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002858 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002859
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002860 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2861 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002862 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2863 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002864}
2865
Len Brown98481e72014-08-15 00:36:50 -04002866void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002867{
Len Brown98481e72014-08-15 00:36:50 -04002868 struct __user_cap_header_struct cap_header_data;
2869 cap_user_header_t cap_header = &cap_header_data;
2870 struct __user_cap_data_struct cap_data_data;
2871 cap_user_data_t cap_data = &cap_data_data;
2872 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2873 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002874 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002875
2876 /* check for CAP_SYS_RAWIO */
2877 cap_header->pid = getpid();
2878 cap_header->version = _LINUX_CAPABILITY_VERSION;
2879 if (capget(cap_header, cap_data) < 0)
2880 err(-6, "capget(2) failed");
2881
2882 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2883 do_exit++;
2884 warnx("capget(CAP_SYS_RAWIO) failed,"
2885 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2886 }
2887
2888 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002889 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2890 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002891 do_exit++;
2892 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2893 }
2894
2895 /* if all else fails, thell them to be root */
2896 if (do_exit)
2897 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002898 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002899
2900 if (do_exit)
2901 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002902}
2903
Len Brownd7899442015-01-23 00:12:33 -05002904/*
2905 * NHM adds support for additional MSRs:
2906 *
2907 * MSR_SMI_COUNT 0x00000034
2908 *
Len Brownec0adc52015-11-12 02:42:31 -05002909 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002910 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002911 *
Len Browncf4cbe52017-01-01 13:08:33 -05002912 * MSR_MISC_PWR_MGMT 0x000001aa
2913 *
Len Brownd7899442015-01-23 00:12:33 -05002914 * MSR_PKG_C3_RESIDENCY 0x000003f8
2915 * MSR_PKG_C6_RESIDENCY 0x000003f9
2916 * MSR_CORE_C3_RESIDENCY 0x000003fc
2917 * MSR_CORE_C6_RESIDENCY 0x000003fd
2918 *
Len Brownee7e38e2015-02-09 23:39:45 -05002919 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002920 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002921 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002922 */
Len Brownee7e38e2015-02-09 23:39:45 -05002923int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002924{
Len Brownee7e38e2015-02-09 23:39:45 -05002925 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002926 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002927 int *pkg_cstate_limits;
2928
Len Brown103a8fe2010-10-22 23:53:03 -04002929 if (!genuine_intel)
2930 return 0;
2931
2932 if (family != 6)
2933 return 0;
2934
Len Brown21ed5572015-10-19 22:37:40 -04002935 bclk = discover_bclk(family, model);
2936
Len Brown103a8fe2010-10-22 23:53:03 -04002937 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002938 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2939 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002940 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002941 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2942 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2943 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2944 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002945 pkg_cstate_limits = nhm_pkg_cstate_limits;
2946 break;
Len Brown869ce692016-06-16 23:22:37 -04002947 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2948 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2949 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2950 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002951 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002952 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002953 break;
Len Brown869ce692016-06-16 23:22:37 -04002954 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2955 case INTEL_FAM6_HASWELL_X: /* HSX */
2956 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2957 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2958 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2959 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2960 case INTEL_FAM6_BROADWELL_X: /* BDX */
2961 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2962 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2963 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2964 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2965 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07002966 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brownee7e38e2015-02-09 23:39:45 -05002967 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002968 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002969 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002970 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2971 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002972 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002973 break;
Len Brown869ce692016-06-16 23:22:37 -04002974 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002975 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002976 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002977 pkg_cstate_limits = slv_pkg_cstate_limits;
2978 break;
Len Brown869ce692016-06-16 23:22:37 -04002979 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002980 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002981 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002982 break;
Len Brown869ce692016-06-16 23:22:37 -04002983 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002984 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002985 pkg_cstate_limits = phi_pkg_cstate_limits;
2986 break;
Len Brown869ce692016-06-16 23:22:37 -04002987 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002988 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002989 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002990 pkg_cstate_limits = bxt_pkg_cstate_limits;
2991 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002992 default:
2993 return 0;
2994 }
Len Brown1df2e552017-01-07 23:24:57 -05002995 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002996 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002997
Len Brownec0adc52015-11-12 02:42:31 -05002998 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002999 base_ratio = (msr >> 8) & 0xFF;
3000
3001 base_hz = base_ratio * bclk * 1000000;
3002 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003003 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04003004}
Len Brown0f7887c2017-01-12 23:49:18 -05003005/*
Len Brown495c76542017-02-08 02:41:51 -05003006 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05003007 *
3008 * MSR_CC6_DEMOTION_POLICY_CONFIG
3009 * MSR_MC6_DEMOTION_POLICY_CONFIG
3010 */
3011
3012int has_slv_msrs(unsigned int family, unsigned int model)
3013{
3014 if (!genuine_intel)
3015 return 0;
3016
3017 switch (model) {
3018 case INTEL_FAM6_ATOM_SILVERMONT1:
3019 case INTEL_FAM6_ATOM_MERRIFIELD:
3020 case INTEL_FAM6_ATOM_MOOREFIELD:
3021 return 1;
3022 }
3023 return 0;
3024}
Len Brown7170a372017-01-27 02:13:27 -05003025int is_dnv(unsigned int family, unsigned int model)
3026{
3027
3028 if (!genuine_intel)
3029 return 0;
3030
3031 switch (model) {
3032 case INTEL_FAM6_ATOM_DENVERTON:
3033 return 1;
3034 }
3035 return 0;
3036}
Len Brownade0eba2017-02-10 01:56:47 -05003037int is_bdx(unsigned int family, unsigned int model)
3038{
3039
3040 if (!genuine_intel)
3041 return 0;
3042
3043 switch (model) {
3044 case INTEL_FAM6_BROADWELL_X:
3045 case INTEL_FAM6_BROADWELL_XEON_D:
3046 return 1;
3047 }
3048 return 0;
3049}
Len Brown34c761972017-01-27 02:36:41 -05003050int is_skx(unsigned int family, unsigned int model)
3051{
3052
3053 if (!genuine_intel)
3054 return 0;
3055
3056 switch (model) {
3057 case INTEL_FAM6_SKYLAKE_X:
3058 return 1;
3059 }
3060 return 0;
3061}
Len Brown0f7887c2017-01-12 23:49:18 -05003062
Len Brown31e07522017-01-31 23:07:49 -05003063int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05003064{
Len Brown0f7887c2017-01-12 23:49:18 -05003065 if (has_slv_msrs(family, model))
3066 return 0;
3067
Len Brownd7899442015-01-23 00:12:33 -05003068 switch (model) {
3069 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04003070 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3071 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3072 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05003073 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05003074 return 0;
3075 default:
3076 return 1;
3077 }
3078}
Len Brown0f7887c2017-01-12 23:49:18 -05003079int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3080{
3081 if (has_slv_msrs(family, model))
3082 return 1;
3083
3084 return 0;
3085}
Len Brown6574a5d2012-09-21 00:01:31 -04003086int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3087{
3088 if (!genuine_intel)
3089 return 0;
3090
3091 if (family != 6)
3092 return 0;
3093
3094 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003095 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3096 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04003097 return 1;
3098 default:
3099 return 0;
3100 }
3101}
Len Brownfcd17212015-03-23 20:29:09 -04003102int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3103{
3104 if (!genuine_intel)
3105 return 0;
3106
3107 if (family != 6)
3108 return 0;
3109
3110 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003111 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003112 return 1;
3113 default:
3114 return 0;
3115 }
3116}
3117
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003118int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3119{
3120 if (!genuine_intel)
3121 return 0;
3122
3123 if (family != 6)
3124 return 0;
3125
3126 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003127 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003128 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003129 return 1;
3130 default:
3131 return 0;
3132 }
3133}
Len Brown31e07522017-01-31 23:07:49 -05003134int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3135{
3136 if (!genuine_intel)
3137 return 0;
3138
3139 if (family != 6)
3140 return 0;
3141
3142 switch (model) {
3143 case INTEL_FAM6_ATOM_GOLDMONT:
3144 case INTEL_FAM6_SKYLAKE_X:
3145 return 1;
3146 default:
3147 return 0;
3148 }
3149}
Len Brown6fb31432015-06-17 16:23:45 -04003150int has_config_tdp(unsigned int family, unsigned int model)
3151{
3152 if (!genuine_intel)
3153 return 0;
3154
3155 if (family != 6)
3156 return 0;
3157
3158 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003159 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3160 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3161 case INTEL_FAM6_HASWELL_X: /* HSX */
3162 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3163 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3164 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3165 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3166 case INTEL_FAM6_BROADWELL_X: /* BDX */
3167 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3168 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3169 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3170 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3171 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003172 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04003173 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003174
Len Brown869ce692016-06-16 23:22:37 -04003175 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003176 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003177 return 1;
3178 default:
3179 return 0;
3180 }
3181}
3182
Len Brownfcd17212015-03-23 20:29:09 -04003183static void
Colin Ian King1b693172016-03-02 13:50:25 +00003184dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003185{
3186 if (!do_nhm_platform_info)
3187 return;
3188
3189 dump_nhm_platform_info();
3190
3191 if (has_hsw_turbo_ratio_limit(family, model))
3192 dump_hsw_turbo_ratio_limits();
3193
3194 if (has_ivt_turbo_ratio_limit(family, model))
3195 dump_ivt_turbo_ratio_limits();
3196
Len Brown31e07522017-01-31 23:07:49 -05003197 if (has_turbo_ratio_limit(family, model))
3198 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003199
Len Brown0f7887c2017-01-12 23:49:18 -05003200 if (has_atom_turbo_ratio_limit(family, model))
3201 dump_atom_turbo_ratio_limits();
3202
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003203 if (has_knl_turbo_ratio_limit(family, model))
3204 dump_knl_turbo_ratio_limits();
3205
Len Brown6fb31432015-06-17 16:23:45 -04003206 if (has_config_tdp(family, model))
3207 dump_config_tdp();
3208
Len Brownfcd17212015-03-23 20:29:09 -04003209 dump_nhm_cst_cfg();
3210}
3211
Len Brown41618e62017-02-09 18:25:22 -05003212static void
3213dump_sysfs_cstate_config(void)
3214{
3215 char path[64];
3216 char name_buf[16];
3217 char desc[64];
3218 FILE *input;
3219 int state;
3220 char *sp;
3221
3222 if (!DO_BIC(BIC_sysfs))
3223 return;
3224
3225 for (state = 0; state < 10; ++state) {
3226
3227 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3228 base_cpu, state);
3229 input = fopen(path, "r");
3230 if (input == NULL)
3231 continue;
3232 fgets(name_buf, sizeof(name_buf), input);
3233
3234 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3235 sp = strchr(name_buf, '-');
3236 if (!sp)
3237 sp = strchrnul(name_buf, '\n');
3238 *sp = '\0';
3239
3240 fclose(input);
3241
3242 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3243 base_cpu, state);
3244 input = fopen(path, "r");
3245 if (input == NULL)
3246 continue;
3247 fgets(desc, sizeof(desc), input);
3248
3249 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3250 fclose(input);
3251 }
3252}
Len Brown7293fcc2017-02-22 00:11:12 -05003253static void
3254dump_sysfs_pstate_config(void)
3255{
3256 char path[64];
3257 char driver_buf[64];
3258 char governor_buf[64];
3259 FILE *input;
3260 int turbo;
3261
3262 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3263 base_cpu);
3264 input = fopen(path, "r");
3265 if (input == NULL) {
3266 fprintf(stderr, "NSFOD %s\n", path);
3267 return;
3268 }
3269 fgets(driver_buf, sizeof(driver_buf), input);
3270 fclose(input);
3271
3272 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3273 base_cpu);
3274 input = fopen(path, "r");
3275 if (input == NULL) {
3276 fprintf(stderr, "NSFOD %s\n", path);
3277 return;
3278 }
3279 fgets(governor_buf, sizeof(governor_buf), input);
3280 fclose(input);
3281
3282 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3283 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3284
3285 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3286 input = fopen(path, "r");
3287 if (input != NULL) {
3288 fscanf(input, "%d", &turbo);
3289 fprintf(outf, "cpufreq boost: %d\n", turbo);
3290 fclose(input);
3291 }
3292
3293 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3294 input = fopen(path, "r");
3295 if (input != NULL) {
3296 fscanf(input, "%d", &turbo);
3297 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3298 fclose(input);
3299 }
3300}
Len Brown41618e62017-02-09 18:25:22 -05003301
Len Brown6574a5d2012-09-21 00:01:31 -04003302
Len Brown889facb2012-11-08 00:48:57 -05003303/*
3304 * print_epb()
3305 * Decode the ENERGY_PERF_BIAS MSR
3306 */
3307int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3308{
3309 unsigned long long msr;
3310 char *epb_string;
3311 int cpu;
3312
3313 if (!has_epb)
3314 return 0;
3315
3316 cpu = t->cpu_id;
3317
3318 /* EPB is per-package */
3319 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3320 return 0;
3321
3322 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003323 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003324 return -1;
3325 }
3326
3327 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3328 return 0;
3329
Len Browne9be7dd2015-05-26 12:19:37 -04003330 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003331 case ENERGY_PERF_BIAS_PERFORMANCE:
3332 epb_string = "performance";
3333 break;
3334 case ENERGY_PERF_BIAS_NORMAL:
3335 epb_string = "balanced";
3336 break;
3337 case ENERGY_PERF_BIAS_POWERSAVE:
3338 epb_string = "powersave";
3339 break;
3340 default:
3341 epb_string = "custom";
3342 break;
3343 }
Len Brownb7d8c142016-02-13 23:36:17 -05003344 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003345
3346 return 0;
3347}
Len Brown7f5c2582015-12-01 01:36:39 -05003348/*
3349 * print_hwp()
3350 * Decode the MSR_HWP_CAPABILITIES
3351 */
3352int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3353{
3354 unsigned long long msr;
3355 int cpu;
3356
3357 if (!has_hwp)
3358 return 0;
3359
3360 cpu = t->cpu_id;
3361
3362 /* MSR_HWP_CAPABILITIES is per-package */
3363 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3364 return 0;
3365
3366 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003367 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003368 return -1;
3369 }
3370
3371 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3372 return 0;
3373
Len Brownb7d8c142016-02-13 23:36:17 -05003374 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003375 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3376
3377 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3378 if ((msr & (1 << 0)) == 0)
3379 return 0;
3380
3381 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3382 return 0;
3383
Len Brownb7d8c142016-02-13 23:36:17 -05003384 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003385 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003386 cpu, msr,
3387 (unsigned int)HWP_HIGHEST_PERF(msr),
3388 (unsigned int)HWP_GUARANTEED_PERF(msr),
3389 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3390 (unsigned int)HWP_LOWEST_PERF(msr));
3391
3392 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3393 return 0;
3394
Len Brownb7d8c142016-02-13 23:36:17 -05003395 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003396 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003397 cpu, msr,
3398 (unsigned int)(((msr) >> 0) & 0xff),
3399 (unsigned int)(((msr) >> 8) & 0xff),
3400 (unsigned int)(((msr) >> 16) & 0xff),
3401 (unsigned int)(((msr) >> 24) & 0xff),
3402 (unsigned int)(((msr) >> 32) & 0xff3),
3403 (unsigned int)(((msr) >> 42) & 0x1));
3404
3405 if (has_hwp_pkg) {
3406 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3407 return 0;
3408
Len Brownb7d8c142016-02-13 23:36:17 -05003409 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003410 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003411 cpu, msr,
3412 (unsigned int)(((msr) >> 0) & 0xff),
3413 (unsigned int)(((msr) >> 8) & 0xff),
3414 (unsigned int)(((msr) >> 16) & 0xff),
3415 (unsigned int)(((msr) >> 24) & 0xff),
3416 (unsigned int)(((msr) >> 32) & 0xff3));
3417 }
3418 if (has_hwp_notify) {
3419 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3420 return 0;
3421
Len Brownb7d8c142016-02-13 23:36:17 -05003422 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003423 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3424 cpu, msr,
3425 ((msr) & 0x1) ? "EN" : "Dis",
3426 ((msr) & 0x2) ? "EN" : "Dis");
3427 }
3428 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3429 return 0;
3430
Len Brownb7d8c142016-02-13 23:36:17 -05003431 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003432 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3433 cpu, msr,
3434 ((msr) & 0x1) ? "" : "No-",
3435 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003436
3437 return 0;
3438}
3439
Len Brown3a9a9412014-08-15 02:39:52 -04003440/*
3441 * print_perf_limit()
3442 */
3443int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3444{
3445 unsigned long long msr;
3446 int cpu;
3447
3448 cpu = t->cpu_id;
3449
3450 /* per-package */
3451 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3452 return 0;
3453
3454 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003455 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003456 return -1;
3457 }
3458
3459 if (do_core_perf_limit_reasons) {
3460 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003461 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3462 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003463 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003464 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003465 (msr & 1 << 13) ? "Transitions, " : "",
3466 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3467 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3468 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3469 (msr & 1 << 9) ? "CorePwr, " : "",
3470 (msr & 1 << 8) ? "Amps, " : "",
3471 (msr & 1 << 6) ? "VR-Therm, " : "",
3472 (msr & 1 << 5) ? "Auto-HWP, " : "",
3473 (msr & 1 << 4) ? "Graphics, " : "",
3474 (msr & 1 << 2) ? "bit2, " : "",
3475 (msr & 1 << 1) ? "ThermStatus, " : "",
3476 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003477 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 -04003478 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003479 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003480 (msr & 1 << 29) ? "Transitions, " : "",
3481 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3482 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3483 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3484 (msr & 1 << 25) ? "CorePwr, " : "",
3485 (msr & 1 << 24) ? "Amps, " : "",
3486 (msr & 1 << 22) ? "VR-Therm, " : "",
3487 (msr & 1 << 21) ? "Auto-HWP, " : "",
3488 (msr & 1 << 20) ? "Graphics, " : "",
3489 (msr & 1 << 18) ? "bit18, " : "",
3490 (msr & 1 << 17) ? "ThermStatus, " : "",
3491 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003492
3493 }
3494 if (do_gfx_perf_limit_reasons) {
3495 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003496 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3497 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003498 (msr & 1 << 0) ? "PROCHOT, " : "",
3499 (msr & 1 << 1) ? "ThermStatus, " : "",
3500 (msr & 1 << 4) ? "Graphics, " : "",
3501 (msr & 1 << 6) ? "VR-Therm, " : "",
3502 (msr & 1 << 8) ? "Amps, " : "",
3503 (msr & 1 << 9) ? "GFXPwr, " : "",
3504 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3505 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003506 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003507 (msr & 1 << 16) ? "PROCHOT, " : "",
3508 (msr & 1 << 17) ? "ThermStatus, " : "",
3509 (msr & 1 << 20) ? "Graphics, " : "",
3510 (msr & 1 << 22) ? "VR-Therm, " : "",
3511 (msr & 1 << 24) ? "Amps, " : "",
3512 (msr & 1 << 25) ? "GFXPwr, " : "",
3513 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3514 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3515 }
3516 if (do_ring_perf_limit_reasons) {
3517 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003518 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3519 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003520 (msr & 1 << 0) ? "PROCHOT, " : "",
3521 (msr & 1 << 1) ? "ThermStatus, " : "",
3522 (msr & 1 << 6) ? "VR-Therm, " : "",
3523 (msr & 1 << 8) ? "Amps, " : "",
3524 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3525 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003526 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003527 (msr & 1 << 16) ? "PROCHOT, " : "",
3528 (msr & 1 << 17) ? "ThermStatus, " : "",
3529 (msr & 1 << 22) ? "VR-Therm, " : "",
3530 (msr & 1 << 24) ? "Amps, " : "",
3531 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3532 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3533 }
3534 return 0;
3535}
3536
Len Brown889facb2012-11-08 00:48:57 -05003537#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3538#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3539
Colin Ian King1b693172016-03-02 13:50:25 +00003540double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003541{
3542 unsigned long long msr;
3543
3544 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003545 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003546 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3547
3548 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003549 case INTEL_FAM6_ATOM_SILVERMONT1:
3550 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003551 return 30.0;
3552 default:
3553 return 135.0;
3554 }
3555}
3556
Andrey Semin40ee8e32014-12-05 00:07:00 -05003557/*
3558 * rapl_dram_energy_units_probe()
3559 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3560 */
3561static double
3562rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3563{
3564 /* only called for genuine_intel, family 6 */
3565
3566 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003567 case INTEL_FAM6_HASWELL_X: /* HSX */
3568 case INTEL_FAM6_BROADWELL_X: /* BDX */
3569 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3570 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003571 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003572 return (rapl_dram_energy_units = 15.3 / 1000000);
3573 default:
3574 return (rapl_energy_units);
3575 }
3576}
3577
Len Brown144b44b2013-11-09 00:30:16 -05003578
Len Brown889facb2012-11-08 00:48:57 -05003579/*
3580 * rapl_probe()
3581 *
Len Brown144b44b2013-11-09 00:30:16 -05003582 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003583 */
3584void rapl_probe(unsigned int family, unsigned int model)
3585{
3586 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003587 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003588 double tdp;
3589
3590 if (!genuine_intel)
3591 return;
3592
3593 if (family != 6)
3594 return;
3595
3596 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003597 case INTEL_FAM6_SANDYBRIDGE:
3598 case INTEL_FAM6_IVYBRIDGE:
3599 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3600 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3601 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3602 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3603 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003604 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003605 if (rapl_joules) {
3606 BIC_PRESENT(BIC_Pkg_J);
3607 BIC_PRESENT(BIC_Cor_J);
3608 BIC_PRESENT(BIC_GFX_J);
3609 } else {
3610 BIC_PRESENT(BIC_PkgWatt);
3611 BIC_PRESENT(BIC_CorWatt);
3612 BIC_PRESENT(BIC_GFXWatt);
3613 }
Len Brown889facb2012-11-08 00:48:57 -05003614 break;
Len Brown869ce692016-06-16 23:22:37 -04003615 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003616 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003617 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003618 if (rapl_joules)
3619 BIC_PRESENT(BIC_Pkg_J);
3620 else
3621 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003622 break;
Len Brown869ce692016-06-16 23:22:37 -04003623 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3624 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3625 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3626 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003627 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown81824922017-03-04 17:23:07 -05003628 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 -05003629 BIC_PRESENT(BIC_PKG__);
3630 BIC_PRESENT(BIC_RAM__);
3631 if (rapl_joules) {
3632 BIC_PRESENT(BIC_Pkg_J);
3633 BIC_PRESENT(BIC_Cor_J);
3634 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003635 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003636 } else {
3637 BIC_PRESENT(BIC_PkgWatt);
3638 BIC_PRESENT(BIC_CorWatt);
3639 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003640 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003641 }
Len Brown0b2bb692015-03-26 00:50:30 -04003642 break;
Len Brown869ce692016-06-16 23:22:37 -04003643 case INTEL_FAM6_HASWELL_X: /* HSX */
3644 case INTEL_FAM6_BROADWELL_X: /* BDX */
3645 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3646 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3647 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003648 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003649 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 -05003650 BIC_PRESENT(BIC_PKG__);
3651 BIC_PRESENT(BIC_RAM__);
3652 if (rapl_joules) {
3653 BIC_PRESENT(BIC_Pkg_J);
3654 BIC_PRESENT(BIC_RAM_J);
3655 } else {
3656 BIC_PRESENT(BIC_PkgWatt);
3657 BIC_PRESENT(BIC_RAMWatt);
3658 }
Len Browne6f9bb32013-12-03 02:19:19 -05003659 break;
Len Brown869ce692016-06-16 23:22:37 -04003660 case INTEL_FAM6_SANDYBRIDGE_X:
3661 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003662 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 -05003663 BIC_PRESENT(BIC_PKG__);
3664 BIC_PRESENT(BIC_RAM__);
3665 if (rapl_joules) {
3666 BIC_PRESENT(BIC_Pkg_J);
3667 BIC_PRESENT(BIC_Cor_J);
3668 BIC_PRESENT(BIC_RAM_J);
3669 } else {
3670 BIC_PRESENT(BIC_PkgWatt);
3671 BIC_PRESENT(BIC_CorWatt);
3672 BIC_PRESENT(BIC_RAMWatt);
3673 }
Len Brown144b44b2013-11-09 00:30:16 -05003674 break;
Len Brown869ce692016-06-16 23:22:37 -04003675 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3676 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003677 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003678 if (rapl_joules) {
3679 BIC_PRESENT(BIC_Pkg_J);
3680 BIC_PRESENT(BIC_Cor_J);
3681 } else {
3682 BIC_PRESENT(BIC_PkgWatt);
3683 BIC_PRESENT(BIC_CorWatt);
3684 }
Len Brown889facb2012-11-08 00:48:57 -05003685 break;
Len Brown869ce692016-06-16 23:22:37 -04003686 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003687 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 -05003688 BIC_PRESENT(BIC_PKG__);
3689 BIC_PRESENT(BIC_RAM__);
3690 if (rapl_joules) {
3691 BIC_PRESENT(BIC_Pkg_J);
3692 BIC_PRESENT(BIC_Cor_J);
3693 BIC_PRESENT(BIC_RAM_J);
3694 } else {
3695 BIC_PRESENT(BIC_PkgWatt);
3696 BIC_PRESENT(BIC_CorWatt);
3697 BIC_PRESENT(BIC_RAMWatt);
3698 }
Jacob Pan0f644902016-06-16 09:48:22 -07003699 break;
Len Brown889facb2012-11-08 00:48:57 -05003700 default:
3701 return;
3702 }
3703
3704 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003705 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003706 return;
3707
3708 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003709 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003710 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3711 else
3712 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003713
Andrey Semin40ee8e32014-12-05 00:07:00 -05003714 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3715
Len Brown144b44b2013-11-09 00:30:16 -05003716 time_unit = msr >> 16 & 0xF;
3717 if (time_unit == 0)
3718 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003719
Len Brown144b44b2013-11-09 00:30:16 -05003720 rapl_time_units = 1.0 / (1 << (time_unit));
3721
3722 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003723
3724 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003725 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003726 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003727
3728 return;
3729}
3730
Colin Ian King1b693172016-03-02 13:50:25 +00003731void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003732{
3733 if (!genuine_intel)
3734 return;
3735
3736 if (family != 6)
3737 return;
3738
3739 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003740 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3741 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3742 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003743 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003744 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003745 do_core_perf_limit_reasons = 1;
3746 do_ring_perf_limit_reasons = 1;
3747 default:
3748 return;
3749 }
3750}
3751
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003752void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3753{
3754 if (is_skx(family, model) || is_bdx(family, model))
3755 has_automatic_cstate_conversion = 1;
3756}
3757
Len Brown889facb2012-11-08 00:48:57 -05003758int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3759{
3760 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003761 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003762 int cpu;
3763
3764 if (!(do_dts || do_ptm))
3765 return 0;
3766
3767 cpu = t->cpu_id;
3768
3769 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003770 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003771 return 0;
3772
3773 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003774 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003775 return -1;
3776 }
3777
3778 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3779 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3780 return 0;
3781
3782 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003783 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003784 cpu, msr, tcc_activation_temp - dts);
3785
Len Brown889facb2012-11-08 00:48:57 -05003786 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3787 return 0;
3788
3789 dts = (msr >> 16) & 0x7F;
3790 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003791 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003792 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003793 }
3794
3795
Len Brownf4896fa52017-03-04 18:10:45 -05003796 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003797 unsigned int resolution;
3798
3799 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3800 return 0;
3801
3802 dts = (msr >> 16) & 0x7F;
3803 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003804 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003805 cpu, msr, tcc_activation_temp - dts, resolution);
3806
Len Brown889facb2012-11-08 00:48:57 -05003807 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3808 return 0;
3809
3810 dts = (msr >> 16) & 0x7F;
3811 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003812 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003813 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003814 }
3815
3816 return 0;
3817}
Len Brown36229892016-02-26 20:51:02 -05003818
Len Brown889facb2012-11-08 00:48:57 -05003819void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3820{
Len Brownb7d8c142016-02-13 23:36:17 -05003821 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003822 cpu, label,
3823 ((msr >> 15) & 1) ? "EN" : "DIS",
3824 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3825 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3826 (((msr >> 16) & 1) ? "EN" : "DIS"));
3827
3828 return;
3829}
3830
3831int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3832{
3833 unsigned long long msr;
3834 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003835
3836 if (!do_rapl)
3837 return 0;
3838
3839 /* RAPL counters are per package, so print only for 1st thread/package */
3840 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3841 return 0;
3842
3843 cpu = t->cpu_id;
3844 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003845 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003846 return -1;
3847 }
3848
3849 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3850 return -1;
3851
Len Brown96e47152017-01-21 02:26:00 -05003852 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3853 rapl_power_units, rapl_energy_units, rapl_time_units);
3854
Len Brown144b44b2013-11-09 00:30:16 -05003855 if (do_rapl & RAPL_PKG_POWER_INFO) {
3856
Len Brown889facb2012-11-08 00:48:57 -05003857 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3858 return -5;
3859
3860
Len Brownb7d8c142016-02-13 23:36:17 -05003861 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 -05003862 cpu, msr,
3863 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3864 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3865 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3866 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3867
Len Brown144b44b2013-11-09 00:30:16 -05003868 }
3869 if (do_rapl & RAPL_PKG) {
3870
Len Brown889facb2012-11-08 00:48:57 -05003871 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3872 return -9;
3873
Len Brownb7d8c142016-02-13 23:36:17 -05003874 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003875 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003876
3877 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003878 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003879 cpu,
3880 ((msr >> 47) & 1) ? "EN" : "DIS",
3881 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3882 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3883 ((msr >> 48) & 1) ? "EN" : "DIS");
3884 }
3885
Len Brown0b2bb692015-03-26 00:50:30 -04003886 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003887 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3888 return -6;
3889
Len Brownb7d8c142016-02-13 23:36:17 -05003890 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 -05003891 cpu, msr,
3892 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3893 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3894 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3895 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003896 }
3897 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003898 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3899 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003900 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003901 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003902
3903 print_power_limit_msr(cpu, msr, "DRAM Limit");
3904 }
Len Brown144b44b2013-11-09 00:30:16 -05003905 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003906 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3907 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003908
Len Brown96e47152017-01-21 02:26:00 -05003909 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003910 }
Jacob Pan91484942016-06-16 09:48:20 -07003911 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003912 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3913 return -9;
3914 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3915 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3916 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003917 }
3918 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003919 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3920 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003921
Len Brown96e47152017-01-21 02:26:00 -05003922 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003923
Len Brown96e47152017-01-21 02:26:00 -05003924 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3925 return -9;
3926 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3927 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3928 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003929 }
3930 return 0;
3931}
3932
Len Brownd7899442015-01-23 00:12:33 -05003933/*
3934 * SNB adds support for additional MSRs:
3935 *
3936 * MSR_PKG_C7_RESIDENCY 0x000003fa
3937 * MSR_CORE_C7_RESIDENCY 0x000003fe
3938 * MSR_PKG_C2_RESIDENCY 0x0000060d
3939 */
Len Brown103a8fe2010-10-22 23:53:03 -04003940
Len Brownd7899442015-01-23 00:12:33 -05003941int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003942{
3943 if (!genuine_intel)
3944 return 0;
3945
3946 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003947 case INTEL_FAM6_SANDYBRIDGE:
3948 case INTEL_FAM6_SANDYBRIDGE_X:
3949 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3950 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3951 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3952 case INTEL_FAM6_HASWELL_X: /* HSW */
3953 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3954 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3955 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3956 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3957 case INTEL_FAM6_BROADWELL_X: /* BDX */
3958 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3959 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3960 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3961 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3962 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003963 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04003964 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3965 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003966 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003967 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003968 return 1;
3969 }
3970 return 0;
3971}
3972
Len Brownd7899442015-01-23 00:12:33 -05003973/*
3974 * HSW adds support for additional MSRs:
3975 *
Len Brown5a634262016-04-06 17:15:55 -04003976 * MSR_PKG_C8_RESIDENCY 0x00000630
3977 * MSR_PKG_C9_RESIDENCY 0x00000631
3978 * MSR_PKG_C10_RESIDENCY 0x00000632
3979 *
3980 * MSR_PKGC8_IRTL 0x00000633
3981 * MSR_PKGC9_IRTL 0x00000634
3982 * MSR_PKGC10_IRTL 0x00000635
3983 *
Len Brownd7899442015-01-23 00:12:33 -05003984 */
3985int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003986{
3987 if (!genuine_intel)
3988 return 0;
3989
3990 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003991 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3992 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3993 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3994 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3995 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3996 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003997 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04003998 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003999 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004000 return 1;
4001 }
4002 return 0;
4003}
4004
Len Brown0b2bb692015-03-26 00:50:30 -04004005/*
4006 * SKL adds support for additional MSRS:
4007 *
4008 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4009 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4010 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4011 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4012 */
4013int has_skl_msrs(unsigned int family, unsigned int model)
4014{
4015 if (!genuine_intel)
4016 return 0;
4017
4018 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004019 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4020 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4021 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4022 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004023 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown0b2bb692015-03-26 00:50:30 -04004024 return 1;
4025 }
4026 return 0;
4027}
4028
Len Brown144b44b2013-11-09 00:30:16 -05004029int is_slm(unsigned int family, unsigned int model)
4030{
4031 if (!genuine_intel)
4032 return 0;
4033 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004034 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
4035 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05004036 return 1;
4037 }
4038 return 0;
4039}
4040
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004041int is_knl(unsigned int family, unsigned int model)
4042{
4043 if (!genuine_intel)
4044 return 0;
4045 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004046 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05004047 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004048 return 1;
4049 }
4050 return 0;
4051}
4052
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004053int is_cnl(unsigned int family, unsigned int model)
4054{
4055 if (!genuine_intel)
4056 return 0;
4057
4058 switch (model) {
4059 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4060 return 1;
4061 }
4062
4063 return 0;
4064}
4065
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004066unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4067{
4068 if (is_knl(family, model))
4069 return 1024;
4070 return 1;
4071}
4072
Len Brown144b44b2013-11-09 00:30:16 -05004073#define SLM_BCLK_FREQS 5
4074double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4075
4076double slm_bclk(void)
4077{
4078 unsigned long long msr = 3;
4079 unsigned int i;
4080 double freq;
4081
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004082 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05004083 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05004084
4085 i = msr & 0xf;
4086 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05004087 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01004088 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05004089 }
4090 freq = slm_freq_table[i];
4091
Len Brown96e47152017-01-21 02:26:00 -05004092 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05004093 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05004094
4095 return freq;
4096}
4097
Len Brown103a8fe2010-10-22 23:53:03 -04004098double discover_bclk(unsigned int family, unsigned int model)
4099{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01004100 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04004101 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05004102 else if (is_slm(family, model))
4103 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04004104 else
4105 return 133.33;
4106}
4107
Len Brown889facb2012-11-08 00:48:57 -05004108/*
4109 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4110 * the Thermal Control Circuit (TCC) activates.
4111 * This is usually equal to tjMax.
4112 *
4113 * Older processors do not have this MSR, so there we guess,
4114 * but also allow cmdline over-ride with -T.
4115 *
4116 * Several MSR temperature values are in units of degrees-C
4117 * below this value, including the Digital Thermal Sensor (DTS),
4118 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4119 */
4120int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4121{
4122 unsigned long long msr;
4123 unsigned int target_c_local;
4124 int cpu;
4125
4126 /* tcc_activation_temp is used only for dts or ptm */
4127 if (!(do_dts || do_ptm))
4128 return 0;
4129
4130 /* this is a per-package concept */
4131 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4132 return 0;
4133
4134 cpu = t->cpu_id;
4135 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004136 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004137 return -1;
4138 }
4139
4140 if (tcc_activation_temp_override != 0) {
4141 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004142 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004143 cpu, tcc_activation_temp);
4144 return 0;
4145 }
4146
4147 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004148 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004149 goto guess;
4150
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004151 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004152 goto guess;
4153
Jean Delvare34821242014-05-01 11:40:19 +02004154 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004155
Len Brown96e47152017-01-21 02:26:00 -05004156 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004157 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004158 cpu, msr, target_c_local);
4159
Jean Delvare34821242014-05-01 11:40:19 +02004160 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004161 goto guess;
4162
4163 tcc_activation_temp = target_c_local;
4164
4165 return 0;
4166
4167guess:
4168 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004169 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004170 cpu, tcc_activation_temp);
4171
4172 return 0;
4173}
Len Brown69807a62015-11-21 12:22:47 -05004174
Len Brownaa8d8cc2016-03-11 13:26:03 -05004175void decode_feature_control_msr(void)
4176{
4177 unsigned long long msr;
4178
4179 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4180 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4181 base_cpu, msr,
4182 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4183 msr & (1 << 18) ? "SGX" : "");
4184}
4185
Len Brown69807a62015-11-21 12:22:47 -05004186void decode_misc_enable_msr(void)
4187{
4188 unsigned long long msr;
4189
Len Brownf26b1512017-06-23 20:45:54 -07004190 if (!genuine_intel)
4191 return;
4192
Len Brown69807a62015-11-21 12:22:47 -05004193 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004194 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004195 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004196 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4197 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004198 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004199 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4200 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004201}
4202
Len Brown33148d62017-01-21 01:26:16 -05004203void decode_misc_feature_control(void)
4204{
4205 unsigned long long msr;
4206
4207 if (!has_misc_feature_control)
4208 return;
4209
4210 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4211 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4212 base_cpu, msr,
4213 msr & (0 << 0) ? "No-" : "",
4214 msr & (1 << 0) ? "No-" : "",
4215 msr & (2 << 0) ? "No-" : "",
4216 msr & (3 << 0) ? "No-" : "");
4217}
Len Brownf0057312015-12-03 01:35:36 -05004218/*
4219 * Decode MSR_MISC_PWR_MGMT
4220 *
4221 * Decode the bits according to the Nehalem documentation
4222 * bit[0] seems to continue to have same meaning going forward
4223 * bit[1] less so...
4224 */
4225void decode_misc_pwr_mgmt_msr(void)
4226{
4227 unsigned long long msr;
4228
4229 if (!do_nhm_platform_info)
4230 return;
4231
Len Browncf4cbe52017-01-01 13:08:33 -05004232 if (no_MSR_MISC_PWR_MGMT)
4233 return;
4234
Len Brownf0057312015-12-03 01:35:36 -05004235 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004236 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 -05004237 base_cpu, msr,
4238 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004239 msr & (1 << 1) ? "EN" : "DIS",
4240 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004241}
Len Brown71616c82017-01-07 22:37:48 -05004242/*
4243 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4244 *
4245 * This MSRs are present on Silvermont processors,
4246 * Intel Atom processor E3000 series (Baytrail), and friends.
4247 */
4248void decode_c6_demotion_policy_msr(void)
4249{
4250 unsigned long long msr;
4251
4252 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4253 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4254 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4255
4256 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4257 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4258 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4259}
Len Brown7f5c2582015-12-01 01:36:39 -05004260
Len Brownfcd17212015-03-23 20:29:09 -04004261void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004262{
Len Brown61a87ba2015-11-23 02:30:51 -05004263 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004264 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004265 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004266
4267 eax = ebx = ecx = edx = 0;
4268
Len Brown5aea2f72016-03-13 03:14:35 -04004269 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004270
4271 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4272 genuine_intel = 1;
4273
Len Brown96e47152017-01-21 02:26:00 -05004274 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004275 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004276 (char *)&ebx, (char *)&edx, (char *)&ecx);
4277
Len Brown5aea2f72016-03-13 03:14:35 -04004278 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004279 family = (fms >> 8) & 0xf;
4280 model = (fms >> 4) & 0xf;
4281 stepping = fms & 0xf;
4282 if (family == 6 || family == 0xf)
4283 model += ((fms >> 16) & 0xf) << 4;
4284
Len Brown96e47152017-01-21 02:26:00 -05004285 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004286 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004287 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004288 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004289 ecx & (1 << 0) ? "SSE3" : "-",
4290 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004291 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004292 ecx & (1 << 7) ? "EIST" : "-",
4293 ecx & (1 << 8) ? "TM2" : "-",
4294 edx & (1 << 4) ? "TSC" : "-",
4295 edx & (1 << 5) ? "MSR" : "-",
4296 edx & (1 << 22) ? "ACPI-TM" : "-",
4297 edx & (1 << 29) ? "TM" : "-");
4298 }
Len Brown103a8fe2010-10-22 23:53:03 -04004299
Josh Triplettb2c95d92013-08-20 17:20:18 -07004300 if (!(edx & (1 << 5)))
4301 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004302
4303 /*
4304 * check max extended function levels of CPUID.
4305 * This is needed to check for invariant TSC.
4306 * This check is valid for both Intel and AMD.
4307 */
4308 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004309 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004310
Len Brown61a87ba2015-11-23 02:30:51 -05004311 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004312
Len Brownd7899442015-01-23 00:12:33 -05004313 /*
4314 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4315 * this check is valid for both Intel and AMD
4316 */
Len Brown5aea2f72016-03-13 03:14:35 -04004317 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004318 has_invariant_tsc = edx & (1 << 8);
4319 }
Len Brown103a8fe2010-10-22 23:53:03 -04004320
4321 /*
4322 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4323 * this check is valid for both Intel and AMD
4324 */
4325
Len Brown5aea2f72016-03-13 03:14:35 -04004326 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004327 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004328 if (has_aperf) {
4329 BIC_PRESENT(BIC_Avg_MHz);
4330 BIC_PRESENT(BIC_Busy);
4331 BIC_PRESENT(BIC_Bzy_MHz);
4332 }
Len Brown889facb2012-11-08 00:48:57 -05004333 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004334 if (do_dts)
4335 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004336 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004337 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004338 if (do_ptm)
4339 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004340 has_hwp = eax & (1 << 7);
4341 has_hwp_notify = eax & (1 << 8);
4342 has_hwp_activity_window = eax & (1 << 9);
4343 has_hwp_epp = eax & (1 << 10);
4344 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004345 has_epb = ecx & (1 << 3);
4346
Len Brown96e47152017-01-21 02:26:00 -05004347 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004348 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004349 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4350 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004351 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004352 do_dts ? "" : "No-",
4353 do_ptm ? "" : "No-",
4354 has_hwp ? "" : "No-",
4355 has_hwp_notify ? "" : "No-",
4356 has_hwp_activity_window ? "" : "No-",
4357 has_hwp_epp ? "" : "No-",
4358 has_hwp_pkg ? "" : "No-",
4359 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004360
Len Brown96e47152017-01-21 02:26:00 -05004361 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004362 decode_misc_enable_msr();
4363
Len Brown33148d62017-01-21 01:26:16 -05004364
Len Brown96e47152017-01-21 02:26:00 -05004365 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004366 int has_sgx;
4367
4368 ecx = 0;
4369
4370 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4371
4372 has_sgx = ebx & (1 << 2);
4373 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4374
4375 if (has_sgx)
4376 decode_feature_control_msr();
4377 }
4378
Len Brown61a87ba2015-11-23 02:30:51 -05004379 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004380 unsigned int eax_crystal;
4381 unsigned int ebx_tsc;
4382
4383 /*
4384 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4385 */
4386 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004387 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004388
4389 if (ebx_tsc != 0) {
4390
Len Brown96e47152017-01-21 02:26:00 -05004391 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004392 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004393 eax_crystal, ebx_tsc, crystal_hz);
4394
4395 if (crystal_hz == 0)
4396 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004397 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4398 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4399 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4400 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004401 crystal_hz = 24000000; /* 24.0 MHz */
4402 break;
Len Brown7268d402016-12-01 23:10:39 -05004403 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004404 crystal_hz = 25000000; /* 25.0 MHz */
4405 break;
Len Brown869ce692016-06-16 23:22:37 -04004406 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004407 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004408 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004409 break;
4410 default:
4411 crystal_hz = 0;
4412 }
4413
4414 if (crystal_hz) {
4415 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004416 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004417 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004418 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4419 }
4420 }
4421 }
Len Brown61a87ba2015-11-23 02:30:51 -05004422 if (max_level >= 0x16) {
4423 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4424
4425 /*
4426 * CPUID 16H Base MHz, Max MHz, Bus MHz
4427 */
4428 base_mhz = max_mhz = bus_mhz = edx = 0;
4429
Len Brown5aea2f72016-03-13 03:14:35 -04004430 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004431 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004432 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004433 base_mhz, max_mhz, bus_mhz);
4434 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004435
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004436 if (has_aperf)
4437 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4438
Len Brown812db3f2017-02-10 00:25:41 -05004439 BIC_PRESENT(BIC_IRQ);
4440 BIC_PRESENT(BIC_TSC_MHz);
4441
4442 if (probe_nhm_msrs(family, model)) {
4443 do_nhm_platform_info = 1;
4444 BIC_PRESENT(BIC_CPU_c1);
4445 BIC_PRESENT(BIC_CPU_c3);
4446 BIC_PRESENT(BIC_CPU_c6);
4447 BIC_PRESENT(BIC_SMI);
4448 }
Len Brownd7899442015-01-23 00:12:33 -05004449 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004450
4451 if (do_snb_cstates)
4452 BIC_PRESENT(BIC_CPU_c7);
4453
Len Brown5a634262016-04-06 17:15:55 -04004454 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004455 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4456 BIC_PRESENT(BIC_Pkgpc2);
4457 if (pkg_cstate_limit >= PCL__3)
4458 BIC_PRESENT(BIC_Pkgpc3);
4459 if (pkg_cstate_limit >= PCL__6)
4460 BIC_PRESENT(BIC_Pkgpc6);
4461 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4462 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004463 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004464 BIC_NOT_PRESENT(BIC_Pkgpc2);
4465 BIC_NOT_PRESENT(BIC_Pkgpc3);
4466 BIC_PRESENT(BIC_Pkgpc6);
4467 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004468 BIC_PRESENT(BIC_Mod_c6);
4469 use_c1_residency_msr = 1;
4470 }
Len Brown7170a372017-01-27 02:13:27 -05004471 if (is_dnv(family, model)) {
4472 BIC_PRESENT(BIC_CPU_c1);
4473 BIC_NOT_PRESENT(BIC_CPU_c3);
4474 BIC_NOT_PRESENT(BIC_Pkgpc3);
4475 BIC_NOT_PRESENT(BIC_CPU_c7);
4476 BIC_NOT_PRESENT(BIC_Pkgpc7);
4477 use_c1_residency_msr = 1;
4478 }
Len Brown34c761972017-01-27 02:36:41 -05004479 if (is_skx(family, model)) {
4480 BIC_NOT_PRESENT(BIC_CPU_c3);
4481 BIC_NOT_PRESENT(BIC_Pkgpc3);
4482 BIC_NOT_PRESENT(BIC_CPU_c7);
4483 BIC_NOT_PRESENT(BIC_Pkgpc7);
4484 }
Len Brownade0eba2017-02-10 01:56:47 -05004485 if (is_bdx(family, model)) {
4486 BIC_NOT_PRESENT(BIC_CPU_c7);
4487 BIC_NOT_PRESENT(BIC_Pkgpc7);
4488 }
Len Brown0f47c082017-01-27 00:50:45 -05004489 if (has_hsw_msrs(family, model)) {
4490 BIC_PRESENT(BIC_Pkgpc8);
4491 BIC_PRESENT(BIC_Pkgpc9);
4492 BIC_PRESENT(BIC_Pkgpc10);
4493 }
Len Brown5a634262016-04-06 17:15:55 -04004494 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004495 if (has_skl_msrs(family, model)) {
4496 BIC_PRESENT(BIC_Totl_c0);
4497 BIC_PRESENT(BIC_Any_c0);
4498 BIC_PRESENT(BIC_GFX_c0);
4499 BIC_PRESENT(BIC_CPUGFX);
4500 }
Len Brown144b44b2013-11-09 00:30:16 -05004501 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004502 do_knl_cstates = is_knl(family, model);
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004503 do_cnl_cstates = is_cnl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004504
Len Brown96e47152017-01-21 02:26:00 -05004505 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004506 decode_misc_pwr_mgmt_msr();
4507
Len Brown96e47152017-01-21 02:26:00 -05004508 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004509 decode_c6_demotion_policy_msr();
4510
Len Brown889facb2012-11-08 00:48:57 -05004511 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004512 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004513 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004514
Len Brown96e47152017-01-21 02:26:00 -05004515 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004516 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004517
Len Brown41618e62017-02-09 18:25:22 -05004518 if (!quiet)
4519 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004520 if (!quiet)
4521 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004522
Len Browna2b7b742015-09-26 00:12:38 -04004523 if (has_skl_msrs(family, model))
4524 calculate_tsc_tweak();
4525
Len Brown812db3f2017-02-10 00:25:41 -05004526 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4527 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004528
Len Brown812db3f2017-02-10 00:25:41 -05004529 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4530 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004531
Len Brownbe0e54c2018-06-01 12:35:53 -04004532 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4533 BIC_PRESENT(BIC_CPU_LPI);
4534 else
4535 BIC_NOT_PRESENT(BIC_CPU_LPI);
4536
4537 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4538 BIC_PRESENT(BIC_SYS_LPI);
4539 else
4540 BIC_NOT_PRESENT(BIC_SYS_LPI);
4541
Len Brown96e47152017-01-21 02:26:00 -05004542 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004543 decode_misc_feature_control();
4544
Len Brown889facb2012-11-08 00:48:57 -05004545 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004546}
4547
Len Brown103a8fe2010-10-22 23:53:03 -04004548
4549/*
4550 * in /dev/cpu/ return success for names that are numbers
4551 * ie. filter out ".", "..", "microcode".
4552 */
4553int dir_filter(const struct dirent *dirp)
4554{
4555 if (isdigit(dirp->d_name[0]))
4556 return 1;
4557 else
4558 return 0;
4559}
4560
4561int open_dev_cpu_msr(int dummy1)
4562{
4563 return 0;
4564}
4565
Len Brownc98d5d92012-06-04 00:56:40 -04004566void topology_probe()
4567{
4568 int i;
4569 int max_core_id = 0;
4570 int max_package_id = 0;
4571 int max_siblings = 0;
4572 struct cpu_topology {
4573 int core_id;
4574 int physical_package_id;
4575 } *cpus;
4576
4577 /* Initialize num_cpus, max_cpu_num */
Prarit Bhargava843c5792018-06-01 10:04:28 -04004578 set_max_cpu_num();
Len Brownc98d5d92012-06-04 00:56:40 -04004579 topo.num_cpus = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004580 for_all_proc_cpus(count_cpus);
4581 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004582 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004583
Len Brownd8af6f52015-02-10 01:56:38 -05004584 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004585 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004586
4587 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004588 if (cpus == NULL)
4589 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004590
4591 /*
4592 * Allocate and initialize cpu_present_set
4593 */
4594 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004595 if (cpu_present_set == NULL)
4596 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004597 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4598 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4599 for_all_proc_cpus(mark_cpu_present);
4600
4601 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004602 * Validate that all cpus in cpu_subset are also in cpu_present_set
4603 */
4604 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4605 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4606 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4607 err(1, "cpu%d not present", i);
4608 }
4609
4610 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004611 * Allocate and initialize cpu_affinity_set
4612 */
4613 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004614 if (cpu_affinity_set == NULL)
4615 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004616 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4617 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4618
4619
4620 /*
4621 * For online cpus
4622 * find max_core_id, max_package_id
4623 */
4624 for (i = 0; i <= topo.max_cpu_num; ++i) {
4625 int siblings;
4626
4627 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004628 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004629 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004630 continue;
4631 }
4632 cpus[i].core_id = get_core_id(i);
4633 if (cpus[i].core_id > max_core_id)
4634 max_core_id = cpus[i].core_id;
4635
4636 cpus[i].physical_package_id = get_physical_package_id(i);
4637 if (cpus[i].physical_package_id > max_package_id)
4638 max_package_id = cpus[i].physical_package_id;
4639
4640 siblings = get_num_ht_siblings(i);
4641 if (siblings > max_siblings)
4642 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004643 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004644 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004645 i, cpus[i].physical_package_id, cpus[i].core_id);
4646 }
4647 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004648 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004649 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004650 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004651 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004652 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004653
4654 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004655 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004656 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004657 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004658 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004659 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004660
4661 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004662 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004663 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004664
4665 free(cpus);
4666}
4667
4668void
4669allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4670{
4671 int i;
4672
4673 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004674 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004675 if (*t == NULL)
4676 goto error;
4677
4678 for (i = 0; i < topo.num_threads_per_core *
4679 topo.num_cores_per_pkg * topo.num_packages; i++)
4680 (*t)[i].cpu_id = -1;
4681
4682 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004683 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004684 if (*c == NULL)
4685 goto error;
4686
4687 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4688 (*c)[i].core_id = -1;
4689
Len Brown678a3bd2017-02-09 22:22:13 -05004690 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004691 if (*p == NULL)
4692 goto error;
4693
4694 for (i = 0; i < topo.num_packages; i++)
4695 (*p)[i].package_id = i;
4696
4697 return;
4698error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004699 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004700}
4701/*
4702 * init_counter()
4703 *
4704 * set cpu_id, core_num, pkg_num
4705 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4706 *
4707 * increment topo.num_cores when 1st core in pkg seen
4708 */
4709void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4710 struct pkg_data *pkg_base, int thread_num, int core_num,
4711 int pkg_num, int cpu_id)
4712{
4713 struct thread_data *t;
4714 struct core_data *c;
4715 struct pkg_data *p;
4716
4717 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4718 c = GET_CORE(core_base, core_num, pkg_num);
4719 p = GET_PKG(pkg_base, pkg_num);
4720
4721 t->cpu_id = cpu_id;
4722 if (thread_num == 0) {
4723 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4724 if (cpu_is_first_core_in_package(cpu_id))
4725 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4726 }
4727
4728 c->core_id = core_num;
4729 p->package_id = pkg_num;
4730}
4731
4732
4733int initialize_counters(int cpu_id)
4734{
4735 int my_thread_id, my_core_id, my_package_id;
4736
4737 my_package_id = get_physical_package_id(cpu_id);
4738 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004739 my_thread_id = get_cpu_position_in_core(cpu_id);
4740 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004741 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004742
4743 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4744 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4745 return 0;
4746}
4747
4748void allocate_output_buffer()
4749{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004750 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004751 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004752 if (outp == NULL)
4753 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004754}
Len Brown36229892016-02-26 20:51:02 -05004755void allocate_fd_percpu(void)
4756{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004757 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004758 if (fd_percpu == NULL)
4759 err(-1, "calloc fd_percpu");
4760}
Len Brown562a2d32016-02-26 23:48:05 -05004761void allocate_irq_buffers(void)
4762{
4763 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4764 if (irq_column_2_cpu == NULL)
4765 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004766
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004767 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004768 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004769 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004770}
Len Brownc98d5d92012-06-04 00:56:40 -04004771void setup_all_buffers(void)
4772{
4773 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004774 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004775 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004776 allocate_counters(&thread_even, &core_even, &package_even);
4777 allocate_counters(&thread_odd, &core_odd, &package_odd);
4778 allocate_output_buffer();
4779 for_all_proc_cpus(initialize_counters);
4780}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004781
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004782void set_base_cpu(void)
4783{
4784 base_cpu = sched_getcpu();
4785 if (base_cpu < 0)
4786 err(-ENODEV, "No valid cpus found");
4787
4788 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004789 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004790}
4791
Len Brown103a8fe2010-10-22 23:53:03 -04004792void turbostat_init()
4793{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004794 setup_all_buffers();
4795 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004796 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004797 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004798 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004799
Len Brown103a8fe2010-10-22 23:53:03 -04004800
Len Brown96e47152017-01-21 02:26:00 -05004801 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004802 for_all_cpus(print_hwp, ODD_COUNTERS);
4803
Len Brown96e47152017-01-21 02:26:00 -05004804 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004805 for_all_cpus(print_epb, ODD_COUNTERS);
4806
Len Brown96e47152017-01-21 02:26:00 -05004807 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004808 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4809
Len Brown96e47152017-01-21 02:26:00 -05004810 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004811 for_all_cpus(print_rapl, ODD_COUNTERS);
4812
4813 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4814
Len Brown96e47152017-01-21 02:26:00 -05004815 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004816 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004817
Len Brown96e47152017-01-21 02:26:00 -05004818 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004819 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004820}
4821
4822int fork_it(char **argv)
4823{
Len Brown103a8fe2010-10-22 23:53:03 -04004824 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004825 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004826
Len Brown218f0e82017-02-14 22:07:52 -05004827 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004828 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4829 if (status)
4830 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004831 /* clear affinity side-effect of get_counters() */
4832 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004833 gettimeofday(&tv_even, (struct timezone *)NULL);
4834
4835 child_pid = fork();
4836 if (!child_pid) {
4837 /* child */
4838 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004839 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004840 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004841
4842 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004843 if (child_pid == -1)
4844 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004845
4846 signal(SIGINT, SIG_IGN);
4847 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004848 if (waitpid(child_pid, &status, 0) == -1)
4849 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004850 }
Len Brownc98d5d92012-06-04 00:56:40 -04004851 /*
4852 * n.b. fork_it() does not check for errors from for_all_cpus()
4853 * because re-starting is problematic when forking
4854 */
Len Brown218f0e82017-02-14 22:07:52 -05004855 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004856 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004857 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004858 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004859 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4860 fprintf(outf, "%s: Counter reset detected\n", progname);
4861 else {
4862 compute_average(EVEN_COUNTERS);
4863 format_all_counters(EVEN_COUNTERS);
4864 }
Len Brown103a8fe2010-10-22 23:53:03 -04004865
Len Brownb7d8c142016-02-13 23:36:17 -05004866 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4867
4868 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004869
Len Brownd91bb172012-11-01 00:08:19 -04004870 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004871}
4872
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004873int get_and_dump_counters(void)
4874{
4875 int status;
4876
Len Brown218f0e82017-02-14 22:07:52 -05004877 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004878 status = for_all_cpus(get_counters, ODD_COUNTERS);
4879 if (status)
4880 return status;
4881
4882 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4883 if (status)
4884 return status;
4885
Len Brownb7d8c142016-02-13 23:36:17 -05004886 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004887
4888 return status;
4889}
4890
Len Brownd8af6f52015-02-10 01:56:38 -05004891void print_version() {
Len Brownf7d44a82017-05-27 21:24:58 -07004892 fprintf(outf, "turbostat version 17.06.23"
Len Brownd8af6f52015-02-10 01:56:38 -05004893 " - Len Brown <lenb@kernel.org>\n");
4894}
4895
Len Brown495c76542017-02-08 02:41:51 -05004896int add_counter(unsigned int msr_num, char *path, char *name,
4897 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004898 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004899{
4900 struct msr_counter *msrp;
4901
4902 msrp = calloc(1, sizeof(struct msr_counter));
4903 if (msrp == NULL) {
4904 perror("calloc");
4905 exit(1);
4906 }
4907
4908 msrp->msr_num = msr_num;
4909 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004910 if (path)
4911 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004912 msrp->width = width;
4913 msrp->type = type;
4914 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004915 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004916
4917 switch (scope) {
4918
4919 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004920 msrp->next = sys.tp;
4921 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004922 sys.added_thread_counters++;
Len Brown0748eaf2018-06-01 12:38:29 -04004923 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
Len Brown678a3bd2017-02-09 22:22:13 -05004924 fprintf(stderr, "exceeded max %d added thread counters\n",
4925 MAX_ADDED_COUNTERS);
4926 exit(-1);
4927 }
Len Brown388e9c82016-12-22 23:57:55 -05004928 break;
4929
4930 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004931 msrp->next = sys.cp;
4932 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004933 sys.added_core_counters++;
4934 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4935 fprintf(stderr, "exceeded max %d added core counters\n",
4936 MAX_ADDED_COUNTERS);
4937 exit(-1);
4938 }
Len Brown388e9c82016-12-22 23:57:55 -05004939 break;
4940
4941 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004942 msrp->next = sys.pp;
4943 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004944 sys.added_package_counters++;
4945 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4946 fprintf(stderr, "exceeded max %d added package counters\n",
4947 MAX_ADDED_COUNTERS);
4948 exit(-1);
4949 }
Len Brown388e9c82016-12-22 23:57:55 -05004950 break;
4951 }
4952
4953 return 0;
4954}
4955
4956void parse_add_command(char *add_command)
4957{
4958 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004959 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004960 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004961 int width = 64;
4962 int fail = 0;
4963 enum counter_scope scope = SCOPE_CPU;
4964 enum counter_type type = COUNTER_CYCLES;
4965 enum counter_format format = FORMAT_DELTA;
4966
4967 while (add_command) {
4968
4969 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4970 goto next;
4971
4972 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4973 goto next;
4974
Len Brown495c76542017-02-08 02:41:51 -05004975 if (*add_command == '/') {
4976 path = add_command;
4977 goto next;
4978 }
4979
Len Brown388e9c82016-12-22 23:57:55 -05004980 if (sscanf(add_command, "u%d", &width) == 1) {
4981 if ((width == 32) || (width == 64))
4982 goto next;
4983 width = 64;
4984 }
4985 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4986 scope = SCOPE_CPU;
4987 goto next;
4988 }
4989 if (!strncmp(add_command, "core", strlen("core"))) {
4990 scope = SCOPE_CORE;
4991 goto next;
4992 }
4993 if (!strncmp(add_command, "package", strlen("package"))) {
4994 scope = SCOPE_PACKAGE;
4995 goto next;
4996 }
4997 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4998 type = COUNTER_CYCLES;
4999 goto next;
5000 }
5001 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5002 type = COUNTER_SECONDS;
5003 goto next;
5004 }
Len Brown41618e62017-02-09 18:25:22 -05005005 if (!strncmp(add_command, "usec", strlen("usec"))) {
5006 type = COUNTER_USEC;
5007 goto next;
5008 }
Len Brown388e9c82016-12-22 23:57:55 -05005009 if (!strncmp(add_command, "raw", strlen("raw"))) {
5010 format = FORMAT_RAW;
5011 goto next;
5012 }
5013 if (!strncmp(add_command, "delta", strlen("delta"))) {
5014 format = FORMAT_DELTA;
5015 goto next;
5016 }
5017 if (!strncmp(add_command, "percent", strlen("percent"))) {
5018 format = FORMAT_PERCENT;
5019 goto next;
5020 }
5021
5022 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
5023 char *eos;
5024
5025 eos = strchr(name_buffer, ',');
5026 if (eos)
5027 *eos = '\0';
5028 goto next;
5029 }
5030
5031next:
5032 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05005033 if (add_command) {
5034 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05005035 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05005036 }
Len Brown388e9c82016-12-22 23:57:55 -05005037
5038 }
Len Brown495c76542017-02-08 02:41:51 -05005039 if ((msr_num == 0) && (path == NULL)) {
5040 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05005041 fail++;
5042 }
5043
5044 /* generate default column header */
5045 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05005046 if (width == 32)
5047 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5048 else
5049 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05005050 }
5051
Len Brown41618e62017-02-09 18:25:22 -05005052 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05005053 fail++;
5054
5055 if (fail) {
5056 help();
5057 exit(1);
5058 }
5059}
Len Brown41618e62017-02-09 18:25:22 -05005060
Len Browndd778a52017-02-21 23:21:13 -05005061int is_deferred_skip(char *name)
5062{
5063 int i;
5064
5065 for (i = 0; i < deferred_skip_index; ++i)
5066 if (!strcmp(name, deferred_skip_names[i]))
5067 return 1;
5068 return 0;
5069}
5070
Len Brown41618e62017-02-09 18:25:22 -05005071void probe_sysfs(void)
5072{
5073 char path[64];
5074 char name_buf[16];
5075 FILE *input;
5076 int state;
5077 char *sp;
5078
5079 if (!DO_BIC(BIC_sysfs))
5080 return;
5081
Len Brown0748eaf2018-06-01 12:38:29 -04005082 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005083
5084 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5085 base_cpu, state);
5086 input = fopen(path, "r");
5087 if (input == NULL)
5088 continue;
5089 fgets(name_buf, sizeof(name_buf), input);
5090
5091 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5092 sp = strchr(name_buf, '-');
5093 if (!sp)
5094 sp = strchrnul(name_buf, '\n');
5095 *sp = '%';
5096 *(sp + 1) = '\0';
5097
5098 fclose(input);
5099
5100 sprintf(path, "cpuidle/state%d/time", state);
5101
Len Browndd778a52017-02-21 23:21:13 -05005102 if (is_deferred_skip(name_buf))
5103 continue;
5104
Len Brown41618e62017-02-09 18:25:22 -05005105 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
5106 FORMAT_PERCENT, SYSFS_PERCPU);
5107 }
5108
Len Brown0748eaf2018-06-01 12:38:29 -04005109 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005110
5111 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5112 base_cpu, state);
5113 input = fopen(path, "r");
5114 if (input == NULL)
5115 continue;
5116 fgets(name_buf, sizeof(name_buf), input);
5117 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5118 sp = strchr(name_buf, '-');
5119 if (!sp)
5120 sp = strchrnul(name_buf, '\n');
5121 *sp = '\0';
5122 fclose(input);
5123
5124 sprintf(path, "cpuidle/state%d/usage", state);
5125
Len Browndd778a52017-02-21 23:21:13 -05005126 if (is_deferred_skip(name_buf))
5127 continue;
5128
Len Brown41618e62017-02-09 18:25:22 -05005129 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
5130 FORMAT_DELTA, SYSFS_PERCPU);
5131 }
5132
5133}
5134
Len Brown1ef7d212017-02-10 23:54:15 -05005135
5136/*
5137 * parse cpuset with following syntax
5138 * 1,2,4..6,8-10 and set bits in cpu_subset
5139 */
5140void parse_cpu_command(char *optarg)
5141{
5142 unsigned int start, end;
5143 char *next;
5144
Len Brown4e4e1e72017-02-21 22:33:42 -05005145 if (!strcmp(optarg, "core")) {
5146 if (cpu_subset)
5147 goto error;
5148 show_core_only++;
5149 return;
5150 }
5151 if (!strcmp(optarg, "package")) {
5152 if (cpu_subset)
5153 goto error;
5154 show_pkg_only++;
5155 return;
5156 }
5157 if (show_core_only || show_pkg_only)
5158 goto error;
5159
Len Brown1ef7d212017-02-10 23:54:15 -05005160 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5161 if (cpu_subset == NULL)
5162 err(3, "CPU_ALLOC");
5163 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5164
5165 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5166
5167 next = optarg;
5168
5169 while (next && *next) {
5170
5171 if (*next == '-') /* no negative cpu numbers */
5172 goto error;
5173
5174 start = strtoul(next, &next, 10);
5175
5176 if (start >= CPU_SUBSET_MAXCPUS)
5177 goto error;
5178 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5179
5180 if (*next == '\0')
5181 break;
5182
5183 if (*next == ',') {
5184 next += 1;
5185 continue;
5186 }
5187
5188 if (*next == '-') {
5189 next += 1; /* start range */
5190 } else if (*next == '.') {
5191 next += 1;
5192 if (*next == '.')
5193 next += 1; /* start range */
5194 else
5195 goto error;
5196 }
5197
5198 end = strtoul(next, &next, 10);
5199 if (end <= start)
5200 goto error;
5201
5202 while (++start <= end) {
5203 if (start >= CPU_SUBSET_MAXCPUS)
5204 goto error;
5205 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5206 }
5207
5208 if (*next == ',')
5209 next += 1;
5210 else if (*next != '\0')
5211 goto error;
5212 }
5213
5214 return;
5215
5216error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005217 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5218 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005219 exit(-1);
5220}
5221
Len Brown812db3f2017-02-10 00:25:41 -05005222
Len Brown103a8fe2010-10-22 23:53:03 -04005223void cmdline(int argc, char **argv)
5224{
5225 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005226 int option_index = 0;
5227 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005228 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005229 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005230 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005231 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005232 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005233 {"interval", required_argument, 0, 'i'},
Chen Yu023fe0a2018-04-26 08:41:03 +08005234 {"num_iterations", required_argument, 0, 'n'},
Len Brownd8af6f52015-02-10 01:56:38 -05005235 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005236 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005237 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005238 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005239 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005240 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005241 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005242 {"Summary", no_argument, 0, 'S'},
5243 {"TCC", required_argument, 0, 'T'},
5244 {"version", no_argument, 0, 'v' },
5245 {0, 0, 0, 0 }
5246 };
Len Brown103a8fe2010-10-22 23:53:03 -04005247
5248 progname = argv[0];
5249
Chen Yu023fe0a2018-04-26 08:41:03 +08005250 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005251 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005252 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005253 case 'a':
5254 parse_add_command(optarg);
5255 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005256 case 'c':
5257 parse_cpu_command(optarg);
5258 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005259 case 'D':
5260 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005261 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005262 case 'e':
5263 /* --enable specified counter */
5264 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5265 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005266 case 'd':
5267 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005268 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005269 break;
Len Brown812db3f2017-02-10 00:25:41 -05005270 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005271 /*
5272 * --hide: do not show those specified
5273 * multiple invocations simply clear more bits in enabled mask
5274 */
5275 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005276 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005277 case 'h':
5278 default:
5279 help();
5280 exit(1);
5281 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005282 {
5283 double interval = strtod(optarg, NULL);
5284
5285 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005286 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005287 interval);
5288 exit(2);
5289 }
5290
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005291 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005292 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005293 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005294 }
Len Brown889facb2012-11-08 00:48:57 -05005295 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005296 case 'J':
5297 rapl_joules++;
5298 break;
Len Brownc8ade362017-02-15 17:15:11 -05005299 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005300 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005301 list_header_only++;
5302 quiet++;
5303 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005304 case 'o':
5305 outf = fopen_or_die(optarg, "w");
5306 break;
Len Brown96e47152017-01-21 02:26:00 -05005307 case 'q':
5308 quiet = 1;
5309 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08005310 case 'n':
5311 num_iterations = strtod(optarg, NULL);
5312
5313 if (num_iterations <= 0) {
5314 fprintf(outf, "iterations %d should be positive number\n",
5315 num_iterations);
5316 exit(2);
5317 }
5318 break;
Len Brown812db3f2017-02-10 00:25:41 -05005319 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005320 /*
5321 * --show: show only those specified
5322 * The 1st invocation will clear and replace the enabled mask
5323 * subsequent invocations can add to it.
5324 */
5325 if (shown == 0)
5326 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5327 else
5328 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5329 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005330 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005331 case 'S':
5332 summary_only++;
5333 break;
5334 case 'T':
5335 tcc_activation_temp_override = atoi(optarg);
5336 break;
5337 case 'v':
5338 print_version();
5339 exit(0);
5340 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005341 }
5342 }
5343}
5344
5345int main(int argc, char **argv)
5346{
Len Brownb7d8c142016-02-13 23:36:17 -05005347 outf = stderr;
5348
Len Brown103a8fe2010-10-22 23:53:03 -04005349 cmdline(argc, argv);
5350
Len Brown96e47152017-01-21 02:26:00 -05005351 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005352 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005353
Len Brown41618e62017-02-09 18:25:22 -05005354 probe_sysfs();
5355
Len Brown103a8fe2010-10-22 23:53:03 -04005356 turbostat_init();
5357
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005358 /* dump counters and exit */
5359 if (dump_only)
5360 return get_and_dump_counters();
5361
Len Brownc8ade362017-02-15 17:15:11 -05005362 /* list header and exit */
5363 if (list_header_only) {
5364 print_header(",");
5365 flush_output_stdout();
5366 return 0;
5367 }
5368
Len Brown103a8fe2010-10-22 23:53:03 -04005369 /*
5370 * if any params left, it must be a command to fork
5371 */
5372 if (argc - optind)
5373 return fork_it(argv + optind);
5374 else
5375 turbostat_loop();
5376
5377 return 0;
5378}