blob: 14530de01e9616732a237b701fc1ebff4f69eb29 [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 Brown869ce69e2016-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 Brown27d47352016-02-27 00:37:54 -050087unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050088unsigned int tcc_activation_temp;
89unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050090double rapl_power_units, rapl_time_units;
91double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050092double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040093unsigned int do_core_perf_limit_reasons;
Artem Bityutskiyac980e12017-09-05 15:14:08 +030094unsigned int has_automatic_cstate_conversion;
Len Brown3a9a9412014-08-15 02:39:52 -040095unsigned int do_gfx_perf_limit_reasons;
96unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040097unsigned int crystal_hz;
98unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040099int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400100double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500101unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
102 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
103unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
104unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
105unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
106unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500107unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500108
Len Browne6f9bb32013-12-03 02:19:19 -0500109#define RAPL_PKG (1 << 0)
110 /* 0x610 MSR_PKG_POWER_LIMIT */
111 /* 0x611 MSR_PKG_ENERGY_STATUS */
112#define RAPL_PKG_PERF_STATUS (1 << 1)
113 /* 0x613 MSR_PKG_PERF_STATUS */
114#define RAPL_PKG_POWER_INFO (1 << 2)
115 /* 0x614 MSR_PKG_POWER_INFO */
116
117#define RAPL_DRAM (1 << 3)
118 /* 0x618 MSR_DRAM_POWER_LIMIT */
119 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500120#define RAPL_DRAM_PERF_STATUS (1 << 4)
121 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400122#define RAPL_DRAM_POWER_INFO (1 << 5)
123 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500124
Jacob Pan91484942016-06-16 09:48:20 -0700125#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500126 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400127#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500128 /* 0x63a MSR_PP0_POLICY */
129
Len Brown0b2bb692015-03-26 00:50:30 -0400130#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500131 /* 0x640 MSR_PP1_POWER_LIMIT */
132 /* 0x641 MSR_PP1_ENERGY_STATUS */
133 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700134
135#define RAPL_CORES_ENERGY_STATUS (1 << 9)
136 /* 0x639 MSR_PP0_ENERGY_STATUS */
137#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500138#define TJMAX_DEFAULT 100
139
140#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400141
Len Brown388e9c82016-12-22 23:57:55 -0500142/*
143 * buffer size used by sscanf() for added column names
144 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
145 */
146#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500147#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500148
Len Brown103a8fe2010-10-22 23:53:03 -0400149int backwards_count;
150char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400151
Len Brown1ef7d212017-02-10 23:54:15 -0500152#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
153cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
154size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown678a3bd2017-02-09 22:22:13 -0500155#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400156
Len Brownc98d5d92012-06-04 00:56:40 -0400157struct thread_data {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700158 struct timeval tv_begin;
159 struct timeval tv_end;
Len Brownc98d5d92012-06-04 00:56:40 -0400160 unsigned long long tsc;
161 unsigned long long aperf;
162 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500163 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500164 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500165 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400166 unsigned int cpu_id;
167 unsigned int flags;
168#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
169#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500170 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400171} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400172
Len Brownc98d5d92012-06-04 00:56:40 -0400173struct core_data {
174 unsigned long long c3;
175 unsigned long long c6;
176 unsigned long long c7;
Len Brown0539ba112017-02-10 00:27:20 -0500177 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500178 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400179 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500180 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400181} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400182
Len Brownc98d5d92012-06-04 00:56:40 -0400183struct pkg_data {
184 unsigned long long pc2;
185 unsigned long long pc3;
186 unsigned long long pc6;
187 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800188 unsigned long long pc8;
189 unsigned long long pc9;
190 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400191 unsigned long long pkg_wtd_core_c0;
192 unsigned long long pkg_any_core_c0;
193 unsigned long long pkg_any_gfxe_c0;
194 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400195 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500196 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400197 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500198 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
199 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
200 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
201 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
202 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
203 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
204 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500205 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400206} *package_even, *package_odd;
207
208#define ODD_COUNTERS thread_odd, core_odd, package_odd
209#define EVEN_COUNTERS thread_even, core_even, package_even
210
211#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
212 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
213 topo.num_threads_per_core + \
214 (core_no) * topo.num_threads_per_core + (thread_no))
215#define GET_CORE(core_base, core_no, pkg_no) \
216 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
217#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
218
Len Brown388e9c82016-12-22 23:57:55 -0500219enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500220enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500221enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
222
223struct msr_counter {
224 unsigned int msr_num;
225 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500226 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500227 unsigned int width;
228 enum counter_type type;
229 enum counter_format format;
230 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500231 unsigned int flags;
232#define FLAGS_HIDE (1 << 0)
233#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500234#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500235};
236
237struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500238 unsigned int added_thread_counters;
239 unsigned int added_core_counters;
240 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500241 struct msr_counter *tp;
242 struct msr_counter *cp;
243 struct msr_counter *pp;
244} sys;
245
Len Brownc98d5d92012-06-04 00:56:40 -0400246struct system_summary {
247 struct thread_data threads;
248 struct core_data cores;
249 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500250} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400251
252
253struct topo_params {
254 int num_packages;
255 int num_cpus;
256 int num_cores;
257 int max_cpu_num;
258 int num_cores_per_pkg;
259 int num_threads_per_core;
260} topo;
261
262struct timeval tv_even, tv_odd, tv_delta;
263
Len Brown562a2d32016-02-26 23:48:05 -0500264int *irq_column_2_cpu; /* /proc/interrupts column numbers */
265int *irqs_per_cpu; /* indexed by cpu_num */
266
Len Brownc98d5d92012-06-04 00:56:40 -0400267void setup_all_buffers(void);
268
269int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400270{
Len Brownc98d5d92012-06-04 00:56:40 -0400271 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400272}
Len Brown88c32812012-03-29 21:44:40 -0400273/*
Len Brownc98d5d92012-06-04 00:56:40 -0400274 * run func(thread, core, package) in topology order
275 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400276 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400277
Len Brownc98d5d92012-06-04 00:56:40 -0400278int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
279 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400280{
Len Brownc98d5d92012-06-04 00:56:40 -0400281 int retval, pkg_no, core_no, thread_no;
282
283 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
284 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
285 for (thread_no = 0; thread_no <
286 topo.num_threads_per_core; ++thread_no) {
287 struct thread_data *t;
288 struct core_data *c;
289 struct pkg_data *p;
290
291 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
292
293 if (cpu_is_not_present(t->cpu_id))
294 continue;
295
296 c = GET_CORE(core_base, core_no, pkg_no);
297 p = GET_PKG(pkg_base, pkg_no);
298
299 retval = func(t, c, p);
300 if (retval)
301 return retval;
302 }
303 }
304 }
305 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400306}
307
308int cpu_migrate(int cpu)
309{
Len Brownc98d5d92012-06-04 00:56:40 -0400310 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
311 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
312 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400313 return -1;
314 else
315 return 0;
316}
Len Brown36229892016-02-26 20:51:02 -0500317int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400318{
Len Brown103a8fe2010-10-22 23:53:03 -0400319 char pathname[32];
320 int fd;
321
Len Brown36229892016-02-26 20:51:02 -0500322 fd = fd_percpu[cpu];
323
324 if (fd)
325 return fd;
326
Len Brown103a8fe2010-10-22 23:53:03 -0400327 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
328 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400329 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400330 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 -0400331
Len Brown36229892016-02-26 20:51:02 -0500332 fd_percpu[cpu] = fd;
333
334 return fd;
335}
336
337int get_msr(int cpu, off_t offset, unsigned long long *msr)
338{
339 ssize_t retval;
340
341 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400342
Len Brown98481e72014-08-15 00:36:50 -0400343 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500344 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400345
346 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400347}
348
Len Brownfc04cc62014-02-06 00:55:19 -0500349/*
Len Brown812db3f2017-02-10 00:25:41 -0500350 * Each string in this array is compared in --show and --hide cmdline.
351 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500352 */
Len Brown812db3f2017-02-10 00:25:41 -0500353struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400354 { 0x0, "usec" },
355 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500356 { 0x0, "Package" },
357 { 0x0, "Avg_MHz" },
358 { 0x0, "Bzy_MHz" },
359 { 0x0, "TSC_MHz" },
360 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500361 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500362 { 0x0, "Busy%" },
363 { 0x0, "CPU%c1" },
364 { 0x0, "CPU%c3" },
365 { 0x0, "CPU%c6" },
366 { 0x0, "CPU%c7" },
367 { 0x0, "ThreadC" },
368 { 0x0, "CoreTmp" },
369 { 0x0, "CoreCnt" },
370 { 0x0, "PkgTmp" },
371 { 0x0, "GFX%rc6" },
372 { 0x0, "GFXMHz" },
373 { 0x0, "Pkg%pc2" },
374 { 0x0, "Pkg%pc3" },
375 { 0x0, "Pkg%pc6" },
376 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500377 { 0x0, "Pkg%pc8" },
378 { 0x0, "Pkg%pc9" },
379 { 0x0, "Pkg%pc10" },
Len Brown812db3f2017-02-10 00:25:41 -0500380 { 0x0, "PkgWatt" },
381 { 0x0, "CorWatt" },
382 { 0x0, "GFXWatt" },
383 { 0x0, "PkgCnt" },
384 { 0x0, "RAMWatt" },
385 { 0x0, "PKG_%" },
386 { 0x0, "RAM_%" },
387 { 0x0, "Pkg_J" },
388 { 0x0, "Cor_J" },
389 { 0x0, "GFX_J" },
390 { 0x0, "RAM_J" },
391 { 0x0, "Core" },
392 { 0x0, "CPU" },
Len Brown0539ba112017-02-10 00:27:20 -0500393 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500394 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400395 { 0x0, "Totl%C0" },
396 { 0x0, "Any%C0" },
397 { 0x0, "GFX%C0" },
398 { 0x0, "CPUGFX%" },
Len Brown812db3f2017-02-10 00:25:41 -0500399};
400
Len Browna99d8732017-05-20 20:11:55 -0400401
402
Len Brown812db3f2017-02-10 00:25:41 -0500403#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400404#define BIC_USEC (1ULL << 0)
405#define BIC_TOD (1ULL << 1)
406#define BIC_Package (1ULL << 2)
407#define BIC_Avg_MHz (1ULL << 3)
408#define BIC_Bzy_MHz (1ULL << 4)
409#define BIC_TSC_MHz (1ULL << 5)
410#define BIC_IRQ (1ULL << 6)
411#define BIC_SMI (1ULL << 7)
412#define BIC_Busy (1ULL << 8)
413#define BIC_CPU_c1 (1ULL << 9)
414#define BIC_CPU_c3 (1ULL << 10)
415#define BIC_CPU_c6 (1ULL << 11)
416#define BIC_CPU_c7 (1ULL << 12)
417#define BIC_ThreadC (1ULL << 13)
418#define BIC_CoreTmp (1ULL << 14)
419#define BIC_CoreCnt (1ULL << 15)
420#define BIC_PkgTmp (1ULL << 16)
421#define BIC_GFX_rc6 (1ULL << 17)
422#define BIC_GFXMHz (1ULL << 18)
423#define BIC_Pkgpc2 (1ULL << 19)
424#define BIC_Pkgpc3 (1ULL << 20)
425#define BIC_Pkgpc6 (1ULL << 21)
426#define BIC_Pkgpc7 (1ULL << 22)
427#define BIC_Pkgpc8 (1ULL << 23)
428#define BIC_Pkgpc9 (1ULL << 24)
429#define BIC_Pkgpc10 (1ULL << 25)
430#define BIC_PkgWatt (1ULL << 26)
431#define BIC_CorWatt (1ULL << 27)
432#define BIC_GFXWatt (1ULL << 28)
433#define BIC_PkgCnt (1ULL << 29)
434#define BIC_RAMWatt (1ULL << 30)
435#define BIC_PKG__ (1ULL << 31)
436#define BIC_RAM__ (1ULL << 32)
437#define BIC_Pkg_J (1ULL << 33)
438#define BIC_Cor_J (1ULL << 34)
439#define BIC_GFX_J (1ULL << 35)
440#define BIC_RAM_J (1ULL << 36)
441#define BIC_Core (1ULL << 37)
442#define BIC_CPU (1ULL << 38)
443#define BIC_Mod_c6 (1ULL << 39)
444#define BIC_sysfs (1ULL << 40)
445#define BIC_Totl_c0 (1ULL << 41)
446#define BIC_Any_c0 (1ULL << 42)
447#define BIC_GFX_c0 (1ULL << 43)
448#define BIC_CPUGFX (1ULL << 44)
Len Brown812db3f2017-02-10 00:25:41 -0500449
Len Brown3f44a5c2017-10-17 15:42:56 -0400450#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
451
452unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
453unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500454
455#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400456#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500457#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500458#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500459
Len Brown3f44a5c2017-10-17 15:42:56 -0400460
Len Browndd778a52017-02-21 23:21:13 -0500461#define MAX_DEFERRED 16
462char *deferred_skip_names[MAX_DEFERRED];
463int deferred_skip_index;
464
465/*
466 * HIDE_LIST - hide this list of counters, show the rest [default]
467 * SHOW_LIST - show this list of counters, hide the rest
468 */
469enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
470
471void help(void)
472{
473 fprintf(outf,
474 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
475 "\n"
476 "Turbostat forks the specified COMMAND and prints statistics\n"
477 "when COMMAND completes.\n"
478 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
479 "to print statistics, until interrupted.\n"
480 "--add add a counter\n"
481 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
482 "--cpu cpu-set limit output to summary plus cpu-set:\n"
483 " {core | package | j,k,l..m,n-p }\n"
484 "--quiet skip decoding system configuration header\n"
Len Brownb9ad8ee2017-07-19 19:28:37 -0400485 "--interval sec.subsec Override default 5-second measurement interval\n"
Len Browndd778a52017-02-21 23:21:13 -0500486 "--help print this help message\n"
487 "--list list column headers only\n"
488 "--out file create or truncate \"file\" for all output\n"
489 "--version print version information\n"
490 "\n"
491 "For more help, run \"man turbostat\"\n");
492}
493
Len Brown812db3f2017-02-10 00:25:41 -0500494/*
495 * bic_lookup
496 * for all the strings in comma separate name_list,
497 * set the approprate bit in return value.
498 */
Len Browndd778a52017-02-21 23:21:13 -0500499unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500500{
501 int i;
502 unsigned long long retval = 0;
503
504 while (name_list) {
505 char *comma;
506
507 comma = strchr(name_list, ',');
508
509 if (comma)
510 *comma = '\0';
511
Len Brown3f44a5c2017-10-17 15:42:56 -0400512 if (!strcmp(name_list, "all"))
513 return ~0;
514
Len Brown812db3f2017-02-10 00:25:41 -0500515 for (i = 0; i < MAX_BIC; ++i) {
516 if (!strcmp(name_list, bic[i].name)) {
517 retval |= (1ULL << i);
518 break;
519 }
520 }
521 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500522 if (mode == SHOW_LIST) {
523 fprintf(stderr, "Invalid counter name: %s\n", name_list);
524 exit(-1);
525 }
526 deferred_skip_names[deferred_skip_index++] = name_list;
527 if (debug)
528 fprintf(stderr, "deferred \"%s\"\n", name_list);
529 if (deferred_skip_index >= MAX_DEFERRED) {
530 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
531 MAX_DEFERRED, name_list);
532 help();
533 exit(1);
534 }
Len Brown812db3f2017-02-10 00:25:41 -0500535 }
536
537 name_list = comma;
538 if (name_list)
539 name_list++;
540
541 }
542 return retval;
543}
Len Brownfc04cc62014-02-06 00:55:19 -0500544
Len Browndd778a52017-02-21 23:21:13 -0500545
Len Brownc8ade362017-02-15 17:15:11 -0500546void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400547{
Len Brown388e9c82016-12-22 23:57:55 -0500548 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500549 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500550
Len Brown3f44a5c2017-10-17 15:42:56 -0400551 if (DO_BIC(BIC_USEC))
552 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
553 if (DO_BIC(BIC_TOD))
554 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500555 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500556 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500557 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500558 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500559 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500560 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500561 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500562 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500563 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500564 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500565 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500566 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500567 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500568 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500569
Len Brown0de6c0d2017-02-15 21:45:40 -0500570 if (DO_BIC(BIC_IRQ)) {
571 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500572 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500573 else
Len Brown6168c2e2017-02-16 23:07:51 -0500574 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500575 }
576
Len Brown812db3f2017-02-10 00:25:41 -0500577 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500578 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500579
Len Brown388e9c82016-12-22 23:57:55 -0500580 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500581
Len Brown388e9c82016-12-22 23:57:55 -0500582 if (mp->format == FORMAT_RAW) {
583 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500584 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500585 else
Len Browndd778a52017-02-21 23:21:13 -0500586 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500587 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500588 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500589 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500590 else
Len Browndd778a52017-02-21 23:21:13 -0500591 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500592 }
593 }
594
Len Brown41618e62017-02-09 18:25:22 -0500595 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500596 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500597 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500598 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500599 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500600 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500601 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500602 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500603
Len Brown0539ba112017-02-10 00:27:20 -0500604 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500605 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500606
Len Brown812db3f2017-02-10 00:25:41 -0500607 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500608 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500609
610 for (mp = sys.cp; mp; mp = mp->next) {
611 if (mp->format == FORMAT_RAW) {
612 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500613 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500614 else
Len Brownc8ade362017-02-15 17:15:11 -0500615 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500616 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500617 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
618 outp += sprintf(outp, "%s%8s", delim, mp->name);
619 else
620 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500621 }
622 }
623
Len Brown812db3f2017-02-10 00:25:41 -0500624 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500625 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500626
Len Brown812db3f2017-02-10 00:25:41 -0500627 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500628 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500629
Len Brown812db3f2017-02-10 00:25:41 -0500630 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500631 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500632
Len Browna99d8732017-05-20 20:11:55 -0400633 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500634 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400635 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500636 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400637 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500638 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400639 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500640 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400641
Len Brown0f47c082017-01-27 00:50:45 -0500642 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500643 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500644 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500645 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500646 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500647 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500648 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500649 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500650 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500651 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500652 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500653 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500654 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500655 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400656
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800657 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500658 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500659 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500660 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500661 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500662 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500663 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500664 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500665 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500666 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500667 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500668 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500669 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500670 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500671 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500672 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500673 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500674 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500675 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500676 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500677 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500678 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500679 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500680 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500681 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500682 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800683 }
Len Brown388e9c82016-12-22 23:57:55 -0500684 for (mp = sys.pp; mp; mp = mp->next) {
685 if (mp->format == FORMAT_RAW) {
686 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500687 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500688 else
Len Brownc8ade362017-02-15 17:15:11 -0500689 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500690 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500691 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
692 outp += sprintf(outp, "%s%8s", delim, mp->name);
693 else
694 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500695 }
696 }
697
Len Brownc98d5d92012-06-04 00:56:40 -0400698 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400699}
700
Len Brownc98d5d92012-06-04 00:56:40 -0400701int dump_counters(struct thread_data *t, struct core_data *c,
702 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400703{
Len Brown388e9c82016-12-22 23:57:55 -0500704 int i;
705 struct msr_counter *mp;
706
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200707 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400708
Len Brownc98d5d92012-06-04 00:56:40 -0400709 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200710 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
711 t->cpu_id, t->flags);
712 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
713 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
714 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
715 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500716
Len Brown812db3f2017-02-10 00:25:41 -0500717 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500718 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500719 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500720 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500721
722 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
723 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
724 i, mp->msr_num, t->counter[i]);
725 }
Len Brownc98d5d92012-06-04 00:56:40 -0400726 }
Len Brown103a8fe2010-10-22 23:53:03 -0400727
Len Brownc98d5d92012-06-04 00:56:40 -0400728 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200729 outp += sprintf(outp, "core: %d\n", c->core_id);
730 outp += sprintf(outp, "c3: %016llX\n", c->c3);
731 outp += sprintf(outp, "c6: %016llX\n", c->c6);
732 outp += sprintf(outp, "c7: %016llX\n", c->c7);
733 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500734
735 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
736 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
737 i, mp->msr_num, c->counter[i]);
738 }
Len Brown0539ba112017-02-10 00:27:20 -0500739 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400740 }
741
742 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200743 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400744
745 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
746 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
747 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
748 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
749
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200750 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500751 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500752 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500753 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500754 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500755 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500756 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200757 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
758 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
759 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
760 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
761 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
762 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
763 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
764 outp += sprintf(outp, "Throttle PKG: %0X\n",
765 p->rapl_pkg_perf_status);
766 outp += sprintf(outp, "Throttle RAM: %0X\n",
767 p->rapl_dram_perf_status);
768 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500769
770 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
771 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
772 i, mp->msr_num, p->counter[i]);
773 }
Len Brownc98d5d92012-06-04 00:56:40 -0400774 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200775
776 outp += sprintf(outp, "\n");
777
Len Brownc98d5d92012-06-04 00:56:40 -0400778 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400779}
780
Len Browne23da032012-02-06 18:37:16 -0500781/*
782 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500783 */
Len Brownc98d5d92012-06-04 00:56:40 -0400784int format_counters(struct thread_data *t, struct core_data *c,
785 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400786{
Len Brown008d396e2017-02-10 00:29:51 -0500787 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500788 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500789 int i;
790 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500791 char *delim = "\t";
792 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400793
Len Brownc98d5d92012-06-04 00:56:40 -0400794 /* if showing only 1st thread in core and this isn't one, bail out */
795 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
796 return 0;
797
798 /* if showing only 1st thread in pkg and this isn't one, bail out */
799 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
800 return 0;
801
Len Brown1ef7d212017-02-10 23:54:15 -0500802 /*if not summary line and --cpu is used */
803 if ((t != &average.threads) &&
804 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
805 return 0;
806
Len Brown3f44a5c2017-10-17 15:42:56 -0400807 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700808 /* on each row, print how many usec each timestamp took to gather */
809 struct timeval tv;
810
811 timersub(&t->tv_end, &t->tv_begin, &tv);
812 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
813 }
814
Len Brown3f44a5c2017-10-17 15:42:56 -0400815 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
816 if (DO_BIC(BIC_TOD))
817 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
818
Len Brown103a8fe2010-10-22 23:53:03 -0400819 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
820
Len Brown008d396e2017-02-10 00:29:51 -0500821 tsc = t->tsc * tsc_tweak;
822
Len Brownc98d5d92012-06-04 00:56:40 -0400823 /* topo columns, print blanks on 1st (average) line */
824 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500825 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500826 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500827 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500828 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500829 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500830 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400831 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500832 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400833 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500834 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400835 else
Len Brown6168c2e2017-02-16 23:07:51 -0500836 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400837 }
Len Brown812db3f2017-02-10 00:25:41 -0500838 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400839 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500840 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400841 else
Len Brown6168c2e2017-02-16 23:07:51 -0500842 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400843 }
Len Brown812db3f2017-02-10 00:25:41 -0500844 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500845 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400846 }
Len Brownfc04cc62014-02-06 00:55:19 -0500847
Len Brown812db3f2017-02-10 00:25:41 -0500848 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500849 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500850 1.0 / units * t->aperf / interval_float);
851
Len Brown812db3f2017-02-10 00:25:41 -0500852 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500853 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400854
Len Brown812db3f2017-02-10 00:25:41 -0500855 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400856 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500857 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400858 else
Len Brown6168c2e2017-02-16 23:07:51 -0500859 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500860 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400861 }
Len Brown103a8fe2010-10-22 23:53:03 -0400862
Len Brown812db3f2017-02-10 00:25:41 -0500863 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500864 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400865
Len Brown562a2d32016-02-26 23:48:05 -0500866 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500867 if (DO_BIC(BIC_IRQ)) {
868 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500869 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500870 else
Len Brown6168c2e2017-02-16 23:07:51 -0500871 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500872 }
Len Brown562a2d32016-02-26 23:48:05 -0500873
Len Brown1cc21f72015-02-23 00:34:57 -0500874 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500875 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500876 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500877
Len Brown678a3bd2017-02-09 22:22:13 -0500878 /* Added counters */
879 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
880 if (mp->format == FORMAT_RAW) {
881 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500882 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500883 else
Len Brown6168c2e2017-02-16 23:07:51 -0500884 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500885 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500886 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500887 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500888 else
Len Brown6168c2e2017-02-16 23:07:51 -0500889 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500890 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500891 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500892 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500893 else
Len Brown6168c2e2017-02-16 23:07:51 -0500894 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500895 }
896 }
897
Len Brown41618e62017-02-09 18:25:22 -0500898 /* C1 */
899 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500900 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500901
902
Len Brownc98d5d92012-06-04 00:56:40 -0400903 /* print per-core data only for 1st thread in core */
904 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
905 goto done;
906
Len Brown812db3f2017-02-10 00:25:41 -0500907 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500908 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500909 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500910 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500911 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500912 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400913
Len Brown0539ba112017-02-10 00:27:20 -0500914 /* Mod%c6 */
915 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500916 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba112017-02-10 00:27:20 -0500917
Len Brown812db3f2017-02-10 00:25:41 -0500918 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500919 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500920
Len Brown388e9c82016-12-22 23:57:55 -0500921 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
922 if (mp->format == FORMAT_RAW) {
923 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500924 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500925 else
Len Brown6168c2e2017-02-16 23:07:51 -0500926 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500927 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500928 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500929 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500930 else
Len Brown6168c2e2017-02-16 23:07:51 -0500931 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500932 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500933 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500934 }
935 }
936
Len Brownc98d5d92012-06-04 00:56:40 -0400937 /* print per-package data only for 1st core in package */
938 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
939 goto done;
940
Len Brown0b2bb692015-03-26 00:50:30 -0400941 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500942 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500943 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500944
Len Brownfdf676e2016-02-27 01:28:12 -0500945 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500946 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400947 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500948 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400949 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500950 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400951 p->gfx_rc6_ms / 10.0 / interval_float);
952 }
953 }
Len Brownfdf676e2016-02-27 01:28:12 -0500954
Len Brown27d47352016-02-27 00:37:54 -0500955 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500956 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500957 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500958
Len Brown0b2bb692015-03-26 00:50:30 -0400959 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -0400960 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500961 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400962 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500963 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400964 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500965 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400966 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500967 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400968
Len Brown0f47c082017-01-27 00:50:45 -0500969 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500970 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500971 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500972 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500973 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500974 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500975 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500976 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500977 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500978 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500979 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500980 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500981 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500982 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500983
984 /*
985 * If measurement interval exceeds minimum RAPL Joule Counter range,
986 * indicate that results are suspect by printing "**" in fraction place.
987 */
Len Brownfc04cc62014-02-06 00:55:19 -0500988 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -0500989 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500990 else
Len Browne975db52016-04-06 23:56:02 -0400991 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500992
Len Brown812db3f2017-02-10 00:25:41 -0500993 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500994 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500995 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500996 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500997 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500998 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500999 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001000 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001001 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001002 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001003 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001004 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001005 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001006 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001007 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001008 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001009 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001010 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 -05001011 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001012 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 -05001013
Len Brown388e9c82016-12-22 23:57:55 -05001014 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1015 if (mp->format == FORMAT_RAW) {
1016 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001017 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001018 else
Len Brown6168c2e2017-02-16 23:07:51 -05001019 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001020 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001021 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001022 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001023 else
Len Brown6168c2e2017-02-16 23:07:51 -05001024 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001025 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001026 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001027 }
1028 }
1029
Len Brownc98d5d92012-06-04 00:56:40 -04001030done:
Len Brownc98d5d92012-06-04 00:56:40 -04001031 outp += sprintf(outp, "\n");
1032
1033 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001034}
1035
Len Brownb7d8c142016-02-13 23:36:17 -05001036void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001037{
Len Brownb7d8c142016-02-13 23:36:17 -05001038 FILE *filep;
1039
1040 if (outf == stderr)
1041 filep = stdout;
1042 else
1043 filep = outf;
1044
1045 fputs(output_buffer, filep);
1046 fflush(filep);
1047
Len Brownc98d5d92012-06-04 00:56:40 -04001048 outp = output_buffer;
1049}
Len Brownb7d8c142016-02-13 23:36:17 -05001050void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001051{
Len Brownb7d8c142016-02-13 23:36:17 -05001052 fputs(output_buffer, outf);
1053 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001054 outp = output_buffer;
1055}
1056void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1057{
Len Browne23da032012-02-06 18:37:16 -05001058 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001059
Len Browne23da032012-02-06 18:37:16 -05001060 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001061 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001062
Len Brownc98d5d92012-06-04 00:56:40 -04001063 if (topo.num_cpus > 1)
1064 format_counters(&average.threads, &average.cores,
1065 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001066
Len Browne23da032012-02-06 18:37:16 -05001067 printed = 1;
1068
1069 if (summary_only)
1070 return;
1071
Len Brownc98d5d92012-06-04 00:56:40 -04001072 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001073}
1074
Len Brown889facb2012-11-08 00:48:57 -05001075#define DELTA_WRAP32(new, old) \
1076 if (new > old) { \
1077 old = new - old; \
1078 } else { \
1079 old = 0x100000000 + new - old; \
1080 }
1081
Len Brownba3dec92016-04-22 20:31:46 -04001082int
Len Brownc98d5d92012-06-04 00:56:40 -04001083delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001084{
Len Brown388e9c82016-12-22 23:57:55 -05001085 int i;
1086 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001087
Len Browna99d8732017-05-20 20:11:55 -04001088
1089 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001090 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001091 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001092 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001093 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001094 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001095 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001096 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 -04001097
Len Brownc98d5d92012-06-04 00:56:40 -04001098 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001099 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001100 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001101 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001102 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001103 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001104 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001105 old->pc8 = new->pc8 - old->pc8;
1106 old->pc9 = new->pc9 - old->pc9;
1107 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -05001108 old->pkg_temp_c = new->pkg_temp_c;
1109
Len Brown9185e982016-04-06 17:16:00 -04001110 /* flag an error when rc6 counter resets/wraps */
1111 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1112 old->gfx_rc6_ms = -1;
1113 else
1114 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1115
Len Brown27d47352016-02-27 00:37:54 -05001116 old->gfx_mhz = new->gfx_mhz;
1117
Len Brown889facb2012-11-08 00:48:57 -05001118 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1119 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1120 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1121 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1122 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1123 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001124
Len Brown388e9c82016-12-22 23:57:55 -05001125 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1126 if (mp->format == FORMAT_RAW)
1127 old->counter[i] = new->counter[i];
1128 else
1129 old->counter[i] = new->counter[i] - old->counter[i];
1130 }
1131
Len Brownba3dec92016-04-22 20:31:46 -04001132 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001133}
Len Brown103a8fe2010-10-22 23:53:03 -04001134
Len Brownc98d5d92012-06-04 00:56:40 -04001135void
1136delta_core(struct core_data *new, struct core_data *old)
1137{
Len Brown388e9c82016-12-22 23:57:55 -05001138 int i;
1139 struct msr_counter *mp;
1140
Len Brownc98d5d92012-06-04 00:56:40 -04001141 old->c3 = new->c3 - old->c3;
1142 old->c6 = new->c6 - old->c6;
1143 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001144 old->core_temp_c = new->core_temp_c;
Len Brown0539ba112017-02-10 00:27:20 -05001145 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001146
1147 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1148 if (mp->format == FORMAT_RAW)
1149 old->counter[i] = new->counter[i];
1150 else
1151 old->counter[i] = new->counter[i] - old->counter[i];
1152 }
Len Brownc98d5d92012-06-04 00:56:40 -04001153}
Len Brown103a8fe2010-10-22 23:53:03 -04001154
Len Brownc3ae3312012-06-13 21:31:46 -04001155/*
1156 * old = new - old
1157 */
Len Brownba3dec92016-04-22 20:31:46 -04001158int
Len Brownc98d5d92012-06-04 00:56:40 -04001159delta_thread(struct thread_data *new, struct thread_data *old,
1160 struct core_data *core_delta)
1161{
Len Brown388e9c82016-12-22 23:57:55 -05001162 int i;
1163 struct msr_counter *mp;
1164
Len Brown3f44a5c2017-10-17 15:42:56 -04001165 /*
1166 * the timestamps from start of measurement interval are in "old"
1167 * the timestamp from end of measurement interval are in "new"
1168 * over-write old w/ new so we can print end of interval values
1169 */
1170
1171 old->tv_begin = new->tv_begin;
1172 old->tv_end = new->tv_end;
1173
Len Brownc98d5d92012-06-04 00:56:40 -04001174 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001175
Len Brownc98d5d92012-06-04 00:56:40 -04001176 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001177 if (old->tsc < (1000 * 1000))
1178 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1179 "You can disable all c-states by booting with \"idle=poll\"\n"
1180 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001181
Len Brownc98d5d92012-06-04 00:56:40 -04001182 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001183
Len Brown812db3f2017-02-10 00:25:41 -05001184 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001185 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1186 old->aperf = new->aperf - old->aperf;
1187 old->mperf = new->mperf - old->mperf;
1188 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001189 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001190 }
Len Brownc98d5d92012-06-04 00:56:40 -04001191 }
Len Brown103a8fe2010-10-22 23:53:03 -04001192
Len Brown103a8fe2010-10-22 23:53:03 -04001193
Len Brown144b44b2013-11-09 00:30:16 -05001194 if (use_c1_residency_msr) {
1195 /*
1196 * Some models have a dedicated C1 residency MSR,
1197 * which should be more accurate than the derivation below.
1198 */
1199 } else {
1200 /*
1201 * As counter collection is not atomic,
1202 * it is possible for mperf's non-halted cycles + idle states
1203 * to exceed TSC's all cycles: show c1 = 0% in that case.
1204 */
Len Brown95149362017-04-12 19:44:51 -04001205 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001206 old->c1 = 0;
1207 else {
1208 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001209 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001210 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001211 }
Len Brownc98d5d92012-06-04 00:56:40 -04001212 }
Len Brownc3ae3312012-06-13 21:31:46 -04001213
Len Brownc98d5d92012-06-04 00:56:40 -04001214 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001215 if (debug > 1)
1216 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001217 old->mperf = 1; /* divide by 0 protection */
1218 }
1219
Len Brown812db3f2017-02-10 00:25:41 -05001220 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001221 old->irq_count = new->irq_count - old->irq_count;
1222
Len Brown812db3f2017-02-10 00:25:41 -05001223 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001224 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001225
Len Brown388e9c82016-12-22 23:57:55 -05001226 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1227 if (mp->format == FORMAT_RAW)
1228 old->counter[i] = new->counter[i];
1229 else
1230 old->counter[i] = new->counter[i] - old->counter[i];
1231 }
Len Brownba3dec92016-04-22 20:31:46 -04001232 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001233}
1234
1235int delta_cpu(struct thread_data *t, struct core_data *c,
1236 struct pkg_data *p, struct thread_data *t2,
1237 struct core_data *c2, struct pkg_data *p2)
1238{
Len Brownba3dec92016-04-22 20:31:46 -04001239 int retval = 0;
1240
Len Brownc98d5d92012-06-04 00:56:40 -04001241 /* calculate core delta only for 1st thread in core */
1242 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1243 delta_core(c, c2);
1244
1245 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001246 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1247 if (retval)
1248 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001249
1250 /* calculate package delta only for 1st core in package */
1251 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001252 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001253
Len Brownba3dec92016-04-22 20:31:46 -04001254 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001255}
1256
1257void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1258{
Len Brown388e9c82016-12-22 23:57:55 -05001259 int i;
1260 struct msr_counter *mp;
1261
Len Brown3f44a5c2017-10-17 15:42:56 -04001262 t->tv_begin.tv_sec = 0;
1263 t->tv_begin.tv_usec = 0;
1264 t->tv_end.tv_sec = 0;
1265 t->tv_end.tv_usec = 0;
1266
Len Brownc98d5d92012-06-04 00:56:40 -04001267 t->tsc = 0;
1268 t->aperf = 0;
1269 t->mperf = 0;
1270 t->c1 = 0;
1271
Len Brown562a2d32016-02-26 23:48:05 -05001272 t->irq_count = 0;
1273 t->smi_count = 0;
1274
Len Brownc98d5d92012-06-04 00:56:40 -04001275 /* tells format_counters to dump all fields from this set */
1276 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1277
1278 c->c3 = 0;
1279 c->c6 = 0;
1280 c->c7 = 0;
Len Brown0539ba112017-02-10 00:27:20 -05001281 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001282 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001283
Len Brown0b2bb692015-03-26 00:50:30 -04001284 p->pkg_wtd_core_c0 = 0;
1285 p->pkg_any_core_c0 = 0;
1286 p->pkg_any_gfxe_c0 = 0;
1287 p->pkg_both_core_gfxe_c0 = 0;
1288
Len Brownc98d5d92012-06-04 00:56:40 -04001289 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001290 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001291 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001292 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001293 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001294 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001295 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001296 p->pc8 = 0;
1297 p->pc9 = 0;
1298 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001299
1300 p->energy_pkg = 0;
1301 p->energy_dram = 0;
1302 p->energy_cores = 0;
1303 p->energy_gfx = 0;
1304 p->rapl_pkg_perf_status = 0;
1305 p->rapl_dram_perf_status = 0;
1306 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001307
Len Brownfdf676e2016-02-27 01:28:12 -05001308 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001309 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001310 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1311 t->counter[i] = 0;
1312
1313 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1314 c->counter[i] = 0;
1315
1316 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1317 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001318}
1319int sum_counters(struct thread_data *t, struct core_data *c,
1320 struct pkg_data *p)
1321{
Len Brown388e9c82016-12-22 23:57:55 -05001322 int i;
1323 struct msr_counter *mp;
1324
Len Brown3f44a5c2017-10-17 15:42:56 -04001325 /* remember first tv_begin */
1326 if (average.threads.tv_begin.tv_sec == 0)
1327 average.threads.tv_begin = t->tv_begin;
1328
1329 /* remember last tv_end */
1330 average.threads.tv_end = t->tv_end;
1331
Len Brownc98d5d92012-06-04 00:56:40 -04001332 average.threads.tsc += t->tsc;
1333 average.threads.aperf += t->aperf;
1334 average.threads.mperf += t->mperf;
1335 average.threads.c1 += t->c1;
1336
Len Brown562a2d32016-02-26 23:48:05 -05001337 average.threads.irq_count += t->irq_count;
1338 average.threads.smi_count += t->smi_count;
1339
Len Brown388e9c82016-12-22 23:57:55 -05001340 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1341 if (mp->format == FORMAT_RAW)
1342 continue;
1343 average.threads.counter[i] += t->counter[i];
1344 }
1345
Len Brownc98d5d92012-06-04 00:56:40 -04001346 /* sum per-core values only for 1st thread in core */
1347 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1348 return 0;
1349
1350 average.cores.c3 += c->c3;
1351 average.cores.c6 += c->c6;
1352 average.cores.c7 += c->c7;
Len Brown0539ba112017-02-10 00:27:20 -05001353 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001354
Len Brown889facb2012-11-08 00:48:57 -05001355 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1356
Len Brown388e9c82016-12-22 23:57:55 -05001357 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1358 if (mp->format == FORMAT_RAW)
1359 continue;
1360 average.cores.counter[i] += c->counter[i];
1361 }
1362
Len Brownc98d5d92012-06-04 00:56:40 -04001363 /* sum per-pkg values only for 1st core in pkg */
1364 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1365 return 0;
1366
Len Browna99d8732017-05-20 20:11:55 -04001367 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001368 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001369 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001370 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001371 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001372 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001373 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001374 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001375
Len Brownc98d5d92012-06-04 00:56:40 -04001376 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001377 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001378 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001379 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001380 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001381 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001382 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001383 average.packages.pc8 += p->pc8;
1384 average.packages.pc9 += p->pc9;
1385 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001386
Len Brown889facb2012-11-08 00:48:57 -05001387 average.packages.energy_pkg += p->energy_pkg;
1388 average.packages.energy_dram += p->energy_dram;
1389 average.packages.energy_cores += p->energy_cores;
1390 average.packages.energy_gfx += p->energy_gfx;
1391
Len Brownfdf676e2016-02-27 01:28:12 -05001392 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001393 average.packages.gfx_mhz = p->gfx_mhz;
1394
Len Brown889facb2012-11-08 00:48:57 -05001395 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1396
1397 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1398 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001399
1400 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1401 if (mp->format == FORMAT_RAW)
1402 continue;
1403 average.packages.counter[i] += p->counter[i];
1404 }
Len Brownc98d5d92012-06-04 00:56:40 -04001405 return 0;
1406}
1407/*
1408 * sum the counters for all cpus in the system
1409 * compute the weighted average
1410 */
1411void compute_average(struct thread_data *t, struct core_data *c,
1412 struct pkg_data *p)
1413{
Len Brown388e9c82016-12-22 23:57:55 -05001414 int i;
1415 struct msr_counter *mp;
1416
Len Brownc98d5d92012-06-04 00:56:40 -04001417 clear_counters(&average.threads, &average.cores, &average.packages);
1418
1419 for_all_cpus(sum_counters, t, c, p);
1420
1421 average.threads.tsc /= topo.num_cpus;
1422 average.threads.aperf /= topo.num_cpus;
1423 average.threads.mperf /= topo.num_cpus;
1424 average.threads.c1 /= topo.num_cpus;
1425
Len Brown0de6c0d2017-02-15 21:45:40 -05001426 if (average.threads.irq_count > 9999999)
1427 sums_need_wide_columns = 1;
1428
Len Brownc98d5d92012-06-04 00:56:40 -04001429 average.cores.c3 /= topo.num_cores;
1430 average.cores.c6 /= topo.num_cores;
1431 average.cores.c7 /= topo.num_cores;
Len Brown0539ba112017-02-10 00:27:20 -05001432 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001433
Len Browna99d8732017-05-20 20:11:55 -04001434 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001435 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001436 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001437 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001438 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001439 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001440 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001441 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001442
Len Brownc98d5d92012-06-04 00:56:40 -04001443 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001444 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001445 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001446 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001447 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001448 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001449 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001450
1451 average.packages.pc8 /= topo.num_packages;
1452 average.packages.pc9 /= topo.num_packages;
1453 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001454
1455 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1456 if (mp->format == FORMAT_RAW)
1457 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001458 if (mp->type == COUNTER_ITEMS) {
1459 if (average.threads.counter[i] > 9999999)
1460 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001461 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001462 }
Len Brown388e9c82016-12-22 23:57:55 -05001463 average.threads.counter[i] /= topo.num_cpus;
1464 }
1465 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1466 if (mp->format == FORMAT_RAW)
1467 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001468 if (mp->type == COUNTER_ITEMS) {
1469 if (average.cores.counter[i] > 9999999)
1470 sums_need_wide_columns = 1;
1471 }
Len Brown388e9c82016-12-22 23:57:55 -05001472 average.cores.counter[i] /= topo.num_cores;
1473 }
1474 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1475 if (mp->format == FORMAT_RAW)
1476 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001477 if (mp->type == COUNTER_ITEMS) {
1478 if (average.packages.counter[i] > 9999999)
1479 sums_need_wide_columns = 1;
1480 }
Len Brown388e9c82016-12-22 23:57:55 -05001481 average.packages.counter[i] /= topo.num_packages;
1482 }
Len Brownc98d5d92012-06-04 00:56:40 -04001483}
1484
1485static unsigned long long rdtsc(void)
1486{
1487 unsigned int low, high;
1488
1489 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1490
1491 return low | ((unsigned long long)high) << 32;
1492}
1493
Len Brownc98d5d92012-06-04 00:56:40 -04001494/*
Len Brown495c76542017-02-08 02:41:51 -05001495 * Open a file, and exit on failure
1496 */
1497FILE *fopen_or_die(const char *path, const char *mode)
1498{
1499 FILE *filep = fopen(path, mode);
1500
1501 if (!filep)
1502 err(1, "%s: open failed", path);
1503 return filep;
1504}
1505/*
1506 * snapshot_sysfs_counter()
1507 *
1508 * return snapshot of given counter
1509 */
1510unsigned long long snapshot_sysfs_counter(char *path)
1511{
1512 FILE *fp;
1513 int retval;
1514 unsigned long long counter;
1515
1516 fp = fopen_or_die(path, "r");
1517
1518 retval = fscanf(fp, "%lld", &counter);
1519 if (retval != 1)
1520 err(1, "snapshot_sysfs_counter(%s)", path);
1521
1522 fclose(fp);
1523
1524 return counter;
1525}
1526
1527int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1528{
1529 if (mp->msr_num != 0) {
1530 if (get_msr(cpu, mp->msr_num, counterp))
1531 return -1;
1532 } else {
Len Brown46c27972017-12-08 17:38:17 -05001533 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001534
1535 if (mp->flags & SYSFS_PERCPU) {
1536 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1537 cpu, mp->path);
1538
1539 *counterp = snapshot_sysfs_counter(path);
1540 } else {
1541 *counterp = snapshot_sysfs_counter(mp->path);
1542 }
Len Brown495c76542017-02-08 02:41:51 -05001543 }
1544
1545 return 0;
1546}
1547
1548/*
Len Brownc98d5d92012-06-04 00:56:40 -04001549 * get_counters(...)
1550 * migrate to cpu
1551 * acquire and record local counters for that cpu
1552 */
1553int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1554{
1555 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001556 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001557 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001558 struct msr_counter *mp;
1559 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001560
Len Brownf4fdf2b2017-05-27 21:06:55 -07001561
1562 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1563
Len Browne52966c2012-11-08 22:38:05 -05001564 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001565 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001566 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001567 }
Len Brownc98d5d92012-06-04 00:56:40 -04001568
Len Brown0102b062016-02-27 03:11:29 -05001569retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001570 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1571
Len Brown812db3f2017-02-10 00:25:41 -05001572 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001573 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1574
1575 /*
1576 * The TSC, APERF and MPERF must be read together for
1577 * APERF/MPERF and MPERF/TSC to give accurate results.
1578 *
1579 * Unfortunately, APERF and MPERF are read by
1580 * individual system call, so delays may occur
1581 * between them. If the time to read them
1582 * varies by a large amount, we re-read them.
1583 */
1584
1585 /*
1586 * This initial dummy APERF read has been seen to
1587 * reduce jitter in the subsequent reads.
1588 */
1589
Len Brown9c63a652012-10-31 01:29:52 -04001590 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001591 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001592
1593 t->tsc = rdtsc(); /* re-read close to APERF */
1594
1595 tsc_before = t->tsc;
1596
1597 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1598 return -3;
1599
1600 tsc_between = rdtsc();
1601
Len Brown9c63a652012-10-31 01:29:52 -04001602 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001603 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001604
1605 tsc_after = rdtsc();
1606
1607 aperf_time = tsc_between - tsc_before;
1608 mperf_time = tsc_after - tsc_between;
1609
1610 /*
1611 * If the system call latency to read APERF and MPERF
1612 * differ by more than 2x, then try again.
1613 */
1614 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1615 aperf_mperf_retry_count++;
1616 if (aperf_mperf_retry_count < 5)
1617 goto retry;
1618 else
1619 warnx("cpu%d jitter %lld %lld",
1620 cpu, aperf_time, mperf_time);
1621 }
1622 aperf_mperf_retry_count = 0;
1623
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001624 t->aperf = t->aperf * aperf_mperf_multiplier;
1625 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001626 }
1627
Len Brown812db3f2017-02-10 00:25:41 -05001628 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001629 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001630 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001631 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1632 return -5;
1633 t->smi_count = msr & 0xFFFFFFFF;
1634 }
Len Brown0539ba112017-02-10 00:27:20 -05001635 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001636 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1637 return -6;
1638 }
1639
Len Brown388e9c82016-12-22 23:57:55 -05001640 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001641 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001642 return -10;
1643 }
1644
Len Brownc98d5d92012-06-04 00:56:40 -04001645 /* collect core counters only for 1st thread in core */
1646 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001647 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001648
Len Brown812db3f2017-02-10 00:25:41 -05001649 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001650 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1651 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001652 }
1653
Len Brown812db3f2017-02-10 00:25:41 -05001654 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001655 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1656 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001657 } else if (do_knl_cstates) {
1658 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1659 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001660 }
1661
Len Brown812db3f2017-02-10 00:25:41 -05001662 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001663 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1664 return -8;
1665
Len Brown0539ba112017-02-10 00:27:20 -05001666 if (DO_BIC(BIC_Mod_c6))
1667 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1668 return -8;
1669
Len Brown812db3f2017-02-10 00:25:41 -05001670 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001671 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1672 return -9;
1673 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1674 }
1675
Len Brown388e9c82016-12-22 23:57:55 -05001676 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001677 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001678 return -10;
1679 }
Len Brown889facb2012-11-08 00:48:57 -05001680
Len Brownc98d5d92012-06-04 00:56:40 -04001681 /* collect package counters only for 1st core in package */
1682 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001683 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001684
Len Browna99d8732017-05-20 20:11:55 -04001685 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001686 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1687 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001688 }
1689 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001690 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1691 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001692 }
1693 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001694 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1695 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001696 }
1697 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001698 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1699 return -13;
1700 }
Len Brown0f47c082017-01-27 00:50:45 -05001701 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001702 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1703 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001704 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba112017-02-10 00:27:20 -05001705 if (do_slm_cstates) {
1706 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1707 return -10;
1708 } else {
1709 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1710 return -10;
1711 }
1712 }
1713
Len Brown0f47c082017-01-27 00:50:45 -05001714 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001715 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1716 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001717 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001718 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1719 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001720 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001721 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1722 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001723 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001724 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1725 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001726 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001727 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1728 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001729
Len Brown889facb2012-11-08 00:48:57 -05001730 if (do_rapl & RAPL_PKG) {
1731 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1732 return -13;
1733 p->energy_pkg = msr & 0xFFFFFFFF;
1734 }
Jacob Pan91484942016-06-16 09:48:20 -07001735 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001736 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1737 return -14;
1738 p->energy_cores = msr & 0xFFFFFFFF;
1739 }
1740 if (do_rapl & RAPL_DRAM) {
1741 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1742 return -15;
1743 p->energy_dram = msr & 0xFFFFFFFF;
1744 }
1745 if (do_rapl & RAPL_GFX) {
1746 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1747 return -16;
1748 p->energy_gfx = msr & 0xFFFFFFFF;
1749 }
1750 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1751 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1752 return -16;
1753 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1754 }
1755 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1756 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1757 return -16;
1758 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1759 }
Len Brown812db3f2017-02-10 00:25:41 -05001760 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001761 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1762 return -17;
1763 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1764 }
Len Brownfdf676e2016-02-27 01:28:12 -05001765
Len Brown812db3f2017-02-10 00:25:41 -05001766 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001767 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1768
Len Brown812db3f2017-02-10 00:25:41 -05001769 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001770 p->gfx_mhz = gfx_cur_mhz;
1771
Len Brown388e9c82016-12-22 23:57:55 -05001772 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001773 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001774 return -10;
1775 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001776done:
1777 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001778
Len Brown103a8fe2010-10-22 23:53:03 -04001779 return 0;
1780}
1781
Len Brownee7e38e2015-02-09 23:39:45 -05001782/*
1783 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1784 * If you change the values, note they are used both in comparisons
1785 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1786 */
1787
1788#define PCLUKN 0 /* Unknown */
1789#define PCLRSV 1 /* Reserved */
1790#define PCL__0 2 /* PC0 */
1791#define PCL__1 3 /* PC1 */
1792#define PCL__2 4 /* PC2 */
1793#define PCL__3 5 /* PC3 */
1794#define PCL__4 6 /* PC4 */
1795#define PCL__6 7 /* PC6 */
1796#define PCL_6N 8 /* PC6 No Retention */
1797#define PCL_6R 9 /* PC6 Retention */
1798#define PCL__7 10 /* PC7 */
1799#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001800#define PCL__8 12 /* PC8 */
1801#define PCL__9 13 /* PC9 */
1802#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001803
1804int pkg_cstate_limit = PCLUKN;
1805char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001806 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001807
Len Browne9257f52015-04-01 21:02:57 -04001808int 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};
1809int 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};
1810int 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 Brown0539ba112017-02-10 00:27:20 -05001811int 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 -05001812int 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 -04001813int 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 -04001814int 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 +03001815int 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 -05001816
Len Browna2b7b742015-09-26 00:12:38 -04001817
1818static void
1819calculate_tsc_tweak()
1820{
Len Browna2b7b742015-09-26 00:12:38 -04001821 tsc_tweak = base_hz / tsc_hz;
1822}
1823
Len Brownfcd17212015-03-23 20:29:09 -04001824static void
1825dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001826{
1827 unsigned long long msr;
1828 unsigned int ratio;
1829
Len Brownec0adc52015-11-12 02:42:31 -05001830 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001831
Len Brownb7d8c142016-02-13 23:36:17 -05001832 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001833
Len Brown103a8fe2010-10-22 23:53:03 -04001834 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001835 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001836 ratio, bclk, ratio * bclk);
1837
1838 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001839 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001840 ratio, bclk, ratio * bclk);
1841
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001842 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001843 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001844 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001845
Len Brownfcd17212015-03-23 20:29:09 -04001846 return;
1847}
1848
1849static void
1850dump_hsw_turbo_ratio_limits(void)
1851{
1852 unsigned long long msr;
1853 unsigned int ratio;
1854
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001855 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001856
Len Brownb7d8c142016-02-13 23:36:17 -05001857 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001858
1859 ratio = (msr >> 8) & 0xFF;
1860 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001861 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001862 ratio, bclk, ratio * bclk);
1863
1864 ratio = (msr >> 0) & 0xFF;
1865 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001866 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001867 ratio, bclk, ratio * bclk);
1868 return;
1869}
1870
1871static void
1872dump_ivt_turbo_ratio_limits(void)
1873{
1874 unsigned long long msr;
1875 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001876
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001877 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001878
Len Brownb7d8c142016-02-13 23:36:17 -05001879 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001880
1881 ratio = (msr >> 56) & 0xFF;
1882 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001883 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001884 ratio, bclk, ratio * bclk);
1885
1886 ratio = (msr >> 48) & 0xFF;
1887 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001888 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001889 ratio, bclk, ratio * bclk);
1890
1891 ratio = (msr >> 40) & 0xFF;
1892 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001893 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001894 ratio, bclk, ratio * bclk);
1895
1896 ratio = (msr >> 32) & 0xFF;
1897 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001898 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001899 ratio, bclk, ratio * bclk);
1900
1901 ratio = (msr >> 24) & 0xFF;
1902 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001903 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001904 ratio, bclk, ratio * bclk);
1905
1906 ratio = (msr >> 16) & 0xFF;
1907 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001908 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001909 ratio, bclk, ratio * bclk);
1910
1911 ratio = (msr >> 8) & 0xFF;
1912 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001913 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001914 ratio, bclk, ratio * bclk);
1915
1916 ratio = (msr >> 0) & 0xFF;
1917 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001918 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001919 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001920 return;
1921}
Len Brown31e07522017-01-31 23:07:49 -05001922int has_turbo_ratio_group_limits(int family, int model)
1923{
1924
1925 if (!genuine_intel)
1926 return 0;
1927
1928 switch (model) {
1929 case INTEL_FAM6_ATOM_GOLDMONT:
1930 case INTEL_FAM6_SKYLAKE_X:
1931 case INTEL_FAM6_ATOM_DENVERTON:
1932 return 1;
1933 }
1934 return 0;
1935}
Len Brown6574a5d2012-09-21 00:01:31 -04001936
Len Brownfcd17212015-03-23 20:29:09 -04001937static void
Len Brown31e07522017-01-31 23:07:49 -05001938dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001939{
Len Brown31e07522017-01-31 23:07:49 -05001940 unsigned long long msr, core_counts;
1941 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001942
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001943 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001944 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001945
Len Brown31e07522017-01-31 23:07:49 -05001946 if (has_turbo_ratio_group_limits(family, model)) {
1947 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1948 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1949 } else {
1950 core_counts = 0x0807060504030201;
1951 }
1952
Len Brown6574a5d2012-09-21 00:01:31 -04001953 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001954 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001955 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001956 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1957 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001958
1959 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001960 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001961 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001962 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1963 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001964
1965 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001966 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001967 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001968 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1969 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001970
1971 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001972 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001973 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001974 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1975 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001976
Len Brown103a8fe2010-10-22 23:53:03 -04001977 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001978 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001979 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001980 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1981 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001982
1983 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001984 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001985 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001986 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1987 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001988
1989 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001990 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001991 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001992 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1993 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001994
1995 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001996 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001997 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001998 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1999 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002000 return;
2001}
Len Brown3a9a9412014-08-15 02:39:52 -04002002
Len Brownfcd17212015-03-23 20:29:09 -04002003static void
Len Brown0f7887c2017-01-12 23:49:18 -05002004dump_atom_turbo_ratio_limits(void)
2005{
2006 unsigned long long msr;
2007 unsigned int ratio;
2008
2009 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2010 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2011
2012 ratio = (msr >> 0) & 0x3F;
2013 if (ratio)
2014 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2015 ratio, bclk, ratio * bclk);
2016
2017 ratio = (msr >> 8) & 0x3F;
2018 if (ratio)
2019 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2020 ratio, bclk, ratio * bclk);
2021
2022 ratio = (msr >> 16) & 0x3F;
2023 if (ratio)
2024 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2025 ratio, bclk, ratio * bclk);
2026
2027 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2028 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2029
2030 ratio = (msr >> 24) & 0x3F;
2031 if (ratio)
2032 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2033 ratio, bclk, ratio * bclk);
2034
2035 ratio = (msr >> 16) & 0x3F;
2036 if (ratio)
2037 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2038 ratio, bclk, ratio * bclk);
2039
2040 ratio = (msr >> 8) & 0x3F;
2041 if (ratio)
2042 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2043 ratio, bclk, ratio * bclk);
2044
2045 ratio = (msr >> 0) & 0x3F;
2046 if (ratio)
2047 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2048 ratio, bclk, ratio * bclk);
2049}
2050
2051static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002052dump_knl_turbo_ratio_limits(void)
2053{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002054 const unsigned int buckets_no = 7;
2055
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002056 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002057 int delta_cores, delta_ratio;
2058 int i, b_nr;
2059 unsigned int cores[buckets_no];
2060 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002061
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002062 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002063
Len Brownb7d8c142016-02-13 23:36:17 -05002064 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002065 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002066
2067 /**
2068 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002069 * [0] -- Reserved
2070 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002071 * [15:8] -- Base value of freq ratio of bucket 1.
2072 * [20:16] -- +ve delta of number of active cores of bucket 2.
2073 * i.e. active cores of bucket 2 =
2074 * active cores of bucket 1 + delta
2075 * [23:21] -- Negative delta of freq ratio of bucket 2.
2076 * i.e. freq ratio of bucket 2 =
2077 * freq ratio of bucket 1 - delta
2078 * [28:24]-- +ve delta of number of active cores of bucket 3.
2079 * [31:29]-- -ve delta of freq ratio of bucket 3.
2080 * [36:32]-- +ve delta of number of active cores of bucket 4.
2081 * [39:37]-- -ve delta of freq ratio of bucket 4.
2082 * [44:40]-- +ve delta of number of active cores of bucket 5.
2083 * [47:45]-- -ve delta of freq ratio of bucket 5.
2084 * [52:48]-- +ve delta of number of active cores of bucket 6.
2085 * [55:53]-- -ve delta of freq ratio of bucket 6.
2086 * [60:56]-- +ve delta of number of active cores of bucket 7.
2087 * [63:61]-- -ve delta of freq ratio of bucket 7.
2088 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002089
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002090 b_nr = 0;
2091 cores[b_nr] = (msr & 0xFF) >> 1;
2092 ratio[b_nr] = (msr >> 8) & 0xFF;
2093
2094 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002095 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002096 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002097
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002098 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2099 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2100 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002101 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002102
2103 for (i = buckets_no - 1; i >= 0; i--)
2104 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002105 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002106 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002107 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002108}
2109
2110static void
Len Brownfcd17212015-03-23 20:29:09 -04002111dump_nhm_cst_cfg(void)
2112{
2113 unsigned long long msr;
2114
Len Brown1df2e552017-01-07 23:24:57 -05002115 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002116
2117#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
2118#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
2119
Len Brown1df2e552017-01-07 23:24:57 -05002120 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002121
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002122 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s",
Len Brownfcd17212015-03-23 20:29:09 -04002123 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2124 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2125 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2126 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2127 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002128 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002129 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002130
2131#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2132 if (has_automatic_cstate_conversion) {
2133 fprintf(outf, ", automatic c-state conversion=%s",
2134 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2135 }
2136
2137 fprintf(outf, ")\n");
2138
Len Brownfcd17212015-03-23 20:29:09 -04002139 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002140}
2141
Len Brown6fb31432015-06-17 16:23:45 -04002142static void
2143dump_config_tdp(void)
2144{
2145 unsigned long long msr;
2146
2147 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002148 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002149 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002150
2151 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002152 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002153 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002154 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2155 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2156 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2157 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002158 }
Len Brownb7d8c142016-02-13 23:36:17 -05002159 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002160
2161 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002162 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002163 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002164 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2165 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2166 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2167 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002168 }
Len Brownb7d8c142016-02-13 23:36:17 -05002169 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002170
2171 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002172 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002173 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002174 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2175 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2176 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002177
Len Brown6fb31432015-06-17 16:23:45 -04002178 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002179 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002180 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002181 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2182 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002183}
Len Brown5a634262016-04-06 17:15:55 -04002184
2185unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2186
2187void print_irtl(void)
2188{
2189 unsigned long long msr;
2190
2191 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2192 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2193 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2194 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2195
2196 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2197 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2198 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2199 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2200
2201 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2202 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2203 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2204 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2205
2206 if (!do_irtl_hsw)
2207 return;
2208
2209 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2210 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2211 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2212 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2213
2214 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2215 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2216 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2217 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2218
2219 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2220 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2221 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2222 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2223
2224}
Len Brown36229892016-02-26 20:51:02 -05002225void free_fd_percpu(void)
2226{
2227 int i;
2228
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002229 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002230 if (fd_percpu[i] != 0)
2231 close(fd_percpu[i]);
2232 }
2233
2234 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002235}
2236
Len Brownc98d5d92012-06-04 00:56:40 -04002237void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002238{
Len Brownc98d5d92012-06-04 00:56:40 -04002239 CPU_FREE(cpu_present_set);
2240 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002241 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002242
Len Brownc98d5d92012-06-04 00:56:40 -04002243 CPU_FREE(cpu_affinity_set);
2244 cpu_affinity_set = NULL;
2245 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002246
Len Brownc98d5d92012-06-04 00:56:40 -04002247 free(thread_even);
2248 free(core_even);
2249 free(package_even);
2250
2251 thread_even = NULL;
2252 core_even = NULL;
2253 package_even = NULL;
2254
2255 free(thread_odd);
2256 free(core_odd);
2257 free(package_odd);
2258
2259 thread_odd = NULL;
2260 core_odd = NULL;
2261 package_odd = NULL;
2262
2263 free(output_buffer);
2264 output_buffer = NULL;
2265 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002266
2267 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002268
2269 free(irq_column_2_cpu);
2270 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002271}
2272
Josh Triplett57a42a32013-08-20 17:20:17 -07002273
2274/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002275 * Parse a file containing a single int.
2276 */
2277int parse_int_file(const char *fmt, ...)
2278{
2279 va_list args;
2280 char path[PATH_MAX];
2281 FILE *filep;
2282 int value;
2283
2284 va_start(args, fmt);
2285 vsnprintf(path, sizeof(path), fmt, args);
2286 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002287 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002288 if (fscanf(filep, "%d", &value) != 1)
2289 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002290 fclose(filep);
2291 return value;
2292}
2293
2294/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002295 * get_cpu_position_in_core(cpu)
2296 * return the position of the CPU among its HT siblings in the core
2297 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002298 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002299int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002300{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002301 char path[64];
2302 FILE *filep;
2303 int this_cpu;
2304 char character;
2305 int i;
2306
2307 sprintf(path,
2308 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2309 cpu);
2310 filep = fopen(path, "r");
2311 if (filep == NULL) {
2312 perror(path);
2313 exit(1);
2314 }
2315
2316 for (i = 0; i < topo.num_threads_per_core; i++) {
2317 fscanf(filep, "%d", &this_cpu);
2318 if (this_cpu == cpu) {
2319 fclose(filep);
2320 return i;
2321 }
2322
2323 /* Account for no separator after last thread*/
2324 if (i != (topo.num_threads_per_core - 1))
2325 fscanf(filep, "%c", &character);
2326 }
2327
2328 fclose(filep);
2329 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002330}
2331
Len Brownc98d5d92012-06-04 00:56:40 -04002332/*
2333 * cpu_is_first_core_in_package(cpu)
2334 * return 1 if given CPU is 1st core in package
2335 */
2336int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002337{
Josh Triplett95aebc42013-08-20 17:20:16 -07002338 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002339}
2340
2341int get_physical_package_id(int cpu)
2342{
Josh Triplett95aebc42013-08-20 17:20:16 -07002343 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002344}
2345
2346int get_core_id(int cpu)
2347{
Josh Triplett95aebc42013-08-20 17:20:16 -07002348 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002349}
2350
Len Brownc98d5d92012-06-04 00:56:40 -04002351int get_num_ht_siblings(int cpu)
2352{
2353 char path[80];
2354 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002355 int sib1;
2356 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002357 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002358 char str[100];
2359 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002360
2361 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002362 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002363
Len Brownc98d5d92012-06-04 00:56:40 -04002364 /*
2365 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002366 * A ',' separated or '-' separated set of numbers
2367 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002368 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002369 fscanf(filep, "%d%c\n", &sib1, &character);
2370 fseek(filep, 0, SEEK_SET);
2371 fgets(str, 100, filep);
2372 ch = strchr(str, character);
2373 while (ch != NULL) {
2374 matches++;
2375 ch = strchr(ch+1, character);
2376 }
Len Brownc98d5d92012-06-04 00:56:40 -04002377
2378 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002379 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002380}
2381
Len Brown103a8fe2010-10-22 23:53:03 -04002382/*
Len Brownc98d5d92012-06-04 00:56:40 -04002383 * run func(thread, core, package) in topology order
2384 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002385 */
2386
Len Brownc98d5d92012-06-04 00:56:40 -04002387int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2388 struct pkg_data *, struct thread_data *, struct core_data *,
2389 struct pkg_data *), struct thread_data *thread_base,
2390 struct core_data *core_base, struct pkg_data *pkg_base,
2391 struct thread_data *thread_base2, struct core_data *core_base2,
2392 struct pkg_data *pkg_base2)
2393{
2394 int retval, pkg_no, core_no, thread_no;
2395
2396 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2397 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2398 for (thread_no = 0; thread_no <
2399 topo.num_threads_per_core; ++thread_no) {
2400 struct thread_data *t, *t2;
2401 struct core_data *c, *c2;
2402 struct pkg_data *p, *p2;
2403
2404 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2405
2406 if (cpu_is_not_present(t->cpu_id))
2407 continue;
2408
2409 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2410
2411 c = GET_CORE(core_base, core_no, pkg_no);
2412 c2 = GET_CORE(core_base2, core_no, pkg_no);
2413
2414 p = GET_PKG(pkg_base, pkg_no);
2415 p2 = GET_PKG(pkg_base2, pkg_no);
2416
2417 retval = func(t, c, p, t2, c2, p2);
2418 if (retval)
2419 return retval;
2420 }
2421 }
2422 }
2423 return 0;
2424}
2425
2426/*
2427 * run func(cpu) on every cpu in /proc/stat
2428 * return max_cpu number
2429 */
2430int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002431{
2432 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002433 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002434 int retval;
2435
Josh Triplett57a42a32013-08-20 17:20:17 -07002436 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002437
2438 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002439 if (retval != 0)
2440 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002441
Len Brownc98d5d92012-06-04 00:56:40 -04002442 while (1) {
2443 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 -04002444 if (retval != 1)
2445 break;
2446
Len Brownc98d5d92012-06-04 00:56:40 -04002447 retval = func(cpu_num);
2448 if (retval) {
2449 fclose(fp);
2450 return(retval);
2451 }
Len Brown103a8fe2010-10-22 23:53:03 -04002452 }
2453 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002454 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002455}
2456
2457void re_initialize(void)
2458{
Len Brownc98d5d92012-06-04 00:56:40 -04002459 free_all_buffers();
2460 setup_all_buffers();
2461 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002462}
2463
Len Brownc98d5d92012-06-04 00:56:40 -04002464
Len Brown103a8fe2010-10-22 23:53:03 -04002465/*
Len Brownc98d5d92012-06-04 00:56:40 -04002466 * count_cpus()
2467 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002468 */
Len Brownc98d5d92012-06-04 00:56:40 -04002469int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002470{
Len Brownc98d5d92012-06-04 00:56:40 -04002471 if (topo.max_cpu_num < cpu)
2472 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002473
Len Brownc98d5d92012-06-04 00:56:40 -04002474 topo.num_cpus += 1;
2475 return 0;
2476}
2477int mark_cpu_present(int cpu)
2478{
2479 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002480 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002481}
2482
Len Brown562a2d32016-02-26 23:48:05 -05002483/*
2484 * snapshot_proc_interrupts()
2485 *
2486 * read and record summary of /proc/interrupts
2487 *
2488 * return 1 if config change requires a restart, else return 0
2489 */
2490int snapshot_proc_interrupts(void)
2491{
2492 static FILE *fp;
2493 int column, retval;
2494
2495 if (fp == NULL)
2496 fp = fopen_or_die("/proc/interrupts", "r");
2497 else
2498 rewind(fp);
2499
2500 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2501 for (column = 0; column < topo.num_cpus; ++column) {
2502 int cpu_number;
2503
2504 retval = fscanf(fp, " CPU%d", &cpu_number);
2505 if (retval != 1)
2506 break;
2507
2508 if (cpu_number > topo.max_cpu_num) {
2509 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2510 return 1;
2511 }
2512
2513 irq_column_2_cpu[column] = cpu_number;
2514 irqs_per_cpu[cpu_number] = 0;
2515 }
2516
2517 /* read /proc/interrupt count lines and sum up irqs per cpu */
2518 while (1) {
2519 int column;
2520 char buf[64];
2521
2522 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2523 if (retval != 1)
2524 break;
2525
2526 /* read the count per cpu */
2527 for (column = 0; column < topo.num_cpus; ++column) {
2528
2529 int cpu_number, irq_count;
2530
2531 retval = fscanf(fp, " %d", &irq_count);
2532 if (retval != 1)
2533 break;
2534
2535 cpu_number = irq_column_2_cpu[column];
2536 irqs_per_cpu[cpu_number] += irq_count;
2537
2538 }
2539
2540 while (getc(fp) != '\n')
2541 ; /* flush interrupt description */
2542
2543 }
2544 return 0;
2545}
Len Brown27d47352016-02-27 00:37:54 -05002546/*
Len Brownfdf676e2016-02-27 01:28:12 -05002547 * snapshot_gfx_rc6_ms()
2548 *
2549 * record snapshot of
2550 * /sys/class/drm/card0/power/rc6_residency_ms
2551 *
2552 * return 1 if config change requires a restart, else return 0
2553 */
2554int snapshot_gfx_rc6_ms(void)
2555{
2556 FILE *fp;
2557 int retval;
2558
2559 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2560
2561 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2562 if (retval != 1)
2563 err(1, "GFX rc6");
2564
2565 fclose(fp);
2566
2567 return 0;
2568}
2569/*
Len Brown27d47352016-02-27 00:37:54 -05002570 * snapshot_gfx_mhz()
2571 *
2572 * record snapshot of
2573 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2574 *
2575 * return 1 if config change requires a restart, else return 0
2576 */
2577int snapshot_gfx_mhz(void)
2578{
2579 static FILE *fp;
2580 int retval;
2581
2582 if (fp == NULL)
2583 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002584 else {
Len Brown27d47352016-02-27 00:37:54 -05002585 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002586 fflush(fp);
2587 }
Len Brown27d47352016-02-27 00:37:54 -05002588
2589 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2590 if (retval != 1)
2591 err(1, "GFX MHz");
2592
2593 return 0;
2594}
Len Brown562a2d32016-02-26 23:48:05 -05002595
2596/*
2597 * snapshot /proc and /sys files
2598 *
2599 * return 1 if configuration restart needed, else return 0
2600 */
2601int snapshot_proc_sysfs_files(void)
2602{
Len Brown218f0e82017-02-14 22:07:52 -05002603 if (DO_BIC(BIC_IRQ))
2604 if (snapshot_proc_interrupts())
2605 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002606
Len Brown812db3f2017-02-10 00:25:41 -05002607 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002608 snapshot_gfx_rc6_ms();
2609
Len Brown812db3f2017-02-10 00:25:41 -05002610 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002611 snapshot_gfx_mhz();
2612
Len Brown562a2d32016-02-26 23:48:05 -05002613 return 0;
2614}
2615
Len Brown8aa2ed02017-07-15 14:57:37 -04002616int exit_requested;
2617
2618static void signal_handler (int signal)
2619{
2620 switch (signal) {
2621 case SIGINT:
2622 exit_requested = 1;
2623 if (debug)
2624 fprintf(stderr, " SIGINT\n");
2625 break;
Len Brown07211962017-07-15 15:51:26 -04002626 case SIGUSR1:
2627 if (debug > 1)
2628 fprintf(stderr, "SIGUSR1\n");
2629 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002630 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002631 /* make sure this manually-invoked interval is at least 1ms long */
2632 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002633}
2634
2635void setup_signal_handler(void)
2636{
2637 struct sigaction sa;
2638
2639 memset(&sa, 0, sizeof(sa));
2640
2641 sa.sa_handler = &signal_handler;
2642
2643 if (sigaction(SIGINT, &sa, NULL) < 0)
2644 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002645 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2646 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002647}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002648
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002649void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002650{
2651 struct timeval select_timeout;
2652 fd_set readfds;
2653 int retval;
2654
2655 FD_ZERO(&readfds);
2656 FD_SET(0, &readfds);
2657
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002658 if (!isatty(fileno(stdin))) {
2659 nanosleep(&interval_ts, NULL);
2660 return;
2661 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002662
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002663 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002664 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2665
2666 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002667 switch (getc(stdin)) {
2668 case 'q':
2669 exit_requested = 1;
2670 break;
2671 }
2672 /* make sure this manually-invoked interval is at least 1ms long */
2673 nanosleep(&one_msec, NULL);
2674 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002675}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002676
Len Brown103a8fe2010-10-22 23:53:03 -04002677void turbostat_loop()
2678{
Len Brownc98d5d92012-06-04 00:56:40 -04002679 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002680 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002681
Len Brown8aa2ed02017-07-15 14:57:37 -04002682 setup_signal_handler();
2683
Len Brown103a8fe2010-10-22 23:53:03 -04002684restart:
Len Browne52966c2012-11-08 22:38:05 -05002685 restarted++;
2686
Len Brown562a2d32016-02-26 23:48:05 -05002687 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002688 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002689 if (retval < -1) {
2690 exit(retval);
2691 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002692 if (restarted > 1) {
2693 exit(retval);
2694 }
Len Brownc98d5d92012-06-04 00:56:40 -04002695 re_initialize();
2696 goto restart;
2697 }
Len Browne52966c2012-11-08 22:38:05 -05002698 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002699 gettimeofday(&tv_even, (struct timezone *)NULL);
2700
2701 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002702 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002703 re_initialize();
2704 goto restart;
2705 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002706 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002707 if (snapshot_proc_sysfs_files())
2708 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002709 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002710 if (retval < -1) {
2711 exit(retval);
2712 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002713 re_initialize();
2714 goto restart;
2715 }
Len Brown103a8fe2010-10-22 23:53:03 -04002716 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002717 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002718 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2719 re_initialize();
2720 goto restart;
2721 }
Len Brownc98d5d92012-06-04 00:56:40 -04002722 compute_average(EVEN_COUNTERS);
2723 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002724 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002725 if (exit_requested)
2726 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002727 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002728 if (snapshot_proc_sysfs_files())
2729 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002730 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002731 if (retval < -1) {
2732 exit(retval);
2733 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002734 re_initialize();
2735 goto restart;
2736 }
Len Brown103a8fe2010-10-22 23:53:03 -04002737 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002738 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002739 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2740 re_initialize();
2741 goto restart;
2742 }
Len Brownc98d5d92012-06-04 00:56:40 -04002743 compute_average(ODD_COUNTERS);
2744 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002745 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002746 if (exit_requested)
2747 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002748 }
2749}
2750
2751void check_dev_msr()
2752{
2753 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002754 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002755
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002756 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2757 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002758 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2759 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002760}
2761
Len Brown98481e72014-08-15 00:36:50 -04002762void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002763{
Len Brown98481e72014-08-15 00:36:50 -04002764 struct __user_cap_header_struct cap_header_data;
2765 cap_user_header_t cap_header = &cap_header_data;
2766 struct __user_cap_data_struct cap_data_data;
2767 cap_user_data_t cap_data = &cap_data_data;
2768 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2769 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002770 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002771
2772 /* check for CAP_SYS_RAWIO */
2773 cap_header->pid = getpid();
2774 cap_header->version = _LINUX_CAPABILITY_VERSION;
2775 if (capget(cap_header, cap_data) < 0)
2776 err(-6, "capget(2) failed");
2777
2778 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2779 do_exit++;
2780 warnx("capget(CAP_SYS_RAWIO) failed,"
2781 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2782 }
2783
2784 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002785 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2786 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002787 do_exit++;
2788 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2789 }
2790
2791 /* if all else fails, thell them to be root */
2792 if (do_exit)
2793 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002794 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002795
2796 if (do_exit)
2797 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002798}
2799
Len Brownd7899442015-01-23 00:12:33 -05002800/*
2801 * NHM adds support for additional MSRs:
2802 *
2803 * MSR_SMI_COUNT 0x00000034
2804 *
Len Brownec0adc52015-11-12 02:42:31 -05002805 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002806 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002807 *
Len Browncf4cbe52017-01-01 13:08:33 -05002808 * MSR_MISC_PWR_MGMT 0x000001aa
2809 *
Len Brownd7899442015-01-23 00:12:33 -05002810 * MSR_PKG_C3_RESIDENCY 0x000003f8
2811 * MSR_PKG_C6_RESIDENCY 0x000003f9
2812 * MSR_CORE_C3_RESIDENCY 0x000003fc
2813 * MSR_CORE_C6_RESIDENCY 0x000003fd
2814 *
Len Brownee7e38e2015-02-09 23:39:45 -05002815 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002816 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002817 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002818 */
Len Brownee7e38e2015-02-09 23:39:45 -05002819int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002820{
Len Brownee7e38e2015-02-09 23:39:45 -05002821 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002822 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002823 int *pkg_cstate_limits;
2824
Len Brown103a8fe2010-10-22 23:53:03 -04002825 if (!genuine_intel)
2826 return 0;
2827
2828 if (family != 6)
2829 return 0;
2830
Len Brown21ed5572015-10-19 22:37:40 -04002831 bclk = discover_bclk(family, model);
2832
Len Brown103a8fe2010-10-22 23:53:03 -04002833 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002834 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2835 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002836 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce69e2016-06-16 23:22:37 -04002837 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2838 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2839 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2840 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002841 pkg_cstate_limits = nhm_pkg_cstate_limits;
2842 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002843 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2844 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2845 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2846 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002847 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002848 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002849 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002850 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2851 case INTEL_FAM6_HASWELL_X: /* HSX */
2852 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2853 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2854 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2855 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2856 case INTEL_FAM6_BROADWELL_X: /* BDX */
2857 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2858 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2859 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2860 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2861 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002862 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002863 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002864 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002865 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2866 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002867 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002868 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002869 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002870 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04002871 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002872 pkg_cstate_limits = slv_pkg_cstate_limits;
2873 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002874 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002875 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002876 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002877 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002878 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002879 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002880 pkg_cstate_limits = phi_pkg_cstate_limits;
2881 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002882 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002883 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce69e2016-06-16 23:22:37 -04002884 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002885 pkg_cstate_limits = bxt_pkg_cstate_limits;
2886 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002887 default:
2888 return 0;
2889 }
Len Brown1df2e552017-01-07 23:24:57 -05002890 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002891 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002892
Len Brownec0adc52015-11-12 02:42:31 -05002893 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002894 base_ratio = (msr >> 8) & 0xFF;
2895
2896 base_hz = base_ratio * bclk * 1000000;
2897 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002898 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002899}
Len Brown0f7887c2017-01-12 23:49:18 -05002900/*
Len Brown495c76542017-02-08 02:41:51 -05002901 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002902 *
2903 * MSR_CC6_DEMOTION_POLICY_CONFIG
2904 * MSR_MC6_DEMOTION_POLICY_CONFIG
2905 */
2906
2907int has_slv_msrs(unsigned int family, unsigned int model)
2908{
2909 if (!genuine_intel)
2910 return 0;
2911
2912 switch (model) {
2913 case INTEL_FAM6_ATOM_SILVERMONT1:
2914 case INTEL_FAM6_ATOM_MERRIFIELD:
2915 case INTEL_FAM6_ATOM_MOOREFIELD:
2916 return 1;
2917 }
2918 return 0;
2919}
Len Brown7170a372017-01-27 02:13:27 -05002920int is_dnv(unsigned int family, unsigned int model)
2921{
2922
2923 if (!genuine_intel)
2924 return 0;
2925
2926 switch (model) {
2927 case INTEL_FAM6_ATOM_DENVERTON:
2928 return 1;
2929 }
2930 return 0;
2931}
Len Brownade0eba2017-02-10 01:56:47 -05002932int is_bdx(unsigned int family, unsigned int model)
2933{
2934
2935 if (!genuine_intel)
2936 return 0;
2937
2938 switch (model) {
2939 case INTEL_FAM6_BROADWELL_X:
2940 case INTEL_FAM6_BROADWELL_XEON_D:
2941 return 1;
2942 }
2943 return 0;
2944}
Len Brown34c761972017-01-27 02:36:41 -05002945int is_skx(unsigned int family, unsigned int model)
2946{
2947
2948 if (!genuine_intel)
2949 return 0;
2950
2951 switch (model) {
2952 case INTEL_FAM6_SKYLAKE_X:
2953 return 1;
2954 }
2955 return 0;
2956}
Len Brown0f7887c2017-01-12 23:49:18 -05002957
Len Brown31e07522017-01-31 23:07:49 -05002958int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002959{
Len Brown0f7887c2017-01-12 23:49:18 -05002960 if (has_slv_msrs(family, model))
2961 return 0;
2962
Len Brownd7899442015-01-23 00:12:33 -05002963 switch (model) {
2964 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce69e2016-06-16 23:22:37 -04002965 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2966 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2967 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002968 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002969 return 0;
2970 default:
2971 return 1;
2972 }
2973}
Len Brown0f7887c2017-01-12 23:49:18 -05002974int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2975{
2976 if (has_slv_msrs(family, model))
2977 return 1;
2978
2979 return 0;
2980}
Len Brown6574a5d2012-09-21 00:01:31 -04002981int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2982{
2983 if (!genuine_intel)
2984 return 0;
2985
2986 if (family != 6)
2987 return 0;
2988
2989 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002990 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2991 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002992 return 1;
2993 default:
2994 return 0;
2995 }
2996}
Len Brownfcd17212015-03-23 20:29:09 -04002997int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2998{
2999 if (!genuine_intel)
3000 return 0;
3001
3002 if (family != 6)
3003 return 0;
3004
3005 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003006 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003007 return 1;
3008 default:
3009 return 0;
3010 }
3011}
3012
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003013int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3014{
3015 if (!genuine_intel)
3016 return 0;
3017
3018 if (family != 6)
3019 return 0;
3020
3021 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003022 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003023 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003024 return 1;
3025 default:
3026 return 0;
3027 }
3028}
Len Brown31e07522017-01-31 23:07:49 -05003029int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3030{
3031 if (!genuine_intel)
3032 return 0;
3033
3034 if (family != 6)
3035 return 0;
3036
3037 switch (model) {
3038 case INTEL_FAM6_ATOM_GOLDMONT:
3039 case INTEL_FAM6_SKYLAKE_X:
3040 return 1;
3041 default:
3042 return 0;
3043 }
3044}
Len Brown6fb31432015-06-17 16:23:45 -04003045int has_config_tdp(unsigned int family, unsigned int model)
3046{
3047 if (!genuine_intel)
3048 return 0;
3049
3050 if (family != 6)
3051 return 0;
3052
3053 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003054 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3055 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3056 case INTEL_FAM6_HASWELL_X: /* HSX */
3057 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3058 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3059 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3060 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3061 case INTEL_FAM6_BROADWELL_X: /* BDX */
3062 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3063 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3064 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3065 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3066 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3067 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003068
Len Brown869ce69e2016-06-16 23:22:37 -04003069 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003070 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003071 return 1;
3072 default:
3073 return 0;
3074 }
3075}
3076
Len Brownfcd17212015-03-23 20:29:09 -04003077static void
Colin Ian King1b693172016-03-02 13:50:25 +00003078dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003079{
3080 if (!do_nhm_platform_info)
3081 return;
3082
3083 dump_nhm_platform_info();
3084
3085 if (has_hsw_turbo_ratio_limit(family, model))
3086 dump_hsw_turbo_ratio_limits();
3087
3088 if (has_ivt_turbo_ratio_limit(family, model))
3089 dump_ivt_turbo_ratio_limits();
3090
Len Brown31e07522017-01-31 23:07:49 -05003091 if (has_turbo_ratio_limit(family, model))
3092 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003093
Len Brown0f7887c2017-01-12 23:49:18 -05003094 if (has_atom_turbo_ratio_limit(family, model))
3095 dump_atom_turbo_ratio_limits();
3096
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003097 if (has_knl_turbo_ratio_limit(family, model))
3098 dump_knl_turbo_ratio_limits();
3099
Len Brown6fb31432015-06-17 16:23:45 -04003100 if (has_config_tdp(family, model))
3101 dump_config_tdp();
3102
Len Brownfcd17212015-03-23 20:29:09 -04003103 dump_nhm_cst_cfg();
3104}
3105
Len Brown41618e62017-02-09 18:25:22 -05003106static void
3107dump_sysfs_cstate_config(void)
3108{
3109 char path[64];
3110 char name_buf[16];
3111 char desc[64];
3112 FILE *input;
3113 int state;
3114 char *sp;
3115
3116 if (!DO_BIC(BIC_sysfs))
3117 return;
3118
3119 for (state = 0; state < 10; ++state) {
3120
3121 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3122 base_cpu, state);
3123 input = fopen(path, "r");
3124 if (input == NULL)
3125 continue;
3126 fgets(name_buf, sizeof(name_buf), input);
3127
3128 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3129 sp = strchr(name_buf, '-');
3130 if (!sp)
3131 sp = strchrnul(name_buf, '\n');
3132 *sp = '\0';
3133
3134 fclose(input);
3135
3136 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3137 base_cpu, state);
3138 input = fopen(path, "r");
3139 if (input == NULL)
3140 continue;
3141 fgets(desc, sizeof(desc), input);
3142
3143 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3144 fclose(input);
3145 }
3146}
Len Brown7293fcc2017-02-22 00:11:12 -05003147static void
3148dump_sysfs_pstate_config(void)
3149{
3150 char path[64];
3151 char driver_buf[64];
3152 char governor_buf[64];
3153 FILE *input;
3154 int turbo;
3155
3156 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3157 base_cpu);
3158 input = fopen(path, "r");
3159 if (input == NULL) {
3160 fprintf(stderr, "NSFOD %s\n", path);
3161 return;
3162 }
3163 fgets(driver_buf, sizeof(driver_buf), input);
3164 fclose(input);
3165
3166 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3167 base_cpu);
3168 input = fopen(path, "r");
3169 if (input == NULL) {
3170 fprintf(stderr, "NSFOD %s\n", path);
3171 return;
3172 }
3173 fgets(governor_buf, sizeof(governor_buf), input);
3174 fclose(input);
3175
3176 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3177 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3178
3179 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3180 input = fopen(path, "r");
3181 if (input != NULL) {
3182 fscanf(input, "%d", &turbo);
3183 fprintf(outf, "cpufreq boost: %d\n", turbo);
3184 fclose(input);
3185 }
3186
3187 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3188 input = fopen(path, "r");
3189 if (input != NULL) {
3190 fscanf(input, "%d", &turbo);
3191 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3192 fclose(input);
3193 }
3194}
Len Brown41618e62017-02-09 18:25:22 -05003195
Len Brown6574a5d2012-09-21 00:01:31 -04003196
Len Brown889facb2012-11-08 00:48:57 -05003197/*
3198 * print_epb()
3199 * Decode the ENERGY_PERF_BIAS MSR
3200 */
3201int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3202{
3203 unsigned long long msr;
3204 char *epb_string;
3205 int cpu;
3206
3207 if (!has_epb)
3208 return 0;
3209
3210 cpu = t->cpu_id;
3211
3212 /* EPB is per-package */
3213 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3214 return 0;
3215
3216 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003217 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003218 return -1;
3219 }
3220
3221 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3222 return 0;
3223
Len Browne9be7dd2015-05-26 12:19:37 -04003224 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003225 case ENERGY_PERF_BIAS_PERFORMANCE:
3226 epb_string = "performance";
3227 break;
3228 case ENERGY_PERF_BIAS_NORMAL:
3229 epb_string = "balanced";
3230 break;
3231 case ENERGY_PERF_BIAS_POWERSAVE:
3232 epb_string = "powersave";
3233 break;
3234 default:
3235 epb_string = "custom";
3236 break;
3237 }
Len Brownb7d8c142016-02-13 23:36:17 -05003238 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003239
3240 return 0;
3241}
Len Brown7f5c2582015-12-01 01:36:39 -05003242/*
3243 * print_hwp()
3244 * Decode the MSR_HWP_CAPABILITIES
3245 */
3246int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3247{
3248 unsigned long long msr;
3249 int cpu;
3250
3251 if (!has_hwp)
3252 return 0;
3253
3254 cpu = t->cpu_id;
3255
3256 /* MSR_HWP_CAPABILITIES is per-package */
3257 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3258 return 0;
3259
3260 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003261 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003262 return -1;
3263 }
3264
3265 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3266 return 0;
3267
Len Brownb7d8c142016-02-13 23:36:17 -05003268 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003269 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3270
3271 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3272 if ((msr & (1 << 0)) == 0)
3273 return 0;
3274
3275 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3276 return 0;
3277
Len Brownb7d8c142016-02-13 23:36:17 -05003278 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003279 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003280 cpu, msr,
3281 (unsigned int)HWP_HIGHEST_PERF(msr),
3282 (unsigned int)HWP_GUARANTEED_PERF(msr),
3283 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3284 (unsigned int)HWP_LOWEST_PERF(msr));
3285
3286 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3287 return 0;
3288
Len Brownb7d8c142016-02-13 23:36:17 -05003289 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003290 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003291 cpu, msr,
3292 (unsigned int)(((msr) >> 0) & 0xff),
3293 (unsigned int)(((msr) >> 8) & 0xff),
3294 (unsigned int)(((msr) >> 16) & 0xff),
3295 (unsigned int)(((msr) >> 24) & 0xff),
3296 (unsigned int)(((msr) >> 32) & 0xff3),
3297 (unsigned int)(((msr) >> 42) & 0x1));
3298
3299 if (has_hwp_pkg) {
3300 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3301 return 0;
3302
Len Brownb7d8c142016-02-13 23:36:17 -05003303 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003304 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003305 cpu, msr,
3306 (unsigned int)(((msr) >> 0) & 0xff),
3307 (unsigned int)(((msr) >> 8) & 0xff),
3308 (unsigned int)(((msr) >> 16) & 0xff),
3309 (unsigned int)(((msr) >> 24) & 0xff),
3310 (unsigned int)(((msr) >> 32) & 0xff3));
3311 }
3312 if (has_hwp_notify) {
3313 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3314 return 0;
3315
Len Brownb7d8c142016-02-13 23:36:17 -05003316 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003317 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3318 cpu, msr,
3319 ((msr) & 0x1) ? "EN" : "Dis",
3320 ((msr) & 0x2) ? "EN" : "Dis");
3321 }
3322 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3323 return 0;
3324
Len Brownb7d8c142016-02-13 23:36:17 -05003325 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003326 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3327 cpu, msr,
3328 ((msr) & 0x1) ? "" : "No-",
3329 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003330
3331 return 0;
3332}
3333
Len Brown3a9a9412014-08-15 02:39:52 -04003334/*
3335 * print_perf_limit()
3336 */
3337int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3338{
3339 unsigned long long msr;
3340 int cpu;
3341
3342 cpu = t->cpu_id;
3343
3344 /* per-package */
3345 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3346 return 0;
3347
3348 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003349 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003350 return -1;
3351 }
3352
3353 if (do_core_perf_limit_reasons) {
3354 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003355 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3356 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003357 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003358 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003359 (msr & 1 << 13) ? "Transitions, " : "",
3360 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3361 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3362 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3363 (msr & 1 << 9) ? "CorePwr, " : "",
3364 (msr & 1 << 8) ? "Amps, " : "",
3365 (msr & 1 << 6) ? "VR-Therm, " : "",
3366 (msr & 1 << 5) ? "Auto-HWP, " : "",
3367 (msr & 1 << 4) ? "Graphics, " : "",
3368 (msr & 1 << 2) ? "bit2, " : "",
3369 (msr & 1 << 1) ? "ThermStatus, " : "",
3370 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003371 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 -04003372 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003373 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003374 (msr & 1 << 29) ? "Transitions, " : "",
3375 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3376 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3377 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3378 (msr & 1 << 25) ? "CorePwr, " : "",
3379 (msr & 1 << 24) ? "Amps, " : "",
3380 (msr & 1 << 22) ? "VR-Therm, " : "",
3381 (msr & 1 << 21) ? "Auto-HWP, " : "",
3382 (msr & 1 << 20) ? "Graphics, " : "",
3383 (msr & 1 << 18) ? "bit18, " : "",
3384 (msr & 1 << 17) ? "ThermStatus, " : "",
3385 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003386
3387 }
3388 if (do_gfx_perf_limit_reasons) {
3389 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003390 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3391 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003392 (msr & 1 << 0) ? "PROCHOT, " : "",
3393 (msr & 1 << 1) ? "ThermStatus, " : "",
3394 (msr & 1 << 4) ? "Graphics, " : "",
3395 (msr & 1 << 6) ? "VR-Therm, " : "",
3396 (msr & 1 << 8) ? "Amps, " : "",
3397 (msr & 1 << 9) ? "GFXPwr, " : "",
3398 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3399 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003400 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003401 (msr & 1 << 16) ? "PROCHOT, " : "",
3402 (msr & 1 << 17) ? "ThermStatus, " : "",
3403 (msr & 1 << 20) ? "Graphics, " : "",
3404 (msr & 1 << 22) ? "VR-Therm, " : "",
3405 (msr & 1 << 24) ? "Amps, " : "",
3406 (msr & 1 << 25) ? "GFXPwr, " : "",
3407 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3408 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3409 }
3410 if (do_ring_perf_limit_reasons) {
3411 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003412 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3413 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003414 (msr & 1 << 0) ? "PROCHOT, " : "",
3415 (msr & 1 << 1) ? "ThermStatus, " : "",
3416 (msr & 1 << 6) ? "VR-Therm, " : "",
3417 (msr & 1 << 8) ? "Amps, " : "",
3418 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3419 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003420 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003421 (msr & 1 << 16) ? "PROCHOT, " : "",
3422 (msr & 1 << 17) ? "ThermStatus, " : "",
3423 (msr & 1 << 22) ? "VR-Therm, " : "",
3424 (msr & 1 << 24) ? "Amps, " : "",
3425 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3426 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3427 }
3428 return 0;
3429}
3430
Len Brown889facb2012-11-08 00:48:57 -05003431#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3432#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3433
Colin Ian King1b693172016-03-02 13:50:25 +00003434double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003435{
3436 unsigned long long msr;
3437
3438 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003439 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003440 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3441
3442 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003443 case INTEL_FAM6_ATOM_SILVERMONT1:
3444 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003445 return 30.0;
3446 default:
3447 return 135.0;
3448 }
3449}
3450
Andrey Semin40ee8e32014-12-05 00:07:00 -05003451/*
3452 * rapl_dram_energy_units_probe()
3453 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3454 */
3455static double
3456rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3457{
3458 /* only called for genuine_intel, family 6 */
3459
3460 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003461 case INTEL_FAM6_HASWELL_X: /* HSX */
3462 case INTEL_FAM6_BROADWELL_X: /* BDX */
3463 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3464 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003465 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003466 return (rapl_dram_energy_units = 15.3 / 1000000);
3467 default:
3468 return (rapl_energy_units);
3469 }
3470}
3471
Len Brown144b44b2013-11-09 00:30:16 -05003472
Len Brown889facb2012-11-08 00:48:57 -05003473/*
3474 * rapl_probe()
3475 *
Len Brown144b44b2013-11-09 00:30:16 -05003476 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003477 */
3478void rapl_probe(unsigned int family, unsigned int model)
3479{
3480 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003481 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003482 double tdp;
3483
3484 if (!genuine_intel)
3485 return;
3486
3487 if (family != 6)
3488 return;
3489
3490 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003491 case INTEL_FAM6_SANDYBRIDGE:
3492 case INTEL_FAM6_IVYBRIDGE:
3493 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3494 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3495 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3496 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3497 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003498 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003499 if (rapl_joules) {
3500 BIC_PRESENT(BIC_Pkg_J);
3501 BIC_PRESENT(BIC_Cor_J);
3502 BIC_PRESENT(BIC_GFX_J);
3503 } else {
3504 BIC_PRESENT(BIC_PkgWatt);
3505 BIC_PRESENT(BIC_CorWatt);
3506 BIC_PRESENT(BIC_GFXWatt);
3507 }
Len Brown889facb2012-11-08 00:48:57 -05003508 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003509 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003510 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003511 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003512 if (rapl_joules)
3513 BIC_PRESENT(BIC_Pkg_J);
3514 else
3515 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003516 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003517 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3518 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3519 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3520 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown81824922017-03-04 17:23:07 -05003521 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 -05003522 BIC_PRESENT(BIC_PKG__);
3523 BIC_PRESENT(BIC_RAM__);
3524 if (rapl_joules) {
3525 BIC_PRESENT(BIC_Pkg_J);
3526 BIC_PRESENT(BIC_Cor_J);
3527 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003528 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003529 } else {
3530 BIC_PRESENT(BIC_PkgWatt);
3531 BIC_PRESENT(BIC_CorWatt);
3532 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003533 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003534 }
Len Brown0b2bb692015-03-26 00:50:30 -04003535 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003536 case INTEL_FAM6_HASWELL_X: /* HSX */
3537 case INTEL_FAM6_BROADWELL_X: /* BDX */
3538 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3539 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3540 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003541 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003542 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 -05003543 BIC_PRESENT(BIC_PKG__);
3544 BIC_PRESENT(BIC_RAM__);
3545 if (rapl_joules) {
3546 BIC_PRESENT(BIC_Pkg_J);
3547 BIC_PRESENT(BIC_RAM_J);
3548 } else {
3549 BIC_PRESENT(BIC_PkgWatt);
3550 BIC_PRESENT(BIC_RAMWatt);
3551 }
Len Browne6f9bb32013-12-03 02:19:19 -05003552 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003553 case INTEL_FAM6_SANDYBRIDGE_X:
3554 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003555 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 -05003556 BIC_PRESENT(BIC_PKG__);
3557 BIC_PRESENT(BIC_RAM__);
3558 if (rapl_joules) {
3559 BIC_PRESENT(BIC_Pkg_J);
3560 BIC_PRESENT(BIC_Cor_J);
3561 BIC_PRESENT(BIC_RAM_J);
3562 } else {
3563 BIC_PRESENT(BIC_PkgWatt);
3564 BIC_PRESENT(BIC_CorWatt);
3565 BIC_PRESENT(BIC_RAMWatt);
3566 }
Len Brown144b44b2013-11-09 00:30:16 -05003567 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003568 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3569 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003570 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003571 if (rapl_joules) {
3572 BIC_PRESENT(BIC_Pkg_J);
3573 BIC_PRESENT(BIC_Cor_J);
3574 } else {
3575 BIC_PRESENT(BIC_PkgWatt);
3576 BIC_PRESENT(BIC_CorWatt);
3577 }
Len Brown889facb2012-11-08 00:48:57 -05003578 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003579 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003580 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 -05003581 BIC_PRESENT(BIC_PKG__);
3582 BIC_PRESENT(BIC_RAM__);
3583 if (rapl_joules) {
3584 BIC_PRESENT(BIC_Pkg_J);
3585 BIC_PRESENT(BIC_Cor_J);
3586 BIC_PRESENT(BIC_RAM_J);
3587 } else {
3588 BIC_PRESENT(BIC_PkgWatt);
3589 BIC_PRESENT(BIC_CorWatt);
3590 BIC_PRESENT(BIC_RAMWatt);
3591 }
Jacob Pan0f644902016-06-16 09:48:22 -07003592 break;
Len Brown889facb2012-11-08 00:48:57 -05003593 default:
3594 return;
3595 }
3596
3597 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003598 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003599 return;
3600
3601 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce69e2016-06-16 23:22:37 -04003602 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003603 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3604 else
3605 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003606
Andrey Semin40ee8e32014-12-05 00:07:00 -05003607 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3608
Len Brown144b44b2013-11-09 00:30:16 -05003609 time_unit = msr >> 16 & 0xF;
3610 if (time_unit == 0)
3611 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003612
Len Brown144b44b2013-11-09 00:30:16 -05003613 rapl_time_units = 1.0 / (1 << (time_unit));
3614
3615 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003616
3617 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003618 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003619 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003620
3621 return;
3622}
3623
Colin Ian King1b693172016-03-02 13:50:25 +00003624void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003625{
3626 if (!genuine_intel)
3627 return;
3628
3629 if (family != 6)
3630 return;
3631
3632 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003633 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3634 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3635 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003636 do_gfx_perf_limit_reasons = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04003637 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003638 do_core_perf_limit_reasons = 1;
3639 do_ring_perf_limit_reasons = 1;
3640 default:
3641 return;
3642 }
3643}
3644
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003645void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3646{
3647 if (is_skx(family, model) || is_bdx(family, model))
3648 has_automatic_cstate_conversion = 1;
3649}
3650
Len Brown889facb2012-11-08 00:48:57 -05003651int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3652{
3653 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003654 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003655 int cpu;
3656
3657 if (!(do_dts || do_ptm))
3658 return 0;
3659
3660 cpu = t->cpu_id;
3661
3662 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003663 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003664 return 0;
3665
3666 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003667 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003668 return -1;
3669 }
3670
3671 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3672 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3673 return 0;
3674
3675 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003676 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003677 cpu, msr, tcc_activation_temp - dts);
3678
Len Brown889facb2012-11-08 00:48:57 -05003679 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3680 return 0;
3681
3682 dts = (msr >> 16) & 0x7F;
3683 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003684 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003685 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003686 }
3687
3688
Len Brownf4896fa52017-03-04 18:10:45 -05003689 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003690 unsigned int resolution;
3691
3692 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3693 return 0;
3694
3695 dts = (msr >> 16) & 0x7F;
3696 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003697 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003698 cpu, msr, tcc_activation_temp - dts, resolution);
3699
Len Brown889facb2012-11-08 00:48:57 -05003700 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3701 return 0;
3702
3703 dts = (msr >> 16) & 0x7F;
3704 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003705 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003706 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003707 }
3708
3709 return 0;
3710}
Len Brown36229892016-02-26 20:51:02 -05003711
Len Brown889facb2012-11-08 00:48:57 -05003712void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3713{
Len Brownb7d8c142016-02-13 23:36:17 -05003714 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003715 cpu, label,
3716 ((msr >> 15) & 1) ? "EN" : "DIS",
3717 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3718 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3719 (((msr >> 16) & 1) ? "EN" : "DIS"));
3720
3721 return;
3722}
3723
3724int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3725{
3726 unsigned long long msr;
3727 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003728
3729 if (!do_rapl)
3730 return 0;
3731
3732 /* RAPL counters are per package, so print only for 1st thread/package */
3733 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3734 return 0;
3735
3736 cpu = t->cpu_id;
3737 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003738 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003739 return -1;
3740 }
3741
3742 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3743 return -1;
3744
Len Brown96e47152017-01-21 02:26:00 -05003745 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3746 rapl_power_units, rapl_energy_units, rapl_time_units);
3747
Len Brown144b44b2013-11-09 00:30:16 -05003748 if (do_rapl & RAPL_PKG_POWER_INFO) {
3749
Len Brown889facb2012-11-08 00:48:57 -05003750 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3751 return -5;
3752
3753
Len Brownb7d8c142016-02-13 23:36:17 -05003754 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 -05003755 cpu, msr,
3756 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3757 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3758 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3759 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3760
Len Brown144b44b2013-11-09 00:30:16 -05003761 }
3762 if (do_rapl & RAPL_PKG) {
3763
Len Brown889facb2012-11-08 00:48:57 -05003764 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3765 return -9;
3766
Len Brownb7d8c142016-02-13 23:36:17 -05003767 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003768 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003769
3770 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003771 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003772 cpu,
3773 ((msr >> 47) & 1) ? "EN" : "DIS",
3774 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3775 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3776 ((msr >> 48) & 1) ? "EN" : "DIS");
3777 }
3778
Len Brown0b2bb692015-03-26 00:50:30 -04003779 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003780 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3781 return -6;
3782
Len Brownb7d8c142016-02-13 23:36:17 -05003783 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 -05003784 cpu, msr,
3785 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3786 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3787 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3788 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003789 }
3790 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003791 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3792 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003793 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003794 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003795
3796 print_power_limit_msr(cpu, msr, "DRAM Limit");
3797 }
Len Brown144b44b2013-11-09 00:30:16 -05003798 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003799 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3800 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003801
Len Brown96e47152017-01-21 02:26:00 -05003802 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003803 }
Jacob Pan91484942016-06-16 09:48:20 -07003804 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003805 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3806 return -9;
3807 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3808 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3809 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003810 }
3811 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003812 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3813 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003814
Len Brown96e47152017-01-21 02:26:00 -05003815 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003816
Len Brown96e47152017-01-21 02:26:00 -05003817 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3818 return -9;
3819 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3820 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3821 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003822 }
3823 return 0;
3824}
3825
Len Brownd7899442015-01-23 00:12:33 -05003826/*
3827 * SNB adds support for additional MSRs:
3828 *
3829 * MSR_PKG_C7_RESIDENCY 0x000003fa
3830 * MSR_CORE_C7_RESIDENCY 0x000003fe
3831 * MSR_PKG_C2_RESIDENCY 0x0000060d
3832 */
Len Brown103a8fe2010-10-22 23:53:03 -04003833
Len Brownd7899442015-01-23 00:12:33 -05003834int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003835{
3836 if (!genuine_intel)
3837 return 0;
3838
3839 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003840 case INTEL_FAM6_SANDYBRIDGE:
3841 case INTEL_FAM6_SANDYBRIDGE_X:
3842 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3843 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3844 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3845 case INTEL_FAM6_HASWELL_X: /* HSW */
3846 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3847 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3848 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3849 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3850 case INTEL_FAM6_BROADWELL_X: /* BDX */
3851 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3852 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3853 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3854 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3855 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3856 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3857 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003858 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003859 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003860 return 1;
3861 }
3862 return 0;
3863}
3864
Len Brownd7899442015-01-23 00:12:33 -05003865/*
3866 * HSW adds support for additional MSRs:
3867 *
Len Brown5a634262016-04-06 17:15:55 -04003868 * MSR_PKG_C8_RESIDENCY 0x00000630
3869 * MSR_PKG_C9_RESIDENCY 0x00000631
3870 * MSR_PKG_C10_RESIDENCY 0x00000632
3871 *
3872 * MSR_PKGC8_IRTL 0x00000633
3873 * MSR_PKGC9_IRTL 0x00000634
3874 * MSR_PKGC10_IRTL 0x00000635
3875 *
Len Brownd7899442015-01-23 00:12:33 -05003876 */
3877int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003878{
3879 if (!genuine_intel)
3880 return 0;
3881
3882 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003883 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3884 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3885 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3886 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3887 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3888 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3889 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003890 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003891 return 1;
3892 }
3893 return 0;
3894}
3895
Len Brown0b2bb692015-03-26 00:50:30 -04003896/*
3897 * SKL adds support for additional MSRS:
3898 *
3899 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3900 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3901 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3902 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3903 */
3904int has_skl_msrs(unsigned int family, unsigned int model)
3905{
3906 if (!genuine_intel)
3907 return 0;
3908
3909 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003910 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3911 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3912 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3913 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003914 return 1;
3915 }
3916 return 0;
3917}
3918
Len Brown144b44b2013-11-09 00:30:16 -05003919int is_slm(unsigned int family, unsigned int model)
3920{
3921 if (!genuine_intel)
3922 return 0;
3923 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003924 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3925 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003926 return 1;
3927 }
3928 return 0;
3929}
3930
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003931int is_knl(unsigned int family, unsigned int model)
3932{
3933 if (!genuine_intel)
3934 return 0;
3935 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003936 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003937 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003938 return 1;
3939 }
3940 return 0;
3941}
3942
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003943unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3944{
3945 if (is_knl(family, model))
3946 return 1024;
3947 return 1;
3948}
3949
Len Brown144b44b2013-11-09 00:30:16 -05003950#define SLM_BCLK_FREQS 5
3951double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3952
3953double slm_bclk(void)
3954{
3955 unsigned long long msr = 3;
3956 unsigned int i;
3957 double freq;
3958
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003959 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003960 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003961
3962 i = msr & 0xf;
3963 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003964 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003965 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003966 }
3967 freq = slm_freq_table[i];
3968
Len Brown96e47152017-01-21 02:26:00 -05003969 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003970 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003971
3972 return freq;
3973}
3974
Len Brown103a8fe2010-10-22 23:53:03 -04003975double discover_bclk(unsigned int family, unsigned int model)
3976{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003977 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003978 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003979 else if (is_slm(family, model))
3980 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003981 else
3982 return 133.33;
3983}
3984
Len Brown889facb2012-11-08 00:48:57 -05003985/*
3986 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3987 * the Thermal Control Circuit (TCC) activates.
3988 * This is usually equal to tjMax.
3989 *
3990 * Older processors do not have this MSR, so there we guess,
3991 * but also allow cmdline over-ride with -T.
3992 *
3993 * Several MSR temperature values are in units of degrees-C
3994 * below this value, including the Digital Thermal Sensor (DTS),
3995 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3996 */
3997int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3998{
3999 unsigned long long msr;
4000 unsigned int target_c_local;
4001 int cpu;
4002
4003 /* tcc_activation_temp is used only for dts or ptm */
4004 if (!(do_dts || do_ptm))
4005 return 0;
4006
4007 /* this is a per-package concept */
4008 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4009 return 0;
4010
4011 cpu = t->cpu_id;
4012 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004013 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004014 return -1;
4015 }
4016
4017 if (tcc_activation_temp_override != 0) {
4018 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004019 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004020 cpu, tcc_activation_temp);
4021 return 0;
4022 }
4023
4024 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004025 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004026 goto guess;
4027
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004028 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004029 goto guess;
4030
Jean Delvare34821242014-05-01 11:40:19 +02004031 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004032
Len Brown96e47152017-01-21 02:26:00 -05004033 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004034 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004035 cpu, msr, target_c_local);
4036
Jean Delvare34821242014-05-01 11:40:19 +02004037 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004038 goto guess;
4039
4040 tcc_activation_temp = target_c_local;
4041
4042 return 0;
4043
4044guess:
4045 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004046 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004047 cpu, tcc_activation_temp);
4048
4049 return 0;
4050}
Len Brown69807a62015-11-21 12:22:47 -05004051
Len Brownaa8d8cc2016-03-11 13:26:03 -05004052void decode_feature_control_msr(void)
4053{
4054 unsigned long long msr;
4055
4056 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4057 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4058 base_cpu, msr,
4059 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4060 msr & (1 << 18) ? "SGX" : "");
4061}
4062
Len Brown69807a62015-11-21 12:22:47 -05004063void decode_misc_enable_msr(void)
4064{
4065 unsigned long long msr;
4066
Len Brownf26b1512017-06-23 20:45:54 -07004067 if (!genuine_intel)
4068 return;
4069
Len Brown69807a62015-11-21 12:22:47 -05004070 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004071 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004072 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004073 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4074 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004075 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004076 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4077 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004078}
4079
Len Brown33148d62017-01-21 01:26:16 -05004080void decode_misc_feature_control(void)
4081{
4082 unsigned long long msr;
4083
4084 if (!has_misc_feature_control)
4085 return;
4086
4087 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4088 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4089 base_cpu, msr,
4090 msr & (0 << 0) ? "No-" : "",
4091 msr & (1 << 0) ? "No-" : "",
4092 msr & (2 << 0) ? "No-" : "",
4093 msr & (3 << 0) ? "No-" : "");
4094}
Len Brownf0057312015-12-03 01:35:36 -05004095/*
4096 * Decode MSR_MISC_PWR_MGMT
4097 *
4098 * Decode the bits according to the Nehalem documentation
4099 * bit[0] seems to continue to have same meaning going forward
4100 * bit[1] less so...
4101 */
4102void decode_misc_pwr_mgmt_msr(void)
4103{
4104 unsigned long long msr;
4105
4106 if (!do_nhm_platform_info)
4107 return;
4108
Len Browncf4cbe52017-01-01 13:08:33 -05004109 if (no_MSR_MISC_PWR_MGMT)
4110 return;
4111
Len Brownf0057312015-12-03 01:35:36 -05004112 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004113 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 -05004114 base_cpu, msr,
4115 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004116 msr & (1 << 1) ? "EN" : "DIS",
4117 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004118}
Len Brown71616c82017-01-07 22:37:48 -05004119/*
4120 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4121 *
4122 * This MSRs are present on Silvermont processors,
4123 * Intel Atom processor E3000 series (Baytrail), and friends.
4124 */
4125void decode_c6_demotion_policy_msr(void)
4126{
4127 unsigned long long msr;
4128
4129 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4130 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4131 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4132
4133 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4134 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4135 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4136}
Len Brown7f5c2582015-12-01 01:36:39 -05004137
Len Brownfcd17212015-03-23 20:29:09 -04004138void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004139{
Len Brown61a87ba2015-11-23 02:30:51 -05004140 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004141 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004142 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004143
4144 eax = ebx = ecx = edx = 0;
4145
Len Brown5aea2f72016-03-13 03:14:35 -04004146 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004147
4148 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4149 genuine_intel = 1;
4150
Len Brown96e47152017-01-21 02:26:00 -05004151 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004152 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004153 (char *)&ebx, (char *)&edx, (char *)&ecx);
4154
Len Brown5aea2f72016-03-13 03:14:35 -04004155 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004156 family = (fms >> 8) & 0xf;
4157 model = (fms >> 4) & 0xf;
4158 stepping = fms & 0xf;
4159 if (family == 6 || family == 0xf)
4160 model += ((fms >> 16) & 0xf) << 4;
4161
Len Brown96e47152017-01-21 02:26:00 -05004162 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004163 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004164 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004165 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004166 ecx & (1 << 0) ? "SSE3" : "-",
4167 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004168 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004169 ecx & (1 << 7) ? "EIST" : "-",
4170 ecx & (1 << 8) ? "TM2" : "-",
4171 edx & (1 << 4) ? "TSC" : "-",
4172 edx & (1 << 5) ? "MSR" : "-",
4173 edx & (1 << 22) ? "ACPI-TM" : "-",
4174 edx & (1 << 29) ? "TM" : "-");
4175 }
Len Brown103a8fe2010-10-22 23:53:03 -04004176
Josh Triplettb2c95d92013-08-20 17:20:18 -07004177 if (!(edx & (1 << 5)))
4178 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004179
4180 /*
4181 * check max extended function levels of CPUID.
4182 * This is needed to check for invariant TSC.
4183 * This check is valid for both Intel and AMD.
4184 */
4185 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004186 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004187
Len Brown61a87ba2015-11-23 02:30:51 -05004188 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004189
Len Brownd7899442015-01-23 00:12:33 -05004190 /*
4191 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4192 * this check is valid for both Intel and AMD
4193 */
Len Brown5aea2f72016-03-13 03:14:35 -04004194 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004195 has_invariant_tsc = edx & (1 << 8);
4196 }
Len Brown103a8fe2010-10-22 23:53:03 -04004197
4198 /*
4199 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4200 * this check is valid for both Intel and AMD
4201 */
4202
Len Brown5aea2f72016-03-13 03:14:35 -04004203 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004204 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004205 if (has_aperf) {
4206 BIC_PRESENT(BIC_Avg_MHz);
4207 BIC_PRESENT(BIC_Busy);
4208 BIC_PRESENT(BIC_Bzy_MHz);
4209 }
Len Brown889facb2012-11-08 00:48:57 -05004210 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004211 if (do_dts)
4212 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004213 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004214 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004215 if (do_ptm)
4216 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004217 has_hwp = eax & (1 << 7);
4218 has_hwp_notify = eax & (1 << 8);
4219 has_hwp_activity_window = eax & (1 << 9);
4220 has_hwp_epp = eax & (1 << 10);
4221 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004222 has_epb = ecx & (1 << 3);
4223
Len Brown96e47152017-01-21 02:26:00 -05004224 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004225 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004226 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4227 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004228 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004229 do_dts ? "" : "No-",
4230 do_ptm ? "" : "No-",
4231 has_hwp ? "" : "No-",
4232 has_hwp_notify ? "" : "No-",
4233 has_hwp_activity_window ? "" : "No-",
4234 has_hwp_epp ? "" : "No-",
4235 has_hwp_pkg ? "" : "No-",
4236 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004237
Len Brown96e47152017-01-21 02:26:00 -05004238 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004239 decode_misc_enable_msr();
4240
Len Brown33148d62017-01-21 01:26:16 -05004241
Len Brown96e47152017-01-21 02:26:00 -05004242 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004243 int has_sgx;
4244
4245 ecx = 0;
4246
4247 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4248
4249 has_sgx = ebx & (1 << 2);
4250 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4251
4252 if (has_sgx)
4253 decode_feature_control_msr();
4254 }
4255
Len Brown61a87ba2015-11-23 02:30:51 -05004256 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004257 unsigned int eax_crystal;
4258 unsigned int ebx_tsc;
4259
4260 /*
4261 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4262 */
4263 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004264 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004265
4266 if (ebx_tsc != 0) {
4267
Len Brown96e47152017-01-21 02:26:00 -05004268 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004269 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004270 eax_crystal, ebx_tsc, crystal_hz);
4271
4272 if (crystal_hz == 0)
4273 switch(model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004274 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4275 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4276 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4277 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004278 crystal_hz = 24000000; /* 24.0 MHz */
4279 break;
Len Brown7268d402016-12-01 23:10:39 -05004280 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004281 crystal_hz = 25000000; /* 25.0 MHz */
4282 break;
Len Brown869ce69e2016-06-16 23:22:37 -04004283 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004284 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004285 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004286 break;
4287 default:
4288 crystal_hz = 0;
4289 }
4290
4291 if (crystal_hz) {
4292 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004293 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004294 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004295 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4296 }
4297 }
4298 }
Len Brown61a87ba2015-11-23 02:30:51 -05004299 if (max_level >= 0x16) {
4300 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4301
4302 /*
4303 * CPUID 16H Base MHz, Max MHz, Bus MHz
4304 */
4305 base_mhz = max_mhz = bus_mhz = edx = 0;
4306
Len Brown5aea2f72016-03-13 03:14:35 -04004307 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004308 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004309 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004310 base_mhz, max_mhz, bus_mhz);
4311 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004312
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004313 if (has_aperf)
4314 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4315
Len Brown812db3f2017-02-10 00:25:41 -05004316 BIC_PRESENT(BIC_IRQ);
4317 BIC_PRESENT(BIC_TSC_MHz);
4318
4319 if (probe_nhm_msrs(family, model)) {
4320 do_nhm_platform_info = 1;
4321 BIC_PRESENT(BIC_CPU_c1);
4322 BIC_PRESENT(BIC_CPU_c3);
4323 BIC_PRESENT(BIC_CPU_c6);
4324 BIC_PRESENT(BIC_SMI);
4325 }
Len Brownd7899442015-01-23 00:12:33 -05004326 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004327
4328 if (do_snb_cstates)
4329 BIC_PRESENT(BIC_CPU_c7);
4330
Len Brown5a634262016-04-06 17:15:55 -04004331 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004332 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4333 BIC_PRESENT(BIC_Pkgpc2);
4334 if (pkg_cstate_limit >= PCL__3)
4335 BIC_PRESENT(BIC_Pkgpc3);
4336 if (pkg_cstate_limit >= PCL__6)
4337 BIC_PRESENT(BIC_Pkgpc6);
4338 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4339 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba112017-02-10 00:27:20 -05004340 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004341 BIC_NOT_PRESENT(BIC_Pkgpc2);
4342 BIC_NOT_PRESENT(BIC_Pkgpc3);
4343 BIC_PRESENT(BIC_Pkgpc6);
4344 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba112017-02-10 00:27:20 -05004345 BIC_PRESENT(BIC_Mod_c6);
4346 use_c1_residency_msr = 1;
4347 }
Len Brown7170a372017-01-27 02:13:27 -05004348 if (is_dnv(family, model)) {
4349 BIC_PRESENT(BIC_CPU_c1);
4350 BIC_NOT_PRESENT(BIC_CPU_c3);
4351 BIC_NOT_PRESENT(BIC_Pkgpc3);
4352 BIC_NOT_PRESENT(BIC_CPU_c7);
4353 BIC_NOT_PRESENT(BIC_Pkgpc7);
4354 use_c1_residency_msr = 1;
4355 }
Len Brown34c761972017-01-27 02:36:41 -05004356 if (is_skx(family, model)) {
4357 BIC_NOT_PRESENT(BIC_CPU_c3);
4358 BIC_NOT_PRESENT(BIC_Pkgpc3);
4359 BIC_NOT_PRESENT(BIC_CPU_c7);
4360 BIC_NOT_PRESENT(BIC_Pkgpc7);
4361 }
Len Brownade0eba2017-02-10 01:56:47 -05004362 if (is_bdx(family, model)) {
4363 BIC_NOT_PRESENT(BIC_CPU_c7);
4364 BIC_NOT_PRESENT(BIC_Pkgpc7);
4365 }
Len Brown0f47c082017-01-27 00:50:45 -05004366 if (has_hsw_msrs(family, model)) {
4367 BIC_PRESENT(BIC_Pkgpc8);
4368 BIC_PRESENT(BIC_Pkgpc9);
4369 BIC_PRESENT(BIC_Pkgpc10);
4370 }
Len Brown5a634262016-04-06 17:15:55 -04004371 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004372 if (has_skl_msrs(family, model)) {
4373 BIC_PRESENT(BIC_Totl_c0);
4374 BIC_PRESENT(BIC_Any_c0);
4375 BIC_PRESENT(BIC_GFX_c0);
4376 BIC_PRESENT(BIC_CPUGFX);
4377 }
Len Brown144b44b2013-11-09 00:30:16 -05004378 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004379 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004380
Len Brown96e47152017-01-21 02:26:00 -05004381 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004382 decode_misc_pwr_mgmt_msr();
4383
Len Brown96e47152017-01-21 02:26:00 -05004384 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004385 decode_c6_demotion_policy_msr();
4386
Len Brown889facb2012-11-08 00:48:57 -05004387 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004388 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004389 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004390
Len Brown96e47152017-01-21 02:26:00 -05004391 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004392 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004393
Len Brown41618e62017-02-09 18:25:22 -05004394 if (!quiet)
4395 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004396 if (!quiet)
4397 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004398
Len Browna2b7b742015-09-26 00:12:38 -04004399 if (has_skl_msrs(family, model))
4400 calculate_tsc_tweak();
4401
Len Brown812db3f2017-02-10 00:25:41 -05004402 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4403 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004404
Len Brown812db3f2017-02-10 00:25:41 -05004405 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4406 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004407
Len Brown96e47152017-01-21 02:26:00 -05004408 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004409 decode_misc_feature_control();
4410
Len Brown889facb2012-11-08 00:48:57 -05004411 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004412}
4413
Len Brown103a8fe2010-10-22 23:53:03 -04004414
4415/*
4416 * in /dev/cpu/ return success for names that are numbers
4417 * ie. filter out ".", "..", "microcode".
4418 */
4419int dir_filter(const struct dirent *dirp)
4420{
4421 if (isdigit(dirp->d_name[0]))
4422 return 1;
4423 else
4424 return 0;
4425}
4426
4427int open_dev_cpu_msr(int dummy1)
4428{
4429 return 0;
4430}
4431
Len Brownc98d5d92012-06-04 00:56:40 -04004432void topology_probe()
4433{
4434 int i;
4435 int max_core_id = 0;
4436 int max_package_id = 0;
4437 int max_siblings = 0;
4438 struct cpu_topology {
4439 int core_id;
4440 int physical_package_id;
4441 } *cpus;
4442
4443 /* Initialize num_cpus, max_cpu_num */
4444 topo.num_cpus = 0;
4445 topo.max_cpu_num = 0;
4446 for_all_proc_cpus(count_cpus);
4447 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004448 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004449
Len Brownd8af6f52015-02-10 01:56:38 -05004450 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004451 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004452
4453 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004454 if (cpus == NULL)
4455 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004456
4457 /*
4458 * Allocate and initialize cpu_present_set
4459 */
4460 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004461 if (cpu_present_set == NULL)
4462 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004463 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4464 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4465 for_all_proc_cpus(mark_cpu_present);
4466
4467 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004468 * Validate that all cpus in cpu_subset are also in cpu_present_set
4469 */
4470 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4471 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4472 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4473 err(1, "cpu%d not present", i);
4474 }
4475
4476 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004477 * Allocate and initialize cpu_affinity_set
4478 */
4479 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004480 if (cpu_affinity_set == NULL)
4481 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004482 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4483 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4484
4485
4486 /*
4487 * For online cpus
4488 * find max_core_id, max_package_id
4489 */
4490 for (i = 0; i <= topo.max_cpu_num; ++i) {
4491 int siblings;
4492
4493 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004494 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004495 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004496 continue;
4497 }
4498 cpus[i].core_id = get_core_id(i);
4499 if (cpus[i].core_id > max_core_id)
4500 max_core_id = cpus[i].core_id;
4501
4502 cpus[i].physical_package_id = get_physical_package_id(i);
4503 if (cpus[i].physical_package_id > max_package_id)
4504 max_package_id = cpus[i].physical_package_id;
4505
4506 siblings = get_num_ht_siblings(i);
4507 if (siblings > max_siblings)
4508 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004509 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004510 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004511 i, cpus[i].physical_package_id, cpus[i].core_id);
4512 }
4513 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004514 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004515 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004516 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004517 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004518 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004519
4520 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004521 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004522 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004523 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004524 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004525 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004526
4527 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004528 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004529 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004530
4531 free(cpus);
4532}
4533
4534void
4535allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4536{
4537 int i;
4538
4539 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004540 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004541 if (*t == NULL)
4542 goto error;
4543
4544 for (i = 0; i < topo.num_threads_per_core *
4545 topo.num_cores_per_pkg * topo.num_packages; i++)
4546 (*t)[i].cpu_id = -1;
4547
4548 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004549 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004550 if (*c == NULL)
4551 goto error;
4552
4553 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4554 (*c)[i].core_id = -1;
4555
Len Brown678a3bd2017-02-09 22:22:13 -05004556 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004557 if (*p == NULL)
4558 goto error;
4559
4560 for (i = 0; i < topo.num_packages; i++)
4561 (*p)[i].package_id = i;
4562
4563 return;
4564error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004565 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004566}
4567/*
4568 * init_counter()
4569 *
4570 * set cpu_id, core_num, pkg_num
4571 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4572 *
4573 * increment topo.num_cores when 1st core in pkg seen
4574 */
4575void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4576 struct pkg_data *pkg_base, int thread_num, int core_num,
4577 int pkg_num, int cpu_id)
4578{
4579 struct thread_data *t;
4580 struct core_data *c;
4581 struct pkg_data *p;
4582
4583 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4584 c = GET_CORE(core_base, core_num, pkg_num);
4585 p = GET_PKG(pkg_base, pkg_num);
4586
4587 t->cpu_id = cpu_id;
4588 if (thread_num == 0) {
4589 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4590 if (cpu_is_first_core_in_package(cpu_id))
4591 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4592 }
4593
4594 c->core_id = core_num;
4595 p->package_id = pkg_num;
4596}
4597
4598
4599int initialize_counters(int cpu_id)
4600{
4601 int my_thread_id, my_core_id, my_package_id;
4602
4603 my_package_id = get_physical_package_id(cpu_id);
4604 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004605 my_thread_id = get_cpu_position_in_core(cpu_id);
4606 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004607 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004608
4609 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4610 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4611 return 0;
4612}
4613
4614void allocate_output_buffer()
4615{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004616 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004617 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004618 if (outp == NULL)
4619 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004620}
Len Brown36229892016-02-26 20:51:02 -05004621void allocate_fd_percpu(void)
4622{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004623 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004624 if (fd_percpu == NULL)
4625 err(-1, "calloc fd_percpu");
4626}
Len Brown562a2d32016-02-26 23:48:05 -05004627void allocate_irq_buffers(void)
4628{
4629 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4630 if (irq_column_2_cpu == NULL)
4631 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004632
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004633 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004634 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004635 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004636}
Len Brownc98d5d92012-06-04 00:56:40 -04004637void setup_all_buffers(void)
4638{
4639 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004640 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004641 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004642 allocate_counters(&thread_even, &core_even, &package_even);
4643 allocate_counters(&thread_odd, &core_odd, &package_odd);
4644 allocate_output_buffer();
4645 for_all_proc_cpus(initialize_counters);
4646}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004647
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004648void set_base_cpu(void)
4649{
4650 base_cpu = sched_getcpu();
4651 if (base_cpu < 0)
4652 err(-ENODEV, "No valid cpus found");
4653
4654 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004655 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004656}
4657
Len Brown103a8fe2010-10-22 23:53:03 -04004658void turbostat_init()
4659{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004660 setup_all_buffers();
4661 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004662 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004663 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004664 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004665
Len Brown103a8fe2010-10-22 23:53:03 -04004666
Len Brown96e47152017-01-21 02:26:00 -05004667 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004668 for_all_cpus(print_hwp, ODD_COUNTERS);
4669
Len Brown96e47152017-01-21 02:26:00 -05004670 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004671 for_all_cpus(print_epb, ODD_COUNTERS);
4672
Len Brown96e47152017-01-21 02:26:00 -05004673 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004674 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4675
Len Brown96e47152017-01-21 02:26:00 -05004676 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004677 for_all_cpus(print_rapl, ODD_COUNTERS);
4678
4679 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4680
Len Brown96e47152017-01-21 02:26:00 -05004681 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004682 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004683
Len Brown96e47152017-01-21 02:26:00 -05004684 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004685 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004686}
4687
4688int fork_it(char **argv)
4689{
Len Brown103a8fe2010-10-22 23:53:03 -04004690 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004691 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004692
Len Brown218f0e82017-02-14 22:07:52 -05004693 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004694 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4695 if (status)
4696 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004697 /* clear affinity side-effect of get_counters() */
4698 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004699 gettimeofday(&tv_even, (struct timezone *)NULL);
4700
4701 child_pid = fork();
4702 if (!child_pid) {
4703 /* child */
4704 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004705 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004706 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004707
4708 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004709 if (child_pid == -1)
4710 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004711
4712 signal(SIGINT, SIG_IGN);
4713 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004714 if (waitpid(child_pid, &status, 0) == -1)
4715 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004716 }
Len Brownc98d5d92012-06-04 00:56:40 -04004717 /*
4718 * n.b. fork_it() does not check for errors from for_all_cpus()
4719 * because re-starting is problematic when forking
4720 */
Len Brown218f0e82017-02-14 22:07:52 -05004721 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004722 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004723 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004724 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004725 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4726 fprintf(outf, "%s: Counter reset detected\n", progname);
4727 else {
4728 compute_average(EVEN_COUNTERS);
4729 format_all_counters(EVEN_COUNTERS);
4730 }
Len Brown103a8fe2010-10-22 23:53:03 -04004731
Len Brownb7d8c142016-02-13 23:36:17 -05004732 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4733
4734 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004735
Len Brownd91bb172012-11-01 00:08:19 -04004736 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004737}
4738
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004739int get_and_dump_counters(void)
4740{
4741 int status;
4742
Len Brown218f0e82017-02-14 22:07:52 -05004743 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004744 status = for_all_cpus(get_counters, ODD_COUNTERS);
4745 if (status)
4746 return status;
4747
4748 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4749 if (status)
4750 return status;
4751
Len Brownb7d8c142016-02-13 23:36:17 -05004752 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004753
4754 return status;
4755}
4756
Len Brownd8af6f52015-02-10 01:56:38 -05004757void print_version() {
Len Brownf7d44a82017-05-27 21:24:58 -07004758 fprintf(outf, "turbostat version 17.06.23"
Len Brownd8af6f52015-02-10 01:56:38 -05004759 " - Len Brown <lenb@kernel.org>\n");
4760}
4761
Len Brown495c76542017-02-08 02:41:51 -05004762int add_counter(unsigned int msr_num, char *path, char *name,
4763 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004764 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004765{
4766 struct msr_counter *msrp;
4767
4768 msrp = calloc(1, sizeof(struct msr_counter));
4769 if (msrp == NULL) {
4770 perror("calloc");
4771 exit(1);
4772 }
4773
4774 msrp->msr_num = msr_num;
4775 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004776 if (path)
4777 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004778 msrp->width = width;
4779 msrp->type = type;
4780 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004781 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004782
4783 switch (scope) {
4784
4785 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004786 msrp->next = sys.tp;
4787 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004788 sys.added_thread_counters++;
4789 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4790 fprintf(stderr, "exceeded max %d added thread counters\n",
4791 MAX_ADDED_COUNTERS);
4792 exit(-1);
4793 }
Len Brown388e9c82016-12-22 23:57:55 -05004794 break;
4795
4796 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004797 msrp->next = sys.cp;
4798 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004799 sys.added_core_counters++;
4800 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4801 fprintf(stderr, "exceeded max %d added core counters\n",
4802 MAX_ADDED_COUNTERS);
4803 exit(-1);
4804 }
Len Brown388e9c82016-12-22 23:57:55 -05004805 break;
4806
4807 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004808 msrp->next = sys.pp;
4809 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004810 sys.added_package_counters++;
4811 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4812 fprintf(stderr, "exceeded max %d added package counters\n",
4813 MAX_ADDED_COUNTERS);
4814 exit(-1);
4815 }
Len Brown388e9c82016-12-22 23:57:55 -05004816 break;
4817 }
4818
4819 return 0;
4820}
4821
4822void parse_add_command(char *add_command)
4823{
4824 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004825 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004826 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004827 int width = 64;
4828 int fail = 0;
4829 enum counter_scope scope = SCOPE_CPU;
4830 enum counter_type type = COUNTER_CYCLES;
4831 enum counter_format format = FORMAT_DELTA;
4832
4833 while (add_command) {
4834
4835 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4836 goto next;
4837
4838 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4839 goto next;
4840
Len Brown495c76542017-02-08 02:41:51 -05004841 if (*add_command == '/') {
4842 path = add_command;
4843 goto next;
4844 }
4845
Len Brown388e9c82016-12-22 23:57:55 -05004846 if (sscanf(add_command, "u%d", &width) == 1) {
4847 if ((width == 32) || (width == 64))
4848 goto next;
4849 width = 64;
4850 }
4851 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4852 scope = SCOPE_CPU;
4853 goto next;
4854 }
4855 if (!strncmp(add_command, "core", strlen("core"))) {
4856 scope = SCOPE_CORE;
4857 goto next;
4858 }
4859 if (!strncmp(add_command, "package", strlen("package"))) {
4860 scope = SCOPE_PACKAGE;
4861 goto next;
4862 }
4863 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4864 type = COUNTER_CYCLES;
4865 goto next;
4866 }
4867 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4868 type = COUNTER_SECONDS;
4869 goto next;
4870 }
Len Brown41618e62017-02-09 18:25:22 -05004871 if (!strncmp(add_command, "usec", strlen("usec"))) {
4872 type = COUNTER_USEC;
4873 goto next;
4874 }
Len Brown388e9c82016-12-22 23:57:55 -05004875 if (!strncmp(add_command, "raw", strlen("raw"))) {
4876 format = FORMAT_RAW;
4877 goto next;
4878 }
4879 if (!strncmp(add_command, "delta", strlen("delta"))) {
4880 format = FORMAT_DELTA;
4881 goto next;
4882 }
4883 if (!strncmp(add_command, "percent", strlen("percent"))) {
4884 format = FORMAT_PERCENT;
4885 goto next;
4886 }
4887
4888 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4889 char *eos;
4890
4891 eos = strchr(name_buffer, ',');
4892 if (eos)
4893 *eos = '\0';
4894 goto next;
4895 }
4896
4897next:
4898 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004899 if (add_command) {
4900 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004901 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004902 }
Len Brown388e9c82016-12-22 23:57:55 -05004903
4904 }
Len Brown495c76542017-02-08 02:41:51 -05004905 if ((msr_num == 0) && (path == NULL)) {
4906 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004907 fail++;
4908 }
4909
4910 /* generate default column header */
4911 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05004912 if (width == 32)
4913 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4914 else
4915 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05004916 }
4917
Len Brown41618e62017-02-09 18:25:22 -05004918 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05004919 fail++;
4920
4921 if (fail) {
4922 help();
4923 exit(1);
4924 }
4925}
Len Brown41618e62017-02-09 18:25:22 -05004926
Len Browndd778a52017-02-21 23:21:13 -05004927int is_deferred_skip(char *name)
4928{
4929 int i;
4930
4931 for (i = 0; i < deferred_skip_index; ++i)
4932 if (!strcmp(name, deferred_skip_names[i]))
4933 return 1;
4934 return 0;
4935}
4936
Len Brown41618e62017-02-09 18:25:22 -05004937void probe_sysfs(void)
4938{
4939 char path[64];
4940 char name_buf[16];
4941 FILE *input;
4942 int state;
4943 char *sp;
4944
4945 if (!DO_BIC(BIC_sysfs))
4946 return;
4947
4948 for (state = 10; state > 0; --state) {
4949
4950 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4951 base_cpu, state);
4952 input = fopen(path, "r");
4953 if (input == NULL)
4954 continue;
4955 fgets(name_buf, sizeof(name_buf), input);
4956
4957 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4958 sp = strchr(name_buf, '-');
4959 if (!sp)
4960 sp = strchrnul(name_buf, '\n');
4961 *sp = '%';
4962 *(sp + 1) = '\0';
4963
4964 fclose(input);
4965
4966 sprintf(path, "cpuidle/state%d/time", state);
4967
Len Browndd778a52017-02-21 23:21:13 -05004968 if (is_deferred_skip(name_buf))
4969 continue;
4970
Len Brown41618e62017-02-09 18:25:22 -05004971 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4972 FORMAT_PERCENT, SYSFS_PERCPU);
4973 }
4974
4975 for (state = 10; state > 0; --state) {
4976
4977 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4978 base_cpu, state);
4979 input = fopen(path, "r");
4980 if (input == NULL)
4981 continue;
4982 fgets(name_buf, sizeof(name_buf), input);
4983 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4984 sp = strchr(name_buf, '-');
4985 if (!sp)
4986 sp = strchrnul(name_buf, '\n');
4987 *sp = '\0';
4988 fclose(input);
4989
4990 sprintf(path, "cpuidle/state%d/usage", state);
4991
Len Browndd778a52017-02-21 23:21:13 -05004992 if (is_deferred_skip(name_buf))
4993 continue;
4994
Len Brown41618e62017-02-09 18:25:22 -05004995 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4996 FORMAT_DELTA, SYSFS_PERCPU);
4997 }
4998
4999}
5000
Len Brown1ef7d212017-02-10 23:54:15 -05005001
5002/*
5003 * parse cpuset with following syntax
5004 * 1,2,4..6,8-10 and set bits in cpu_subset
5005 */
5006void parse_cpu_command(char *optarg)
5007{
5008 unsigned int start, end;
5009 char *next;
5010
Len Brown4e4e1e72017-02-21 22:33:42 -05005011 if (!strcmp(optarg, "core")) {
5012 if (cpu_subset)
5013 goto error;
5014 show_core_only++;
5015 return;
5016 }
5017 if (!strcmp(optarg, "package")) {
5018 if (cpu_subset)
5019 goto error;
5020 show_pkg_only++;
5021 return;
5022 }
5023 if (show_core_only || show_pkg_only)
5024 goto error;
5025
Len Brown1ef7d212017-02-10 23:54:15 -05005026 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5027 if (cpu_subset == NULL)
5028 err(3, "CPU_ALLOC");
5029 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5030
5031 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5032
5033 next = optarg;
5034
5035 while (next && *next) {
5036
5037 if (*next == '-') /* no negative cpu numbers */
5038 goto error;
5039
5040 start = strtoul(next, &next, 10);
5041
5042 if (start >= CPU_SUBSET_MAXCPUS)
5043 goto error;
5044 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5045
5046 if (*next == '\0')
5047 break;
5048
5049 if (*next == ',') {
5050 next += 1;
5051 continue;
5052 }
5053
5054 if (*next == '-') {
5055 next += 1; /* start range */
5056 } else if (*next == '.') {
5057 next += 1;
5058 if (*next == '.')
5059 next += 1; /* start range */
5060 else
5061 goto error;
5062 }
5063
5064 end = strtoul(next, &next, 10);
5065 if (end <= start)
5066 goto error;
5067
5068 while (++start <= end) {
5069 if (start >= CPU_SUBSET_MAXCPUS)
5070 goto error;
5071 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5072 }
5073
5074 if (*next == ',')
5075 next += 1;
5076 else if (*next != '\0')
5077 goto error;
5078 }
5079
5080 return;
5081
5082error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005083 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5084 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005085 exit(-1);
5086}
5087
Len Brown812db3f2017-02-10 00:25:41 -05005088
Len Brown103a8fe2010-10-22 23:53:03 -04005089void cmdline(int argc, char **argv)
5090{
5091 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005092 int option_index = 0;
5093 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005094 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005095 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005096 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005097 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005098 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005099 {"interval", required_argument, 0, 'i'},
5100 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005101 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005102 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005103 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005104 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005105 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005106 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005107 {"Summary", no_argument, 0, 'S'},
5108 {"TCC", required_argument, 0, 'T'},
5109 {"version", no_argument, 0, 'v' },
5110 {0, 0, 0, 0 }
5111 };
Len Brown103a8fe2010-10-22 23:53:03 -04005112
5113 progname = argv[0];
5114
Len Brown3f44a5c2017-10-17 15:42:56 -04005115 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jo:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005116 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005117 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005118 case 'a':
5119 parse_add_command(optarg);
5120 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005121 case 'c':
5122 parse_cpu_command(optarg);
5123 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005124 case 'D':
5125 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005126 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005127 case 'e':
5128 /* --enable specified counter */
5129 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5130 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005131 case 'd':
5132 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005133 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005134 break;
Len Brown812db3f2017-02-10 00:25:41 -05005135 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005136 /*
5137 * --hide: do not show those specified
5138 * multiple invocations simply clear more bits in enabled mask
5139 */
5140 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005141 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005142 case 'h':
5143 default:
5144 help();
5145 exit(1);
5146 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005147 {
5148 double interval = strtod(optarg, NULL);
5149
5150 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005151 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005152 interval);
5153 exit(2);
5154 }
5155
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005156 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005157 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005158 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005159 }
Len Brown889facb2012-11-08 00:48:57 -05005160 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005161 case 'J':
5162 rapl_joules++;
5163 break;
Len Brownc8ade362017-02-15 17:15:11 -05005164 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005165 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005166 list_header_only++;
5167 quiet++;
5168 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005169 case 'o':
5170 outf = fopen_or_die(optarg, "w");
5171 break;
Len Brown96e47152017-01-21 02:26:00 -05005172 case 'q':
5173 quiet = 1;
5174 break;
Len Brown812db3f2017-02-10 00:25:41 -05005175 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005176 /*
5177 * --show: show only those specified
5178 * The 1st invocation will clear and replace the enabled mask
5179 * subsequent invocations can add to it.
5180 */
5181 if (shown == 0)
5182 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5183 else
5184 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5185 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005186 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005187 case 'S':
5188 summary_only++;
5189 break;
5190 case 'T':
5191 tcc_activation_temp_override = atoi(optarg);
5192 break;
5193 case 'v':
5194 print_version();
5195 exit(0);
5196 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005197 }
5198 }
5199}
5200
5201int main(int argc, char **argv)
5202{
Len Brownb7d8c142016-02-13 23:36:17 -05005203 outf = stderr;
5204
Len Brown103a8fe2010-10-22 23:53:03 -04005205 cmdline(argc, argv);
5206
Len Brown96e47152017-01-21 02:26:00 -05005207 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005208 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005209
Len Brown41618e62017-02-09 18:25:22 -05005210 probe_sysfs();
5211
Len Brown103a8fe2010-10-22 23:53:03 -04005212 turbostat_init();
5213
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005214 /* dump counters and exit */
5215 if (dump_only)
5216 return get_and_dump_counters();
5217
Len Brownc8ade362017-02-15 17:15:11 -05005218 /* list header and exit */
5219 if (list_header_only) {
5220 print_header(",");
5221 flush_output_stdout();
5222 return 0;
5223 }
5224
Len Brown103a8fe2010-10-22 23:53:03 -04005225 /*
5226 * if any params left, it must be a command to fork
5227 */
5228 if (argc - optind)
5229 return fork_it(argv + optind);
5230 else
5231 turbostat_loop();
5232
5233 return 0;
5234}