blob: a9085775a3de6b78a9653f4c4813d6e8bd0c2c6e [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};
Len Brownd8af6f52015-02-10 01:56:38 -050054unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050055unsigned int quiet;
Len Brown3f44a5c2017-10-17 15:42:56 -040056unsigned int shown;
Len Brown0de6c0d2017-02-15 21:45:40 -050057unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050058unsigned int rapl_joules;
59unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050060unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050061unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040062unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070063unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050064unsigned int do_slm_cstates;
65unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050067unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040068unsigned int do_irtl_snb;
69unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050070unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040071unsigned int genuine_intel;
72unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050073unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050074unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020075unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040076double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040077double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040078unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040079double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040080unsigned int show_pkg_only;
81unsigned int show_core_only;
82char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050083unsigned int do_rapl;
84unsigned int do_dts;
85unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050086unsigned long long gfx_cur_rc6_ms;
Len Brownbe0e54c2018-06-01 12:35:53 -040087unsigned long long cpuidle_cur_cpu_lpi_us;
88unsigned long long cpuidle_cur_sys_lpi_us;
Len Brown27d47352016-02-27 00:37:54 -050089unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050090unsigned int tcc_activation_temp;
91unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050092double rapl_power_units, rapl_time_units;
93double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050094double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040095unsigned int do_core_perf_limit_reasons;
Artem Bityutskiyac980e12017-09-05 15:14:08 +030096unsigned int has_automatic_cstate_conversion;
Len Brown3a9a9412014-08-15 02:39:52 -040097unsigned int do_gfx_perf_limit_reasons;
98unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040099unsigned int crystal_hz;
100unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400101int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400102double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500103unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
104 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
105unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
106unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
107unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
108unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500109unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500110
Len Browne6f9bb32013-12-03 02:19:19 -0500111#define RAPL_PKG (1 << 0)
112 /* 0x610 MSR_PKG_POWER_LIMIT */
113 /* 0x611 MSR_PKG_ENERGY_STATUS */
114#define RAPL_PKG_PERF_STATUS (1 << 1)
115 /* 0x613 MSR_PKG_PERF_STATUS */
116#define RAPL_PKG_POWER_INFO (1 << 2)
117 /* 0x614 MSR_PKG_POWER_INFO */
118
119#define RAPL_DRAM (1 << 3)
120 /* 0x618 MSR_DRAM_POWER_LIMIT */
121 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500122#define RAPL_DRAM_PERF_STATUS (1 << 4)
123 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400124#define RAPL_DRAM_POWER_INFO (1 << 5)
125 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500126
Jacob Pan91484942016-06-16 09:48:20 -0700127#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500128 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400129#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500130 /* 0x63a MSR_PP0_POLICY */
131
Len Brown0b2bb692015-03-26 00:50:30 -0400132#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500133 /* 0x640 MSR_PP1_POWER_LIMIT */
134 /* 0x641 MSR_PP1_ENERGY_STATUS */
135 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700136
137#define RAPL_CORES_ENERGY_STATUS (1 << 9)
138 /* 0x639 MSR_PP0_ENERGY_STATUS */
139#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500140#define TJMAX_DEFAULT 100
141
142#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400143
Len Brown388e9c82016-12-22 23:57:55 -0500144/*
145 * buffer size used by sscanf() for added column names
146 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
147 */
148#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500149#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500150
Len Brown103a8fe2010-10-22 23:53:03 -0400151int backwards_count;
152char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400153
Len Brown1ef7d212017-02-10 23:54:15 -0500154#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
155cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
156size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown0748eaf2018-06-01 12:38:29 -0400157#define MAX_ADDED_COUNTERS 8
158#define MAX_ADDED_THREAD_COUNTERS 24
Len Brown103a8fe2010-10-22 23:53:03 -0400159
Len Brownc98d5d92012-06-04 00:56:40 -0400160struct thread_data {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700161 struct timeval tv_begin;
162 struct timeval tv_end;
Len Brownc98d5d92012-06-04 00:56:40 -0400163 unsigned long long tsc;
164 unsigned long long aperf;
165 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500166 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500167 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500168 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400169 unsigned int cpu_id;
170 unsigned int flags;
171#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
172#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown0748eaf2018-06-01 12:38:29 -0400173 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400174} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400175
Len Brownc98d5d92012-06-04 00:56:40 -0400176struct core_data {
177 unsigned long long c3;
178 unsigned long long c6;
179 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500180 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500181 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400182 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500183 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400184} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400185
Len Brownc98d5d92012-06-04 00:56:40 -0400186struct pkg_data {
187 unsigned long long pc2;
188 unsigned long long pc3;
189 unsigned long long pc6;
190 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800191 unsigned long long pc8;
192 unsigned long long pc9;
193 unsigned long long pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -0400194 unsigned long long cpu_lpi;
195 unsigned long long sys_lpi;
Len Brown0b2bb692015-03-26 00:50:30 -0400196 unsigned long long pkg_wtd_core_c0;
197 unsigned long long pkg_any_core_c0;
198 unsigned long long pkg_any_gfxe_c0;
199 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400200 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500201 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400202 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500203 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
204 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
205 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
206 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
207 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
208 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
209 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500210 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400211} *package_even, *package_odd;
212
213#define ODD_COUNTERS thread_odd, core_odd, package_odd
214#define EVEN_COUNTERS thread_even, core_even, package_even
215
216#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
217 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
218 topo.num_threads_per_core + \
219 (core_no) * topo.num_threads_per_core + (thread_no))
220#define GET_CORE(core_base, core_no, pkg_no) \
221 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
222#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
223
Len Brown388e9c82016-12-22 23:57:55 -0500224enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500225enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500226enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
227
228struct msr_counter {
229 unsigned int msr_num;
230 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500231 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500232 unsigned int width;
233 enum counter_type type;
234 enum counter_format format;
235 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500236 unsigned int flags;
237#define FLAGS_HIDE (1 << 0)
238#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500239#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500240};
241
242struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500243 unsigned int added_thread_counters;
244 unsigned int added_core_counters;
245 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500246 struct msr_counter *tp;
247 struct msr_counter *cp;
248 struct msr_counter *pp;
249} sys;
250
Len Brownc98d5d92012-06-04 00:56:40 -0400251struct system_summary {
252 struct thread_data threads;
253 struct core_data cores;
254 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500255} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400256
257
258struct topo_params {
259 int num_packages;
260 int num_cpus;
261 int num_cores;
262 int max_cpu_num;
263 int num_cores_per_pkg;
264 int num_threads_per_core;
265} topo;
266
267struct timeval tv_even, tv_odd, tv_delta;
268
Len Brown562a2d32016-02-26 23:48:05 -0500269int *irq_column_2_cpu; /* /proc/interrupts column numbers */
270int *irqs_per_cpu; /* indexed by cpu_num */
271
Len Brownc98d5d92012-06-04 00:56:40 -0400272void setup_all_buffers(void);
273
274int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400275{
Len Brownc98d5d92012-06-04 00:56:40 -0400276 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400277}
Len Brown88c32812012-03-29 21:44:40 -0400278/*
Len Brownc98d5d92012-06-04 00:56:40 -0400279 * run func(thread, core, package) in topology order
280 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400281 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400282
Len Brownc98d5d92012-06-04 00:56:40 -0400283int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
284 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400285{
Len Brownc98d5d92012-06-04 00:56:40 -0400286 int retval, pkg_no, core_no, thread_no;
287
288 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
289 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
290 for (thread_no = 0; thread_no <
291 topo.num_threads_per_core; ++thread_no) {
292 struct thread_data *t;
293 struct core_data *c;
294 struct pkg_data *p;
295
296 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
297
298 if (cpu_is_not_present(t->cpu_id))
299 continue;
300
301 c = GET_CORE(core_base, core_no, pkg_no);
302 p = GET_PKG(pkg_base, pkg_no);
303
304 retval = func(t, c, p);
305 if (retval)
306 return retval;
307 }
308 }
309 }
310 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400311}
312
313int cpu_migrate(int cpu)
314{
Len Brownc98d5d92012-06-04 00:56:40 -0400315 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
316 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
317 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400318 return -1;
319 else
320 return 0;
321}
Len Brown36229892016-02-26 20:51:02 -0500322int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400323{
Len Brown103a8fe2010-10-22 23:53:03 -0400324 char pathname[32];
325 int fd;
326
Len Brown36229892016-02-26 20:51:02 -0500327 fd = fd_percpu[cpu];
328
329 if (fd)
330 return fd;
331
Len Brown103a8fe2010-10-22 23:53:03 -0400332 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
333 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400334 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400335 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 -0400336
Len Brown36229892016-02-26 20:51:02 -0500337 fd_percpu[cpu] = fd;
338
339 return fd;
340}
341
342int get_msr(int cpu, off_t offset, unsigned long long *msr)
343{
344 ssize_t retval;
345
346 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400347
Len Brown98481e72014-08-15 00:36:50 -0400348 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500349 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400350
351 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400352}
353
Len Brownfc04cc62014-02-06 00:55:19 -0500354/*
Len Brown812db3f2017-02-10 00:25:41 -0500355 * Each string in this array is compared in --show and --hide cmdline.
356 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500357 */
Len Brown812db3f2017-02-10 00:25:41 -0500358struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400359 { 0x0, "usec" },
360 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500361 { 0x0, "Package" },
362 { 0x0, "Avg_MHz" },
363 { 0x0, "Bzy_MHz" },
364 { 0x0, "TSC_MHz" },
365 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500366 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500367 { 0x0, "Busy%" },
368 { 0x0, "CPU%c1" },
369 { 0x0, "CPU%c3" },
370 { 0x0, "CPU%c6" },
371 { 0x0, "CPU%c7" },
372 { 0x0, "ThreadC" },
373 { 0x0, "CoreTmp" },
374 { 0x0, "CoreCnt" },
375 { 0x0, "PkgTmp" },
376 { 0x0, "GFX%rc6" },
377 { 0x0, "GFXMHz" },
378 { 0x0, "Pkg%pc2" },
379 { 0x0, "Pkg%pc3" },
380 { 0x0, "Pkg%pc6" },
381 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500382 { 0x0, "Pkg%pc8" },
383 { 0x0, "Pkg%pc9" },
Len Brown4bd1f8f2018-01-28 23:42:42 -0500384 { 0x0, "Pk%pc10" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400385 { 0x0, "CPU%LPI" },
386 { 0x0, "SYS%LPI" },
Len Brown812db3f2017-02-10 00:25:41 -0500387 { 0x0, "PkgWatt" },
388 { 0x0, "CorWatt" },
389 { 0x0, "GFXWatt" },
390 { 0x0, "PkgCnt" },
391 { 0x0, "RAMWatt" },
392 { 0x0, "PKG_%" },
393 { 0x0, "RAM_%" },
394 { 0x0, "Pkg_J" },
395 { 0x0, "Cor_J" },
396 { 0x0, "GFX_J" },
397 { 0x0, "RAM_J" },
398 { 0x0, "Core" },
399 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500400 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500401 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400402 { 0x0, "Totl%C0" },
403 { 0x0, "Any%C0" },
404 { 0x0, "GFX%C0" },
405 { 0x0, "CPUGFX%" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400406 { 0x0, "Node%" },
Len Brown812db3f2017-02-10 00:25:41 -0500407};
408
Len Browna99d8732017-05-20 20:11:55 -0400409
410
Len Brown812db3f2017-02-10 00:25:41 -0500411#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400412#define BIC_USEC (1ULL << 0)
413#define BIC_TOD (1ULL << 1)
414#define BIC_Package (1ULL << 2)
415#define BIC_Avg_MHz (1ULL << 3)
416#define BIC_Bzy_MHz (1ULL << 4)
417#define BIC_TSC_MHz (1ULL << 5)
418#define BIC_IRQ (1ULL << 6)
419#define BIC_SMI (1ULL << 7)
420#define BIC_Busy (1ULL << 8)
421#define BIC_CPU_c1 (1ULL << 9)
422#define BIC_CPU_c3 (1ULL << 10)
423#define BIC_CPU_c6 (1ULL << 11)
424#define BIC_CPU_c7 (1ULL << 12)
425#define BIC_ThreadC (1ULL << 13)
426#define BIC_CoreTmp (1ULL << 14)
427#define BIC_CoreCnt (1ULL << 15)
428#define BIC_PkgTmp (1ULL << 16)
429#define BIC_GFX_rc6 (1ULL << 17)
430#define BIC_GFXMHz (1ULL << 18)
431#define BIC_Pkgpc2 (1ULL << 19)
432#define BIC_Pkgpc3 (1ULL << 20)
433#define BIC_Pkgpc6 (1ULL << 21)
434#define BIC_Pkgpc7 (1ULL << 22)
435#define BIC_Pkgpc8 (1ULL << 23)
436#define BIC_Pkgpc9 (1ULL << 24)
437#define BIC_Pkgpc10 (1ULL << 25)
Len Brownbe0e54c2018-06-01 12:35:53 -0400438#define BIC_CPU_LPI (1ULL << 26)
439#define BIC_SYS_LPI (1ULL << 27)
Len Brown3f44a5c2017-10-17 15:42:56 -0400440#define BIC_PkgWatt (1ULL << 26)
441#define BIC_CorWatt (1ULL << 27)
442#define BIC_GFXWatt (1ULL << 28)
443#define BIC_PkgCnt (1ULL << 29)
444#define BIC_RAMWatt (1ULL << 30)
445#define BIC_PKG__ (1ULL << 31)
446#define BIC_RAM__ (1ULL << 32)
447#define BIC_Pkg_J (1ULL << 33)
448#define BIC_Cor_J (1ULL << 34)
449#define BIC_GFX_J (1ULL << 35)
450#define BIC_RAM_J (1ULL << 36)
451#define BIC_Core (1ULL << 37)
452#define BIC_CPU (1ULL << 38)
453#define BIC_Mod_c6 (1ULL << 39)
454#define BIC_sysfs (1ULL << 40)
455#define BIC_Totl_c0 (1ULL << 41)
456#define BIC_Any_c0 (1ULL << 42)
457#define BIC_GFX_c0 (1ULL << 43)
458#define BIC_CPUGFX (1ULL << 44)
Len Brown812db3f2017-02-10 00:25:41 -0500459
Len Brown3f44a5c2017-10-17 15:42:56 -0400460#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
461
462unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
463unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500464
465#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400466#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500467#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500468#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500469
Len Brown3f44a5c2017-10-17 15:42:56 -0400470
Len Browndd778a52017-02-21 23:21:13 -0500471#define MAX_DEFERRED 16
472char *deferred_skip_names[MAX_DEFERRED];
473int deferred_skip_index;
474
475/*
476 * HIDE_LIST - hide this list of counters, show the rest [default]
477 * SHOW_LIST - show this list of counters, hide the rest
478 */
479enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
480
481void help(void)
482{
483 fprintf(outf,
484 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
485 "\n"
486 "Turbostat forks the specified COMMAND and prints statistics\n"
487 "when COMMAND completes.\n"
488 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
489 "to print statistics, until interrupted.\n"
490 "--add add a counter\n"
491 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
492 "--cpu cpu-set limit output to summary plus cpu-set:\n"
493 " {core | package | j,k,l..m,n-p }\n"
494 "--quiet skip decoding system configuration header\n"
Len Brownb9ad8ee2017-07-19 19:28:37 -0400495 "--interval sec.subsec Override default 5-second measurement interval\n"
Len Browndd778a52017-02-21 23:21:13 -0500496 "--help print this help message\n"
497 "--list list column headers only\n"
498 "--out file create or truncate \"file\" for all output\n"
499 "--version print version information\n"
500 "\n"
501 "For more help, run \"man turbostat\"\n");
502}
503
Len Brown812db3f2017-02-10 00:25:41 -0500504/*
505 * bic_lookup
506 * for all the strings in comma separate name_list,
507 * set the approprate bit in return value.
508 */
Len Browndd778a52017-02-21 23:21:13 -0500509unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500510{
511 int i;
512 unsigned long long retval = 0;
513
514 while (name_list) {
515 char *comma;
516
517 comma = strchr(name_list, ',');
518
519 if (comma)
520 *comma = '\0';
521
Len Brown3f44a5c2017-10-17 15:42:56 -0400522 if (!strcmp(name_list, "all"))
523 return ~0;
524
Len Brown812db3f2017-02-10 00:25:41 -0500525 for (i = 0; i < MAX_BIC; ++i) {
526 if (!strcmp(name_list, bic[i].name)) {
527 retval |= (1ULL << i);
528 break;
529 }
530 }
531 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500532 if (mode == SHOW_LIST) {
533 fprintf(stderr, "Invalid counter name: %s\n", name_list);
534 exit(-1);
535 }
536 deferred_skip_names[deferred_skip_index++] = name_list;
537 if (debug)
538 fprintf(stderr, "deferred \"%s\"\n", name_list);
539 if (deferred_skip_index >= MAX_DEFERRED) {
540 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
541 MAX_DEFERRED, name_list);
542 help();
543 exit(1);
544 }
Len Brown812db3f2017-02-10 00:25:41 -0500545 }
546
547 name_list = comma;
548 if (name_list)
549 name_list++;
550
551 }
552 return retval;
553}
Len Brownfc04cc62014-02-06 00:55:19 -0500554
Len Browndd778a52017-02-21 23:21:13 -0500555
Len Brownc8ade362017-02-15 17:15:11 -0500556void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400557{
Len Brown388e9c82016-12-22 23:57:55 -0500558 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500559 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500560
Len Brown3f44a5c2017-10-17 15:42:56 -0400561 if (DO_BIC(BIC_USEC))
562 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
563 if (DO_BIC(BIC_TOD))
564 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500565 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500566 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500567 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500568 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500569 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500570 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500571 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500572 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500573 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500574 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500575 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500576 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500577 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500578 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500579
Len Brown0de6c0d2017-02-15 21:45:40 -0500580 if (DO_BIC(BIC_IRQ)) {
581 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500582 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500583 else
Len Brown6168c2e2017-02-16 23:07:51 -0500584 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500585 }
586
Len Brown812db3f2017-02-10 00:25:41 -0500587 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500588 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500589
Len Brown388e9c82016-12-22 23:57:55 -0500590 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500591
Len Brown388e9c82016-12-22 23:57:55 -0500592 if (mp->format == FORMAT_RAW) {
593 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500594 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500595 else
Len Browndd778a52017-02-21 23:21:13 -0500596 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500597 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500598 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500599 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500600 else
Len Browndd778a52017-02-21 23:21:13 -0500601 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500602 }
603 }
604
Len Brown41618e62017-02-09 18:25:22 -0500605 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500606 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500607 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500608 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500609 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500610 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500611 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500612 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500613
Len Brown0539ba12017-02-10 00:27:20 -0500614 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500615 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500616
Len Brown812db3f2017-02-10 00:25:41 -0500617 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500618 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500619
620 for (mp = sys.cp; mp; mp = mp->next) {
621 if (mp->format == FORMAT_RAW) {
622 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500623 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500624 else
Len Brownc8ade362017-02-15 17:15:11 -0500625 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500626 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500627 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
628 outp += sprintf(outp, "%s%8s", delim, mp->name);
629 else
630 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500631 }
632 }
633
Len Brown812db3f2017-02-10 00:25:41 -0500634 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500635 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500636
Len Brown812db3f2017-02-10 00:25:41 -0500637 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500638 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500639
Len Brown812db3f2017-02-10 00:25:41 -0500640 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500641 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500642
Len Browna99d8732017-05-20 20:11:55 -0400643 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500644 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400645 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500646 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400647 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500648 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400649 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500650 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400651
Len Brown0f47c082017-01-27 00:50:45 -0500652 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500653 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500654 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500655 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500656 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500657 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500658 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500659 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500660 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500661 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500662 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500663 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500664 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500665 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brownbe0e54c2018-06-01 12:35:53 -0400666 if (DO_BIC(BIC_CPU_LPI))
667 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
668 if (DO_BIC(BIC_SYS_LPI))
669 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400670
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800671 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500672 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500673 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500674 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500675 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500676 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500677 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500678 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500679 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500680 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500681 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500682 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500683 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500684 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500685 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500686 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500687 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500688 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500689 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500690 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500691 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500692 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500693 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500694 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500695 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500696 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800697 }
Len Brown388e9c82016-12-22 23:57:55 -0500698 for (mp = sys.pp; mp; mp = mp->next) {
699 if (mp->format == FORMAT_RAW) {
700 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500701 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500702 else
Len Brownc8ade362017-02-15 17:15:11 -0500703 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500704 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500705 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
706 outp += sprintf(outp, "%s%8s", delim, mp->name);
707 else
708 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500709 }
710 }
711
Len Brownc98d5d92012-06-04 00:56:40 -0400712 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400713}
714
Len Brownc98d5d92012-06-04 00:56:40 -0400715int dump_counters(struct thread_data *t, struct core_data *c,
716 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400717{
Len Brown388e9c82016-12-22 23:57:55 -0500718 int i;
719 struct msr_counter *mp;
720
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200721 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400722
Len Brownc98d5d92012-06-04 00:56:40 -0400723 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200724 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
725 t->cpu_id, t->flags);
726 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
727 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
728 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
729 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500730
Len Brown812db3f2017-02-10 00:25:41 -0500731 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500732 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500733 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500734 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500735
736 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
737 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
738 i, mp->msr_num, t->counter[i]);
739 }
Len Brownc98d5d92012-06-04 00:56:40 -0400740 }
Len Brown103a8fe2010-10-22 23:53:03 -0400741
Len Brownc98d5d92012-06-04 00:56:40 -0400742 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200743 outp += sprintf(outp, "core: %d\n", c->core_id);
744 outp += sprintf(outp, "c3: %016llX\n", c->c3);
745 outp += sprintf(outp, "c6: %016llX\n", c->c6);
746 outp += sprintf(outp, "c7: %016llX\n", c->c7);
747 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500748
749 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
750 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
751 i, mp->msr_num, c->counter[i]);
752 }
Len Brown0539ba12017-02-10 00:27:20 -0500753 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400754 }
755
756 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200757 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400758
759 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
760 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
761 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
762 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
763
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200764 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500765 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500766 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500767 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500768 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500769 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500770 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200771 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
772 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
773 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
Len Brownbe0e54c2018-06-01 12:35:53 -0400774 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
775 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
776 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200777 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
778 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
779 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
780 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
781 outp += sprintf(outp, "Throttle PKG: %0X\n",
782 p->rapl_pkg_perf_status);
783 outp += sprintf(outp, "Throttle RAM: %0X\n",
784 p->rapl_dram_perf_status);
785 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500786
787 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
788 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
789 i, mp->msr_num, p->counter[i]);
790 }
Len Brownc98d5d92012-06-04 00:56:40 -0400791 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200792
793 outp += sprintf(outp, "\n");
794
Len Brownc98d5d92012-06-04 00:56:40 -0400795 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400796}
797
Len Browne23da032012-02-06 18:37:16 -0500798/*
799 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500800 */
Len Brownc98d5d92012-06-04 00:56:40 -0400801int format_counters(struct thread_data *t, struct core_data *c,
802 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400803{
Len Brown008d396e2017-02-10 00:29:51 -0500804 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500805 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500806 int i;
807 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500808 char *delim = "\t";
809 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400810
Len Brownc98d5d92012-06-04 00:56:40 -0400811 /* if showing only 1st thread in core and this isn't one, bail out */
812 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
813 return 0;
814
815 /* if showing only 1st thread in pkg and this isn't one, bail out */
816 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
817 return 0;
818
Len Brown1ef7d212017-02-10 23:54:15 -0500819 /*if not summary line and --cpu is used */
820 if ((t != &average.threads) &&
821 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
822 return 0;
823
Len Brown3f44a5c2017-10-17 15:42:56 -0400824 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700825 /* on each row, print how many usec each timestamp took to gather */
826 struct timeval tv;
827
828 timersub(&t->tv_end, &t->tv_begin, &tv);
829 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
830 }
831
Len Brown3f44a5c2017-10-17 15:42:56 -0400832 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
833 if (DO_BIC(BIC_TOD))
834 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
835
Len Brown103a8fe2010-10-22 23:53:03 -0400836 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
837
Len Brown008d396e2017-02-10 00:29:51 -0500838 tsc = t->tsc * tsc_tweak;
839
Len Brownc98d5d92012-06-04 00:56:40 -0400840 /* topo columns, print blanks on 1st (average) line */
841 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500842 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500843 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500844 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500845 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500846 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500847 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400848 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500849 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400850 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500851 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400852 else
Len Brown6168c2e2017-02-16 23:07:51 -0500853 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400854 }
Len Brown812db3f2017-02-10 00:25:41 -0500855 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400856 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500857 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400858 else
Len Brown6168c2e2017-02-16 23:07:51 -0500859 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400860 }
Len Brown812db3f2017-02-10 00:25:41 -0500861 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500862 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400863 }
Len Brownfc04cc62014-02-06 00:55:19 -0500864
Len Brown812db3f2017-02-10 00:25:41 -0500865 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500866 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500867 1.0 / units * t->aperf / interval_float);
868
Len Brown812db3f2017-02-10 00:25:41 -0500869 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500870 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400871
Len Brown812db3f2017-02-10 00:25:41 -0500872 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400873 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500874 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400875 else
Len Brown6168c2e2017-02-16 23:07:51 -0500876 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500877 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400878 }
Len Brown103a8fe2010-10-22 23:53:03 -0400879
Len Brown812db3f2017-02-10 00:25:41 -0500880 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500881 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400882
Len Brown562a2d32016-02-26 23:48:05 -0500883 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500884 if (DO_BIC(BIC_IRQ)) {
885 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500886 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500887 else
Len Brown6168c2e2017-02-16 23:07:51 -0500888 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500889 }
Len Brown562a2d32016-02-26 23:48:05 -0500890
Len Brown1cc21f72015-02-23 00:34:57 -0500891 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500892 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500893 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500894
Len Brown678a3bd2017-02-09 22:22:13 -0500895 /* Added counters */
896 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
897 if (mp->format == FORMAT_RAW) {
898 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500899 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500900 else
Len Brown6168c2e2017-02-16 23:07:51 -0500901 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500902 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500903 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500904 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500905 else
Len Brown6168c2e2017-02-16 23:07:51 -0500906 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500907 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500908 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500909 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500910 else
Len Brown6168c2e2017-02-16 23:07:51 -0500911 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500912 }
913 }
914
Len Brown41618e62017-02-09 18:25:22 -0500915 /* C1 */
916 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500917 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500918
919
Len Brownc98d5d92012-06-04 00:56:40 -0400920 /* print per-core data only for 1st thread in core */
921 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
922 goto done;
923
Len Brown812db3f2017-02-10 00:25:41 -0500924 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500925 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500926 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500927 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500928 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500929 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400930
Len Brown0539ba12017-02-10 00:27:20 -0500931 /* Mod%c6 */
932 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500933 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500934
Len Brown812db3f2017-02-10 00:25:41 -0500935 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500936 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500937
Len Brown388e9c82016-12-22 23:57:55 -0500938 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
939 if (mp->format == FORMAT_RAW) {
940 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500941 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500942 else
Len Brown6168c2e2017-02-16 23:07:51 -0500943 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500944 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500945 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500946 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500947 else
Len Brown6168c2e2017-02-16 23:07:51 -0500948 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500949 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500950 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500951 }
952 }
953
Len Brownc98d5d92012-06-04 00:56:40 -0400954 /* print per-package data only for 1st core in package */
955 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
956 goto done;
957
Len Brown0b2bb692015-03-26 00:50:30 -0400958 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500959 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500960 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500961
Len Brownfdf676e2016-02-27 01:28:12 -0500962 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500963 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400964 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500965 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400966 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500967 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400968 p->gfx_rc6_ms / 10.0 / interval_float);
969 }
970 }
Len Brownfdf676e2016-02-27 01:28:12 -0500971
Len Brown27d47352016-02-27 00:37:54 -0500972 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500973 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500974 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500975
Len Brown0b2bb692015-03-26 00:50:30 -0400976 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -0400977 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500978 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400979 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500980 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400981 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500982 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400983 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500984 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400985
Len Brown0f47c082017-01-27 00:50:45 -0500986 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500987 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500988 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500989 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500990 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500991 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500992 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500993 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500994 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500995 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500996 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500997 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500998 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500999 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -05001000
Len Brownbe0e54c2018-06-01 12:35:53 -04001001 if (DO_BIC(BIC_CPU_LPI))
1002 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1003 if (DO_BIC(BIC_SYS_LPI))
1004 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1005
Len Brown889facb2012-11-08 00:48:57 -05001006 /*
1007 * If measurement interval exceeds minimum RAPL Joule Counter range,
1008 * indicate that results are suspect by printing "**" in fraction place.
1009 */
Len Brownfc04cc62014-02-06 00:55:19 -05001010 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -05001011 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -05001012 else
Len Browne975db52016-04-06 23:56:02 -04001013 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -05001014
Len Brown812db3f2017-02-10 00:25:41 -05001015 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001016 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001017 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001018 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001019 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001020 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001021 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001022 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001023 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001024 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001025 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001026 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001027 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001028 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001029 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001030 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001031 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001032 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 -05001033 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001034 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 -05001035
Len Brown388e9c82016-12-22 23:57:55 -05001036 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1037 if (mp->format == FORMAT_RAW) {
1038 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001039 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001040 else
Len Brown6168c2e2017-02-16 23:07:51 -05001041 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001042 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001043 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001044 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001045 else
Len Brown6168c2e2017-02-16 23:07:51 -05001046 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001047 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001048 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001049 }
1050 }
1051
Len Brownc98d5d92012-06-04 00:56:40 -04001052done:
Len Brown94d6ab42018-01-27 22:39:21 -05001053 if (*(outp - 1) != '\n')
1054 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001055
1056 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001057}
1058
Len Brownb7d8c142016-02-13 23:36:17 -05001059void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001060{
Len Brownb7d8c142016-02-13 23:36:17 -05001061 FILE *filep;
1062
1063 if (outf == stderr)
1064 filep = stdout;
1065 else
1066 filep = outf;
1067
1068 fputs(output_buffer, filep);
1069 fflush(filep);
1070
Len Brownc98d5d92012-06-04 00:56:40 -04001071 outp = output_buffer;
1072}
Len Brownb7d8c142016-02-13 23:36:17 -05001073void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001074{
Len Brownb7d8c142016-02-13 23:36:17 -05001075 fputs(output_buffer, outf);
1076 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001077 outp = output_buffer;
1078}
1079void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1080{
Len Browne23da032012-02-06 18:37:16 -05001081 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001082
Len Browne23da032012-02-06 18:37:16 -05001083 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001084 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001085
Len Brownc98d5d92012-06-04 00:56:40 -04001086 if (topo.num_cpus > 1)
1087 format_counters(&average.threads, &average.cores,
1088 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001089
Len Browne23da032012-02-06 18:37:16 -05001090 printed = 1;
1091
1092 if (summary_only)
1093 return;
1094
Len Brownc98d5d92012-06-04 00:56:40 -04001095 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001096}
1097
Len Brown889facb2012-11-08 00:48:57 -05001098#define DELTA_WRAP32(new, old) \
1099 if (new > old) { \
1100 old = new - old; \
1101 } else { \
1102 old = 0x100000000 + new - old; \
1103 }
1104
Len Brownba3dec92016-04-22 20:31:46 -04001105int
Len Brownc98d5d92012-06-04 00:56:40 -04001106delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001107{
Len Brown388e9c82016-12-22 23:57:55 -05001108 int i;
1109 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001110
Len Browna99d8732017-05-20 20:11:55 -04001111
1112 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001113 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001114 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001115 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001116 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001117 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001118 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001119 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 -04001120
Len Brownc98d5d92012-06-04 00:56:40 -04001121 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001122 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001123 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001124 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001125 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001126 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001127 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001128 old->pc8 = new->pc8 - old->pc8;
1129 old->pc9 = new->pc9 - old->pc9;
1130 old->pc10 = new->pc10 - old->pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -04001131 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1132 old->sys_lpi = new->sys_lpi - old->sys_lpi;
Len Brown889facb2012-11-08 00:48:57 -05001133 old->pkg_temp_c = new->pkg_temp_c;
1134
Len Brown9185e982016-04-06 17:16:00 -04001135 /* flag an error when rc6 counter resets/wraps */
1136 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1137 old->gfx_rc6_ms = -1;
1138 else
1139 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1140
Len Brown27d47352016-02-27 00:37:54 -05001141 old->gfx_mhz = new->gfx_mhz;
1142
Len Brown889facb2012-11-08 00:48:57 -05001143 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1144 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1145 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1146 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1147 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1148 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001149
Len Brown388e9c82016-12-22 23:57:55 -05001150 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1151 if (mp->format == FORMAT_RAW)
1152 old->counter[i] = new->counter[i];
1153 else
1154 old->counter[i] = new->counter[i] - old->counter[i];
1155 }
1156
Len Brownba3dec92016-04-22 20:31:46 -04001157 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001158}
Len Brown103a8fe2010-10-22 23:53:03 -04001159
Len Brownc98d5d92012-06-04 00:56:40 -04001160void
1161delta_core(struct core_data *new, struct core_data *old)
1162{
Len Brown388e9c82016-12-22 23:57:55 -05001163 int i;
1164 struct msr_counter *mp;
1165
Len Brownc98d5d92012-06-04 00:56:40 -04001166 old->c3 = new->c3 - old->c3;
1167 old->c6 = new->c6 - old->c6;
1168 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001169 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001170 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001171
1172 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1173 if (mp->format == FORMAT_RAW)
1174 old->counter[i] = new->counter[i];
1175 else
1176 old->counter[i] = new->counter[i] - old->counter[i];
1177 }
Len Brownc98d5d92012-06-04 00:56:40 -04001178}
Len Brown103a8fe2010-10-22 23:53:03 -04001179
Len Brownc3ae3312012-06-13 21:31:46 -04001180/*
1181 * old = new - old
1182 */
Len Brownba3dec92016-04-22 20:31:46 -04001183int
Len Brownc98d5d92012-06-04 00:56:40 -04001184delta_thread(struct thread_data *new, struct thread_data *old,
1185 struct core_data *core_delta)
1186{
Len Brown388e9c82016-12-22 23:57:55 -05001187 int i;
1188 struct msr_counter *mp;
1189
Len Brown3f44a5c2017-10-17 15:42:56 -04001190 /*
1191 * the timestamps from start of measurement interval are in "old"
1192 * the timestamp from end of measurement interval are in "new"
1193 * over-write old w/ new so we can print end of interval values
1194 */
1195
1196 old->tv_begin = new->tv_begin;
1197 old->tv_end = new->tv_end;
1198
Len Brownc98d5d92012-06-04 00:56:40 -04001199 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001200
Len Brownc98d5d92012-06-04 00:56:40 -04001201 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001202 if (old->tsc < (1000 * 1000))
1203 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1204 "You can disable all c-states by booting with \"idle=poll\"\n"
1205 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001206
Len Brownc98d5d92012-06-04 00:56:40 -04001207 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001208
Len Brown812db3f2017-02-10 00:25:41 -05001209 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001210 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1211 old->aperf = new->aperf - old->aperf;
1212 old->mperf = new->mperf - old->mperf;
1213 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001214 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001215 }
Len Brownc98d5d92012-06-04 00:56:40 -04001216 }
Len Brown103a8fe2010-10-22 23:53:03 -04001217
Len Brown103a8fe2010-10-22 23:53:03 -04001218
Len Brown144b44b2013-11-09 00:30:16 -05001219 if (use_c1_residency_msr) {
1220 /*
1221 * Some models have a dedicated C1 residency MSR,
1222 * which should be more accurate than the derivation below.
1223 */
1224 } else {
1225 /*
1226 * As counter collection is not atomic,
1227 * it is possible for mperf's non-halted cycles + idle states
1228 * to exceed TSC's all cycles: show c1 = 0% in that case.
1229 */
Len Brown95149362017-04-12 19:44:51 -04001230 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001231 old->c1 = 0;
1232 else {
1233 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001234 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001235 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001236 }
Len Brownc98d5d92012-06-04 00:56:40 -04001237 }
Len Brownc3ae3312012-06-13 21:31:46 -04001238
Len Brownc98d5d92012-06-04 00:56:40 -04001239 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001240 if (debug > 1)
1241 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001242 old->mperf = 1; /* divide by 0 protection */
1243 }
1244
Len Brown812db3f2017-02-10 00:25:41 -05001245 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001246 old->irq_count = new->irq_count - old->irq_count;
1247
Len Brown812db3f2017-02-10 00:25:41 -05001248 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001249 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001250
Len Brown388e9c82016-12-22 23:57:55 -05001251 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1252 if (mp->format == FORMAT_RAW)
1253 old->counter[i] = new->counter[i];
1254 else
1255 old->counter[i] = new->counter[i] - old->counter[i];
1256 }
Len Brownba3dec92016-04-22 20:31:46 -04001257 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001258}
1259
1260int delta_cpu(struct thread_data *t, struct core_data *c,
1261 struct pkg_data *p, struct thread_data *t2,
1262 struct core_data *c2, struct pkg_data *p2)
1263{
Len Brownba3dec92016-04-22 20:31:46 -04001264 int retval = 0;
1265
Len Brownc98d5d92012-06-04 00:56:40 -04001266 /* calculate core delta only for 1st thread in core */
1267 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1268 delta_core(c, c2);
1269
1270 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001271 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1272 if (retval)
1273 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001274
1275 /* calculate package delta only for 1st core in package */
1276 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001277 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001278
Len Brownba3dec92016-04-22 20:31:46 -04001279 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001280}
1281
1282void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1283{
Len Brown388e9c82016-12-22 23:57:55 -05001284 int i;
1285 struct msr_counter *mp;
1286
Len Brown3f44a5c2017-10-17 15:42:56 -04001287 t->tv_begin.tv_sec = 0;
1288 t->tv_begin.tv_usec = 0;
1289 t->tv_end.tv_sec = 0;
1290 t->tv_end.tv_usec = 0;
1291
Len Brownc98d5d92012-06-04 00:56:40 -04001292 t->tsc = 0;
1293 t->aperf = 0;
1294 t->mperf = 0;
1295 t->c1 = 0;
1296
Len Brown562a2d32016-02-26 23:48:05 -05001297 t->irq_count = 0;
1298 t->smi_count = 0;
1299
Len Brownc98d5d92012-06-04 00:56:40 -04001300 /* tells format_counters to dump all fields from this set */
1301 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1302
1303 c->c3 = 0;
1304 c->c6 = 0;
1305 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001306 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001307 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001308
Len Brown0b2bb692015-03-26 00:50:30 -04001309 p->pkg_wtd_core_c0 = 0;
1310 p->pkg_any_core_c0 = 0;
1311 p->pkg_any_gfxe_c0 = 0;
1312 p->pkg_both_core_gfxe_c0 = 0;
1313
Len Brownc98d5d92012-06-04 00:56:40 -04001314 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001315 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001316 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001317 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001318 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001319 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001320 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001321 p->pc8 = 0;
1322 p->pc9 = 0;
1323 p->pc10 = 0;
Len Brownbe0e54c2018-06-01 12:35:53 -04001324 p->cpu_lpi = 0;
1325 p->sys_lpi = 0;
Len Brown889facb2012-11-08 00:48:57 -05001326
1327 p->energy_pkg = 0;
1328 p->energy_dram = 0;
1329 p->energy_cores = 0;
1330 p->energy_gfx = 0;
1331 p->rapl_pkg_perf_status = 0;
1332 p->rapl_dram_perf_status = 0;
1333 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001334
Len Brownfdf676e2016-02-27 01:28:12 -05001335 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001336 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001337 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1338 t->counter[i] = 0;
1339
1340 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1341 c->counter[i] = 0;
1342
1343 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1344 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001345}
1346int sum_counters(struct thread_data *t, struct core_data *c,
1347 struct pkg_data *p)
1348{
Len Brown388e9c82016-12-22 23:57:55 -05001349 int i;
1350 struct msr_counter *mp;
1351
Len Brown3f44a5c2017-10-17 15:42:56 -04001352 /* remember first tv_begin */
1353 if (average.threads.tv_begin.tv_sec == 0)
1354 average.threads.tv_begin = t->tv_begin;
1355
1356 /* remember last tv_end */
1357 average.threads.tv_end = t->tv_end;
1358
Len Brownc98d5d92012-06-04 00:56:40 -04001359 average.threads.tsc += t->tsc;
1360 average.threads.aperf += t->aperf;
1361 average.threads.mperf += t->mperf;
1362 average.threads.c1 += t->c1;
1363
Len Brown562a2d32016-02-26 23:48:05 -05001364 average.threads.irq_count += t->irq_count;
1365 average.threads.smi_count += t->smi_count;
1366
Len Brown388e9c82016-12-22 23:57:55 -05001367 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1368 if (mp->format == FORMAT_RAW)
1369 continue;
1370 average.threads.counter[i] += t->counter[i];
1371 }
1372
Len Brownc98d5d92012-06-04 00:56:40 -04001373 /* sum per-core values only for 1st thread in core */
1374 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1375 return 0;
1376
1377 average.cores.c3 += c->c3;
1378 average.cores.c6 += c->c6;
1379 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001380 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001381
Len Brown889facb2012-11-08 00:48:57 -05001382 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1383
Len Brown388e9c82016-12-22 23:57:55 -05001384 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1385 if (mp->format == FORMAT_RAW)
1386 continue;
1387 average.cores.counter[i] += c->counter[i];
1388 }
1389
Len Brownc98d5d92012-06-04 00:56:40 -04001390 /* sum per-pkg values only for 1st core in pkg */
1391 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1392 return 0;
1393
Len Browna99d8732017-05-20 20:11:55 -04001394 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001395 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001396 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001397 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001398 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001399 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001400 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001401 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001402
Len Brownc98d5d92012-06-04 00:56:40 -04001403 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001404 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001405 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001406 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001407 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001408 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001409 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001410 average.packages.pc8 += p->pc8;
1411 average.packages.pc9 += p->pc9;
1412 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001413
Len Brownbe0e54c2018-06-01 12:35:53 -04001414 average.packages.cpu_lpi = p->cpu_lpi;
1415 average.packages.sys_lpi = p->sys_lpi;
1416
Len Brown889facb2012-11-08 00:48:57 -05001417 average.packages.energy_pkg += p->energy_pkg;
1418 average.packages.energy_dram += p->energy_dram;
1419 average.packages.energy_cores += p->energy_cores;
1420 average.packages.energy_gfx += p->energy_gfx;
1421
Len Brownfdf676e2016-02-27 01:28:12 -05001422 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001423 average.packages.gfx_mhz = p->gfx_mhz;
1424
Len Brown889facb2012-11-08 00:48:57 -05001425 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1426
1427 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1428 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001429
1430 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1431 if (mp->format == FORMAT_RAW)
1432 continue;
1433 average.packages.counter[i] += p->counter[i];
1434 }
Len Brownc98d5d92012-06-04 00:56:40 -04001435 return 0;
1436}
1437/*
1438 * sum the counters for all cpus in the system
1439 * compute the weighted average
1440 */
1441void compute_average(struct thread_data *t, struct core_data *c,
1442 struct pkg_data *p)
1443{
Len Brown388e9c82016-12-22 23:57:55 -05001444 int i;
1445 struct msr_counter *mp;
1446
Len Brownc98d5d92012-06-04 00:56:40 -04001447 clear_counters(&average.threads, &average.cores, &average.packages);
1448
1449 for_all_cpus(sum_counters, t, c, p);
1450
1451 average.threads.tsc /= topo.num_cpus;
1452 average.threads.aperf /= topo.num_cpus;
1453 average.threads.mperf /= topo.num_cpus;
1454 average.threads.c1 /= topo.num_cpus;
1455
Len Brown0de6c0d2017-02-15 21:45:40 -05001456 if (average.threads.irq_count > 9999999)
1457 sums_need_wide_columns = 1;
1458
Len Brownc98d5d92012-06-04 00:56:40 -04001459 average.cores.c3 /= topo.num_cores;
1460 average.cores.c6 /= topo.num_cores;
1461 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001462 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001463
Len Browna99d8732017-05-20 20:11:55 -04001464 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001465 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001466 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001467 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001468 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001469 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001470 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001471 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001472
Len Brownc98d5d92012-06-04 00:56:40 -04001473 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001474 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001475 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001476 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001477 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001478 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001479 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001480
1481 average.packages.pc8 /= topo.num_packages;
1482 average.packages.pc9 /= topo.num_packages;
1483 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001484
1485 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1486 if (mp->format == FORMAT_RAW)
1487 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001488 if (mp->type == COUNTER_ITEMS) {
1489 if (average.threads.counter[i] > 9999999)
1490 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001491 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001492 }
Len Brown388e9c82016-12-22 23:57:55 -05001493 average.threads.counter[i] /= topo.num_cpus;
1494 }
1495 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1496 if (mp->format == FORMAT_RAW)
1497 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001498 if (mp->type == COUNTER_ITEMS) {
1499 if (average.cores.counter[i] > 9999999)
1500 sums_need_wide_columns = 1;
1501 }
Len Brown388e9c82016-12-22 23:57:55 -05001502 average.cores.counter[i] /= topo.num_cores;
1503 }
1504 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1505 if (mp->format == FORMAT_RAW)
1506 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001507 if (mp->type == COUNTER_ITEMS) {
1508 if (average.packages.counter[i] > 9999999)
1509 sums_need_wide_columns = 1;
1510 }
Len Brown388e9c82016-12-22 23:57:55 -05001511 average.packages.counter[i] /= topo.num_packages;
1512 }
Len Brownc98d5d92012-06-04 00:56:40 -04001513}
1514
1515static unsigned long long rdtsc(void)
1516{
1517 unsigned int low, high;
1518
1519 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1520
1521 return low | ((unsigned long long)high) << 32;
1522}
1523
Len Brownc98d5d92012-06-04 00:56:40 -04001524/*
Len Brown495c76542017-02-08 02:41:51 -05001525 * Open a file, and exit on failure
1526 */
1527FILE *fopen_or_die(const char *path, const char *mode)
1528{
1529 FILE *filep = fopen(path, mode);
1530
1531 if (!filep)
1532 err(1, "%s: open failed", path);
1533 return filep;
1534}
1535/*
1536 * snapshot_sysfs_counter()
1537 *
1538 * return snapshot of given counter
1539 */
1540unsigned long long snapshot_sysfs_counter(char *path)
1541{
1542 FILE *fp;
1543 int retval;
1544 unsigned long long counter;
1545
1546 fp = fopen_or_die(path, "r");
1547
1548 retval = fscanf(fp, "%lld", &counter);
1549 if (retval != 1)
1550 err(1, "snapshot_sysfs_counter(%s)", path);
1551
1552 fclose(fp);
1553
1554 return counter;
1555}
1556
1557int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1558{
1559 if (mp->msr_num != 0) {
1560 if (get_msr(cpu, mp->msr_num, counterp))
1561 return -1;
1562 } else {
Len Brown46c27972017-12-08 17:38:17 -05001563 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001564
1565 if (mp->flags & SYSFS_PERCPU) {
1566 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1567 cpu, mp->path);
1568
1569 *counterp = snapshot_sysfs_counter(path);
1570 } else {
1571 *counterp = snapshot_sysfs_counter(mp->path);
1572 }
Len Brown495c76542017-02-08 02:41:51 -05001573 }
1574
1575 return 0;
1576}
1577
1578/*
Len Brownc98d5d92012-06-04 00:56:40 -04001579 * get_counters(...)
1580 * migrate to cpu
1581 * acquire and record local counters for that cpu
1582 */
1583int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1584{
1585 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001586 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001587 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001588 struct msr_counter *mp;
1589 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001590
Len Brownf4fdf2b2017-05-27 21:06:55 -07001591
1592 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1593
Len Browne52966c2012-11-08 22:38:05 -05001594 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001595 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001596 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001597 }
Len Brownc98d5d92012-06-04 00:56:40 -04001598
Len Brown0102b062016-02-27 03:11:29 -05001599retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001600 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1601
Len Brown812db3f2017-02-10 00:25:41 -05001602 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001603 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1604
1605 /*
1606 * The TSC, APERF and MPERF must be read together for
1607 * APERF/MPERF and MPERF/TSC to give accurate results.
1608 *
1609 * Unfortunately, APERF and MPERF are read by
1610 * individual system call, so delays may occur
1611 * between them. If the time to read them
1612 * varies by a large amount, we re-read them.
1613 */
1614
1615 /*
1616 * This initial dummy APERF read has been seen to
1617 * reduce jitter in the subsequent reads.
1618 */
1619
Len Brown9c63a652012-10-31 01:29:52 -04001620 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001621 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001622
1623 t->tsc = rdtsc(); /* re-read close to APERF */
1624
1625 tsc_before = t->tsc;
1626
1627 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1628 return -3;
1629
1630 tsc_between = rdtsc();
1631
Len Brown9c63a652012-10-31 01:29:52 -04001632 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001633 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001634
1635 tsc_after = rdtsc();
1636
1637 aperf_time = tsc_between - tsc_before;
1638 mperf_time = tsc_after - tsc_between;
1639
1640 /*
1641 * If the system call latency to read APERF and MPERF
1642 * differ by more than 2x, then try again.
1643 */
1644 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1645 aperf_mperf_retry_count++;
1646 if (aperf_mperf_retry_count < 5)
1647 goto retry;
1648 else
1649 warnx("cpu%d jitter %lld %lld",
1650 cpu, aperf_time, mperf_time);
1651 }
1652 aperf_mperf_retry_count = 0;
1653
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001654 t->aperf = t->aperf * aperf_mperf_multiplier;
1655 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001656 }
1657
Len Brown812db3f2017-02-10 00:25:41 -05001658 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001659 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001660 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001661 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1662 return -5;
1663 t->smi_count = msr & 0xFFFFFFFF;
1664 }
Len Brown0539ba12017-02-10 00:27:20 -05001665 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001666 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1667 return -6;
1668 }
1669
Len Brown388e9c82016-12-22 23:57:55 -05001670 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001671 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001672 return -10;
1673 }
1674
Len Brownc98d5d92012-06-04 00:56:40 -04001675 /* collect core counters only for 1st thread in core */
1676 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001677 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001678
Len Brown812db3f2017-02-10 00:25:41 -05001679 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001680 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1681 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001682 }
1683
Len Brown812db3f2017-02-10 00:25:41 -05001684 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001685 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1686 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001687 } else if (do_knl_cstates) {
1688 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1689 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001690 }
1691
Len Brown812db3f2017-02-10 00:25:41 -05001692 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001693 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1694 return -8;
1695
Len Brown0539ba12017-02-10 00:27:20 -05001696 if (DO_BIC(BIC_Mod_c6))
1697 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1698 return -8;
1699
Len Brown812db3f2017-02-10 00:25:41 -05001700 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001701 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1702 return -9;
1703 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1704 }
1705
Len Brown388e9c82016-12-22 23:57:55 -05001706 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001707 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001708 return -10;
1709 }
Len Brown889facb2012-11-08 00:48:57 -05001710
Len Brownc98d5d92012-06-04 00:56:40 -04001711 /* collect package counters only for 1st core in package */
1712 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001713 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001714
Len Browna99d8732017-05-20 20:11:55 -04001715 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001716 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1717 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001718 }
1719 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001720 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1721 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001722 }
1723 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001724 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1725 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001726 }
1727 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001728 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1729 return -13;
1730 }
Len Brown0f47c082017-01-27 00:50:45 -05001731 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001732 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1733 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001734 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001735 if (do_slm_cstates) {
1736 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1737 return -10;
1738 } else {
1739 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1740 return -10;
1741 }
1742 }
1743
Len Brown0f47c082017-01-27 00:50:45 -05001744 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001745 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1746 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001747 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001748 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1749 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001750 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001751 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1752 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001753 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001754 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1755 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001756 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001757 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1758 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001759
Len Brownbe0e54c2018-06-01 12:35:53 -04001760 if (DO_BIC(BIC_CPU_LPI))
1761 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1762 if (DO_BIC(BIC_SYS_LPI))
1763 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1764
Len Brown889facb2012-11-08 00:48:57 -05001765 if (do_rapl & RAPL_PKG) {
1766 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1767 return -13;
1768 p->energy_pkg = msr & 0xFFFFFFFF;
1769 }
Jacob Pan91484942016-06-16 09:48:20 -07001770 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001771 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1772 return -14;
1773 p->energy_cores = msr & 0xFFFFFFFF;
1774 }
1775 if (do_rapl & RAPL_DRAM) {
1776 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1777 return -15;
1778 p->energy_dram = msr & 0xFFFFFFFF;
1779 }
1780 if (do_rapl & RAPL_GFX) {
1781 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1782 return -16;
1783 p->energy_gfx = msr & 0xFFFFFFFF;
1784 }
1785 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1786 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1787 return -16;
1788 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1789 }
1790 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1791 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1792 return -16;
1793 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1794 }
Len Brown812db3f2017-02-10 00:25:41 -05001795 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001796 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1797 return -17;
1798 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1799 }
Len Brownfdf676e2016-02-27 01:28:12 -05001800
Len Brown812db3f2017-02-10 00:25:41 -05001801 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001802 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1803
Len Brown812db3f2017-02-10 00:25:41 -05001804 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001805 p->gfx_mhz = gfx_cur_mhz;
1806
Len Brown388e9c82016-12-22 23:57:55 -05001807 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001808 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001809 return -10;
1810 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001811done:
1812 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001813
Len Brown103a8fe2010-10-22 23:53:03 -04001814 return 0;
1815}
1816
Len Brownee7e38e2015-02-09 23:39:45 -05001817/*
1818 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1819 * If you change the values, note they are used both in comparisons
1820 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1821 */
1822
1823#define PCLUKN 0 /* Unknown */
1824#define PCLRSV 1 /* Reserved */
1825#define PCL__0 2 /* PC0 */
1826#define PCL__1 3 /* PC1 */
1827#define PCL__2 4 /* PC2 */
1828#define PCL__3 5 /* PC3 */
1829#define PCL__4 6 /* PC4 */
1830#define PCL__6 7 /* PC6 */
1831#define PCL_6N 8 /* PC6 No Retention */
1832#define PCL_6R 9 /* PC6 Retention */
1833#define PCL__7 10 /* PC7 */
1834#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001835#define PCL__8 12 /* PC8 */
1836#define PCL__9 13 /* PC9 */
1837#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001838
1839int pkg_cstate_limit = PCLUKN;
1840char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001841 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001842
Len Browne9257f52015-04-01 21:02:57 -04001843int 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};
1844int 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};
1845int 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 -05001846int 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 -05001847int 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 -04001848int 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 -04001849int 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 +03001850int 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 -05001851
Len Browna2b7b742015-09-26 00:12:38 -04001852
1853static void
1854calculate_tsc_tweak()
1855{
Len Browna2b7b742015-09-26 00:12:38 -04001856 tsc_tweak = base_hz / tsc_hz;
1857}
1858
Len Brownfcd17212015-03-23 20:29:09 -04001859static void
1860dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001861{
1862 unsigned long long msr;
1863 unsigned int ratio;
1864
Len Brownec0adc52015-11-12 02:42:31 -05001865 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001866
Len Brownb7d8c142016-02-13 23:36:17 -05001867 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001868
Len Brown103a8fe2010-10-22 23:53:03 -04001869 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001870 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001871 ratio, bclk, ratio * bclk);
1872
1873 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001874 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001875 ratio, bclk, ratio * bclk);
1876
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001877 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001878 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001879 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001880
Len Brownfcd17212015-03-23 20:29:09 -04001881 return;
1882}
1883
1884static void
1885dump_hsw_turbo_ratio_limits(void)
1886{
1887 unsigned long long msr;
1888 unsigned int ratio;
1889
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001890 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001891
Len Brownb7d8c142016-02-13 23:36:17 -05001892 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001893
1894 ratio = (msr >> 8) & 0xFF;
1895 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001896 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001897 ratio, bclk, ratio * bclk);
1898
1899 ratio = (msr >> 0) & 0xFF;
1900 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001901 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001902 ratio, bclk, ratio * bclk);
1903 return;
1904}
1905
1906static void
1907dump_ivt_turbo_ratio_limits(void)
1908{
1909 unsigned long long msr;
1910 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001911
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001912 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001913
Len Brownb7d8c142016-02-13 23:36:17 -05001914 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001915
1916 ratio = (msr >> 56) & 0xFF;
1917 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001918 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001919 ratio, bclk, ratio * bclk);
1920
1921 ratio = (msr >> 48) & 0xFF;
1922 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001923 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001924 ratio, bclk, ratio * bclk);
1925
1926 ratio = (msr >> 40) & 0xFF;
1927 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001928 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001929 ratio, bclk, ratio * bclk);
1930
1931 ratio = (msr >> 32) & 0xFF;
1932 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001933 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001934 ratio, bclk, ratio * bclk);
1935
1936 ratio = (msr >> 24) & 0xFF;
1937 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001938 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001939 ratio, bclk, ratio * bclk);
1940
1941 ratio = (msr >> 16) & 0xFF;
1942 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001943 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001944 ratio, bclk, ratio * bclk);
1945
1946 ratio = (msr >> 8) & 0xFF;
1947 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001948 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001949 ratio, bclk, ratio * bclk);
1950
1951 ratio = (msr >> 0) & 0xFF;
1952 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001953 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001954 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001955 return;
1956}
Len Brown31e07522017-01-31 23:07:49 -05001957int has_turbo_ratio_group_limits(int family, int model)
1958{
1959
1960 if (!genuine_intel)
1961 return 0;
1962
1963 switch (model) {
1964 case INTEL_FAM6_ATOM_GOLDMONT:
1965 case INTEL_FAM6_SKYLAKE_X:
1966 case INTEL_FAM6_ATOM_DENVERTON:
1967 return 1;
1968 }
1969 return 0;
1970}
Len Brown6574a5d2012-09-21 00:01:31 -04001971
Len Brownfcd17212015-03-23 20:29:09 -04001972static void
Len Brown31e07522017-01-31 23:07:49 -05001973dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001974{
Len Brown31e07522017-01-31 23:07:49 -05001975 unsigned long long msr, core_counts;
1976 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001977
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001978 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001979 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001980
Len Brown31e07522017-01-31 23:07:49 -05001981 if (has_turbo_ratio_group_limits(family, model)) {
1982 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1983 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1984 } else {
1985 core_counts = 0x0807060504030201;
1986 }
1987
Len Brown6574a5d2012-09-21 00:01:31 -04001988 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001989 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001990 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001991 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1992 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001993
1994 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001995 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001996 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001997 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1998 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001999
2000 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002001 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002002 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002003 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2004 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002005
2006 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002007 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002008 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002009 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2010 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002011
Len Brown103a8fe2010-10-22 23:53:03 -04002012 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002013 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002014 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002015 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2016 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002017
2018 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002019 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002020 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002021 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2022 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002023
2024 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002025 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002026 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002027 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2028 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002029
2030 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002031 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002032 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002033 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2034 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002035 return;
2036}
Len Brown3a9a9412014-08-15 02:39:52 -04002037
Len Brownfcd17212015-03-23 20:29:09 -04002038static void
Len Brown0f7887c2017-01-12 23:49:18 -05002039dump_atom_turbo_ratio_limits(void)
2040{
2041 unsigned long long msr;
2042 unsigned int ratio;
2043
2044 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2045 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2046
2047 ratio = (msr >> 0) & 0x3F;
2048 if (ratio)
2049 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2050 ratio, bclk, ratio * bclk);
2051
2052 ratio = (msr >> 8) & 0x3F;
2053 if (ratio)
2054 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2055 ratio, bclk, ratio * bclk);
2056
2057 ratio = (msr >> 16) & 0x3F;
2058 if (ratio)
2059 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2060 ratio, bclk, ratio * bclk);
2061
2062 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2063 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2064
2065 ratio = (msr >> 24) & 0x3F;
2066 if (ratio)
2067 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2068 ratio, bclk, ratio * bclk);
2069
2070 ratio = (msr >> 16) & 0x3F;
2071 if (ratio)
2072 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2073 ratio, bclk, ratio * bclk);
2074
2075 ratio = (msr >> 8) & 0x3F;
2076 if (ratio)
2077 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2078 ratio, bclk, ratio * bclk);
2079
2080 ratio = (msr >> 0) & 0x3F;
2081 if (ratio)
2082 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2083 ratio, bclk, ratio * bclk);
2084}
2085
2086static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002087dump_knl_turbo_ratio_limits(void)
2088{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002089 const unsigned int buckets_no = 7;
2090
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002091 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002092 int delta_cores, delta_ratio;
2093 int i, b_nr;
2094 unsigned int cores[buckets_no];
2095 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002096
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002097 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002098
Len Brownb7d8c142016-02-13 23:36:17 -05002099 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002100 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002101
2102 /**
2103 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002104 * [0] -- Reserved
2105 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002106 * [15:8] -- Base value of freq ratio of bucket 1.
2107 * [20:16] -- +ve delta of number of active cores of bucket 2.
2108 * i.e. active cores of bucket 2 =
2109 * active cores of bucket 1 + delta
2110 * [23:21] -- Negative delta of freq ratio of bucket 2.
2111 * i.e. freq ratio of bucket 2 =
2112 * freq ratio of bucket 1 - delta
2113 * [28:24]-- +ve delta of number of active cores of bucket 3.
2114 * [31:29]-- -ve delta of freq ratio of bucket 3.
2115 * [36:32]-- +ve delta of number of active cores of bucket 4.
2116 * [39:37]-- -ve delta of freq ratio of bucket 4.
2117 * [44:40]-- +ve delta of number of active cores of bucket 5.
2118 * [47:45]-- -ve delta of freq ratio of bucket 5.
2119 * [52:48]-- +ve delta of number of active cores of bucket 6.
2120 * [55:53]-- -ve delta of freq ratio of bucket 6.
2121 * [60:56]-- +ve delta of number of active cores of bucket 7.
2122 * [63:61]-- -ve delta of freq ratio of bucket 7.
2123 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002124
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002125 b_nr = 0;
2126 cores[b_nr] = (msr & 0xFF) >> 1;
2127 ratio[b_nr] = (msr >> 8) & 0xFF;
2128
2129 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002130 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002131 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002132
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002133 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2134 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2135 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002136 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002137
2138 for (i = buckets_no - 1; i >= 0; i--)
2139 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002140 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002141 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002142 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002143}
2144
2145static void
Len Brownfcd17212015-03-23 20:29:09 -04002146dump_nhm_cst_cfg(void)
2147{
2148 unsigned long long msr;
2149
Len Brown1df2e552017-01-07 23:24:57 -05002150 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002151
Len Brown1df2e552017-01-07 23:24:57 -05002152 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002153
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002154 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002155 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2156 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2157 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2158 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2159 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002160 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002161 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002162
2163#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2164 if (has_automatic_cstate_conversion) {
2165 fprintf(outf, ", automatic c-state conversion=%s",
2166 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2167 }
2168
2169 fprintf(outf, ")\n");
2170
Len Brownfcd17212015-03-23 20:29:09 -04002171 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002172}
2173
Len Brown6fb31432015-06-17 16:23:45 -04002174static void
2175dump_config_tdp(void)
2176{
2177 unsigned long long msr;
2178
2179 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002180 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002181 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002182
2183 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002184 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002185 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002186 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2187 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2188 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2189 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002190 }
Len Brownb7d8c142016-02-13 23:36:17 -05002191 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002192
2193 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002194 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002195 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002196 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2197 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2198 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2199 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002200 }
Len Brownb7d8c142016-02-13 23:36:17 -05002201 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002202
2203 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002204 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002205 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002206 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2207 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2208 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002209
Len Brown6fb31432015-06-17 16:23:45 -04002210 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002211 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002212 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002213 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2214 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002215}
Len Brown5a634262016-04-06 17:15:55 -04002216
2217unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2218
2219void print_irtl(void)
2220{
2221 unsigned long long msr;
2222
2223 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2224 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2225 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2226 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2227
2228 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2229 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2230 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2231 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2232
2233 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2234 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2235 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2236 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2237
2238 if (!do_irtl_hsw)
2239 return;
2240
2241 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2242 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2243 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2244 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2245
2246 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2247 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2248 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2249 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2250
2251 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2252 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2253 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2254 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2255
2256}
Len Brown36229892016-02-26 20:51:02 -05002257void free_fd_percpu(void)
2258{
2259 int i;
2260
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002261 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002262 if (fd_percpu[i] != 0)
2263 close(fd_percpu[i]);
2264 }
2265
2266 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002267}
2268
Len Brownc98d5d92012-06-04 00:56:40 -04002269void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002270{
Len Brownc98d5d92012-06-04 00:56:40 -04002271 CPU_FREE(cpu_present_set);
2272 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002273 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002274
Len Brownc98d5d92012-06-04 00:56:40 -04002275 CPU_FREE(cpu_affinity_set);
2276 cpu_affinity_set = NULL;
2277 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002278
Len Brownc98d5d92012-06-04 00:56:40 -04002279 free(thread_even);
2280 free(core_even);
2281 free(package_even);
2282
2283 thread_even = NULL;
2284 core_even = NULL;
2285 package_even = NULL;
2286
2287 free(thread_odd);
2288 free(core_odd);
2289 free(package_odd);
2290
2291 thread_odd = NULL;
2292 core_odd = NULL;
2293 package_odd = NULL;
2294
2295 free(output_buffer);
2296 output_buffer = NULL;
2297 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002298
2299 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002300
2301 free(irq_column_2_cpu);
2302 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002303}
2304
Josh Triplett57a42a32013-08-20 17:20:17 -07002305
2306/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002307 * Parse a file containing a single int.
2308 */
2309int parse_int_file(const char *fmt, ...)
2310{
2311 va_list args;
2312 char path[PATH_MAX];
2313 FILE *filep;
2314 int value;
2315
2316 va_start(args, fmt);
2317 vsnprintf(path, sizeof(path), fmt, args);
2318 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002319 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002320 if (fscanf(filep, "%d", &value) != 1)
2321 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002322 fclose(filep);
2323 return value;
2324}
2325
2326/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002327 * get_cpu_position_in_core(cpu)
2328 * return the position of the CPU among its HT siblings in the core
2329 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002330 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002331int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002332{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002333 char path[64];
2334 FILE *filep;
2335 int this_cpu;
2336 char character;
2337 int i;
2338
2339 sprintf(path,
2340 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2341 cpu);
2342 filep = fopen(path, "r");
2343 if (filep == NULL) {
2344 perror(path);
2345 exit(1);
2346 }
2347
2348 for (i = 0; i < topo.num_threads_per_core; i++) {
2349 fscanf(filep, "%d", &this_cpu);
2350 if (this_cpu == cpu) {
2351 fclose(filep);
2352 return i;
2353 }
2354
2355 /* Account for no separator after last thread*/
2356 if (i != (topo.num_threads_per_core - 1))
2357 fscanf(filep, "%c", &character);
2358 }
2359
2360 fclose(filep);
2361 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002362}
2363
Len Brownc98d5d92012-06-04 00:56:40 -04002364/*
2365 * cpu_is_first_core_in_package(cpu)
2366 * return 1 if given CPU is 1st core in package
2367 */
2368int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002369{
Josh Triplett95aebc42013-08-20 17:20:16 -07002370 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002371}
2372
2373int get_physical_package_id(int cpu)
2374{
Josh Triplett95aebc42013-08-20 17:20:16 -07002375 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002376}
2377
2378int get_core_id(int cpu)
2379{
Josh Triplett95aebc42013-08-20 17:20:16 -07002380 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002381}
2382
Len Brownc98d5d92012-06-04 00:56:40 -04002383int get_num_ht_siblings(int cpu)
2384{
2385 char path[80];
2386 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002387 int sib1;
2388 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002389 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002390 char str[100];
2391 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002392
2393 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002394 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002395
Len Brownc98d5d92012-06-04 00:56:40 -04002396 /*
2397 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002398 * A ',' separated or '-' separated set of numbers
2399 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002400 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002401 fscanf(filep, "%d%c\n", &sib1, &character);
2402 fseek(filep, 0, SEEK_SET);
2403 fgets(str, 100, filep);
2404 ch = strchr(str, character);
2405 while (ch != NULL) {
2406 matches++;
2407 ch = strchr(ch+1, character);
2408 }
Len Brownc98d5d92012-06-04 00:56:40 -04002409
2410 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002411 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002412}
2413
Len Brown103a8fe2010-10-22 23:53:03 -04002414/*
Len Brownc98d5d92012-06-04 00:56:40 -04002415 * run func(thread, core, package) in topology order
2416 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002417 */
2418
Len Brownc98d5d92012-06-04 00:56:40 -04002419int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2420 struct pkg_data *, struct thread_data *, struct core_data *,
2421 struct pkg_data *), struct thread_data *thread_base,
2422 struct core_data *core_base, struct pkg_data *pkg_base,
2423 struct thread_data *thread_base2, struct core_data *core_base2,
2424 struct pkg_data *pkg_base2)
2425{
2426 int retval, pkg_no, core_no, thread_no;
2427
2428 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2429 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2430 for (thread_no = 0; thread_no <
2431 topo.num_threads_per_core; ++thread_no) {
2432 struct thread_data *t, *t2;
2433 struct core_data *c, *c2;
2434 struct pkg_data *p, *p2;
2435
2436 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2437
2438 if (cpu_is_not_present(t->cpu_id))
2439 continue;
2440
2441 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2442
2443 c = GET_CORE(core_base, core_no, pkg_no);
2444 c2 = GET_CORE(core_base2, core_no, pkg_no);
2445
2446 p = GET_PKG(pkg_base, pkg_no);
2447 p2 = GET_PKG(pkg_base2, pkg_no);
2448
2449 retval = func(t, c, p, t2, c2, p2);
2450 if (retval)
2451 return retval;
2452 }
2453 }
2454 }
2455 return 0;
2456}
2457
2458/*
2459 * run func(cpu) on every cpu in /proc/stat
2460 * return max_cpu number
2461 */
2462int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002463{
2464 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002465 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002466 int retval;
2467
Josh Triplett57a42a32013-08-20 17:20:17 -07002468 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002469
2470 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002471 if (retval != 0)
2472 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002473
Len Brownc98d5d92012-06-04 00:56:40 -04002474 while (1) {
2475 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 -04002476 if (retval != 1)
2477 break;
2478
Len Brownc98d5d92012-06-04 00:56:40 -04002479 retval = func(cpu_num);
2480 if (retval) {
2481 fclose(fp);
2482 return(retval);
2483 }
Len Brown103a8fe2010-10-22 23:53:03 -04002484 }
2485 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002486 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002487}
2488
2489void re_initialize(void)
2490{
Len Brownc98d5d92012-06-04 00:56:40 -04002491 free_all_buffers();
2492 setup_all_buffers();
2493 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002494}
2495
Len Brownc98d5d92012-06-04 00:56:40 -04002496
Len Brown103a8fe2010-10-22 23:53:03 -04002497/*
Len Brownc98d5d92012-06-04 00:56:40 -04002498 * count_cpus()
2499 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002500 */
Len Brownc98d5d92012-06-04 00:56:40 -04002501int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002502{
Len Brownc98d5d92012-06-04 00:56:40 -04002503 if (topo.max_cpu_num < cpu)
2504 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002505
Len Brownc98d5d92012-06-04 00:56:40 -04002506 topo.num_cpus += 1;
2507 return 0;
2508}
2509int mark_cpu_present(int cpu)
2510{
2511 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002512 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002513}
2514
Len Brown562a2d32016-02-26 23:48:05 -05002515/*
2516 * snapshot_proc_interrupts()
2517 *
2518 * read and record summary of /proc/interrupts
2519 *
2520 * return 1 if config change requires a restart, else return 0
2521 */
2522int snapshot_proc_interrupts(void)
2523{
2524 static FILE *fp;
2525 int column, retval;
2526
2527 if (fp == NULL)
2528 fp = fopen_or_die("/proc/interrupts", "r");
2529 else
2530 rewind(fp);
2531
2532 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2533 for (column = 0; column < topo.num_cpus; ++column) {
2534 int cpu_number;
2535
2536 retval = fscanf(fp, " CPU%d", &cpu_number);
2537 if (retval != 1)
2538 break;
2539
2540 if (cpu_number > topo.max_cpu_num) {
2541 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2542 return 1;
2543 }
2544
2545 irq_column_2_cpu[column] = cpu_number;
2546 irqs_per_cpu[cpu_number] = 0;
2547 }
2548
2549 /* read /proc/interrupt count lines and sum up irqs per cpu */
2550 while (1) {
2551 int column;
2552 char buf[64];
2553
2554 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2555 if (retval != 1)
2556 break;
2557
2558 /* read the count per cpu */
2559 for (column = 0; column < topo.num_cpus; ++column) {
2560
2561 int cpu_number, irq_count;
2562
2563 retval = fscanf(fp, " %d", &irq_count);
2564 if (retval != 1)
2565 break;
2566
2567 cpu_number = irq_column_2_cpu[column];
2568 irqs_per_cpu[cpu_number] += irq_count;
2569
2570 }
2571
2572 while (getc(fp) != '\n')
2573 ; /* flush interrupt description */
2574
2575 }
2576 return 0;
2577}
Len Brown27d47352016-02-27 00:37:54 -05002578/*
Len Brownfdf676e2016-02-27 01:28:12 -05002579 * snapshot_gfx_rc6_ms()
2580 *
2581 * record snapshot of
2582 * /sys/class/drm/card0/power/rc6_residency_ms
2583 *
2584 * return 1 if config change requires a restart, else return 0
2585 */
2586int snapshot_gfx_rc6_ms(void)
2587{
2588 FILE *fp;
2589 int retval;
2590
2591 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2592
2593 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2594 if (retval != 1)
2595 err(1, "GFX rc6");
2596
2597 fclose(fp);
2598
2599 return 0;
2600}
2601/*
Len Brown27d47352016-02-27 00:37:54 -05002602 * snapshot_gfx_mhz()
2603 *
2604 * record snapshot of
2605 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2606 *
2607 * return 1 if config change requires a restart, else return 0
2608 */
2609int snapshot_gfx_mhz(void)
2610{
2611 static FILE *fp;
2612 int retval;
2613
2614 if (fp == NULL)
2615 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002616 else {
Len Brown27d47352016-02-27 00:37:54 -05002617 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002618 fflush(fp);
2619 }
Len Brown27d47352016-02-27 00:37:54 -05002620
2621 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2622 if (retval != 1)
2623 err(1, "GFX MHz");
2624
2625 return 0;
2626}
Len Brown562a2d32016-02-26 23:48:05 -05002627
2628/*
Len Brownbe0e54c2018-06-01 12:35:53 -04002629 * snapshot_cpu_lpi()
2630 *
2631 * record snapshot of
2632 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2633 *
2634 * return 1 if config change requires a restart, else return 0
2635 */
2636int snapshot_cpu_lpi_us(void)
2637{
2638 FILE *fp;
2639 int retval;
2640
2641 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2642
2643 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2644 if (retval != 1)
2645 err(1, "CPU LPI");
2646
2647 fclose(fp);
2648
2649 return 0;
2650}
2651/*
2652 * snapshot_sys_lpi()
2653 *
2654 * record snapshot of
2655 * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2656 *
2657 * return 1 if config change requires a restart, else return 0
2658 */
2659int snapshot_sys_lpi_us(void)
2660{
2661 FILE *fp;
2662 int retval;
2663
2664 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2665
2666 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2667 if (retval != 1)
2668 err(1, "SYS LPI");
2669
2670 fclose(fp);
2671
2672 return 0;
2673}
2674/*
Len Brown562a2d32016-02-26 23:48:05 -05002675 * snapshot /proc and /sys files
2676 *
2677 * return 1 if configuration restart needed, else return 0
2678 */
2679int snapshot_proc_sysfs_files(void)
2680{
Len Brown218f0e82017-02-14 22:07:52 -05002681 if (DO_BIC(BIC_IRQ))
2682 if (snapshot_proc_interrupts())
2683 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002684
Len Brown812db3f2017-02-10 00:25:41 -05002685 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002686 snapshot_gfx_rc6_ms();
2687
Len Brown812db3f2017-02-10 00:25:41 -05002688 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002689 snapshot_gfx_mhz();
2690
Len Brownbe0e54c2018-06-01 12:35:53 -04002691 if (DO_BIC(BIC_CPU_LPI))
2692 snapshot_cpu_lpi_us();
2693
2694 if (DO_BIC(BIC_SYS_LPI))
2695 snapshot_sys_lpi_us();
2696
Len Brown562a2d32016-02-26 23:48:05 -05002697 return 0;
2698}
2699
Len Brown8aa2ed02017-07-15 14:57:37 -04002700int exit_requested;
2701
2702static void signal_handler (int signal)
2703{
2704 switch (signal) {
2705 case SIGINT:
2706 exit_requested = 1;
2707 if (debug)
2708 fprintf(stderr, " SIGINT\n");
2709 break;
Len Brown07211962017-07-15 15:51:26 -04002710 case SIGUSR1:
2711 if (debug > 1)
2712 fprintf(stderr, "SIGUSR1\n");
2713 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002714 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002715 /* make sure this manually-invoked interval is at least 1ms long */
2716 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002717}
2718
2719void setup_signal_handler(void)
2720{
2721 struct sigaction sa;
2722
2723 memset(&sa, 0, sizeof(sa));
2724
2725 sa.sa_handler = &signal_handler;
2726
2727 if (sigaction(SIGINT, &sa, NULL) < 0)
2728 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002729 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2730 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002731}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002732
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002733void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002734{
2735 struct timeval select_timeout;
2736 fd_set readfds;
2737 int retval;
2738
2739 FD_ZERO(&readfds);
2740 FD_SET(0, &readfds);
2741
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002742 if (!isatty(fileno(stdin))) {
2743 nanosleep(&interval_ts, NULL);
2744 return;
2745 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002746
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002747 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002748 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2749
2750 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002751 switch (getc(stdin)) {
2752 case 'q':
2753 exit_requested = 1;
2754 break;
2755 }
2756 /* make sure this manually-invoked interval is at least 1ms long */
2757 nanosleep(&one_msec, NULL);
2758 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002759}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002760
Len Brown103a8fe2010-10-22 23:53:03 -04002761void turbostat_loop()
2762{
Len Brownc98d5d92012-06-04 00:56:40 -04002763 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002764 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002765
Len Brown8aa2ed02017-07-15 14:57:37 -04002766 setup_signal_handler();
2767
Len Brown103a8fe2010-10-22 23:53:03 -04002768restart:
Len Browne52966c2012-11-08 22:38:05 -05002769 restarted++;
2770
Len Brown562a2d32016-02-26 23:48:05 -05002771 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002772 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002773 if (retval < -1) {
2774 exit(retval);
2775 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002776 if (restarted > 1) {
2777 exit(retval);
2778 }
Len Brownc98d5d92012-06-04 00:56:40 -04002779 re_initialize();
2780 goto restart;
2781 }
Len Browne52966c2012-11-08 22:38:05 -05002782 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002783 gettimeofday(&tv_even, (struct timezone *)NULL);
2784
2785 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002786 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002787 re_initialize();
2788 goto restart;
2789 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002790 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002791 if (snapshot_proc_sysfs_files())
2792 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002793 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002794 if (retval < -1) {
2795 exit(retval);
2796 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002797 re_initialize();
2798 goto restart;
2799 }
Len Brown103a8fe2010-10-22 23:53:03 -04002800 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002801 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002802 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2803 re_initialize();
2804 goto restart;
2805 }
Len Brownc98d5d92012-06-04 00:56:40 -04002806 compute_average(EVEN_COUNTERS);
2807 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002808 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002809 if (exit_requested)
2810 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002811 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002812 if (snapshot_proc_sysfs_files())
2813 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002814 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002815 if (retval < -1) {
2816 exit(retval);
2817 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002818 re_initialize();
2819 goto restart;
2820 }
Len Brown103a8fe2010-10-22 23:53:03 -04002821 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002822 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002823 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2824 re_initialize();
2825 goto restart;
2826 }
Len Brownc98d5d92012-06-04 00:56:40 -04002827 compute_average(ODD_COUNTERS);
2828 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002829 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002830 if (exit_requested)
2831 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002832 }
2833}
2834
2835void check_dev_msr()
2836{
2837 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002838 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002839
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002840 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2841 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002842 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2843 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002844}
2845
Len Brown98481e72014-08-15 00:36:50 -04002846void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002847{
Len Brown98481e72014-08-15 00:36:50 -04002848 struct __user_cap_header_struct cap_header_data;
2849 cap_user_header_t cap_header = &cap_header_data;
2850 struct __user_cap_data_struct cap_data_data;
2851 cap_user_data_t cap_data = &cap_data_data;
2852 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2853 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002854 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002855
2856 /* check for CAP_SYS_RAWIO */
2857 cap_header->pid = getpid();
2858 cap_header->version = _LINUX_CAPABILITY_VERSION;
2859 if (capget(cap_header, cap_data) < 0)
2860 err(-6, "capget(2) failed");
2861
2862 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2863 do_exit++;
2864 warnx("capget(CAP_SYS_RAWIO) failed,"
2865 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2866 }
2867
2868 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002869 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2870 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002871 do_exit++;
2872 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2873 }
2874
2875 /* if all else fails, thell them to be root */
2876 if (do_exit)
2877 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002878 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002879
2880 if (do_exit)
2881 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002882}
2883
Len Brownd7899442015-01-23 00:12:33 -05002884/*
2885 * NHM adds support for additional MSRs:
2886 *
2887 * MSR_SMI_COUNT 0x00000034
2888 *
Len Brownec0adc52015-11-12 02:42:31 -05002889 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002890 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002891 *
Len Browncf4cbe52017-01-01 13:08:33 -05002892 * MSR_MISC_PWR_MGMT 0x000001aa
2893 *
Len Brownd7899442015-01-23 00:12:33 -05002894 * MSR_PKG_C3_RESIDENCY 0x000003f8
2895 * MSR_PKG_C6_RESIDENCY 0x000003f9
2896 * MSR_CORE_C3_RESIDENCY 0x000003fc
2897 * MSR_CORE_C6_RESIDENCY 0x000003fd
2898 *
Len Brownee7e38e2015-02-09 23:39:45 -05002899 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002900 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002901 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002902 */
Len Brownee7e38e2015-02-09 23:39:45 -05002903int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002904{
Len Brownee7e38e2015-02-09 23:39:45 -05002905 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002906 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002907 int *pkg_cstate_limits;
2908
Len Brown103a8fe2010-10-22 23:53:03 -04002909 if (!genuine_intel)
2910 return 0;
2911
2912 if (family != 6)
2913 return 0;
2914
Len Brown21ed5572015-10-19 22:37:40 -04002915 bclk = discover_bclk(family, model);
2916
Len Brown103a8fe2010-10-22 23:53:03 -04002917 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002918 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2919 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002920 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002921 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2922 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2923 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2924 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002925 pkg_cstate_limits = nhm_pkg_cstate_limits;
2926 break;
Len Brown869ce692016-06-16 23:22:37 -04002927 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2928 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2929 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2930 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002931 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002932 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002933 break;
Len Brown869ce692016-06-16 23:22:37 -04002934 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2935 case INTEL_FAM6_HASWELL_X: /* HSX */
2936 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2937 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2938 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2939 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2940 case INTEL_FAM6_BROADWELL_X: /* BDX */
2941 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2942 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2943 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2944 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2945 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002946 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002947 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002948 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002949 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2950 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002951 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002952 break;
Len Brown869ce692016-06-16 23:22:37 -04002953 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002954 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002955 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002956 pkg_cstate_limits = slv_pkg_cstate_limits;
2957 break;
Len Brown869ce692016-06-16 23:22:37 -04002958 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002959 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002960 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002961 break;
Len Brown869ce692016-06-16 23:22:37 -04002962 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002963 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002964 pkg_cstate_limits = phi_pkg_cstate_limits;
2965 break;
Len Brown869ce692016-06-16 23:22:37 -04002966 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002967 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002968 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002969 pkg_cstate_limits = bxt_pkg_cstate_limits;
2970 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002971 default:
2972 return 0;
2973 }
Len Brown1df2e552017-01-07 23:24:57 -05002974 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002975 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002976
Len Brownec0adc52015-11-12 02:42:31 -05002977 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002978 base_ratio = (msr >> 8) & 0xFF;
2979
2980 base_hz = base_ratio * bclk * 1000000;
2981 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002982 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002983}
Len Brown0f7887c2017-01-12 23:49:18 -05002984/*
Len Brown495c76542017-02-08 02:41:51 -05002985 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002986 *
2987 * MSR_CC6_DEMOTION_POLICY_CONFIG
2988 * MSR_MC6_DEMOTION_POLICY_CONFIG
2989 */
2990
2991int has_slv_msrs(unsigned int family, unsigned int model)
2992{
2993 if (!genuine_intel)
2994 return 0;
2995
2996 switch (model) {
2997 case INTEL_FAM6_ATOM_SILVERMONT1:
2998 case INTEL_FAM6_ATOM_MERRIFIELD:
2999 case INTEL_FAM6_ATOM_MOOREFIELD:
3000 return 1;
3001 }
3002 return 0;
3003}
Len Brown7170a372017-01-27 02:13:27 -05003004int is_dnv(unsigned int family, unsigned int model)
3005{
3006
3007 if (!genuine_intel)
3008 return 0;
3009
3010 switch (model) {
3011 case INTEL_FAM6_ATOM_DENVERTON:
3012 return 1;
3013 }
3014 return 0;
3015}
Len Brownade0eba2017-02-10 01:56:47 -05003016int is_bdx(unsigned int family, unsigned int model)
3017{
3018
3019 if (!genuine_intel)
3020 return 0;
3021
3022 switch (model) {
3023 case INTEL_FAM6_BROADWELL_X:
3024 case INTEL_FAM6_BROADWELL_XEON_D:
3025 return 1;
3026 }
3027 return 0;
3028}
Len Brown34c761972017-01-27 02:36:41 -05003029int is_skx(unsigned int family, unsigned int model)
3030{
3031
3032 if (!genuine_intel)
3033 return 0;
3034
3035 switch (model) {
3036 case INTEL_FAM6_SKYLAKE_X:
3037 return 1;
3038 }
3039 return 0;
3040}
Len Brown0f7887c2017-01-12 23:49:18 -05003041
Len Brown31e07522017-01-31 23:07:49 -05003042int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05003043{
Len Brown0f7887c2017-01-12 23:49:18 -05003044 if (has_slv_msrs(family, model))
3045 return 0;
3046
Len Brownd7899442015-01-23 00:12:33 -05003047 switch (model) {
3048 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04003049 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3050 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3051 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05003052 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05003053 return 0;
3054 default:
3055 return 1;
3056 }
3057}
Len Brown0f7887c2017-01-12 23:49:18 -05003058int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3059{
3060 if (has_slv_msrs(family, model))
3061 return 1;
3062
3063 return 0;
3064}
Len Brown6574a5d2012-09-21 00:01:31 -04003065int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3066{
3067 if (!genuine_intel)
3068 return 0;
3069
3070 if (family != 6)
3071 return 0;
3072
3073 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003074 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3075 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04003076 return 1;
3077 default:
3078 return 0;
3079 }
3080}
Len Brownfcd17212015-03-23 20:29:09 -04003081int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3082{
3083 if (!genuine_intel)
3084 return 0;
3085
3086 if (family != 6)
3087 return 0;
3088
3089 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003090 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003091 return 1;
3092 default:
3093 return 0;
3094 }
3095}
3096
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003097int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3098{
3099 if (!genuine_intel)
3100 return 0;
3101
3102 if (family != 6)
3103 return 0;
3104
3105 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003106 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003107 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003108 return 1;
3109 default:
3110 return 0;
3111 }
3112}
Len Brown31e07522017-01-31 23:07:49 -05003113int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3114{
3115 if (!genuine_intel)
3116 return 0;
3117
3118 if (family != 6)
3119 return 0;
3120
3121 switch (model) {
3122 case INTEL_FAM6_ATOM_GOLDMONT:
3123 case INTEL_FAM6_SKYLAKE_X:
3124 return 1;
3125 default:
3126 return 0;
3127 }
3128}
Len Brown6fb31432015-06-17 16:23:45 -04003129int has_config_tdp(unsigned int family, unsigned int model)
3130{
3131 if (!genuine_intel)
3132 return 0;
3133
3134 if (family != 6)
3135 return 0;
3136
3137 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003138 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3139 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3140 case INTEL_FAM6_HASWELL_X: /* HSX */
3141 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3142 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3143 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3144 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3145 case INTEL_FAM6_BROADWELL_X: /* BDX */
3146 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3147 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3148 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3149 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3150 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3151 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003152
Len Brown869ce692016-06-16 23:22:37 -04003153 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003154 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003155 return 1;
3156 default:
3157 return 0;
3158 }
3159}
3160
Len Brownfcd17212015-03-23 20:29:09 -04003161static void
Colin Ian King1b693172016-03-02 13:50:25 +00003162dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003163{
3164 if (!do_nhm_platform_info)
3165 return;
3166
3167 dump_nhm_platform_info();
3168
3169 if (has_hsw_turbo_ratio_limit(family, model))
3170 dump_hsw_turbo_ratio_limits();
3171
3172 if (has_ivt_turbo_ratio_limit(family, model))
3173 dump_ivt_turbo_ratio_limits();
3174
Len Brown31e07522017-01-31 23:07:49 -05003175 if (has_turbo_ratio_limit(family, model))
3176 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003177
Len Brown0f7887c2017-01-12 23:49:18 -05003178 if (has_atom_turbo_ratio_limit(family, model))
3179 dump_atom_turbo_ratio_limits();
3180
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003181 if (has_knl_turbo_ratio_limit(family, model))
3182 dump_knl_turbo_ratio_limits();
3183
Len Brown6fb31432015-06-17 16:23:45 -04003184 if (has_config_tdp(family, model))
3185 dump_config_tdp();
3186
Len Brownfcd17212015-03-23 20:29:09 -04003187 dump_nhm_cst_cfg();
3188}
3189
Len Brown41618e62017-02-09 18:25:22 -05003190static void
3191dump_sysfs_cstate_config(void)
3192{
3193 char path[64];
3194 char name_buf[16];
3195 char desc[64];
3196 FILE *input;
3197 int state;
3198 char *sp;
3199
3200 if (!DO_BIC(BIC_sysfs))
3201 return;
3202
3203 for (state = 0; state < 10; ++state) {
3204
3205 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3206 base_cpu, state);
3207 input = fopen(path, "r");
3208 if (input == NULL)
3209 continue;
3210 fgets(name_buf, sizeof(name_buf), input);
3211
3212 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3213 sp = strchr(name_buf, '-');
3214 if (!sp)
3215 sp = strchrnul(name_buf, '\n');
3216 *sp = '\0';
3217
3218 fclose(input);
3219
3220 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3221 base_cpu, state);
3222 input = fopen(path, "r");
3223 if (input == NULL)
3224 continue;
3225 fgets(desc, sizeof(desc), input);
3226
3227 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3228 fclose(input);
3229 }
3230}
Len Brown7293fcc2017-02-22 00:11:12 -05003231static void
3232dump_sysfs_pstate_config(void)
3233{
3234 char path[64];
3235 char driver_buf[64];
3236 char governor_buf[64];
3237 FILE *input;
3238 int turbo;
3239
3240 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3241 base_cpu);
3242 input = fopen(path, "r");
3243 if (input == NULL) {
3244 fprintf(stderr, "NSFOD %s\n", path);
3245 return;
3246 }
3247 fgets(driver_buf, sizeof(driver_buf), input);
3248 fclose(input);
3249
3250 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3251 base_cpu);
3252 input = fopen(path, "r");
3253 if (input == NULL) {
3254 fprintf(stderr, "NSFOD %s\n", path);
3255 return;
3256 }
3257 fgets(governor_buf, sizeof(governor_buf), input);
3258 fclose(input);
3259
3260 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3261 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3262
3263 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3264 input = fopen(path, "r");
3265 if (input != NULL) {
3266 fscanf(input, "%d", &turbo);
3267 fprintf(outf, "cpufreq boost: %d\n", turbo);
3268 fclose(input);
3269 }
3270
3271 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3272 input = fopen(path, "r");
3273 if (input != NULL) {
3274 fscanf(input, "%d", &turbo);
3275 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3276 fclose(input);
3277 }
3278}
Len Brown41618e62017-02-09 18:25:22 -05003279
Len Brown6574a5d2012-09-21 00:01:31 -04003280
Len Brown889facb2012-11-08 00:48:57 -05003281/*
3282 * print_epb()
3283 * Decode the ENERGY_PERF_BIAS MSR
3284 */
3285int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3286{
3287 unsigned long long msr;
3288 char *epb_string;
3289 int cpu;
3290
3291 if (!has_epb)
3292 return 0;
3293
3294 cpu = t->cpu_id;
3295
3296 /* EPB is per-package */
3297 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3298 return 0;
3299
3300 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003301 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003302 return -1;
3303 }
3304
3305 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3306 return 0;
3307
Len Browne9be7dd2015-05-26 12:19:37 -04003308 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003309 case ENERGY_PERF_BIAS_PERFORMANCE:
3310 epb_string = "performance";
3311 break;
3312 case ENERGY_PERF_BIAS_NORMAL:
3313 epb_string = "balanced";
3314 break;
3315 case ENERGY_PERF_BIAS_POWERSAVE:
3316 epb_string = "powersave";
3317 break;
3318 default:
3319 epb_string = "custom";
3320 break;
3321 }
Len Brownb7d8c142016-02-13 23:36:17 -05003322 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003323
3324 return 0;
3325}
Len Brown7f5c2582015-12-01 01:36:39 -05003326/*
3327 * print_hwp()
3328 * Decode the MSR_HWP_CAPABILITIES
3329 */
3330int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3331{
3332 unsigned long long msr;
3333 int cpu;
3334
3335 if (!has_hwp)
3336 return 0;
3337
3338 cpu = t->cpu_id;
3339
3340 /* MSR_HWP_CAPABILITIES is per-package */
3341 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3342 return 0;
3343
3344 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003345 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003346 return -1;
3347 }
3348
3349 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3350 return 0;
3351
Len Brownb7d8c142016-02-13 23:36:17 -05003352 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003353 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3354
3355 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3356 if ((msr & (1 << 0)) == 0)
3357 return 0;
3358
3359 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3360 return 0;
3361
Len Brownb7d8c142016-02-13 23:36:17 -05003362 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003363 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003364 cpu, msr,
3365 (unsigned int)HWP_HIGHEST_PERF(msr),
3366 (unsigned int)HWP_GUARANTEED_PERF(msr),
3367 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3368 (unsigned int)HWP_LOWEST_PERF(msr));
3369
3370 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3371 return 0;
3372
Len Brownb7d8c142016-02-13 23:36:17 -05003373 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003374 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003375 cpu, msr,
3376 (unsigned int)(((msr) >> 0) & 0xff),
3377 (unsigned int)(((msr) >> 8) & 0xff),
3378 (unsigned int)(((msr) >> 16) & 0xff),
3379 (unsigned int)(((msr) >> 24) & 0xff),
3380 (unsigned int)(((msr) >> 32) & 0xff3),
3381 (unsigned int)(((msr) >> 42) & 0x1));
3382
3383 if (has_hwp_pkg) {
3384 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3385 return 0;
3386
Len Brownb7d8c142016-02-13 23:36:17 -05003387 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003388 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003389 cpu, msr,
3390 (unsigned int)(((msr) >> 0) & 0xff),
3391 (unsigned int)(((msr) >> 8) & 0xff),
3392 (unsigned int)(((msr) >> 16) & 0xff),
3393 (unsigned int)(((msr) >> 24) & 0xff),
3394 (unsigned int)(((msr) >> 32) & 0xff3));
3395 }
3396 if (has_hwp_notify) {
3397 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3398 return 0;
3399
Len Brownb7d8c142016-02-13 23:36:17 -05003400 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003401 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3402 cpu, msr,
3403 ((msr) & 0x1) ? "EN" : "Dis",
3404 ((msr) & 0x2) ? "EN" : "Dis");
3405 }
3406 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3407 return 0;
3408
Len Brownb7d8c142016-02-13 23:36:17 -05003409 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003410 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3411 cpu, msr,
3412 ((msr) & 0x1) ? "" : "No-",
3413 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003414
3415 return 0;
3416}
3417
Len Brown3a9a9412014-08-15 02:39:52 -04003418/*
3419 * print_perf_limit()
3420 */
3421int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3422{
3423 unsigned long long msr;
3424 int cpu;
3425
3426 cpu = t->cpu_id;
3427
3428 /* per-package */
3429 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3430 return 0;
3431
3432 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003433 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003434 return -1;
3435 }
3436
3437 if (do_core_perf_limit_reasons) {
3438 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003439 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3440 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003441 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003442 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003443 (msr & 1 << 13) ? "Transitions, " : "",
3444 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3445 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3446 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3447 (msr & 1 << 9) ? "CorePwr, " : "",
3448 (msr & 1 << 8) ? "Amps, " : "",
3449 (msr & 1 << 6) ? "VR-Therm, " : "",
3450 (msr & 1 << 5) ? "Auto-HWP, " : "",
3451 (msr & 1 << 4) ? "Graphics, " : "",
3452 (msr & 1 << 2) ? "bit2, " : "",
3453 (msr & 1 << 1) ? "ThermStatus, " : "",
3454 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003455 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 -04003456 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003457 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003458 (msr & 1 << 29) ? "Transitions, " : "",
3459 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3460 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3461 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3462 (msr & 1 << 25) ? "CorePwr, " : "",
3463 (msr & 1 << 24) ? "Amps, " : "",
3464 (msr & 1 << 22) ? "VR-Therm, " : "",
3465 (msr & 1 << 21) ? "Auto-HWP, " : "",
3466 (msr & 1 << 20) ? "Graphics, " : "",
3467 (msr & 1 << 18) ? "bit18, " : "",
3468 (msr & 1 << 17) ? "ThermStatus, " : "",
3469 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003470
3471 }
3472 if (do_gfx_perf_limit_reasons) {
3473 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003474 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3475 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003476 (msr & 1 << 0) ? "PROCHOT, " : "",
3477 (msr & 1 << 1) ? "ThermStatus, " : "",
3478 (msr & 1 << 4) ? "Graphics, " : "",
3479 (msr & 1 << 6) ? "VR-Therm, " : "",
3480 (msr & 1 << 8) ? "Amps, " : "",
3481 (msr & 1 << 9) ? "GFXPwr, " : "",
3482 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3483 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003484 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003485 (msr & 1 << 16) ? "PROCHOT, " : "",
3486 (msr & 1 << 17) ? "ThermStatus, " : "",
3487 (msr & 1 << 20) ? "Graphics, " : "",
3488 (msr & 1 << 22) ? "VR-Therm, " : "",
3489 (msr & 1 << 24) ? "Amps, " : "",
3490 (msr & 1 << 25) ? "GFXPwr, " : "",
3491 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3492 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3493 }
3494 if (do_ring_perf_limit_reasons) {
3495 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003496 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3497 fprintf(outf, " (Active: %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 << 6) ? "VR-Therm, " : "",
3501 (msr & 1 << 8) ? "Amps, " : "",
3502 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3503 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003504 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003505 (msr & 1 << 16) ? "PROCHOT, " : "",
3506 (msr & 1 << 17) ? "ThermStatus, " : "",
3507 (msr & 1 << 22) ? "VR-Therm, " : "",
3508 (msr & 1 << 24) ? "Amps, " : "",
3509 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3510 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3511 }
3512 return 0;
3513}
3514
Len Brown889facb2012-11-08 00:48:57 -05003515#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3516#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3517
Colin Ian King1b693172016-03-02 13:50:25 +00003518double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003519{
3520 unsigned long long msr;
3521
3522 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003523 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003524 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3525
3526 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003527 case INTEL_FAM6_ATOM_SILVERMONT1:
3528 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003529 return 30.0;
3530 default:
3531 return 135.0;
3532 }
3533}
3534
Andrey Semin40ee8e32014-12-05 00:07:00 -05003535/*
3536 * rapl_dram_energy_units_probe()
3537 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3538 */
3539static double
3540rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3541{
3542 /* only called for genuine_intel, family 6 */
3543
3544 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003545 case INTEL_FAM6_HASWELL_X: /* HSX */
3546 case INTEL_FAM6_BROADWELL_X: /* BDX */
3547 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3548 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003549 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003550 return (rapl_dram_energy_units = 15.3 / 1000000);
3551 default:
3552 return (rapl_energy_units);
3553 }
3554}
3555
Len Brown144b44b2013-11-09 00:30:16 -05003556
Len Brown889facb2012-11-08 00:48:57 -05003557/*
3558 * rapl_probe()
3559 *
Len Brown144b44b2013-11-09 00:30:16 -05003560 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003561 */
3562void rapl_probe(unsigned int family, unsigned int model)
3563{
3564 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003565 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003566 double tdp;
3567
3568 if (!genuine_intel)
3569 return;
3570
3571 if (family != 6)
3572 return;
3573
3574 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003575 case INTEL_FAM6_SANDYBRIDGE:
3576 case INTEL_FAM6_IVYBRIDGE:
3577 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3578 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3579 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3580 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3581 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003582 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003583 if (rapl_joules) {
3584 BIC_PRESENT(BIC_Pkg_J);
3585 BIC_PRESENT(BIC_Cor_J);
3586 BIC_PRESENT(BIC_GFX_J);
3587 } else {
3588 BIC_PRESENT(BIC_PkgWatt);
3589 BIC_PRESENT(BIC_CorWatt);
3590 BIC_PRESENT(BIC_GFXWatt);
3591 }
Len Brown889facb2012-11-08 00:48:57 -05003592 break;
Len Brown869ce692016-06-16 23:22:37 -04003593 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003594 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003595 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003596 if (rapl_joules)
3597 BIC_PRESENT(BIC_Pkg_J);
3598 else
3599 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003600 break;
Len Brown869ce692016-06-16 23:22:37 -04003601 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3602 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3603 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3604 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown81824922017-03-04 17:23:07 -05003605 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 -05003606 BIC_PRESENT(BIC_PKG__);
3607 BIC_PRESENT(BIC_RAM__);
3608 if (rapl_joules) {
3609 BIC_PRESENT(BIC_Pkg_J);
3610 BIC_PRESENT(BIC_Cor_J);
3611 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003612 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003613 } else {
3614 BIC_PRESENT(BIC_PkgWatt);
3615 BIC_PRESENT(BIC_CorWatt);
3616 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003617 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003618 }
Len Brown0b2bb692015-03-26 00:50:30 -04003619 break;
Len Brown869ce692016-06-16 23:22:37 -04003620 case INTEL_FAM6_HASWELL_X: /* HSX */
3621 case INTEL_FAM6_BROADWELL_X: /* BDX */
3622 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3623 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3624 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003625 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003626 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 -05003627 BIC_PRESENT(BIC_PKG__);
3628 BIC_PRESENT(BIC_RAM__);
3629 if (rapl_joules) {
3630 BIC_PRESENT(BIC_Pkg_J);
3631 BIC_PRESENT(BIC_RAM_J);
3632 } else {
3633 BIC_PRESENT(BIC_PkgWatt);
3634 BIC_PRESENT(BIC_RAMWatt);
3635 }
Len Browne6f9bb32013-12-03 02:19:19 -05003636 break;
Len Brown869ce692016-06-16 23:22:37 -04003637 case INTEL_FAM6_SANDYBRIDGE_X:
3638 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003639 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 -05003640 BIC_PRESENT(BIC_PKG__);
3641 BIC_PRESENT(BIC_RAM__);
3642 if (rapl_joules) {
3643 BIC_PRESENT(BIC_Pkg_J);
3644 BIC_PRESENT(BIC_Cor_J);
3645 BIC_PRESENT(BIC_RAM_J);
3646 } else {
3647 BIC_PRESENT(BIC_PkgWatt);
3648 BIC_PRESENT(BIC_CorWatt);
3649 BIC_PRESENT(BIC_RAMWatt);
3650 }
Len Brown144b44b2013-11-09 00:30:16 -05003651 break;
Len Brown869ce692016-06-16 23:22:37 -04003652 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3653 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003654 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003655 if (rapl_joules) {
3656 BIC_PRESENT(BIC_Pkg_J);
3657 BIC_PRESENT(BIC_Cor_J);
3658 } else {
3659 BIC_PRESENT(BIC_PkgWatt);
3660 BIC_PRESENT(BIC_CorWatt);
3661 }
Len Brown889facb2012-11-08 00:48:57 -05003662 break;
Len Brown869ce692016-06-16 23:22:37 -04003663 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003664 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 -05003665 BIC_PRESENT(BIC_PKG__);
3666 BIC_PRESENT(BIC_RAM__);
3667 if (rapl_joules) {
3668 BIC_PRESENT(BIC_Pkg_J);
3669 BIC_PRESENT(BIC_Cor_J);
3670 BIC_PRESENT(BIC_RAM_J);
3671 } else {
3672 BIC_PRESENT(BIC_PkgWatt);
3673 BIC_PRESENT(BIC_CorWatt);
3674 BIC_PRESENT(BIC_RAMWatt);
3675 }
Jacob Pan0f644902016-06-16 09:48:22 -07003676 break;
Len Brown889facb2012-11-08 00:48:57 -05003677 default:
3678 return;
3679 }
3680
3681 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003682 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003683 return;
3684
3685 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003686 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003687 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3688 else
3689 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003690
Andrey Semin40ee8e32014-12-05 00:07:00 -05003691 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3692
Len Brown144b44b2013-11-09 00:30:16 -05003693 time_unit = msr >> 16 & 0xF;
3694 if (time_unit == 0)
3695 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003696
Len Brown144b44b2013-11-09 00:30:16 -05003697 rapl_time_units = 1.0 / (1 << (time_unit));
3698
3699 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003700
3701 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003702 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003703 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003704
3705 return;
3706}
3707
Colin Ian King1b693172016-03-02 13:50:25 +00003708void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003709{
3710 if (!genuine_intel)
3711 return;
3712
3713 if (family != 6)
3714 return;
3715
3716 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003717 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3718 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3719 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003720 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003721 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003722 do_core_perf_limit_reasons = 1;
3723 do_ring_perf_limit_reasons = 1;
3724 default:
3725 return;
3726 }
3727}
3728
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003729void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3730{
3731 if (is_skx(family, model) || is_bdx(family, model))
3732 has_automatic_cstate_conversion = 1;
3733}
3734
Len Brown889facb2012-11-08 00:48:57 -05003735int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3736{
3737 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003738 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003739 int cpu;
3740
3741 if (!(do_dts || do_ptm))
3742 return 0;
3743
3744 cpu = t->cpu_id;
3745
3746 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003747 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003748 return 0;
3749
3750 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003751 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003752 return -1;
3753 }
3754
3755 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3756 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3757 return 0;
3758
3759 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003760 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003761 cpu, msr, tcc_activation_temp - dts);
3762
Len Brown889facb2012-11-08 00:48:57 -05003763 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3764 return 0;
3765
3766 dts = (msr >> 16) & 0x7F;
3767 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003768 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003769 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003770 }
3771
3772
Len Brownf4896fa52017-03-04 18:10:45 -05003773 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003774 unsigned int resolution;
3775
3776 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3777 return 0;
3778
3779 dts = (msr >> 16) & 0x7F;
3780 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003781 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003782 cpu, msr, tcc_activation_temp - dts, resolution);
3783
Len Brown889facb2012-11-08 00:48:57 -05003784 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3785 return 0;
3786
3787 dts = (msr >> 16) & 0x7F;
3788 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003789 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003790 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003791 }
3792
3793 return 0;
3794}
Len Brown36229892016-02-26 20:51:02 -05003795
Len Brown889facb2012-11-08 00:48:57 -05003796void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3797{
Len Brownb7d8c142016-02-13 23:36:17 -05003798 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003799 cpu, label,
3800 ((msr >> 15) & 1) ? "EN" : "DIS",
3801 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3802 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3803 (((msr >> 16) & 1) ? "EN" : "DIS"));
3804
3805 return;
3806}
3807
3808int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3809{
3810 unsigned long long msr;
3811 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003812
3813 if (!do_rapl)
3814 return 0;
3815
3816 /* RAPL counters are per package, so print only for 1st thread/package */
3817 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3818 return 0;
3819
3820 cpu = t->cpu_id;
3821 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003822 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003823 return -1;
3824 }
3825
3826 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3827 return -1;
3828
Len Brown96e47152017-01-21 02:26:00 -05003829 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3830 rapl_power_units, rapl_energy_units, rapl_time_units);
3831
Len Brown144b44b2013-11-09 00:30:16 -05003832 if (do_rapl & RAPL_PKG_POWER_INFO) {
3833
Len Brown889facb2012-11-08 00:48:57 -05003834 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3835 return -5;
3836
3837
Len Brownb7d8c142016-02-13 23:36:17 -05003838 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 -05003839 cpu, msr,
3840 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3841 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3842 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3843 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3844
Len Brown144b44b2013-11-09 00:30:16 -05003845 }
3846 if (do_rapl & RAPL_PKG) {
3847
Len Brown889facb2012-11-08 00:48:57 -05003848 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3849 return -9;
3850
Len Brownb7d8c142016-02-13 23:36:17 -05003851 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003852 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003853
3854 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003855 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003856 cpu,
3857 ((msr >> 47) & 1) ? "EN" : "DIS",
3858 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3859 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3860 ((msr >> 48) & 1) ? "EN" : "DIS");
3861 }
3862
Len Brown0b2bb692015-03-26 00:50:30 -04003863 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003864 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3865 return -6;
3866
Len Brownb7d8c142016-02-13 23:36:17 -05003867 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 -05003868 cpu, msr,
3869 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3870 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3871 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3872 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003873 }
3874 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003875 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3876 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003877 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003878 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003879
3880 print_power_limit_msr(cpu, msr, "DRAM Limit");
3881 }
Len Brown144b44b2013-11-09 00:30:16 -05003882 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003883 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3884 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003885
Len Brown96e47152017-01-21 02:26:00 -05003886 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003887 }
Jacob Pan91484942016-06-16 09:48:20 -07003888 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003889 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3890 return -9;
3891 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3892 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3893 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003894 }
3895 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003896 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3897 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003898
Len Brown96e47152017-01-21 02:26:00 -05003899 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003900
Len Brown96e47152017-01-21 02:26:00 -05003901 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3902 return -9;
3903 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3904 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3905 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003906 }
3907 return 0;
3908}
3909
Len Brownd7899442015-01-23 00:12:33 -05003910/*
3911 * SNB adds support for additional MSRs:
3912 *
3913 * MSR_PKG_C7_RESIDENCY 0x000003fa
3914 * MSR_CORE_C7_RESIDENCY 0x000003fe
3915 * MSR_PKG_C2_RESIDENCY 0x0000060d
3916 */
Len Brown103a8fe2010-10-22 23:53:03 -04003917
Len Brownd7899442015-01-23 00:12:33 -05003918int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003919{
3920 if (!genuine_intel)
3921 return 0;
3922
3923 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003924 case INTEL_FAM6_SANDYBRIDGE:
3925 case INTEL_FAM6_SANDYBRIDGE_X:
3926 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3927 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3928 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3929 case INTEL_FAM6_HASWELL_X: /* HSW */
3930 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3931 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3932 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3933 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3934 case INTEL_FAM6_BROADWELL_X: /* BDX */
3935 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3936 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3937 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3938 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3939 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3940 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3941 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003942 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003943 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003944 return 1;
3945 }
3946 return 0;
3947}
3948
Len Brownd7899442015-01-23 00:12:33 -05003949/*
3950 * HSW adds support for additional MSRs:
3951 *
Len Brown5a634262016-04-06 17:15:55 -04003952 * MSR_PKG_C8_RESIDENCY 0x00000630
3953 * MSR_PKG_C9_RESIDENCY 0x00000631
3954 * MSR_PKG_C10_RESIDENCY 0x00000632
3955 *
3956 * MSR_PKGC8_IRTL 0x00000633
3957 * MSR_PKGC9_IRTL 0x00000634
3958 * MSR_PKGC10_IRTL 0x00000635
3959 *
Len Brownd7899442015-01-23 00:12:33 -05003960 */
3961int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003962{
3963 if (!genuine_intel)
3964 return 0;
3965
3966 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003967 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3968 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3969 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3970 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3971 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3972 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3973 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003974 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003975 return 1;
3976 }
3977 return 0;
3978}
3979
Len Brown0b2bb692015-03-26 00:50:30 -04003980/*
3981 * SKL adds support for additional MSRS:
3982 *
3983 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3984 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3985 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3986 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3987 */
3988int has_skl_msrs(unsigned int family, unsigned int model)
3989{
3990 if (!genuine_intel)
3991 return 0;
3992
3993 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003994 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3995 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3996 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3997 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003998 return 1;
3999 }
4000 return 0;
4001}
4002
Len Brown144b44b2013-11-09 00:30:16 -05004003int is_slm(unsigned int family, unsigned int model)
4004{
4005 if (!genuine_intel)
4006 return 0;
4007 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004008 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
4009 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05004010 return 1;
4011 }
4012 return 0;
4013}
4014
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004015int is_knl(unsigned int family, unsigned int model)
4016{
4017 if (!genuine_intel)
4018 return 0;
4019 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004020 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05004021 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004022 return 1;
4023 }
4024 return 0;
4025}
4026
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004027unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4028{
4029 if (is_knl(family, model))
4030 return 1024;
4031 return 1;
4032}
4033
Len Brown144b44b2013-11-09 00:30:16 -05004034#define SLM_BCLK_FREQS 5
4035double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4036
4037double slm_bclk(void)
4038{
4039 unsigned long long msr = 3;
4040 unsigned int i;
4041 double freq;
4042
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004043 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05004044 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05004045
4046 i = msr & 0xf;
4047 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05004048 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01004049 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05004050 }
4051 freq = slm_freq_table[i];
4052
Len Brown96e47152017-01-21 02:26:00 -05004053 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05004054 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05004055
4056 return freq;
4057}
4058
Len Brown103a8fe2010-10-22 23:53:03 -04004059double discover_bclk(unsigned int family, unsigned int model)
4060{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01004061 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04004062 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05004063 else if (is_slm(family, model))
4064 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04004065 else
4066 return 133.33;
4067}
4068
Len Brown889facb2012-11-08 00:48:57 -05004069/*
4070 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4071 * the Thermal Control Circuit (TCC) activates.
4072 * This is usually equal to tjMax.
4073 *
4074 * Older processors do not have this MSR, so there we guess,
4075 * but also allow cmdline over-ride with -T.
4076 *
4077 * Several MSR temperature values are in units of degrees-C
4078 * below this value, including the Digital Thermal Sensor (DTS),
4079 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4080 */
4081int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4082{
4083 unsigned long long msr;
4084 unsigned int target_c_local;
4085 int cpu;
4086
4087 /* tcc_activation_temp is used only for dts or ptm */
4088 if (!(do_dts || do_ptm))
4089 return 0;
4090
4091 /* this is a per-package concept */
4092 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4093 return 0;
4094
4095 cpu = t->cpu_id;
4096 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004097 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004098 return -1;
4099 }
4100
4101 if (tcc_activation_temp_override != 0) {
4102 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004103 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004104 cpu, tcc_activation_temp);
4105 return 0;
4106 }
4107
4108 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004109 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004110 goto guess;
4111
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004112 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004113 goto guess;
4114
Jean Delvare34821242014-05-01 11:40:19 +02004115 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004116
Len Brown96e47152017-01-21 02:26:00 -05004117 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004118 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004119 cpu, msr, target_c_local);
4120
Jean Delvare34821242014-05-01 11:40:19 +02004121 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004122 goto guess;
4123
4124 tcc_activation_temp = target_c_local;
4125
4126 return 0;
4127
4128guess:
4129 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004130 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004131 cpu, tcc_activation_temp);
4132
4133 return 0;
4134}
Len Brown69807a62015-11-21 12:22:47 -05004135
Len Brownaa8d8cc2016-03-11 13:26:03 -05004136void decode_feature_control_msr(void)
4137{
4138 unsigned long long msr;
4139
4140 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4141 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4142 base_cpu, msr,
4143 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4144 msr & (1 << 18) ? "SGX" : "");
4145}
4146
Len Brown69807a62015-11-21 12:22:47 -05004147void decode_misc_enable_msr(void)
4148{
4149 unsigned long long msr;
4150
Len Brownf26b1512017-06-23 20:45:54 -07004151 if (!genuine_intel)
4152 return;
4153
Len Brown69807a62015-11-21 12:22:47 -05004154 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004155 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004156 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004157 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4158 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004159 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004160 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4161 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004162}
4163
Len Brown33148d62017-01-21 01:26:16 -05004164void decode_misc_feature_control(void)
4165{
4166 unsigned long long msr;
4167
4168 if (!has_misc_feature_control)
4169 return;
4170
4171 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4172 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4173 base_cpu, msr,
4174 msr & (0 << 0) ? "No-" : "",
4175 msr & (1 << 0) ? "No-" : "",
4176 msr & (2 << 0) ? "No-" : "",
4177 msr & (3 << 0) ? "No-" : "");
4178}
Len Brownf0057312015-12-03 01:35:36 -05004179/*
4180 * Decode MSR_MISC_PWR_MGMT
4181 *
4182 * Decode the bits according to the Nehalem documentation
4183 * bit[0] seems to continue to have same meaning going forward
4184 * bit[1] less so...
4185 */
4186void decode_misc_pwr_mgmt_msr(void)
4187{
4188 unsigned long long msr;
4189
4190 if (!do_nhm_platform_info)
4191 return;
4192
Len Browncf4cbe52017-01-01 13:08:33 -05004193 if (no_MSR_MISC_PWR_MGMT)
4194 return;
4195
Len Brownf0057312015-12-03 01:35:36 -05004196 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004197 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 -05004198 base_cpu, msr,
4199 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004200 msr & (1 << 1) ? "EN" : "DIS",
4201 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004202}
Len Brown71616c82017-01-07 22:37:48 -05004203/*
4204 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4205 *
4206 * This MSRs are present on Silvermont processors,
4207 * Intel Atom processor E3000 series (Baytrail), and friends.
4208 */
4209void decode_c6_demotion_policy_msr(void)
4210{
4211 unsigned long long msr;
4212
4213 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4214 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4215 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4216
4217 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4218 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4219 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4220}
Len Brown7f5c2582015-12-01 01:36:39 -05004221
Len Brownfcd17212015-03-23 20:29:09 -04004222void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004223{
Len Brown61a87ba2015-11-23 02:30:51 -05004224 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004225 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004226 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004227
4228 eax = ebx = ecx = edx = 0;
4229
Len Brown5aea2f72016-03-13 03:14:35 -04004230 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004231
4232 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4233 genuine_intel = 1;
4234
Len Brown96e47152017-01-21 02:26:00 -05004235 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004236 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004237 (char *)&ebx, (char *)&edx, (char *)&ecx);
4238
Len Brown5aea2f72016-03-13 03:14:35 -04004239 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004240 family = (fms >> 8) & 0xf;
4241 model = (fms >> 4) & 0xf;
4242 stepping = fms & 0xf;
4243 if (family == 6 || family == 0xf)
4244 model += ((fms >> 16) & 0xf) << 4;
4245
Len Brown96e47152017-01-21 02:26:00 -05004246 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004247 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004248 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004249 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004250 ecx & (1 << 0) ? "SSE3" : "-",
4251 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004252 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004253 ecx & (1 << 7) ? "EIST" : "-",
4254 ecx & (1 << 8) ? "TM2" : "-",
4255 edx & (1 << 4) ? "TSC" : "-",
4256 edx & (1 << 5) ? "MSR" : "-",
4257 edx & (1 << 22) ? "ACPI-TM" : "-",
4258 edx & (1 << 29) ? "TM" : "-");
4259 }
Len Brown103a8fe2010-10-22 23:53:03 -04004260
Josh Triplettb2c95d92013-08-20 17:20:18 -07004261 if (!(edx & (1 << 5)))
4262 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004263
4264 /*
4265 * check max extended function levels of CPUID.
4266 * This is needed to check for invariant TSC.
4267 * This check is valid for both Intel and AMD.
4268 */
4269 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004270 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004271
Len Brown61a87ba2015-11-23 02:30:51 -05004272 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004273
Len Brownd7899442015-01-23 00:12:33 -05004274 /*
4275 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4276 * this check is valid for both Intel and AMD
4277 */
Len Brown5aea2f72016-03-13 03:14:35 -04004278 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004279 has_invariant_tsc = edx & (1 << 8);
4280 }
Len Brown103a8fe2010-10-22 23:53:03 -04004281
4282 /*
4283 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4284 * this check is valid for both Intel and AMD
4285 */
4286
Len Brown5aea2f72016-03-13 03:14:35 -04004287 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004288 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004289 if (has_aperf) {
4290 BIC_PRESENT(BIC_Avg_MHz);
4291 BIC_PRESENT(BIC_Busy);
4292 BIC_PRESENT(BIC_Bzy_MHz);
4293 }
Len Brown889facb2012-11-08 00:48:57 -05004294 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004295 if (do_dts)
4296 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004297 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004298 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004299 if (do_ptm)
4300 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004301 has_hwp = eax & (1 << 7);
4302 has_hwp_notify = eax & (1 << 8);
4303 has_hwp_activity_window = eax & (1 << 9);
4304 has_hwp_epp = eax & (1 << 10);
4305 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004306 has_epb = ecx & (1 << 3);
4307
Len Brown96e47152017-01-21 02:26:00 -05004308 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004309 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004310 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4311 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004312 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004313 do_dts ? "" : "No-",
4314 do_ptm ? "" : "No-",
4315 has_hwp ? "" : "No-",
4316 has_hwp_notify ? "" : "No-",
4317 has_hwp_activity_window ? "" : "No-",
4318 has_hwp_epp ? "" : "No-",
4319 has_hwp_pkg ? "" : "No-",
4320 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004321
Len Brown96e47152017-01-21 02:26:00 -05004322 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004323 decode_misc_enable_msr();
4324
Len Brown33148d62017-01-21 01:26:16 -05004325
Len Brown96e47152017-01-21 02:26:00 -05004326 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004327 int has_sgx;
4328
4329 ecx = 0;
4330
4331 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4332
4333 has_sgx = ebx & (1 << 2);
4334 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4335
4336 if (has_sgx)
4337 decode_feature_control_msr();
4338 }
4339
Len Brown61a87ba2015-11-23 02:30:51 -05004340 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004341 unsigned int eax_crystal;
4342 unsigned int ebx_tsc;
4343
4344 /*
4345 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4346 */
4347 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004348 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004349
4350 if (ebx_tsc != 0) {
4351
Len Brown96e47152017-01-21 02:26:00 -05004352 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004353 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004354 eax_crystal, ebx_tsc, crystal_hz);
4355
4356 if (crystal_hz == 0)
4357 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004358 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4359 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4360 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4361 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004362 crystal_hz = 24000000; /* 24.0 MHz */
4363 break;
Len Brown7268d402016-12-01 23:10:39 -05004364 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004365 crystal_hz = 25000000; /* 25.0 MHz */
4366 break;
Len Brown869ce692016-06-16 23:22:37 -04004367 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004368 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004369 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004370 break;
4371 default:
4372 crystal_hz = 0;
4373 }
4374
4375 if (crystal_hz) {
4376 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004377 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004378 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004379 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4380 }
4381 }
4382 }
Len Brown61a87ba2015-11-23 02:30:51 -05004383 if (max_level >= 0x16) {
4384 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4385
4386 /*
4387 * CPUID 16H Base MHz, Max MHz, Bus MHz
4388 */
4389 base_mhz = max_mhz = bus_mhz = edx = 0;
4390
Len Brown5aea2f72016-03-13 03:14:35 -04004391 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004392 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004393 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004394 base_mhz, max_mhz, bus_mhz);
4395 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004396
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004397 if (has_aperf)
4398 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4399
Len Brown812db3f2017-02-10 00:25:41 -05004400 BIC_PRESENT(BIC_IRQ);
4401 BIC_PRESENT(BIC_TSC_MHz);
4402
4403 if (probe_nhm_msrs(family, model)) {
4404 do_nhm_platform_info = 1;
4405 BIC_PRESENT(BIC_CPU_c1);
4406 BIC_PRESENT(BIC_CPU_c3);
4407 BIC_PRESENT(BIC_CPU_c6);
4408 BIC_PRESENT(BIC_SMI);
4409 }
Len Brownd7899442015-01-23 00:12:33 -05004410 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004411
4412 if (do_snb_cstates)
4413 BIC_PRESENT(BIC_CPU_c7);
4414
Len Brown5a634262016-04-06 17:15:55 -04004415 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004416 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4417 BIC_PRESENT(BIC_Pkgpc2);
4418 if (pkg_cstate_limit >= PCL__3)
4419 BIC_PRESENT(BIC_Pkgpc3);
4420 if (pkg_cstate_limit >= PCL__6)
4421 BIC_PRESENT(BIC_Pkgpc6);
4422 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4423 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004424 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004425 BIC_NOT_PRESENT(BIC_Pkgpc2);
4426 BIC_NOT_PRESENT(BIC_Pkgpc3);
4427 BIC_PRESENT(BIC_Pkgpc6);
4428 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004429 BIC_PRESENT(BIC_Mod_c6);
4430 use_c1_residency_msr = 1;
4431 }
Len Brown7170a372017-01-27 02:13:27 -05004432 if (is_dnv(family, model)) {
4433 BIC_PRESENT(BIC_CPU_c1);
4434 BIC_NOT_PRESENT(BIC_CPU_c3);
4435 BIC_NOT_PRESENT(BIC_Pkgpc3);
4436 BIC_NOT_PRESENT(BIC_CPU_c7);
4437 BIC_NOT_PRESENT(BIC_Pkgpc7);
4438 use_c1_residency_msr = 1;
4439 }
Len Brown34c761972017-01-27 02:36:41 -05004440 if (is_skx(family, model)) {
4441 BIC_NOT_PRESENT(BIC_CPU_c3);
4442 BIC_NOT_PRESENT(BIC_Pkgpc3);
4443 BIC_NOT_PRESENT(BIC_CPU_c7);
4444 BIC_NOT_PRESENT(BIC_Pkgpc7);
4445 }
Len Brownade0eba2017-02-10 01:56:47 -05004446 if (is_bdx(family, model)) {
4447 BIC_NOT_PRESENT(BIC_CPU_c7);
4448 BIC_NOT_PRESENT(BIC_Pkgpc7);
4449 }
Len Brown0f47c082017-01-27 00:50:45 -05004450 if (has_hsw_msrs(family, model)) {
4451 BIC_PRESENT(BIC_Pkgpc8);
4452 BIC_PRESENT(BIC_Pkgpc9);
4453 BIC_PRESENT(BIC_Pkgpc10);
4454 }
Len Brown5a634262016-04-06 17:15:55 -04004455 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004456 if (has_skl_msrs(family, model)) {
4457 BIC_PRESENT(BIC_Totl_c0);
4458 BIC_PRESENT(BIC_Any_c0);
4459 BIC_PRESENT(BIC_GFX_c0);
4460 BIC_PRESENT(BIC_CPUGFX);
4461 }
Len Brown144b44b2013-11-09 00:30:16 -05004462 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004463 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004464
Len Brown96e47152017-01-21 02:26:00 -05004465 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004466 decode_misc_pwr_mgmt_msr();
4467
Len Brown96e47152017-01-21 02:26:00 -05004468 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004469 decode_c6_demotion_policy_msr();
4470
Len Brown889facb2012-11-08 00:48:57 -05004471 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004472 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004473 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004474
Len Brown96e47152017-01-21 02:26:00 -05004475 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004476 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004477
Len Brown41618e62017-02-09 18:25:22 -05004478 if (!quiet)
4479 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004480 if (!quiet)
4481 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004482
Len Browna2b7b742015-09-26 00:12:38 -04004483 if (has_skl_msrs(family, model))
4484 calculate_tsc_tweak();
4485
Len Brown812db3f2017-02-10 00:25:41 -05004486 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4487 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004488
Len Brown812db3f2017-02-10 00:25:41 -05004489 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4490 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004491
Len Brownbe0e54c2018-06-01 12:35:53 -04004492 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4493 BIC_PRESENT(BIC_CPU_LPI);
4494 else
4495 BIC_NOT_PRESENT(BIC_CPU_LPI);
4496
4497 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4498 BIC_PRESENT(BIC_SYS_LPI);
4499 else
4500 BIC_NOT_PRESENT(BIC_SYS_LPI);
4501
Len Brown96e47152017-01-21 02:26:00 -05004502 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004503 decode_misc_feature_control();
4504
Len Brown889facb2012-11-08 00:48:57 -05004505 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004506}
4507
Len Brown103a8fe2010-10-22 23:53:03 -04004508
4509/*
4510 * in /dev/cpu/ return success for names that are numbers
4511 * ie. filter out ".", "..", "microcode".
4512 */
4513int dir_filter(const struct dirent *dirp)
4514{
4515 if (isdigit(dirp->d_name[0]))
4516 return 1;
4517 else
4518 return 0;
4519}
4520
4521int open_dev_cpu_msr(int dummy1)
4522{
4523 return 0;
4524}
4525
Len Brownc98d5d92012-06-04 00:56:40 -04004526void topology_probe()
4527{
4528 int i;
4529 int max_core_id = 0;
4530 int max_package_id = 0;
4531 int max_siblings = 0;
4532 struct cpu_topology {
4533 int core_id;
4534 int physical_package_id;
4535 } *cpus;
4536
4537 /* Initialize num_cpus, max_cpu_num */
4538 topo.num_cpus = 0;
4539 topo.max_cpu_num = 0;
4540 for_all_proc_cpus(count_cpus);
4541 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004542 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004543
Len Brownd8af6f52015-02-10 01:56:38 -05004544 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004545 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004546
4547 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004548 if (cpus == NULL)
4549 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004550
4551 /*
4552 * Allocate and initialize cpu_present_set
4553 */
4554 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004555 if (cpu_present_set == NULL)
4556 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004557 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4558 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4559 for_all_proc_cpus(mark_cpu_present);
4560
4561 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004562 * Validate that all cpus in cpu_subset are also in cpu_present_set
4563 */
4564 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4565 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4566 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4567 err(1, "cpu%d not present", i);
4568 }
4569
4570 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004571 * Allocate and initialize cpu_affinity_set
4572 */
4573 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004574 if (cpu_affinity_set == NULL)
4575 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004576 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4577 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4578
4579
4580 /*
4581 * For online cpus
4582 * find max_core_id, max_package_id
4583 */
4584 for (i = 0; i <= topo.max_cpu_num; ++i) {
4585 int siblings;
4586
4587 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004588 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004589 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004590 continue;
4591 }
4592 cpus[i].core_id = get_core_id(i);
4593 if (cpus[i].core_id > max_core_id)
4594 max_core_id = cpus[i].core_id;
4595
4596 cpus[i].physical_package_id = get_physical_package_id(i);
4597 if (cpus[i].physical_package_id > max_package_id)
4598 max_package_id = cpus[i].physical_package_id;
4599
4600 siblings = get_num_ht_siblings(i);
4601 if (siblings > max_siblings)
4602 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004603 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004604 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004605 i, cpus[i].physical_package_id, cpus[i].core_id);
4606 }
4607 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004608 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004609 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004610 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004611 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004612 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004613
4614 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004615 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004616 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004617 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004618 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004619 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004620
4621 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004622 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004623 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004624
4625 free(cpus);
4626}
4627
4628void
4629allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4630{
4631 int i;
4632
4633 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004634 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004635 if (*t == NULL)
4636 goto error;
4637
4638 for (i = 0; i < topo.num_threads_per_core *
4639 topo.num_cores_per_pkg * topo.num_packages; i++)
4640 (*t)[i].cpu_id = -1;
4641
4642 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004643 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004644 if (*c == NULL)
4645 goto error;
4646
4647 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4648 (*c)[i].core_id = -1;
4649
Len Brown678a3bd2017-02-09 22:22:13 -05004650 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004651 if (*p == NULL)
4652 goto error;
4653
4654 for (i = 0; i < topo.num_packages; i++)
4655 (*p)[i].package_id = i;
4656
4657 return;
4658error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004659 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004660}
4661/*
4662 * init_counter()
4663 *
4664 * set cpu_id, core_num, pkg_num
4665 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4666 *
4667 * increment topo.num_cores when 1st core in pkg seen
4668 */
4669void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4670 struct pkg_data *pkg_base, int thread_num, int core_num,
4671 int pkg_num, int cpu_id)
4672{
4673 struct thread_data *t;
4674 struct core_data *c;
4675 struct pkg_data *p;
4676
4677 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4678 c = GET_CORE(core_base, core_num, pkg_num);
4679 p = GET_PKG(pkg_base, pkg_num);
4680
4681 t->cpu_id = cpu_id;
4682 if (thread_num == 0) {
4683 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4684 if (cpu_is_first_core_in_package(cpu_id))
4685 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4686 }
4687
4688 c->core_id = core_num;
4689 p->package_id = pkg_num;
4690}
4691
4692
4693int initialize_counters(int cpu_id)
4694{
4695 int my_thread_id, my_core_id, my_package_id;
4696
4697 my_package_id = get_physical_package_id(cpu_id);
4698 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004699 my_thread_id = get_cpu_position_in_core(cpu_id);
4700 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004701 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004702
4703 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4704 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4705 return 0;
4706}
4707
4708void allocate_output_buffer()
4709{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004710 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004711 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004712 if (outp == NULL)
4713 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004714}
Len Brown36229892016-02-26 20:51:02 -05004715void allocate_fd_percpu(void)
4716{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004717 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004718 if (fd_percpu == NULL)
4719 err(-1, "calloc fd_percpu");
4720}
Len Brown562a2d32016-02-26 23:48:05 -05004721void allocate_irq_buffers(void)
4722{
4723 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4724 if (irq_column_2_cpu == NULL)
4725 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004726
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004727 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004728 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004729 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004730}
Len Brownc98d5d92012-06-04 00:56:40 -04004731void setup_all_buffers(void)
4732{
4733 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004734 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004735 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004736 allocate_counters(&thread_even, &core_even, &package_even);
4737 allocate_counters(&thread_odd, &core_odd, &package_odd);
4738 allocate_output_buffer();
4739 for_all_proc_cpus(initialize_counters);
4740}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004741
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004742void set_base_cpu(void)
4743{
4744 base_cpu = sched_getcpu();
4745 if (base_cpu < 0)
4746 err(-ENODEV, "No valid cpus found");
4747
4748 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004749 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004750}
4751
Len Brown103a8fe2010-10-22 23:53:03 -04004752void turbostat_init()
4753{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004754 setup_all_buffers();
4755 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004756 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004757 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004758 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004759
Len Brown103a8fe2010-10-22 23:53:03 -04004760
Len Brown96e47152017-01-21 02:26:00 -05004761 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004762 for_all_cpus(print_hwp, ODD_COUNTERS);
4763
Len Brown96e47152017-01-21 02:26:00 -05004764 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004765 for_all_cpus(print_epb, ODD_COUNTERS);
4766
Len Brown96e47152017-01-21 02:26:00 -05004767 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004768 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4769
Len Brown96e47152017-01-21 02:26:00 -05004770 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004771 for_all_cpus(print_rapl, ODD_COUNTERS);
4772
4773 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4774
Len Brown96e47152017-01-21 02:26:00 -05004775 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004776 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004777
Len Brown96e47152017-01-21 02:26:00 -05004778 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004779 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004780}
4781
4782int fork_it(char **argv)
4783{
Len Brown103a8fe2010-10-22 23:53:03 -04004784 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004785 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004786
Len Brown218f0e82017-02-14 22:07:52 -05004787 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004788 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4789 if (status)
4790 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004791 /* clear affinity side-effect of get_counters() */
4792 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004793 gettimeofday(&tv_even, (struct timezone *)NULL);
4794
4795 child_pid = fork();
4796 if (!child_pid) {
4797 /* child */
4798 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004799 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004800 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004801
4802 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004803 if (child_pid == -1)
4804 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004805
4806 signal(SIGINT, SIG_IGN);
4807 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004808 if (waitpid(child_pid, &status, 0) == -1)
4809 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004810 }
Len Brownc98d5d92012-06-04 00:56:40 -04004811 /*
4812 * n.b. fork_it() does not check for errors from for_all_cpus()
4813 * because re-starting is problematic when forking
4814 */
Len Brown218f0e82017-02-14 22:07:52 -05004815 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004816 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004817 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004818 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004819 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4820 fprintf(outf, "%s: Counter reset detected\n", progname);
4821 else {
4822 compute_average(EVEN_COUNTERS);
4823 format_all_counters(EVEN_COUNTERS);
4824 }
Len Brown103a8fe2010-10-22 23:53:03 -04004825
Len Brownb7d8c142016-02-13 23:36:17 -05004826 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4827
4828 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004829
Len Brownd91bb172012-11-01 00:08:19 -04004830 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004831}
4832
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004833int get_and_dump_counters(void)
4834{
4835 int status;
4836
Len Brown218f0e82017-02-14 22:07:52 -05004837 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004838 status = for_all_cpus(get_counters, ODD_COUNTERS);
4839 if (status)
4840 return status;
4841
4842 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4843 if (status)
4844 return status;
4845
Len Brownb7d8c142016-02-13 23:36:17 -05004846 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004847
4848 return status;
4849}
4850
Len Brownd8af6f52015-02-10 01:56:38 -05004851void print_version() {
Len Brownf7d44a82017-05-27 21:24:58 -07004852 fprintf(outf, "turbostat version 17.06.23"
Len Brownd8af6f52015-02-10 01:56:38 -05004853 " - Len Brown <lenb@kernel.org>\n");
4854}
4855
Len Brown495c76542017-02-08 02:41:51 -05004856int add_counter(unsigned int msr_num, char *path, char *name,
4857 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004858 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004859{
4860 struct msr_counter *msrp;
4861
4862 msrp = calloc(1, sizeof(struct msr_counter));
4863 if (msrp == NULL) {
4864 perror("calloc");
4865 exit(1);
4866 }
4867
4868 msrp->msr_num = msr_num;
4869 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004870 if (path)
4871 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004872 msrp->width = width;
4873 msrp->type = type;
4874 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004875 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004876
4877 switch (scope) {
4878
4879 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004880 msrp->next = sys.tp;
4881 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004882 sys.added_thread_counters++;
Len Brown0748eaf2018-06-01 12:38:29 -04004883 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
Len Brown678a3bd2017-02-09 22:22:13 -05004884 fprintf(stderr, "exceeded max %d added thread counters\n",
4885 MAX_ADDED_COUNTERS);
4886 exit(-1);
4887 }
Len Brown388e9c82016-12-22 23:57:55 -05004888 break;
4889
4890 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004891 msrp->next = sys.cp;
4892 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004893 sys.added_core_counters++;
4894 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4895 fprintf(stderr, "exceeded max %d added core counters\n",
4896 MAX_ADDED_COUNTERS);
4897 exit(-1);
4898 }
Len Brown388e9c82016-12-22 23:57:55 -05004899 break;
4900
4901 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004902 msrp->next = sys.pp;
4903 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004904 sys.added_package_counters++;
4905 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4906 fprintf(stderr, "exceeded max %d added package counters\n",
4907 MAX_ADDED_COUNTERS);
4908 exit(-1);
4909 }
Len Brown388e9c82016-12-22 23:57:55 -05004910 break;
4911 }
4912
4913 return 0;
4914}
4915
4916void parse_add_command(char *add_command)
4917{
4918 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004919 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004920 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004921 int width = 64;
4922 int fail = 0;
4923 enum counter_scope scope = SCOPE_CPU;
4924 enum counter_type type = COUNTER_CYCLES;
4925 enum counter_format format = FORMAT_DELTA;
4926
4927 while (add_command) {
4928
4929 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4930 goto next;
4931
4932 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4933 goto next;
4934
Len Brown495c76542017-02-08 02:41:51 -05004935 if (*add_command == '/') {
4936 path = add_command;
4937 goto next;
4938 }
4939
Len Brown388e9c82016-12-22 23:57:55 -05004940 if (sscanf(add_command, "u%d", &width) == 1) {
4941 if ((width == 32) || (width == 64))
4942 goto next;
4943 width = 64;
4944 }
4945 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4946 scope = SCOPE_CPU;
4947 goto next;
4948 }
4949 if (!strncmp(add_command, "core", strlen("core"))) {
4950 scope = SCOPE_CORE;
4951 goto next;
4952 }
4953 if (!strncmp(add_command, "package", strlen("package"))) {
4954 scope = SCOPE_PACKAGE;
4955 goto next;
4956 }
4957 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4958 type = COUNTER_CYCLES;
4959 goto next;
4960 }
4961 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4962 type = COUNTER_SECONDS;
4963 goto next;
4964 }
Len Brown41618e62017-02-09 18:25:22 -05004965 if (!strncmp(add_command, "usec", strlen("usec"))) {
4966 type = COUNTER_USEC;
4967 goto next;
4968 }
Len Brown388e9c82016-12-22 23:57:55 -05004969 if (!strncmp(add_command, "raw", strlen("raw"))) {
4970 format = FORMAT_RAW;
4971 goto next;
4972 }
4973 if (!strncmp(add_command, "delta", strlen("delta"))) {
4974 format = FORMAT_DELTA;
4975 goto next;
4976 }
4977 if (!strncmp(add_command, "percent", strlen("percent"))) {
4978 format = FORMAT_PERCENT;
4979 goto next;
4980 }
4981
4982 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4983 char *eos;
4984
4985 eos = strchr(name_buffer, ',');
4986 if (eos)
4987 *eos = '\0';
4988 goto next;
4989 }
4990
4991next:
4992 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004993 if (add_command) {
4994 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004995 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004996 }
Len Brown388e9c82016-12-22 23:57:55 -05004997
4998 }
Len Brown495c76542017-02-08 02:41:51 -05004999 if ((msr_num == 0) && (path == NULL)) {
5000 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05005001 fail++;
5002 }
5003
5004 /* generate default column header */
5005 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05005006 if (width == 32)
5007 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5008 else
5009 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05005010 }
5011
Len Brown41618e62017-02-09 18:25:22 -05005012 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05005013 fail++;
5014
5015 if (fail) {
5016 help();
5017 exit(1);
5018 }
5019}
Len Brown41618e62017-02-09 18:25:22 -05005020
Len Browndd778a52017-02-21 23:21:13 -05005021int is_deferred_skip(char *name)
5022{
5023 int i;
5024
5025 for (i = 0; i < deferred_skip_index; ++i)
5026 if (!strcmp(name, deferred_skip_names[i]))
5027 return 1;
5028 return 0;
5029}
5030
Len Brown41618e62017-02-09 18:25:22 -05005031void probe_sysfs(void)
5032{
5033 char path[64];
5034 char name_buf[16];
5035 FILE *input;
5036 int state;
5037 char *sp;
5038
5039 if (!DO_BIC(BIC_sysfs))
5040 return;
5041
Len Brown0748eaf2018-06-01 12:38:29 -04005042 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005043
5044 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5045 base_cpu, state);
5046 input = fopen(path, "r");
5047 if (input == NULL)
5048 continue;
5049 fgets(name_buf, sizeof(name_buf), input);
5050
5051 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5052 sp = strchr(name_buf, '-');
5053 if (!sp)
5054 sp = strchrnul(name_buf, '\n');
5055 *sp = '%';
5056 *(sp + 1) = '\0';
5057
5058 fclose(input);
5059
5060 sprintf(path, "cpuidle/state%d/time", state);
5061
Len Browndd778a52017-02-21 23:21:13 -05005062 if (is_deferred_skip(name_buf))
5063 continue;
5064
Len Brown41618e62017-02-09 18:25:22 -05005065 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
5066 FORMAT_PERCENT, SYSFS_PERCPU);
5067 }
5068
Len Brown0748eaf2018-06-01 12:38:29 -04005069 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005070
5071 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5072 base_cpu, state);
5073 input = fopen(path, "r");
5074 if (input == NULL)
5075 continue;
5076 fgets(name_buf, sizeof(name_buf), input);
5077 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5078 sp = strchr(name_buf, '-');
5079 if (!sp)
5080 sp = strchrnul(name_buf, '\n');
5081 *sp = '\0';
5082 fclose(input);
5083
5084 sprintf(path, "cpuidle/state%d/usage", state);
5085
Len Browndd778a52017-02-21 23:21:13 -05005086 if (is_deferred_skip(name_buf))
5087 continue;
5088
Len Brown41618e62017-02-09 18:25:22 -05005089 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
5090 FORMAT_DELTA, SYSFS_PERCPU);
5091 }
5092
5093}
5094
Len Brown1ef7d212017-02-10 23:54:15 -05005095
5096/*
5097 * parse cpuset with following syntax
5098 * 1,2,4..6,8-10 and set bits in cpu_subset
5099 */
5100void parse_cpu_command(char *optarg)
5101{
5102 unsigned int start, end;
5103 char *next;
5104
Len Brown4e4e1e72017-02-21 22:33:42 -05005105 if (!strcmp(optarg, "core")) {
5106 if (cpu_subset)
5107 goto error;
5108 show_core_only++;
5109 return;
5110 }
5111 if (!strcmp(optarg, "package")) {
5112 if (cpu_subset)
5113 goto error;
5114 show_pkg_only++;
5115 return;
5116 }
5117 if (show_core_only || show_pkg_only)
5118 goto error;
5119
Len Brown1ef7d212017-02-10 23:54:15 -05005120 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5121 if (cpu_subset == NULL)
5122 err(3, "CPU_ALLOC");
5123 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5124
5125 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5126
5127 next = optarg;
5128
5129 while (next && *next) {
5130
5131 if (*next == '-') /* no negative cpu numbers */
5132 goto error;
5133
5134 start = strtoul(next, &next, 10);
5135
5136 if (start >= CPU_SUBSET_MAXCPUS)
5137 goto error;
5138 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5139
5140 if (*next == '\0')
5141 break;
5142
5143 if (*next == ',') {
5144 next += 1;
5145 continue;
5146 }
5147
5148 if (*next == '-') {
5149 next += 1; /* start range */
5150 } else if (*next == '.') {
5151 next += 1;
5152 if (*next == '.')
5153 next += 1; /* start range */
5154 else
5155 goto error;
5156 }
5157
5158 end = strtoul(next, &next, 10);
5159 if (end <= start)
5160 goto error;
5161
5162 while (++start <= end) {
5163 if (start >= CPU_SUBSET_MAXCPUS)
5164 goto error;
5165 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5166 }
5167
5168 if (*next == ',')
5169 next += 1;
5170 else if (*next != '\0')
5171 goto error;
5172 }
5173
5174 return;
5175
5176error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005177 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5178 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005179 exit(-1);
5180}
5181
Len Brown812db3f2017-02-10 00:25:41 -05005182
Len Brown103a8fe2010-10-22 23:53:03 -04005183void cmdline(int argc, char **argv)
5184{
5185 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005186 int option_index = 0;
5187 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005188 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005189 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005190 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005191 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005192 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005193 {"interval", required_argument, 0, 'i'},
5194 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005195 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005196 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005197 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005198 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005199 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005200 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005201 {"Summary", no_argument, 0, 'S'},
5202 {"TCC", required_argument, 0, 'T'},
5203 {"version", no_argument, 0, 'v' },
5204 {0, 0, 0, 0 }
5205 };
Len Brown103a8fe2010-10-22 23:53:03 -04005206
5207 progname = argv[0];
5208
Len Brown3f44a5c2017-10-17 15:42:56 -04005209 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jo:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005210 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005211 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005212 case 'a':
5213 parse_add_command(optarg);
5214 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005215 case 'c':
5216 parse_cpu_command(optarg);
5217 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005218 case 'D':
5219 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005220 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005221 case 'e':
5222 /* --enable specified counter */
5223 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5224 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005225 case 'd':
5226 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005227 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005228 break;
Len Brown812db3f2017-02-10 00:25:41 -05005229 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005230 /*
5231 * --hide: do not show those specified
5232 * multiple invocations simply clear more bits in enabled mask
5233 */
5234 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005235 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005236 case 'h':
5237 default:
5238 help();
5239 exit(1);
5240 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005241 {
5242 double interval = strtod(optarg, NULL);
5243
5244 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005245 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005246 interval);
5247 exit(2);
5248 }
5249
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005250 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005251 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005252 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005253 }
Len Brown889facb2012-11-08 00:48:57 -05005254 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005255 case 'J':
5256 rapl_joules++;
5257 break;
Len Brownc8ade362017-02-15 17:15:11 -05005258 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005259 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005260 list_header_only++;
5261 quiet++;
5262 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005263 case 'o':
5264 outf = fopen_or_die(optarg, "w");
5265 break;
Len Brown96e47152017-01-21 02:26:00 -05005266 case 'q':
5267 quiet = 1;
5268 break;
Len Brown812db3f2017-02-10 00:25:41 -05005269 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005270 /*
5271 * --show: show only those specified
5272 * The 1st invocation will clear and replace the enabled mask
5273 * subsequent invocations can add to it.
5274 */
5275 if (shown == 0)
5276 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5277 else
5278 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5279 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005280 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005281 case 'S':
5282 summary_only++;
5283 break;
5284 case 'T':
5285 tcc_activation_temp_override = atoi(optarg);
5286 break;
5287 case 'v':
5288 print_version();
5289 exit(0);
5290 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005291 }
5292 }
5293}
5294
5295int main(int argc, char **argv)
5296{
Len Brownb7d8c142016-02-13 23:36:17 -05005297 outf = stderr;
5298
Len Brown103a8fe2010-10-22 23:53:03 -04005299 cmdline(argc, argv);
5300
Len Brown96e47152017-01-21 02:26:00 -05005301 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005302 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005303
Len Brown41618e62017-02-09 18:25:22 -05005304 probe_sysfs();
5305
Len Brown103a8fe2010-10-22 23:53:03 -04005306 turbostat_init();
5307
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005308 /* dump counters and exit */
5309 if (dump_only)
5310 return get_and_dump_counters();
5311
Len Brownc8ade362017-02-15 17:15:11 -05005312 /* list header and exit */
5313 if (list_header_only) {
5314 print_header(",");
5315 flush_output_stdout();
5316 return 0;
5317 }
5318
Len Brown103a8fe2010-10-22 23:53:03 -04005319 /*
5320 * if any params left, it must be a command to fork
5321 */
5322 if (argc - optind)
5323 return fork_it(argv + optind);
5324 else
5325 turbostat_loop();
5326
5327 return 0;
5328}