blob: 29e9cd5fcd8897b9425c3a486657c03c4a73bf1e [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
Len Brownb9ad8ee2017-07-19 19:28:37 -040032#include <sys/select.h>
Len Brown103a8fe2010-10-22 23:53:03 -040033#include <sys/resource.h>
34#include <fcntl.h>
35#include <signal.h>
36#include <sys/time.h>
37#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050038#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040039#include <dirent.h>
40#include <string.h>
41#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040042#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050043#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070044#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040045#include <linux/capability.h>
46#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040047
Len Brown103a8fe2010-10-22 23:53:03 -040048char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050049FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050050int *fd_percpu;
Len Brownb9ad8ee2017-07-19 19:28:37 -040051struct timeval interval_tv = {5, 0};
Artem Bityutskiy47936f92017-10-04 15:01:47 +030052struct timespec interval_ts = {5, 0};
Len Brownb9ad8ee2017-07-19 19:28:37 -040053struct timespec one_msec = {0, 1000000};
Len Brownd8af6f52015-02-10 01:56:38 -050054unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050055unsigned int quiet;
Len Brown3f44a5c2017-10-17 15:42:56 -040056unsigned int shown;
Len Brown0de6c0d2017-02-15 21:45:40 -050057unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050058unsigned int rapl_joules;
59unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050060unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050061unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040062unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070063unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050064unsigned int do_slm_cstates;
65unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050067unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040068unsigned int do_irtl_snb;
69unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050070unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040071unsigned int genuine_intel;
72unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050073unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050074unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020075unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040076double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040077double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040078unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040079double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040080unsigned int show_pkg_only;
81unsigned int show_core_only;
82char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050083unsigned int do_rapl;
84unsigned int do_dts;
85unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050086unsigned long long gfx_cur_rc6_ms;
Len 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 Brown0539ba12017-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 Brown0539ba12017-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 Brown0539ba12017-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 Brown0539ba12017-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 Brown0539ba12017-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 Brown0539ba12017-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 Brown94d6ab42018-01-27 22:39:21 -05001031 if (*(outp - 1) != '\n')
1032 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001033
1034 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001035}
1036
Len Brownb7d8c142016-02-13 23:36:17 -05001037void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001038{
Len Brownb7d8c142016-02-13 23:36:17 -05001039 FILE *filep;
1040
1041 if (outf == stderr)
1042 filep = stdout;
1043 else
1044 filep = outf;
1045
1046 fputs(output_buffer, filep);
1047 fflush(filep);
1048
Len Brownc98d5d92012-06-04 00:56:40 -04001049 outp = output_buffer;
1050}
Len Brownb7d8c142016-02-13 23:36:17 -05001051void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001052{
Len Brownb7d8c142016-02-13 23:36:17 -05001053 fputs(output_buffer, outf);
1054 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001055 outp = output_buffer;
1056}
1057void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1058{
Len Browne23da032012-02-06 18:37:16 -05001059 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001060
Len Browne23da032012-02-06 18:37:16 -05001061 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001062 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001063
Len Brownc98d5d92012-06-04 00:56:40 -04001064 if (topo.num_cpus > 1)
1065 format_counters(&average.threads, &average.cores,
1066 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001067
Len Browne23da032012-02-06 18:37:16 -05001068 printed = 1;
1069
1070 if (summary_only)
1071 return;
1072
Len Brownc98d5d92012-06-04 00:56:40 -04001073 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001074}
1075
Len Brown889facb2012-11-08 00:48:57 -05001076#define DELTA_WRAP32(new, old) \
1077 if (new > old) { \
1078 old = new - old; \
1079 } else { \
1080 old = 0x100000000 + new - old; \
1081 }
1082
Len Brownba3dec92016-04-22 20:31:46 -04001083int
Len Brownc98d5d92012-06-04 00:56:40 -04001084delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001085{
Len Brown388e9c82016-12-22 23:57:55 -05001086 int i;
1087 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001088
Len Browna99d8732017-05-20 20:11:55 -04001089
1090 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001091 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001092 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001093 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001094 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001095 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001096 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001097 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 -04001098
Len Brownc98d5d92012-06-04 00:56:40 -04001099 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001100 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001101 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001102 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001103 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001104 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001105 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001106 old->pc8 = new->pc8 - old->pc8;
1107 old->pc9 = new->pc9 - old->pc9;
1108 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -05001109 old->pkg_temp_c = new->pkg_temp_c;
1110
Len Brown9185e982016-04-06 17:16:00 -04001111 /* flag an error when rc6 counter resets/wraps */
1112 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1113 old->gfx_rc6_ms = -1;
1114 else
1115 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1116
Len Brown27d47352016-02-27 00:37:54 -05001117 old->gfx_mhz = new->gfx_mhz;
1118
Len Brown889facb2012-11-08 00:48:57 -05001119 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1120 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1121 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1122 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1123 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1124 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001125
Len Brown388e9c82016-12-22 23:57:55 -05001126 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1127 if (mp->format == FORMAT_RAW)
1128 old->counter[i] = new->counter[i];
1129 else
1130 old->counter[i] = new->counter[i] - old->counter[i];
1131 }
1132
Len Brownba3dec92016-04-22 20:31:46 -04001133 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001134}
Len Brown103a8fe2010-10-22 23:53:03 -04001135
Len Brownc98d5d92012-06-04 00:56:40 -04001136void
1137delta_core(struct core_data *new, struct core_data *old)
1138{
Len Brown388e9c82016-12-22 23:57:55 -05001139 int i;
1140 struct msr_counter *mp;
1141
Len Brownc98d5d92012-06-04 00:56:40 -04001142 old->c3 = new->c3 - old->c3;
1143 old->c6 = new->c6 - old->c6;
1144 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001145 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001146 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001147
1148 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1149 if (mp->format == FORMAT_RAW)
1150 old->counter[i] = new->counter[i];
1151 else
1152 old->counter[i] = new->counter[i] - old->counter[i];
1153 }
Len Brownc98d5d92012-06-04 00:56:40 -04001154}
Len Brown103a8fe2010-10-22 23:53:03 -04001155
Len Brownc3ae3312012-06-13 21:31:46 -04001156/*
1157 * old = new - old
1158 */
Len Brownba3dec92016-04-22 20:31:46 -04001159int
Len Brownc98d5d92012-06-04 00:56:40 -04001160delta_thread(struct thread_data *new, struct thread_data *old,
1161 struct core_data *core_delta)
1162{
Len Brown388e9c82016-12-22 23:57:55 -05001163 int i;
1164 struct msr_counter *mp;
1165
Len Brown3f44a5c2017-10-17 15:42:56 -04001166 /*
1167 * the timestamps from start of measurement interval are in "old"
1168 * the timestamp from end of measurement interval are in "new"
1169 * over-write old w/ new so we can print end of interval values
1170 */
1171
1172 old->tv_begin = new->tv_begin;
1173 old->tv_end = new->tv_end;
1174
Len Brownc98d5d92012-06-04 00:56:40 -04001175 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001176
Len Brownc98d5d92012-06-04 00:56:40 -04001177 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001178 if (old->tsc < (1000 * 1000))
1179 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1180 "You can disable all c-states by booting with \"idle=poll\"\n"
1181 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001182
Len Brownc98d5d92012-06-04 00:56:40 -04001183 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001184
Len Brown812db3f2017-02-10 00:25:41 -05001185 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001186 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1187 old->aperf = new->aperf - old->aperf;
1188 old->mperf = new->mperf - old->mperf;
1189 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001190 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001191 }
Len Brownc98d5d92012-06-04 00:56:40 -04001192 }
Len Brown103a8fe2010-10-22 23:53:03 -04001193
Len Brown103a8fe2010-10-22 23:53:03 -04001194
Len Brown144b44b2013-11-09 00:30:16 -05001195 if (use_c1_residency_msr) {
1196 /*
1197 * Some models have a dedicated C1 residency MSR,
1198 * which should be more accurate than the derivation below.
1199 */
1200 } else {
1201 /*
1202 * As counter collection is not atomic,
1203 * it is possible for mperf's non-halted cycles + idle states
1204 * to exceed TSC's all cycles: show c1 = 0% in that case.
1205 */
Len Brown95149362017-04-12 19:44:51 -04001206 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001207 old->c1 = 0;
1208 else {
1209 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001210 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001211 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001212 }
Len Brownc98d5d92012-06-04 00:56:40 -04001213 }
Len Brownc3ae3312012-06-13 21:31:46 -04001214
Len Brownc98d5d92012-06-04 00:56:40 -04001215 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001216 if (debug > 1)
1217 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001218 old->mperf = 1; /* divide by 0 protection */
1219 }
1220
Len Brown812db3f2017-02-10 00:25:41 -05001221 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001222 old->irq_count = new->irq_count - old->irq_count;
1223
Len Brown812db3f2017-02-10 00:25:41 -05001224 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001225 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001226
Len Brown388e9c82016-12-22 23:57:55 -05001227 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1228 if (mp->format == FORMAT_RAW)
1229 old->counter[i] = new->counter[i];
1230 else
1231 old->counter[i] = new->counter[i] - old->counter[i];
1232 }
Len Brownba3dec92016-04-22 20:31:46 -04001233 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001234}
1235
1236int delta_cpu(struct thread_data *t, struct core_data *c,
1237 struct pkg_data *p, struct thread_data *t2,
1238 struct core_data *c2, struct pkg_data *p2)
1239{
Len Brownba3dec92016-04-22 20:31:46 -04001240 int retval = 0;
1241
Len Brownc98d5d92012-06-04 00:56:40 -04001242 /* calculate core delta only for 1st thread in core */
1243 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1244 delta_core(c, c2);
1245
1246 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001247 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1248 if (retval)
1249 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001250
1251 /* calculate package delta only for 1st core in package */
1252 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001253 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001254
Len Brownba3dec92016-04-22 20:31:46 -04001255 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001256}
1257
1258void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1259{
Len Brown388e9c82016-12-22 23:57:55 -05001260 int i;
1261 struct msr_counter *mp;
1262
Len Brown3f44a5c2017-10-17 15:42:56 -04001263 t->tv_begin.tv_sec = 0;
1264 t->tv_begin.tv_usec = 0;
1265 t->tv_end.tv_sec = 0;
1266 t->tv_end.tv_usec = 0;
1267
Len Brownc98d5d92012-06-04 00:56:40 -04001268 t->tsc = 0;
1269 t->aperf = 0;
1270 t->mperf = 0;
1271 t->c1 = 0;
1272
Len Brown562a2d32016-02-26 23:48:05 -05001273 t->irq_count = 0;
1274 t->smi_count = 0;
1275
Len Brownc98d5d92012-06-04 00:56:40 -04001276 /* tells format_counters to dump all fields from this set */
1277 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1278
1279 c->c3 = 0;
1280 c->c6 = 0;
1281 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001282 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001283 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001284
Len Brown0b2bb692015-03-26 00:50:30 -04001285 p->pkg_wtd_core_c0 = 0;
1286 p->pkg_any_core_c0 = 0;
1287 p->pkg_any_gfxe_c0 = 0;
1288 p->pkg_both_core_gfxe_c0 = 0;
1289
Len Brownc98d5d92012-06-04 00:56:40 -04001290 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001291 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001292 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001293 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001294 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001295 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001296 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001297 p->pc8 = 0;
1298 p->pc9 = 0;
1299 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001300
1301 p->energy_pkg = 0;
1302 p->energy_dram = 0;
1303 p->energy_cores = 0;
1304 p->energy_gfx = 0;
1305 p->rapl_pkg_perf_status = 0;
1306 p->rapl_dram_perf_status = 0;
1307 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001308
Len Brownfdf676e2016-02-27 01:28:12 -05001309 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001310 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001311 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1312 t->counter[i] = 0;
1313
1314 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1315 c->counter[i] = 0;
1316
1317 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1318 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001319}
1320int sum_counters(struct thread_data *t, struct core_data *c,
1321 struct pkg_data *p)
1322{
Len Brown388e9c82016-12-22 23:57:55 -05001323 int i;
1324 struct msr_counter *mp;
1325
Len Brown3f44a5c2017-10-17 15:42:56 -04001326 /* remember first tv_begin */
1327 if (average.threads.tv_begin.tv_sec == 0)
1328 average.threads.tv_begin = t->tv_begin;
1329
1330 /* remember last tv_end */
1331 average.threads.tv_end = t->tv_end;
1332
Len Brownc98d5d92012-06-04 00:56:40 -04001333 average.threads.tsc += t->tsc;
1334 average.threads.aperf += t->aperf;
1335 average.threads.mperf += t->mperf;
1336 average.threads.c1 += t->c1;
1337
Len Brown562a2d32016-02-26 23:48:05 -05001338 average.threads.irq_count += t->irq_count;
1339 average.threads.smi_count += t->smi_count;
1340
Len Brown388e9c82016-12-22 23:57:55 -05001341 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1342 if (mp->format == FORMAT_RAW)
1343 continue;
1344 average.threads.counter[i] += t->counter[i];
1345 }
1346
Len Brownc98d5d92012-06-04 00:56:40 -04001347 /* sum per-core values only for 1st thread in core */
1348 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1349 return 0;
1350
1351 average.cores.c3 += c->c3;
1352 average.cores.c6 += c->c6;
1353 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001354 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001355
Len Brown889facb2012-11-08 00:48:57 -05001356 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1357
Len Brown388e9c82016-12-22 23:57:55 -05001358 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1359 if (mp->format == FORMAT_RAW)
1360 continue;
1361 average.cores.counter[i] += c->counter[i];
1362 }
1363
Len Brownc98d5d92012-06-04 00:56:40 -04001364 /* sum per-pkg values only for 1st core in pkg */
1365 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1366 return 0;
1367
Len Browna99d8732017-05-20 20:11:55 -04001368 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001369 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001370 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001371 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001372 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001373 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001374 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001375 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001376
Len Brownc98d5d92012-06-04 00:56:40 -04001377 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001378 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001379 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001380 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001381 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001382 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001383 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001384 average.packages.pc8 += p->pc8;
1385 average.packages.pc9 += p->pc9;
1386 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001387
Len Brown889facb2012-11-08 00:48:57 -05001388 average.packages.energy_pkg += p->energy_pkg;
1389 average.packages.energy_dram += p->energy_dram;
1390 average.packages.energy_cores += p->energy_cores;
1391 average.packages.energy_gfx += p->energy_gfx;
1392
Len Brownfdf676e2016-02-27 01:28:12 -05001393 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001394 average.packages.gfx_mhz = p->gfx_mhz;
1395
Len Brown889facb2012-11-08 00:48:57 -05001396 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1397
1398 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1399 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001400
1401 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1402 if (mp->format == FORMAT_RAW)
1403 continue;
1404 average.packages.counter[i] += p->counter[i];
1405 }
Len Brownc98d5d92012-06-04 00:56:40 -04001406 return 0;
1407}
1408/*
1409 * sum the counters for all cpus in the system
1410 * compute the weighted average
1411 */
1412void compute_average(struct thread_data *t, struct core_data *c,
1413 struct pkg_data *p)
1414{
Len Brown388e9c82016-12-22 23:57:55 -05001415 int i;
1416 struct msr_counter *mp;
1417
Len Brownc98d5d92012-06-04 00:56:40 -04001418 clear_counters(&average.threads, &average.cores, &average.packages);
1419
1420 for_all_cpus(sum_counters, t, c, p);
1421
1422 average.threads.tsc /= topo.num_cpus;
1423 average.threads.aperf /= topo.num_cpus;
1424 average.threads.mperf /= topo.num_cpus;
1425 average.threads.c1 /= topo.num_cpus;
1426
Len Brown0de6c0d2017-02-15 21:45:40 -05001427 if (average.threads.irq_count > 9999999)
1428 sums_need_wide_columns = 1;
1429
Len Brownc98d5d92012-06-04 00:56:40 -04001430 average.cores.c3 /= topo.num_cores;
1431 average.cores.c6 /= topo.num_cores;
1432 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001433 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001434
Len Browna99d8732017-05-20 20:11:55 -04001435 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001436 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001437 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001438 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001439 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001440 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001441 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001442 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001443
Len Brownc98d5d92012-06-04 00:56:40 -04001444 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001445 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001446 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001447 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001448 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001449 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001450 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001451
1452 average.packages.pc8 /= topo.num_packages;
1453 average.packages.pc9 /= topo.num_packages;
1454 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001455
1456 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1457 if (mp->format == FORMAT_RAW)
1458 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001459 if (mp->type == COUNTER_ITEMS) {
1460 if (average.threads.counter[i] > 9999999)
1461 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001462 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001463 }
Len Brown388e9c82016-12-22 23:57:55 -05001464 average.threads.counter[i] /= topo.num_cpus;
1465 }
1466 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1467 if (mp->format == FORMAT_RAW)
1468 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001469 if (mp->type == COUNTER_ITEMS) {
1470 if (average.cores.counter[i] > 9999999)
1471 sums_need_wide_columns = 1;
1472 }
Len Brown388e9c82016-12-22 23:57:55 -05001473 average.cores.counter[i] /= topo.num_cores;
1474 }
1475 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1476 if (mp->format == FORMAT_RAW)
1477 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001478 if (mp->type == COUNTER_ITEMS) {
1479 if (average.packages.counter[i] > 9999999)
1480 sums_need_wide_columns = 1;
1481 }
Len Brown388e9c82016-12-22 23:57:55 -05001482 average.packages.counter[i] /= topo.num_packages;
1483 }
Len Brownc98d5d92012-06-04 00:56:40 -04001484}
1485
1486static unsigned long long rdtsc(void)
1487{
1488 unsigned int low, high;
1489
1490 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1491
1492 return low | ((unsigned long long)high) << 32;
1493}
1494
Len Brownc98d5d92012-06-04 00:56:40 -04001495/*
Len Brown495c76542017-02-08 02:41:51 -05001496 * Open a file, and exit on failure
1497 */
1498FILE *fopen_or_die(const char *path, const char *mode)
1499{
1500 FILE *filep = fopen(path, mode);
1501
1502 if (!filep)
1503 err(1, "%s: open failed", path);
1504 return filep;
1505}
1506/*
1507 * snapshot_sysfs_counter()
1508 *
1509 * return snapshot of given counter
1510 */
1511unsigned long long snapshot_sysfs_counter(char *path)
1512{
1513 FILE *fp;
1514 int retval;
1515 unsigned long long counter;
1516
1517 fp = fopen_or_die(path, "r");
1518
1519 retval = fscanf(fp, "%lld", &counter);
1520 if (retval != 1)
1521 err(1, "snapshot_sysfs_counter(%s)", path);
1522
1523 fclose(fp);
1524
1525 return counter;
1526}
1527
1528int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1529{
1530 if (mp->msr_num != 0) {
1531 if (get_msr(cpu, mp->msr_num, counterp))
1532 return -1;
1533 } else {
Len Brown46c27972017-12-08 17:38:17 -05001534 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001535
1536 if (mp->flags & SYSFS_PERCPU) {
1537 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1538 cpu, mp->path);
1539
1540 *counterp = snapshot_sysfs_counter(path);
1541 } else {
1542 *counterp = snapshot_sysfs_counter(mp->path);
1543 }
Len Brown495c76542017-02-08 02:41:51 -05001544 }
1545
1546 return 0;
1547}
1548
1549/*
Len Brownc98d5d92012-06-04 00:56:40 -04001550 * get_counters(...)
1551 * migrate to cpu
1552 * acquire and record local counters for that cpu
1553 */
1554int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1555{
1556 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001557 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001558 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001559 struct msr_counter *mp;
1560 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001561
Len Brownf4fdf2b2017-05-27 21:06:55 -07001562
1563 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1564
Len Browne52966c2012-11-08 22:38:05 -05001565 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001566 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001567 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001568 }
Len Brownc98d5d92012-06-04 00:56:40 -04001569
Len Brown0102b062016-02-27 03:11:29 -05001570retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001571 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1572
Len Brown812db3f2017-02-10 00:25:41 -05001573 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001574 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1575
1576 /*
1577 * The TSC, APERF and MPERF must be read together for
1578 * APERF/MPERF and MPERF/TSC to give accurate results.
1579 *
1580 * Unfortunately, APERF and MPERF are read by
1581 * individual system call, so delays may occur
1582 * between them. If the time to read them
1583 * varies by a large amount, we re-read them.
1584 */
1585
1586 /*
1587 * This initial dummy APERF read has been seen to
1588 * reduce jitter in the subsequent reads.
1589 */
1590
Len Brown9c63a652012-10-31 01:29:52 -04001591 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001592 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001593
1594 t->tsc = rdtsc(); /* re-read close to APERF */
1595
1596 tsc_before = t->tsc;
1597
1598 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1599 return -3;
1600
1601 tsc_between = rdtsc();
1602
Len Brown9c63a652012-10-31 01:29:52 -04001603 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001604 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001605
1606 tsc_after = rdtsc();
1607
1608 aperf_time = tsc_between - tsc_before;
1609 mperf_time = tsc_after - tsc_between;
1610
1611 /*
1612 * If the system call latency to read APERF and MPERF
1613 * differ by more than 2x, then try again.
1614 */
1615 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1616 aperf_mperf_retry_count++;
1617 if (aperf_mperf_retry_count < 5)
1618 goto retry;
1619 else
1620 warnx("cpu%d jitter %lld %lld",
1621 cpu, aperf_time, mperf_time);
1622 }
1623 aperf_mperf_retry_count = 0;
1624
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001625 t->aperf = t->aperf * aperf_mperf_multiplier;
1626 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001627 }
1628
Len Brown812db3f2017-02-10 00:25:41 -05001629 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001630 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001631 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001632 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1633 return -5;
1634 t->smi_count = msr & 0xFFFFFFFF;
1635 }
Len Brown0539ba12017-02-10 00:27:20 -05001636 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001637 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1638 return -6;
1639 }
1640
Len Brown388e9c82016-12-22 23:57:55 -05001641 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001642 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001643 return -10;
1644 }
1645
Len Brownc98d5d92012-06-04 00:56:40 -04001646 /* collect core counters only for 1st thread in core */
1647 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001648 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001649
Len Brown812db3f2017-02-10 00:25:41 -05001650 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001651 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1652 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001653 }
1654
Len Brown812db3f2017-02-10 00:25:41 -05001655 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001656 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1657 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001658 } else if (do_knl_cstates) {
1659 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1660 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001661 }
1662
Len Brown812db3f2017-02-10 00:25:41 -05001663 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001664 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1665 return -8;
1666
Len Brown0539ba12017-02-10 00:27:20 -05001667 if (DO_BIC(BIC_Mod_c6))
1668 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1669 return -8;
1670
Len Brown812db3f2017-02-10 00:25:41 -05001671 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001672 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1673 return -9;
1674 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1675 }
1676
Len Brown388e9c82016-12-22 23:57:55 -05001677 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001678 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001679 return -10;
1680 }
Len Brown889facb2012-11-08 00:48:57 -05001681
Len Brownc98d5d92012-06-04 00:56:40 -04001682 /* collect package counters only for 1st core in package */
1683 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001684 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001685
Len Browna99d8732017-05-20 20:11:55 -04001686 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001687 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1688 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001689 }
1690 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001691 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1692 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001693 }
1694 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001695 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1696 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001697 }
1698 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001699 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1700 return -13;
1701 }
Len Brown0f47c082017-01-27 00:50:45 -05001702 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001703 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1704 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001705 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001706 if (do_slm_cstates) {
1707 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1708 return -10;
1709 } else {
1710 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1711 return -10;
1712 }
1713 }
1714
Len Brown0f47c082017-01-27 00:50:45 -05001715 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001716 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1717 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001718 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001719 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1720 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001721 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001722 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1723 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001724 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001725 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1726 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001727 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001728 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1729 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001730
Len Brown889facb2012-11-08 00:48:57 -05001731 if (do_rapl & RAPL_PKG) {
1732 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1733 return -13;
1734 p->energy_pkg = msr & 0xFFFFFFFF;
1735 }
Jacob Pan91484942016-06-16 09:48:20 -07001736 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001737 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1738 return -14;
1739 p->energy_cores = msr & 0xFFFFFFFF;
1740 }
1741 if (do_rapl & RAPL_DRAM) {
1742 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1743 return -15;
1744 p->energy_dram = msr & 0xFFFFFFFF;
1745 }
1746 if (do_rapl & RAPL_GFX) {
1747 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1748 return -16;
1749 p->energy_gfx = msr & 0xFFFFFFFF;
1750 }
1751 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1752 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1753 return -16;
1754 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1755 }
1756 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1757 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1758 return -16;
1759 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1760 }
Len Brown812db3f2017-02-10 00:25:41 -05001761 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001762 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1763 return -17;
1764 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1765 }
Len Brownfdf676e2016-02-27 01:28:12 -05001766
Len Brown812db3f2017-02-10 00:25:41 -05001767 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001768 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1769
Len Brown812db3f2017-02-10 00:25:41 -05001770 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001771 p->gfx_mhz = gfx_cur_mhz;
1772
Len Brown388e9c82016-12-22 23:57:55 -05001773 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001774 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001775 return -10;
1776 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001777done:
1778 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001779
Len Brown103a8fe2010-10-22 23:53:03 -04001780 return 0;
1781}
1782
Len Brownee7e38e2015-02-09 23:39:45 -05001783/*
1784 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1785 * If you change the values, note they are used both in comparisons
1786 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1787 */
1788
1789#define PCLUKN 0 /* Unknown */
1790#define PCLRSV 1 /* Reserved */
1791#define PCL__0 2 /* PC0 */
1792#define PCL__1 3 /* PC1 */
1793#define PCL__2 4 /* PC2 */
1794#define PCL__3 5 /* PC3 */
1795#define PCL__4 6 /* PC4 */
1796#define PCL__6 7 /* PC6 */
1797#define PCL_6N 8 /* PC6 No Retention */
1798#define PCL_6R 9 /* PC6 Retention */
1799#define PCL__7 10 /* PC7 */
1800#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001801#define PCL__8 12 /* PC8 */
1802#define PCL__9 13 /* PC9 */
1803#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001804
1805int pkg_cstate_limit = PCLUKN;
1806char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001807 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001808
Len Browne9257f52015-04-01 21:02:57 -04001809int 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};
1810int 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};
1811int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brown0539ba12017-02-10 00:27:20 -05001812int 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 -05001813int 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 -04001814int 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 -04001815int 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 +03001816int 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 -05001817
Len Browna2b7b742015-09-26 00:12:38 -04001818
1819static void
1820calculate_tsc_tweak()
1821{
Len Browna2b7b742015-09-26 00:12:38 -04001822 tsc_tweak = base_hz / tsc_hz;
1823}
1824
Len Brownfcd17212015-03-23 20:29:09 -04001825static void
1826dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001827{
1828 unsigned long long msr;
1829 unsigned int ratio;
1830
Len Brownec0adc52015-11-12 02:42:31 -05001831 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001832
Len Brownb7d8c142016-02-13 23:36:17 -05001833 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001834
Len Brown103a8fe2010-10-22 23:53:03 -04001835 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001836 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001837 ratio, bclk, ratio * bclk);
1838
1839 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001840 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001841 ratio, bclk, ratio * bclk);
1842
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001843 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001844 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001845 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001846
Len Brownfcd17212015-03-23 20:29:09 -04001847 return;
1848}
1849
1850static void
1851dump_hsw_turbo_ratio_limits(void)
1852{
1853 unsigned long long msr;
1854 unsigned int ratio;
1855
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001856 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001857
Len Brownb7d8c142016-02-13 23:36:17 -05001858 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001859
1860 ratio = (msr >> 8) & 0xFF;
1861 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001862 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001863 ratio, bclk, ratio * bclk);
1864
1865 ratio = (msr >> 0) & 0xFF;
1866 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001867 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001868 ratio, bclk, ratio * bclk);
1869 return;
1870}
1871
1872static void
1873dump_ivt_turbo_ratio_limits(void)
1874{
1875 unsigned long long msr;
1876 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001877
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001878 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001879
Len Brownb7d8c142016-02-13 23:36:17 -05001880 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001881
1882 ratio = (msr >> 56) & 0xFF;
1883 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001884 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001885 ratio, bclk, ratio * bclk);
1886
1887 ratio = (msr >> 48) & 0xFF;
1888 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001889 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001890 ratio, bclk, ratio * bclk);
1891
1892 ratio = (msr >> 40) & 0xFF;
1893 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001894 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001895 ratio, bclk, ratio * bclk);
1896
1897 ratio = (msr >> 32) & 0xFF;
1898 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001899 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001900 ratio, bclk, ratio * bclk);
1901
1902 ratio = (msr >> 24) & 0xFF;
1903 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001904 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001905 ratio, bclk, ratio * bclk);
1906
1907 ratio = (msr >> 16) & 0xFF;
1908 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001909 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001910 ratio, bclk, ratio * bclk);
1911
1912 ratio = (msr >> 8) & 0xFF;
1913 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001914 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001915 ratio, bclk, ratio * bclk);
1916
1917 ratio = (msr >> 0) & 0xFF;
1918 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001919 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001920 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001921 return;
1922}
Len Brown31e07522017-01-31 23:07:49 -05001923int has_turbo_ratio_group_limits(int family, int model)
1924{
1925
1926 if (!genuine_intel)
1927 return 0;
1928
1929 switch (model) {
1930 case INTEL_FAM6_ATOM_GOLDMONT:
1931 case INTEL_FAM6_SKYLAKE_X:
1932 case INTEL_FAM6_ATOM_DENVERTON:
1933 return 1;
1934 }
1935 return 0;
1936}
Len Brown6574a5d2012-09-21 00:01:31 -04001937
Len Brownfcd17212015-03-23 20:29:09 -04001938static void
Len Brown31e07522017-01-31 23:07:49 -05001939dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001940{
Len Brown31e07522017-01-31 23:07:49 -05001941 unsigned long long msr, core_counts;
1942 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001943
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001944 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001945 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001946
Len Brown31e07522017-01-31 23:07:49 -05001947 if (has_turbo_ratio_group_limits(family, model)) {
1948 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1949 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1950 } else {
1951 core_counts = 0x0807060504030201;
1952 }
1953
Len Brown6574a5d2012-09-21 00:01:31 -04001954 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001955 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001956 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001957 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1958 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001959
1960 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001961 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001962 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001963 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1964 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001965
1966 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001967 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001968 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001969 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1970 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001971
1972 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001973 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001974 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001975 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1976 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001977
Len Brown103a8fe2010-10-22 23:53:03 -04001978 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001979 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001980 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001981 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1982 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001983
1984 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001985 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001986 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001987 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1988 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001989
1990 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001991 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001992 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001993 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1994 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001995
1996 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001997 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001998 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001999 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2000 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002001 return;
2002}
Len Brown3a9a9412014-08-15 02:39:52 -04002003
Len Brownfcd17212015-03-23 20:29:09 -04002004static void
Len Brown0f7887c2017-01-12 23:49:18 -05002005dump_atom_turbo_ratio_limits(void)
2006{
2007 unsigned long long msr;
2008 unsigned int ratio;
2009
2010 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2011 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2012
2013 ratio = (msr >> 0) & 0x3F;
2014 if (ratio)
2015 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2016 ratio, bclk, ratio * bclk);
2017
2018 ratio = (msr >> 8) & 0x3F;
2019 if (ratio)
2020 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2021 ratio, bclk, ratio * bclk);
2022
2023 ratio = (msr >> 16) & 0x3F;
2024 if (ratio)
2025 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2026 ratio, bclk, ratio * bclk);
2027
2028 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2029 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2030
2031 ratio = (msr >> 24) & 0x3F;
2032 if (ratio)
2033 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2034 ratio, bclk, ratio * bclk);
2035
2036 ratio = (msr >> 16) & 0x3F;
2037 if (ratio)
2038 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2039 ratio, bclk, ratio * bclk);
2040
2041 ratio = (msr >> 8) & 0x3F;
2042 if (ratio)
2043 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2044 ratio, bclk, ratio * bclk);
2045
2046 ratio = (msr >> 0) & 0x3F;
2047 if (ratio)
2048 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2049 ratio, bclk, ratio * bclk);
2050}
2051
2052static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002053dump_knl_turbo_ratio_limits(void)
2054{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002055 const unsigned int buckets_no = 7;
2056
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002057 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002058 int delta_cores, delta_ratio;
2059 int i, b_nr;
2060 unsigned int cores[buckets_no];
2061 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002062
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002063 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002064
Len Brownb7d8c142016-02-13 23:36:17 -05002065 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002066 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002067
2068 /**
2069 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002070 * [0] -- Reserved
2071 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002072 * [15:8] -- Base value of freq ratio of bucket 1.
2073 * [20:16] -- +ve delta of number of active cores of bucket 2.
2074 * i.e. active cores of bucket 2 =
2075 * active cores of bucket 1 + delta
2076 * [23:21] -- Negative delta of freq ratio of bucket 2.
2077 * i.e. freq ratio of bucket 2 =
2078 * freq ratio of bucket 1 - delta
2079 * [28:24]-- +ve delta of number of active cores of bucket 3.
2080 * [31:29]-- -ve delta of freq ratio of bucket 3.
2081 * [36:32]-- +ve delta of number of active cores of bucket 4.
2082 * [39:37]-- -ve delta of freq ratio of bucket 4.
2083 * [44:40]-- +ve delta of number of active cores of bucket 5.
2084 * [47:45]-- -ve delta of freq ratio of bucket 5.
2085 * [52:48]-- +ve delta of number of active cores of bucket 6.
2086 * [55:53]-- -ve delta of freq ratio of bucket 6.
2087 * [60:56]-- +ve delta of number of active cores of bucket 7.
2088 * [63:61]-- -ve delta of freq ratio of bucket 7.
2089 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002090
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002091 b_nr = 0;
2092 cores[b_nr] = (msr & 0xFF) >> 1;
2093 ratio[b_nr] = (msr >> 8) & 0xFF;
2094
2095 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002096 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002097 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002098
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002099 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2100 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2101 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002102 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002103
2104 for (i = buckets_no - 1; i >= 0; i--)
2105 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002106 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002107 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002108 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002109}
2110
2111static void
Len Brownfcd17212015-03-23 20:29:09 -04002112dump_nhm_cst_cfg(void)
2113{
2114 unsigned long long msr;
2115
Len Brown1df2e552017-01-07 23:24:57 -05002116 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002117
2118#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
2119#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
2120
Len Brown1df2e552017-01-07 23:24:57 -05002121 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002122
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002123 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002124 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2125 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2126 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2127 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2128 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002129 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002130 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002131
2132#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2133 if (has_automatic_cstate_conversion) {
2134 fprintf(outf, ", automatic c-state conversion=%s",
2135 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2136 }
2137
2138 fprintf(outf, ")\n");
2139
Len Brownfcd17212015-03-23 20:29:09 -04002140 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002141}
2142
Len Brown6fb31432015-06-17 16:23:45 -04002143static void
2144dump_config_tdp(void)
2145{
2146 unsigned long long msr;
2147
2148 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002149 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002150 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002151
2152 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002153 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002154 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002155 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2156 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2157 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2158 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002159 }
Len Brownb7d8c142016-02-13 23:36:17 -05002160 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002161
2162 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002163 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002164 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002165 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2166 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2167 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2168 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002169 }
Len Brownb7d8c142016-02-13 23:36:17 -05002170 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002171
2172 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002173 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002174 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002175 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2176 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2177 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002178
Len Brown6fb31432015-06-17 16:23:45 -04002179 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002180 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002181 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002182 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2183 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002184}
Len Brown5a634262016-04-06 17:15:55 -04002185
2186unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2187
2188void print_irtl(void)
2189{
2190 unsigned long long msr;
2191
2192 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2193 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2194 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2195 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2196
2197 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2198 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2199 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2200 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2201
2202 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2203 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2204 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2205 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2206
2207 if (!do_irtl_hsw)
2208 return;
2209
2210 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2211 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2212 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2213 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2214
2215 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2216 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2217 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2218 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2219
2220 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2221 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2222 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2223 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2224
2225}
Len Brown36229892016-02-26 20:51:02 -05002226void free_fd_percpu(void)
2227{
2228 int i;
2229
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002230 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002231 if (fd_percpu[i] != 0)
2232 close(fd_percpu[i]);
2233 }
2234
2235 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002236}
2237
Len Brownc98d5d92012-06-04 00:56:40 -04002238void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002239{
Len Brownc98d5d92012-06-04 00:56:40 -04002240 CPU_FREE(cpu_present_set);
2241 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002242 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002243
Len Brownc98d5d92012-06-04 00:56:40 -04002244 CPU_FREE(cpu_affinity_set);
2245 cpu_affinity_set = NULL;
2246 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002247
Len Brownc98d5d92012-06-04 00:56:40 -04002248 free(thread_even);
2249 free(core_even);
2250 free(package_even);
2251
2252 thread_even = NULL;
2253 core_even = NULL;
2254 package_even = NULL;
2255
2256 free(thread_odd);
2257 free(core_odd);
2258 free(package_odd);
2259
2260 thread_odd = NULL;
2261 core_odd = NULL;
2262 package_odd = NULL;
2263
2264 free(output_buffer);
2265 output_buffer = NULL;
2266 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002267
2268 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002269
2270 free(irq_column_2_cpu);
2271 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002272}
2273
Josh Triplett57a42a32013-08-20 17:20:17 -07002274
2275/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002276 * Parse a file containing a single int.
2277 */
2278int parse_int_file(const char *fmt, ...)
2279{
2280 va_list args;
2281 char path[PATH_MAX];
2282 FILE *filep;
2283 int value;
2284
2285 va_start(args, fmt);
2286 vsnprintf(path, sizeof(path), fmt, args);
2287 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002288 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002289 if (fscanf(filep, "%d", &value) != 1)
2290 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002291 fclose(filep);
2292 return value;
2293}
2294
2295/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002296 * get_cpu_position_in_core(cpu)
2297 * return the position of the CPU among its HT siblings in the core
2298 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002299 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002300int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002301{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002302 char path[64];
2303 FILE *filep;
2304 int this_cpu;
2305 char character;
2306 int i;
2307
2308 sprintf(path,
2309 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2310 cpu);
2311 filep = fopen(path, "r");
2312 if (filep == NULL) {
2313 perror(path);
2314 exit(1);
2315 }
2316
2317 for (i = 0; i < topo.num_threads_per_core; i++) {
2318 fscanf(filep, "%d", &this_cpu);
2319 if (this_cpu == cpu) {
2320 fclose(filep);
2321 return i;
2322 }
2323
2324 /* Account for no separator after last thread*/
2325 if (i != (topo.num_threads_per_core - 1))
2326 fscanf(filep, "%c", &character);
2327 }
2328
2329 fclose(filep);
2330 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002331}
2332
Len Brownc98d5d92012-06-04 00:56:40 -04002333/*
2334 * cpu_is_first_core_in_package(cpu)
2335 * return 1 if given CPU is 1st core in package
2336 */
2337int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002338{
Josh Triplett95aebc42013-08-20 17:20:16 -07002339 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002340}
2341
2342int get_physical_package_id(int cpu)
2343{
Josh Triplett95aebc42013-08-20 17:20:16 -07002344 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002345}
2346
2347int get_core_id(int cpu)
2348{
Josh Triplett95aebc42013-08-20 17:20:16 -07002349 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002350}
2351
Len Brownc98d5d92012-06-04 00:56:40 -04002352int get_num_ht_siblings(int cpu)
2353{
2354 char path[80];
2355 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002356 int sib1;
2357 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002358 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002359 char str[100];
2360 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002361
2362 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002363 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002364
Len Brownc98d5d92012-06-04 00:56:40 -04002365 /*
2366 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002367 * A ',' separated or '-' separated set of numbers
2368 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002369 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002370 fscanf(filep, "%d%c\n", &sib1, &character);
2371 fseek(filep, 0, SEEK_SET);
2372 fgets(str, 100, filep);
2373 ch = strchr(str, character);
2374 while (ch != NULL) {
2375 matches++;
2376 ch = strchr(ch+1, character);
2377 }
Len Brownc98d5d92012-06-04 00:56:40 -04002378
2379 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002380 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002381}
2382
Len Brown103a8fe2010-10-22 23:53:03 -04002383/*
Len Brownc98d5d92012-06-04 00:56:40 -04002384 * run func(thread, core, package) in topology order
2385 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002386 */
2387
Len Brownc98d5d92012-06-04 00:56:40 -04002388int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2389 struct pkg_data *, struct thread_data *, struct core_data *,
2390 struct pkg_data *), struct thread_data *thread_base,
2391 struct core_data *core_base, struct pkg_data *pkg_base,
2392 struct thread_data *thread_base2, struct core_data *core_base2,
2393 struct pkg_data *pkg_base2)
2394{
2395 int retval, pkg_no, core_no, thread_no;
2396
2397 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2398 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2399 for (thread_no = 0; thread_no <
2400 topo.num_threads_per_core; ++thread_no) {
2401 struct thread_data *t, *t2;
2402 struct core_data *c, *c2;
2403 struct pkg_data *p, *p2;
2404
2405 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2406
2407 if (cpu_is_not_present(t->cpu_id))
2408 continue;
2409
2410 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2411
2412 c = GET_CORE(core_base, core_no, pkg_no);
2413 c2 = GET_CORE(core_base2, core_no, pkg_no);
2414
2415 p = GET_PKG(pkg_base, pkg_no);
2416 p2 = GET_PKG(pkg_base2, pkg_no);
2417
2418 retval = func(t, c, p, t2, c2, p2);
2419 if (retval)
2420 return retval;
2421 }
2422 }
2423 }
2424 return 0;
2425}
2426
2427/*
2428 * run func(cpu) on every cpu in /proc/stat
2429 * return max_cpu number
2430 */
2431int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002432{
2433 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002434 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002435 int retval;
2436
Josh Triplett57a42a32013-08-20 17:20:17 -07002437 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002438
2439 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002440 if (retval != 0)
2441 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002442
Len Brownc98d5d92012-06-04 00:56:40 -04002443 while (1) {
2444 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 -04002445 if (retval != 1)
2446 break;
2447
Len Brownc98d5d92012-06-04 00:56:40 -04002448 retval = func(cpu_num);
2449 if (retval) {
2450 fclose(fp);
2451 return(retval);
2452 }
Len Brown103a8fe2010-10-22 23:53:03 -04002453 }
2454 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002455 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002456}
2457
2458void re_initialize(void)
2459{
Len Brownc98d5d92012-06-04 00:56:40 -04002460 free_all_buffers();
2461 setup_all_buffers();
2462 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002463}
2464
Len Brownc98d5d92012-06-04 00:56:40 -04002465
Len Brown103a8fe2010-10-22 23:53:03 -04002466/*
Len Brownc98d5d92012-06-04 00:56:40 -04002467 * count_cpus()
2468 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002469 */
Len Brownc98d5d92012-06-04 00:56:40 -04002470int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002471{
Len Brownc98d5d92012-06-04 00:56:40 -04002472 if (topo.max_cpu_num < cpu)
2473 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002474
Len Brownc98d5d92012-06-04 00:56:40 -04002475 topo.num_cpus += 1;
2476 return 0;
2477}
2478int mark_cpu_present(int cpu)
2479{
2480 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002481 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002482}
2483
Len Brown562a2d32016-02-26 23:48:05 -05002484/*
2485 * snapshot_proc_interrupts()
2486 *
2487 * read and record summary of /proc/interrupts
2488 *
2489 * return 1 if config change requires a restart, else return 0
2490 */
2491int snapshot_proc_interrupts(void)
2492{
2493 static FILE *fp;
2494 int column, retval;
2495
2496 if (fp == NULL)
2497 fp = fopen_or_die("/proc/interrupts", "r");
2498 else
2499 rewind(fp);
2500
2501 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2502 for (column = 0; column < topo.num_cpus; ++column) {
2503 int cpu_number;
2504
2505 retval = fscanf(fp, " CPU%d", &cpu_number);
2506 if (retval != 1)
2507 break;
2508
2509 if (cpu_number > topo.max_cpu_num) {
2510 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2511 return 1;
2512 }
2513
2514 irq_column_2_cpu[column] = cpu_number;
2515 irqs_per_cpu[cpu_number] = 0;
2516 }
2517
2518 /* read /proc/interrupt count lines and sum up irqs per cpu */
2519 while (1) {
2520 int column;
2521 char buf[64];
2522
2523 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2524 if (retval != 1)
2525 break;
2526
2527 /* read the count per cpu */
2528 for (column = 0; column < topo.num_cpus; ++column) {
2529
2530 int cpu_number, irq_count;
2531
2532 retval = fscanf(fp, " %d", &irq_count);
2533 if (retval != 1)
2534 break;
2535
2536 cpu_number = irq_column_2_cpu[column];
2537 irqs_per_cpu[cpu_number] += irq_count;
2538
2539 }
2540
2541 while (getc(fp) != '\n')
2542 ; /* flush interrupt description */
2543
2544 }
2545 return 0;
2546}
Len Brown27d47352016-02-27 00:37:54 -05002547/*
Len Brownfdf676e2016-02-27 01:28:12 -05002548 * snapshot_gfx_rc6_ms()
2549 *
2550 * record snapshot of
2551 * /sys/class/drm/card0/power/rc6_residency_ms
2552 *
2553 * return 1 if config change requires a restart, else return 0
2554 */
2555int snapshot_gfx_rc6_ms(void)
2556{
2557 FILE *fp;
2558 int retval;
2559
2560 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2561
2562 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2563 if (retval != 1)
2564 err(1, "GFX rc6");
2565
2566 fclose(fp);
2567
2568 return 0;
2569}
2570/*
Len Brown27d47352016-02-27 00:37:54 -05002571 * snapshot_gfx_mhz()
2572 *
2573 * record snapshot of
2574 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2575 *
2576 * return 1 if config change requires a restart, else return 0
2577 */
2578int snapshot_gfx_mhz(void)
2579{
2580 static FILE *fp;
2581 int retval;
2582
2583 if (fp == NULL)
2584 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002585 else {
Len Brown27d47352016-02-27 00:37:54 -05002586 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002587 fflush(fp);
2588 }
Len Brown27d47352016-02-27 00:37:54 -05002589
2590 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2591 if (retval != 1)
2592 err(1, "GFX MHz");
2593
2594 return 0;
2595}
Len Brown562a2d32016-02-26 23:48:05 -05002596
2597/*
2598 * snapshot /proc and /sys files
2599 *
2600 * return 1 if configuration restart needed, else return 0
2601 */
2602int snapshot_proc_sysfs_files(void)
2603{
Len Brown218f0e82017-02-14 22:07:52 -05002604 if (DO_BIC(BIC_IRQ))
2605 if (snapshot_proc_interrupts())
2606 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002607
Len Brown812db3f2017-02-10 00:25:41 -05002608 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002609 snapshot_gfx_rc6_ms();
2610
Len Brown812db3f2017-02-10 00:25:41 -05002611 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002612 snapshot_gfx_mhz();
2613
Len Brown562a2d32016-02-26 23:48:05 -05002614 return 0;
2615}
2616
Len Brown8aa2ed02017-07-15 14:57:37 -04002617int exit_requested;
2618
2619static void signal_handler (int signal)
2620{
2621 switch (signal) {
2622 case SIGINT:
2623 exit_requested = 1;
2624 if (debug)
2625 fprintf(stderr, " SIGINT\n");
2626 break;
Len Brown07211962017-07-15 15:51:26 -04002627 case SIGUSR1:
2628 if (debug > 1)
2629 fprintf(stderr, "SIGUSR1\n");
2630 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002631 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002632 /* make sure this manually-invoked interval is at least 1ms long */
2633 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002634}
2635
2636void setup_signal_handler(void)
2637{
2638 struct sigaction sa;
2639
2640 memset(&sa, 0, sizeof(sa));
2641
2642 sa.sa_handler = &signal_handler;
2643
2644 if (sigaction(SIGINT, &sa, NULL) < 0)
2645 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002646 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2647 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002648}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002649
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002650void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002651{
2652 struct timeval select_timeout;
2653 fd_set readfds;
2654 int retval;
2655
2656 FD_ZERO(&readfds);
2657 FD_SET(0, &readfds);
2658
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002659 if (!isatty(fileno(stdin))) {
2660 nanosleep(&interval_ts, NULL);
2661 return;
2662 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002663
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002664 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002665 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2666
2667 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002668 switch (getc(stdin)) {
2669 case 'q':
2670 exit_requested = 1;
2671 break;
2672 }
2673 /* make sure this manually-invoked interval is at least 1ms long */
2674 nanosleep(&one_msec, NULL);
2675 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002676}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002677
Len Brown103a8fe2010-10-22 23:53:03 -04002678void turbostat_loop()
2679{
Len Brownc98d5d92012-06-04 00:56:40 -04002680 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002681 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002682
Len Brown8aa2ed02017-07-15 14:57:37 -04002683 setup_signal_handler();
2684
Len Brown103a8fe2010-10-22 23:53:03 -04002685restart:
Len Browne52966c2012-11-08 22:38:05 -05002686 restarted++;
2687
Len Brown562a2d32016-02-26 23:48:05 -05002688 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002689 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002690 if (retval < -1) {
2691 exit(retval);
2692 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002693 if (restarted > 1) {
2694 exit(retval);
2695 }
Len Brownc98d5d92012-06-04 00:56:40 -04002696 re_initialize();
2697 goto restart;
2698 }
Len Browne52966c2012-11-08 22:38:05 -05002699 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002700 gettimeofday(&tv_even, (struct timezone *)NULL);
2701
2702 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002703 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002704 re_initialize();
2705 goto restart;
2706 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002707 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002708 if (snapshot_proc_sysfs_files())
2709 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002710 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002711 if (retval < -1) {
2712 exit(retval);
2713 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002714 re_initialize();
2715 goto restart;
2716 }
Len Brown103a8fe2010-10-22 23:53:03 -04002717 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002718 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002719 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2720 re_initialize();
2721 goto restart;
2722 }
Len Brownc98d5d92012-06-04 00:56:40 -04002723 compute_average(EVEN_COUNTERS);
2724 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002725 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002726 if (exit_requested)
2727 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002728 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002729 if (snapshot_proc_sysfs_files())
2730 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002731 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002732 if (retval < -1) {
2733 exit(retval);
2734 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002735 re_initialize();
2736 goto restart;
2737 }
Len Brown103a8fe2010-10-22 23:53:03 -04002738 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002739 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002740 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2741 re_initialize();
2742 goto restart;
2743 }
Len Brownc98d5d92012-06-04 00:56:40 -04002744 compute_average(ODD_COUNTERS);
2745 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002746 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002747 if (exit_requested)
2748 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002749 }
2750}
2751
2752void check_dev_msr()
2753{
2754 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002755 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002756
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002757 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2758 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002759 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2760 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002761}
2762
Len Brown98481e72014-08-15 00:36:50 -04002763void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002764{
Len Brown98481e72014-08-15 00:36:50 -04002765 struct __user_cap_header_struct cap_header_data;
2766 cap_user_header_t cap_header = &cap_header_data;
2767 struct __user_cap_data_struct cap_data_data;
2768 cap_user_data_t cap_data = &cap_data_data;
2769 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2770 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002771 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002772
2773 /* check for CAP_SYS_RAWIO */
2774 cap_header->pid = getpid();
2775 cap_header->version = _LINUX_CAPABILITY_VERSION;
2776 if (capget(cap_header, cap_data) < 0)
2777 err(-6, "capget(2) failed");
2778
2779 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2780 do_exit++;
2781 warnx("capget(CAP_SYS_RAWIO) failed,"
2782 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2783 }
2784
2785 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002786 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2787 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002788 do_exit++;
2789 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2790 }
2791
2792 /* if all else fails, thell them to be root */
2793 if (do_exit)
2794 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002795 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002796
2797 if (do_exit)
2798 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002799}
2800
Len Brownd7899442015-01-23 00:12:33 -05002801/*
2802 * NHM adds support for additional MSRs:
2803 *
2804 * MSR_SMI_COUNT 0x00000034
2805 *
Len Brownec0adc52015-11-12 02:42:31 -05002806 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002807 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002808 *
Len Browncf4cbe52017-01-01 13:08:33 -05002809 * MSR_MISC_PWR_MGMT 0x000001aa
2810 *
Len Brownd7899442015-01-23 00:12:33 -05002811 * MSR_PKG_C3_RESIDENCY 0x000003f8
2812 * MSR_PKG_C6_RESIDENCY 0x000003f9
2813 * MSR_CORE_C3_RESIDENCY 0x000003fc
2814 * MSR_CORE_C6_RESIDENCY 0x000003fd
2815 *
Len Brownee7e38e2015-02-09 23:39:45 -05002816 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002817 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002818 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002819 */
Len Brownee7e38e2015-02-09 23:39:45 -05002820int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002821{
Len Brownee7e38e2015-02-09 23:39:45 -05002822 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002823 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002824 int *pkg_cstate_limits;
2825
Len Brown103a8fe2010-10-22 23:53:03 -04002826 if (!genuine_intel)
2827 return 0;
2828
2829 if (family != 6)
2830 return 0;
2831
Len Brown21ed5572015-10-19 22:37:40 -04002832 bclk = discover_bclk(family, model);
2833
Len Brown103a8fe2010-10-22 23:53:03 -04002834 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002835 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2836 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002837 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002838 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2839 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2840 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2841 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002842 pkg_cstate_limits = nhm_pkg_cstate_limits;
2843 break;
Len Brown869ce692016-06-16 23:22:37 -04002844 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2845 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2846 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2847 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002848 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002849 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002850 break;
Len Brown869ce692016-06-16 23:22:37 -04002851 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2852 case INTEL_FAM6_HASWELL_X: /* HSX */
2853 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2854 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2855 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2856 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2857 case INTEL_FAM6_BROADWELL_X: /* BDX */
2858 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2859 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2860 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2861 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2862 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002863 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002864 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002865 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002866 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2867 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002868 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002869 break;
Len Brown869ce692016-06-16 23:22:37 -04002870 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002871 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002872 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002873 pkg_cstate_limits = slv_pkg_cstate_limits;
2874 break;
Len Brown869ce692016-06-16 23:22:37 -04002875 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002876 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002877 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002878 break;
Len Brown869ce692016-06-16 23:22:37 -04002879 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002880 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002881 pkg_cstate_limits = phi_pkg_cstate_limits;
2882 break;
Len Brown869ce692016-06-16 23:22:37 -04002883 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002884 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002885 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002886 pkg_cstate_limits = bxt_pkg_cstate_limits;
2887 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002888 default:
2889 return 0;
2890 }
Len Brown1df2e552017-01-07 23:24:57 -05002891 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002892 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002893
Len Brownec0adc52015-11-12 02:42:31 -05002894 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002895 base_ratio = (msr >> 8) & 0xFF;
2896
2897 base_hz = base_ratio * bclk * 1000000;
2898 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002899 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002900}
Len Brown0f7887c2017-01-12 23:49:18 -05002901/*
Len Brown495c76542017-02-08 02:41:51 -05002902 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002903 *
2904 * MSR_CC6_DEMOTION_POLICY_CONFIG
2905 * MSR_MC6_DEMOTION_POLICY_CONFIG
2906 */
2907
2908int has_slv_msrs(unsigned int family, unsigned int model)
2909{
2910 if (!genuine_intel)
2911 return 0;
2912
2913 switch (model) {
2914 case INTEL_FAM6_ATOM_SILVERMONT1:
2915 case INTEL_FAM6_ATOM_MERRIFIELD:
2916 case INTEL_FAM6_ATOM_MOOREFIELD:
2917 return 1;
2918 }
2919 return 0;
2920}
Len Brown7170a372017-01-27 02:13:27 -05002921int is_dnv(unsigned int family, unsigned int model)
2922{
2923
2924 if (!genuine_intel)
2925 return 0;
2926
2927 switch (model) {
2928 case INTEL_FAM6_ATOM_DENVERTON:
2929 return 1;
2930 }
2931 return 0;
2932}
Len Brownade0eba2017-02-10 01:56:47 -05002933int is_bdx(unsigned int family, unsigned int model)
2934{
2935
2936 if (!genuine_intel)
2937 return 0;
2938
2939 switch (model) {
2940 case INTEL_FAM6_BROADWELL_X:
2941 case INTEL_FAM6_BROADWELL_XEON_D:
2942 return 1;
2943 }
2944 return 0;
2945}
Len Brown34c761972017-01-27 02:36:41 -05002946int is_skx(unsigned int family, unsigned int model)
2947{
2948
2949 if (!genuine_intel)
2950 return 0;
2951
2952 switch (model) {
2953 case INTEL_FAM6_SKYLAKE_X:
2954 return 1;
2955 }
2956 return 0;
2957}
Len Brown0f7887c2017-01-12 23:49:18 -05002958
Len Brown31e07522017-01-31 23:07:49 -05002959int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002960{
Len Brown0f7887c2017-01-12 23:49:18 -05002961 if (has_slv_msrs(family, model))
2962 return 0;
2963
Len Brownd7899442015-01-23 00:12:33 -05002964 switch (model) {
2965 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002966 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2967 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2968 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002969 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002970 return 0;
2971 default:
2972 return 1;
2973 }
2974}
Len Brown0f7887c2017-01-12 23:49:18 -05002975int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2976{
2977 if (has_slv_msrs(family, model))
2978 return 1;
2979
2980 return 0;
2981}
Len Brown6574a5d2012-09-21 00:01:31 -04002982int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2983{
2984 if (!genuine_intel)
2985 return 0;
2986
2987 if (family != 6)
2988 return 0;
2989
2990 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002991 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2992 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002993 return 1;
2994 default:
2995 return 0;
2996 }
2997}
Len Brownfcd17212015-03-23 20:29:09 -04002998int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2999{
3000 if (!genuine_intel)
3001 return 0;
3002
3003 if (family != 6)
3004 return 0;
3005
3006 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003007 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003008 return 1;
3009 default:
3010 return 0;
3011 }
3012}
3013
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003014int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3015{
3016 if (!genuine_intel)
3017 return 0;
3018
3019 if (family != 6)
3020 return 0;
3021
3022 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003023 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003024 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003025 return 1;
3026 default:
3027 return 0;
3028 }
3029}
Len Brown31e07522017-01-31 23:07:49 -05003030int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3031{
3032 if (!genuine_intel)
3033 return 0;
3034
3035 if (family != 6)
3036 return 0;
3037
3038 switch (model) {
3039 case INTEL_FAM6_ATOM_GOLDMONT:
3040 case INTEL_FAM6_SKYLAKE_X:
3041 return 1;
3042 default:
3043 return 0;
3044 }
3045}
Len Brown6fb31432015-06-17 16:23:45 -04003046int has_config_tdp(unsigned int family, unsigned int model)
3047{
3048 if (!genuine_intel)
3049 return 0;
3050
3051 if (family != 6)
3052 return 0;
3053
3054 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003055 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3056 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3057 case INTEL_FAM6_HASWELL_X: /* HSX */
3058 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3059 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3060 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3061 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3062 case INTEL_FAM6_BROADWELL_X: /* BDX */
3063 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3064 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3065 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3066 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3067 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3068 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003069
Len Brown869ce692016-06-16 23:22:37 -04003070 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003071 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003072 return 1;
3073 default:
3074 return 0;
3075 }
3076}
3077
Len Brownfcd17212015-03-23 20:29:09 -04003078static void
Colin Ian King1b693172016-03-02 13:50:25 +00003079dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003080{
3081 if (!do_nhm_platform_info)
3082 return;
3083
3084 dump_nhm_platform_info();
3085
3086 if (has_hsw_turbo_ratio_limit(family, model))
3087 dump_hsw_turbo_ratio_limits();
3088
3089 if (has_ivt_turbo_ratio_limit(family, model))
3090 dump_ivt_turbo_ratio_limits();
3091
Len Brown31e07522017-01-31 23:07:49 -05003092 if (has_turbo_ratio_limit(family, model))
3093 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003094
Len Brown0f7887c2017-01-12 23:49:18 -05003095 if (has_atom_turbo_ratio_limit(family, model))
3096 dump_atom_turbo_ratio_limits();
3097
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003098 if (has_knl_turbo_ratio_limit(family, model))
3099 dump_knl_turbo_ratio_limits();
3100
Len Brown6fb31432015-06-17 16:23:45 -04003101 if (has_config_tdp(family, model))
3102 dump_config_tdp();
3103
Len Brownfcd17212015-03-23 20:29:09 -04003104 dump_nhm_cst_cfg();
3105}
3106
Len Brown41618e62017-02-09 18:25:22 -05003107static void
3108dump_sysfs_cstate_config(void)
3109{
3110 char path[64];
3111 char name_buf[16];
3112 char desc[64];
3113 FILE *input;
3114 int state;
3115 char *sp;
3116
3117 if (!DO_BIC(BIC_sysfs))
3118 return;
3119
3120 for (state = 0; state < 10; ++state) {
3121
3122 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3123 base_cpu, state);
3124 input = fopen(path, "r");
3125 if (input == NULL)
3126 continue;
3127 fgets(name_buf, sizeof(name_buf), input);
3128
3129 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3130 sp = strchr(name_buf, '-');
3131 if (!sp)
3132 sp = strchrnul(name_buf, '\n');
3133 *sp = '\0';
3134
3135 fclose(input);
3136
3137 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3138 base_cpu, state);
3139 input = fopen(path, "r");
3140 if (input == NULL)
3141 continue;
3142 fgets(desc, sizeof(desc), input);
3143
3144 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3145 fclose(input);
3146 }
3147}
Len Brown7293fcc2017-02-22 00:11:12 -05003148static void
3149dump_sysfs_pstate_config(void)
3150{
3151 char path[64];
3152 char driver_buf[64];
3153 char governor_buf[64];
3154 FILE *input;
3155 int turbo;
3156
3157 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3158 base_cpu);
3159 input = fopen(path, "r");
3160 if (input == NULL) {
3161 fprintf(stderr, "NSFOD %s\n", path);
3162 return;
3163 }
3164 fgets(driver_buf, sizeof(driver_buf), input);
3165 fclose(input);
3166
3167 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3168 base_cpu);
3169 input = fopen(path, "r");
3170 if (input == NULL) {
3171 fprintf(stderr, "NSFOD %s\n", path);
3172 return;
3173 }
3174 fgets(governor_buf, sizeof(governor_buf), input);
3175 fclose(input);
3176
3177 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3178 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3179
3180 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3181 input = fopen(path, "r");
3182 if (input != NULL) {
3183 fscanf(input, "%d", &turbo);
3184 fprintf(outf, "cpufreq boost: %d\n", turbo);
3185 fclose(input);
3186 }
3187
3188 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3189 input = fopen(path, "r");
3190 if (input != NULL) {
3191 fscanf(input, "%d", &turbo);
3192 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3193 fclose(input);
3194 }
3195}
Len Brown41618e62017-02-09 18:25:22 -05003196
Len Brown6574a5d2012-09-21 00:01:31 -04003197
Len Brown889facb2012-11-08 00:48:57 -05003198/*
3199 * print_epb()
3200 * Decode the ENERGY_PERF_BIAS MSR
3201 */
3202int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3203{
3204 unsigned long long msr;
3205 char *epb_string;
3206 int cpu;
3207
3208 if (!has_epb)
3209 return 0;
3210
3211 cpu = t->cpu_id;
3212
3213 /* EPB is per-package */
3214 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3215 return 0;
3216
3217 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003218 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003219 return -1;
3220 }
3221
3222 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3223 return 0;
3224
Len Browne9be7dd2015-05-26 12:19:37 -04003225 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003226 case ENERGY_PERF_BIAS_PERFORMANCE:
3227 epb_string = "performance";
3228 break;
3229 case ENERGY_PERF_BIAS_NORMAL:
3230 epb_string = "balanced";
3231 break;
3232 case ENERGY_PERF_BIAS_POWERSAVE:
3233 epb_string = "powersave";
3234 break;
3235 default:
3236 epb_string = "custom";
3237 break;
3238 }
Len Brownb7d8c142016-02-13 23:36:17 -05003239 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003240
3241 return 0;
3242}
Len Brown7f5c2582015-12-01 01:36:39 -05003243/*
3244 * print_hwp()
3245 * Decode the MSR_HWP_CAPABILITIES
3246 */
3247int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3248{
3249 unsigned long long msr;
3250 int cpu;
3251
3252 if (!has_hwp)
3253 return 0;
3254
3255 cpu = t->cpu_id;
3256
3257 /* MSR_HWP_CAPABILITIES is per-package */
3258 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3259 return 0;
3260
3261 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003262 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003263 return -1;
3264 }
3265
3266 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3267 return 0;
3268
Len Brownb7d8c142016-02-13 23:36:17 -05003269 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003270 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3271
3272 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3273 if ((msr & (1 << 0)) == 0)
3274 return 0;
3275
3276 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3277 return 0;
3278
Len Brownb7d8c142016-02-13 23:36:17 -05003279 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003280 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003281 cpu, msr,
3282 (unsigned int)HWP_HIGHEST_PERF(msr),
3283 (unsigned int)HWP_GUARANTEED_PERF(msr),
3284 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3285 (unsigned int)HWP_LOWEST_PERF(msr));
3286
3287 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3288 return 0;
3289
Len Brownb7d8c142016-02-13 23:36:17 -05003290 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003291 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003292 cpu, msr,
3293 (unsigned int)(((msr) >> 0) & 0xff),
3294 (unsigned int)(((msr) >> 8) & 0xff),
3295 (unsigned int)(((msr) >> 16) & 0xff),
3296 (unsigned int)(((msr) >> 24) & 0xff),
3297 (unsigned int)(((msr) >> 32) & 0xff3),
3298 (unsigned int)(((msr) >> 42) & 0x1));
3299
3300 if (has_hwp_pkg) {
3301 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3302 return 0;
3303
Len Brownb7d8c142016-02-13 23:36:17 -05003304 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003305 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003306 cpu, msr,
3307 (unsigned int)(((msr) >> 0) & 0xff),
3308 (unsigned int)(((msr) >> 8) & 0xff),
3309 (unsigned int)(((msr) >> 16) & 0xff),
3310 (unsigned int)(((msr) >> 24) & 0xff),
3311 (unsigned int)(((msr) >> 32) & 0xff3));
3312 }
3313 if (has_hwp_notify) {
3314 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3315 return 0;
3316
Len Brownb7d8c142016-02-13 23:36:17 -05003317 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003318 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3319 cpu, msr,
3320 ((msr) & 0x1) ? "EN" : "Dis",
3321 ((msr) & 0x2) ? "EN" : "Dis");
3322 }
3323 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3324 return 0;
3325
Len Brownb7d8c142016-02-13 23:36:17 -05003326 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003327 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3328 cpu, msr,
3329 ((msr) & 0x1) ? "" : "No-",
3330 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003331
3332 return 0;
3333}
3334
Len Brown3a9a9412014-08-15 02:39:52 -04003335/*
3336 * print_perf_limit()
3337 */
3338int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3339{
3340 unsigned long long msr;
3341 int cpu;
3342
3343 cpu = t->cpu_id;
3344
3345 /* per-package */
3346 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3347 return 0;
3348
3349 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003350 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003351 return -1;
3352 }
3353
3354 if (do_core_perf_limit_reasons) {
3355 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003356 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3357 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003358 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003359 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003360 (msr & 1 << 13) ? "Transitions, " : "",
3361 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3362 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3363 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3364 (msr & 1 << 9) ? "CorePwr, " : "",
3365 (msr & 1 << 8) ? "Amps, " : "",
3366 (msr & 1 << 6) ? "VR-Therm, " : "",
3367 (msr & 1 << 5) ? "Auto-HWP, " : "",
3368 (msr & 1 << 4) ? "Graphics, " : "",
3369 (msr & 1 << 2) ? "bit2, " : "",
3370 (msr & 1 << 1) ? "ThermStatus, " : "",
3371 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003372 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 -04003373 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003374 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003375 (msr & 1 << 29) ? "Transitions, " : "",
3376 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3377 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3378 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3379 (msr & 1 << 25) ? "CorePwr, " : "",
3380 (msr & 1 << 24) ? "Amps, " : "",
3381 (msr & 1 << 22) ? "VR-Therm, " : "",
3382 (msr & 1 << 21) ? "Auto-HWP, " : "",
3383 (msr & 1 << 20) ? "Graphics, " : "",
3384 (msr & 1 << 18) ? "bit18, " : "",
3385 (msr & 1 << 17) ? "ThermStatus, " : "",
3386 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003387
3388 }
3389 if (do_gfx_perf_limit_reasons) {
3390 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003391 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3392 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003393 (msr & 1 << 0) ? "PROCHOT, " : "",
3394 (msr & 1 << 1) ? "ThermStatus, " : "",
3395 (msr & 1 << 4) ? "Graphics, " : "",
3396 (msr & 1 << 6) ? "VR-Therm, " : "",
3397 (msr & 1 << 8) ? "Amps, " : "",
3398 (msr & 1 << 9) ? "GFXPwr, " : "",
3399 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3400 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003401 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003402 (msr & 1 << 16) ? "PROCHOT, " : "",
3403 (msr & 1 << 17) ? "ThermStatus, " : "",
3404 (msr & 1 << 20) ? "Graphics, " : "",
3405 (msr & 1 << 22) ? "VR-Therm, " : "",
3406 (msr & 1 << 24) ? "Amps, " : "",
3407 (msr & 1 << 25) ? "GFXPwr, " : "",
3408 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3409 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3410 }
3411 if (do_ring_perf_limit_reasons) {
3412 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003413 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3414 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003415 (msr & 1 << 0) ? "PROCHOT, " : "",
3416 (msr & 1 << 1) ? "ThermStatus, " : "",
3417 (msr & 1 << 6) ? "VR-Therm, " : "",
3418 (msr & 1 << 8) ? "Amps, " : "",
3419 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3420 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003421 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003422 (msr & 1 << 16) ? "PROCHOT, " : "",
3423 (msr & 1 << 17) ? "ThermStatus, " : "",
3424 (msr & 1 << 22) ? "VR-Therm, " : "",
3425 (msr & 1 << 24) ? "Amps, " : "",
3426 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3427 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3428 }
3429 return 0;
3430}
3431
Len Brown889facb2012-11-08 00:48:57 -05003432#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3433#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3434
Colin Ian King1b693172016-03-02 13:50:25 +00003435double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003436{
3437 unsigned long long msr;
3438
3439 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003440 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003441 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3442
3443 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003444 case INTEL_FAM6_ATOM_SILVERMONT1:
3445 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003446 return 30.0;
3447 default:
3448 return 135.0;
3449 }
3450}
3451
Andrey Semin40ee8e32014-12-05 00:07:00 -05003452/*
3453 * rapl_dram_energy_units_probe()
3454 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3455 */
3456static double
3457rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3458{
3459 /* only called for genuine_intel, family 6 */
3460
3461 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003462 case INTEL_FAM6_HASWELL_X: /* HSX */
3463 case INTEL_FAM6_BROADWELL_X: /* BDX */
3464 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3465 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003466 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003467 return (rapl_dram_energy_units = 15.3 / 1000000);
3468 default:
3469 return (rapl_energy_units);
3470 }
3471}
3472
Len Brown144b44b2013-11-09 00:30:16 -05003473
Len Brown889facb2012-11-08 00:48:57 -05003474/*
3475 * rapl_probe()
3476 *
Len Brown144b44b2013-11-09 00:30:16 -05003477 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003478 */
3479void rapl_probe(unsigned int family, unsigned int model)
3480{
3481 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003482 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003483 double tdp;
3484
3485 if (!genuine_intel)
3486 return;
3487
3488 if (family != 6)
3489 return;
3490
3491 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003492 case INTEL_FAM6_SANDYBRIDGE:
3493 case INTEL_FAM6_IVYBRIDGE:
3494 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3495 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3496 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3497 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3498 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003499 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003500 if (rapl_joules) {
3501 BIC_PRESENT(BIC_Pkg_J);
3502 BIC_PRESENT(BIC_Cor_J);
3503 BIC_PRESENT(BIC_GFX_J);
3504 } else {
3505 BIC_PRESENT(BIC_PkgWatt);
3506 BIC_PRESENT(BIC_CorWatt);
3507 BIC_PRESENT(BIC_GFXWatt);
3508 }
Len Brown889facb2012-11-08 00:48:57 -05003509 break;
Len Brown869ce692016-06-16 23:22:37 -04003510 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003511 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003512 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003513 if (rapl_joules)
3514 BIC_PRESENT(BIC_Pkg_J);
3515 else
3516 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003517 break;
Len Brown869ce692016-06-16 23:22:37 -04003518 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3519 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3520 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3521 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown81824922017-03-04 17:23:07 -05003522 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 -05003523 BIC_PRESENT(BIC_PKG__);
3524 BIC_PRESENT(BIC_RAM__);
3525 if (rapl_joules) {
3526 BIC_PRESENT(BIC_Pkg_J);
3527 BIC_PRESENT(BIC_Cor_J);
3528 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003529 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003530 } else {
3531 BIC_PRESENT(BIC_PkgWatt);
3532 BIC_PRESENT(BIC_CorWatt);
3533 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003534 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003535 }
Len Brown0b2bb692015-03-26 00:50:30 -04003536 break;
Len Brown869ce692016-06-16 23:22:37 -04003537 case INTEL_FAM6_HASWELL_X: /* HSX */
3538 case INTEL_FAM6_BROADWELL_X: /* BDX */
3539 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3540 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3541 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003542 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003543 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 -05003544 BIC_PRESENT(BIC_PKG__);
3545 BIC_PRESENT(BIC_RAM__);
3546 if (rapl_joules) {
3547 BIC_PRESENT(BIC_Pkg_J);
3548 BIC_PRESENT(BIC_RAM_J);
3549 } else {
3550 BIC_PRESENT(BIC_PkgWatt);
3551 BIC_PRESENT(BIC_RAMWatt);
3552 }
Len Browne6f9bb32013-12-03 02:19:19 -05003553 break;
Len Brown869ce692016-06-16 23:22:37 -04003554 case INTEL_FAM6_SANDYBRIDGE_X:
3555 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003556 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 -05003557 BIC_PRESENT(BIC_PKG__);
3558 BIC_PRESENT(BIC_RAM__);
3559 if (rapl_joules) {
3560 BIC_PRESENT(BIC_Pkg_J);
3561 BIC_PRESENT(BIC_Cor_J);
3562 BIC_PRESENT(BIC_RAM_J);
3563 } else {
3564 BIC_PRESENT(BIC_PkgWatt);
3565 BIC_PRESENT(BIC_CorWatt);
3566 BIC_PRESENT(BIC_RAMWatt);
3567 }
Len Brown144b44b2013-11-09 00:30:16 -05003568 break;
Len Brown869ce692016-06-16 23:22:37 -04003569 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3570 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003571 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003572 if (rapl_joules) {
3573 BIC_PRESENT(BIC_Pkg_J);
3574 BIC_PRESENT(BIC_Cor_J);
3575 } else {
3576 BIC_PRESENT(BIC_PkgWatt);
3577 BIC_PRESENT(BIC_CorWatt);
3578 }
Len Brown889facb2012-11-08 00:48:57 -05003579 break;
Len Brown869ce692016-06-16 23:22:37 -04003580 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003581 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 -05003582 BIC_PRESENT(BIC_PKG__);
3583 BIC_PRESENT(BIC_RAM__);
3584 if (rapl_joules) {
3585 BIC_PRESENT(BIC_Pkg_J);
3586 BIC_PRESENT(BIC_Cor_J);
3587 BIC_PRESENT(BIC_RAM_J);
3588 } else {
3589 BIC_PRESENT(BIC_PkgWatt);
3590 BIC_PRESENT(BIC_CorWatt);
3591 BIC_PRESENT(BIC_RAMWatt);
3592 }
Jacob Pan0f644902016-06-16 09:48:22 -07003593 break;
Len Brown889facb2012-11-08 00:48:57 -05003594 default:
3595 return;
3596 }
3597
3598 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003599 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003600 return;
3601
3602 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003603 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003604 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3605 else
3606 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003607
Andrey Semin40ee8e32014-12-05 00:07:00 -05003608 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3609
Len Brown144b44b2013-11-09 00:30:16 -05003610 time_unit = msr >> 16 & 0xF;
3611 if (time_unit == 0)
3612 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003613
Len Brown144b44b2013-11-09 00:30:16 -05003614 rapl_time_units = 1.0 / (1 << (time_unit));
3615
3616 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003617
3618 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003619 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003620 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003621
3622 return;
3623}
3624
Colin Ian King1b693172016-03-02 13:50:25 +00003625void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003626{
3627 if (!genuine_intel)
3628 return;
3629
3630 if (family != 6)
3631 return;
3632
3633 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003634 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3635 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3636 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003637 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003638 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003639 do_core_perf_limit_reasons = 1;
3640 do_ring_perf_limit_reasons = 1;
3641 default:
3642 return;
3643 }
3644}
3645
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003646void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3647{
3648 if (is_skx(family, model) || is_bdx(family, model))
3649 has_automatic_cstate_conversion = 1;
3650}
3651
Len Brown889facb2012-11-08 00:48:57 -05003652int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3653{
3654 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003655 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003656 int cpu;
3657
3658 if (!(do_dts || do_ptm))
3659 return 0;
3660
3661 cpu = t->cpu_id;
3662
3663 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003664 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003665 return 0;
3666
3667 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003668 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003669 return -1;
3670 }
3671
3672 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3673 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3674 return 0;
3675
3676 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003677 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003678 cpu, msr, tcc_activation_temp - dts);
3679
Len Brown889facb2012-11-08 00:48:57 -05003680 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3681 return 0;
3682
3683 dts = (msr >> 16) & 0x7F;
3684 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003685 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003686 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003687 }
3688
3689
Len Brownf4896fa52017-03-04 18:10:45 -05003690 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003691 unsigned int resolution;
3692
3693 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3694 return 0;
3695
3696 dts = (msr >> 16) & 0x7F;
3697 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003698 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003699 cpu, msr, tcc_activation_temp - dts, resolution);
3700
Len Brown889facb2012-11-08 00:48:57 -05003701 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3702 return 0;
3703
3704 dts = (msr >> 16) & 0x7F;
3705 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003706 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003707 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003708 }
3709
3710 return 0;
3711}
Len Brown36229892016-02-26 20:51:02 -05003712
Len Brown889facb2012-11-08 00:48:57 -05003713void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3714{
Len Brownb7d8c142016-02-13 23:36:17 -05003715 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003716 cpu, label,
3717 ((msr >> 15) & 1) ? "EN" : "DIS",
3718 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3719 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3720 (((msr >> 16) & 1) ? "EN" : "DIS"));
3721
3722 return;
3723}
3724
3725int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3726{
3727 unsigned long long msr;
3728 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003729
3730 if (!do_rapl)
3731 return 0;
3732
3733 /* RAPL counters are per package, so print only for 1st thread/package */
3734 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3735 return 0;
3736
3737 cpu = t->cpu_id;
3738 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003739 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003740 return -1;
3741 }
3742
3743 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3744 return -1;
3745
Len Brown96e47152017-01-21 02:26:00 -05003746 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3747 rapl_power_units, rapl_energy_units, rapl_time_units);
3748
Len Brown144b44b2013-11-09 00:30:16 -05003749 if (do_rapl & RAPL_PKG_POWER_INFO) {
3750
Len Brown889facb2012-11-08 00:48:57 -05003751 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3752 return -5;
3753
3754
Len Brownb7d8c142016-02-13 23:36:17 -05003755 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 -05003756 cpu, msr,
3757 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3758 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3759 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3760 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3761
Len Brown144b44b2013-11-09 00:30:16 -05003762 }
3763 if (do_rapl & RAPL_PKG) {
3764
Len Brown889facb2012-11-08 00:48:57 -05003765 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3766 return -9;
3767
Len Brownb7d8c142016-02-13 23:36:17 -05003768 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003769 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003770
3771 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003772 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003773 cpu,
3774 ((msr >> 47) & 1) ? "EN" : "DIS",
3775 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3776 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3777 ((msr >> 48) & 1) ? "EN" : "DIS");
3778 }
3779
Len Brown0b2bb692015-03-26 00:50:30 -04003780 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003781 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3782 return -6;
3783
Len Brownb7d8c142016-02-13 23:36:17 -05003784 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 -05003785 cpu, msr,
3786 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3787 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3788 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3789 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003790 }
3791 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003792 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3793 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003794 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003795 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003796
3797 print_power_limit_msr(cpu, msr, "DRAM Limit");
3798 }
Len Brown144b44b2013-11-09 00:30:16 -05003799 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003800 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3801 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003802
Len Brown96e47152017-01-21 02:26:00 -05003803 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003804 }
Jacob Pan91484942016-06-16 09:48:20 -07003805 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003806 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3807 return -9;
3808 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3809 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3810 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003811 }
3812 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003813 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3814 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003815
Len Brown96e47152017-01-21 02:26:00 -05003816 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003817
Len Brown96e47152017-01-21 02:26:00 -05003818 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3819 return -9;
3820 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3821 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3822 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003823 }
3824 return 0;
3825}
3826
Len Brownd7899442015-01-23 00:12:33 -05003827/*
3828 * SNB adds support for additional MSRs:
3829 *
3830 * MSR_PKG_C7_RESIDENCY 0x000003fa
3831 * MSR_CORE_C7_RESIDENCY 0x000003fe
3832 * MSR_PKG_C2_RESIDENCY 0x0000060d
3833 */
Len Brown103a8fe2010-10-22 23:53:03 -04003834
Len Brownd7899442015-01-23 00:12:33 -05003835int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003836{
3837 if (!genuine_intel)
3838 return 0;
3839
3840 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003841 case INTEL_FAM6_SANDYBRIDGE:
3842 case INTEL_FAM6_SANDYBRIDGE_X:
3843 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3844 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3845 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3846 case INTEL_FAM6_HASWELL_X: /* HSW */
3847 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3848 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3849 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3850 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3851 case INTEL_FAM6_BROADWELL_X: /* BDX */
3852 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3853 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3854 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3855 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3856 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3857 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3858 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003859 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003860 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003861 return 1;
3862 }
3863 return 0;
3864}
3865
Len Brownd7899442015-01-23 00:12:33 -05003866/*
3867 * HSW adds support for additional MSRs:
3868 *
Len Brown5a634262016-04-06 17:15:55 -04003869 * MSR_PKG_C8_RESIDENCY 0x00000630
3870 * MSR_PKG_C9_RESIDENCY 0x00000631
3871 * MSR_PKG_C10_RESIDENCY 0x00000632
3872 *
3873 * MSR_PKGC8_IRTL 0x00000633
3874 * MSR_PKGC9_IRTL 0x00000634
3875 * MSR_PKGC10_IRTL 0x00000635
3876 *
Len Brownd7899442015-01-23 00:12:33 -05003877 */
3878int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003879{
3880 if (!genuine_intel)
3881 return 0;
3882
3883 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003884 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3885 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3886 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3887 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3888 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3889 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3890 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003891 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003892 return 1;
3893 }
3894 return 0;
3895}
3896
Len Brown0b2bb692015-03-26 00:50:30 -04003897/*
3898 * SKL adds support for additional MSRS:
3899 *
3900 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3901 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3902 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3903 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3904 */
3905int has_skl_msrs(unsigned int family, unsigned int model)
3906{
3907 if (!genuine_intel)
3908 return 0;
3909
3910 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003911 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3912 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3913 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3914 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003915 return 1;
3916 }
3917 return 0;
3918}
3919
Len Brown144b44b2013-11-09 00:30:16 -05003920int is_slm(unsigned int family, unsigned int model)
3921{
3922 if (!genuine_intel)
3923 return 0;
3924 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003925 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3926 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003927 return 1;
3928 }
3929 return 0;
3930}
3931
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003932int is_knl(unsigned int family, unsigned int model)
3933{
3934 if (!genuine_intel)
3935 return 0;
3936 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003937 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003938 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003939 return 1;
3940 }
3941 return 0;
3942}
3943
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003944unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3945{
3946 if (is_knl(family, model))
3947 return 1024;
3948 return 1;
3949}
3950
Len Brown144b44b2013-11-09 00:30:16 -05003951#define SLM_BCLK_FREQS 5
3952double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3953
3954double slm_bclk(void)
3955{
3956 unsigned long long msr = 3;
3957 unsigned int i;
3958 double freq;
3959
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003960 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003961 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003962
3963 i = msr & 0xf;
3964 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003965 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003966 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003967 }
3968 freq = slm_freq_table[i];
3969
Len Brown96e47152017-01-21 02:26:00 -05003970 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003971 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003972
3973 return freq;
3974}
3975
Len Brown103a8fe2010-10-22 23:53:03 -04003976double discover_bclk(unsigned int family, unsigned int model)
3977{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003978 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003979 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003980 else if (is_slm(family, model))
3981 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003982 else
3983 return 133.33;
3984}
3985
Len Brown889facb2012-11-08 00:48:57 -05003986/*
3987 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3988 * the Thermal Control Circuit (TCC) activates.
3989 * This is usually equal to tjMax.
3990 *
3991 * Older processors do not have this MSR, so there we guess,
3992 * but also allow cmdline over-ride with -T.
3993 *
3994 * Several MSR temperature values are in units of degrees-C
3995 * below this value, including the Digital Thermal Sensor (DTS),
3996 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3997 */
3998int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3999{
4000 unsigned long long msr;
4001 unsigned int target_c_local;
4002 int cpu;
4003
4004 /* tcc_activation_temp is used only for dts or ptm */
4005 if (!(do_dts || do_ptm))
4006 return 0;
4007
4008 /* this is a per-package concept */
4009 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4010 return 0;
4011
4012 cpu = t->cpu_id;
4013 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004014 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004015 return -1;
4016 }
4017
4018 if (tcc_activation_temp_override != 0) {
4019 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004020 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004021 cpu, tcc_activation_temp);
4022 return 0;
4023 }
4024
4025 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004026 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004027 goto guess;
4028
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004029 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004030 goto guess;
4031
Jean Delvare34821242014-05-01 11:40:19 +02004032 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004033
Len Brown96e47152017-01-21 02:26:00 -05004034 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004035 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004036 cpu, msr, target_c_local);
4037
Jean Delvare34821242014-05-01 11:40:19 +02004038 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004039 goto guess;
4040
4041 tcc_activation_temp = target_c_local;
4042
4043 return 0;
4044
4045guess:
4046 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004047 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004048 cpu, tcc_activation_temp);
4049
4050 return 0;
4051}
Len Brown69807a62015-11-21 12:22:47 -05004052
Len Brownaa8d8cc2016-03-11 13:26:03 -05004053void decode_feature_control_msr(void)
4054{
4055 unsigned long long msr;
4056
4057 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4058 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4059 base_cpu, msr,
4060 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4061 msr & (1 << 18) ? "SGX" : "");
4062}
4063
Len Brown69807a62015-11-21 12:22:47 -05004064void decode_misc_enable_msr(void)
4065{
4066 unsigned long long msr;
4067
Len Brownf26b1512017-06-23 20:45:54 -07004068 if (!genuine_intel)
4069 return;
4070
Len Brown69807a62015-11-21 12:22:47 -05004071 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004072 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004073 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004074 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4075 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004076 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004077 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4078 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004079}
4080
Len Brown33148d62017-01-21 01:26:16 -05004081void decode_misc_feature_control(void)
4082{
4083 unsigned long long msr;
4084
4085 if (!has_misc_feature_control)
4086 return;
4087
4088 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4089 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4090 base_cpu, msr,
4091 msr & (0 << 0) ? "No-" : "",
4092 msr & (1 << 0) ? "No-" : "",
4093 msr & (2 << 0) ? "No-" : "",
4094 msr & (3 << 0) ? "No-" : "");
4095}
Len Brownf0057312015-12-03 01:35:36 -05004096/*
4097 * Decode MSR_MISC_PWR_MGMT
4098 *
4099 * Decode the bits according to the Nehalem documentation
4100 * bit[0] seems to continue to have same meaning going forward
4101 * bit[1] less so...
4102 */
4103void decode_misc_pwr_mgmt_msr(void)
4104{
4105 unsigned long long msr;
4106
4107 if (!do_nhm_platform_info)
4108 return;
4109
Len Browncf4cbe52017-01-01 13:08:33 -05004110 if (no_MSR_MISC_PWR_MGMT)
4111 return;
4112
Len Brownf0057312015-12-03 01:35:36 -05004113 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004114 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 -05004115 base_cpu, msr,
4116 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004117 msr & (1 << 1) ? "EN" : "DIS",
4118 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004119}
Len Brown71616c82017-01-07 22:37:48 -05004120/*
4121 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4122 *
4123 * This MSRs are present on Silvermont processors,
4124 * Intel Atom processor E3000 series (Baytrail), and friends.
4125 */
4126void decode_c6_demotion_policy_msr(void)
4127{
4128 unsigned long long msr;
4129
4130 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4131 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4132 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4133
4134 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4135 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4136 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4137}
Len Brown7f5c2582015-12-01 01:36:39 -05004138
Len Brownfcd17212015-03-23 20:29:09 -04004139void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004140{
Len Brown61a87ba2015-11-23 02:30:51 -05004141 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004142 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004143 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004144
4145 eax = ebx = ecx = edx = 0;
4146
Len Brown5aea2f72016-03-13 03:14:35 -04004147 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004148
4149 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4150 genuine_intel = 1;
4151
Len Brown96e47152017-01-21 02:26:00 -05004152 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004153 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004154 (char *)&ebx, (char *)&edx, (char *)&ecx);
4155
Len Brown5aea2f72016-03-13 03:14:35 -04004156 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004157 family = (fms >> 8) & 0xf;
4158 model = (fms >> 4) & 0xf;
4159 stepping = fms & 0xf;
4160 if (family == 6 || family == 0xf)
4161 model += ((fms >> 16) & 0xf) << 4;
4162
Len Brown96e47152017-01-21 02:26:00 -05004163 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004164 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004165 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004166 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004167 ecx & (1 << 0) ? "SSE3" : "-",
4168 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004169 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004170 ecx & (1 << 7) ? "EIST" : "-",
4171 ecx & (1 << 8) ? "TM2" : "-",
4172 edx & (1 << 4) ? "TSC" : "-",
4173 edx & (1 << 5) ? "MSR" : "-",
4174 edx & (1 << 22) ? "ACPI-TM" : "-",
4175 edx & (1 << 29) ? "TM" : "-");
4176 }
Len Brown103a8fe2010-10-22 23:53:03 -04004177
Josh Triplettb2c95d92013-08-20 17:20:18 -07004178 if (!(edx & (1 << 5)))
4179 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004180
4181 /*
4182 * check max extended function levels of CPUID.
4183 * This is needed to check for invariant TSC.
4184 * This check is valid for both Intel and AMD.
4185 */
4186 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004187 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004188
Len Brown61a87ba2015-11-23 02:30:51 -05004189 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004190
Len Brownd7899442015-01-23 00:12:33 -05004191 /*
4192 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4193 * this check is valid for both Intel and AMD
4194 */
Len Brown5aea2f72016-03-13 03:14:35 -04004195 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004196 has_invariant_tsc = edx & (1 << 8);
4197 }
Len Brown103a8fe2010-10-22 23:53:03 -04004198
4199 /*
4200 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4201 * this check is valid for both Intel and AMD
4202 */
4203
Len Brown5aea2f72016-03-13 03:14:35 -04004204 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004205 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004206 if (has_aperf) {
4207 BIC_PRESENT(BIC_Avg_MHz);
4208 BIC_PRESENT(BIC_Busy);
4209 BIC_PRESENT(BIC_Bzy_MHz);
4210 }
Len Brown889facb2012-11-08 00:48:57 -05004211 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004212 if (do_dts)
4213 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004214 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004215 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004216 if (do_ptm)
4217 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004218 has_hwp = eax & (1 << 7);
4219 has_hwp_notify = eax & (1 << 8);
4220 has_hwp_activity_window = eax & (1 << 9);
4221 has_hwp_epp = eax & (1 << 10);
4222 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004223 has_epb = ecx & (1 << 3);
4224
Len Brown96e47152017-01-21 02:26:00 -05004225 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004226 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004227 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4228 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004229 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004230 do_dts ? "" : "No-",
4231 do_ptm ? "" : "No-",
4232 has_hwp ? "" : "No-",
4233 has_hwp_notify ? "" : "No-",
4234 has_hwp_activity_window ? "" : "No-",
4235 has_hwp_epp ? "" : "No-",
4236 has_hwp_pkg ? "" : "No-",
4237 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004238
Len Brown96e47152017-01-21 02:26:00 -05004239 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004240 decode_misc_enable_msr();
4241
Len Brown33148d62017-01-21 01:26:16 -05004242
Len Brown96e47152017-01-21 02:26:00 -05004243 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004244 int has_sgx;
4245
4246 ecx = 0;
4247
4248 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4249
4250 has_sgx = ebx & (1 << 2);
4251 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4252
4253 if (has_sgx)
4254 decode_feature_control_msr();
4255 }
4256
Len Brown61a87ba2015-11-23 02:30:51 -05004257 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004258 unsigned int eax_crystal;
4259 unsigned int ebx_tsc;
4260
4261 /*
4262 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4263 */
4264 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004265 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004266
4267 if (ebx_tsc != 0) {
4268
Len Brown96e47152017-01-21 02:26:00 -05004269 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004270 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004271 eax_crystal, ebx_tsc, crystal_hz);
4272
4273 if (crystal_hz == 0)
4274 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004275 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4276 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4277 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4278 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004279 crystal_hz = 24000000; /* 24.0 MHz */
4280 break;
Len Brown7268d402016-12-01 23:10:39 -05004281 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004282 crystal_hz = 25000000; /* 25.0 MHz */
4283 break;
Len Brown869ce692016-06-16 23:22:37 -04004284 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004285 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004286 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004287 break;
4288 default:
4289 crystal_hz = 0;
4290 }
4291
4292 if (crystal_hz) {
4293 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004294 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004295 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004296 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4297 }
4298 }
4299 }
Len Brown61a87ba2015-11-23 02:30:51 -05004300 if (max_level >= 0x16) {
4301 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4302
4303 /*
4304 * CPUID 16H Base MHz, Max MHz, Bus MHz
4305 */
4306 base_mhz = max_mhz = bus_mhz = edx = 0;
4307
Len Brown5aea2f72016-03-13 03:14:35 -04004308 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004309 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004310 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004311 base_mhz, max_mhz, bus_mhz);
4312 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004313
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004314 if (has_aperf)
4315 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4316
Len Brown812db3f2017-02-10 00:25:41 -05004317 BIC_PRESENT(BIC_IRQ);
4318 BIC_PRESENT(BIC_TSC_MHz);
4319
4320 if (probe_nhm_msrs(family, model)) {
4321 do_nhm_platform_info = 1;
4322 BIC_PRESENT(BIC_CPU_c1);
4323 BIC_PRESENT(BIC_CPU_c3);
4324 BIC_PRESENT(BIC_CPU_c6);
4325 BIC_PRESENT(BIC_SMI);
4326 }
Len Brownd7899442015-01-23 00:12:33 -05004327 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004328
4329 if (do_snb_cstates)
4330 BIC_PRESENT(BIC_CPU_c7);
4331
Len Brown5a634262016-04-06 17:15:55 -04004332 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004333 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4334 BIC_PRESENT(BIC_Pkgpc2);
4335 if (pkg_cstate_limit >= PCL__3)
4336 BIC_PRESENT(BIC_Pkgpc3);
4337 if (pkg_cstate_limit >= PCL__6)
4338 BIC_PRESENT(BIC_Pkgpc6);
4339 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4340 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004341 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004342 BIC_NOT_PRESENT(BIC_Pkgpc2);
4343 BIC_NOT_PRESENT(BIC_Pkgpc3);
4344 BIC_PRESENT(BIC_Pkgpc6);
4345 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004346 BIC_PRESENT(BIC_Mod_c6);
4347 use_c1_residency_msr = 1;
4348 }
Len Brown7170a372017-01-27 02:13:27 -05004349 if (is_dnv(family, model)) {
4350 BIC_PRESENT(BIC_CPU_c1);
4351 BIC_NOT_PRESENT(BIC_CPU_c3);
4352 BIC_NOT_PRESENT(BIC_Pkgpc3);
4353 BIC_NOT_PRESENT(BIC_CPU_c7);
4354 BIC_NOT_PRESENT(BIC_Pkgpc7);
4355 use_c1_residency_msr = 1;
4356 }
Len Brown34c761972017-01-27 02:36:41 -05004357 if (is_skx(family, model)) {
4358 BIC_NOT_PRESENT(BIC_CPU_c3);
4359 BIC_NOT_PRESENT(BIC_Pkgpc3);
4360 BIC_NOT_PRESENT(BIC_CPU_c7);
4361 BIC_NOT_PRESENT(BIC_Pkgpc7);
4362 }
Len Brownade0eba2017-02-10 01:56:47 -05004363 if (is_bdx(family, model)) {
4364 BIC_NOT_PRESENT(BIC_CPU_c7);
4365 BIC_NOT_PRESENT(BIC_Pkgpc7);
4366 }
Len Brown0f47c082017-01-27 00:50:45 -05004367 if (has_hsw_msrs(family, model)) {
4368 BIC_PRESENT(BIC_Pkgpc8);
4369 BIC_PRESENT(BIC_Pkgpc9);
4370 BIC_PRESENT(BIC_Pkgpc10);
4371 }
Len Brown5a634262016-04-06 17:15:55 -04004372 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004373 if (has_skl_msrs(family, model)) {
4374 BIC_PRESENT(BIC_Totl_c0);
4375 BIC_PRESENT(BIC_Any_c0);
4376 BIC_PRESENT(BIC_GFX_c0);
4377 BIC_PRESENT(BIC_CPUGFX);
4378 }
Len Brown144b44b2013-11-09 00:30:16 -05004379 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004380 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004381
Len Brown96e47152017-01-21 02:26:00 -05004382 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004383 decode_misc_pwr_mgmt_msr();
4384
Len Brown96e47152017-01-21 02:26:00 -05004385 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004386 decode_c6_demotion_policy_msr();
4387
Len Brown889facb2012-11-08 00:48:57 -05004388 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004389 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004390 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004391
Len Brown96e47152017-01-21 02:26:00 -05004392 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004393 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004394
Len Brown41618e62017-02-09 18:25:22 -05004395 if (!quiet)
4396 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004397 if (!quiet)
4398 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004399
Len Browna2b7b742015-09-26 00:12:38 -04004400 if (has_skl_msrs(family, model))
4401 calculate_tsc_tweak();
4402
Len Brown812db3f2017-02-10 00:25:41 -05004403 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4404 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004405
Len Brown812db3f2017-02-10 00:25:41 -05004406 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4407 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004408
Len Brown96e47152017-01-21 02:26:00 -05004409 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004410 decode_misc_feature_control();
4411
Len Brown889facb2012-11-08 00:48:57 -05004412 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004413}
4414
Len Brown103a8fe2010-10-22 23:53:03 -04004415
4416/*
4417 * in /dev/cpu/ return success for names that are numbers
4418 * ie. filter out ".", "..", "microcode".
4419 */
4420int dir_filter(const struct dirent *dirp)
4421{
4422 if (isdigit(dirp->d_name[0]))
4423 return 1;
4424 else
4425 return 0;
4426}
4427
4428int open_dev_cpu_msr(int dummy1)
4429{
4430 return 0;
4431}
4432
Len Brownc98d5d92012-06-04 00:56:40 -04004433void topology_probe()
4434{
4435 int i;
4436 int max_core_id = 0;
4437 int max_package_id = 0;
4438 int max_siblings = 0;
4439 struct cpu_topology {
4440 int core_id;
4441 int physical_package_id;
4442 } *cpus;
4443
4444 /* Initialize num_cpus, max_cpu_num */
4445 topo.num_cpus = 0;
4446 topo.max_cpu_num = 0;
4447 for_all_proc_cpus(count_cpus);
4448 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004449 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004450
Len Brownd8af6f52015-02-10 01:56:38 -05004451 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004452 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004453
4454 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004455 if (cpus == NULL)
4456 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004457
4458 /*
4459 * Allocate and initialize cpu_present_set
4460 */
4461 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004462 if (cpu_present_set == NULL)
4463 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004464 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4465 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4466 for_all_proc_cpus(mark_cpu_present);
4467
4468 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004469 * Validate that all cpus in cpu_subset are also in cpu_present_set
4470 */
4471 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4472 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4473 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4474 err(1, "cpu%d not present", i);
4475 }
4476
4477 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004478 * Allocate and initialize cpu_affinity_set
4479 */
4480 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004481 if (cpu_affinity_set == NULL)
4482 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004483 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4484 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4485
4486
4487 /*
4488 * For online cpus
4489 * find max_core_id, max_package_id
4490 */
4491 for (i = 0; i <= topo.max_cpu_num; ++i) {
4492 int siblings;
4493
4494 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004495 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004496 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004497 continue;
4498 }
4499 cpus[i].core_id = get_core_id(i);
4500 if (cpus[i].core_id > max_core_id)
4501 max_core_id = cpus[i].core_id;
4502
4503 cpus[i].physical_package_id = get_physical_package_id(i);
4504 if (cpus[i].physical_package_id > max_package_id)
4505 max_package_id = cpus[i].physical_package_id;
4506
4507 siblings = get_num_ht_siblings(i);
4508 if (siblings > max_siblings)
4509 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004510 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004511 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004512 i, cpus[i].physical_package_id, cpus[i].core_id);
4513 }
4514 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004515 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004516 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004517 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004518 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004519 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004520
4521 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004522 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004523 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004524 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004525 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004526 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004527
4528 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004529 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004530 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004531
4532 free(cpus);
4533}
4534
4535void
4536allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4537{
4538 int i;
4539
4540 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004541 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004542 if (*t == NULL)
4543 goto error;
4544
4545 for (i = 0; i < topo.num_threads_per_core *
4546 topo.num_cores_per_pkg * topo.num_packages; i++)
4547 (*t)[i].cpu_id = -1;
4548
4549 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004550 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004551 if (*c == NULL)
4552 goto error;
4553
4554 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4555 (*c)[i].core_id = -1;
4556
Len Brown678a3bd2017-02-09 22:22:13 -05004557 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004558 if (*p == NULL)
4559 goto error;
4560
4561 for (i = 0; i < topo.num_packages; i++)
4562 (*p)[i].package_id = i;
4563
4564 return;
4565error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004566 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004567}
4568/*
4569 * init_counter()
4570 *
4571 * set cpu_id, core_num, pkg_num
4572 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4573 *
4574 * increment topo.num_cores when 1st core in pkg seen
4575 */
4576void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4577 struct pkg_data *pkg_base, int thread_num, int core_num,
4578 int pkg_num, int cpu_id)
4579{
4580 struct thread_data *t;
4581 struct core_data *c;
4582 struct pkg_data *p;
4583
4584 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4585 c = GET_CORE(core_base, core_num, pkg_num);
4586 p = GET_PKG(pkg_base, pkg_num);
4587
4588 t->cpu_id = cpu_id;
4589 if (thread_num == 0) {
4590 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4591 if (cpu_is_first_core_in_package(cpu_id))
4592 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4593 }
4594
4595 c->core_id = core_num;
4596 p->package_id = pkg_num;
4597}
4598
4599
4600int initialize_counters(int cpu_id)
4601{
4602 int my_thread_id, my_core_id, my_package_id;
4603
4604 my_package_id = get_physical_package_id(cpu_id);
4605 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004606 my_thread_id = get_cpu_position_in_core(cpu_id);
4607 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004608 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004609
4610 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4611 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4612 return 0;
4613}
4614
4615void allocate_output_buffer()
4616{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004617 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004618 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004619 if (outp == NULL)
4620 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004621}
Len Brown36229892016-02-26 20:51:02 -05004622void allocate_fd_percpu(void)
4623{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004624 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004625 if (fd_percpu == NULL)
4626 err(-1, "calloc fd_percpu");
4627}
Len Brown562a2d32016-02-26 23:48:05 -05004628void allocate_irq_buffers(void)
4629{
4630 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4631 if (irq_column_2_cpu == NULL)
4632 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004633
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004634 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004635 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004636 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004637}
Len Brownc98d5d92012-06-04 00:56:40 -04004638void setup_all_buffers(void)
4639{
4640 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004641 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004642 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004643 allocate_counters(&thread_even, &core_even, &package_even);
4644 allocate_counters(&thread_odd, &core_odd, &package_odd);
4645 allocate_output_buffer();
4646 for_all_proc_cpus(initialize_counters);
4647}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004648
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004649void set_base_cpu(void)
4650{
4651 base_cpu = sched_getcpu();
4652 if (base_cpu < 0)
4653 err(-ENODEV, "No valid cpus found");
4654
4655 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004656 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004657}
4658
Len Brown103a8fe2010-10-22 23:53:03 -04004659void turbostat_init()
4660{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004661 setup_all_buffers();
4662 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004663 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004664 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004665 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004666
Len Brown103a8fe2010-10-22 23:53:03 -04004667
Len Brown96e47152017-01-21 02:26:00 -05004668 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004669 for_all_cpus(print_hwp, ODD_COUNTERS);
4670
Len Brown96e47152017-01-21 02:26:00 -05004671 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004672 for_all_cpus(print_epb, ODD_COUNTERS);
4673
Len Brown96e47152017-01-21 02:26:00 -05004674 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004675 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4676
Len Brown96e47152017-01-21 02:26:00 -05004677 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004678 for_all_cpus(print_rapl, ODD_COUNTERS);
4679
4680 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4681
Len Brown96e47152017-01-21 02:26:00 -05004682 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004683 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004684
Len Brown96e47152017-01-21 02:26:00 -05004685 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004686 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004687}
4688
4689int fork_it(char **argv)
4690{
Len Brown103a8fe2010-10-22 23:53:03 -04004691 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004692 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004693
Len Brown218f0e82017-02-14 22:07:52 -05004694 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004695 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4696 if (status)
4697 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004698 /* clear affinity side-effect of get_counters() */
4699 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004700 gettimeofday(&tv_even, (struct timezone *)NULL);
4701
4702 child_pid = fork();
4703 if (!child_pid) {
4704 /* child */
4705 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004706 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004707 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004708
4709 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004710 if (child_pid == -1)
4711 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004712
4713 signal(SIGINT, SIG_IGN);
4714 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004715 if (waitpid(child_pid, &status, 0) == -1)
4716 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004717 }
Len Brownc98d5d92012-06-04 00:56:40 -04004718 /*
4719 * n.b. fork_it() does not check for errors from for_all_cpus()
4720 * because re-starting is problematic when forking
4721 */
Len Brown218f0e82017-02-14 22:07:52 -05004722 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004723 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004724 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004725 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004726 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4727 fprintf(outf, "%s: Counter reset detected\n", progname);
4728 else {
4729 compute_average(EVEN_COUNTERS);
4730 format_all_counters(EVEN_COUNTERS);
4731 }
Len Brown103a8fe2010-10-22 23:53:03 -04004732
Len Brownb7d8c142016-02-13 23:36:17 -05004733 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4734
4735 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004736
Len Brownd91bb172012-11-01 00:08:19 -04004737 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004738}
4739
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004740int get_and_dump_counters(void)
4741{
4742 int status;
4743
Len Brown218f0e82017-02-14 22:07:52 -05004744 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004745 status = for_all_cpus(get_counters, ODD_COUNTERS);
4746 if (status)
4747 return status;
4748
4749 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4750 if (status)
4751 return status;
4752
Len Brownb7d8c142016-02-13 23:36:17 -05004753 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004754
4755 return status;
4756}
4757
Len Brownd8af6f52015-02-10 01:56:38 -05004758void print_version() {
Len Brownf7d44a82017-05-27 21:24:58 -07004759 fprintf(outf, "turbostat version 17.06.23"
Len Brownd8af6f52015-02-10 01:56:38 -05004760 " - Len Brown <lenb@kernel.org>\n");
4761}
4762
Len Brown495c76542017-02-08 02:41:51 -05004763int add_counter(unsigned int msr_num, char *path, char *name,
4764 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004765 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004766{
4767 struct msr_counter *msrp;
4768
4769 msrp = calloc(1, sizeof(struct msr_counter));
4770 if (msrp == NULL) {
4771 perror("calloc");
4772 exit(1);
4773 }
4774
4775 msrp->msr_num = msr_num;
4776 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004777 if (path)
4778 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004779 msrp->width = width;
4780 msrp->type = type;
4781 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004782 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004783
4784 switch (scope) {
4785
4786 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004787 msrp->next = sys.tp;
4788 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004789 sys.added_thread_counters++;
4790 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4791 fprintf(stderr, "exceeded max %d added thread counters\n",
4792 MAX_ADDED_COUNTERS);
4793 exit(-1);
4794 }
Len Brown388e9c82016-12-22 23:57:55 -05004795 break;
4796
4797 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004798 msrp->next = sys.cp;
4799 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004800 sys.added_core_counters++;
4801 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4802 fprintf(stderr, "exceeded max %d added core counters\n",
4803 MAX_ADDED_COUNTERS);
4804 exit(-1);
4805 }
Len Brown388e9c82016-12-22 23:57:55 -05004806 break;
4807
4808 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004809 msrp->next = sys.pp;
4810 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004811 sys.added_package_counters++;
4812 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4813 fprintf(stderr, "exceeded max %d added package counters\n",
4814 MAX_ADDED_COUNTERS);
4815 exit(-1);
4816 }
Len Brown388e9c82016-12-22 23:57:55 -05004817 break;
4818 }
4819
4820 return 0;
4821}
4822
4823void parse_add_command(char *add_command)
4824{
4825 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004826 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004827 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004828 int width = 64;
4829 int fail = 0;
4830 enum counter_scope scope = SCOPE_CPU;
4831 enum counter_type type = COUNTER_CYCLES;
4832 enum counter_format format = FORMAT_DELTA;
4833
4834 while (add_command) {
4835
4836 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4837 goto next;
4838
4839 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4840 goto next;
4841
Len Brown495c76542017-02-08 02:41:51 -05004842 if (*add_command == '/') {
4843 path = add_command;
4844 goto next;
4845 }
4846
Len Brown388e9c82016-12-22 23:57:55 -05004847 if (sscanf(add_command, "u%d", &width) == 1) {
4848 if ((width == 32) || (width == 64))
4849 goto next;
4850 width = 64;
4851 }
4852 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4853 scope = SCOPE_CPU;
4854 goto next;
4855 }
4856 if (!strncmp(add_command, "core", strlen("core"))) {
4857 scope = SCOPE_CORE;
4858 goto next;
4859 }
4860 if (!strncmp(add_command, "package", strlen("package"))) {
4861 scope = SCOPE_PACKAGE;
4862 goto next;
4863 }
4864 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4865 type = COUNTER_CYCLES;
4866 goto next;
4867 }
4868 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4869 type = COUNTER_SECONDS;
4870 goto next;
4871 }
Len Brown41618e62017-02-09 18:25:22 -05004872 if (!strncmp(add_command, "usec", strlen("usec"))) {
4873 type = COUNTER_USEC;
4874 goto next;
4875 }
Len Brown388e9c82016-12-22 23:57:55 -05004876 if (!strncmp(add_command, "raw", strlen("raw"))) {
4877 format = FORMAT_RAW;
4878 goto next;
4879 }
4880 if (!strncmp(add_command, "delta", strlen("delta"))) {
4881 format = FORMAT_DELTA;
4882 goto next;
4883 }
4884 if (!strncmp(add_command, "percent", strlen("percent"))) {
4885 format = FORMAT_PERCENT;
4886 goto next;
4887 }
4888
4889 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4890 char *eos;
4891
4892 eos = strchr(name_buffer, ',');
4893 if (eos)
4894 *eos = '\0';
4895 goto next;
4896 }
4897
4898next:
4899 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004900 if (add_command) {
4901 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004902 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004903 }
Len Brown388e9c82016-12-22 23:57:55 -05004904
4905 }
Len Brown495c76542017-02-08 02:41:51 -05004906 if ((msr_num == 0) && (path == NULL)) {
4907 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004908 fail++;
4909 }
4910
4911 /* generate default column header */
4912 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05004913 if (width == 32)
4914 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4915 else
4916 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05004917 }
4918
Len Brown41618e62017-02-09 18:25:22 -05004919 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05004920 fail++;
4921
4922 if (fail) {
4923 help();
4924 exit(1);
4925 }
4926}
Len Brown41618e62017-02-09 18:25:22 -05004927
Len Browndd778a52017-02-21 23:21:13 -05004928int is_deferred_skip(char *name)
4929{
4930 int i;
4931
4932 for (i = 0; i < deferred_skip_index; ++i)
4933 if (!strcmp(name, deferred_skip_names[i]))
4934 return 1;
4935 return 0;
4936}
4937
Len Brown41618e62017-02-09 18:25:22 -05004938void probe_sysfs(void)
4939{
4940 char path[64];
4941 char name_buf[16];
4942 FILE *input;
4943 int state;
4944 char *sp;
4945
4946 if (!DO_BIC(BIC_sysfs))
4947 return;
4948
4949 for (state = 10; state > 0; --state) {
4950
4951 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4952 base_cpu, state);
4953 input = fopen(path, "r");
4954 if (input == NULL)
4955 continue;
4956 fgets(name_buf, sizeof(name_buf), input);
4957
4958 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4959 sp = strchr(name_buf, '-');
4960 if (!sp)
4961 sp = strchrnul(name_buf, '\n');
4962 *sp = '%';
4963 *(sp + 1) = '\0';
4964
4965 fclose(input);
4966
4967 sprintf(path, "cpuidle/state%d/time", state);
4968
Len Browndd778a52017-02-21 23:21:13 -05004969 if (is_deferred_skip(name_buf))
4970 continue;
4971
Len Brown41618e62017-02-09 18:25:22 -05004972 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4973 FORMAT_PERCENT, SYSFS_PERCPU);
4974 }
4975
4976 for (state = 10; state > 0; --state) {
4977
4978 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4979 base_cpu, state);
4980 input = fopen(path, "r");
4981 if (input == NULL)
4982 continue;
4983 fgets(name_buf, sizeof(name_buf), input);
4984 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4985 sp = strchr(name_buf, '-');
4986 if (!sp)
4987 sp = strchrnul(name_buf, '\n');
4988 *sp = '\0';
4989 fclose(input);
4990
4991 sprintf(path, "cpuidle/state%d/usage", state);
4992
Len Browndd778a52017-02-21 23:21:13 -05004993 if (is_deferred_skip(name_buf))
4994 continue;
4995
Len Brown41618e62017-02-09 18:25:22 -05004996 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4997 FORMAT_DELTA, SYSFS_PERCPU);
4998 }
4999
5000}
5001
Len Brown1ef7d212017-02-10 23:54:15 -05005002
5003/*
5004 * parse cpuset with following syntax
5005 * 1,2,4..6,8-10 and set bits in cpu_subset
5006 */
5007void parse_cpu_command(char *optarg)
5008{
5009 unsigned int start, end;
5010 char *next;
5011
Len Brown4e4e1e72017-02-21 22:33:42 -05005012 if (!strcmp(optarg, "core")) {
5013 if (cpu_subset)
5014 goto error;
5015 show_core_only++;
5016 return;
5017 }
5018 if (!strcmp(optarg, "package")) {
5019 if (cpu_subset)
5020 goto error;
5021 show_pkg_only++;
5022 return;
5023 }
5024 if (show_core_only || show_pkg_only)
5025 goto error;
5026
Len Brown1ef7d212017-02-10 23:54:15 -05005027 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5028 if (cpu_subset == NULL)
5029 err(3, "CPU_ALLOC");
5030 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5031
5032 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5033
5034 next = optarg;
5035
5036 while (next && *next) {
5037
5038 if (*next == '-') /* no negative cpu numbers */
5039 goto error;
5040
5041 start = strtoul(next, &next, 10);
5042
5043 if (start >= CPU_SUBSET_MAXCPUS)
5044 goto error;
5045 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5046
5047 if (*next == '\0')
5048 break;
5049
5050 if (*next == ',') {
5051 next += 1;
5052 continue;
5053 }
5054
5055 if (*next == '-') {
5056 next += 1; /* start range */
5057 } else if (*next == '.') {
5058 next += 1;
5059 if (*next == '.')
5060 next += 1; /* start range */
5061 else
5062 goto error;
5063 }
5064
5065 end = strtoul(next, &next, 10);
5066 if (end <= start)
5067 goto error;
5068
5069 while (++start <= end) {
5070 if (start >= CPU_SUBSET_MAXCPUS)
5071 goto error;
5072 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5073 }
5074
5075 if (*next == ',')
5076 next += 1;
5077 else if (*next != '\0')
5078 goto error;
5079 }
5080
5081 return;
5082
5083error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005084 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5085 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005086 exit(-1);
5087}
5088
Len Brown812db3f2017-02-10 00:25:41 -05005089
Len Brown103a8fe2010-10-22 23:53:03 -04005090void cmdline(int argc, char **argv)
5091{
5092 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005093 int option_index = 0;
5094 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005095 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005096 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005097 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005098 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005099 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005100 {"interval", required_argument, 0, 'i'},
5101 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005102 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005103 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005104 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005105 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005106 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005107 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005108 {"Summary", no_argument, 0, 'S'},
5109 {"TCC", required_argument, 0, 'T'},
5110 {"version", no_argument, 0, 'v' },
5111 {0, 0, 0, 0 }
5112 };
Len Brown103a8fe2010-10-22 23:53:03 -04005113
5114 progname = argv[0];
5115
Len Brown3f44a5c2017-10-17 15:42:56 -04005116 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jo:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005117 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005118 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005119 case 'a':
5120 parse_add_command(optarg);
5121 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005122 case 'c':
5123 parse_cpu_command(optarg);
5124 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005125 case 'D':
5126 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005127 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005128 case 'e':
5129 /* --enable specified counter */
5130 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5131 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005132 case 'd':
5133 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005134 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005135 break;
Len Brown812db3f2017-02-10 00:25:41 -05005136 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005137 /*
5138 * --hide: do not show those specified
5139 * multiple invocations simply clear more bits in enabled mask
5140 */
5141 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005142 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005143 case 'h':
5144 default:
5145 help();
5146 exit(1);
5147 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005148 {
5149 double interval = strtod(optarg, NULL);
5150
5151 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005152 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005153 interval);
5154 exit(2);
5155 }
5156
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005157 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005158 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005159 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005160 }
Len Brown889facb2012-11-08 00:48:57 -05005161 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005162 case 'J':
5163 rapl_joules++;
5164 break;
Len Brownc8ade362017-02-15 17:15:11 -05005165 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005166 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005167 list_header_only++;
5168 quiet++;
5169 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005170 case 'o':
5171 outf = fopen_or_die(optarg, "w");
5172 break;
Len Brown96e47152017-01-21 02:26:00 -05005173 case 'q':
5174 quiet = 1;
5175 break;
Len Brown812db3f2017-02-10 00:25:41 -05005176 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005177 /*
5178 * --show: show only those specified
5179 * The 1st invocation will clear and replace the enabled mask
5180 * subsequent invocations can add to it.
5181 */
5182 if (shown == 0)
5183 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5184 else
5185 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5186 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005187 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005188 case 'S':
5189 summary_only++;
5190 break;
5191 case 'T':
5192 tcc_activation_temp_override = atoi(optarg);
5193 break;
5194 case 'v':
5195 print_version();
5196 exit(0);
5197 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005198 }
5199 }
5200}
5201
5202int main(int argc, char **argv)
5203{
Len Brownb7d8c142016-02-13 23:36:17 -05005204 outf = stderr;
5205
Len Brown103a8fe2010-10-22 23:53:03 -04005206 cmdline(argc, argv);
5207
Len Brown96e47152017-01-21 02:26:00 -05005208 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005209 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005210
Len Brown41618e62017-02-09 18:25:22 -05005211 probe_sysfs();
5212
Len Brown103a8fe2010-10-22 23:53:03 -04005213 turbostat_init();
5214
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005215 /* dump counters and exit */
5216 if (dump_only)
5217 return get_and_dump_counters();
5218
Len Brownc8ade362017-02-15 17:15:11 -05005219 /* list header and exit */
5220 if (list_header_only) {
5221 print_header(",");
5222 flush_output_stdout();
5223 return 0;
5224 }
5225
Len Brown103a8fe2010-10-22 23:53:03 -04005226 /*
5227 * if any params left, it must be a command to fork
5228 */
5229 if (argc - optind)
5230 return fork_it(argv + optind);
5231 else
5232 turbostat_loop();
5233
5234 return 0;
5235}