blob: 1b53a2489ebb672e9a6f8896686347e1a090ea69 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce69e2016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
Len Brownb9ad8ee2017-07-19 19:28:37 -040032#include <sys/select.h>
Len Brown103a8fe2010-10-22 23:53:03 -040033#include <sys/resource.h>
34#include <fcntl.h>
35#include <signal.h>
36#include <sys/time.h>
37#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050038#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040039#include <dirent.h>
40#include <string.h>
41#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040042#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050043#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070044#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040045#include <linux/capability.h>
46#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040047
Len Brown103a8fe2010-10-22 23:53:03 -040048char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050049FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050050int *fd_percpu;
Len Brownb9ad8ee2017-07-19 19:28:37 -040051struct timeval interval_tv = {5, 0};
Artem Bityutskiy47936f92017-10-04 15:01:47 +030052struct timespec interval_ts = {5, 0};
Len Brownb9ad8ee2017-07-19 19:28:37 -040053struct timespec one_msec = {0, 1000000};
Chen Yu023fe0a2018-04-26 08:41:03 +080054unsigned int num_iterations;
Len Brownd8af6f52015-02-10 01:56:38 -050055unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050056unsigned int quiet;
Len Brown3f44a5c2017-10-17 15:42:56 -040057unsigned int shown;
Len Brown0de6c0d2017-02-15 21:45:40 -050058unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050059unsigned int rapl_joules;
60unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050061unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050062unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040063unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070064unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050065unsigned int do_slm_cstates;
Srinivas Pandruvada997e5392018-05-31 10:39:07 -070066unsigned int do_cnl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050067unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040068unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050069unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040070unsigned int do_irtl_snb;
71unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050072unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040073unsigned int genuine_intel;
74unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050075unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050076unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020077unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040078double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040079double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040080unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040081double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040082unsigned int show_pkg_only;
83unsigned int show_core_only;
84char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050085unsigned int do_rapl;
86unsigned int do_dts;
87unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050088unsigned long long gfx_cur_rc6_ms;
Len Brownbe0e54c2018-06-01 12:35:53 -040089unsigned long long cpuidle_cur_cpu_lpi_us;
90unsigned long long cpuidle_cur_sys_lpi_us;
Len Brown27d47352016-02-27 00:37:54 -050091unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050092unsigned int tcc_activation_temp;
93unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050094double rapl_power_units, rapl_time_units;
95double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050096double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040097unsigned int do_core_perf_limit_reasons;
Artem Bityutskiyac980e12017-09-05 15:14:08 +030098unsigned int has_automatic_cstate_conversion;
Len Brown3a9a9412014-08-15 02:39:52 -040099unsigned int do_gfx_perf_limit_reasons;
100unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -0400101unsigned int crystal_hz;
102unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400103int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400104double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500105unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
106 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
107unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
108unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
109unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
110unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500111unsigned int has_misc_feature_control;
Len Brown4c2122d2018-06-06 17:44:48 -0400112unsigned int first_counter_read = 1;
Len Brown889facb2012-11-08 00:48:57 -0500113
Len Browne6f9bb32013-12-03 02:19:19 -0500114#define RAPL_PKG (1 << 0)
115 /* 0x610 MSR_PKG_POWER_LIMIT */
116 /* 0x611 MSR_PKG_ENERGY_STATUS */
117#define RAPL_PKG_PERF_STATUS (1 << 1)
118 /* 0x613 MSR_PKG_PERF_STATUS */
119#define RAPL_PKG_POWER_INFO (1 << 2)
120 /* 0x614 MSR_PKG_POWER_INFO */
121
122#define RAPL_DRAM (1 << 3)
123 /* 0x618 MSR_DRAM_POWER_LIMIT */
124 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500125#define RAPL_DRAM_PERF_STATUS (1 << 4)
126 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400127#define RAPL_DRAM_POWER_INFO (1 << 5)
128 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500129
Jacob Pan91484942016-06-16 09:48:20 -0700130#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500131 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400132#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500133 /* 0x63a MSR_PP0_POLICY */
134
Len Brown0b2bb692015-03-26 00:50:30 -0400135#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500136 /* 0x640 MSR_PP1_POWER_LIMIT */
137 /* 0x641 MSR_PP1_ENERGY_STATUS */
138 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700139
140#define RAPL_CORES_ENERGY_STATUS (1 << 9)
141 /* 0x639 MSR_PP0_ENERGY_STATUS */
142#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500143#define TJMAX_DEFAULT 100
144
145#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400146
Len Brown388e9c82016-12-22 23:57:55 -0500147/*
148 * buffer size used by sscanf() for added column names
149 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
150 */
151#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500152#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500153
Len Brown103a8fe2010-10-22 23:53:03 -0400154int backwards_count;
155char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400156
Len Brown1ef7d212017-02-10 23:54:15 -0500157#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
158cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
159size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown0748eaf2018-06-01 12:38:29 -0400160#define MAX_ADDED_COUNTERS 8
161#define MAX_ADDED_THREAD_COUNTERS 24
Len Brown0e2d8f02018-06-01 22:08:58 -0400162#define BITMASK_SIZE 32
Len Brown103a8fe2010-10-22 23:53:03 -0400163
Len Brownc98d5d92012-06-04 00:56:40 -0400164struct thread_data {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700165 struct timeval tv_begin;
166 struct timeval tv_end;
Len Brownc98d5d92012-06-04 00:56:40 -0400167 unsigned long long tsc;
168 unsigned long long aperf;
169 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500170 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500171 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500172 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400173 unsigned int cpu_id;
Len Brown4c2122d2018-06-06 17:44:48 -0400174 unsigned int apic_id;
175 unsigned int x2apic_id;
Len Brownc98d5d92012-06-04 00:56:40 -0400176 unsigned int flags;
177#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
178#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown0748eaf2018-06-01 12:38:29 -0400179 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400180} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400181
Len Brownc98d5d92012-06-04 00:56:40 -0400182struct core_data {
183 unsigned long long c3;
184 unsigned long long c6;
185 unsigned long long c7;
Len Brown0539ba112017-02-10 00:27:20 -0500186 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500187 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400188 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500189 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400190} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400191
Len Brownc98d5d92012-06-04 00:56:40 -0400192struct pkg_data {
193 unsigned long long pc2;
194 unsigned long long pc3;
195 unsigned long long pc6;
196 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800197 unsigned long long pc8;
198 unsigned long long pc9;
199 unsigned long long pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -0400200 unsigned long long cpu_lpi;
201 unsigned long long sys_lpi;
Len Brown0b2bb692015-03-26 00:50:30 -0400202 unsigned long long pkg_wtd_core_c0;
203 unsigned long long pkg_any_core_c0;
204 unsigned long long pkg_any_gfxe_c0;
205 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400206 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500207 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400208 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500209 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
210 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
211 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
212 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
213 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
214 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
215 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500216 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400217} *package_even, *package_odd;
218
219#define ODD_COUNTERS thread_odd, core_odd, package_odd
220#define EVEN_COUNTERS thread_even, core_even, package_even
221
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400222#define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \
223 ((thread_base) + \
224 ((pkg_no) * \
225 topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
226 ((node_no) * topo.cores_per_node * topo.threads_per_core) + \
227 ((core_no) * topo.threads_per_core) + \
228 (thread_no))
229
230#define GET_CORE(core_base, core_no, node_no, pkg_no) \
231 ((core_base) + \
232 ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \
233 ((node_no) * topo.cores_per_node) + \
234 (core_no))
235
236
Len Brownc98d5d92012-06-04 00:56:40 -0400237#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
238
Len Brown388e9c82016-12-22 23:57:55 -0500239enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500240enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500241enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
242
243struct msr_counter {
244 unsigned int msr_num;
245 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500246 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500247 unsigned int width;
248 enum counter_type type;
249 enum counter_format format;
250 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500251 unsigned int flags;
252#define FLAGS_HIDE (1 << 0)
253#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500254#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500255};
256
257struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500258 unsigned int added_thread_counters;
259 unsigned int added_core_counters;
260 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500261 struct msr_counter *tp;
262 struct msr_counter *cp;
263 struct msr_counter *pp;
264} sys;
265
Len Brownc98d5d92012-06-04 00:56:40 -0400266struct system_summary {
267 struct thread_data threads;
268 struct core_data cores;
269 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500270} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400271
Len Brown0e2d8f02018-06-01 22:08:58 -0400272struct cpu_topology {
273 int physical_package_id;
274 int logical_cpu_id;
Prarit Bhargavaef605742018-06-01 10:04:30 -0400275 int physical_node_id;
276 int logical_node_id; /* 0-based count within the package */
Len Brown0e2d8f02018-06-01 22:08:58 -0400277 int physical_core_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -0400278 int thread_id;
Len Brown0e2d8f02018-06-01 22:08:58 -0400279 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
280} *cpus;
Len Brownc98d5d92012-06-04 00:56:40 -0400281
282struct topo_params {
283 int num_packages;
284 int num_cpus;
285 int num_cores;
286 int max_cpu_num;
Prarit Bhargavaef605742018-06-01 10:04:30 -0400287 int max_node_num;
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400288 int nodes_per_pkg;
289 int cores_per_node;
290 int threads_per_core;
Len Brownc98d5d92012-06-04 00:56:40 -0400291} topo;
292
293struct timeval tv_even, tv_odd, tv_delta;
294
Len Brown562a2d32016-02-26 23:48:05 -0500295int *irq_column_2_cpu; /* /proc/interrupts column numbers */
296int *irqs_per_cpu; /* indexed by cpu_num */
297
Len Brownc98d5d92012-06-04 00:56:40 -0400298void setup_all_buffers(void);
299
300int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400301{
Len Brownc98d5d92012-06-04 00:56:40 -0400302 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400303}
Len Brown88c32812012-03-29 21:44:40 -0400304/*
Len Brownc98d5d92012-06-04 00:56:40 -0400305 * run func(thread, core, package) in topology order
306 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400307 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400308
Len Brownc98d5d92012-06-04 00:56:40 -0400309int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
310 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400311{
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400312 int retval, pkg_no, core_no, thread_no, node_no;
Len Brownc98d5d92012-06-04 00:56:40 -0400313
314 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400315 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400316 for (node_no = 0; node_no < topo.nodes_per_pkg;
317 node_no++) {
318 for (thread_no = 0; thread_no <
319 topo.threads_per_core; ++thread_no) {
320 struct thread_data *t;
321 struct core_data *c;
322 struct pkg_data *p;
Len Brownc98d5d92012-06-04 00:56:40 -0400323
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400324 t = GET_THREAD(thread_base, thread_no,
325 core_no, node_no,
326 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -0400327
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400328 if (cpu_is_not_present(t->cpu_id))
329 continue;
Len Brownc98d5d92012-06-04 00:56:40 -0400330
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400331 c = GET_CORE(core_base, core_no,
332 node_no, pkg_no);
333 p = GET_PKG(pkg_base, pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -0400334
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400335 retval = func(t, c, p);
336 if (retval)
337 return retval;
338 }
Len Brownc98d5d92012-06-04 00:56:40 -0400339 }
340 }
341 }
342 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400343}
344
345int cpu_migrate(int cpu)
346{
Len Brownc98d5d92012-06-04 00:56:40 -0400347 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
348 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
349 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400350 return -1;
351 else
352 return 0;
353}
Len Brown36229892016-02-26 20:51:02 -0500354int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400355{
Len Brown103a8fe2010-10-22 23:53:03 -0400356 char pathname[32];
357 int fd;
358
Len Brown36229892016-02-26 20:51:02 -0500359 fd = fd_percpu[cpu];
360
361 if (fd)
362 return fd;
363
Len Brown103a8fe2010-10-22 23:53:03 -0400364 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
365 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400366 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400367 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 -0400368
Len Brown36229892016-02-26 20:51:02 -0500369 fd_percpu[cpu] = fd;
370
371 return fd;
372}
373
374int get_msr(int cpu, off_t offset, unsigned long long *msr)
375{
376 ssize_t retval;
377
378 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400379
Len Brown98481e72014-08-15 00:36:50 -0400380 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500381 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400382
383 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400384}
385
Len Brownfc04cc62014-02-06 00:55:19 -0500386/*
Len Brownbdd5ae32018-06-06 17:18:36 -0400387 * This list matches the column headers, except
388 * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
389 * 2. Core and CPU are moved to the end, we can't have strings that contain them
390 * matching on them for --show and --hide.
Len Brownfc04cc62014-02-06 00:55:19 -0500391 */
Len Brown812db3f2017-02-10 00:25:41 -0500392struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400393 { 0x0, "usec" },
394 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500395 { 0x0, "Package" },
Len Brownbdd5ae32018-06-06 17:18:36 -0400396 { 0x0, "Node" },
Len Brown812db3f2017-02-10 00:25:41 -0500397 { 0x0, "Avg_MHz" },
Len Brownbdd5ae32018-06-06 17:18:36 -0400398 { 0x0, "Busy%" },
Len Brown812db3f2017-02-10 00:25:41 -0500399 { 0x0, "Bzy_MHz" },
400 { 0x0, "TSC_MHz" },
401 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500402 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brownbdd5ae32018-06-06 17:18:36 -0400403 { 0x0, "sysfs" },
Len Brown812db3f2017-02-10 00:25:41 -0500404 { 0x0, "CPU%c1" },
405 { 0x0, "CPU%c3" },
406 { 0x0, "CPU%c6" },
407 { 0x0, "CPU%c7" },
408 { 0x0, "ThreadC" },
409 { 0x0, "CoreTmp" },
410 { 0x0, "CoreCnt" },
411 { 0x0, "PkgTmp" },
412 { 0x0, "GFX%rc6" },
413 { 0x0, "GFXMHz" },
414 { 0x0, "Pkg%pc2" },
415 { 0x0, "Pkg%pc3" },
416 { 0x0, "Pkg%pc6" },
417 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500418 { 0x0, "Pkg%pc8" },
419 { 0x0, "Pkg%pc9" },
Len Brown4bd1f8f2018-01-28 23:42:42 -0500420 { 0x0, "Pk%pc10" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400421 { 0x0, "CPU%LPI" },
422 { 0x0, "SYS%LPI" },
Len Brown812db3f2017-02-10 00:25:41 -0500423 { 0x0, "PkgWatt" },
424 { 0x0, "CorWatt" },
425 { 0x0, "GFXWatt" },
426 { 0x0, "PkgCnt" },
427 { 0x0, "RAMWatt" },
428 { 0x0, "PKG_%" },
429 { 0x0, "RAM_%" },
430 { 0x0, "Pkg_J" },
431 { 0x0, "Cor_J" },
432 { 0x0, "GFX_J" },
433 { 0x0, "RAM_J" },
Len Brown0539ba112017-02-10 00:27:20 -0500434 { 0x0, "Mod%c6" },
Len Browna99d8732017-05-20 20:11:55 -0400435 { 0x0, "Totl%C0" },
436 { 0x0, "Any%C0" },
437 { 0x0, "GFX%C0" },
438 { 0x0, "CPUGFX%" },
Len Brownbdd5ae32018-06-06 17:18:36 -0400439 { 0x0, "Core" },
440 { 0x0, "CPU" },
Len Brown4c2122d2018-06-06 17:44:48 -0400441 { 0x0, "APIC" },
442 { 0x0, "X2APIC" },
Len Brown812db3f2017-02-10 00:25:41 -0500443};
444
445#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400446#define BIC_USEC (1ULL << 0)
447#define BIC_TOD (1ULL << 1)
448#define BIC_Package (1ULL << 2)
Len Brownbdd5ae32018-06-06 17:18:36 -0400449#define BIC_Node (1ULL << 3)
450#define BIC_Avg_MHz (1ULL << 4)
451#define BIC_Busy (1ULL << 5)
452#define BIC_Bzy_MHz (1ULL << 6)
453#define BIC_TSC_MHz (1ULL << 7)
454#define BIC_IRQ (1ULL << 8)
455#define BIC_SMI (1ULL << 9)
456#define BIC_sysfs (1ULL << 10)
457#define BIC_CPU_c1 (1ULL << 11)
458#define BIC_CPU_c3 (1ULL << 12)
459#define BIC_CPU_c6 (1ULL << 13)
460#define BIC_CPU_c7 (1ULL << 14)
461#define BIC_ThreadC (1ULL << 15)
462#define BIC_CoreTmp (1ULL << 16)
463#define BIC_CoreCnt (1ULL << 17)
464#define BIC_PkgTmp (1ULL << 18)
465#define BIC_GFX_rc6 (1ULL << 19)
466#define BIC_GFXMHz (1ULL << 20)
467#define BIC_Pkgpc2 (1ULL << 21)
468#define BIC_Pkgpc3 (1ULL << 22)
469#define BIC_Pkgpc6 (1ULL << 23)
470#define BIC_Pkgpc7 (1ULL << 24)
471#define BIC_Pkgpc8 (1ULL << 25)
472#define BIC_Pkgpc9 (1ULL << 26)
473#define BIC_Pkgpc10 (1ULL << 27)
474#define BIC_CPU_LPI (1ULL << 28)
475#define BIC_SYS_LPI (1ULL << 29)
476#define BIC_PkgWatt (1ULL << 30)
477#define BIC_CorWatt (1ULL << 31)
478#define BIC_GFXWatt (1ULL << 32)
479#define BIC_PkgCnt (1ULL << 33)
480#define BIC_RAMWatt (1ULL << 34)
481#define BIC_PKG__ (1ULL << 35)
482#define BIC_RAM__ (1ULL << 36)
483#define BIC_Pkg_J (1ULL << 37)
484#define BIC_Cor_J (1ULL << 38)
485#define BIC_GFX_J (1ULL << 39)
486#define BIC_RAM_J (1ULL << 40)
487#define BIC_Mod_c6 (1ULL << 41)
488#define BIC_Totl_c0 (1ULL << 42)
489#define BIC_Any_c0 (1ULL << 43)
490#define BIC_GFX_c0 (1ULL << 44)
491#define BIC_CPUGFX (1ULL << 45)
492#define BIC_Core (1ULL << 46)
493#define BIC_CPU (1ULL << 47)
Len Brown4c2122d2018-06-06 17:44:48 -0400494#define BIC_APIC (1ULL << 48)
495#define BIC_X2APIC (1ULL << 49)
Len Brown812db3f2017-02-10 00:25:41 -0500496
Len Brown4c2122d2018-06-06 17:44:48 -0400497#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
Len Brown3f44a5c2017-10-17 15:42:56 -0400498
499unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
Len Brown4c2122d2018-06-06 17:44:48 -0400500unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
Len Brown812db3f2017-02-10 00:25:41 -0500501
502#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400503#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500504#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500505#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500506
Len Brown3f44a5c2017-10-17 15:42:56 -0400507
Len Browndd778a52017-02-21 23:21:13 -0500508#define MAX_DEFERRED 16
509char *deferred_skip_names[MAX_DEFERRED];
510int deferred_skip_index;
511
512/*
513 * HIDE_LIST - hide this list of counters, show the rest [default]
514 * SHOW_LIST - show this list of counters, hide the rest
515 */
516enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
517
518void help(void)
519{
520 fprintf(outf,
521 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
522 "\n"
523 "Turbostat forks the specified COMMAND and prints statistics\n"
524 "when COMMAND completes.\n"
525 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
526 "to print statistics, until interrupted.\n"
Nathan Ciobanucc481652018-06-13 19:51:33 -0700527 " -a, --add add a counter\n"
528 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
529 " -c, --cpu cpu-set limit output to summary plus cpu-set:\n"
530 " {core | package | j,k,l..m,n-p }\n"
Nathan Ciobanu9ce80572018-06-13 19:51:34 -0700531 " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n"
532 " -D, --Dump displays the raw counter values\n"
533 " -e, --enable [all | column]\n"
534 " shows all or the specified disabled column\n"
535 " -H, --hide [column|column,column,...]\n"
536 " hide the specified column(s)\n"
Nathan Ciobanucc481652018-06-13 19:51:33 -0700537 " -i, --interval sec.subsec\n"
538 " Override default 5-second measurement interval\n"
Nathan Ciobanu9ce80572018-06-13 19:51:34 -0700539 " -J, --Joules displays energy in Joules instead of Watts\n"
Nathan Ciobanucc481652018-06-13 19:51:33 -0700540 " -l, --list list column headers only\n"
541 " -n, --num_iterations num\n"
542 " number of the measurement iterations\n"
543 " -o, --out file\n"
544 " create or truncate \"file\" for all output\n"
545 " -q, --quiet skip decoding system configuration header\n"
Nathan Ciobanu9ce80572018-06-13 19:51:34 -0700546 " -s, --show [column|column,column,...]\n"
547 " show only the specified column(s)\n"
548 " -S, --Summary\n"
549 " limits output to 1-line system summary per interval\n"
550 " -T, --TCC temperature\n"
551 " sets the Thermal Control Circuit temperature in\n"
552 " degrees Celsius\n"
Nathan Ciobanucc481652018-06-13 19:51:33 -0700553 " -h, --help print this help message\n"
554 " -v, --version print version information\n"
Len Browndd778a52017-02-21 23:21:13 -0500555 "\n"
556 "For more help, run \"man turbostat\"\n");
557}
558
Len Brown812db3f2017-02-10 00:25:41 -0500559/*
560 * bic_lookup
561 * for all the strings in comma separate name_list,
562 * set the approprate bit in return value.
563 */
Len Browndd778a52017-02-21 23:21:13 -0500564unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500565{
566 int i;
567 unsigned long long retval = 0;
568
569 while (name_list) {
570 char *comma;
571
572 comma = strchr(name_list, ',');
573
574 if (comma)
575 *comma = '\0';
576
Len Brown3f44a5c2017-10-17 15:42:56 -0400577 if (!strcmp(name_list, "all"))
578 return ~0;
579
Len Brown812db3f2017-02-10 00:25:41 -0500580 for (i = 0; i < MAX_BIC; ++i) {
581 if (!strcmp(name_list, bic[i].name)) {
582 retval |= (1ULL << i);
583 break;
584 }
585 }
586 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500587 if (mode == SHOW_LIST) {
588 fprintf(stderr, "Invalid counter name: %s\n", name_list);
589 exit(-1);
590 }
591 deferred_skip_names[deferred_skip_index++] = name_list;
592 if (debug)
593 fprintf(stderr, "deferred \"%s\"\n", name_list);
594 if (deferred_skip_index >= MAX_DEFERRED) {
595 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
596 MAX_DEFERRED, name_list);
597 help();
598 exit(1);
599 }
Len Brown812db3f2017-02-10 00:25:41 -0500600 }
601
602 name_list = comma;
603 if (name_list)
604 name_list++;
605
606 }
607 return retval;
608}
Len Brownfc04cc62014-02-06 00:55:19 -0500609
Len Browndd778a52017-02-21 23:21:13 -0500610
Len Brownc8ade362017-02-15 17:15:11 -0500611void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400612{
Len Brown388e9c82016-12-22 23:57:55 -0500613 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500614 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500615
Len Brown3f44a5c2017-10-17 15:42:56 -0400616 if (DO_BIC(BIC_USEC))
617 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
618 if (DO_BIC(BIC_TOD))
619 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500620 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500621 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Prarit Bhargava01235042018-06-01 10:04:35 -0400622 if (DO_BIC(BIC_Node))
623 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500624 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500625 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500626 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500627 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown4c2122d2018-06-06 17:44:48 -0400628 if (DO_BIC(BIC_APIC))
629 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
630 if (DO_BIC(BIC_X2APIC))
631 outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500632 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500633 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500634 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500635 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500636 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500637 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500638 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500639 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500640
Len Brown0de6c0d2017-02-15 21:45:40 -0500641 if (DO_BIC(BIC_IRQ)) {
642 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500643 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500644 else
Len Brown6168c2e2017-02-16 23:07:51 -0500645 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500646 }
647
Len Brown812db3f2017-02-10 00:25:41 -0500648 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500649 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500650
Len Brown388e9c82016-12-22 23:57:55 -0500651 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500652
Len Brown388e9c82016-12-22 23:57:55 -0500653 if (mp->format == FORMAT_RAW) {
654 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500655 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500656 else
Len Browndd778a52017-02-21 23:21:13 -0500657 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500658 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500659 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500660 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500661 else
Len Browndd778a52017-02-21 23:21:13 -0500662 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500663 }
664 }
665
Len Brown41618e62017-02-09 18:25:22 -0500666 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500667 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700668 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500669 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500670 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500671 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500672 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500673 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500674
Len Brown0539ba112017-02-10 00:27:20 -0500675 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500676 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500677
Len Brown812db3f2017-02-10 00:25:41 -0500678 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500679 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500680
681 for (mp = sys.cp; mp; mp = mp->next) {
682 if (mp->format == FORMAT_RAW) {
683 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500684 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500685 else
Len Brownc8ade362017-02-15 17:15:11 -0500686 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500687 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500688 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
689 outp += sprintf(outp, "%s%8s", delim, mp->name);
690 else
691 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500692 }
693 }
694
Len Brown812db3f2017-02-10 00:25:41 -0500695 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500696 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500697
Len Brown812db3f2017-02-10 00:25:41 -0500698 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500699 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500700
Len Brown812db3f2017-02-10 00:25:41 -0500701 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500702 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500703
Len Browna99d8732017-05-20 20:11:55 -0400704 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500705 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400706 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500707 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400708 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500709 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400710 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500711 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400712
Len Brown0f47c082017-01-27 00:50:45 -0500713 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500714 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500715 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500716 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500717 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500718 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500719 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500720 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500721 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500722 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500723 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500724 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500725 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500726 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brownbe0e54c2018-06-01 12:35:53 -0400727 if (DO_BIC(BIC_CPU_LPI))
728 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
729 if (DO_BIC(BIC_SYS_LPI))
730 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400731
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800732 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500733 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500734 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500735 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500736 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500737 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500738 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500739 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500740 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500741 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500742 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500743 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500744 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500745 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500746 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500747 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500748 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500749 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500750 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500751 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500752 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500753 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500754 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500755 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500756 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500757 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800758 }
Len Brown388e9c82016-12-22 23:57:55 -0500759 for (mp = sys.pp; mp; mp = mp->next) {
760 if (mp->format == FORMAT_RAW) {
761 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500762 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500763 else
Len Brownc8ade362017-02-15 17:15:11 -0500764 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500765 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500766 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
767 outp += sprintf(outp, "%s%8s", delim, mp->name);
768 else
769 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500770 }
771 }
772
Len Brownc98d5d92012-06-04 00:56:40 -0400773 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400774}
775
Len Brownc98d5d92012-06-04 00:56:40 -0400776int dump_counters(struct thread_data *t, struct core_data *c,
777 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400778{
Len Brown388e9c82016-12-22 23:57:55 -0500779 int i;
780 struct msr_counter *mp;
781
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200782 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400783
Len Brownc98d5d92012-06-04 00:56:40 -0400784 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200785 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
786 t->cpu_id, t->flags);
787 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
788 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
789 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
790 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500791
Len Brown812db3f2017-02-10 00:25:41 -0500792 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500793 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500794 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500795 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500796
797 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
798 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
799 i, mp->msr_num, t->counter[i]);
800 }
Len Brownc98d5d92012-06-04 00:56:40 -0400801 }
Len Brown103a8fe2010-10-22 23:53:03 -0400802
Len Brownc98d5d92012-06-04 00:56:40 -0400803 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200804 outp += sprintf(outp, "core: %d\n", c->core_id);
805 outp += sprintf(outp, "c3: %016llX\n", c->c3);
806 outp += sprintf(outp, "c6: %016llX\n", c->c6);
807 outp += sprintf(outp, "c7: %016llX\n", c->c7);
808 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500809
810 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
811 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
812 i, mp->msr_num, c->counter[i]);
813 }
Len Brown0539ba112017-02-10 00:27:20 -0500814 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400815 }
816
817 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200818 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400819
820 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
821 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
822 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
823 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
824
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200825 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500826 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500827 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500828 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500829 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500830 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500831 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200832 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
833 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
834 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
Len Brownbe0e54c2018-06-01 12:35:53 -0400835 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
836 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
837 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200838 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
839 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
840 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
841 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
842 outp += sprintf(outp, "Throttle PKG: %0X\n",
843 p->rapl_pkg_perf_status);
844 outp += sprintf(outp, "Throttle RAM: %0X\n",
845 p->rapl_dram_perf_status);
846 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500847
848 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
849 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
850 i, mp->msr_num, p->counter[i]);
851 }
Len Brownc98d5d92012-06-04 00:56:40 -0400852 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200853
854 outp += sprintf(outp, "\n");
855
Len Brownc98d5d92012-06-04 00:56:40 -0400856 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400857}
858
Len Browne23da032012-02-06 18:37:16 -0500859/*
860 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500861 */
Len Brownc98d5d92012-06-04 00:56:40 -0400862int format_counters(struct thread_data *t, struct core_data *c,
863 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400864{
Len Brown008d396e2017-02-10 00:29:51 -0500865 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500866 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500867 int i;
868 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500869 char *delim = "\t";
870 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400871
Len Brownc98d5d92012-06-04 00:56:40 -0400872 /* if showing only 1st thread in core and this isn't one, bail out */
873 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
874 return 0;
875
876 /* if showing only 1st thread in pkg and this isn't one, bail out */
877 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
878 return 0;
879
Len Brown1ef7d212017-02-10 23:54:15 -0500880 /*if not summary line and --cpu is used */
881 if ((t != &average.threads) &&
882 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
883 return 0;
884
Len Brown3f44a5c2017-10-17 15:42:56 -0400885 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700886 /* on each row, print how many usec each timestamp took to gather */
887 struct timeval tv;
888
889 timersub(&t->tv_end, &t->tv_begin, &tv);
890 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
891 }
892
Len Brown3f44a5c2017-10-17 15:42:56 -0400893 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
894 if (DO_BIC(BIC_TOD))
895 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
896
Len Brown103a8fe2010-10-22 23:53:03 -0400897 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
898
Len Brown008d396e2017-02-10 00:29:51 -0500899 tsc = t->tsc * tsc_tweak;
900
Len Brownc98d5d92012-06-04 00:56:40 -0400901 /* topo columns, print blanks on 1st (average) line */
902 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500903 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500904 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Prarit Bhargava01235042018-06-01 10:04:35 -0400905 if (DO_BIC(BIC_Node))
906 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500907 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500908 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500909 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500910 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown4c2122d2018-06-06 17:44:48 -0400911 if (DO_BIC(BIC_APIC))
912 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
913 if (DO_BIC(BIC_X2APIC))
914 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400915 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500916 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400917 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500918 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400919 else
Len Brown6168c2e2017-02-16 23:07:51 -0500920 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400921 }
Prarit Bhargava01235042018-06-01 10:04:35 -0400922 if (DO_BIC(BIC_Node)) {
923 if (t)
924 outp += sprintf(outp, "%s%d",
925 (printed++ ? delim : ""),
926 cpus[t->cpu_id].physical_node_id);
927 else
928 outp += sprintf(outp, "%s-",
929 (printed++ ? delim : ""));
930 }
Len Brown812db3f2017-02-10 00:25:41 -0500931 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400932 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500933 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400934 else
Len Brown6168c2e2017-02-16 23:07:51 -0500935 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400936 }
Len Brown812db3f2017-02-10 00:25:41 -0500937 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500938 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown4c2122d2018-06-06 17:44:48 -0400939 if (DO_BIC(BIC_APIC))
940 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
941 if (DO_BIC(BIC_X2APIC))
942 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400943 }
Len Brownfc04cc62014-02-06 00:55:19 -0500944
Len Brown812db3f2017-02-10 00:25:41 -0500945 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500946 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500947 1.0 / units * t->aperf / interval_float);
948
Len Brown812db3f2017-02-10 00:25:41 -0500949 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500950 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400951
Len Brown812db3f2017-02-10 00:25:41 -0500952 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400953 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500954 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400955 else
Len Brown6168c2e2017-02-16 23:07:51 -0500956 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500957 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400958 }
Len Brown103a8fe2010-10-22 23:53:03 -0400959
Len Brown812db3f2017-02-10 00:25:41 -0500960 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500961 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400962
Len Brown562a2d32016-02-26 23:48:05 -0500963 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500964 if (DO_BIC(BIC_IRQ)) {
965 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500966 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500967 else
Len Brown6168c2e2017-02-16 23:07:51 -0500968 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500969 }
Len Brown562a2d32016-02-26 23:48:05 -0500970
Len Brown1cc21f72015-02-23 00:34:57 -0500971 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500972 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500973 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500974
Len Brown678a3bd2017-02-09 22:22:13 -0500975 /* Added counters */
976 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
977 if (mp->format == FORMAT_RAW) {
978 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500979 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500980 else
Len Brown6168c2e2017-02-16 23:07:51 -0500981 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500982 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500983 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500984 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500985 else
Len Brown6168c2e2017-02-16 23:07:51 -0500986 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500987 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500988 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500989 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500990 else
Len Brown6168c2e2017-02-16 23:07:51 -0500991 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500992 }
993 }
994
Len Brown41618e62017-02-09 18:25:22 -0500995 /* C1 */
996 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500997 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500998
999
Len Brownc98d5d92012-06-04 00:56:40 -04001000 /* print per-core data only for 1st thread in core */
1001 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1002 goto done;
1003
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07001004 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -05001005 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -05001006 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -05001007 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -05001008 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -05001009 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -04001010
Len Brown0539ba112017-02-10 00:27:20 -05001011 /* Mod%c6 */
1012 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -05001013 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba112017-02-10 00:27:20 -05001014
Len Brown812db3f2017-02-10 00:25:41 -05001015 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -05001016 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -05001017
Len Brown388e9c82016-12-22 23:57:55 -05001018 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1019 if (mp->format == FORMAT_RAW) {
1020 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001021 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001022 else
Len Brown6168c2e2017-02-16 23:07:51 -05001023 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001024 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001025 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001026 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001027 else
Len Brown6168c2e2017-02-16 23:07:51 -05001028 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001029 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001030 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001031 }
1032 }
1033
Len Brownc98d5d92012-06-04 00:56:40 -04001034 /* print per-package data only for 1st core in package */
1035 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1036 goto done;
1037
Len Brown0b2bb692015-03-26 00:50:30 -04001038 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -05001039 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -05001040 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -05001041
Len Brownfdf676e2016-02-27 01:28:12 -05001042 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -05001043 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -04001044 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -05001045 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -04001046 } else {
Len Brown6168c2e2017-02-16 23:07:51 -05001047 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -04001048 p->gfx_rc6_ms / 10.0 / interval_float);
1049 }
1050 }
Len Brownfdf676e2016-02-27 01:28:12 -05001051
Len Brown27d47352016-02-27 00:37:54 -05001052 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -05001053 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -05001054 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -05001055
Len Brown0b2bb692015-03-26 00:50:30 -04001056 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -04001057 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001058 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001059 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001060 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001061 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001062 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001063 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -05001064 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -04001065
Len Brown0f47c082017-01-27 00:50:45 -05001066 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -05001067 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001068 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -05001069 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001070 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -05001071 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001072 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -05001073 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001074 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -05001075 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001076 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -05001077 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001078 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -05001079 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -05001080
Len Brownbe0e54c2018-06-01 12:35:53 -04001081 if (DO_BIC(BIC_CPU_LPI))
1082 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1083 if (DO_BIC(BIC_SYS_LPI))
1084 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1085
Len Brown889facb2012-11-08 00:48:57 -05001086 /*
1087 * If measurement interval exceeds minimum RAPL Joule Counter range,
1088 * indicate that results are suspect by printing "**" in fraction place.
1089 */
Len Brownfc04cc62014-02-06 00:55:19 -05001090 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -05001091 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -05001092 else
Len Browne975db52016-04-06 23:56:02 -04001093 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -05001094
Len Brown812db3f2017-02-10 00:25:41 -05001095 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001096 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001097 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001098 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001099 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001100 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001101 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001102 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001103 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001104 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001105 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001106 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001107 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001108 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001109 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001110 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001111 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001112 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 -05001113 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001114 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 -05001115
Len Brown388e9c82016-12-22 23:57:55 -05001116 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1117 if (mp->format == FORMAT_RAW) {
1118 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001119 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001120 else
Len Brown6168c2e2017-02-16 23:07:51 -05001121 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001122 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001123 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001124 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001125 else
Len Brown6168c2e2017-02-16 23:07:51 -05001126 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001127 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001128 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001129 }
1130 }
1131
Len Brownc98d5d92012-06-04 00:56:40 -04001132done:
Len Brown94d6ab42018-01-27 22:39:21 -05001133 if (*(outp - 1) != '\n')
1134 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001135
1136 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001137}
1138
Len Brownb7d8c142016-02-13 23:36:17 -05001139void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001140{
Len Brownb7d8c142016-02-13 23:36:17 -05001141 FILE *filep;
1142
1143 if (outf == stderr)
1144 filep = stdout;
1145 else
1146 filep = outf;
1147
1148 fputs(output_buffer, filep);
1149 fflush(filep);
1150
Len Brownc98d5d92012-06-04 00:56:40 -04001151 outp = output_buffer;
1152}
Len Brownb7d8c142016-02-13 23:36:17 -05001153void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001154{
Len Brownb7d8c142016-02-13 23:36:17 -05001155 fputs(output_buffer, outf);
1156 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001157 outp = output_buffer;
1158}
1159void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1160{
Len Browne23da032012-02-06 18:37:16 -05001161 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001162
Len Browne23da032012-02-06 18:37:16 -05001163 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001164 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001165
Len Brown9d836012018-07-20 14:47:03 -04001166 format_counters(&average.threads, &average.cores, &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001167
Len Browne23da032012-02-06 18:37:16 -05001168 printed = 1;
1169
1170 if (summary_only)
1171 return;
1172
Len Brownc98d5d92012-06-04 00:56:40 -04001173 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001174}
1175
Len Brown889facb2012-11-08 00:48:57 -05001176#define DELTA_WRAP32(new, old) \
1177 if (new > old) { \
1178 old = new - old; \
1179 } else { \
1180 old = 0x100000000 + new - old; \
1181 }
1182
Len Brownba3dec92016-04-22 20:31:46 -04001183int
Len Brownc98d5d92012-06-04 00:56:40 -04001184delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001185{
Len Brown388e9c82016-12-22 23:57:55 -05001186 int i;
1187 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001188
Len Browna99d8732017-05-20 20:11:55 -04001189
1190 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001191 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001192 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001193 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001194 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001195 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001196 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001197 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 -04001198
Len Brownc98d5d92012-06-04 00:56:40 -04001199 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001200 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001201 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001202 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001203 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001204 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001205 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001206 old->pc8 = new->pc8 - old->pc8;
1207 old->pc9 = new->pc9 - old->pc9;
1208 old->pc10 = new->pc10 - old->pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -04001209 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1210 old->sys_lpi = new->sys_lpi - old->sys_lpi;
Len Brown889facb2012-11-08 00:48:57 -05001211 old->pkg_temp_c = new->pkg_temp_c;
1212
Len Brown9185e982016-04-06 17:16:00 -04001213 /* flag an error when rc6 counter resets/wraps */
1214 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1215 old->gfx_rc6_ms = -1;
1216 else
1217 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1218
Len Brown27d47352016-02-27 00:37:54 -05001219 old->gfx_mhz = new->gfx_mhz;
1220
Len Brown889facb2012-11-08 00:48:57 -05001221 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1222 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1223 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1224 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1225 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1226 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001227
Len Brown388e9c82016-12-22 23:57:55 -05001228 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1229 if (mp->format == FORMAT_RAW)
1230 old->counter[i] = new->counter[i];
1231 else
1232 old->counter[i] = new->counter[i] - old->counter[i];
1233 }
1234
Len Brownba3dec92016-04-22 20:31:46 -04001235 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001236}
Len Brown103a8fe2010-10-22 23:53:03 -04001237
Len Brownc98d5d92012-06-04 00:56:40 -04001238void
1239delta_core(struct core_data *new, struct core_data *old)
1240{
Len Brown388e9c82016-12-22 23:57:55 -05001241 int i;
1242 struct msr_counter *mp;
1243
Len Brownc98d5d92012-06-04 00:56:40 -04001244 old->c3 = new->c3 - old->c3;
1245 old->c6 = new->c6 - old->c6;
1246 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001247 old->core_temp_c = new->core_temp_c;
Len Brown0539ba112017-02-10 00:27:20 -05001248 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001249
1250 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1251 if (mp->format == FORMAT_RAW)
1252 old->counter[i] = new->counter[i];
1253 else
1254 old->counter[i] = new->counter[i] - old->counter[i];
1255 }
Len Brownc98d5d92012-06-04 00:56:40 -04001256}
Len Brown103a8fe2010-10-22 23:53:03 -04001257
Len Brownc3ae3312012-06-13 21:31:46 -04001258/*
1259 * old = new - old
1260 */
Len Brownba3dec92016-04-22 20:31:46 -04001261int
Len Brownc98d5d92012-06-04 00:56:40 -04001262delta_thread(struct thread_data *new, struct thread_data *old,
1263 struct core_data *core_delta)
1264{
Len Brown388e9c82016-12-22 23:57:55 -05001265 int i;
1266 struct msr_counter *mp;
1267
Len Brown4c2122d2018-06-06 17:44:48 -04001268 /* we run cpuid just the 1st time, copy the results */
1269 if (DO_BIC(BIC_APIC))
1270 new->apic_id = old->apic_id;
1271 if (DO_BIC(BIC_X2APIC))
1272 new->x2apic_id = old->x2apic_id;
1273
Len Brown3f44a5c2017-10-17 15:42:56 -04001274 /*
1275 * the timestamps from start of measurement interval are in "old"
1276 * the timestamp from end of measurement interval are in "new"
1277 * over-write old w/ new so we can print end of interval values
1278 */
1279
1280 old->tv_begin = new->tv_begin;
1281 old->tv_end = new->tv_end;
1282
Len Brownc98d5d92012-06-04 00:56:40 -04001283 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001284
Len Brownc98d5d92012-06-04 00:56:40 -04001285 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001286 if (old->tsc < (1000 * 1000))
1287 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1288 "You can disable all c-states by booting with \"idle=poll\"\n"
1289 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001290
Len Brownc98d5d92012-06-04 00:56:40 -04001291 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001292
Len Brown812db3f2017-02-10 00:25:41 -05001293 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001294 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1295 old->aperf = new->aperf - old->aperf;
1296 old->mperf = new->mperf - old->mperf;
1297 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001298 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001299 }
Len Brownc98d5d92012-06-04 00:56:40 -04001300 }
Len Brown103a8fe2010-10-22 23:53:03 -04001301
Len Brown103a8fe2010-10-22 23:53:03 -04001302
Len Brown144b44b2013-11-09 00:30:16 -05001303 if (use_c1_residency_msr) {
1304 /*
1305 * Some models have a dedicated C1 residency MSR,
1306 * which should be more accurate than the derivation below.
1307 */
1308 } else {
1309 /*
1310 * As counter collection is not atomic,
1311 * it is possible for mperf's non-halted cycles + idle states
1312 * to exceed TSC's all cycles: show c1 = 0% in that case.
1313 */
Len Brown95149362017-04-12 19:44:51 -04001314 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001315 old->c1 = 0;
1316 else {
1317 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001318 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001319 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001320 }
Len Brownc98d5d92012-06-04 00:56:40 -04001321 }
Len Brownc3ae3312012-06-13 21:31:46 -04001322
Len Brownc98d5d92012-06-04 00:56:40 -04001323 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001324 if (debug > 1)
1325 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001326 old->mperf = 1; /* divide by 0 protection */
1327 }
1328
Len Brown812db3f2017-02-10 00:25:41 -05001329 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001330 old->irq_count = new->irq_count - old->irq_count;
1331
Len Brown812db3f2017-02-10 00:25:41 -05001332 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001333 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001334
Len Brown388e9c82016-12-22 23:57:55 -05001335 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1336 if (mp->format == FORMAT_RAW)
1337 old->counter[i] = new->counter[i];
1338 else
1339 old->counter[i] = new->counter[i] - old->counter[i];
1340 }
Len Brownba3dec92016-04-22 20:31:46 -04001341 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001342}
1343
1344int delta_cpu(struct thread_data *t, struct core_data *c,
1345 struct pkg_data *p, struct thread_data *t2,
1346 struct core_data *c2, struct pkg_data *p2)
1347{
Len Brownba3dec92016-04-22 20:31:46 -04001348 int retval = 0;
1349
Len Brownc98d5d92012-06-04 00:56:40 -04001350 /* calculate core delta only for 1st thread in core */
1351 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1352 delta_core(c, c2);
1353
1354 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001355 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1356 if (retval)
1357 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001358
1359 /* calculate package delta only for 1st core in package */
1360 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001361 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001362
Len Brownba3dec92016-04-22 20:31:46 -04001363 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001364}
1365
1366void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1367{
Len Brown388e9c82016-12-22 23:57:55 -05001368 int i;
1369 struct msr_counter *mp;
1370
Len Brown3f44a5c2017-10-17 15:42:56 -04001371 t->tv_begin.tv_sec = 0;
1372 t->tv_begin.tv_usec = 0;
1373 t->tv_end.tv_sec = 0;
1374 t->tv_end.tv_usec = 0;
1375
Len Brownc98d5d92012-06-04 00:56:40 -04001376 t->tsc = 0;
1377 t->aperf = 0;
1378 t->mperf = 0;
1379 t->c1 = 0;
1380
Len Brown562a2d32016-02-26 23:48:05 -05001381 t->irq_count = 0;
1382 t->smi_count = 0;
1383
Len Brownc98d5d92012-06-04 00:56:40 -04001384 /* tells format_counters to dump all fields from this set */
1385 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1386
1387 c->c3 = 0;
1388 c->c6 = 0;
1389 c->c7 = 0;
Len Brown0539ba112017-02-10 00:27:20 -05001390 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001391 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001392
Len Brown0b2bb692015-03-26 00:50:30 -04001393 p->pkg_wtd_core_c0 = 0;
1394 p->pkg_any_core_c0 = 0;
1395 p->pkg_any_gfxe_c0 = 0;
1396 p->pkg_both_core_gfxe_c0 = 0;
1397
Len Brownc98d5d92012-06-04 00:56:40 -04001398 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001399 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001400 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001401 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001402 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001403 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001404 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001405 p->pc8 = 0;
1406 p->pc9 = 0;
1407 p->pc10 = 0;
Len Brownbe0e54c2018-06-01 12:35:53 -04001408 p->cpu_lpi = 0;
1409 p->sys_lpi = 0;
Len Brown889facb2012-11-08 00:48:57 -05001410
1411 p->energy_pkg = 0;
1412 p->energy_dram = 0;
1413 p->energy_cores = 0;
1414 p->energy_gfx = 0;
1415 p->rapl_pkg_perf_status = 0;
1416 p->rapl_dram_perf_status = 0;
1417 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001418
Len Brownfdf676e2016-02-27 01:28:12 -05001419 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001420 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001421 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1422 t->counter[i] = 0;
1423
1424 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1425 c->counter[i] = 0;
1426
1427 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1428 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001429}
1430int sum_counters(struct thread_data *t, struct core_data *c,
1431 struct pkg_data *p)
1432{
Len Brown388e9c82016-12-22 23:57:55 -05001433 int i;
1434 struct msr_counter *mp;
1435
Len Brown4c2122d2018-06-06 17:44:48 -04001436 /* copy un-changing apic_id's */
1437 if (DO_BIC(BIC_APIC))
1438 average.threads.apic_id = t->apic_id;
1439 if (DO_BIC(BIC_X2APIC))
1440 average.threads.x2apic_id = t->x2apic_id;
1441
Len Brown3f44a5c2017-10-17 15:42:56 -04001442 /* remember first tv_begin */
1443 if (average.threads.tv_begin.tv_sec == 0)
1444 average.threads.tv_begin = t->tv_begin;
1445
1446 /* remember last tv_end */
1447 average.threads.tv_end = t->tv_end;
1448
Len Brownc98d5d92012-06-04 00:56:40 -04001449 average.threads.tsc += t->tsc;
1450 average.threads.aperf += t->aperf;
1451 average.threads.mperf += t->mperf;
1452 average.threads.c1 += t->c1;
1453
Len Brown562a2d32016-02-26 23:48:05 -05001454 average.threads.irq_count += t->irq_count;
1455 average.threads.smi_count += t->smi_count;
1456
Len Brown388e9c82016-12-22 23:57:55 -05001457 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1458 if (mp->format == FORMAT_RAW)
1459 continue;
1460 average.threads.counter[i] += t->counter[i];
1461 }
1462
Len Brownc98d5d92012-06-04 00:56:40 -04001463 /* sum per-core values only for 1st thread in core */
1464 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1465 return 0;
1466
1467 average.cores.c3 += c->c3;
1468 average.cores.c6 += c->c6;
1469 average.cores.c7 += c->c7;
Len Brown0539ba112017-02-10 00:27:20 -05001470 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001471
Len Brown889facb2012-11-08 00:48:57 -05001472 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1473
Len Brown388e9c82016-12-22 23:57:55 -05001474 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1475 if (mp->format == FORMAT_RAW)
1476 continue;
1477 average.cores.counter[i] += c->counter[i];
1478 }
1479
Len Brownc98d5d92012-06-04 00:56:40 -04001480 /* sum per-pkg values only for 1st core in pkg */
1481 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1482 return 0;
1483
Len Browna99d8732017-05-20 20:11:55 -04001484 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001485 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001486 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001487 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001488 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001489 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001490 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001491 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001492
Len Brownc98d5d92012-06-04 00:56:40 -04001493 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001494 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001495 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001496 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001497 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001498 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001499 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001500 average.packages.pc8 += p->pc8;
1501 average.packages.pc9 += p->pc9;
1502 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001503
Len Brownbe0e54c2018-06-01 12:35:53 -04001504 average.packages.cpu_lpi = p->cpu_lpi;
1505 average.packages.sys_lpi = p->sys_lpi;
1506
Len Brown889facb2012-11-08 00:48:57 -05001507 average.packages.energy_pkg += p->energy_pkg;
1508 average.packages.energy_dram += p->energy_dram;
1509 average.packages.energy_cores += p->energy_cores;
1510 average.packages.energy_gfx += p->energy_gfx;
1511
Len Brownfdf676e2016-02-27 01:28:12 -05001512 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001513 average.packages.gfx_mhz = p->gfx_mhz;
1514
Len Brown889facb2012-11-08 00:48:57 -05001515 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1516
1517 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1518 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001519
1520 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1521 if (mp->format == FORMAT_RAW)
1522 continue;
1523 average.packages.counter[i] += p->counter[i];
1524 }
Len Brownc98d5d92012-06-04 00:56:40 -04001525 return 0;
1526}
1527/*
1528 * sum the counters for all cpus in the system
1529 * compute the weighted average
1530 */
1531void compute_average(struct thread_data *t, struct core_data *c,
1532 struct pkg_data *p)
1533{
Len Brown388e9c82016-12-22 23:57:55 -05001534 int i;
1535 struct msr_counter *mp;
1536
Len Brownc98d5d92012-06-04 00:56:40 -04001537 clear_counters(&average.threads, &average.cores, &average.packages);
1538
1539 for_all_cpus(sum_counters, t, c, p);
1540
1541 average.threads.tsc /= topo.num_cpus;
1542 average.threads.aperf /= topo.num_cpus;
1543 average.threads.mperf /= topo.num_cpus;
1544 average.threads.c1 /= topo.num_cpus;
1545
Len Brown0de6c0d2017-02-15 21:45:40 -05001546 if (average.threads.irq_count > 9999999)
1547 sums_need_wide_columns = 1;
1548
Len Brownc98d5d92012-06-04 00:56:40 -04001549 average.cores.c3 /= topo.num_cores;
1550 average.cores.c6 /= topo.num_cores;
1551 average.cores.c7 /= topo.num_cores;
Len Brown0539ba112017-02-10 00:27:20 -05001552 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001553
Len Browna99d8732017-05-20 20:11:55 -04001554 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001555 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001556 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001557 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001558 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001559 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001560 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001561 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001562
Len Brownc98d5d92012-06-04 00:56:40 -04001563 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001564 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001565 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001566 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001567 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001568 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001569 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001570
1571 average.packages.pc8 /= topo.num_packages;
1572 average.packages.pc9 /= topo.num_packages;
1573 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001574
1575 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1576 if (mp->format == FORMAT_RAW)
1577 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001578 if (mp->type == COUNTER_ITEMS) {
1579 if (average.threads.counter[i] > 9999999)
1580 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001581 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001582 }
Len Brown388e9c82016-12-22 23:57:55 -05001583 average.threads.counter[i] /= topo.num_cpus;
1584 }
1585 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1586 if (mp->format == FORMAT_RAW)
1587 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001588 if (mp->type == COUNTER_ITEMS) {
1589 if (average.cores.counter[i] > 9999999)
1590 sums_need_wide_columns = 1;
1591 }
Len Brown388e9c82016-12-22 23:57:55 -05001592 average.cores.counter[i] /= topo.num_cores;
1593 }
1594 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1595 if (mp->format == FORMAT_RAW)
1596 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001597 if (mp->type == COUNTER_ITEMS) {
1598 if (average.packages.counter[i] > 9999999)
1599 sums_need_wide_columns = 1;
1600 }
Len Brown388e9c82016-12-22 23:57:55 -05001601 average.packages.counter[i] /= topo.num_packages;
1602 }
Len Brownc98d5d92012-06-04 00:56:40 -04001603}
1604
1605static unsigned long long rdtsc(void)
1606{
1607 unsigned int low, high;
1608
1609 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1610
1611 return low | ((unsigned long long)high) << 32;
1612}
1613
Len Brownc98d5d92012-06-04 00:56:40 -04001614/*
Len Brown495c76542017-02-08 02:41:51 -05001615 * Open a file, and exit on failure
1616 */
1617FILE *fopen_or_die(const char *path, const char *mode)
1618{
1619 FILE *filep = fopen(path, mode);
1620
1621 if (!filep)
1622 err(1, "%s: open failed", path);
1623 return filep;
1624}
1625/*
1626 * snapshot_sysfs_counter()
1627 *
1628 * return snapshot of given counter
1629 */
1630unsigned long long snapshot_sysfs_counter(char *path)
1631{
1632 FILE *fp;
1633 int retval;
1634 unsigned long long counter;
1635
1636 fp = fopen_or_die(path, "r");
1637
1638 retval = fscanf(fp, "%lld", &counter);
1639 if (retval != 1)
1640 err(1, "snapshot_sysfs_counter(%s)", path);
1641
1642 fclose(fp);
1643
1644 return counter;
1645}
1646
1647int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1648{
1649 if (mp->msr_num != 0) {
1650 if (get_msr(cpu, mp->msr_num, counterp))
1651 return -1;
1652 } else {
Len Brown46c27972017-12-08 17:38:17 -05001653 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001654
1655 if (mp->flags & SYSFS_PERCPU) {
1656 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1657 cpu, mp->path);
1658
1659 *counterp = snapshot_sysfs_counter(path);
1660 } else {
1661 *counterp = snapshot_sysfs_counter(mp->path);
1662 }
Len Brown495c76542017-02-08 02:41:51 -05001663 }
1664
1665 return 0;
1666}
1667
Len Brown4c2122d2018-06-06 17:44:48 -04001668void get_apic_id(struct thread_data *t)
1669{
1670 unsigned int eax, ebx, ecx, edx, max_level;
1671
1672 eax = ebx = ecx = edx = 0;
1673
1674 if (!genuine_intel)
1675 return;
1676
1677 __cpuid(0, max_level, ebx, ecx, edx);
1678
1679 __cpuid(1, eax, ebx, ecx, edx);
1680 t->apic_id = (ebx >> 24) & 0xf;
1681
1682 if (max_level < 0xb)
1683 return;
1684
1685 if (!DO_BIC(BIC_X2APIC))
1686 return;
1687
1688 ecx = 0;
1689 __cpuid(0xb, eax, ebx, ecx, edx);
1690 t->x2apic_id = edx;
1691
1692 if (debug && (t->apic_id != t->x2apic_id))
1693 fprintf(stderr, "cpu%d: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id);
1694}
1695
Len Brown495c76542017-02-08 02:41:51 -05001696/*
Len Brownc98d5d92012-06-04 00:56:40 -04001697 * get_counters(...)
1698 * migrate to cpu
1699 * acquire and record local counters for that cpu
1700 */
1701int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1702{
1703 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001704 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001705 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001706 struct msr_counter *mp;
1707 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001708
Len Brownf4fdf2b2017-05-27 21:06:55 -07001709 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1710
Len Browne52966c2012-11-08 22:38:05 -05001711 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001712 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001713 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001714 }
Len Brownc98d5d92012-06-04 00:56:40 -04001715
Len Brown4c2122d2018-06-06 17:44:48 -04001716 if (first_counter_read)
1717 get_apic_id(t);
Len Brown0102b062016-02-27 03:11:29 -05001718retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001719 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1720
Len Brown812db3f2017-02-10 00:25:41 -05001721 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001722 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1723
1724 /*
1725 * The TSC, APERF and MPERF must be read together for
1726 * APERF/MPERF and MPERF/TSC to give accurate results.
1727 *
1728 * Unfortunately, APERF and MPERF are read by
1729 * individual system call, so delays may occur
1730 * between them. If the time to read them
1731 * varies by a large amount, we re-read them.
1732 */
1733
1734 /*
1735 * This initial dummy APERF read has been seen to
1736 * reduce jitter in the subsequent reads.
1737 */
1738
Len Brown9c63a652012-10-31 01:29:52 -04001739 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001740 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001741
1742 t->tsc = rdtsc(); /* re-read close to APERF */
1743
1744 tsc_before = t->tsc;
1745
1746 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1747 return -3;
1748
1749 tsc_between = rdtsc();
1750
Len Brown9c63a652012-10-31 01:29:52 -04001751 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001752 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001753
1754 tsc_after = rdtsc();
1755
1756 aperf_time = tsc_between - tsc_before;
1757 mperf_time = tsc_after - tsc_between;
1758
1759 /*
1760 * If the system call latency to read APERF and MPERF
1761 * differ by more than 2x, then try again.
1762 */
1763 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1764 aperf_mperf_retry_count++;
1765 if (aperf_mperf_retry_count < 5)
1766 goto retry;
1767 else
1768 warnx("cpu%d jitter %lld %lld",
1769 cpu, aperf_time, mperf_time);
1770 }
1771 aperf_mperf_retry_count = 0;
1772
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001773 t->aperf = t->aperf * aperf_mperf_multiplier;
1774 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001775 }
1776
Len Brown812db3f2017-02-10 00:25:41 -05001777 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001778 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001779 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001780 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1781 return -5;
1782 t->smi_count = msr & 0xFFFFFFFF;
1783 }
Len Brown0539ba112017-02-10 00:27:20 -05001784 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001785 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1786 return -6;
1787 }
1788
Len Brown388e9c82016-12-22 23:57:55 -05001789 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001790 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001791 return -10;
1792 }
1793
Len Brownc98d5d92012-06-04 00:56:40 -04001794 /* collect core counters only for 1st thread in core */
1795 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001796 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001797
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07001798 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001799 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1800 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001801 }
1802
Len Brown812db3f2017-02-10 00:25:41 -05001803 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001804 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1805 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001806 } else if (do_knl_cstates) {
1807 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1808 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001809 }
1810
Len Brown812db3f2017-02-10 00:25:41 -05001811 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001812 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1813 return -8;
1814
Len Brown0539ba112017-02-10 00:27:20 -05001815 if (DO_BIC(BIC_Mod_c6))
1816 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1817 return -8;
1818
Len Brown812db3f2017-02-10 00:25:41 -05001819 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001820 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1821 return -9;
1822 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1823 }
1824
Len Brown388e9c82016-12-22 23:57:55 -05001825 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001826 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001827 return -10;
1828 }
Len Brown889facb2012-11-08 00:48:57 -05001829
Len Brownc98d5d92012-06-04 00:56:40 -04001830 /* collect package counters only for 1st core in package */
1831 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001832 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001833
Len Browna99d8732017-05-20 20:11:55 -04001834 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001835 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1836 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001837 }
1838 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001839 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1840 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001841 }
1842 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001843 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1844 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001845 }
1846 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001847 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1848 return -13;
1849 }
Len Brown0f47c082017-01-27 00:50:45 -05001850 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001851 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1852 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001853 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba112017-02-10 00:27:20 -05001854 if (do_slm_cstates) {
1855 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1856 return -10;
1857 } else {
1858 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1859 return -10;
1860 }
1861 }
1862
Len Brown0f47c082017-01-27 00:50:45 -05001863 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001864 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1865 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001866 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001867 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1868 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001869 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001870 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1871 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001872 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001873 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1874 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001875 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001876 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1877 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001878
Len Brownbe0e54c2018-06-01 12:35:53 -04001879 if (DO_BIC(BIC_CPU_LPI))
1880 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1881 if (DO_BIC(BIC_SYS_LPI))
1882 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1883
Len Brown889facb2012-11-08 00:48:57 -05001884 if (do_rapl & RAPL_PKG) {
1885 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1886 return -13;
1887 p->energy_pkg = msr & 0xFFFFFFFF;
1888 }
Jacob Pan91484942016-06-16 09:48:20 -07001889 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001890 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1891 return -14;
1892 p->energy_cores = msr & 0xFFFFFFFF;
1893 }
1894 if (do_rapl & RAPL_DRAM) {
1895 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1896 return -15;
1897 p->energy_dram = msr & 0xFFFFFFFF;
1898 }
1899 if (do_rapl & RAPL_GFX) {
1900 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1901 return -16;
1902 p->energy_gfx = msr & 0xFFFFFFFF;
1903 }
1904 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1905 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1906 return -16;
1907 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1908 }
1909 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1910 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1911 return -16;
1912 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1913 }
Len Brown812db3f2017-02-10 00:25:41 -05001914 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001915 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1916 return -17;
1917 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1918 }
Len Brownfdf676e2016-02-27 01:28:12 -05001919
Len Brown812db3f2017-02-10 00:25:41 -05001920 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001921 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1922
Len Brown812db3f2017-02-10 00:25:41 -05001923 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001924 p->gfx_mhz = gfx_cur_mhz;
1925
Len Brown388e9c82016-12-22 23:57:55 -05001926 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001927 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001928 return -10;
1929 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001930done:
1931 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001932
Len Brown103a8fe2010-10-22 23:53:03 -04001933 return 0;
1934}
1935
Len Brownee7e38e2015-02-09 23:39:45 -05001936/*
1937 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1938 * If you change the values, note they are used both in comparisons
1939 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1940 */
1941
1942#define PCLUKN 0 /* Unknown */
1943#define PCLRSV 1 /* Reserved */
1944#define PCL__0 2 /* PC0 */
1945#define PCL__1 3 /* PC1 */
1946#define PCL__2 4 /* PC2 */
1947#define PCL__3 5 /* PC3 */
1948#define PCL__4 6 /* PC4 */
1949#define PCL__6 7 /* PC6 */
1950#define PCL_6N 8 /* PC6 No Retention */
1951#define PCL_6R 9 /* PC6 Retention */
1952#define PCL__7 10 /* PC7 */
1953#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001954#define PCL__8 12 /* PC8 */
1955#define PCL__9 13 /* PC9 */
1956#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001957
1958int pkg_cstate_limit = PCLUKN;
1959char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001960 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001961
Len Browne9257f52015-04-01 21:02:57 -04001962int 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};
1963int 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};
1964int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brown0539ba112017-02-10 00:27:20 -05001965int 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 -05001966int 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 -04001967int 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 -04001968int 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 +03001969int 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 -05001970
Len Browna2b7b742015-09-26 00:12:38 -04001971
1972static void
1973calculate_tsc_tweak()
1974{
Len Browna2b7b742015-09-26 00:12:38 -04001975 tsc_tweak = base_hz / tsc_hz;
1976}
1977
Len Brownfcd17212015-03-23 20:29:09 -04001978static void
1979dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001980{
1981 unsigned long long msr;
1982 unsigned int ratio;
1983
Len Brownec0adc52015-11-12 02:42:31 -05001984 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001985
Len Brownb7d8c142016-02-13 23:36:17 -05001986 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001987
Len Brown103a8fe2010-10-22 23:53:03 -04001988 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001989 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001990 ratio, bclk, ratio * bclk);
1991
1992 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001993 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001994 ratio, bclk, ratio * bclk);
1995
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001996 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001997 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001998 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001999
Len Brownfcd17212015-03-23 20:29:09 -04002000 return;
2001}
2002
2003static void
2004dump_hsw_turbo_ratio_limits(void)
2005{
2006 unsigned long long msr;
2007 unsigned int ratio;
2008
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002009 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002010
Len Brownb7d8c142016-02-13 23:36:17 -05002011 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002012
2013 ratio = (msr >> 8) & 0xFF;
2014 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002015 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04002016 ratio, bclk, ratio * bclk);
2017
2018 ratio = (msr >> 0) & 0xFF;
2019 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002020 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04002021 ratio, bclk, ratio * bclk);
2022 return;
2023}
2024
2025static void
2026dump_ivt_turbo_ratio_limits(void)
2027{
2028 unsigned long long msr;
2029 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04002030
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002031 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04002032
Len Brownb7d8c142016-02-13 23:36:17 -05002033 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04002034
2035 ratio = (msr >> 56) & 0xFF;
2036 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002037 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04002038 ratio, bclk, ratio * bclk);
2039
2040 ratio = (msr >> 48) & 0xFF;
2041 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002042 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04002043 ratio, bclk, ratio * bclk);
2044
2045 ratio = (msr >> 40) & 0xFF;
2046 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002047 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04002048 ratio, bclk, ratio * bclk);
2049
2050 ratio = (msr >> 32) & 0xFF;
2051 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002052 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04002053 ratio, bclk, ratio * bclk);
2054
2055 ratio = (msr >> 24) & 0xFF;
2056 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002057 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04002058 ratio, bclk, ratio * bclk);
2059
2060 ratio = (msr >> 16) & 0xFF;
2061 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002062 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04002063 ratio, bclk, ratio * bclk);
2064
2065 ratio = (msr >> 8) & 0xFF;
2066 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002067 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04002068 ratio, bclk, ratio * bclk);
2069
2070 ratio = (msr >> 0) & 0xFF;
2071 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05002072 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04002073 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04002074 return;
2075}
Len Brown31e07522017-01-31 23:07:49 -05002076int has_turbo_ratio_group_limits(int family, int model)
2077{
2078
2079 if (!genuine_intel)
2080 return 0;
2081
2082 switch (model) {
2083 case INTEL_FAM6_ATOM_GOLDMONT:
2084 case INTEL_FAM6_SKYLAKE_X:
2085 case INTEL_FAM6_ATOM_DENVERTON:
2086 return 1;
2087 }
2088 return 0;
2089}
Len Brown6574a5d2012-09-21 00:01:31 -04002090
Len Brownfcd17212015-03-23 20:29:09 -04002091static void
Len Brown31e07522017-01-31 23:07:49 -05002092dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04002093{
Len Brown31e07522017-01-31 23:07:49 -05002094 unsigned long long msr, core_counts;
2095 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04002096
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002097 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002098 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04002099
Len Brown31e07522017-01-31 23:07:49 -05002100 if (has_turbo_ratio_group_limits(family, model)) {
2101 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
2102 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
2103 } else {
2104 core_counts = 0x0807060504030201;
2105 }
2106
Len Brown6574a5d2012-09-21 00:01:31 -04002107 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002108 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002109 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002110 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2111 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002112
2113 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002114 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002115 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002116 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2117 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002118
2119 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002120 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002121 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002122 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2123 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002124
2125 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002126 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002127 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002128 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2129 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002130
Len Brown103a8fe2010-10-22 23:53:03 -04002131 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002132 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002133 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002134 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2135 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002136
2137 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002138 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002139 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002140 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2141 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002142
2143 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002144 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002145 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002146 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2147 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002148
2149 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002150 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002151 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002152 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2153 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002154 return;
2155}
Len Brown3a9a9412014-08-15 02:39:52 -04002156
Len Brownfcd17212015-03-23 20:29:09 -04002157static void
Len Brown0f7887c2017-01-12 23:49:18 -05002158dump_atom_turbo_ratio_limits(void)
2159{
2160 unsigned long long msr;
2161 unsigned int ratio;
2162
2163 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2164 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2165
2166 ratio = (msr >> 0) & 0x3F;
2167 if (ratio)
2168 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2169 ratio, bclk, ratio * bclk);
2170
2171 ratio = (msr >> 8) & 0x3F;
2172 if (ratio)
2173 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2174 ratio, bclk, ratio * bclk);
2175
2176 ratio = (msr >> 16) & 0x3F;
2177 if (ratio)
2178 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2179 ratio, bclk, ratio * bclk);
2180
2181 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2182 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2183
2184 ratio = (msr >> 24) & 0x3F;
2185 if (ratio)
2186 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2187 ratio, bclk, ratio * bclk);
2188
2189 ratio = (msr >> 16) & 0x3F;
2190 if (ratio)
2191 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2192 ratio, bclk, ratio * bclk);
2193
2194 ratio = (msr >> 8) & 0x3F;
2195 if (ratio)
2196 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2197 ratio, bclk, ratio * bclk);
2198
2199 ratio = (msr >> 0) & 0x3F;
2200 if (ratio)
2201 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2202 ratio, bclk, ratio * bclk);
2203}
2204
2205static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002206dump_knl_turbo_ratio_limits(void)
2207{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002208 const unsigned int buckets_no = 7;
2209
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002210 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002211 int delta_cores, delta_ratio;
2212 int i, b_nr;
2213 unsigned int cores[buckets_no];
2214 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002215
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002216 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002217
Len Brownb7d8c142016-02-13 23:36:17 -05002218 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002219 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002220
2221 /**
2222 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002223 * [0] -- Reserved
2224 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002225 * [15:8] -- Base value of freq ratio of bucket 1.
2226 * [20:16] -- +ve delta of number of active cores of bucket 2.
2227 * i.e. active cores of bucket 2 =
2228 * active cores of bucket 1 + delta
2229 * [23:21] -- Negative delta of freq ratio of bucket 2.
2230 * i.e. freq ratio of bucket 2 =
2231 * freq ratio of bucket 1 - delta
2232 * [28:24]-- +ve delta of number of active cores of bucket 3.
2233 * [31:29]-- -ve delta of freq ratio of bucket 3.
2234 * [36:32]-- +ve delta of number of active cores of bucket 4.
2235 * [39:37]-- -ve delta of freq ratio of bucket 4.
2236 * [44:40]-- +ve delta of number of active cores of bucket 5.
2237 * [47:45]-- -ve delta of freq ratio of bucket 5.
2238 * [52:48]-- +ve delta of number of active cores of bucket 6.
2239 * [55:53]-- -ve delta of freq ratio of bucket 6.
2240 * [60:56]-- +ve delta of number of active cores of bucket 7.
2241 * [63:61]-- -ve delta of freq ratio of bucket 7.
2242 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002243
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002244 b_nr = 0;
2245 cores[b_nr] = (msr & 0xFF) >> 1;
2246 ratio[b_nr] = (msr >> 8) & 0xFF;
2247
2248 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002249 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002250 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002251
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002252 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2253 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2254 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002255 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002256
2257 for (i = buckets_no - 1; i >= 0; i--)
2258 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002259 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002260 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002261 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002262}
2263
2264static void
Len Brownfcd17212015-03-23 20:29:09 -04002265dump_nhm_cst_cfg(void)
2266{
2267 unsigned long long msr;
2268
Len Brown1df2e552017-01-07 23:24:57 -05002269 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002270
Len Brown1df2e552017-01-07 23:24:57 -05002271 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002272
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002273 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002274 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2275 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2276 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2277 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2278 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002279 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002280 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002281
2282#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2283 if (has_automatic_cstate_conversion) {
2284 fprintf(outf, ", automatic c-state conversion=%s",
2285 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2286 }
2287
2288 fprintf(outf, ")\n");
2289
Len Brownfcd17212015-03-23 20:29:09 -04002290 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002291}
2292
Len Brown6fb31432015-06-17 16:23:45 -04002293static void
2294dump_config_tdp(void)
2295{
2296 unsigned long long msr;
2297
2298 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002299 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002300 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002301
2302 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002303 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002304 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002305 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2306 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2307 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2308 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002309 }
Len Brownb7d8c142016-02-13 23:36:17 -05002310 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002311
2312 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002313 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002314 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002315 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2316 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2317 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2318 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002319 }
Len Brownb7d8c142016-02-13 23:36:17 -05002320 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002321
2322 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002323 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002324 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002325 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2326 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2327 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002328
Len Brown6fb31432015-06-17 16:23:45 -04002329 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002330 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002331 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002332 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2333 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002334}
Len Brown5a634262016-04-06 17:15:55 -04002335
2336unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2337
2338void print_irtl(void)
2339{
2340 unsigned long long msr;
2341
2342 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2343 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2344 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2345 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2346
2347 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2348 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2349 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2350 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2351
2352 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2353 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2354 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2355 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2356
2357 if (!do_irtl_hsw)
2358 return;
2359
2360 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2361 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2362 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2363 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2364
2365 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2366 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2367 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2368 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2369
2370 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2371 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2372 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2373 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2374
2375}
Len Brown36229892016-02-26 20:51:02 -05002376void free_fd_percpu(void)
2377{
2378 int i;
2379
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002380 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002381 if (fd_percpu[i] != 0)
2382 close(fd_percpu[i]);
2383 }
2384
2385 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002386}
2387
Len Brownc98d5d92012-06-04 00:56:40 -04002388void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002389{
Len Brown0e2d8f02018-06-01 22:08:58 -04002390 int i;
2391
Len Brownc98d5d92012-06-04 00:56:40 -04002392 CPU_FREE(cpu_present_set);
2393 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002394 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002395
Len Brownc98d5d92012-06-04 00:56:40 -04002396 CPU_FREE(cpu_affinity_set);
2397 cpu_affinity_set = NULL;
2398 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002399
Len Brownc98d5d92012-06-04 00:56:40 -04002400 free(thread_even);
2401 free(core_even);
2402 free(package_even);
2403
2404 thread_even = NULL;
2405 core_even = NULL;
2406 package_even = NULL;
2407
2408 free(thread_odd);
2409 free(core_odd);
2410 free(package_odd);
2411
2412 thread_odd = NULL;
2413 core_odd = NULL;
2414 package_odd = NULL;
2415
2416 free(output_buffer);
2417 output_buffer = NULL;
2418 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002419
2420 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002421
2422 free(irq_column_2_cpu);
2423 free(irqs_per_cpu);
Len Brown0e2d8f02018-06-01 22:08:58 -04002424
2425 for (i = 0; i <= topo.max_cpu_num; ++i) {
2426 if (cpus[i].put_ids)
2427 CPU_FREE(cpus[i].put_ids);
2428 }
2429 free(cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002430}
2431
Josh Triplett57a42a32013-08-20 17:20:17 -07002432
2433/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002434 * Parse a file containing a single int.
2435 */
2436int parse_int_file(const char *fmt, ...)
2437{
2438 va_list args;
2439 char path[PATH_MAX];
2440 FILE *filep;
2441 int value;
2442
2443 va_start(args, fmt);
2444 vsnprintf(path, sizeof(path), fmt, args);
2445 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002446 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002447 if (fscanf(filep, "%d", &value) != 1)
2448 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002449 fclose(filep);
2450 return value;
2451}
2452
2453/*
Len Brownc98d5d92012-06-04 00:56:40 -04002454 * cpu_is_first_core_in_package(cpu)
2455 * return 1 if given CPU is 1st core in package
2456 */
2457int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002458{
Josh Triplett95aebc42013-08-20 17:20:16 -07002459 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002460}
2461
2462int get_physical_package_id(int cpu)
2463{
Josh Triplett95aebc42013-08-20 17:20:16 -07002464 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002465}
2466
2467int get_core_id(int cpu)
2468{
Josh Triplett95aebc42013-08-20 17:20:16 -07002469 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002470}
2471
Prarit Bhargavaef605742018-06-01 10:04:30 -04002472void set_node_data(void)
2473{
2474 char path[80];
2475 FILE *filep;
2476 int pkg, node, cpu;
2477
2478 struct pkg_node_info {
2479 int count;
2480 int min;
2481 } *pni;
2482
2483 pni = calloc(topo.num_packages, sizeof(struct pkg_node_info));
2484 if (!pni)
2485 err(1, "calloc pkg_node_count");
2486
2487 for (pkg = 0; pkg < topo.num_packages; pkg++)
2488 pni[pkg].min = topo.num_cpus;
2489
2490 for (node = 0; node <= topo.max_node_num; node++) {
2491 /* find the "first" cpu in the node */
2492 sprintf(path, "/sys/bus/node/devices/node%d/cpulist", node);
2493 filep = fopen(path, "r");
2494 if (!filep)
2495 continue;
2496 fscanf(filep, "%d", &cpu);
2497 fclose(filep);
2498
2499 pkg = cpus[cpu].physical_package_id;
2500 pni[pkg].count++;
2501
2502 if (node < pni[pkg].min)
2503 pni[pkg].min = node;
2504 }
2505
2506 for (pkg = 0; pkg < topo.num_packages; pkg++)
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04002507 if (pni[pkg].count > topo.nodes_per_pkg)
2508 topo.nodes_per_pkg = pni[0].count;
Prarit Bhargavaef605742018-06-01 10:04:30 -04002509
Nathan Ciobanu42dd4522018-06-08 15:15:12 -07002510 /* Fake 1 node per pkg for machines that don't
2511 * expose nodes and thus avoid -nan results
2512 */
2513 if (topo.nodes_per_pkg == 0)
2514 topo.nodes_per_pkg = 1;
2515
Prarit Bhargavaef605742018-06-01 10:04:30 -04002516 for (cpu = 0; cpu < topo.num_cpus; cpu++) {
2517 pkg = cpus[cpu].physical_package_id;
2518 node = cpus[cpu].physical_node_id;
2519 cpus[cpu].logical_node_id = node - pni[pkg].min;
2520 }
2521 free(pni);
2522
2523}
2524
2525int get_physical_node_id(struct cpu_topology *thiscpu)
Len Brownc98d5d92012-06-04 00:56:40 -04002526{
2527 char path[80];
2528 FILE *filep;
Len Brown0e2d8f02018-06-01 22:08:58 -04002529 int i;
2530 int cpu = thiscpu->logical_cpu_id;
Len Brownc98d5d92012-06-04 00:56:40 -04002531
Len Brown0e2d8f02018-06-01 22:08:58 -04002532 for (i = 0; i <= topo.max_cpu_num; i++) {
2533 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2534 cpu, i);
2535 filep = fopen(path, "r");
2536 if (!filep)
2537 continue;
2538 fclose(filep);
2539 return i;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002540 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002541 return -1;
2542}
Len Brownc98d5d92012-06-04 00:56:40 -04002543
Len Brown0e2d8f02018-06-01 22:08:58 -04002544int get_thread_siblings(struct cpu_topology *thiscpu)
2545{
2546 char path[80], character;
2547 FILE *filep;
2548 unsigned long map;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002549 int so, shift, sib_core;
Len Brown0e2d8f02018-06-01 22:08:58 -04002550 int cpu = thiscpu->logical_cpu_id;
2551 int offset = topo.max_cpu_num + 1;
2552 size_t size;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002553 int thread_id = 0;
Len Brown0e2d8f02018-06-01 22:08:58 -04002554
2555 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002556 if (thiscpu->thread_id < 0)
2557 thiscpu->thread_id = thread_id++;
Len Brown0e2d8f02018-06-01 22:08:58 -04002558 if (!thiscpu->put_ids)
2559 return -1;
2560
2561 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2562 CPU_ZERO_S(size, thiscpu->put_ids);
2563
2564 sprintf(path,
2565 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2566 filep = fopen_or_die(path, "r");
2567 do {
2568 offset -= BITMASK_SIZE;
2569 fscanf(filep, "%lx%c", &map, &character);
2570 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2571 if ((map >> shift) & 0x1) {
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002572 so = shift + offset;
2573 sib_core = get_core_id(so);
2574 if (sib_core == thiscpu->physical_core_id) {
2575 CPU_SET_S(so, size, thiscpu->put_ids);
2576 if ((so != cpu) &&
2577 (cpus[so].thread_id < 0))
2578 cpus[so].thread_id =
2579 thread_id++;
2580 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002581 }
2582 }
2583 } while (!strncmp(&character, ",", 1));
Len Brownc98d5d92012-06-04 00:56:40 -04002584 fclose(filep);
Len Brown0e2d8f02018-06-01 22:08:58 -04002585
2586 return CPU_COUNT_S(size, thiscpu->put_ids);
Len Brownc98d5d92012-06-04 00:56:40 -04002587}
2588
Len Brown103a8fe2010-10-22 23:53:03 -04002589/*
Len Brownc98d5d92012-06-04 00:56:40 -04002590 * run func(thread, core, package) in topology order
2591 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002592 */
2593
Len Brownc98d5d92012-06-04 00:56:40 -04002594int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2595 struct pkg_data *, struct thread_data *, struct core_data *,
2596 struct pkg_data *), struct thread_data *thread_base,
2597 struct core_data *core_base, struct pkg_data *pkg_base,
2598 struct thread_data *thread_base2, struct core_data *core_base2,
2599 struct pkg_data *pkg_base2)
2600{
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002601 int retval, pkg_no, node_no, core_no, thread_no;
Len Brownc98d5d92012-06-04 00:56:40 -04002602
2603 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002604 for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
2605 for (core_no = 0; core_no < topo.cores_per_node;
2606 ++core_no) {
2607 for (thread_no = 0; thread_no <
2608 topo.threads_per_core; ++thread_no) {
2609 struct thread_data *t, *t2;
2610 struct core_data *c, *c2;
2611 struct pkg_data *p, *p2;
Len Brownc98d5d92012-06-04 00:56:40 -04002612
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002613 t = GET_THREAD(thread_base, thread_no,
2614 core_no, node_no,
2615 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002616
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002617 if (cpu_is_not_present(t->cpu_id))
2618 continue;
Len Brownc98d5d92012-06-04 00:56:40 -04002619
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002620 t2 = GET_THREAD(thread_base2, thread_no,
2621 core_no, node_no,
2622 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002623
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002624 c = GET_CORE(core_base, core_no,
2625 node_no, pkg_no);
2626 c2 = GET_CORE(core_base2, core_no,
2627 node_no,
2628 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002629
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002630 p = GET_PKG(pkg_base, pkg_no);
2631 p2 = GET_PKG(pkg_base2, pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002632
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002633 retval = func(t, c, p, t2, c2, p2);
2634 if (retval)
2635 return retval;
2636 }
Len Brownc98d5d92012-06-04 00:56:40 -04002637 }
2638 }
2639 }
2640 return 0;
2641}
2642
2643/*
2644 * run func(cpu) on every cpu in /proc/stat
2645 * return max_cpu number
2646 */
2647int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002648{
2649 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002650 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002651 int retval;
2652
Josh Triplett57a42a32013-08-20 17:20:17 -07002653 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002654
2655 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002656 if (retval != 0)
2657 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002658
Len Brownc98d5d92012-06-04 00:56:40 -04002659 while (1) {
2660 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 -04002661 if (retval != 1)
2662 break;
2663
Len Brownc98d5d92012-06-04 00:56:40 -04002664 retval = func(cpu_num);
2665 if (retval) {
2666 fclose(fp);
2667 return(retval);
2668 }
Len Brown103a8fe2010-10-22 23:53:03 -04002669 }
2670 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002671 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002672}
2673
2674void re_initialize(void)
2675{
Len Brownc98d5d92012-06-04 00:56:40 -04002676 free_all_buffers();
2677 setup_all_buffers();
2678 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002679}
2680
Prarit Bhargava843c5792018-06-01 10:04:28 -04002681void set_max_cpu_num(void)
2682{
2683 FILE *filep;
2684 unsigned long dummy;
2685
2686 topo.max_cpu_num = 0;
2687 filep = fopen_or_die(
2688 "/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2689 "r");
2690 while (fscanf(filep, "%lx,", &dummy) == 1)
Len Brown0e2d8f02018-06-01 22:08:58 -04002691 topo.max_cpu_num += BITMASK_SIZE;
Prarit Bhargava843c5792018-06-01 10:04:28 -04002692 fclose(filep);
2693 topo.max_cpu_num--; /* 0 based */
2694}
Len Brownc98d5d92012-06-04 00:56:40 -04002695
Len Brown103a8fe2010-10-22 23:53:03 -04002696/*
Len Brownc98d5d92012-06-04 00:56:40 -04002697 * count_cpus()
2698 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002699 */
Len Brownc98d5d92012-06-04 00:56:40 -04002700int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002701{
Prarit Bhargava843c5792018-06-01 10:04:28 -04002702 topo.num_cpus++;
Len Brownc98d5d92012-06-04 00:56:40 -04002703 return 0;
2704}
2705int mark_cpu_present(int cpu)
2706{
2707 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002708 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002709}
2710
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002711int init_thread_id(int cpu)
2712{
2713 cpus[cpu].thread_id = -1;
2714 return 0;
2715}
2716
Len Brown562a2d32016-02-26 23:48:05 -05002717/*
2718 * snapshot_proc_interrupts()
2719 *
2720 * read and record summary of /proc/interrupts
2721 *
2722 * return 1 if config change requires a restart, else return 0
2723 */
2724int snapshot_proc_interrupts(void)
2725{
2726 static FILE *fp;
2727 int column, retval;
2728
2729 if (fp == NULL)
2730 fp = fopen_or_die("/proc/interrupts", "r");
2731 else
2732 rewind(fp);
2733
2734 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2735 for (column = 0; column < topo.num_cpus; ++column) {
2736 int cpu_number;
2737
2738 retval = fscanf(fp, " CPU%d", &cpu_number);
2739 if (retval != 1)
2740 break;
2741
2742 if (cpu_number > topo.max_cpu_num) {
2743 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2744 return 1;
2745 }
2746
2747 irq_column_2_cpu[column] = cpu_number;
2748 irqs_per_cpu[cpu_number] = 0;
2749 }
2750
2751 /* read /proc/interrupt count lines and sum up irqs per cpu */
2752 while (1) {
2753 int column;
2754 char buf[64];
2755
2756 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2757 if (retval != 1)
2758 break;
2759
2760 /* read the count per cpu */
2761 for (column = 0; column < topo.num_cpus; ++column) {
2762
2763 int cpu_number, irq_count;
2764
2765 retval = fscanf(fp, " %d", &irq_count);
2766 if (retval != 1)
2767 break;
2768
2769 cpu_number = irq_column_2_cpu[column];
2770 irqs_per_cpu[cpu_number] += irq_count;
2771
2772 }
2773
2774 while (getc(fp) != '\n')
2775 ; /* flush interrupt description */
2776
2777 }
2778 return 0;
2779}
Len Brown27d47352016-02-27 00:37:54 -05002780/*
Len Brownfdf676e2016-02-27 01:28:12 -05002781 * snapshot_gfx_rc6_ms()
2782 *
2783 * record snapshot of
2784 * /sys/class/drm/card0/power/rc6_residency_ms
2785 *
2786 * return 1 if config change requires a restart, else return 0
2787 */
2788int snapshot_gfx_rc6_ms(void)
2789{
2790 FILE *fp;
2791 int retval;
2792
2793 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2794
2795 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2796 if (retval != 1)
2797 err(1, "GFX rc6");
2798
2799 fclose(fp);
2800
2801 return 0;
2802}
2803/*
Len Brown27d47352016-02-27 00:37:54 -05002804 * snapshot_gfx_mhz()
2805 *
2806 * record snapshot of
2807 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2808 *
2809 * return 1 if config change requires a restart, else return 0
2810 */
2811int snapshot_gfx_mhz(void)
2812{
2813 static FILE *fp;
2814 int retval;
2815
2816 if (fp == NULL)
2817 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002818 else {
Len Brown27d47352016-02-27 00:37:54 -05002819 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002820 fflush(fp);
2821 }
Len Brown27d47352016-02-27 00:37:54 -05002822
2823 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2824 if (retval != 1)
2825 err(1, "GFX MHz");
2826
2827 return 0;
2828}
Len Brown562a2d32016-02-26 23:48:05 -05002829
2830/*
Len Brownbe0e54c2018-06-01 12:35:53 -04002831 * snapshot_cpu_lpi()
2832 *
2833 * record snapshot of
2834 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2835 *
2836 * return 1 if config change requires a restart, else return 0
2837 */
2838int snapshot_cpu_lpi_us(void)
2839{
2840 FILE *fp;
2841 int retval;
2842
2843 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2844
2845 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2846 if (retval != 1)
2847 err(1, "CPU LPI");
2848
2849 fclose(fp);
2850
2851 return 0;
2852}
2853/*
2854 * snapshot_sys_lpi()
2855 *
2856 * record snapshot of
2857 * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2858 *
2859 * return 1 if config change requires a restart, else return 0
2860 */
2861int snapshot_sys_lpi_us(void)
2862{
2863 FILE *fp;
2864 int retval;
2865
2866 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2867
2868 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2869 if (retval != 1)
2870 err(1, "SYS LPI");
2871
2872 fclose(fp);
2873
2874 return 0;
2875}
2876/*
Len Brown562a2d32016-02-26 23:48:05 -05002877 * snapshot /proc and /sys files
2878 *
2879 * return 1 if configuration restart needed, else return 0
2880 */
2881int snapshot_proc_sysfs_files(void)
2882{
Len Brown218f0e82017-02-14 22:07:52 -05002883 if (DO_BIC(BIC_IRQ))
2884 if (snapshot_proc_interrupts())
2885 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002886
Len Brown812db3f2017-02-10 00:25:41 -05002887 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002888 snapshot_gfx_rc6_ms();
2889
Len Brown812db3f2017-02-10 00:25:41 -05002890 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002891 snapshot_gfx_mhz();
2892
Len Brownbe0e54c2018-06-01 12:35:53 -04002893 if (DO_BIC(BIC_CPU_LPI))
2894 snapshot_cpu_lpi_us();
2895
2896 if (DO_BIC(BIC_SYS_LPI))
2897 snapshot_sys_lpi_us();
2898
Len Brown562a2d32016-02-26 23:48:05 -05002899 return 0;
2900}
2901
Len Brown8aa2ed02017-07-15 14:57:37 -04002902int exit_requested;
2903
2904static void signal_handler (int signal)
2905{
2906 switch (signal) {
2907 case SIGINT:
2908 exit_requested = 1;
2909 if (debug)
2910 fprintf(stderr, " SIGINT\n");
2911 break;
Len Brown07211962017-07-15 15:51:26 -04002912 case SIGUSR1:
2913 if (debug > 1)
2914 fprintf(stderr, "SIGUSR1\n");
2915 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002916 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002917 /* make sure this manually-invoked interval is at least 1ms long */
2918 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002919}
2920
2921void setup_signal_handler(void)
2922{
2923 struct sigaction sa;
2924
2925 memset(&sa, 0, sizeof(sa));
2926
2927 sa.sa_handler = &signal_handler;
2928
2929 if (sigaction(SIGINT, &sa, NULL) < 0)
2930 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002931 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2932 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002933}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002934
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002935void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002936{
2937 struct timeval select_timeout;
2938 fd_set readfds;
2939 int retval;
2940
2941 FD_ZERO(&readfds);
2942 FD_SET(0, &readfds);
2943
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002944 if (!isatty(fileno(stdin))) {
2945 nanosleep(&interval_ts, NULL);
2946 return;
2947 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002948
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002949 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002950 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2951
2952 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002953 switch (getc(stdin)) {
2954 case 'q':
2955 exit_requested = 1;
2956 break;
2957 }
2958 /* make sure this manually-invoked interval is at least 1ms long */
2959 nanosleep(&one_msec, NULL);
2960 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002961}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002962
Len Brown4c2122d2018-06-06 17:44:48 -04002963
Len Brown103a8fe2010-10-22 23:53:03 -04002964void turbostat_loop()
2965{
Len Brownc98d5d92012-06-04 00:56:40 -04002966 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002967 int restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002968 int done_iters = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002969
Len Brown8aa2ed02017-07-15 14:57:37 -04002970 setup_signal_handler();
2971
Len Brown103a8fe2010-10-22 23:53:03 -04002972restart:
Len Browne52966c2012-11-08 22:38:05 -05002973 restarted++;
2974
Len Brown562a2d32016-02-26 23:48:05 -05002975 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002976 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brown4c2122d2018-06-06 17:44:48 -04002977 first_counter_read = 0;
Len Brownd91bb172012-11-01 00:08:19 -04002978 if (retval < -1) {
2979 exit(retval);
2980 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002981 if (restarted > 1) {
2982 exit(retval);
2983 }
Len Brownc98d5d92012-06-04 00:56:40 -04002984 re_initialize();
2985 goto restart;
2986 }
Len Browne52966c2012-11-08 22:38:05 -05002987 restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002988 done_iters = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002989 gettimeofday(&tv_even, (struct timezone *)NULL);
2990
2991 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002992 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002993 re_initialize();
2994 goto restart;
2995 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002996 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002997 if (snapshot_proc_sysfs_files())
2998 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002999 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04003000 if (retval < -1) {
3001 exit(retval);
3002 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04003003 re_initialize();
3004 goto restart;
3005 }
Len Brown103a8fe2010-10-22 23:53:03 -04003006 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04003007 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04003008 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3009 re_initialize();
3010 goto restart;
3011 }
Len Brownc98d5d92012-06-04 00:56:40 -04003012 compute_average(EVEN_COUNTERS);
3013 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05003014 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04003015 if (exit_requested)
3016 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08003017 if (num_iterations && ++done_iters >= num_iterations)
3018 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04003019 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05003020 if (snapshot_proc_sysfs_files())
3021 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04003022 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04003023 if (retval < -1) {
3024 exit(retval);
3025 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04003026 re_initialize();
3027 goto restart;
3028 }
Len Brown103a8fe2010-10-22 23:53:03 -04003029 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04003030 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04003031 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3032 re_initialize();
3033 goto restart;
3034 }
Len Brownc98d5d92012-06-04 00:56:40 -04003035 compute_average(ODD_COUNTERS);
3036 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05003037 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04003038 if (exit_requested)
3039 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08003040 if (num_iterations && ++done_iters >= num_iterations)
3041 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003042 }
3043}
3044
3045void check_dev_msr()
3046{
3047 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003048 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04003049
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003050 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3051 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04003052 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3053 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04003054}
3055
Len Brown98481e72014-08-15 00:36:50 -04003056void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04003057{
Len Brown98481e72014-08-15 00:36:50 -04003058 struct __user_cap_header_struct cap_header_data;
3059 cap_user_header_t cap_header = &cap_header_data;
3060 struct __user_cap_data_struct cap_data_data;
3061 cap_user_data_t cap_data = &cap_data_data;
3062 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
3063 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003064 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04003065
3066 /* check for CAP_SYS_RAWIO */
3067 cap_header->pid = getpid();
3068 cap_header->version = _LINUX_CAPABILITY_VERSION;
3069 if (capget(cap_header, cap_data) < 0)
3070 err(-6, "capget(2) failed");
3071
3072 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
3073 do_exit++;
3074 warnx("capget(CAP_SYS_RAWIO) failed,"
3075 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3076 }
3077
3078 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003079 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3080 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04003081 do_exit++;
3082 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
3083 }
3084
3085 /* if all else fails, thell them to be root */
3086 if (do_exit)
3087 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05003088 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04003089
3090 if (do_exit)
3091 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04003092}
3093
Len Brownd7899442015-01-23 00:12:33 -05003094/*
3095 * NHM adds support for additional MSRs:
3096 *
3097 * MSR_SMI_COUNT 0x00000034
3098 *
Len Brownec0adc52015-11-12 02:42:31 -05003099 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05003100 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05003101 *
Len Browncf4cbe52017-01-01 13:08:33 -05003102 * MSR_MISC_PWR_MGMT 0x000001aa
3103 *
Len Brownd7899442015-01-23 00:12:33 -05003104 * MSR_PKG_C3_RESIDENCY 0x000003f8
3105 * MSR_PKG_C6_RESIDENCY 0x000003f9
3106 * MSR_CORE_C3_RESIDENCY 0x000003fc
3107 * MSR_CORE_C6_RESIDENCY 0x000003fd
3108 *
Len Brownee7e38e2015-02-09 23:39:45 -05003109 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05003110 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05003111 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05003112 */
Len Brownee7e38e2015-02-09 23:39:45 -05003113int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003114{
Len Brownee7e38e2015-02-09 23:39:45 -05003115 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04003116 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05003117 int *pkg_cstate_limits;
3118
Len Brown103a8fe2010-10-22 23:53:03 -04003119 if (!genuine_intel)
3120 return 0;
3121
3122 if (family != 6)
3123 return 0;
3124
Len Brown21ed5572015-10-19 22:37:40 -04003125 bclk = discover_bclk(family, model);
3126
Len Brown103a8fe2010-10-22 23:53:03 -04003127 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003128 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3129 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04003130 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce69e2016-06-16 23:22:37 -04003131 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
3132 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
3133 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3134 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05003135 pkg_cstate_limits = nhm_pkg_cstate_limits;
3136 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003137 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3138 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3139 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3140 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05003141 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003142 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003143 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003144 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3145 case INTEL_FAM6_HASWELL_X: /* HSX */
3146 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3147 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3148 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3149 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3150 case INTEL_FAM6_BROADWELL_X: /* BDX */
3151 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3152 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3153 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3154 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3155 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003156 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brownee7e38e2015-02-09 23:39:45 -05003157 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003158 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003159 break;
Len Brownd8ebb442016-12-01 20:27:46 -05003160 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3161 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003162 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05003163 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003164 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05003165 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04003166 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05003167 pkg_cstate_limits = slv_pkg_cstate_limits;
3168 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003169 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05003170 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05003171 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003172 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003173 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05003174 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05003175 pkg_cstate_limits = phi_pkg_cstate_limits;
3176 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003177 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003178 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce69e2016-06-16 23:22:37 -04003179 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04003180 pkg_cstate_limits = bxt_pkg_cstate_limits;
3181 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003182 default:
3183 return 0;
3184 }
Len Brown1df2e552017-01-07 23:24:57 -05003185 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04003186 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05003187
Len Brownec0adc52015-11-12 02:42:31 -05003188 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04003189 base_ratio = (msr >> 8) & 0xFF;
3190
3191 base_hz = base_ratio * bclk * 1000000;
3192 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003193 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04003194}
Len Brown0f7887c2017-01-12 23:49:18 -05003195/*
Len Brown495c76542017-02-08 02:41:51 -05003196 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05003197 *
3198 * MSR_CC6_DEMOTION_POLICY_CONFIG
3199 * MSR_MC6_DEMOTION_POLICY_CONFIG
3200 */
3201
3202int has_slv_msrs(unsigned int family, unsigned int model)
3203{
3204 if (!genuine_intel)
3205 return 0;
3206
3207 switch (model) {
3208 case INTEL_FAM6_ATOM_SILVERMONT1:
3209 case INTEL_FAM6_ATOM_MERRIFIELD:
3210 case INTEL_FAM6_ATOM_MOOREFIELD:
3211 return 1;
3212 }
3213 return 0;
3214}
Len Brown7170a372017-01-27 02:13:27 -05003215int is_dnv(unsigned int family, unsigned int model)
3216{
3217
3218 if (!genuine_intel)
3219 return 0;
3220
3221 switch (model) {
3222 case INTEL_FAM6_ATOM_DENVERTON:
3223 return 1;
3224 }
3225 return 0;
3226}
Len Brownade0eba2017-02-10 01:56:47 -05003227int is_bdx(unsigned int family, unsigned int model)
3228{
3229
3230 if (!genuine_intel)
3231 return 0;
3232
3233 switch (model) {
3234 case INTEL_FAM6_BROADWELL_X:
3235 case INTEL_FAM6_BROADWELL_XEON_D:
3236 return 1;
3237 }
3238 return 0;
3239}
Len Brown34c761972017-01-27 02:36:41 -05003240int is_skx(unsigned int family, unsigned int model)
3241{
3242
3243 if (!genuine_intel)
3244 return 0;
3245
3246 switch (model) {
3247 case INTEL_FAM6_SKYLAKE_X:
3248 return 1;
3249 }
3250 return 0;
3251}
Len Brown0f7887c2017-01-12 23:49:18 -05003252
Len Brown31e07522017-01-31 23:07:49 -05003253int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05003254{
Len Brown0f7887c2017-01-12 23:49:18 -05003255 if (has_slv_msrs(family, model))
3256 return 0;
3257
Len Brownd7899442015-01-23 00:12:33 -05003258 switch (model) {
3259 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce69e2016-06-16 23:22:37 -04003260 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3261 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3262 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05003263 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05003264 return 0;
3265 default:
3266 return 1;
3267 }
3268}
Len Brown0f7887c2017-01-12 23:49:18 -05003269int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3270{
3271 if (has_slv_msrs(family, model))
3272 return 1;
3273
3274 return 0;
3275}
Len Brown6574a5d2012-09-21 00:01:31 -04003276int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3277{
3278 if (!genuine_intel)
3279 return 0;
3280
3281 if (family != 6)
3282 return 0;
3283
3284 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003285 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3286 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04003287 return 1;
3288 default:
3289 return 0;
3290 }
3291}
Len Brownfcd17212015-03-23 20:29:09 -04003292int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3293{
3294 if (!genuine_intel)
3295 return 0;
3296
3297 if (family != 6)
3298 return 0;
3299
3300 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003301 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003302 return 1;
3303 default:
3304 return 0;
3305 }
3306}
3307
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003308int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3309{
3310 if (!genuine_intel)
3311 return 0;
3312
3313 if (family != 6)
3314 return 0;
3315
3316 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003317 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003318 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003319 return 1;
3320 default:
3321 return 0;
3322 }
3323}
Len Brown31e07522017-01-31 23:07:49 -05003324int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3325{
3326 if (!genuine_intel)
3327 return 0;
3328
3329 if (family != 6)
3330 return 0;
3331
3332 switch (model) {
3333 case INTEL_FAM6_ATOM_GOLDMONT:
3334 case INTEL_FAM6_SKYLAKE_X:
3335 return 1;
3336 default:
3337 return 0;
3338 }
3339}
Len Brown6fb31432015-06-17 16:23:45 -04003340int has_config_tdp(unsigned int family, unsigned int model)
3341{
3342 if (!genuine_intel)
3343 return 0;
3344
3345 if (family != 6)
3346 return 0;
3347
3348 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003349 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3350 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3351 case INTEL_FAM6_HASWELL_X: /* HSX */
3352 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3353 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3354 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3355 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3356 case INTEL_FAM6_BROADWELL_X: /* BDX */
3357 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3358 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3359 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3360 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3361 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003362 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce69e2016-06-16 23:22:37 -04003363 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003364
Len Brown869ce69e2016-06-16 23:22:37 -04003365 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003366 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003367 return 1;
3368 default:
3369 return 0;
3370 }
3371}
3372
Len Brownfcd17212015-03-23 20:29:09 -04003373static void
Colin Ian King1b693172016-03-02 13:50:25 +00003374dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003375{
3376 if (!do_nhm_platform_info)
3377 return;
3378
3379 dump_nhm_platform_info();
3380
3381 if (has_hsw_turbo_ratio_limit(family, model))
3382 dump_hsw_turbo_ratio_limits();
3383
3384 if (has_ivt_turbo_ratio_limit(family, model))
3385 dump_ivt_turbo_ratio_limits();
3386
Len Brown31e07522017-01-31 23:07:49 -05003387 if (has_turbo_ratio_limit(family, model))
3388 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003389
Len Brown0f7887c2017-01-12 23:49:18 -05003390 if (has_atom_turbo_ratio_limit(family, model))
3391 dump_atom_turbo_ratio_limits();
3392
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003393 if (has_knl_turbo_ratio_limit(family, model))
3394 dump_knl_turbo_ratio_limits();
3395
Len Brown6fb31432015-06-17 16:23:45 -04003396 if (has_config_tdp(family, model))
3397 dump_config_tdp();
3398
Len Brownfcd17212015-03-23 20:29:09 -04003399 dump_nhm_cst_cfg();
3400}
3401
Len Brown41618e62017-02-09 18:25:22 -05003402static void
3403dump_sysfs_cstate_config(void)
3404{
3405 char path[64];
3406 char name_buf[16];
3407 char desc[64];
3408 FILE *input;
3409 int state;
3410 char *sp;
3411
3412 if (!DO_BIC(BIC_sysfs))
3413 return;
3414
3415 for (state = 0; state < 10; ++state) {
3416
3417 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3418 base_cpu, state);
3419 input = fopen(path, "r");
3420 if (input == NULL)
3421 continue;
3422 fgets(name_buf, sizeof(name_buf), input);
3423
3424 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3425 sp = strchr(name_buf, '-');
3426 if (!sp)
3427 sp = strchrnul(name_buf, '\n');
3428 *sp = '\0';
3429
3430 fclose(input);
3431
3432 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3433 base_cpu, state);
3434 input = fopen(path, "r");
3435 if (input == NULL)
3436 continue;
3437 fgets(desc, sizeof(desc), input);
3438
3439 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3440 fclose(input);
3441 }
3442}
Len Brown7293fcc2017-02-22 00:11:12 -05003443static void
3444dump_sysfs_pstate_config(void)
3445{
3446 char path[64];
3447 char driver_buf[64];
3448 char governor_buf[64];
3449 FILE *input;
3450 int turbo;
3451
3452 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3453 base_cpu);
3454 input = fopen(path, "r");
3455 if (input == NULL) {
3456 fprintf(stderr, "NSFOD %s\n", path);
3457 return;
3458 }
3459 fgets(driver_buf, sizeof(driver_buf), input);
3460 fclose(input);
3461
3462 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3463 base_cpu);
3464 input = fopen(path, "r");
3465 if (input == NULL) {
3466 fprintf(stderr, "NSFOD %s\n", path);
3467 return;
3468 }
3469 fgets(governor_buf, sizeof(governor_buf), input);
3470 fclose(input);
3471
3472 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3473 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3474
3475 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3476 input = fopen(path, "r");
3477 if (input != NULL) {
3478 fscanf(input, "%d", &turbo);
3479 fprintf(outf, "cpufreq boost: %d\n", turbo);
3480 fclose(input);
3481 }
3482
3483 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3484 input = fopen(path, "r");
3485 if (input != NULL) {
3486 fscanf(input, "%d", &turbo);
3487 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3488 fclose(input);
3489 }
3490}
Len Brown41618e62017-02-09 18:25:22 -05003491
Len Brown6574a5d2012-09-21 00:01:31 -04003492
Len Brown889facb2012-11-08 00:48:57 -05003493/*
3494 * print_epb()
3495 * Decode the ENERGY_PERF_BIAS MSR
3496 */
3497int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3498{
3499 unsigned long long msr;
3500 char *epb_string;
3501 int cpu;
3502
3503 if (!has_epb)
3504 return 0;
3505
3506 cpu = t->cpu_id;
3507
3508 /* EPB is per-package */
3509 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3510 return 0;
3511
3512 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003513 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003514 return -1;
3515 }
3516
3517 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3518 return 0;
3519
Len Browne9be7dd2015-05-26 12:19:37 -04003520 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003521 case ENERGY_PERF_BIAS_PERFORMANCE:
3522 epb_string = "performance";
3523 break;
3524 case ENERGY_PERF_BIAS_NORMAL:
3525 epb_string = "balanced";
3526 break;
3527 case ENERGY_PERF_BIAS_POWERSAVE:
3528 epb_string = "powersave";
3529 break;
3530 default:
3531 epb_string = "custom";
3532 break;
3533 }
Len Brownb7d8c142016-02-13 23:36:17 -05003534 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003535
3536 return 0;
3537}
Len Brown7f5c2582015-12-01 01:36:39 -05003538/*
3539 * print_hwp()
3540 * Decode the MSR_HWP_CAPABILITIES
3541 */
3542int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3543{
3544 unsigned long long msr;
3545 int cpu;
3546
3547 if (!has_hwp)
3548 return 0;
3549
3550 cpu = t->cpu_id;
3551
3552 /* MSR_HWP_CAPABILITIES is per-package */
3553 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3554 return 0;
3555
3556 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003557 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003558 return -1;
3559 }
3560
3561 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3562 return 0;
3563
Len Brownb7d8c142016-02-13 23:36:17 -05003564 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003565 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3566
3567 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3568 if ((msr & (1 << 0)) == 0)
3569 return 0;
3570
3571 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3572 return 0;
3573
Len Brownb7d8c142016-02-13 23:36:17 -05003574 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003575 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003576 cpu, msr,
3577 (unsigned int)HWP_HIGHEST_PERF(msr),
3578 (unsigned int)HWP_GUARANTEED_PERF(msr),
3579 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3580 (unsigned int)HWP_LOWEST_PERF(msr));
3581
3582 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3583 return 0;
3584
Len Brownb7d8c142016-02-13 23:36:17 -05003585 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003586 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003587 cpu, msr,
3588 (unsigned int)(((msr) >> 0) & 0xff),
3589 (unsigned int)(((msr) >> 8) & 0xff),
3590 (unsigned int)(((msr) >> 16) & 0xff),
3591 (unsigned int)(((msr) >> 24) & 0xff),
3592 (unsigned int)(((msr) >> 32) & 0xff3),
3593 (unsigned int)(((msr) >> 42) & 0x1));
3594
3595 if (has_hwp_pkg) {
3596 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3597 return 0;
3598
Len Brownb7d8c142016-02-13 23:36:17 -05003599 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003600 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003601 cpu, msr,
3602 (unsigned int)(((msr) >> 0) & 0xff),
3603 (unsigned int)(((msr) >> 8) & 0xff),
3604 (unsigned int)(((msr) >> 16) & 0xff),
3605 (unsigned int)(((msr) >> 24) & 0xff),
3606 (unsigned int)(((msr) >> 32) & 0xff3));
3607 }
3608 if (has_hwp_notify) {
3609 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3610 return 0;
3611
Len Brownb7d8c142016-02-13 23:36:17 -05003612 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003613 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3614 cpu, msr,
3615 ((msr) & 0x1) ? "EN" : "Dis",
3616 ((msr) & 0x2) ? "EN" : "Dis");
3617 }
3618 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3619 return 0;
3620
Len Brownb7d8c142016-02-13 23:36:17 -05003621 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003622 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3623 cpu, msr,
3624 ((msr) & 0x1) ? "" : "No-",
3625 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003626
3627 return 0;
3628}
3629
Len Brown3a9a9412014-08-15 02:39:52 -04003630/*
3631 * print_perf_limit()
3632 */
3633int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3634{
3635 unsigned long long msr;
3636 int cpu;
3637
3638 cpu = t->cpu_id;
3639
3640 /* per-package */
3641 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3642 return 0;
3643
3644 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003645 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003646 return -1;
3647 }
3648
3649 if (do_core_perf_limit_reasons) {
3650 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003651 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3652 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003653 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003654 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003655 (msr & 1 << 13) ? "Transitions, " : "",
3656 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3657 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3658 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3659 (msr & 1 << 9) ? "CorePwr, " : "",
3660 (msr & 1 << 8) ? "Amps, " : "",
3661 (msr & 1 << 6) ? "VR-Therm, " : "",
3662 (msr & 1 << 5) ? "Auto-HWP, " : "",
3663 (msr & 1 << 4) ? "Graphics, " : "",
3664 (msr & 1 << 2) ? "bit2, " : "",
3665 (msr & 1 << 1) ? "ThermStatus, " : "",
3666 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003667 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 -04003668 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003669 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003670 (msr & 1 << 29) ? "Transitions, " : "",
3671 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3672 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3673 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3674 (msr & 1 << 25) ? "CorePwr, " : "",
3675 (msr & 1 << 24) ? "Amps, " : "",
3676 (msr & 1 << 22) ? "VR-Therm, " : "",
3677 (msr & 1 << 21) ? "Auto-HWP, " : "",
3678 (msr & 1 << 20) ? "Graphics, " : "",
3679 (msr & 1 << 18) ? "bit18, " : "",
3680 (msr & 1 << 17) ? "ThermStatus, " : "",
3681 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003682
3683 }
3684 if (do_gfx_perf_limit_reasons) {
3685 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003686 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3687 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003688 (msr & 1 << 0) ? "PROCHOT, " : "",
3689 (msr & 1 << 1) ? "ThermStatus, " : "",
3690 (msr & 1 << 4) ? "Graphics, " : "",
3691 (msr & 1 << 6) ? "VR-Therm, " : "",
3692 (msr & 1 << 8) ? "Amps, " : "",
3693 (msr & 1 << 9) ? "GFXPwr, " : "",
3694 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3695 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003696 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003697 (msr & 1 << 16) ? "PROCHOT, " : "",
3698 (msr & 1 << 17) ? "ThermStatus, " : "",
3699 (msr & 1 << 20) ? "Graphics, " : "",
3700 (msr & 1 << 22) ? "VR-Therm, " : "",
3701 (msr & 1 << 24) ? "Amps, " : "",
3702 (msr & 1 << 25) ? "GFXPwr, " : "",
3703 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3704 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3705 }
3706 if (do_ring_perf_limit_reasons) {
3707 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003708 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3709 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003710 (msr & 1 << 0) ? "PROCHOT, " : "",
3711 (msr & 1 << 1) ? "ThermStatus, " : "",
3712 (msr & 1 << 6) ? "VR-Therm, " : "",
3713 (msr & 1 << 8) ? "Amps, " : "",
3714 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3715 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003716 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003717 (msr & 1 << 16) ? "PROCHOT, " : "",
3718 (msr & 1 << 17) ? "ThermStatus, " : "",
3719 (msr & 1 << 22) ? "VR-Therm, " : "",
3720 (msr & 1 << 24) ? "Amps, " : "",
3721 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3722 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3723 }
3724 return 0;
3725}
3726
Len Brown889facb2012-11-08 00:48:57 -05003727#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3728#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3729
Colin Ian King1b693172016-03-02 13:50:25 +00003730double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003731{
3732 unsigned long long msr;
3733
3734 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003735 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003736 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3737
3738 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003739 case INTEL_FAM6_ATOM_SILVERMONT1:
3740 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003741 return 30.0;
3742 default:
3743 return 135.0;
3744 }
3745}
3746
Andrey Semin40ee8e32014-12-05 00:07:00 -05003747/*
3748 * rapl_dram_energy_units_probe()
3749 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3750 */
3751static double
3752rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3753{
3754 /* only called for genuine_intel, family 6 */
3755
3756 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003757 case INTEL_FAM6_HASWELL_X: /* HSX */
3758 case INTEL_FAM6_BROADWELL_X: /* BDX */
3759 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3760 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003761 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003762 return (rapl_dram_energy_units = 15.3 / 1000000);
3763 default:
3764 return (rapl_energy_units);
3765 }
3766}
3767
Len Brown144b44b2013-11-09 00:30:16 -05003768
Len Brown889facb2012-11-08 00:48:57 -05003769/*
3770 * rapl_probe()
3771 *
Len Brown144b44b2013-11-09 00:30:16 -05003772 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003773 */
3774void rapl_probe(unsigned int family, unsigned int model)
3775{
3776 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003777 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003778 double tdp;
3779
3780 if (!genuine_intel)
3781 return;
3782
3783 if (family != 6)
3784 return;
3785
3786 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003787 case INTEL_FAM6_SANDYBRIDGE:
3788 case INTEL_FAM6_IVYBRIDGE:
3789 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3790 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3791 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3792 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3793 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003794 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003795 if (rapl_joules) {
3796 BIC_PRESENT(BIC_Pkg_J);
3797 BIC_PRESENT(BIC_Cor_J);
3798 BIC_PRESENT(BIC_GFX_J);
3799 } else {
3800 BIC_PRESENT(BIC_PkgWatt);
3801 BIC_PRESENT(BIC_CorWatt);
3802 BIC_PRESENT(BIC_GFXWatt);
3803 }
Len Brown889facb2012-11-08 00:48:57 -05003804 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003805 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003806 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003807 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003808 if (rapl_joules)
3809 BIC_PRESENT(BIC_Pkg_J);
3810 else
3811 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003812 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003813 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3814 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3815 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3816 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003817 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown81824922017-03-04 17:23:07 -05003818 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 -05003819 BIC_PRESENT(BIC_PKG__);
3820 BIC_PRESENT(BIC_RAM__);
3821 if (rapl_joules) {
3822 BIC_PRESENT(BIC_Pkg_J);
3823 BIC_PRESENT(BIC_Cor_J);
3824 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003825 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003826 } else {
3827 BIC_PRESENT(BIC_PkgWatt);
3828 BIC_PRESENT(BIC_CorWatt);
3829 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003830 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003831 }
Len Brown0b2bb692015-03-26 00:50:30 -04003832 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003833 case INTEL_FAM6_HASWELL_X: /* HSX */
3834 case INTEL_FAM6_BROADWELL_X: /* BDX */
3835 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3836 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3837 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003838 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003839 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 -05003840 BIC_PRESENT(BIC_PKG__);
3841 BIC_PRESENT(BIC_RAM__);
3842 if (rapl_joules) {
3843 BIC_PRESENT(BIC_Pkg_J);
3844 BIC_PRESENT(BIC_RAM_J);
3845 } else {
3846 BIC_PRESENT(BIC_PkgWatt);
3847 BIC_PRESENT(BIC_RAMWatt);
3848 }
Len Browne6f9bb32013-12-03 02:19:19 -05003849 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003850 case INTEL_FAM6_SANDYBRIDGE_X:
3851 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003852 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 -05003853 BIC_PRESENT(BIC_PKG__);
3854 BIC_PRESENT(BIC_RAM__);
3855 if (rapl_joules) {
3856 BIC_PRESENT(BIC_Pkg_J);
3857 BIC_PRESENT(BIC_Cor_J);
3858 BIC_PRESENT(BIC_RAM_J);
3859 } else {
3860 BIC_PRESENT(BIC_PkgWatt);
3861 BIC_PRESENT(BIC_CorWatt);
3862 BIC_PRESENT(BIC_RAMWatt);
3863 }
Len Brown144b44b2013-11-09 00:30:16 -05003864 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003865 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3866 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003867 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003868 if (rapl_joules) {
3869 BIC_PRESENT(BIC_Pkg_J);
3870 BIC_PRESENT(BIC_Cor_J);
3871 } else {
3872 BIC_PRESENT(BIC_PkgWatt);
3873 BIC_PRESENT(BIC_CorWatt);
3874 }
Len Brown889facb2012-11-08 00:48:57 -05003875 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003876 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003877 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 -05003878 BIC_PRESENT(BIC_PKG__);
3879 BIC_PRESENT(BIC_RAM__);
3880 if (rapl_joules) {
3881 BIC_PRESENT(BIC_Pkg_J);
3882 BIC_PRESENT(BIC_Cor_J);
3883 BIC_PRESENT(BIC_RAM_J);
3884 } else {
3885 BIC_PRESENT(BIC_PkgWatt);
3886 BIC_PRESENT(BIC_CorWatt);
3887 BIC_PRESENT(BIC_RAMWatt);
3888 }
Jacob Pan0f644902016-06-16 09:48:22 -07003889 break;
Len Brown889facb2012-11-08 00:48:57 -05003890 default:
3891 return;
3892 }
3893
3894 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003895 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003896 return;
3897
3898 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce69e2016-06-16 23:22:37 -04003899 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003900 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3901 else
3902 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003903
Andrey Semin40ee8e32014-12-05 00:07:00 -05003904 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3905
Len Brown144b44b2013-11-09 00:30:16 -05003906 time_unit = msr >> 16 & 0xF;
3907 if (time_unit == 0)
3908 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003909
Len Brown144b44b2013-11-09 00:30:16 -05003910 rapl_time_units = 1.0 / (1 << (time_unit));
3911
3912 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003913
3914 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003915 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003916 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003917
3918 return;
3919}
3920
Colin Ian King1b693172016-03-02 13:50:25 +00003921void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003922{
3923 if (!genuine_intel)
3924 return;
3925
3926 if (family != 6)
3927 return;
3928
3929 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003930 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3931 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3932 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003933 do_gfx_perf_limit_reasons = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04003934 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003935 do_core_perf_limit_reasons = 1;
3936 do_ring_perf_limit_reasons = 1;
3937 default:
3938 return;
3939 }
3940}
3941
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003942void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3943{
3944 if (is_skx(family, model) || is_bdx(family, model))
3945 has_automatic_cstate_conversion = 1;
3946}
3947
Len Brown889facb2012-11-08 00:48:57 -05003948int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3949{
3950 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003951 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003952 int cpu;
3953
3954 if (!(do_dts || do_ptm))
3955 return 0;
3956
3957 cpu = t->cpu_id;
3958
3959 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003960 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003961 return 0;
3962
3963 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003964 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003965 return -1;
3966 }
3967
3968 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3969 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3970 return 0;
3971
3972 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003973 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003974 cpu, msr, tcc_activation_temp - dts);
3975
Len Brown889facb2012-11-08 00:48:57 -05003976 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3977 return 0;
3978
3979 dts = (msr >> 16) & 0x7F;
3980 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003981 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003982 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003983 }
3984
3985
Len Brownf4896fa52017-03-04 18:10:45 -05003986 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003987 unsigned int resolution;
3988
3989 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3990 return 0;
3991
3992 dts = (msr >> 16) & 0x7F;
3993 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003994 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003995 cpu, msr, tcc_activation_temp - dts, resolution);
3996
Len Brown889facb2012-11-08 00:48:57 -05003997 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3998 return 0;
3999
4000 dts = (msr >> 16) & 0x7F;
4001 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05004002 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004003 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05004004 }
4005
4006 return 0;
4007}
Len Brown36229892016-02-26 20:51:02 -05004008
Len Brown889facb2012-11-08 00:48:57 -05004009void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4010{
Len Brownb7d8c142016-02-13 23:36:17 -05004011 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05004012 cpu, label,
4013 ((msr >> 15) & 1) ? "EN" : "DIS",
4014 ((msr >> 0) & 0x7FFF) * rapl_power_units,
4015 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4016 (((msr >> 16) & 1) ? "EN" : "DIS"));
4017
4018 return;
4019}
4020
4021int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4022{
4023 unsigned long long msr;
4024 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05004025
4026 if (!do_rapl)
4027 return 0;
4028
4029 /* RAPL counters are per package, so print only for 1st thread/package */
4030 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4031 return 0;
4032
4033 cpu = t->cpu_id;
4034 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004035 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004036 return -1;
4037 }
4038
4039 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4040 return -1;
4041
Len Brown96e47152017-01-21 02:26:00 -05004042 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
4043 rapl_power_units, rapl_energy_units, rapl_time_units);
4044
Len Brown144b44b2013-11-09 00:30:16 -05004045 if (do_rapl & RAPL_PKG_POWER_INFO) {
4046
Len Brown889facb2012-11-08 00:48:57 -05004047 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4048 return -5;
4049
4050
Len Brownb7d8c142016-02-13 23:36:17 -05004051 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 -05004052 cpu, msr,
4053 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4054 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4055 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4056 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4057
Len Brown144b44b2013-11-09 00:30:16 -05004058 }
4059 if (do_rapl & RAPL_PKG) {
4060
Len Brown889facb2012-11-08 00:48:57 -05004061 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4062 return -9;
4063
Len Brownb7d8c142016-02-13 23:36:17 -05004064 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05004065 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05004066
4067 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05004068 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05004069 cpu,
4070 ((msr >> 47) & 1) ? "EN" : "DIS",
4071 ((msr >> 32) & 0x7FFF) * rapl_power_units,
4072 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4073 ((msr >> 48) & 1) ? "EN" : "DIS");
4074 }
4075
Len Brown0b2bb692015-03-26 00:50:30 -04004076 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05004077 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4078 return -6;
4079
Len Brownb7d8c142016-02-13 23:36:17 -05004080 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 -05004081 cpu, msr,
4082 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4083 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4084 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4085 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04004086 }
4087 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05004088 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4089 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05004090 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05004091 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05004092
4093 print_power_limit_msr(cpu, msr, "DRAM Limit");
4094 }
Len Brown144b44b2013-11-09 00:30:16 -05004095 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05004096 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4097 return -7;
Len Brown889facb2012-11-08 00:48:57 -05004098
Len Brown96e47152017-01-21 02:26:00 -05004099 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05004100 }
Jacob Pan91484942016-06-16 09:48:20 -07004101 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05004102 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4103 return -9;
4104 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4105 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4106 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05004107 }
4108 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05004109 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4110 return -8;
Len Brown889facb2012-11-08 00:48:57 -05004111
Len Brown96e47152017-01-21 02:26:00 -05004112 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05004113
Len Brown96e47152017-01-21 02:26:00 -05004114 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4115 return -9;
4116 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4117 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4118 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05004119 }
4120 return 0;
4121}
4122
Len Brownd7899442015-01-23 00:12:33 -05004123/*
4124 * SNB adds support for additional MSRs:
4125 *
4126 * MSR_PKG_C7_RESIDENCY 0x000003fa
4127 * MSR_CORE_C7_RESIDENCY 0x000003fe
4128 * MSR_PKG_C2_RESIDENCY 0x0000060d
4129 */
Len Brown103a8fe2010-10-22 23:53:03 -04004130
Len Brownd7899442015-01-23 00:12:33 -05004131int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04004132{
4133 if (!genuine_intel)
4134 return 0;
4135
4136 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004137 case INTEL_FAM6_SANDYBRIDGE:
4138 case INTEL_FAM6_SANDYBRIDGE_X:
4139 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4140 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4141 case INTEL_FAM6_HASWELL_CORE: /* HSW */
4142 case INTEL_FAM6_HASWELL_X: /* HSW */
4143 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4144 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
4145 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4146 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
4147 case INTEL_FAM6_BROADWELL_X: /* BDX */
4148 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
4149 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4150 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4151 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4152 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004153 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce69e2016-06-16 23:22:37 -04004154 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4155 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004156 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08004157 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04004158 return 1;
4159 }
4160 return 0;
4161}
4162
Len Brownd7899442015-01-23 00:12:33 -05004163/*
4164 * HSW adds support for additional MSRs:
4165 *
Len Brown5a634262016-04-06 17:15:55 -04004166 * MSR_PKG_C8_RESIDENCY 0x00000630
4167 * MSR_PKG_C9_RESIDENCY 0x00000631
4168 * MSR_PKG_C10_RESIDENCY 0x00000632
4169 *
4170 * MSR_PKGC8_IRTL 0x00000633
4171 * MSR_PKGC9_IRTL 0x00000634
4172 * MSR_PKGC10_IRTL 0x00000635
4173 *
Len Brownd7899442015-01-23 00:12:33 -05004174 */
4175int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004176{
4177 if (!genuine_intel)
4178 return 0;
4179
4180 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004181 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4182 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4183 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4184 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4185 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4186 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004187 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce69e2016-06-16 23:22:37 -04004188 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004189 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004190 return 1;
4191 }
4192 return 0;
4193}
4194
Len Brown0b2bb692015-03-26 00:50:30 -04004195/*
4196 * SKL adds support for additional MSRS:
4197 *
4198 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4199 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4200 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4201 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4202 */
4203int has_skl_msrs(unsigned int family, unsigned int model)
4204{
4205 if (!genuine_intel)
4206 return 0;
4207
4208 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004209 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4210 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4211 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4212 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004213 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown0b2bb692015-03-26 00:50:30 -04004214 return 1;
4215 }
4216 return 0;
4217}
4218
Len Brown144b44b2013-11-09 00:30:16 -05004219int is_slm(unsigned int family, unsigned int model)
4220{
4221 if (!genuine_intel)
4222 return 0;
4223 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004224 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
4225 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05004226 return 1;
4227 }
4228 return 0;
4229}
4230
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004231int is_knl(unsigned int family, unsigned int model)
4232{
4233 if (!genuine_intel)
4234 return 0;
4235 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004236 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05004237 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004238 return 1;
4239 }
4240 return 0;
4241}
4242
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004243int is_cnl(unsigned int family, unsigned int model)
4244{
4245 if (!genuine_intel)
4246 return 0;
4247
4248 switch (model) {
4249 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4250 return 1;
4251 }
4252
4253 return 0;
4254}
4255
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004256unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4257{
4258 if (is_knl(family, model))
4259 return 1024;
4260 return 1;
4261}
4262
Len Brown144b44b2013-11-09 00:30:16 -05004263#define SLM_BCLK_FREQS 5
4264double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4265
4266double slm_bclk(void)
4267{
4268 unsigned long long msr = 3;
4269 unsigned int i;
4270 double freq;
4271
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004272 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05004273 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05004274
4275 i = msr & 0xf;
4276 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05004277 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01004278 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05004279 }
4280 freq = slm_freq_table[i];
4281
Len Brown96e47152017-01-21 02:26:00 -05004282 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05004283 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05004284
4285 return freq;
4286}
4287
Len Brown103a8fe2010-10-22 23:53:03 -04004288double discover_bclk(unsigned int family, unsigned int model)
4289{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01004290 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04004291 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05004292 else if (is_slm(family, model))
4293 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04004294 else
4295 return 133.33;
4296}
4297
Len Brown889facb2012-11-08 00:48:57 -05004298/*
4299 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4300 * the Thermal Control Circuit (TCC) activates.
4301 * This is usually equal to tjMax.
4302 *
4303 * Older processors do not have this MSR, so there we guess,
4304 * but also allow cmdline over-ride with -T.
4305 *
4306 * Several MSR temperature values are in units of degrees-C
4307 * below this value, including the Digital Thermal Sensor (DTS),
4308 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4309 */
4310int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4311{
4312 unsigned long long msr;
4313 unsigned int target_c_local;
4314 int cpu;
4315
4316 /* tcc_activation_temp is used only for dts or ptm */
4317 if (!(do_dts || do_ptm))
4318 return 0;
4319
4320 /* this is a per-package concept */
4321 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4322 return 0;
4323
4324 cpu = t->cpu_id;
4325 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004326 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004327 return -1;
4328 }
4329
4330 if (tcc_activation_temp_override != 0) {
4331 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004332 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004333 cpu, tcc_activation_temp);
4334 return 0;
4335 }
4336
4337 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004338 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004339 goto guess;
4340
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004341 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004342 goto guess;
4343
Jean Delvare34821242014-05-01 11:40:19 +02004344 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004345
Len Brown96e47152017-01-21 02:26:00 -05004346 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004347 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004348 cpu, msr, target_c_local);
4349
Jean Delvare34821242014-05-01 11:40:19 +02004350 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004351 goto guess;
4352
4353 tcc_activation_temp = target_c_local;
4354
4355 return 0;
4356
4357guess:
4358 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004359 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004360 cpu, tcc_activation_temp);
4361
4362 return 0;
4363}
Len Brown69807a62015-11-21 12:22:47 -05004364
Len Brownaa8d8cc2016-03-11 13:26:03 -05004365void decode_feature_control_msr(void)
4366{
4367 unsigned long long msr;
4368
4369 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4370 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4371 base_cpu, msr,
4372 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4373 msr & (1 << 18) ? "SGX" : "");
4374}
4375
Len Brown69807a62015-11-21 12:22:47 -05004376void decode_misc_enable_msr(void)
4377{
4378 unsigned long long msr;
4379
Len Brownf26b1512017-06-23 20:45:54 -07004380 if (!genuine_intel)
4381 return;
4382
Len Brown69807a62015-11-21 12:22:47 -05004383 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004384 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004385 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004386 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4387 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004388 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004389 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4390 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004391}
4392
Len Brown33148d62017-01-21 01:26:16 -05004393void decode_misc_feature_control(void)
4394{
4395 unsigned long long msr;
4396
4397 if (!has_misc_feature_control)
4398 return;
4399
4400 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4401 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4402 base_cpu, msr,
4403 msr & (0 << 0) ? "No-" : "",
4404 msr & (1 << 0) ? "No-" : "",
4405 msr & (2 << 0) ? "No-" : "",
4406 msr & (3 << 0) ? "No-" : "");
4407}
Len Brownf0057312015-12-03 01:35:36 -05004408/*
4409 * Decode MSR_MISC_PWR_MGMT
4410 *
4411 * Decode the bits according to the Nehalem documentation
4412 * bit[0] seems to continue to have same meaning going forward
4413 * bit[1] less so...
4414 */
4415void decode_misc_pwr_mgmt_msr(void)
4416{
4417 unsigned long long msr;
4418
4419 if (!do_nhm_platform_info)
4420 return;
4421
Len Browncf4cbe52017-01-01 13:08:33 -05004422 if (no_MSR_MISC_PWR_MGMT)
4423 return;
4424
Len Brownf0057312015-12-03 01:35:36 -05004425 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004426 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 -05004427 base_cpu, msr,
4428 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004429 msr & (1 << 1) ? "EN" : "DIS",
4430 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004431}
Len Brown71616c82017-01-07 22:37:48 -05004432/*
4433 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4434 *
4435 * This MSRs are present on Silvermont processors,
4436 * Intel Atom processor E3000 series (Baytrail), and friends.
4437 */
4438void decode_c6_demotion_policy_msr(void)
4439{
4440 unsigned long long msr;
4441
4442 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4443 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4444 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4445
4446 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4447 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4448 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4449}
Len Brown7f5c2582015-12-01 01:36:39 -05004450
Len Brownfcd17212015-03-23 20:29:09 -04004451void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004452{
Len Brown61a87ba2015-11-23 02:30:51 -05004453 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004454 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004455 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004456
4457 eax = ebx = ecx = edx = 0;
4458
Len Brown5aea2f72016-03-13 03:14:35 -04004459 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004460
4461 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4462 genuine_intel = 1;
4463
Len Brown96e47152017-01-21 02:26:00 -05004464 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004465 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004466 (char *)&ebx, (char *)&edx, (char *)&ecx);
4467
Len Brown5aea2f72016-03-13 03:14:35 -04004468 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004469 family = (fms >> 8) & 0xf;
4470 model = (fms >> 4) & 0xf;
4471 stepping = fms & 0xf;
4472 if (family == 6 || family == 0xf)
4473 model += ((fms >> 16) & 0xf) << 4;
4474
Len Brown96e47152017-01-21 02:26:00 -05004475 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004476 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004477 max_level, family, model, stepping, family, model, stepping);
Len Brownd9d226f2018-06-06 15:47:36 -04004478 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004479 ecx & (1 << 0) ? "SSE3" : "-",
4480 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004481 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004482 ecx & (1 << 7) ? "EIST" : "-",
4483 ecx & (1 << 8) ? "TM2" : "-",
4484 edx & (1 << 4) ? "TSC" : "-",
4485 edx & (1 << 5) ? "MSR" : "-",
4486 edx & (1 << 22) ? "ACPI-TM" : "-",
Len Brownd9d226f2018-06-06 15:47:36 -04004487 edx & (1 << 28) ? "HT" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004488 edx & (1 << 29) ? "TM" : "-");
4489 }
Len Brown103a8fe2010-10-22 23:53:03 -04004490
Josh Triplettb2c95d92013-08-20 17:20:18 -07004491 if (!(edx & (1 << 5)))
4492 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004493
4494 /*
4495 * check max extended function levels of CPUID.
4496 * This is needed to check for invariant TSC.
4497 * This check is valid for both Intel and AMD.
4498 */
4499 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004500 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004501
Len Brown61a87ba2015-11-23 02:30:51 -05004502 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004503
Len Brownd7899442015-01-23 00:12:33 -05004504 /*
4505 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4506 * this check is valid for both Intel and AMD
4507 */
Len Brown5aea2f72016-03-13 03:14:35 -04004508 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004509 has_invariant_tsc = edx & (1 << 8);
4510 }
Len Brown103a8fe2010-10-22 23:53:03 -04004511
4512 /*
4513 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4514 * this check is valid for both Intel and AMD
4515 */
4516
Len Brown5aea2f72016-03-13 03:14:35 -04004517 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004518 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004519 if (has_aperf) {
4520 BIC_PRESENT(BIC_Avg_MHz);
4521 BIC_PRESENT(BIC_Busy);
4522 BIC_PRESENT(BIC_Bzy_MHz);
4523 }
Len Brown889facb2012-11-08 00:48:57 -05004524 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004525 if (do_dts)
4526 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004527 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004528 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004529 if (do_ptm)
4530 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004531 has_hwp = eax & (1 << 7);
4532 has_hwp_notify = eax & (1 << 8);
4533 has_hwp_activity_window = eax & (1 << 9);
4534 has_hwp_epp = eax & (1 << 10);
4535 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004536 has_epb = ecx & (1 << 3);
4537
Len Brown96e47152017-01-21 02:26:00 -05004538 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004539 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004540 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4541 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004542 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004543 do_dts ? "" : "No-",
4544 do_ptm ? "" : "No-",
4545 has_hwp ? "" : "No-",
4546 has_hwp_notify ? "" : "No-",
4547 has_hwp_activity_window ? "" : "No-",
4548 has_hwp_epp ? "" : "No-",
4549 has_hwp_pkg ? "" : "No-",
4550 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004551
Len Brown96e47152017-01-21 02:26:00 -05004552 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004553 decode_misc_enable_msr();
4554
Len Brown33148d62017-01-21 01:26:16 -05004555
Len Brown96e47152017-01-21 02:26:00 -05004556 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004557 int has_sgx;
4558
4559 ecx = 0;
4560
4561 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4562
4563 has_sgx = ebx & (1 << 2);
4564 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4565
4566 if (has_sgx)
4567 decode_feature_control_msr();
4568 }
4569
Len Brown61a87ba2015-11-23 02:30:51 -05004570 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004571 unsigned int eax_crystal;
4572 unsigned int ebx_tsc;
4573
4574 /*
4575 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4576 */
4577 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004578 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004579
4580 if (ebx_tsc != 0) {
4581
Len Brown96e47152017-01-21 02:26:00 -05004582 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004583 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004584 eax_crystal, ebx_tsc, crystal_hz);
4585
4586 if (crystal_hz == 0)
4587 switch(model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004588 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4589 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4590 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4591 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004592 crystal_hz = 24000000; /* 24.0 MHz */
4593 break;
Len Brown7268d402016-12-01 23:10:39 -05004594 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004595 crystal_hz = 25000000; /* 25.0 MHz */
4596 break;
Len Brown869ce69e2016-06-16 23:22:37 -04004597 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004598 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004599 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004600 break;
4601 default:
4602 crystal_hz = 0;
4603 }
4604
4605 if (crystal_hz) {
4606 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004607 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004608 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004609 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4610 }
4611 }
4612 }
Len Brown61a87ba2015-11-23 02:30:51 -05004613 if (max_level >= 0x16) {
4614 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4615
4616 /*
4617 * CPUID 16H Base MHz, Max MHz, Bus MHz
4618 */
4619 base_mhz = max_mhz = bus_mhz = edx = 0;
4620
Len Brown5aea2f72016-03-13 03:14:35 -04004621 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004622 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004623 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004624 base_mhz, max_mhz, bus_mhz);
4625 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004626
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004627 if (has_aperf)
4628 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4629
Len Brown812db3f2017-02-10 00:25:41 -05004630 BIC_PRESENT(BIC_IRQ);
4631 BIC_PRESENT(BIC_TSC_MHz);
4632
4633 if (probe_nhm_msrs(family, model)) {
4634 do_nhm_platform_info = 1;
4635 BIC_PRESENT(BIC_CPU_c1);
4636 BIC_PRESENT(BIC_CPU_c3);
4637 BIC_PRESENT(BIC_CPU_c6);
4638 BIC_PRESENT(BIC_SMI);
4639 }
Len Brownd7899442015-01-23 00:12:33 -05004640 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004641
4642 if (do_snb_cstates)
4643 BIC_PRESENT(BIC_CPU_c7);
4644
Len Brown5a634262016-04-06 17:15:55 -04004645 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004646 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4647 BIC_PRESENT(BIC_Pkgpc2);
4648 if (pkg_cstate_limit >= PCL__3)
4649 BIC_PRESENT(BIC_Pkgpc3);
4650 if (pkg_cstate_limit >= PCL__6)
4651 BIC_PRESENT(BIC_Pkgpc6);
4652 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4653 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba112017-02-10 00:27:20 -05004654 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004655 BIC_NOT_PRESENT(BIC_Pkgpc2);
4656 BIC_NOT_PRESENT(BIC_Pkgpc3);
4657 BIC_PRESENT(BIC_Pkgpc6);
4658 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba112017-02-10 00:27:20 -05004659 BIC_PRESENT(BIC_Mod_c6);
4660 use_c1_residency_msr = 1;
4661 }
Len Brown7170a372017-01-27 02:13:27 -05004662 if (is_dnv(family, model)) {
4663 BIC_PRESENT(BIC_CPU_c1);
4664 BIC_NOT_PRESENT(BIC_CPU_c3);
4665 BIC_NOT_PRESENT(BIC_Pkgpc3);
4666 BIC_NOT_PRESENT(BIC_CPU_c7);
4667 BIC_NOT_PRESENT(BIC_Pkgpc7);
4668 use_c1_residency_msr = 1;
4669 }
Len Brown34c761972017-01-27 02:36:41 -05004670 if (is_skx(family, model)) {
4671 BIC_NOT_PRESENT(BIC_CPU_c3);
4672 BIC_NOT_PRESENT(BIC_Pkgpc3);
4673 BIC_NOT_PRESENT(BIC_CPU_c7);
4674 BIC_NOT_PRESENT(BIC_Pkgpc7);
4675 }
Len Brownade0eba2017-02-10 01:56:47 -05004676 if (is_bdx(family, model)) {
4677 BIC_NOT_PRESENT(BIC_CPU_c7);
4678 BIC_NOT_PRESENT(BIC_Pkgpc7);
4679 }
Len Brown0f47c082017-01-27 00:50:45 -05004680 if (has_hsw_msrs(family, model)) {
4681 BIC_PRESENT(BIC_Pkgpc8);
4682 BIC_PRESENT(BIC_Pkgpc9);
4683 BIC_PRESENT(BIC_Pkgpc10);
4684 }
Len Brown5a634262016-04-06 17:15:55 -04004685 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004686 if (has_skl_msrs(family, model)) {
4687 BIC_PRESENT(BIC_Totl_c0);
4688 BIC_PRESENT(BIC_Any_c0);
4689 BIC_PRESENT(BIC_GFX_c0);
4690 BIC_PRESENT(BIC_CPUGFX);
4691 }
Len Brown144b44b2013-11-09 00:30:16 -05004692 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004693 do_knl_cstates = is_knl(family, model);
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004694 do_cnl_cstates = is_cnl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004695
Len Brown96e47152017-01-21 02:26:00 -05004696 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004697 decode_misc_pwr_mgmt_msr();
4698
Len Brown96e47152017-01-21 02:26:00 -05004699 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004700 decode_c6_demotion_policy_msr();
4701
Len Brown889facb2012-11-08 00:48:57 -05004702 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004703 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004704 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004705
Len Brown96e47152017-01-21 02:26:00 -05004706 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004707 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004708
Len Brown41618e62017-02-09 18:25:22 -05004709 if (!quiet)
4710 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004711 if (!quiet)
4712 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004713
Len Browna2b7b742015-09-26 00:12:38 -04004714 if (has_skl_msrs(family, model))
4715 calculate_tsc_tweak();
4716
Len Brown812db3f2017-02-10 00:25:41 -05004717 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4718 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004719
Len Brown812db3f2017-02-10 00:25:41 -05004720 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4721 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004722
Len Brownbe0e54c2018-06-01 12:35:53 -04004723 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4724 BIC_PRESENT(BIC_CPU_LPI);
4725 else
4726 BIC_NOT_PRESENT(BIC_CPU_LPI);
4727
4728 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4729 BIC_PRESENT(BIC_SYS_LPI);
4730 else
4731 BIC_NOT_PRESENT(BIC_SYS_LPI);
4732
Len Brown96e47152017-01-21 02:26:00 -05004733 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004734 decode_misc_feature_control();
4735
Len Brown889facb2012-11-08 00:48:57 -05004736 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004737}
4738
Len Brown103a8fe2010-10-22 23:53:03 -04004739/*
4740 * in /dev/cpu/ return success for names that are numbers
4741 * ie. filter out ".", "..", "microcode".
4742 */
4743int dir_filter(const struct dirent *dirp)
4744{
4745 if (isdigit(dirp->d_name[0]))
4746 return 1;
4747 else
4748 return 0;
4749}
4750
4751int open_dev_cpu_msr(int dummy1)
4752{
4753 return 0;
4754}
4755
Len Brownc98d5d92012-06-04 00:56:40 -04004756void topology_probe()
4757{
4758 int i;
4759 int max_core_id = 0;
4760 int max_package_id = 0;
4761 int max_siblings = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004762
4763 /* Initialize num_cpus, max_cpu_num */
Prarit Bhargava843c5792018-06-01 10:04:28 -04004764 set_max_cpu_num();
Len Brownc98d5d92012-06-04 00:56:40 -04004765 topo.num_cpus = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004766 for_all_proc_cpus(count_cpus);
4767 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004768 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004769
Len Brownd8af6f52015-02-10 01:56:38 -05004770 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004771 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004772
4773 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004774 if (cpus == NULL)
4775 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004776
4777 /*
4778 * Allocate and initialize cpu_present_set
4779 */
4780 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004781 if (cpu_present_set == NULL)
4782 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004783 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4784 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4785 for_all_proc_cpus(mark_cpu_present);
4786
4787 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004788 * Validate that all cpus in cpu_subset are also in cpu_present_set
4789 */
4790 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4791 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4792 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4793 err(1, "cpu%d not present", i);
4794 }
4795
4796 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004797 * Allocate and initialize cpu_affinity_set
4798 */
4799 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004800 if (cpu_affinity_set == NULL)
4801 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004802 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4803 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4804
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004805 for_all_proc_cpus(init_thread_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004806
4807 /*
4808 * For online cpus
4809 * find max_core_id, max_package_id
4810 */
4811 for (i = 0; i <= topo.max_cpu_num; ++i) {
4812 int siblings;
4813
4814 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004815 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004816 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004817 continue;
4818 }
Len Brownc98d5d92012-06-04 00:56:40 -04004819
Len Brown0e2d8f02018-06-01 22:08:58 -04004820 cpus[i].logical_cpu_id = i;
4821
4822 /* get package information */
Len Brownc98d5d92012-06-04 00:56:40 -04004823 cpus[i].physical_package_id = get_physical_package_id(i);
4824 if (cpus[i].physical_package_id > max_package_id)
4825 max_package_id = cpus[i].physical_package_id;
4826
Len Brown0e2d8f02018-06-01 22:08:58 -04004827 /* get numa node information */
Prarit Bhargavaef605742018-06-01 10:04:30 -04004828 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4829 if (cpus[i].physical_node_id > topo.max_node_num)
4830 topo.max_node_num = cpus[i].physical_node_id;
Len Brown0e2d8f02018-06-01 22:08:58 -04004831
4832 /* get core information */
4833 cpus[i].physical_core_id = get_core_id(i);
4834 if (cpus[i].physical_core_id > max_core_id)
4835 max_core_id = cpus[i].physical_core_id;
4836
4837 /* get thread information */
4838 siblings = get_thread_siblings(&cpus[i]);
Len Brownc98d5d92012-06-04 00:56:40 -04004839 if (siblings > max_siblings)
4840 max_siblings = siblings;
Artem Bityutskiy4f206a02018-07-25 11:52:06 +03004841 if (cpus[i].thread_id == 0)
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004842 topo.num_cores++;
Len Brown0e2d8f02018-06-01 22:08:58 -04004843
Len Brownd8af6f52015-02-10 01:56:38 -05004844 if (debug > 1)
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004845 fprintf(outf,
4846 "cpu %d pkg %d node %d core %d thread %d\n",
Len Brown0e2d8f02018-06-01 22:08:58 -04004847 i, cpus[i].physical_package_id,
Prarit Bhargavaef605742018-06-01 10:04:30 -04004848 cpus[i].physical_node_id,
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004849 cpus[i].physical_core_id,
4850 cpus[i].thread_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004851 }
Prarit Bhargavaef605742018-06-01 10:04:30 -04004852
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004853 topo.cores_per_node = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004854 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004855 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004856 max_core_id, topo.cores_per_node);
4857 if (!summary_only && topo.cores_per_node > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004858 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004859
4860 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004861 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004862 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004863 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004864 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004865 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004866
Prarit Bhargavaef605742018-06-01 10:04:30 -04004867 set_node_data();
4868 if (debug > 1)
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004869 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
Prarit Bhargava01235042018-06-01 10:04:35 -04004870 if (!summary_only && topo.nodes_per_pkg > 1)
4871 BIC_PRESENT(BIC_Node);
Prarit Bhargavaef605742018-06-01 10:04:30 -04004872
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004873 topo.threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004874 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004875 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004876}
4877
4878void
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004879allocate_counters(struct thread_data **t, struct core_data **c,
4880 struct pkg_data **p)
Len Brownc98d5d92012-06-04 00:56:40 -04004881{
4882 int i;
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004883 int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
4884 topo.num_packages;
4885 int num_threads = topo.threads_per_core * num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04004886
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004887 *t = calloc(num_threads, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004888 if (*t == NULL)
4889 goto error;
4890
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004891 for (i = 0; i < num_threads; i++)
Len Brownc98d5d92012-06-04 00:56:40 -04004892 (*t)[i].cpu_id = -1;
4893
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004894 *c = calloc(num_cores, sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004895 if (*c == NULL)
4896 goto error;
4897
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004898 for (i = 0; i < num_cores; i++)
Len Brownc98d5d92012-06-04 00:56:40 -04004899 (*c)[i].core_id = -1;
4900
Len Brown678a3bd2017-02-09 22:22:13 -05004901 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004902 if (*p == NULL)
4903 goto error;
4904
4905 for (i = 0; i < topo.num_packages; i++)
4906 (*p)[i].package_id = i;
4907
4908 return;
4909error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004910 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004911}
4912/*
4913 * init_counter()
4914 *
Len Brownc98d5d92012-06-04 00:56:40 -04004915 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
Len Brownc98d5d92012-06-04 00:56:40 -04004916 */
4917void init_counter(struct thread_data *thread_base, struct core_data *core_base,
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004918 struct pkg_data *pkg_base, int cpu_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004919{
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004920 int pkg_id = cpus[cpu_id].physical_package_id;
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004921 int node_id = cpus[cpu_id].logical_node_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004922 int core_id = cpus[cpu_id].physical_core_id;
4923 int thread_id = cpus[cpu_id].thread_id;
Len Brownc98d5d92012-06-04 00:56:40 -04004924 struct thread_data *t;
4925 struct core_data *c;
4926 struct pkg_data *p;
4927
Nathan Ciobanu42dd4522018-06-08 15:15:12 -07004928
4929 /* Workaround for systems where physical_node_id==-1
4930 * and logical_node_id==(-1 - topo.num_cpus)
4931 */
4932 if (node_id < 0)
4933 node_id = 0;
4934
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004935 t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
4936 c = GET_CORE(core_base, core_id, node_id, pkg_id);
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004937 p = GET_PKG(pkg_base, pkg_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004938
4939 t->cpu_id = cpu_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004940 if (thread_id == 0) {
Len Brownc98d5d92012-06-04 00:56:40 -04004941 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4942 if (cpu_is_first_core_in_package(cpu_id))
4943 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4944 }
4945
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004946 c->core_id = core_id;
4947 p->package_id = pkg_id;
Len Brownc98d5d92012-06-04 00:56:40 -04004948}
4949
4950
4951int initialize_counters(int cpu_id)
4952{
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004953 init_counter(EVEN_COUNTERS, cpu_id);
4954 init_counter(ODD_COUNTERS, cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004955 return 0;
4956}
4957
4958void allocate_output_buffer()
4959{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004960 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004961 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004962 if (outp == NULL)
4963 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004964}
Len Brown36229892016-02-26 20:51:02 -05004965void allocate_fd_percpu(void)
4966{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004967 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004968 if (fd_percpu == NULL)
4969 err(-1, "calloc fd_percpu");
4970}
Len Brown562a2d32016-02-26 23:48:05 -05004971void allocate_irq_buffers(void)
4972{
4973 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4974 if (irq_column_2_cpu == NULL)
4975 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004976
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004977 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004978 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004979 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004980}
Len Brownc98d5d92012-06-04 00:56:40 -04004981void setup_all_buffers(void)
4982{
4983 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004984 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004985 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004986 allocate_counters(&thread_even, &core_even, &package_even);
4987 allocate_counters(&thread_odd, &core_odd, &package_odd);
4988 allocate_output_buffer();
4989 for_all_proc_cpus(initialize_counters);
4990}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004991
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004992void set_base_cpu(void)
4993{
4994 base_cpu = sched_getcpu();
4995 if (base_cpu < 0)
4996 err(-ENODEV, "No valid cpus found");
4997
4998 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004999 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04005000}
5001
Len Brown103a8fe2010-10-22 23:53:03 -04005002void turbostat_init()
5003{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04005004 setup_all_buffers();
5005 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04005006 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04005007 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04005008 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04005009
Len Brown103a8fe2010-10-22 23:53:03 -04005010
Len Brown96e47152017-01-21 02:26:00 -05005011 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05005012 for_all_cpus(print_hwp, ODD_COUNTERS);
5013
Len Brown96e47152017-01-21 02:26:00 -05005014 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05005015 for_all_cpus(print_epb, ODD_COUNTERS);
5016
Len Brown96e47152017-01-21 02:26:00 -05005017 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04005018 for_all_cpus(print_perf_limit, ODD_COUNTERS);
5019
Len Brown96e47152017-01-21 02:26:00 -05005020 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05005021 for_all_cpus(print_rapl, ODD_COUNTERS);
5022
5023 for_all_cpus(set_temperature_target, ODD_COUNTERS);
5024
Len Brown96e47152017-01-21 02:26:00 -05005025 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05005026 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04005027
Len Brown96e47152017-01-21 02:26:00 -05005028 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04005029 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04005030}
5031
5032int fork_it(char **argv)
5033{
Len Brown103a8fe2010-10-22 23:53:03 -04005034 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04005035 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04005036
Len Brown218f0e82017-02-14 22:07:52 -05005037 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04005038 status = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brown4c2122d2018-06-06 17:44:48 -04005039 first_counter_read = 0;
Len Brownd91bb172012-11-01 00:08:19 -04005040 if (status)
5041 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04005042 /* clear affinity side-effect of get_counters() */
5043 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04005044 gettimeofday(&tv_even, (struct timezone *)NULL);
5045
5046 child_pid = fork();
5047 if (!child_pid) {
5048 /* child */
5049 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05005050 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04005051 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04005052
5053 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07005054 if (child_pid == -1)
5055 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04005056
5057 signal(SIGINT, SIG_IGN);
5058 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07005059 if (waitpid(child_pid, &status, 0) == -1)
5060 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04005061 }
Len Brownc98d5d92012-06-04 00:56:40 -04005062 /*
5063 * n.b. fork_it() does not check for errors from for_all_cpus()
5064 * because re-starting is problematic when forking
5065 */
Len Brown218f0e82017-02-14 22:07:52 -05005066 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04005067 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04005068 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04005069 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04005070 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5071 fprintf(outf, "%s: Counter reset detected\n", progname);
5072 else {
5073 compute_average(EVEN_COUNTERS);
5074 format_all_counters(EVEN_COUNTERS);
5075 }
Len Brown103a8fe2010-10-22 23:53:03 -04005076
Len Brownb7d8c142016-02-13 23:36:17 -05005077 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5078
5079 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04005080
Len Brownd91bb172012-11-01 00:08:19 -04005081 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04005082}
5083
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005084int get_and_dump_counters(void)
5085{
5086 int status;
5087
Len Brown218f0e82017-02-14 22:07:52 -05005088 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005089 status = for_all_cpus(get_counters, ODD_COUNTERS);
5090 if (status)
5091 return status;
5092
5093 status = for_all_cpus(dump_counters, ODD_COUNTERS);
5094 if (status)
5095 return status;
5096
Len Brownb7d8c142016-02-13 23:36:17 -05005097 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005098
5099 return status;
5100}
5101
Len Brownd8af6f52015-02-10 01:56:38 -05005102void print_version() {
Len Brown73780cd2018-06-20 13:55:29 -04005103 fprintf(outf, "turbostat version 18.06.20"
Len Brownd8af6f52015-02-10 01:56:38 -05005104 " - Len Brown <lenb@kernel.org>\n");
5105}
5106
Len Brown495c76542017-02-08 02:41:51 -05005107int add_counter(unsigned int msr_num, char *path, char *name,
5108 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05005109 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05005110{
5111 struct msr_counter *msrp;
5112
5113 msrp = calloc(1, sizeof(struct msr_counter));
5114 if (msrp == NULL) {
5115 perror("calloc");
5116 exit(1);
5117 }
5118
5119 msrp->msr_num = msr_num;
5120 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05005121 if (path)
5122 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05005123 msrp->width = width;
5124 msrp->type = type;
5125 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05005126 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05005127
5128 switch (scope) {
5129
5130 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05005131 msrp->next = sys.tp;
5132 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005133 sys.added_thread_counters++;
Len Brown0748eaf2018-06-01 12:38:29 -04005134 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
Len Brown678a3bd2017-02-09 22:22:13 -05005135 fprintf(stderr, "exceeded max %d added thread counters\n",
5136 MAX_ADDED_COUNTERS);
5137 exit(-1);
5138 }
Len Brown388e9c82016-12-22 23:57:55 -05005139 break;
5140
5141 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05005142 msrp->next = sys.cp;
5143 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005144 sys.added_core_counters++;
5145 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5146 fprintf(stderr, "exceeded max %d added core counters\n",
5147 MAX_ADDED_COUNTERS);
5148 exit(-1);
5149 }
Len Brown388e9c82016-12-22 23:57:55 -05005150 break;
5151
5152 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05005153 msrp->next = sys.pp;
5154 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005155 sys.added_package_counters++;
5156 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5157 fprintf(stderr, "exceeded max %d added package counters\n",
5158 MAX_ADDED_COUNTERS);
5159 exit(-1);
5160 }
Len Brown388e9c82016-12-22 23:57:55 -05005161 break;
5162 }
5163
5164 return 0;
5165}
5166
5167void parse_add_command(char *add_command)
5168{
5169 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05005170 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05005171 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05005172 int width = 64;
5173 int fail = 0;
5174 enum counter_scope scope = SCOPE_CPU;
5175 enum counter_type type = COUNTER_CYCLES;
5176 enum counter_format format = FORMAT_DELTA;
5177
5178 while (add_command) {
5179
5180 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5181 goto next;
5182
5183 if (sscanf(add_command, "msr%d", &msr_num) == 1)
5184 goto next;
5185
Len Brown495c76542017-02-08 02:41:51 -05005186 if (*add_command == '/') {
5187 path = add_command;
5188 goto next;
5189 }
5190
Len Brown388e9c82016-12-22 23:57:55 -05005191 if (sscanf(add_command, "u%d", &width) == 1) {
5192 if ((width == 32) || (width == 64))
5193 goto next;
5194 width = 64;
5195 }
5196 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5197 scope = SCOPE_CPU;
5198 goto next;
5199 }
5200 if (!strncmp(add_command, "core", strlen("core"))) {
5201 scope = SCOPE_CORE;
5202 goto next;
5203 }
5204 if (!strncmp(add_command, "package", strlen("package"))) {
5205 scope = SCOPE_PACKAGE;
5206 goto next;
5207 }
5208 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5209 type = COUNTER_CYCLES;
5210 goto next;
5211 }
5212 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5213 type = COUNTER_SECONDS;
5214 goto next;
5215 }
Len Brown41618e62017-02-09 18:25:22 -05005216 if (!strncmp(add_command, "usec", strlen("usec"))) {
5217 type = COUNTER_USEC;
5218 goto next;
5219 }
Len Brown388e9c82016-12-22 23:57:55 -05005220 if (!strncmp(add_command, "raw", strlen("raw"))) {
5221 format = FORMAT_RAW;
5222 goto next;
5223 }
5224 if (!strncmp(add_command, "delta", strlen("delta"))) {
5225 format = FORMAT_DELTA;
5226 goto next;
5227 }
5228 if (!strncmp(add_command, "percent", strlen("percent"))) {
5229 format = FORMAT_PERCENT;
5230 goto next;
5231 }
5232
5233 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
5234 char *eos;
5235
5236 eos = strchr(name_buffer, ',');
5237 if (eos)
5238 *eos = '\0';
5239 goto next;
5240 }
5241
5242next:
5243 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05005244 if (add_command) {
5245 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05005246 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05005247 }
Len Brown388e9c82016-12-22 23:57:55 -05005248
5249 }
Len Brown495c76542017-02-08 02:41:51 -05005250 if ((msr_num == 0) && (path == NULL)) {
5251 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05005252 fail++;
5253 }
5254
5255 /* generate default column header */
5256 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05005257 if (width == 32)
5258 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5259 else
5260 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05005261 }
5262
Len Brown41618e62017-02-09 18:25:22 -05005263 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05005264 fail++;
5265
5266 if (fail) {
5267 help();
5268 exit(1);
5269 }
5270}
Len Brown41618e62017-02-09 18:25:22 -05005271
Len Browndd778a52017-02-21 23:21:13 -05005272int is_deferred_skip(char *name)
5273{
5274 int i;
5275
5276 for (i = 0; i < deferred_skip_index; ++i)
5277 if (!strcmp(name, deferred_skip_names[i]))
5278 return 1;
5279 return 0;
5280}
5281
Len Brown41618e62017-02-09 18:25:22 -05005282void probe_sysfs(void)
5283{
5284 char path[64];
5285 char name_buf[16];
5286 FILE *input;
5287 int state;
5288 char *sp;
5289
5290 if (!DO_BIC(BIC_sysfs))
5291 return;
5292
Len Brown0748eaf2018-06-01 12:38:29 -04005293 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005294
5295 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5296 base_cpu, state);
5297 input = fopen(path, "r");
5298 if (input == NULL)
5299 continue;
5300 fgets(name_buf, sizeof(name_buf), input);
5301
5302 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5303 sp = strchr(name_buf, '-');
5304 if (!sp)
5305 sp = strchrnul(name_buf, '\n');
5306 *sp = '%';
5307 *(sp + 1) = '\0';
5308
5309 fclose(input);
5310
5311 sprintf(path, "cpuidle/state%d/time", state);
5312
Len Browndd778a52017-02-21 23:21:13 -05005313 if (is_deferred_skip(name_buf))
5314 continue;
5315
Len Brown41618e62017-02-09 18:25:22 -05005316 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
5317 FORMAT_PERCENT, SYSFS_PERCPU);
5318 }
5319
Len Brown0748eaf2018-06-01 12:38:29 -04005320 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005321
5322 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5323 base_cpu, state);
5324 input = fopen(path, "r");
5325 if (input == NULL)
5326 continue;
5327 fgets(name_buf, sizeof(name_buf), input);
5328 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5329 sp = strchr(name_buf, '-');
5330 if (!sp)
5331 sp = strchrnul(name_buf, '\n');
5332 *sp = '\0';
5333 fclose(input);
5334
5335 sprintf(path, "cpuidle/state%d/usage", state);
5336
Len Browndd778a52017-02-21 23:21:13 -05005337 if (is_deferred_skip(name_buf))
5338 continue;
5339
Len Brown41618e62017-02-09 18:25:22 -05005340 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
5341 FORMAT_DELTA, SYSFS_PERCPU);
5342 }
5343
5344}
5345
Len Brown1ef7d212017-02-10 23:54:15 -05005346
5347/*
5348 * parse cpuset with following syntax
5349 * 1,2,4..6,8-10 and set bits in cpu_subset
5350 */
5351void parse_cpu_command(char *optarg)
5352{
5353 unsigned int start, end;
5354 char *next;
5355
Len Brown4e4e1e72017-02-21 22:33:42 -05005356 if (!strcmp(optarg, "core")) {
5357 if (cpu_subset)
5358 goto error;
5359 show_core_only++;
5360 return;
5361 }
5362 if (!strcmp(optarg, "package")) {
5363 if (cpu_subset)
5364 goto error;
5365 show_pkg_only++;
5366 return;
5367 }
5368 if (show_core_only || show_pkg_only)
5369 goto error;
5370
Len Brown1ef7d212017-02-10 23:54:15 -05005371 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5372 if (cpu_subset == NULL)
5373 err(3, "CPU_ALLOC");
5374 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5375
5376 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5377
5378 next = optarg;
5379
5380 while (next && *next) {
5381
5382 if (*next == '-') /* no negative cpu numbers */
5383 goto error;
5384
5385 start = strtoul(next, &next, 10);
5386
5387 if (start >= CPU_SUBSET_MAXCPUS)
5388 goto error;
5389 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5390
5391 if (*next == '\0')
5392 break;
5393
5394 if (*next == ',') {
5395 next += 1;
5396 continue;
5397 }
5398
5399 if (*next == '-') {
5400 next += 1; /* start range */
5401 } else if (*next == '.') {
5402 next += 1;
5403 if (*next == '.')
5404 next += 1; /* start range */
5405 else
5406 goto error;
5407 }
5408
5409 end = strtoul(next, &next, 10);
5410 if (end <= start)
5411 goto error;
5412
5413 while (++start <= end) {
5414 if (start >= CPU_SUBSET_MAXCPUS)
5415 goto error;
5416 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5417 }
5418
5419 if (*next == ',')
5420 next += 1;
5421 else if (*next != '\0')
5422 goto error;
5423 }
5424
5425 return;
5426
5427error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005428 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5429 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005430 exit(-1);
5431}
5432
Len Brown812db3f2017-02-10 00:25:41 -05005433
Len Brown103a8fe2010-10-22 23:53:03 -04005434void cmdline(int argc, char **argv)
5435{
5436 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005437 int option_index = 0;
5438 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005439 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005440 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005441 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005442 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005443 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005444 {"interval", required_argument, 0, 'i'},
Chen Yu023fe0a2018-04-26 08:41:03 +08005445 {"num_iterations", required_argument, 0, 'n'},
Len Brownd8af6f52015-02-10 01:56:38 -05005446 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005447 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005448 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005449 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005450 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005451 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005452 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005453 {"Summary", no_argument, 0, 'S'},
5454 {"TCC", required_argument, 0, 'T'},
5455 {"version", no_argument, 0, 'v' },
5456 {0, 0, 0, 0 }
5457 };
Len Brown103a8fe2010-10-22 23:53:03 -04005458
5459 progname = argv[0];
5460
Chen Yu023fe0a2018-04-26 08:41:03 +08005461 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005462 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005463 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005464 case 'a':
5465 parse_add_command(optarg);
5466 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005467 case 'c':
5468 parse_cpu_command(optarg);
5469 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005470 case 'D':
5471 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005472 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005473 case 'e':
5474 /* --enable specified counter */
Len Brown4c2122d2018-06-06 17:44:48 -04005475 bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
Len Brown3f44a5c2017-10-17 15:42:56 -04005476 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005477 case 'd':
5478 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005479 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005480 break;
Len Brown812db3f2017-02-10 00:25:41 -05005481 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005482 /*
5483 * --hide: do not show those specified
5484 * multiple invocations simply clear more bits in enabled mask
5485 */
5486 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005487 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005488 case 'h':
5489 default:
5490 help();
5491 exit(1);
5492 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005493 {
5494 double interval = strtod(optarg, NULL);
5495
5496 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005497 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005498 interval);
5499 exit(2);
5500 }
5501
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005502 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005503 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005504 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005505 }
Len Brown889facb2012-11-08 00:48:57 -05005506 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005507 case 'J':
5508 rapl_joules++;
5509 break;
Len Brownc8ade362017-02-15 17:15:11 -05005510 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005511 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005512 list_header_only++;
5513 quiet++;
5514 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005515 case 'o':
5516 outf = fopen_or_die(optarg, "w");
5517 break;
Len Brown96e47152017-01-21 02:26:00 -05005518 case 'q':
5519 quiet = 1;
5520 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08005521 case 'n':
5522 num_iterations = strtod(optarg, NULL);
5523
5524 if (num_iterations <= 0) {
5525 fprintf(outf, "iterations %d should be positive number\n",
5526 num_iterations);
5527 exit(2);
5528 }
5529 break;
Len Brown812db3f2017-02-10 00:25:41 -05005530 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005531 /*
5532 * --show: show only those specified
5533 * The 1st invocation will clear and replace the enabled mask
5534 * subsequent invocations can add to it.
5535 */
5536 if (shown == 0)
5537 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5538 else
5539 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5540 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005541 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005542 case 'S':
5543 summary_only++;
5544 break;
5545 case 'T':
5546 tcc_activation_temp_override = atoi(optarg);
5547 break;
5548 case 'v':
5549 print_version();
5550 exit(0);
5551 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005552 }
5553 }
5554}
5555
5556int main(int argc, char **argv)
5557{
Len Brownb7d8c142016-02-13 23:36:17 -05005558 outf = stderr;
Len Brown103a8fe2010-10-22 23:53:03 -04005559 cmdline(argc, argv);
5560
Len Brown96e47152017-01-21 02:26:00 -05005561 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005562 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005563
Len Brown41618e62017-02-09 18:25:22 -05005564 probe_sysfs();
5565
Len Brown103a8fe2010-10-22 23:53:03 -04005566 turbostat_init();
5567
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005568 /* dump counters and exit */
5569 if (dump_only)
5570 return get_and_dump_counters();
5571
Len Brownc8ade362017-02-15 17:15:11 -05005572 /* list header and exit */
5573 if (list_header_only) {
5574 print_header(",");
5575 flush_output_stdout();
5576 return 0;
5577 }
5578
Len Brown103a8fe2010-10-22 23:53:03 -04005579 /*
5580 * if any params left, it must be a command to fork
5581 */
5582 if (argc - optind)
5583 return fork_it(argv + optind);
5584 else
5585 turbostat_loop();
5586
5587 return 0;
5588}