blob: fa7e836cdfef7df88639e06d160ce9c5b4e5ea0b [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
Len Brownb9ad8ee2017-07-19 19:28:37 -040032#include <sys/select.h>
Len Brown103a8fe2010-10-22 23:53:03 -040033#include <sys/resource.h>
34#include <fcntl.h>
35#include <signal.h>
36#include <sys/time.h>
37#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050038#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040039#include <dirent.h>
40#include <string.h>
41#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040042#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050043#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070044#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040045#include <linux/capability.h>
46#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040047
Len Brown103a8fe2010-10-22 23:53:03 -040048char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050049FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050050int *fd_percpu;
Len Brownb9ad8ee2017-07-19 19:28:37 -040051struct timeval interval_tv = {5, 0};
Artem Bityutskiy47936f92017-10-04 15:01:47 +030052struct timespec interval_ts = {5, 0};
Len Brownb9ad8ee2017-07-19 19:28:37 -040053struct timespec one_msec = {0, 1000000};
Chen Yu023fe0a2018-04-26 08:41:03 +080054unsigned int num_iterations;
Len Brownd8af6f52015-02-10 01:56:38 -050055unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050056unsigned int quiet;
Len Brown3f44a5c2017-10-17 15:42:56 -040057unsigned int shown;
Len Brown0de6c0d2017-02-15 21:45:40 -050058unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050059unsigned int rapl_joules;
60unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050061unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050062unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040063unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070064unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050065unsigned int do_slm_cstates;
Srinivas Pandruvada997e5392018-05-31 10:39:07 -070066unsigned int do_cnl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050067unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040068unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050069unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040070unsigned int do_irtl_snb;
71unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050072unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040073unsigned int genuine_intel;
74unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050075unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050076unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020077unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040078double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040079double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040080unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040081double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040082unsigned int show_pkg_only;
83unsigned int show_core_only;
84char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050085unsigned int do_rapl;
86unsigned int do_dts;
87unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050088unsigned long long gfx_cur_rc6_ms;
Len Brownbe0e54c2018-06-01 12:35:53 -040089unsigned long long cpuidle_cur_cpu_lpi_us;
90unsigned long long cpuidle_cur_sys_lpi_us;
Len Brown27d47352016-02-27 00:37:54 -050091unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050092unsigned int tcc_activation_temp;
93unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050094double rapl_power_units, rapl_time_units;
95double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050096double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040097unsigned int do_core_perf_limit_reasons;
Artem Bityutskiyac980e12017-09-05 15:14:08 +030098unsigned int has_automatic_cstate_conversion;
Len Brown3a9a9412014-08-15 02:39:52 -040099unsigned int do_gfx_perf_limit_reasons;
100unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -0400101unsigned int crystal_hz;
102unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400103int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400104double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500105unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
106 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
107unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
108unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
109unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
110unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500111unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500112
Len Browne6f9bb32013-12-03 02:19:19 -0500113#define RAPL_PKG (1 << 0)
114 /* 0x610 MSR_PKG_POWER_LIMIT */
115 /* 0x611 MSR_PKG_ENERGY_STATUS */
116#define RAPL_PKG_PERF_STATUS (1 << 1)
117 /* 0x613 MSR_PKG_PERF_STATUS */
118#define RAPL_PKG_POWER_INFO (1 << 2)
119 /* 0x614 MSR_PKG_POWER_INFO */
120
121#define RAPL_DRAM (1 << 3)
122 /* 0x618 MSR_DRAM_POWER_LIMIT */
123 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500124#define RAPL_DRAM_PERF_STATUS (1 << 4)
125 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400126#define RAPL_DRAM_POWER_INFO (1 << 5)
127 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500128
Jacob Pan91484942016-06-16 09:48:20 -0700129#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500130 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400131#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500132 /* 0x63a MSR_PP0_POLICY */
133
Len Brown0b2bb692015-03-26 00:50:30 -0400134#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500135 /* 0x640 MSR_PP1_POWER_LIMIT */
136 /* 0x641 MSR_PP1_ENERGY_STATUS */
137 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700138
139#define RAPL_CORES_ENERGY_STATUS (1 << 9)
140 /* 0x639 MSR_PP0_ENERGY_STATUS */
141#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500142#define TJMAX_DEFAULT 100
143
144#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brown388e9c82016-12-22 23:57:55 -0500146/*
147 * buffer size used by sscanf() for added column names
148 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
149 */
150#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500151#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500152
Len Brown103a8fe2010-10-22 23:53:03 -0400153int backwards_count;
154char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400155
Len Brown1ef7d212017-02-10 23:54:15 -0500156#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
157cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
158size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown0748eaf2018-06-01 12:38:29 -0400159#define MAX_ADDED_COUNTERS 8
160#define MAX_ADDED_THREAD_COUNTERS 24
Len Brown0e2d8f02018-06-01 22:08:58 -0400161#define BITMASK_SIZE 32
Len Brown103a8fe2010-10-22 23:53:03 -0400162
Len Brownc98d5d92012-06-04 00:56:40 -0400163struct thread_data {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700164 struct timeval tv_begin;
165 struct timeval tv_end;
Len Brownc98d5d92012-06-04 00:56:40 -0400166 unsigned long long tsc;
167 unsigned long long aperf;
168 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500169 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500170 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500171 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400172 unsigned int cpu_id;
173 unsigned int flags;
174#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
175#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown0748eaf2018-06-01 12:38:29 -0400176 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400177} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400178
Len Brownc98d5d92012-06-04 00:56:40 -0400179struct core_data {
180 unsigned long long c3;
181 unsigned long long c6;
182 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500183 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500184 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400185 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500186 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400187} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400188
Len Brownc98d5d92012-06-04 00:56:40 -0400189struct pkg_data {
190 unsigned long long pc2;
191 unsigned long long pc3;
192 unsigned long long pc6;
193 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800194 unsigned long long pc8;
195 unsigned long long pc9;
196 unsigned long long pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -0400197 unsigned long long cpu_lpi;
198 unsigned long long sys_lpi;
Len Brown0b2bb692015-03-26 00:50:30 -0400199 unsigned long long pkg_wtd_core_c0;
200 unsigned long long pkg_any_core_c0;
201 unsigned long long pkg_any_gfxe_c0;
202 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400203 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500204 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400205 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500206 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
207 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
208 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
209 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
210 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
211 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
212 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500213 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400214} *package_even, *package_odd;
215
216#define ODD_COUNTERS thread_odd, core_odd, package_odd
217#define EVEN_COUNTERS thread_even, core_even, package_even
218
219#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400220 (thread_base + (pkg_no) * topo.cores_per_node * \
221 topo.threads_per_core + \
222 (core_no) * topo.threads_per_core + (thread_no))
Len Brownc98d5d92012-06-04 00:56:40 -0400223#define GET_CORE(core_base, core_no, pkg_no) \
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400224 (core_base + (pkg_no) * topo.cores_per_node + (core_no))
Len Brownc98d5d92012-06-04 00:56:40 -0400225#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
226
Len Brown388e9c82016-12-22 23:57:55 -0500227enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500228enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500229enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
230
231struct msr_counter {
232 unsigned int msr_num;
233 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500234 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500235 unsigned int width;
236 enum counter_type type;
237 enum counter_format format;
238 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500239 unsigned int flags;
240#define FLAGS_HIDE (1 << 0)
241#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500242#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500243};
244
245struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500246 unsigned int added_thread_counters;
247 unsigned int added_core_counters;
248 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500249 struct msr_counter *tp;
250 struct msr_counter *cp;
251 struct msr_counter *pp;
252} sys;
253
Len Brownc98d5d92012-06-04 00:56:40 -0400254struct system_summary {
255 struct thread_data threads;
256 struct core_data cores;
257 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500258} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400259
Len Brown0e2d8f02018-06-01 22:08:58 -0400260struct cpu_topology {
261 int physical_package_id;
262 int logical_cpu_id;
Prarit Bhargavaef605742018-06-01 10:04:30 -0400263 int physical_node_id;
264 int logical_node_id; /* 0-based count within the package */
Len Brown0e2d8f02018-06-01 22:08:58 -0400265 int physical_core_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -0400266 int thread_id;
Len Brown0e2d8f02018-06-01 22:08:58 -0400267 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
268} *cpus;
Len Brownc98d5d92012-06-04 00:56:40 -0400269
270struct topo_params {
271 int num_packages;
272 int num_cpus;
273 int num_cores;
274 int max_cpu_num;
Prarit Bhargavaef605742018-06-01 10:04:30 -0400275 int max_node_num;
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400276 int nodes_per_pkg;
277 int cores_per_node;
278 int threads_per_core;
Len Brownc98d5d92012-06-04 00:56:40 -0400279} topo;
280
281struct timeval tv_even, tv_odd, tv_delta;
282
Len Brown562a2d32016-02-26 23:48:05 -0500283int *irq_column_2_cpu; /* /proc/interrupts column numbers */
284int *irqs_per_cpu; /* indexed by cpu_num */
285
Len Brownc98d5d92012-06-04 00:56:40 -0400286void setup_all_buffers(void);
287
288int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400289{
Len Brownc98d5d92012-06-04 00:56:40 -0400290 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400291}
Len Brown88c32812012-03-29 21:44:40 -0400292/*
Len Brownc98d5d92012-06-04 00:56:40 -0400293 * run func(thread, core, package) in topology order
294 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400295 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400296
Len Brownc98d5d92012-06-04 00:56:40 -0400297int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
298 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400299{
Len Brownc98d5d92012-06-04 00:56:40 -0400300 int retval, pkg_no, core_no, thread_no;
301
302 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400303 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
Len Brownc98d5d92012-06-04 00:56:40 -0400304 for (thread_no = 0; thread_no <
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400305 topo.threads_per_core; ++thread_no) {
Len Brownc98d5d92012-06-04 00:56:40 -0400306 struct thread_data *t;
307 struct core_data *c;
308 struct pkg_data *p;
309
310 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
311
312 if (cpu_is_not_present(t->cpu_id))
313 continue;
314
315 c = GET_CORE(core_base, core_no, pkg_no);
316 p = GET_PKG(pkg_base, pkg_no);
317
318 retval = func(t, c, p);
319 if (retval)
320 return retval;
321 }
322 }
323 }
324 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400325}
326
327int cpu_migrate(int cpu)
328{
Len Brownc98d5d92012-06-04 00:56:40 -0400329 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
330 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
331 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400332 return -1;
333 else
334 return 0;
335}
Len Brown36229892016-02-26 20:51:02 -0500336int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400337{
Len Brown103a8fe2010-10-22 23:53:03 -0400338 char pathname[32];
339 int fd;
340
Len Brown36229892016-02-26 20:51:02 -0500341 fd = fd_percpu[cpu];
342
343 if (fd)
344 return fd;
345
Len Brown103a8fe2010-10-22 23:53:03 -0400346 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
347 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400348 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400349 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 -0400350
Len Brown36229892016-02-26 20:51:02 -0500351 fd_percpu[cpu] = fd;
352
353 return fd;
354}
355
356int get_msr(int cpu, off_t offset, unsigned long long *msr)
357{
358 ssize_t retval;
359
360 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400361
Len Brown98481e72014-08-15 00:36:50 -0400362 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500363 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400364
365 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400366}
367
Len Brownfc04cc62014-02-06 00:55:19 -0500368/*
Len Brown812db3f2017-02-10 00:25:41 -0500369 * Each string in this array is compared in --show and --hide cmdline.
370 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500371 */
Len Brown812db3f2017-02-10 00:25:41 -0500372struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400373 { 0x0, "usec" },
374 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500375 { 0x0, "Package" },
376 { 0x0, "Avg_MHz" },
377 { 0x0, "Bzy_MHz" },
378 { 0x0, "TSC_MHz" },
379 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500380 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500381 { 0x0, "Busy%" },
382 { 0x0, "CPU%c1" },
383 { 0x0, "CPU%c3" },
384 { 0x0, "CPU%c6" },
385 { 0x0, "CPU%c7" },
386 { 0x0, "ThreadC" },
387 { 0x0, "CoreTmp" },
388 { 0x0, "CoreCnt" },
389 { 0x0, "PkgTmp" },
390 { 0x0, "GFX%rc6" },
391 { 0x0, "GFXMHz" },
392 { 0x0, "Pkg%pc2" },
393 { 0x0, "Pkg%pc3" },
394 { 0x0, "Pkg%pc6" },
395 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500396 { 0x0, "Pkg%pc8" },
397 { 0x0, "Pkg%pc9" },
Len Brown4bd1f8f2018-01-28 23:42:42 -0500398 { 0x0, "Pk%pc10" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400399 { 0x0, "CPU%LPI" },
400 { 0x0, "SYS%LPI" },
Len Brown812db3f2017-02-10 00:25:41 -0500401 { 0x0, "PkgWatt" },
402 { 0x0, "CorWatt" },
403 { 0x0, "GFXWatt" },
404 { 0x0, "PkgCnt" },
405 { 0x0, "RAMWatt" },
406 { 0x0, "PKG_%" },
407 { 0x0, "RAM_%" },
408 { 0x0, "Pkg_J" },
409 { 0x0, "Cor_J" },
410 { 0x0, "GFX_J" },
411 { 0x0, "RAM_J" },
412 { 0x0, "Core" },
413 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500414 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500415 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400416 { 0x0, "Totl%C0" },
417 { 0x0, "Any%C0" },
418 { 0x0, "GFX%C0" },
419 { 0x0, "CPUGFX%" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400420 { 0x0, "Node%" },
Len Brown812db3f2017-02-10 00:25:41 -0500421};
422
Len Browna99d8732017-05-20 20:11:55 -0400423
424
Len Brown812db3f2017-02-10 00:25:41 -0500425#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400426#define BIC_USEC (1ULL << 0)
427#define BIC_TOD (1ULL << 1)
428#define BIC_Package (1ULL << 2)
429#define BIC_Avg_MHz (1ULL << 3)
430#define BIC_Bzy_MHz (1ULL << 4)
431#define BIC_TSC_MHz (1ULL << 5)
432#define BIC_IRQ (1ULL << 6)
433#define BIC_SMI (1ULL << 7)
434#define BIC_Busy (1ULL << 8)
435#define BIC_CPU_c1 (1ULL << 9)
436#define BIC_CPU_c3 (1ULL << 10)
437#define BIC_CPU_c6 (1ULL << 11)
438#define BIC_CPU_c7 (1ULL << 12)
439#define BIC_ThreadC (1ULL << 13)
440#define BIC_CoreTmp (1ULL << 14)
441#define BIC_CoreCnt (1ULL << 15)
442#define BIC_PkgTmp (1ULL << 16)
443#define BIC_GFX_rc6 (1ULL << 17)
444#define BIC_GFXMHz (1ULL << 18)
445#define BIC_Pkgpc2 (1ULL << 19)
446#define BIC_Pkgpc3 (1ULL << 20)
447#define BIC_Pkgpc6 (1ULL << 21)
448#define BIC_Pkgpc7 (1ULL << 22)
449#define BIC_Pkgpc8 (1ULL << 23)
450#define BIC_Pkgpc9 (1ULL << 24)
451#define BIC_Pkgpc10 (1ULL << 25)
Len Brownbe0e54c2018-06-01 12:35:53 -0400452#define BIC_CPU_LPI (1ULL << 26)
453#define BIC_SYS_LPI (1ULL << 27)
Len Brown3f44a5c2017-10-17 15:42:56 -0400454#define BIC_PkgWatt (1ULL << 26)
455#define BIC_CorWatt (1ULL << 27)
456#define BIC_GFXWatt (1ULL << 28)
457#define BIC_PkgCnt (1ULL << 29)
458#define BIC_RAMWatt (1ULL << 30)
459#define BIC_PKG__ (1ULL << 31)
460#define BIC_RAM__ (1ULL << 32)
461#define BIC_Pkg_J (1ULL << 33)
462#define BIC_Cor_J (1ULL << 34)
463#define BIC_GFX_J (1ULL << 35)
464#define BIC_RAM_J (1ULL << 36)
465#define BIC_Core (1ULL << 37)
466#define BIC_CPU (1ULL << 38)
467#define BIC_Mod_c6 (1ULL << 39)
468#define BIC_sysfs (1ULL << 40)
469#define BIC_Totl_c0 (1ULL << 41)
470#define BIC_Any_c0 (1ULL << 42)
471#define BIC_GFX_c0 (1ULL << 43)
472#define BIC_CPUGFX (1ULL << 44)
Len Brown812db3f2017-02-10 00:25:41 -0500473
Len Brown3f44a5c2017-10-17 15:42:56 -0400474#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
475
476unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
477unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500478
479#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400480#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500481#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500482#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500483
Len Brown3f44a5c2017-10-17 15:42:56 -0400484
Len Browndd778a52017-02-21 23:21:13 -0500485#define MAX_DEFERRED 16
486char *deferred_skip_names[MAX_DEFERRED];
487int deferred_skip_index;
488
489/*
490 * HIDE_LIST - hide this list of counters, show the rest [default]
491 * SHOW_LIST - show this list of counters, hide the rest
492 */
493enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
494
495void help(void)
496{
497 fprintf(outf,
498 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
499 "\n"
500 "Turbostat forks the specified COMMAND and prints statistics\n"
501 "when COMMAND completes.\n"
502 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
503 "to print statistics, until interrupted.\n"
504 "--add add a counter\n"
505 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
506 "--cpu cpu-set limit output to summary plus cpu-set:\n"
507 " {core | package | j,k,l..m,n-p }\n"
508 "--quiet skip decoding system configuration header\n"
Len Brownb9ad8ee2017-07-19 19:28:37 -0400509 "--interval sec.subsec Override default 5-second measurement interval\n"
Len Browndd778a52017-02-21 23:21:13 -0500510 "--help print this help message\n"
511 "--list list column headers only\n"
Chen Yu023fe0a2018-04-26 08:41:03 +0800512 "--num_iterations num number of the measurement iterations\n"
Len Browndd778a52017-02-21 23:21:13 -0500513 "--out file create or truncate \"file\" for all output\n"
514 "--version print version information\n"
515 "\n"
516 "For more help, run \"man turbostat\"\n");
517}
518
Len Brown812db3f2017-02-10 00:25:41 -0500519/*
520 * bic_lookup
521 * for all the strings in comma separate name_list,
522 * set the approprate bit in return value.
523 */
Len Browndd778a52017-02-21 23:21:13 -0500524unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500525{
526 int i;
527 unsigned long long retval = 0;
528
529 while (name_list) {
530 char *comma;
531
532 comma = strchr(name_list, ',');
533
534 if (comma)
535 *comma = '\0';
536
Len Brown3f44a5c2017-10-17 15:42:56 -0400537 if (!strcmp(name_list, "all"))
538 return ~0;
539
Len Brown812db3f2017-02-10 00:25:41 -0500540 for (i = 0; i < MAX_BIC; ++i) {
541 if (!strcmp(name_list, bic[i].name)) {
542 retval |= (1ULL << i);
543 break;
544 }
545 }
546 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500547 if (mode == SHOW_LIST) {
548 fprintf(stderr, "Invalid counter name: %s\n", name_list);
549 exit(-1);
550 }
551 deferred_skip_names[deferred_skip_index++] = name_list;
552 if (debug)
553 fprintf(stderr, "deferred \"%s\"\n", name_list);
554 if (deferred_skip_index >= MAX_DEFERRED) {
555 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
556 MAX_DEFERRED, name_list);
557 help();
558 exit(1);
559 }
Len Brown812db3f2017-02-10 00:25:41 -0500560 }
561
562 name_list = comma;
563 if (name_list)
564 name_list++;
565
566 }
567 return retval;
568}
Len Brownfc04cc62014-02-06 00:55:19 -0500569
Len Browndd778a52017-02-21 23:21:13 -0500570
Len Brownc8ade362017-02-15 17:15:11 -0500571void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400572{
Len Brown388e9c82016-12-22 23:57:55 -0500573 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500574 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500575
Len Brown3f44a5c2017-10-17 15:42:56 -0400576 if (DO_BIC(BIC_USEC))
577 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
578 if (DO_BIC(BIC_TOD))
579 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500580 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500581 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500582 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500583 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500584 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500585 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500586 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500587 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500588 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500589 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500590 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500591 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500592 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500593 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500594
Len Brown0de6c0d2017-02-15 21:45:40 -0500595 if (DO_BIC(BIC_IRQ)) {
596 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500597 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500598 else
Len Brown6168c2e2017-02-16 23:07:51 -0500599 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500600 }
601
Len Brown812db3f2017-02-10 00:25:41 -0500602 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500603 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500604
Len Brown388e9c82016-12-22 23:57:55 -0500605 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500606
Len Brown388e9c82016-12-22 23:57:55 -0500607 if (mp->format == FORMAT_RAW) {
608 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500609 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500610 else
Len Browndd778a52017-02-21 23:21:13 -0500611 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500612 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500613 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500614 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500615 else
Len Browndd778a52017-02-21 23:21:13 -0500616 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500617 }
618 }
619
Len Brown41618e62017-02-09 18:25:22 -0500620 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500621 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700622 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500623 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500624 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500625 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500626 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500627 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500628
Len Brown0539ba12017-02-10 00:27:20 -0500629 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500630 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500631
Len Brown812db3f2017-02-10 00:25:41 -0500632 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500633 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500634
635 for (mp = sys.cp; mp; mp = mp->next) {
636 if (mp->format == FORMAT_RAW) {
637 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500638 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500639 else
Len Brownc8ade362017-02-15 17:15:11 -0500640 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500641 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500642 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
643 outp += sprintf(outp, "%s%8s", delim, mp->name);
644 else
645 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500646 }
647 }
648
Len Brown812db3f2017-02-10 00:25:41 -0500649 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500650 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500651
Len Brown812db3f2017-02-10 00:25:41 -0500652 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500653 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500654
Len Brown812db3f2017-02-10 00:25:41 -0500655 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500656 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500657
Len Browna99d8732017-05-20 20:11:55 -0400658 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500659 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400660 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500661 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400662 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500663 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400664 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500665 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400666
Len Brown0f47c082017-01-27 00:50:45 -0500667 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500668 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500669 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500670 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500671 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500672 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500673 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500674 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500675 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500676 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500677 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500678 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500679 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500680 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brownbe0e54c2018-06-01 12:35:53 -0400681 if (DO_BIC(BIC_CPU_LPI))
682 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
683 if (DO_BIC(BIC_SYS_LPI))
684 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400685
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800686 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500687 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500688 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500689 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500690 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500691 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500692 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500693 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500694 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500695 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500696 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500697 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500698 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500699 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500700 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500701 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500702 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500703 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500704 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500705 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500706 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500707 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500708 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500709 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500710 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500711 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800712 }
Len Brown388e9c82016-12-22 23:57:55 -0500713 for (mp = sys.pp; mp; mp = mp->next) {
714 if (mp->format == FORMAT_RAW) {
715 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500716 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500717 else
Len Brownc8ade362017-02-15 17:15:11 -0500718 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500719 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500720 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
721 outp += sprintf(outp, "%s%8s", delim, mp->name);
722 else
723 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500724 }
725 }
726
Len Brownc98d5d92012-06-04 00:56:40 -0400727 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400728}
729
Len Brownc98d5d92012-06-04 00:56:40 -0400730int dump_counters(struct thread_data *t, struct core_data *c,
731 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400732{
Len Brown388e9c82016-12-22 23:57:55 -0500733 int i;
734 struct msr_counter *mp;
735
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200736 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400737
Len Brownc98d5d92012-06-04 00:56:40 -0400738 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200739 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
740 t->cpu_id, t->flags);
741 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
742 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
743 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
744 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500745
Len Brown812db3f2017-02-10 00:25:41 -0500746 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500747 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500748 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500749 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500750
751 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
752 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
753 i, mp->msr_num, t->counter[i]);
754 }
Len Brownc98d5d92012-06-04 00:56:40 -0400755 }
Len Brown103a8fe2010-10-22 23:53:03 -0400756
Len Brownc98d5d92012-06-04 00:56:40 -0400757 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200758 outp += sprintf(outp, "core: %d\n", c->core_id);
759 outp += sprintf(outp, "c3: %016llX\n", c->c3);
760 outp += sprintf(outp, "c6: %016llX\n", c->c6);
761 outp += sprintf(outp, "c7: %016llX\n", c->c7);
762 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500763
764 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
765 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
766 i, mp->msr_num, c->counter[i]);
767 }
Len Brown0539ba12017-02-10 00:27:20 -0500768 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400769 }
770
771 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200772 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400773
774 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
775 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
776 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
777 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
778
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200779 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500780 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500781 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500782 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500783 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500784 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500785 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200786 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
787 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
788 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
Len Brownbe0e54c2018-06-01 12:35:53 -0400789 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
790 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
791 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200792 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
793 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
794 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
795 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
796 outp += sprintf(outp, "Throttle PKG: %0X\n",
797 p->rapl_pkg_perf_status);
798 outp += sprintf(outp, "Throttle RAM: %0X\n",
799 p->rapl_dram_perf_status);
800 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500801
802 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
803 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
804 i, mp->msr_num, p->counter[i]);
805 }
Len Brownc98d5d92012-06-04 00:56:40 -0400806 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200807
808 outp += sprintf(outp, "\n");
809
Len Brownc98d5d92012-06-04 00:56:40 -0400810 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400811}
812
Len Browne23da032012-02-06 18:37:16 -0500813/*
814 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500815 */
Len Brownc98d5d92012-06-04 00:56:40 -0400816int format_counters(struct thread_data *t, struct core_data *c,
817 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400818{
Len Brown008d396e2017-02-10 00:29:51 -0500819 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500820 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500821 int i;
822 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500823 char *delim = "\t";
824 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400825
Len Brownc98d5d92012-06-04 00:56:40 -0400826 /* if showing only 1st thread in core and this isn't one, bail out */
827 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
828 return 0;
829
830 /* if showing only 1st thread in pkg and this isn't one, bail out */
831 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
832 return 0;
833
Len Brown1ef7d212017-02-10 23:54:15 -0500834 /*if not summary line and --cpu is used */
835 if ((t != &average.threads) &&
836 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
837 return 0;
838
Len Brown3f44a5c2017-10-17 15:42:56 -0400839 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700840 /* on each row, print how many usec each timestamp took to gather */
841 struct timeval tv;
842
843 timersub(&t->tv_end, &t->tv_begin, &tv);
844 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
845 }
846
Len Brown3f44a5c2017-10-17 15:42:56 -0400847 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
848 if (DO_BIC(BIC_TOD))
849 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
850
Len Brown103a8fe2010-10-22 23:53:03 -0400851 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
852
Len Brown008d396e2017-02-10 00:29:51 -0500853 tsc = t->tsc * tsc_tweak;
854
Len Brownc98d5d92012-06-04 00:56:40 -0400855 /* topo columns, print blanks on 1st (average) line */
856 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500857 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500858 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500859 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500860 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500861 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500862 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400863 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500864 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400865 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500866 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400867 else
Len Brown6168c2e2017-02-16 23:07:51 -0500868 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400869 }
Len Brown812db3f2017-02-10 00:25:41 -0500870 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400871 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500872 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400873 else
Len Brown6168c2e2017-02-16 23:07:51 -0500874 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400875 }
Len Brown812db3f2017-02-10 00:25:41 -0500876 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500877 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400878 }
Len Brownfc04cc62014-02-06 00:55:19 -0500879
Len Brown812db3f2017-02-10 00:25:41 -0500880 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500881 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500882 1.0 / units * t->aperf / interval_float);
883
Len Brown812db3f2017-02-10 00:25:41 -0500884 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500885 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400886
Len Brown812db3f2017-02-10 00:25:41 -0500887 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400888 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500889 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400890 else
Len Brown6168c2e2017-02-16 23:07:51 -0500891 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500892 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400893 }
Len Brown103a8fe2010-10-22 23:53:03 -0400894
Len Brown812db3f2017-02-10 00:25:41 -0500895 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500896 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400897
Len Brown562a2d32016-02-26 23:48:05 -0500898 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500899 if (DO_BIC(BIC_IRQ)) {
900 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500901 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500902 else
Len Brown6168c2e2017-02-16 23:07:51 -0500903 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500904 }
Len Brown562a2d32016-02-26 23:48:05 -0500905
Len Brown1cc21f72015-02-23 00:34:57 -0500906 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500907 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500908 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500909
Len Brown678a3bd2017-02-09 22:22:13 -0500910 /* Added counters */
911 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
912 if (mp->format == FORMAT_RAW) {
913 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500914 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500915 else
Len Brown6168c2e2017-02-16 23:07:51 -0500916 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500917 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500918 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500919 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500920 else
Len Brown6168c2e2017-02-16 23:07:51 -0500921 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500922 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500923 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500924 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500925 else
Len Brown6168c2e2017-02-16 23:07:51 -0500926 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500927 }
928 }
929
Len Brown41618e62017-02-09 18:25:22 -0500930 /* C1 */
931 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500932 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500933
934
Len Brownc98d5d92012-06-04 00:56:40 -0400935 /* print per-core data only for 1st thread in core */
936 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
937 goto done;
938
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700939 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500940 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500941 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500942 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500943 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500944 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400945
Len Brown0539ba12017-02-10 00:27:20 -0500946 /* Mod%c6 */
947 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500948 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500949
Len Brown812db3f2017-02-10 00:25:41 -0500950 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500951 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500952
Len Brown388e9c82016-12-22 23:57:55 -0500953 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
954 if (mp->format == FORMAT_RAW) {
955 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500956 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500957 else
Len Brown6168c2e2017-02-16 23:07:51 -0500958 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500959 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500960 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500961 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500962 else
Len Brown6168c2e2017-02-16 23:07:51 -0500963 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500964 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500965 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500966 }
967 }
968
Len Brownc98d5d92012-06-04 00:56:40 -0400969 /* print per-package data only for 1st core in package */
970 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
971 goto done;
972
Len Brown0b2bb692015-03-26 00:50:30 -0400973 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500974 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500975 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500976
Len Brownfdf676e2016-02-27 01:28:12 -0500977 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500978 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400979 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500980 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400981 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500982 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400983 p->gfx_rc6_ms / 10.0 / interval_float);
984 }
985 }
Len Brownfdf676e2016-02-27 01:28:12 -0500986
Len Brown27d47352016-02-27 00:37:54 -0500987 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500988 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500989 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500990
Len Brown0b2bb692015-03-26 00:50:30 -0400991 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -0400992 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500993 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400994 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500995 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400996 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500997 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400998 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500999 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -04001000
Len Brown0f47c082017-01-27 00:50:45 -05001001 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -05001002 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001003 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -05001004 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001005 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -05001006 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001007 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -05001008 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001009 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -05001010 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001011 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -05001012 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001013 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -05001014 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -05001015
Len Brownbe0e54c2018-06-01 12:35:53 -04001016 if (DO_BIC(BIC_CPU_LPI))
1017 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1018 if (DO_BIC(BIC_SYS_LPI))
1019 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1020
Len Brown889facb2012-11-08 00:48:57 -05001021 /*
1022 * If measurement interval exceeds minimum RAPL Joule Counter range,
1023 * indicate that results are suspect by printing "**" in fraction place.
1024 */
Len Brownfc04cc62014-02-06 00:55:19 -05001025 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -05001026 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -05001027 else
Len Browne975db52016-04-06 23:56:02 -04001028 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -05001029
Len Brown812db3f2017-02-10 00:25:41 -05001030 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001031 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001032 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001033 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001034 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001035 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001036 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001037 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001038 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001039 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001040 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001041 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001042 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001043 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001044 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001045 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001046 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001047 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 -05001048 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001049 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 -05001050
Len Brown388e9c82016-12-22 23:57:55 -05001051 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1052 if (mp->format == FORMAT_RAW) {
1053 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001054 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001055 else
Len Brown6168c2e2017-02-16 23:07:51 -05001056 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001057 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001058 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001059 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001060 else
Len Brown6168c2e2017-02-16 23:07:51 -05001061 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001062 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001063 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001064 }
1065 }
1066
Len Brownc98d5d92012-06-04 00:56:40 -04001067done:
Len Brown94d6ab42018-01-27 22:39:21 -05001068 if (*(outp - 1) != '\n')
1069 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001070
1071 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001072}
1073
Len Brownb7d8c142016-02-13 23:36:17 -05001074void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001075{
Len Brownb7d8c142016-02-13 23:36:17 -05001076 FILE *filep;
1077
1078 if (outf == stderr)
1079 filep = stdout;
1080 else
1081 filep = outf;
1082
1083 fputs(output_buffer, filep);
1084 fflush(filep);
1085
Len Brownc98d5d92012-06-04 00:56:40 -04001086 outp = output_buffer;
1087}
Len Brownb7d8c142016-02-13 23:36:17 -05001088void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001089{
Len Brownb7d8c142016-02-13 23:36:17 -05001090 fputs(output_buffer, outf);
1091 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001092 outp = output_buffer;
1093}
1094void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1095{
Len Browne23da032012-02-06 18:37:16 -05001096 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001097
Len Browne23da032012-02-06 18:37:16 -05001098 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001099 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001100
Len Brownc98d5d92012-06-04 00:56:40 -04001101 if (topo.num_cpus > 1)
1102 format_counters(&average.threads, &average.cores,
1103 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001104
Len Browne23da032012-02-06 18:37:16 -05001105 printed = 1;
1106
1107 if (summary_only)
1108 return;
1109
Len Brownc98d5d92012-06-04 00:56:40 -04001110 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001111}
1112
Len Brown889facb2012-11-08 00:48:57 -05001113#define DELTA_WRAP32(new, old) \
1114 if (new > old) { \
1115 old = new - old; \
1116 } else { \
1117 old = 0x100000000 + new - old; \
1118 }
1119
Len Brownba3dec92016-04-22 20:31:46 -04001120int
Len Brownc98d5d92012-06-04 00:56:40 -04001121delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001122{
Len Brown388e9c82016-12-22 23:57:55 -05001123 int i;
1124 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001125
Len Browna99d8732017-05-20 20:11:55 -04001126
1127 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001128 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001129 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001130 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001131 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001132 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001133 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001134 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 -04001135
Len Brownc98d5d92012-06-04 00:56:40 -04001136 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001137 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001138 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001139 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001140 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001141 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001142 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001143 old->pc8 = new->pc8 - old->pc8;
1144 old->pc9 = new->pc9 - old->pc9;
1145 old->pc10 = new->pc10 - old->pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -04001146 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1147 old->sys_lpi = new->sys_lpi - old->sys_lpi;
Len Brown889facb2012-11-08 00:48:57 -05001148 old->pkg_temp_c = new->pkg_temp_c;
1149
Len Brown9185e982016-04-06 17:16:00 -04001150 /* flag an error when rc6 counter resets/wraps */
1151 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1152 old->gfx_rc6_ms = -1;
1153 else
1154 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1155
Len Brown27d47352016-02-27 00:37:54 -05001156 old->gfx_mhz = new->gfx_mhz;
1157
Len Brown889facb2012-11-08 00:48:57 -05001158 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1159 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1160 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1161 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1162 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1163 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001164
Len Brown388e9c82016-12-22 23:57:55 -05001165 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1166 if (mp->format == FORMAT_RAW)
1167 old->counter[i] = new->counter[i];
1168 else
1169 old->counter[i] = new->counter[i] - old->counter[i];
1170 }
1171
Len Brownba3dec92016-04-22 20:31:46 -04001172 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001173}
Len Brown103a8fe2010-10-22 23:53:03 -04001174
Len Brownc98d5d92012-06-04 00:56:40 -04001175void
1176delta_core(struct core_data *new, struct core_data *old)
1177{
Len Brown388e9c82016-12-22 23:57:55 -05001178 int i;
1179 struct msr_counter *mp;
1180
Len Brownc98d5d92012-06-04 00:56:40 -04001181 old->c3 = new->c3 - old->c3;
1182 old->c6 = new->c6 - old->c6;
1183 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001184 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001185 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001186
1187 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1188 if (mp->format == FORMAT_RAW)
1189 old->counter[i] = new->counter[i];
1190 else
1191 old->counter[i] = new->counter[i] - old->counter[i];
1192 }
Len Brownc98d5d92012-06-04 00:56:40 -04001193}
Len Brown103a8fe2010-10-22 23:53:03 -04001194
Len Brownc3ae3312012-06-13 21:31:46 -04001195/*
1196 * old = new - old
1197 */
Len Brownba3dec92016-04-22 20:31:46 -04001198int
Len Brownc98d5d92012-06-04 00:56:40 -04001199delta_thread(struct thread_data *new, struct thread_data *old,
1200 struct core_data *core_delta)
1201{
Len Brown388e9c82016-12-22 23:57:55 -05001202 int i;
1203 struct msr_counter *mp;
1204
Len Brown3f44a5c2017-10-17 15:42:56 -04001205 /*
1206 * the timestamps from start of measurement interval are in "old"
1207 * the timestamp from end of measurement interval are in "new"
1208 * over-write old w/ new so we can print end of interval values
1209 */
1210
1211 old->tv_begin = new->tv_begin;
1212 old->tv_end = new->tv_end;
1213
Len Brownc98d5d92012-06-04 00:56:40 -04001214 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001215
Len Brownc98d5d92012-06-04 00:56:40 -04001216 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001217 if (old->tsc < (1000 * 1000))
1218 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1219 "You can disable all c-states by booting with \"idle=poll\"\n"
1220 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001221
Len Brownc98d5d92012-06-04 00:56:40 -04001222 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001223
Len Brown812db3f2017-02-10 00:25:41 -05001224 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001225 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1226 old->aperf = new->aperf - old->aperf;
1227 old->mperf = new->mperf - old->mperf;
1228 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001229 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001230 }
Len Brownc98d5d92012-06-04 00:56:40 -04001231 }
Len Brown103a8fe2010-10-22 23:53:03 -04001232
Len Brown103a8fe2010-10-22 23:53:03 -04001233
Len Brown144b44b2013-11-09 00:30:16 -05001234 if (use_c1_residency_msr) {
1235 /*
1236 * Some models have a dedicated C1 residency MSR,
1237 * which should be more accurate than the derivation below.
1238 */
1239 } else {
1240 /*
1241 * As counter collection is not atomic,
1242 * it is possible for mperf's non-halted cycles + idle states
1243 * to exceed TSC's all cycles: show c1 = 0% in that case.
1244 */
Len Brown95149362017-04-12 19:44:51 -04001245 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001246 old->c1 = 0;
1247 else {
1248 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001249 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001250 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001251 }
Len Brownc98d5d92012-06-04 00:56:40 -04001252 }
Len Brownc3ae3312012-06-13 21:31:46 -04001253
Len Brownc98d5d92012-06-04 00:56:40 -04001254 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001255 if (debug > 1)
1256 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001257 old->mperf = 1; /* divide by 0 protection */
1258 }
1259
Len Brown812db3f2017-02-10 00:25:41 -05001260 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001261 old->irq_count = new->irq_count - old->irq_count;
1262
Len Brown812db3f2017-02-10 00:25:41 -05001263 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001264 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001265
Len Brown388e9c82016-12-22 23:57:55 -05001266 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1267 if (mp->format == FORMAT_RAW)
1268 old->counter[i] = new->counter[i];
1269 else
1270 old->counter[i] = new->counter[i] - old->counter[i];
1271 }
Len Brownba3dec92016-04-22 20:31:46 -04001272 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001273}
1274
1275int delta_cpu(struct thread_data *t, struct core_data *c,
1276 struct pkg_data *p, struct thread_data *t2,
1277 struct core_data *c2, struct pkg_data *p2)
1278{
Len Brownba3dec92016-04-22 20:31:46 -04001279 int retval = 0;
1280
Len Brownc98d5d92012-06-04 00:56:40 -04001281 /* calculate core delta only for 1st thread in core */
1282 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1283 delta_core(c, c2);
1284
1285 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001286 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1287 if (retval)
1288 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001289
1290 /* calculate package delta only for 1st core in package */
1291 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001292 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001293
Len Brownba3dec92016-04-22 20:31:46 -04001294 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001295}
1296
1297void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1298{
Len Brown388e9c82016-12-22 23:57:55 -05001299 int i;
1300 struct msr_counter *mp;
1301
Len Brown3f44a5c2017-10-17 15:42:56 -04001302 t->tv_begin.tv_sec = 0;
1303 t->tv_begin.tv_usec = 0;
1304 t->tv_end.tv_sec = 0;
1305 t->tv_end.tv_usec = 0;
1306
Len Brownc98d5d92012-06-04 00:56:40 -04001307 t->tsc = 0;
1308 t->aperf = 0;
1309 t->mperf = 0;
1310 t->c1 = 0;
1311
Len Brown562a2d32016-02-26 23:48:05 -05001312 t->irq_count = 0;
1313 t->smi_count = 0;
1314
Len Brownc98d5d92012-06-04 00:56:40 -04001315 /* tells format_counters to dump all fields from this set */
1316 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1317
1318 c->c3 = 0;
1319 c->c6 = 0;
1320 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001321 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001322 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001323
Len Brown0b2bb692015-03-26 00:50:30 -04001324 p->pkg_wtd_core_c0 = 0;
1325 p->pkg_any_core_c0 = 0;
1326 p->pkg_any_gfxe_c0 = 0;
1327 p->pkg_both_core_gfxe_c0 = 0;
1328
Len Brownc98d5d92012-06-04 00:56:40 -04001329 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001330 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001331 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001332 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001333 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001334 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001335 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001336 p->pc8 = 0;
1337 p->pc9 = 0;
1338 p->pc10 = 0;
Len Brownbe0e54c2018-06-01 12:35:53 -04001339 p->cpu_lpi = 0;
1340 p->sys_lpi = 0;
Len Brown889facb2012-11-08 00:48:57 -05001341
1342 p->energy_pkg = 0;
1343 p->energy_dram = 0;
1344 p->energy_cores = 0;
1345 p->energy_gfx = 0;
1346 p->rapl_pkg_perf_status = 0;
1347 p->rapl_dram_perf_status = 0;
1348 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001349
Len Brownfdf676e2016-02-27 01:28:12 -05001350 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001351 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001352 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1353 t->counter[i] = 0;
1354
1355 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1356 c->counter[i] = 0;
1357
1358 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1359 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001360}
1361int sum_counters(struct thread_data *t, struct core_data *c,
1362 struct pkg_data *p)
1363{
Len Brown388e9c82016-12-22 23:57:55 -05001364 int i;
1365 struct msr_counter *mp;
1366
Len Brown3f44a5c2017-10-17 15:42:56 -04001367 /* remember first tv_begin */
1368 if (average.threads.tv_begin.tv_sec == 0)
1369 average.threads.tv_begin = t->tv_begin;
1370
1371 /* remember last tv_end */
1372 average.threads.tv_end = t->tv_end;
1373
Len Brownc98d5d92012-06-04 00:56:40 -04001374 average.threads.tsc += t->tsc;
1375 average.threads.aperf += t->aperf;
1376 average.threads.mperf += t->mperf;
1377 average.threads.c1 += t->c1;
1378
Len Brown562a2d32016-02-26 23:48:05 -05001379 average.threads.irq_count += t->irq_count;
1380 average.threads.smi_count += t->smi_count;
1381
Len Brown388e9c82016-12-22 23:57:55 -05001382 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1383 if (mp->format == FORMAT_RAW)
1384 continue;
1385 average.threads.counter[i] += t->counter[i];
1386 }
1387
Len Brownc98d5d92012-06-04 00:56:40 -04001388 /* sum per-core values only for 1st thread in core */
1389 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1390 return 0;
1391
1392 average.cores.c3 += c->c3;
1393 average.cores.c6 += c->c6;
1394 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001395 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001396
Len Brown889facb2012-11-08 00:48:57 -05001397 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1398
Len Brown388e9c82016-12-22 23:57:55 -05001399 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1400 if (mp->format == FORMAT_RAW)
1401 continue;
1402 average.cores.counter[i] += c->counter[i];
1403 }
1404
Len Brownc98d5d92012-06-04 00:56:40 -04001405 /* sum per-pkg values only for 1st core in pkg */
1406 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1407 return 0;
1408
Len Browna99d8732017-05-20 20:11:55 -04001409 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001410 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001411 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001412 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001413 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001414 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001415 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001416 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001417
Len Brownc98d5d92012-06-04 00:56:40 -04001418 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001419 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001420 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001421 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001422 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001423 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001424 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001425 average.packages.pc8 += p->pc8;
1426 average.packages.pc9 += p->pc9;
1427 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001428
Len Brownbe0e54c2018-06-01 12:35:53 -04001429 average.packages.cpu_lpi = p->cpu_lpi;
1430 average.packages.sys_lpi = p->sys_lpi;
1431
Len Brown889facb2012-11-08 00:48:57 -05001432 average.packages.energy_pkg += p->energy_pkg;
1433 average.packages.energy_dram += p->energy_dram;
1434 average.packages.energy_cores += p->energy_cores;
1435 average.packages.energy_gfx += p->energy_gfx;
1436
Len Brownfdf676e2016-02-27 01:28:12 -05001437 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001438 average.packages.gfx_mhz = p->gfx_mhz;
1439
Len Brown889facb2012-11-08 00:48:57 -05001440 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1441
1442 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1443 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001444
1445 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1446 if (mp->format == FORMAT_RAW)
1447 continue;
1448 average.packages.counter[i] += p->counter[i];
1449 }
Len Brownc98d5d92012-06-04 00:56:40 -04001450 return 0;
1451}
1452/*
1453 * sum the counters for all cpus in the system
1454 * compute the weighted average
1455 */
1456void compute_average(struct thread_data *t, struct core_data *c,
1457 struct pkg_data *p)
1458{
Len Brown388e9c82016-12-22 23:57:55 -05001459 int i;
1460 struct msr_counter *mp;
1461
Len Brownc98d5d92012-06-04 00:56:40 -04001462 clear_counters(&average.threads, &average.cores, &average.packages);
1463
1464 for_all_cpus(sum_counters, t, c, p);
1465
1466 average.threads.tsc /= topo.num_cpus;
1467 average.threads.aperf /= topo.num_cpus;
1468 average.threads.mperf /= topo.num_cpus;
1469 average.threads.c1 /= topo.num_cpus;
1470
Len Brown0de6c0d2017-02-15 21:45:40 -05001471 if (average.threads.irq_count > 9999999)
1472 sums_need_wide_columns = 1;
1473
Len Brownc98d5d92012-06-04 00:56:40 -04001474 average.cores.c3 /= topo.num_cores;
1475 average.cores.c6 /= topo.num_cores;
1476 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001477 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001478
Len Browna99d8732017-05-20 20:11:55 -04001479 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001480 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001481 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001482 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001483 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001484 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001485 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001486 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001487
Len Brownc98d5d92012-06-04 00:56:40 -04001488 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001489 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001490 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001491 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001492 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001493 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001494 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001495
1496 average.packages.pc8 /= topo.num_packages;
1497 average.packages.pc9 /= topo.num_packages;
1498 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001499
1500 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1501 if (mp->format == FORMAT_RAW)
1502 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001503 if (mp->type == COUNTER_ITEMS) {
1504 if (average.threads.counter[i] > 9999999)
1505 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001506 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001507 }
Len Brown388e9c82016-12-22 23:57:55 -05001508 average.threads.counter[i] /= topo.num_cpus;
1509 }
1510 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1511 if (mp->format == FORMAT_RAW)
1512 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001513 if (mp->type == COUNTER_ITEMS) {
1514 if (average.cores.counter[i] > 9999999)
1515 sums_need_wide_columns = 1;
1516 }
Len Brown388e9c82016-12-22 23:57:55 -05001517 average.cores.counter[i] /= topo.num_cores;
1518 }
1519 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1520 if (mp->format == FORMAT_RAW)
1521 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001522 if (mp->type == COUNTER_ITEMS) {
1523 if (average.packages.counter[i] > 9999999)
1524 sums_need_wide_columns = 1;
1525 }
Len Brown388e9c82016-12-22 23:57:55 -05001526 average.packages.counter[i] /= topo.num_packages;
1527 }
Len Brownc98d5d92012-06-04 00:56:40 -04001528}
1529
1530static unsigned long long rdtsc(void)
1531{
1532 unsigned int low, high;
1533
1534 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1535
1536 return low | ((unsigned long long)high) << 32;
1537}
1538
Len Brownc98d5d92012-06-04 00:56:40 -04001539/*
Len Brown495c76542017-02-08 02:41:51 -05001540 * Open a file, and exit on failure
1541 */
1542FILE *fopen_or_die(const char *path, const char *mode)
1543{
1544 FILE *filep = fopen(path, mode);
1545
1546 if (!filep)
1547 err(1, "%s: open failed", path);
1548 return filep;
1549}
1550/*
1551 * snapshot_sysfs_counter()
1552 *
1553 * return snapshot of given counter
1554 */
1555unsigned long long snapshot_sysfs_counter(char *path)
1556{
1557 FILE *fp;
1558 int retval;
1559 unsigned long long counter;
1560
1561 fp = fopen_or_die(path, "r");
1562
1563 retval = fscanf(fp, "%lld", &counter);
1564 if (retval != 1)
1565 err(1, "snapshot_sysfs_counter(%s)", path);
1566
1567 fclose(fp);
1568
1569 return counter;
1570}
1571
1572int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1573{
1574 if (mp->msr_num != 0) {
1575 if (get_msr(cpu, mp->msr_num, counterp))
1576 return -1;
1577 } else {
Len Brown46c27972017-12-08 17:38:17 -05001578 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001579
1580 if (mp->flags & SYSFS_PERCPU) {
1581 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1582 cpu, mp->path);
1583
1584 *counterp = snapshot_sysfs_counter(path);
1585 } else {
1586 *counterp = snapshot_sysfs_counter(mp->path);
1587 }
Len Brown495c76542017-02-08 02:41:51 -05001588 }
1589
1590 return 0;
1591}
1592
1593/*
Len Brownc98d5d92012-06-04 00:56:40 -04001594 * get_counters(...)
1595 * migrate to cpu
1596 * acquire and record local counters for that cpu
1597 */
1598int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1599{
1600 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001601 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001602 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001603 struct msr_counter *mp;
1604 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001605
Len Brownf4fdf2b2017-05-27 21:06:55 -07001606
1607 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1608
Len Browne52966c2012-11-08 22:38:05 -05001609 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001610 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001611 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001612 }
Len Brownc98d5d92012-06-04 00:56:40 -04001613
Len Brown0102b062016-02-27 03:11:29 -05001614retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001615 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1616
Len Brown812db3f2017-02-10 00:25:41 -05001617 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001618 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1619
1620 /*
1621 * The TSC, APERF and MPERF must be read together for
1622 * APERF/MPERF and MPERF/TSC to give accurate results.
1623 *
1624 * Unfortunately, APERF and MPERF are read by
1625 * individual system call, so delays may occur
1626 * between them. If the time to read them
1627 * varies by a large amount, we re-read them.
1628 */
1629
1630 /*
1631 * This initial dummy APERF read has been seen to
1632 * reduce jitter in the subsequent reads.
1633 */
1634
Len Brown9c63a652012-10-31 01:29:52 -04001635 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001636 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001637
1638 t->tsc = rdtsc(); /* re-read close to APERF */
1639
1640 tsc_before = t->tsc;
1641
1642 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1643 return -3;
1644
1645 tsc_between = rdtsc();
1646
Len Brown9c63a652012-10-31 01:29:52 -04001647 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001648 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001649
1650 tsc_after = rdtsc();
1651
1652 aperf_time = tsc_between - tsc_before;
1653 mperf_time = tsc_after - tsc_between;
1654
1655 /*
1656 * If the system call latency to read APERF and MPERF
1657 * differ by more than 2x, then try again.
1658 */
1659 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1660 aperf_mperf_retry_count++;
1661 if (aperf_mperf_retry_count < 5)
1662 goto retry;
1663 else
1664 warnx("cpu%d jitter %lld %lld",
1665 cpu, aperf_time, mperf_time);
1666 }
1667 aperf_mperf_retry_count = 0;
1668
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001669 t->aperf = t->aperf * aperf_mperf_multiplier;
1670 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001671 }
1672
Len Brown812db3f2017-02-10 00:25:41 -05001673 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001674 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001675 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001676 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1677 return -5;
1678 t->smi_count = msr & 0xFFFFFFFF;
1679 }
Len Brown0539ba12017-02-10 00:27:20 -05001680 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001681 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1682 return -6;
1683 }
1684
Len Brown388e9c82016-12-22 23:57:55 -05001685 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001686 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001687 return -10;
1688 }
1689
Len Brownc98d5d92012-06-04 00:56:40 -04001690 /* collect core counters only for 1st thread in core */
1691 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001692 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001693
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07001694 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001695 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1696 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001697 }
1698
Len Brown812db3f2017-02-10 00:25:41 -05001699 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001700 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1701 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001702 } else if (do_knl_cstates) {
1703 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1704 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001705 }
1706
Len Brown812db3f2017-02-10 00:25:41 -05001707 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001708 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1709 return -8;
1710
Len Brown0539ba12017-02-10 00:27:20 -05001711 if (DO_BIC(BIC_Mod_c6))
1712 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1713 return -8;
1714
Len Brown812db3f2017-02-10 00:25:41 -05001715 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001716 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1717 return -9;
1718 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1719 }
1720
Len Brown388e9c82016-12-22 23:57:55 -05001721 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001722 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001723 return -10;
1724 }
Len Brown889facb2012-11-08 00:48:57 -05001725
Len Brownc98d5d92012-06-04 00:56:40 -04001726 /* collect package counters only for 1st core in package */
1727 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001728 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001729
Len Browna99d8732017-05-20 20:11:55 -04001730 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001731 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1732 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001733 }
1734 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001735 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1736 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001737 }
1738 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001739 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1740 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001741 }
1742 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001743 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1744 return -13;
1745 }
Len Brown0f47c082017-01-27 00:50:45 -05001746 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001747 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1748 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001749 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001750 if (do_slm_cstates) {
1751 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1752 return -10;
1753 } else {
1754 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1755 return -10;
1756 }
1757 }
1758
Len Brown0f47c082017-01-27 00:50:45 -05001759 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001760 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1761 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001762 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001763 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1764 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001765 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001766 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1767 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001768 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001769 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1770 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001771 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001772 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1773 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001774
Len Brownbe0e54c2018-06-01 12:35:53 -04001775 if (DO_BIC(BIC_CPU_LPI))
1776 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1777 if (DO_BIC(BIC_SYS_LPI))
1778 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1779
Len Brown889facb2012-11-08 00:48:57 -05001780 if (do_rapl & RAPL_PKG) {
1781 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1782 return -13;
1783 p->energy_pkg = msr & 0xFFFFFFFF;
1784 }
Jacob Pan91484942016-06-16 09:48:20 -07001785 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001786 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1787 return -14;
1788 p->energy_cores = msr & 0xFFFFFFFF;
1789 }
1790 if (do_rapl & RAPL_DRAM) {
1791 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1792 return -15;
1793 p->energy_dram = msr & 0xFFFFFFFF;
1794 }
1795 if (do_rapl & RAPL_GFX) {
1796 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1797 return -16;
1798 p->energy_gfx = msr & 0xFFFFFFFF;
1799 }
1800 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1801 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1802 return -16;
1803 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1804 }
1805 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1806 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1807 return -16;
1808 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1809 }
Len Brown812db3f2017-02-10 00:25:41 -05001810 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001811 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1812 return -17;
1813 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1814 }
Len Brownfdf676e2016-02-27 01:28:12 -05001815
Len Brown812db3f2017-02-10 00:25:41 -05001816 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001817 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1818
Len Brown812db3f2017-02-10 00:25:41 -05001819 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001820 p->gfx_mhz = gfx_cur_mhz;
1821
Len Brown388e9c82016-12-22 23:57:55 -05001822 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001823 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001824 return -10;
1825 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001826done:
1827 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001828
Len Brown103a8fe2010-10-22 23:53:03 -04001829 return 0;
1830}
1831
Len Brownee7e38e2015-02-09 23:39:45 -05001832/*
1833 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1834 * If you change the values, note they are used both in comparisons
1835 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1836 */
1837
1838#define PCLUKN 0 /* Unknown */
1839#define PCLRSV 1 /* Reserved */
1840#define PCL__0 2 /* PC0 */
1841#define PCL__1 3 /* PC1 */
1842#define PCL__2 4 /* PC2 */
1843#define PCL__3 5 /* PC3 */
1844#define PCL__4 6 /* PC4 */
1845#define PCL__6 7 /* PC6 */
1846#define PCL_6N 8 /* PC6 No Retention */
1847#define PCL_6R 9 /* PC6 Retention */
1848#define PCL__7 10 /* PC7 */
1849#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001850#define PCL__8 12 /* PC8 */
1851#define PCL__9 13 /* PC9 */
1852#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001853
1854int pkg_cstate_limit = PCLUKN;
1855char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001856 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001857
Len Browne9257f52015-04-01 21:02:57 -04001858int 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};
1859int 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};
1860int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brown0539ba12017-02-10 00:27:20 -05001861int 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 -05001862int 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 -04001863int 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 -04001864int 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 +03001865int 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 -05001866
Len Browna2b7b742015-09-26 00:12:38 -04001867
1868static void
1869calculate_tsc_tweak()
1870{
Len Browna2b7b742015-09-26 00:12:38 -04001871 tsc_tweak = base_hz / tsc_hz;
1872}
1873
Len Brownfcd17212015-03-23 20:29:09 -04001874static void
1875dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001876{
1877 unsigned long long msr;
1878 unsigned int ratio;
1879
Len Brownec0adc52015-11-12 02:42:31 -05001880 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001881
Len Brownb7d8c142016-02-13 23:36:17 -05001882 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001883
Len Brown103a8fe2010-10-22 23:53:03 -04001884 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001885 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001886 ratio, bclk, ratio * bclk);
1887
1888 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001889 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001890 ratio, bclk, ratio * bclk);
1891
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001892 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001893 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001894 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001895
Len Brownfcd17212015-03-23 20:29:09 -04001896 return;
1897}
1898
1899static void
1900dump_hsw_turbo_ratio_limits(void)
1901{
1902 unsigned long long msr;
1903 unsigned int ratio;
1904
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001905 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001906
Len Brownb7d8c142016-02-13 23:36:17 -05001907 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001908
1909 ratio = (msr >> 8) & 0xFF;
1910 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001911 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001912 ratio, bclk, ratio * bclk);
1913
1914 ratio = (msr >> 0) & 0xFF;
1915 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001916 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001917 ratio, bclk, ratio * bclk);
1918 return;
1919}
1920
1921static void
1922dump_ivt_turbo_ratio_limits(void)
1923{
1924 unsigned long long msr;
1925 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001926
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001927 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001928
Len Brownb7d8c142016-02-13 23:36:17 -05001929 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001930
1931 ratio = (msr >> 56) & 0xFF;
1932 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001933 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001934 ratio, bclk, ratio * bclk);
1935
1936 ratio = (msr >> 48) & 0xFF;
1937 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001938 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001939 ratio, bclk, ratio * bclk);
1940
1941 ratio = (msr >> 40) & 0xFF;
1942 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001943 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001944 ratio, bclk, ratio * bclk);
1945
1946 ratio = (msr >> 32) & 0xFF;
1947 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001948 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001949 ratio, bclk, ratio * bclk);
1950
1951 ratio = (msr >> 24) & 0xFF;
1952 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001953 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001954 ratio, bclk, ratio * bclk);
1955
1956 ratio = (msr >> 16) & 0xFF;
1957 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001958 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001959 ratio, bclk, ratio * bclk);
1960
1961 ratio = (msr >> 8) & 0xFF;
1962 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001963 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001964 ratio, bclk, ratio * bclk);
1965
1966 ratio = (msr >> 0) & 0xFF;
1967 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001968 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001969 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001970 return;
1971}
Len Brown31e07522017-01-31 23:07:49 -05001972int has_turbo_ratio_group_limits(int family, int model)
1973{
1974
1975 if (!genuine_intel)
1976 return 0;
1977
1978 switch (model) {
1979 case INTEL_FAM6_ATOM_GOLDMONT:
1980 case INTEL_FAM6_SKYLAKE_X:
1981 case INTEL_FAM6_ATOM_DENVERTON:
1982 return 1;
1983 }
1984 return 0;
1985}
Len Brown6574a5d2012-09-21 00:01:31 -04001986
Len Brownfcd17212015-03-23 20:29:09 -04001987static void
Len Brown31e07522017-01-31 23:07:49 -05001988dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001989{
Len Brown31e07522017-01-31 23:07:49 -05001990 unsigned long long msr, core_counts;
1991 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001992
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001993 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001994 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001995
Len Brown31e07522017-01-31 23:07:49 -05001996 if (has_turbo_ratio_group_limits(family, model)) {
1997 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1998 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1999 } else {
2000 core_counts = 0x0807060504030201;
2001 }
2002
Len Brown6574a5d2012-09-21 00:01:31 -04002003 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002004 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002005 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002006 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2007 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002008
2009 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002010 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002011 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002012 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2013 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002014
2015 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002016 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002017 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002018 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2019 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002020
2021 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002022 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002023 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002024 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2025 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002026
Len Brown103a8fe2010-10-22 23:53:03 -04002027 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002028 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002029 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002030 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2031 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002032
2033 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002034 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002035 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002036 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2037 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002038
2039 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002040 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002041 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002042 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2043 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002044
2045 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002046 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002047 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002048 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2049 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002050 return;
2051}
Len Brown3a9a9412014-08-15 02:39:52 -04002052
Len Brownfcd17212015-03-23 20:29:09 -04002053static void
Len Brown0f7887c2017-01-12 23:49:18 -05002054dump_atom_turbo_ratio_limits(void)
2055{
2056 unsigned long long msr;
2057 unsigned int ratio;
2058
2059 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2060 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2061
2062 ratio = (msr >> 0) & 0x3F;
2063 if (ratio)
2064 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2065 ratio, bclk, ratio * bclk);
2066
2067 ratio = (msr >> 8) & 0x3F;
2068 if (ratio)
2069 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2070 ratio, bclk, ratio * bclk);
2071
2072 ratio = (msr >> 16) & 0x3F;
2073 if (ratio)
2074 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2075 ratio, bclk, ratio * bclk);
2076
2077 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2078 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2079
2080 ratio = (msr >> 24) & 0x3F;
2081 if (ratio)
2082 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2083 ratio, bclk, ratio * bclk);
2084
2085 ratio = (msr >> 16) & 0x3F;
2086 if (ratio)
2087 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2088 ratio, bclk, ratio * bclk);
2089
2090 ratio = (msr >> 8) & 0x3F;
2091 if (ratio)
2092 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2093 ratio, bclk, ratio * bclk);
2094
2095 ratio = (msr >> 0) & 0x3F;
2096 if (ratio)
2097 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2098 ratio, bclk, ratio * bclk);
2099}
2100
2101static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002102dump_knl_turbo_ratio_limits(void)
2103{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002104 const unsigned int buckets_no = 7;
2105
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002106 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002107 int delta_cores, delta_ratio;
2108 int i, b_nr;
2109 unsigned int cores[buckets_no];
2110 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002111
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002112 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002113
Len Brownb7d8c142016-02-13 23:36:17 -05002114 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002115 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002116
2117 /**
2118 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002119 * [0] -- Reserved
2120 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002121 * [15:8] -- Base value of freq ratio of bucket 1.
2122 * [20:16] -- +ve delta of number of active cores of bucket 2.
2123 * i.e. active cores of bucket 2 =
2124 * active cores of bucket 1 + delta
2125 * [23:21] -- Negative delta of freq ratio of bucket 2.
2126 * i.e. freq ratio of bucket 2 =
2127 * freq ratio of bucket 1 - delta
2128 * [28:24]-- +ve delta of number of active cores of bucket 3.
2129 * [31:29]-- -ve delta of freq ratio of bucket 3.
2130 * [36:32]-- +ve delta of number of active cores of bucket 4.
2131 * [39:37]-- -ve delta of freq ratio of bucket 4.
2132 * [44:40]-- +ve delta of number of active cores of bucket 5.
2133 * [47:45]-- -ve delta of freq ratio of bucket 5.
2134 * [52:48]-- +ve delta of number of active cores of bucket 6.
2135 * [55:53]-- -ve delta of freq ratio of bucket 6.
2136 * [60:56]-- +ve delta of number of active cores of bucket 7.
2137 * [63:61]-- -ve delta of freq ratio of bucket 7.
2138 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002139
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002140 b_nr = 0;
2141 cores[b_nr] = (msr & 0xFF) >> 1;
2142 ratio[b_nr] = (msr >> 8) & 0xFF;
2143
2144 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002145 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002146 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002147
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002148 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2149 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2150 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002151 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002152
2153 for (i = buckets_no - 1; i >= 0; i--)
2154 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002155 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002156 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002157 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002158}
2159
2160static void
Len Brownfcd17212015-03-23 20:29:09 -04002161dump_nhm_cst_cfg(void)
2162{
2163 unsigned long long msr;
2164
Len Brown1df2e552017-01-07 23:24:57 -05002165 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002166
Len Brown1df2e552017-01-07 23:24:57 -05002167 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002168
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002169 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002170 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2171 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2172 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2173 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2174 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002175 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002176 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002177
2178#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2179 if (has_automatic_cstate_conversion) {
2180 fprintf(outf, ", automatic c-state conversion=%s",
2181 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2182 }
2183
2184 fprintf(outf, ")\n");
2185
Len Brownfcd17212015-03-23 20:29:09 -04002186 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002187}
2188
Len Brown6fb31432015-06-17 16:23:45 -04002189static void
2190dump_config_tdp(void)
2191{
2192 unsigned long long msr;
2193
2194 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002195 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002196 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002197
2198 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002199 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002200 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002201 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2202 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2203 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2204 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002205 }
Len Brownb7d8c142016-02-13 23:36:17 -05002206 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002207
2208 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002209 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002210 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002211 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2212 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2213 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2214 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002215 }
Len Brownb7d8c142016-02-13 23:36:17 -05002216 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002217
2218 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002219 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002220 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002221 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2222 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2223 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002224
Len Brown6fb31432015-06-17 16:23:45 -04002225 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002226 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002227 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002228 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2229 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002230}
Len Brown5a634262016-04-06 17:15:55 -04002231
2232unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2233
2234void print_irtl(void)
2235{
2236 unsigned long long msr;
2237
2238 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2239 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2240 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2241 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2242
2243 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2244 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2245 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2246 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2247
2248 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2249 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2250 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2251 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2252
2253 if (!do_irtl_hsw)
2254 return;
2255
2256 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2257 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2258 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2259 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2260
2261 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2262 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2263 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2264 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2265
2266 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2267 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2268 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2269 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2270
2271}
Len Brown36229892016-02-26 20:51:02 -05002272void free_fd_percpu(void)
2273{
2274 int i;
2275
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002276 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002277 if (fd_percpu[i] != 0)
2278 close(fd_percpu[i]);
2279 }
2280
2281 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002282}
2283
Len Brownc98d5d92012-06-04 00:56:40 -04002284void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002285{
Len Brown0e2d8f02018-06-01 22:08:58 -04002286 int i;
2287
Len Brownc98d5d92012-06-04 00:56:40 -04002288 CPU_FREE(cpu_present_set);
2289 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002290 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002291
Len Brownc98d5d92012-06-04 00:56:40 -04002292 CPU_FREE(cpu_affinity_set);
2293 cpu_affinity_set = NULL;
2294 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002295
Len Brownc98d5d92012-06-04 00:56:40 -04002296 free(thread_even);
2297 free(core_even);
2298 free(package_even);
2299
2300 thread_even = NULL;
2301 core_even = NULL;
2302 package_even = NULL;
2303
2304 free(thread_odd);
2305 free(core_odd);
2306 free(package_odd);
2307
2308 thread_odd = NULL;
2309 core_odd = NULL;
2310 package_odd = NULL;
2311
2312 free(output_buffer);
2313 output_buffer = NULL;
2314 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002315
2316 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002317
2318 free(irq_column_2_cpu);
2319 free(irqs_per_cpu);
Len Brown0e2d8f02018-06-01 22:08:58 -04002320
2321 for (i = 0; i <= topo.max_cpu_num; ++i) {
2322 if (cpus[i].put_ids)
2323 CPU_FREE(cpus[i].put_ids);
2324 }
2325 free(cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002326}
2327
Josh Triplett57a42a32013-08-20 17:20:17 -07002328
2329/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002330 * Parse a file containing a single int.
2331 */
2332int parse_int_file(const char *fmt, ...)
2333{
2334 va_list args;
2335 char path[PATH_MAX];
2336 FILE *filep;
2337 int value;
2338
2339 va_start(args, fmt);
2340 vsnprintf(path, sizeof(path), fmt, args);
2341 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002342 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002343 if (fscanf(filep, "%d", &value) != 1)
2344 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002345 fclose(filep);
2346 return value;
2347}
2348
2349/*
Len Brownc98d5d92012-06-04 00:56:40 -04002350 * cpu_is_first_core_in_package(cpu)
2351 * return 1 if given CPU is 1st core in package
2352 */
2353int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002354{
Josh Triplett95aebc42013-08-20 17:20:16 -07002355 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002356}
2357
2358int get_physical_package_id(int cpu)
2359{
Josh Triplett95aebc42013-08-20 17:20:16 -07002360 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002361}
2362
2363int get_core_id(int cpu)
2364{
Josh Triplett95aebc42013-08-20 17:20:16 -07002365 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002366}
2367
Prarit Bhargavaef605742018-06-01 10:04:30 -04002368void set_node_data(void)
2369{
2370 char path[80];
2371 FILE *filep;
2372 int pkg, node, cpu;
2373
2374 struct pkg_node_info {
2375 int count;
2376 int min;
2377 } *pni;
2378
2379 pni = calloc(topo.num_packages, sizeof(struct pkg_node_info));
2380 if (!pni)
2381 err(1, "calloc pkg_node_count");
2382
2383 for (pkg = 0; pkg < topo.num_packages; pkg++)
2384 pni[pkg].min = topo.num_cpus;
2385
2386 for (node = 0; node <= topo.max_node_num; node++) {
2387 /* find the "first" cpu in the node */
2388 sprintf(path, "/sys/bus/node/devices/node%d/cpulist", node);
2389 filep = fopen(path, "r");
2390 if (!filep)
2391 continue;
2392 fscanf(filep, "%d", &cpu);
2393 fclose(filep);
2394
2395 pkg = cpus[cpu].physical_package_id;
2396 pni[pkg].count++;
2397
2398 if (node < pni[pkg].min)
2399 pni[pkg].min = node;
2400 }
2401
2402 for (pkg = 0; pkg < topo.num_packages; pkg++)
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04002403 if (pni[pkg].count > topo.nodes_per_pkg)
2404 topo.nodes_per_pkg = pni[0].count;
Prarit Bhargavaef605742018-06-01 10:04:30 -04002405
2406 for (cpu = 0; cpu < topo.num_cpus; cpu++) {
2407 pkg = cpus[cpu].physical_package_id;
2408 node = cpus[cpu].physical_node_id;
2409 cpus[cpu].logical_node_id = node - pni[pkg].min;
2410 }
2411 free(pni);
2412
2413}
2414
2415int get_physical_node_id(struct cpu_topology *thiscpu)
Len Brownc98d5d92012-06-04 00:56:40 -04002416{
2417 char path[80];
2418 FILE *filep;
Len Brown0e2d8f02018-06-01 22:08:58 -04002419 int i;
2420 int cpu = thiscpu->logical_cpu_id;
Len Brownc98d5d92012-06-04 00:56:40 -04002421
Len Brown0e2d8f02018-06-01 22:08:58 -04002422 for (i = 0; i <= topo.max_cpu_num; i++) {
2423 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2424 cpu, i);
2425 filep = fopen(path, "r");
2426 if (!filep)
2427 continue;
2428 fclose(filep);
2429 return i;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002430 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002431 return -1;
2432}
Len Brownc98d5d92012-06-04 00:56:40 -04002433
Len Brown0e2d8f02018-06-01 22:08:58 -04002434int get_thread_siblings(struct cpu_topology *thiscpu)
2435{
2436 char path[80], character;
2437 FILE *filep;
2438 unsigned long map;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002439 int so, shift, sib_core;
Len Brown0e2d8f02018-06-01 22:08:58 -04002440 int cpu = thiscpu->logical_cpu_id;
2441 int offset = topo.max_cpu_num + 1;
2442 size_t size;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002443 int thread_id = 0;
Len Brown0e2d8f02018-06-01 22:08:58 -04002444
2445 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002446 if (thiscpu->thread_id < 0)
2447 thiscpu->thread_id = thread_id++;
Len Brown0e2d8f02018-06-01 22:08:58 -04002448 if (!thiscpu->put_ids)
2449 return -1;
2450
2451 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2452 CPU_ZERO_S(size, thiscpu->put_ids);
2453
2454 sprintf(path,
2455 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2456 filep = fopen_or_die(path, "r");
2457 do {
2458 offset -= BITMASK_SIZE;
2459 fscanf(filep, "%lx%c", &map, &character);
2460 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2461 if ((map >> shift) & 0x1) {
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002462 so = shift + offset;
2463 sib_core = get_core_id(so);
2464 if (sib_core == thiscpu->physical_core_id) {
2465 CPU_SET_S(so, size, thiscpu->put_ids);
2466 if ((so != cpu) &&
2467 (cpus[so].thread_id < 0))
2468 cpus[so].thread_id =
2469 thread_id++;
2470 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002471 }
2472 }
2473 } while (!strncmp(&character, ",", 1));
Len Brownc98d5d92012-06-04 00:56:40 -04002474 fclose(filep);
Len Brown0e2d8f02018-06-01 22:08:58 -04002475
2476 return CPU_COUNT_S(size, thiscpu->put_ids);
Len Brownc98d5d92012-06-04 00:56:40 -04002477}
2478
Len Brown103a8fe2010-10-22 23:53:03 -04002479/*
Len Brownc98d5d92012-06-04 00:56:40 -04002480 * run func(thread, core, package) in topology order
2481 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002482 */
2483
Len Brownc98d5d92012-06-04 00:56:40 -04002484int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2485 struct pkg_data *, struct thread_data *, struct core_data *,
2486 struct pkg_data *), struct thread_data *thread_base,
2487 struct core_data *core_base, struct pkg_data *pkg_base,
2488 struct thread_data *thread_base2, struct core_data *core_base2,
2489 struct pkg_data *pkg_base2)
2490{
2491 int retval, pkg_no, core_no, thread_no;
2492
2493 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04002494 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
Len Brownc98d5d92012-06-04 00:56:40 -04002495 for (thread_no = 0; thread_no <
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04002496 topo.threads_per_core; ++thread_no) {
Len Brownc98d5d92012-06-04 00:56:40 -04002497 struct thread_data *t, *t2;
2498 struct core_data *c, *c2;
2499 struct pkg_data *p, *p2;
2500
2501 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2502
2503 if (cpu_is_not_present(t->cpu_id))
2504 continue;
2505
2506 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2507
2508 c = GET_CORE(core_base, core_no, pkg_no);
2509 c2 = GET_CORE(core_base2, core_no, pkg_no);
2510
2511 p = GET_PKG(pkg_base, pkg_no);
2512 p2 = GET_PKG(pkg_base2, pkg_no);
2513
2514 retval = func(t, c, p, t2, c2, p2);
2515 if (retval)
2516 return retval;
2517 }
2518 }
2519 }
2520 return 0;
2521}
2522
2523/*
2524 * run func(cpu) on every cpu in /proc/stat
2525 * return max_cpu number
2526 */
2527int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002528{
2529 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002530 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002531 int retval;
2532
Josh Triplett57a42a32013-08-20 17:20:17 -07002533 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002534
2535 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002536 if (retval != 0)
2537 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002538
Len Brownc98d5d92012-06-04 00:56:40 -04002539 while (1) {
2540 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 -04002541 if (retval != 1)
2542 break;
2543
Len Brownc98d5d92012-06-04 00:56:40 -04002544 retval = func(cpu_num);
2545 if (retval) {
2546 fclose(fp);
2547 return(retval);
2548 }
Len Brown103a8fe2010-10-22 23:53:03 -04002549 }
2550 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002551 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002552}
2553
2554void re_initialize(void)
2555{
Len Brownc98d5d92012-06-04 00:56:40 -04002556 free_all_buffers();
2557 setup_all_buffers();
2558 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002559}
2560
Prarit Bhargava843c5792018-06-01 10:04:28 -04002561void set_max_cpu_num(void)
2562{
2563 FILE *filep;
2564 unsigned long dummy;
2565
2566 topo.max_cpu_num = 0;
2567 filep = fopen_or_die(
2568 "/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2569 "r");
2570 while (fscanf(filep, "%lx,", &dummy) == 1)
Len Brown0e2d8f02018-06-01 22:08:58 -04002571 topo.max_cpu_num += BITMASK_SIZE;
Prarit Bhargava843c5792018-06-01 10:04:28 -04002572 fclose(filep);
2573 topo.max_cpu_num--; /* 0 based */
2574}
Len Brownc98d5d92012-06-04 00:56:40 -04002575
Len Brown103a8fe2010-10-22 23:53:03 -04002576/*
Len Brownc98d5d92012-06-04 00:56:40 -04002577 * count_cpus()
2578 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002579 */
Len Brownc98d5d92012-06-04 00:56:40 -04002580int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002581{
Prarit Bhargava843c5792018-06-01 10:04:28 -04002582 topo.num_cpus++;
Len Brownc98d5d92012-06-04 00:56:40 -04002583 return 0;
2584}
2585int mark_cpu_present(int cpu)
2586{
2587 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002588 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002589}
2590
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002591int init_thread_id(int cpu)
2592{
2593 cpus[cpu].thread_id = -1;
2594 return 0;
2595}
2596
Len Brown562a2d32016-02-26 23:48:05 -05002597/*
2598 * snapshot_proc_interrupts()
2599 *
2600 * read and record summary of /proc/interrupts
2601 *
2602 * return 1 if config change requires a restart, else return 0
2603 */
2604int snapshot_proc_interrupts(void)
2605{
2606 static FILE *fp;
2607 int column, retval;
2608
2609 if (fp == NULL)
2610 fp = fopen_or_die("/proc/interrupts", "r");
2611 else
2612 rewind(fp);
2613
2614 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2615 for (column = 0; column < topo.num_cpus; ++column) {
2616 int cpu_number;
2617
2618 retval = fscanf(fp, " CPU%d", &cpu_number);
2619 if (retval != 1)
2620 break;
2621
2622 if (cpu_number > topo.max_cpu_num) {
2623 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2624 return 1;
2625 }
2626
2627 irq_column_2_cpu[column] = cpu_number;
2628 irqs_per_cpu[cpu_number] = 0;
2629 }
2630
2631 /* read /proc/interrupt count lines and sum up irqs per cpu */
2632 while (1) {
2633 int column;
2634 char buf[64];
2635
2636 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2637 if (retval != 1)
2638 break;
2639
2640 /* read the count per cpu */
2641 for (column = 0; column < topo.num_cpus; ++column) {
2642
2643 int cpu_number, irq_count;
2644
2645 retval = fscanf(fp, " %d", &irq_count);
2646 if (retval != 1)
2647 break;
2648
2649 cpu_number = irq_column_2_cpu[column];
2650 irqs_per_cpu[cpu_number] += irq_count;
2651
2652 }
2653
2654 while (getc(fp) != '\n')
2655 ; /* flush interrupt description */
2656
2657 }
2658 return 0;
2659}
Len Brown27d47352016-02-27 00:37:54 -05002660/*
Len Brownfdf676e2016-02-27 01:28:12 -05002661 * snapshot_gfx_rc6_ms()
2662 *
2663 * record snapshot of
2664 * /sys/class/drm/card0/power/rc6_residency_ms
2665 *
2666 * return 1 if config change requires a restart, else return 0
2667 */
2668int snapshot_gfx_rc6_ms(void)
2669{
2670 FILE *fp;
2671 int retval;
2672
2673 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2674
2675 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2676 if (retval != 1)
2677 err(1, "GFX rc6");
2678
2679 fclose(fp);
2680
2681 return 0;
2682}
2683/*
Len Brown27d47352016-02-27 00:37:54 -05002684 * snapshot_gfx_mhz()
2685 *
2686 * record snapshot of
2687 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2688 *
2689 * return 1 if config change requires a restart, else return 0
2690 */
2691int snapshot_gfx_mhz(void)
2692{
2693 static FILE *fp;
2694 int retval;
2695
2696 if (fp == NULL)
2697 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002698 else {
Len Brown27d47352016-02-27 00:37:54 -05002699 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002700 fflush(fp);
2701 }
Len Brown27d47352016-02-27 00:37:54 -05002702
2703 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2704 if (retval != 1)
2705 err(1, "GFX MHz");
2706
2707 return 0;
2708}
Len Brown562a2d32016-02-26 23:48:05 -05002709
2710/*
Len Brownbe0e54c2018-06-01 12:35:53 -04002711 * snapshot_cpu_lpi()
2712 *
2713 * record snapshot of
2714 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2715 *
2716 * return 1 if config change requires a restart, else return 0
2717 */
2718int snapshot_cpu_lpi_us(void)
2719{
2720 FILE *fp;
2721 int retval;
2722
2723 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2724
2725 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2726 if (retval != 1)
2727 err(1, "CPU LPI");
2728
2729 fclose(fp);
2730
2731 return 0;
2732}
2733/*
2734 * snapshot_sys_lpi()
2735 *
2736 * record snapshot of
2737 * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2738 *
2739 * return 1 if config change requires a restart, else return 0
2740 */
2741int snapshot_sys_lpi_us(void)
2742{
2743 FILE *fp;
2744 int retval;
2745
2746 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2747
2748 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2749 if (retval != 1)
2750 err(1, "SYS LPI");
2751
2752 fclose(fp);
2753
2754 return 0;
2755}
2756/*
Len Brown562a2d32016-02-26 23:48:05 -05002757 * snapshot /proc and /sys files
2758 *
2759 * return 1 if configuration restart needed, else return 0
2760 */
2761int snapshot_proc_sysfs_files(void)
2762{
Len Brown218f0e82017-02-14 22:07:52 -05002763 if (DO_BIC(BIC_IRQ))
2764 if (snapshot_proc_interrupts())
2765 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002766
Len Brown812db3f2017-02-10 00:25:41 -05002767 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002768 snapshot_gfx_rc6_ms();
2769
Len Brown812db3f2017-02-10 00:25:41 -05002770 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002771 snapshot_gfx_mhz();
2772
Len Brownbe0e54c2018-06-01 12:35:53 -04002773 if (DO_BIC(BIC_CPU_LPI))
2774 snapshot_cpu_lpi_us();
2775
2776 if (DO_BIC(BIC_SYS_LPI))
2777 snapshot_sys_lpi_us();
2778
Len Brown562a2d32016-02-26 23:48:05 -05002779 return 0;
2780}
2781
Len Brown8aa2ed02017-07-15 14:57:37 -04002782int exit_requested;
2783
2784static void signal_handler (int signal)
2785{
2786 switch (signal) {
2787 case SIGINT:
2788 exit_requested = 1;
2789 if (debug)
2790 fprintf(stderr, " SIGINT\n");
2791 break;
Len Brown07211962017-07-15 15:51:26 -04002792 case SIGUSR1:
2793 if (debug > 1)
2794 fprintf(stderr, "SIGUSR1\n");
2795 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002796 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002797 /* make sure this manually-invoked interval is at least 1ms long */
2798 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002799}
2800
2801void setup_signal_handler(void)
2802{
2803 struct sigaction sa;
2804
2805 memset(&sa, 0, sizeof(sa));
2806
2807 sa.sa_handler = &signal_handler;
2808
2809 if (sigaction(SIGINT, &sa, NULL) < 0)
2810 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002811 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2812 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002813}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002814
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002815void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002816{
2817 struct timeval select_timeout;
2818 fd_set readfds;
2819 int retval;
2820
2821 FD_ZERO(&readfds);
2822 FD_SET(0, &readfds);
2823
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002824 if (!isatty(fileno(stdin))) {
2825 nanosleep(&interval_ts, NULL);
2826 return;
2827 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002828
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002829 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002830 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2831
2832 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002833 switch (getc(stdin)) {
2834 case 'q':
2835 exit_requested = 1;
2836 break;
2837 }
2838 /* make sure this manually-invoked interval is at least 1ms long */
2839 nanosleep(&one_msec, NULL);
2840 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002841}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002842
Len Brown103a8fe2010-10-22 23:53:03 -04002843void turbostat_loop()
2844{
Len Brownc98d5d92012-06-04 00:56:40 -04002845 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002846 int restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002847 int done_iters = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002848
Len Brown8aa2ed02017-07-15 14:57:37 -04002849 setup_signal_handler();
2850
Len Brown103a8fe2010-10-22 23:53:03 -04002851restart:
Len Browne52966c2012-11-08 22:38:05 -05002852 restarted++;
2853
Len Brown562a2d32016-02-26 23:48:05 -05002854 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002855 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002856 if (retval < -1) {
2857 exit(retval);
2858 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002859 if (restarted > 1) {
2860 exit(retval);
2861 }
Len Brownc98d5d92012-06-04 00:56:40 -04002862 re_initialize();
2863 goto restart;
2864 }
Len Browne52966c2012-11-08 22:38:05 -05002865 restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002866 done_iters = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002867 gettimeofday(&tv_even, (struct timezone *)NULL);
2868
2869 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002870 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002871 re_initialize();
2872 goto restart;
2873 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002874 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002875 if (snapshot_proc_sysfs_files())
2876 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002877 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002878 if (retval < -1) {
2879 exit(retval);
2880 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002881 re_initialize();
2882 goto restart;
2883 }
Len Brown103a8fe2010-10-22 23:53:03 -04002884 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002885 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002886 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2887 re_initialize();
2888 goto restart;
2889 }
Len Brownc98d5d92012-06-04 00:56:40 -04002890 compute_average(EVEN_COUNTERS);
2891 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002892 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002893 if (exit_requested)
2894 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002895 if (num_iterations && ++done_iters >= num_iterations)
2896 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002897 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002898 if (snapshot_proc_sysfs_files())
2899 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002900 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002901 if (retval < -1) {
2902 exit(retval);
2903 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002904 re_initialize();
2905 goto restart;
2906 }
Len Brown103a8fe2010-10-22 23:53:03 -04002907 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002908 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002909 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2910 re_initialize();
2911 goto restart;
2912 }
Len Brownc98d5d92012-06-04 00:56:40 -04002913 compute_average(ODD_COUNTERS);
2914 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002915 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002916 if (exit_requested)
2917 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002918 if (num_iterations && ++done_iters >= num_iterations)
2919 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002920 }
2921}
2922
2923void check_dev_msr()
2924{
2925 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002926 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002927
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002928 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2929 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002930 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2931 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002932}
2933
Len Brown98481e72014-08-15 00:36:50 -04002934void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002935{
Len Brown98481e72014-08-15 00:36:50 -04002936 struct __user_cap_header_struct cap_header_data;
2937 cap_user_header_t cap_header = &cap_header_data;
2938 struct __user_cap_data_struct cap_data_data;
2939 cap_user_data_t cap_data = &cap_data_data;
2940 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2941 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002942 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002943
2944 /* check for CAP_SYS_RAWIO */
2945 cap_header->pid = getpid();
2946 cap_header->version = _LINUX_CAPABILITY_VERSION;
2947 if (capget(cap_header, cap_data) < 0)
2948 err(-6, "capget(2) failed");
2949
2950 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2951 do_exit++;
2952 warnx("capget(CAP_SYS_RAWIO) failed,"
2953 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2954 }
2955
2956 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002957 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2958 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002959 do_exit++;
2960 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2961 }
2962
2963 /* if all else fails, thell them to be root */
2964 if (do_exit)
2965 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002966 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002967
2968 if (do_exit)
2969 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002970}
2971
Len Brownd7899442015-01-23 00:12:33 -05002972/*
2973 * NHM adds support for additional MSRs:
2974 *
2975 * MSR_SMI_COUNT 0x00000034
2976 *
Len Brownec0adc52015-11-12 02:42:31 -05002977 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002978 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002979 *
Len Browncf4cbe52017-01-01 13:08:33 -05002980 * MSR_MISC_PWR_MGMT 0x000001aa
2981 *
Len Brownd7899442015-01-23 00:12:33 -05002982 * MSR_PKG_C3_RESIDENCY 0x000003f8
2983 * MSR_PKG_C6_RESIDENCY 0x000003f9
2984 * MSR_CORE_C3_RESIDENCY 0x000003fc
2985 * MSR_CORE_C6_RESIDENCY 0x000003fd
2986 *
Len Brownee7e38e2015-02-09 23:39:45 -05002987 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002988 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002989 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002990 */
Len Brownee7e38e2015-02-09 23:39:45 -05002991int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002992{
Len Brownee7e38e2015-02-09 23:39:45 -05002993 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002994 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002995 int *pkg_cstate_limits;
2996
Len Brown103a8fe2010-10-22 23:53:03 -04002997 if (!genuine_intel)
2998 return 0;
2999
3000 if (family != 6)
3001 return 0;
3002
Len Brown21ed5572015-10-19 22:37:40 -04003003 bclk = discover_bclk(family, model);
3004
Len Brown103a8fe2010-10-22 23:53:03 -04003005 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003006 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3007 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04003008 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04003009 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
3010 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
3011 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3012 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05003013 pkg_cstate_limits = nhm_pkg_cstate_limits;
3014 break;
Len Brown869ce692016-06-16 23:22:37 -04003015 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3016 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3017 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3018 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05003019 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003020 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003021 break;
Len Brown869ce692016-06-16 23:22:37 -04003022 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3023 case INTEL_FAM6_HASWELL_X: /* HSX */
3024 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3025 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3026 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3027 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3028 case INTEL_FAM6_BROADWELL_X: /* BDX */
3029 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3030 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3031 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3032 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3033 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003034 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brownee7e38e2015-02-09 23:39:45 -05003035 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003036 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003037 break;
Len Brownd8ebb442016-12-01 20:27:46 -05003038 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3039 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003040 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05003041 break;
Len Brown869ce692016-06-16 23:22:37 -04003042 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05003043 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04003044 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05003045 pkg_cstate_limits = slv_pkg_cstate_limits;
3046 break;
Len Brown869ce692016-06-16 23:22:37 -04003047 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05003048 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05003049 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003050 break;
Len Brown869ce692016-06-16 23:22:37 -04003051 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05003052 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05003053 pkg_cstate_limits = phi_pkg_cstate_limits;
3054 break;
Len Brown869ce692016-06-16 23:22:37 -04003055 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003056 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04003057 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04003058 pkg_cstate_limits = bxt_pkg_cstate_limits;
3059 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003060 default:
3061 return 0;
3062 }
Len Brown1df2e552017-01-07 23:24:57 -05003063 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04003064 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05003065
Len Brownec0adc52015-11-12 02:42:31 -05003066 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04003067 base_ratio = (msr >> 8) & 0xFF;
3068
3069 base_hz = base_ratio * bclk * 1000000;
3070 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003071 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04003072}
Len Brown0f7887c2017-01-12 23:49:18 -05003073/*
Len Brown495c76542017-02-08 02:41:51 -05003074 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05003075 *
3076 * MSR_CC6_DEMOTION_POLICY_CONFIG
3077 * MSR_MC6_DEMOTION_POLICY_CONFIG
3078 */
3079
3080int has_slv_msrs(unsigned int family, unsigned int model)
3081{
3082 if (!genuine_intel)
3083 return 0;
3084
3085 switch (model) {
3086 case INTEL_FAM6_ATOM_SILVERMONT1:
3087 case INTEL_FAM6_ATOM_MERRIFIELD:
3088 case INTEL_FAM6_ATOM_MOOREFIELD:
3089 return 1;
3090 }
3091 return 0;
3092}
Len Brown7170a372017-01-27 02:13:27 -05003093int is_dnv(unsigned int family, unsigned int model)
3094{
3095
3096 if (!genuine_intel)
3097 return 0;
3098
3099 switch (model) {
3100 case INTEL_FAM6_ATOM_DENVERTON:
3101 return 1;
3102 }
3103 return 0;
3104}
Len Brownade0eba2017-02-10 01:56:47 -05003105int is_bdx(unsigned int family, unsigned int model)
3106{
3107
3108 if (!genuine_intel)
3109 return 0;
3110
3111 switch (model) {
3112 case INTEL_FAM6_BROADWELL_X:
3113 case INTEL_FAM6_BROADWELL_XEON_D:
3114 return 1;
3115 }
3116 return 0;
3117}
Len Brown34c761972017-01-27 02:36:41 -05003118int is_skx(unsigned int family, unsigned int model)
3119{
3120
3121 if (!genuine_intel)
3122 return 0;
3123
3124 switch (model) {
3125 case INTEL_FAM6_SKYLAKE_X:
3126 return 1;
3127 }
3128 return 0;
3129}
Len Brown0f7887c2017-01-12 23:49:18 -05003130
Len Brown31e07522017-01-31 23:07:49 -05003131int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05003132{
Len Brown0f7887c2017-01-12 23:49:18 -05003133 if (has_slv_msrs(family, model))
3134 return 0;
3135
Len Brownd7899442015-01-23 00:12:33 -05003136 switch (model) {
3137 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04003138 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3139 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3140 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05003141 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05003142 return 0;
3143 default:
3144 return 1;
3145 }
3146}
Len Brown0f7887c2017-01-12 23:49:18 -05003147int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3148{
3149 if (has_slv_msrs(family, model))
3150 return 1;
3151
3152 return 0;
3153}
Len Brown6574a5d2012-09-21 00:01:31 -04003154int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3155{
3156 if (!genuine_intel)
3157 return 0;
3158
3159 if (family != 6)
3160 return 0;
3161
3162 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003163 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3164 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04003165 return 1;
3166 default:
3167 return 0;
3168 }
3169}
Len Brownfcd17212015-03-23 20:29:09 -04003170int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3171{
3172 if (!genuine_intel)
3173 return 0;
3174
3175 if (family != 6)
3176 return 0;
3177
3178 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003179 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003180 return 1;
3181 default:
3182 return 0;
3183 }
3184}
3185
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003186int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3187{
3188 if (!genuine_intel)
3189 return 0;
3190
3191 if (family != 6)
3192 return 0;
3193
3194 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003195 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003196 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003197 return 1;
3198 default:
3199 return 0;
3200 }
3201}
Len Brown31e07522017-01-31 23:07:49 -05003202int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3203{
3204 if (!genuine_intel)
3205 return 0;
3206
3207 if (family != 6)
3208 return 0;
3209
3210 switch (model) {
3211 case INTEL_FAM6_ATOM_GOLDMONT:
3212 case INTEL_FAM6_SKYLAKE_X:
3213 return 1;
3214 default:
3215 return 0;
3216 }
3217}
Len Brown6fb31432015-06-17 16:23:45 -04003218int has_config_tdp(unsigned int family, unsigned int model)
3219{
3220 if (!genuine_intel)
3221 return 0;
3222
3223 if (family != 6)
3224 return 0;
3225
3226 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003227 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3228 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3229 case INTEL_FAM6_HASWELL_X: /* HSX */
3230 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3231 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3232 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3233 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3234 case INTEL_FAM6_BROADWELL_X: /* BDX */
3235 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3236 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3237 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3238 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3239 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003240 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04003241 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003242
Len Brown869ce692016-06-16 23:22:37 -04003243 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003244 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003245 return 1;
3246 default:
3247 return 0;
3248 }
3249}
3250
Len Brownfcd17212015-03-23 20:29:09 -04003251static void
Colin Ian King1b693172016-03-02 13:50:25 +00003252dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003253{
3254 if (!do_nhm_platform_info)
3255 return;
3256
3257 dump_nhm_platform_info();
3258
3259 if (has_hsw_turbo_ratio_limit(family, model))
3260 dump_hsw_turbo_ratio_limits();
3261
3262 if (has_ivt_turbo_ratio_limit(family, model))
3263 dump_ivt_turbo_ratio_limits();
3264
Len Brown31e07522017-01-31 23:07:49 -05003265 if (has_turbo_ratio_limit(family, model))
3266 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003267
Len Brown0f7887c2017-01-12 23:49:18 -05003268 if (has_atom_turbo_ratio_limit(family, model))
3269 dump_atom_turbo_ratio_limits();
3270
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003271 if (has_knl_turbo_ratio_limit(family, model))
3272 dump_knl_turbo_ratio_limits();
3273
Len Brown6fb31432015-06-17 16:23:45 -04003274 if (has_config_tdp(family, model))
3275 dump_config_tdp();
3276
Len Brownfcd17212015-03-23 20:29:09 -04003277 dump_nhm_cst_cfg();
3278}
3279
Len Brown41618e62017-02-09 18:25:22 -05003280static void
3281dump_sysfs_cstate_config(void)
3282{
3283 char path[64];
3284 char name_buf[16];
3285 char desc[64];
3286 FILE *input;
3287 int state;
3288 char *sp;
3289
3290 if (!DO_BIC(BIC_sysfs))
3291 return;
3292
3293 for (state = 0; state < 10; ++state) {
3294
3295 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3296 base_cpu, state);
3297 input = fopen(path, "r");
3298 if (input == NULL)
3299 continue;
3300 fgets(name_buf, sizeof(name_buf), input);
3301
3302 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3303 sp = strchr(name_buf, '-');
3304 if (!sp)
3305 sp = strchrnul(name_buf, '\n');
3306 *sp = '\0';
3307
3308 fclose(input);
3309
3310 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3311 base_cpu, state);
3312 input = fopen(path, "r");
3313 if (input == NULL)
3314 continue;
3315 fgets(desc, sizeof(desc), input);
3316
3317 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3318 fclose(input);
3319 }
3320}
Len Brown7293fcc2017-02-22 00:11:12 -05003321static void
3322dump_sysfs_pstate_config(void)
3323{
3324 char path[64];
3325 char driver_buf[64];
3326 char governor_buf[64];
3327 FILE *input;
3328 int turbo;
3329
3330 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3331 base_cpu);
3332 input = fopen(path, "r");
3333 if (input == NULL) {
3334 fprintf(stderr, "NSFOD %s\n", path);
3335 return;
3336 }
3337 fgets(driver_buf, sizeof(driver_buf), input);
3338 fclose(input);
3339
3340 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3341 base_cpu);
3342 input = fopen(path, "r");
3343 if (input == NULL) {
3344 fprintf(stderr, "NSFOD %s\n", path);
3345 return;
3346 }
3347 fgets(governor_buf, sizeof(governor_buf), input);
3348 fclose(input);
3349
3350 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3351 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3352
3353 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3354 input = fopen(path, "r");
3355 if (input != NULL) {
3356 fscanf(input, "%d", &turbo);
3357 fprintf(outf, "cpufreq boost: %d\n", turbo);
3358 fclose(input);
3359 }
3360
3361 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3362 input = fopen(path, "r");
3363 if (input != NULL) {
3364 fscanf(input, "%d", &turbo);
3365 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3366 fclose(input);
3367 }
3368}
Len Brown41618e62017-02-09 18:25:22 -05003369
Len Brown6574a5d2012-09-21 00:01:31 -04003370
Len Brown889facb2012-11-08 00:48:57 -05003371/*
3372 * print_epb()
3373 * Decode the ENERGY_PERF_BIAS MSR
3374 */
3375int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3376{
3377 unsigned long long msr;
3378 char *epb_string;
3379 int cpu;
3380
3381 if (!has_epb)
3382 return 0;
3383
3384 cpu = t->cpu_id;
3385
3386 /* EPB is per-package */
3387 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3388 return 0;
3389
3390 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003391 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003392 return -1;
3393 }
3394
3395 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3396 return 0;
3397
Len Browne9be7dd2015-05-26 12:19:37 -04003398 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003399 case ENERGY_PERF_BIAS_PERFORMANCE:
3400 epb_string = "performance";
3401 break;
3402 case ENERGY_PERF_BIAS_NORMAL:
3403 epb_string = "balanced";
3404 break;
3405 case ENERGY_PERF_BIAS_POWERSAVE:
3406 epb_string = "powersave";
3407 break;
3408 default:
3409 epb_string = "custom";
3410 break;
3411 }
Len Brownb7d8c142016-02-13 23:36:17 -05003412 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003413
3414 return 0;
3415}
Len Brown7f5c2582015-12-01 01:36:39 -05003416/*
3417 * print_hwp()
3418 * Decode the MSR_HWP_CAPABILITIES
3419 */
3420int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3421{
3422 unsigned long long msr;
3423 int cpu;
3424
3425 if (!has_hwp)
3426 return 0;
3427
3428 cpu = t->cpu_id;
3429
3430 /* MSR_HWP_CAPABILITIES is per-package */
3431 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3432 return 0;
3433
3434 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003435 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003436 return -1;
3437 }
3438
3439 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3440 return 0;
3441
Len Brownb7d8c142016-02-13 23:36:17 -05003442 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003443 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3444
3445 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3446 if ((msr & (1 << 0)) == 0)
3447 return 0;
3448
3449 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3450 return 0;
3451
Len Brownb7d8c142016-02-13 23:36:17 -05003452 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003453 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003454 cpu, msr,
3455 (unsigned int)HWP_HIGHEST_PERF(msr),
3456 (unsigned int)HWP_GUARANTEED_PERF(msr),
3457 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3458 (unsigned int)HWP_LOWEST_PERF(msr));
3459
3460 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3461 return 0;
3462
Len Brownb7d8c142016-02-13 23:36:17 -05003463 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003464 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003465 cpu, msr,
3466 (unsigned int)(((msr) >> 0) & 0xff),
3467 (unsigned int)(((msr) >> 8) & 0xff),
3468 (unsigned int)(((msr) >> 16) & 0xff),
3469 (unsigned int)(((msr) >> 24) & 0xff),
3470 (unsigned int)(((msr) >> 32) & 0xff3),
3471 (unsigned int)(((msr) >> 42) & 0x1));
3472
3473 if (has_hwp_pkg) {
3474 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3475 return 0;
3476
Len Brownb7d8c142016-02-13 23:36:17 -05003477 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003478 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003479 cpu, msr,
3480 (unsigned int)(((msr) >> 0) & 0xff),
3481 (unsigned int)(((msr) >> 8) & 0xff),
3482 (unsigned int)(((msr) >> 16) & 0xff),
3483 (unsigned int)(((msr) >> 24) & 0xff),
3484 (unsigned int)(((msr) >> 32) & 0xff3));
3485 }
3486 if (has_hwp_notify) {
3487 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3488 return 0;
3489
Len Brownb7d8c142016-02-13 23:36:17 -05003490 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003491 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3492 cpu, msr,
3493 ((msr) & 0x1) ? "EN" : "Dis",
3494 ((msr) & 0x2) ? "EN" : "Dis");
3495 }
3496 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3497 return 0;
3498
Len Brownb7d8c142016-02-13 23:36:17 -05003499 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003500 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3501 cpu, msr,
3502 ((msr) & 0x1) ? "" : "No-",
3503 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003504
3505 return 0;
3506}
3507
Len Brown3a9a9412014-08-15 02:39:52 -04003508/*
3509 * print_perf_limit()
3510 */
3511int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3512{
3513 unsigned long long msr;
3514 int cpu;
3515
3516 cpu = t->cpu_id;
3517
3518 /* per-package */
3519 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3520 return 0;
3521
3522 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003523 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003524 return -1;
3525 }
3526
3527 if (do_core_perf_limit_reasons) {
3528 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003529 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3530 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003531 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003532 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003533 (msr & 1 << 13) ? "Transitions, " : "",
3534 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3535 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3536 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3537 (msr & 1 << 9) ? "CorePwr, " : "",
3538 (msr & 1 << 8) ? "Amps, " : "",
3539 (msr & 1 << 6) ? "VR-Therm, " : "",
3540 (msr & 1 << 5) ? "Auto-HWP, " : "",
3541 (msr & 1 << 4) ? "Graphics, " : "",
3542 (msr & 1 << 2) ? "bit2, " : "",
3543 (msr & 1 << 1) ? "ThermStatus, " : "",
3544 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003545 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 -04003546 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003547 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003548 (msr & 1 << 29) ? "Transitions, " : "",
3549 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3550 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3551 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3552 (msr & 1 << 25) ? "CorePwr, " : "",
3553 (msr & 1 << 24) ? "Amps, " : "",
3554 (msr & 1 << 22) ? "VR-Therm, " : "",
3555 (msr & 1 << 21) ? "Auto-HWP, " : "",
3556 (msr & 1 << 20) ? "Graphics, " : "",
3557 (msr & 1 << 18) ? "bit18, " : "",
3558 (msr & 1 << 17) ? "ThermStatus, " : "",
3559 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003560
3561 }
3562 if (do_gfx_perf_limit_reasons) {
3563 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003564 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3565 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003566 (msr & 1 << 0) ? "PROCHOT, " : "",
3567 (msr & 1 << 1) ? "ThermStatus, " : "",
3568 (msr & 1 << 4) ? "Graphics, " : "",
3569 (msr & 1 << 6) ? "VR-Therm, " : "",
3570 (msr & 1 << 8) ? "Amps, " : "",
3571 (msr & 1 << 9) ? "GFXPwr, " : "",
3572 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3573 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003574 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003575 (msr & 1 << 16) ? "PROCHOT, " : "",
3576 (msr & 1 << 17) ? "ThermStatus, " : "",
3577 (msr & 1 << 20) ? "Graphics, " : "",
3578 (msr & 1 << 22) ? "VR-Therm, " : "",
3579 (msr & 1 << 24) ? "Amps, " : "",
3580 (msr & 1 << 25) ? "GFXPwr, " : "",
3581 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3582 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3583 }
3584 if (do_ring_perf_limit_reasons) {
3585 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003586 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3587 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003588 (msr & 1 << 0) ? "PROCHOT, " : "",
3589 (msr & 1 << 1) ? "ThermStatus, " : "",
3590 (msr & 1 << 6) ? "VR-Therm, " : "",
3591 (msr & 1 << 8) ? "Amps, " : "",
3592 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3593 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003594 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003595 (msr & 1 << 16) ? "PROCHOT, " : "",
3596 (msr & 1 << 17) ? "ThermStatus, " : "",
3597 (msr & 1 << 22) ? "VR-Therm, " : "",
3598 (msr & 1 << 24) ? "Amps, " : "",
3599 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3600 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3601 }
3602 return 0;
3603}
3604
Len Brown889facb2012-11-08 00:48:57 -05003605#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3606#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3607
Colin Ian King1b693172016-03-02 13:50:25 +00003608double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003609{
3610 unsigned long long msr;
3611
3612 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003613 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003614 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3615
3616 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003617 case INTEL_FAM6_ATOM_SILVERMONT1:
3618 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003619 return 30.0;
3620 default:
3621 return 135.0;
3622 }
3623}
3624
Andrey Semin40ee8e32014-12-05 00:07:00 -05003625/*
3626 * rapl_dram_energy_units_probe()
3627 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3628 */
3629static double
3630rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3631{
3632 /* only called for genuine_intel, family 6 */
3633
3634 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003635 case INTEL_FAM6_HASWELL_X: /* HSX */
3636 case INTEL_FAM6_BROADWELL_X: /* BDX */
3637 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3638 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003639 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003640 return (rapl_dram_energy_units = 15.3 / 1000000);
3641 default:
3642 return (rapl_energy_units);
3643 }
3644}
3645
Len Brown144b44b2013-11-09 00:30:16 -05003646
Len Brown889facb2012-11-08 00:48:57 -05003647/*
3648 * rapl_probe()
3649 *
Len Brown144b44b2013-11-09 00:30:16 -05003650 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003651 */
3652void rapl_probe(unsigned int family, unsigned int model)
3653{
3654 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003655 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003656 double tdp;
3657
3658 if (!genuine_intel)
3659 return;
3660
3661 if (family != 6)
3662 return;
3663
3664 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003665 case INTEL_FAM6_SANDYBRIDGE:
3666 case INTEL_FAM6_IVYBRIDGE:
3667 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3668 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3669 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3670 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3671 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003672 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003673 if (rapl_joules) {
3674 BIC_PRESENT(BIC_Pkg_J);
3675 BIC_PRESENT(BIC_Cor_J);
3676 BIC_PRESENT(BIC_GFX_J);
3677 } else {
3678 BIC_PRESENT(BIC_PkgWatt);
3679 BIC_PRESENT(BIC_CorWatt);
3680 BIC_PRESENT(BIC_GFXWatt);
3681 }
Len Brown889facb2012-11-08 00:48:57 -05003682 break;
Len Brown869ce692016-06-16 23:22:37 -04003683 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003684 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003685 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003686 if (rapl_joules)
3687 BIC_PRESENT(BIC_Pkg_J);
3688 else
3689 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003690 break;
Len Brown869ce692016-06-16 23:22:37 -04003691 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3692 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3693 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3694 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003695 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown81824922017-03-04 17:23:07 -05003696 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 -05003697 BIC_PRESENT(BIC_PKG__);
3698 BIC_PRESENT(BIC_RAM__);
3699 if (rapl_joules) {
3700 BIC_PRESENT(BIC_Pkg_J);
3701 BIC_PRESENT(BIC_Cor_J);
3702 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003703 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003704 } else {
3705 BIC_PRESENT(BIC_PkgWatt);
3706 BIC_PRESENT(BIC_CorWatt);
3707 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003708 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003709 }
Len Brown0b2bb692015-03-26 00:50:30 -04003710 break;
Len Brown869ce692016-06-16 23:22:37 -04003711 case INTEL_FAM6_HASWELL_X: /* HSX */
3712 case INTEL_FAM6_BROADWELL_X: /* BDX */
3713 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3714 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3715 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003716 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003717 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 -05003718 BIC_PRESENT(BIC_PKG__);
3719 BIC_PRESENT(BIC_RAM__);
3720 if (rapl_joules) {
3721 BIC_PRESENT(BIC_Pkg_J);
3722 BIC_PRESENT(BIC_RAM_J);
3723 } else {
3724 BIC_PRESENT(BIC_PkgWatt);
3725 BIC_PRESENT(BIC_RAMWatt);
3726 }
Len Browne6f9bb32013-12-03 02:19:19 -05003727 break;
Len Brown869ce692016-06-16 23:22:37 -04003728 case INTEL_FAM6_SANDYBRIDGE_X:
3729 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003730 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 -05003731 BIC_PRESENT(BIC_PKG__);
3732 BIC_PRESENT(BIC_RAM__);
3733 if (rapl_joules) {
3734 BIC_PRESENT(BIC_Pkg_J);
3735 BIC_PRESENT(BIC_Cor_J);
3736 BIC_PRESENT(BIC_RAM_J);
3737 } else {
3738 BIC_PRESENT(BIC_PkgWatt);
3739 BIC_PRESENT(BIC_CorWatt);
3740 BIC_PRESENT(BIC_RAMWatt);
3741 }
Len Brown144b44b2013-11-09 00:30:16 -05003742 break;
Len Brown869ce692016-06-16 23:22:37 -04003743 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3744 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003745 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003746 if (rapl_joules) {
3747 BIC_PRESENT(BIC_Pkg_J);
3748 BIC_PRESENT(BIC_Cor_J);
3749 } else {
3750 BIC_PRESENT(BIC_PkgWatt);
3751 BIC_PRESENT(BIC_CorWatt);
3752 }
Len Brown889facb2012-11-08 00:48:57 -05003753 break;
Len Brown869ce692016-06-16 23:22:37 -04003754 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003755 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 -05003756 BIC_PRESENT(BIC_PKG__);
3757 BIC_PRESENT(BIC_RAM__);
3758 if (rapl_joules) {
3759 BIC_PRESENT(BIC_Pkg_J);
3760 BIC_PRESENT(BIC_Cor_J);
3761 BIC_PRESENT(BIC_RAM_J);
3762 } else {
3763 BIC_PRESENT(BIC_PkgWatt);
3764 BIC_PRESENT(BIC_CorWatt);
3765 BIC_PRESENT(BIC_RAMWatt);
3766 }
Jacob Pan0f644902016-06-16 09:48:22 -07003767 break;
Len Brown889facb2012-11-08 00:48:57 -05003768 default:
3769 return;
3770 }
3771
3772 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003773 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003774 return;
3775
3776 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003777 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003778 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3779 else
3780 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003781
Andrey Semin40ee8e32014-12-05 00:07:00 -05003782 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3783
Len Brown144b44b2013-11-09 00:30:16 -05003784 time_unit = msr >> 16 & 0xF;
3785 if (time_unit == 0)
3786 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003787
Len Brown144b44b2013-11-09 00:30:16 -05003788 rapl_time_units = 1.0 / (1 << (time_unit));
3789
3790 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003791
3792 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003793 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003794 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003795
3796 return;
3797}
3798
Colin Ian King1b693172016-03-02 13:50:25 +00003799void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003800{
3801 if (!genuine_intel)
3802 return;
3803
3804 if (family != 6)
3805 return;
3806
3807 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003808 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3809 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3810 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003811 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003812 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003813 do_core_perf_limit_reasons = 1;
3814 do_ring_perf_limit_reasons = 1;
3815 default:
3816 return;
3817 }
3818}
3819
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003820void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3821{
3822 if (is_skx(family, model) || is_bdx(family, model))
3823 has_automatic_cstate_conversion = 1;
3824}
3825
Len Brown889facb2012-11-08 00:48:57 -05003826int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3827{
3828 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003829 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003830 int cpu;
3831
3832 if (!(do_dts || do_ptm))
3833 return 0;
3834
3835 cpu = t->cpu_id;
3836
3837 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003838 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003839 return 0;
3840
3841 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003842 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003843 return -1;
3844 }
3845
3846 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3847 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3848 return 0;
3849
3850 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003851 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003852 cpu, msr, tcc_activation_temp - dts);
3853
Len Brown889facb2012-11-08 00:48:57 -05003854 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3855 return 0;
3856
3857 dts = (msr >> 16) & 0x7F;
3858 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003859 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003860 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003861 }
3862
3863
Len Brownf4896fa52017-03-04 18:10:45 -05003864 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003865 unsigned int resolution;
3866
3867 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3868 return 0;
3869
3870 dts = (msr >> 16) & 0x7F;
3871 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003872 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003873 cpu, msr, tcc_activation_temp - dts, resolution);
3874
Len Brown889facb2012-11-08 00:48:57 -05003875 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3876 return 0;
3877
3878 dts = (msr >> 16) & 0x7F;
3879 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003880 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003881 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003882 }
3883
3884 return 0;
3885}
Len Brown36229892016-02-26 20:51:02 -05003886
Len Brown889facb2012-11-08 00:48:57 -05003887void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3888{
Len Brownb7d8c142016-02-13 23:36:17 -05003889 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003890 cpu, label,
3891 ((msr >> 15) & 1) ? "EN" : "DIS",
3892 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3893 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3894 (((msr >> 16) & 1) ? "EN" : "DIS"));
3895
3896 return;
3897}
3898
3899int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3900{
3901 unsigned long long msr;
3902 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003903
3904 if (!do_rapl)
3905 return 0;
3906
3907 /* RAPL counters are per package, so print only for 1st thread/package */
3908 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3909 return 0;
3910
3911 cpu = t->cpu_id;
3912 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003913 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003914 return -1;
3915 }
3916
3917 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3918 return -1;
3919
Len Brown96e47152017-01-21 02:26:00 -05003920 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3921 rapl_power_units, rapl_energy_units, rapl_time_units);
3922
Len Brown144b44b2013-11-09 00:30:16 -05003923 if (do_rapl & RAPL_PKG_POWER_INFO) {
3924
Len Brown889facb2012-11-08 00:48:57 -05003925 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3926 return -5;
3927
3928
Len Brownb7d8c142016-02-13 23:36:17 -05003929 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 -05003930 cpu, msr,
3931 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3932 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3933 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3934 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3935
Len Brown144b44b2013-11-09 00:30:16 -05003936 }
3937 if (do_rapl & RAPL_PKG) {
3938
Len Brown889facb2012-11-08 00:48:57 -05003939 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3940 return -9;
3941
Len Brownb7d8c142016-02-13 23:36:17 -05003942 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003943 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003944
3945 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003946 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003947 cpu,
3948 ((msr >> 47) & 1) ? "EN" : "DIS",
3949 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3950 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3951 ((msr >> 48) & 1) ? "EN" : "DIS");
3952 }
3953
Len Brown0b2bb692015-03-26 00:50:30 -04003954 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003955 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3956 return -6;
3957
Len Brownb7d8c142016-02-13 23:36:17 -05003958 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 -05003959 cpu, msr,
3960 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3961 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3962 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3963 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003964 }
3965 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003966 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3967 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003968 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003969 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003970
3971 print_power_limit_msr(cpu, msr, "DRAM Limit");
3972 }
Len Brown144b44b2013-11-09 00:30:16 -05003973 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003974 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3975 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003976
Len Brown96e47152017-01-21 02:26:00 -05003977 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003978 }
Jacob Pan91484942016-06-16 09:48:20 -07003979 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003980 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3981 return -9;
3982 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3983 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3984 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003985 }
3986 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003987 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3988 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003989
Len Brown96e47152017-01-21 02:26:00 -05003990 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003991
Len Brown96e47152017-01-21 02:26:00 -05003992 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3993 return -9;
3994 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3995 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3996 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003997 }
3998 return 0;
3999}
4000
Len Brownd7899442015-01-23 00:12:33 -05004001/*
4002 * SNB adds support for additional MSRs:
4003 *
4004 * MSR_PKG_C7_RESIDENCY 0x000003fa
4005 * MSR_CORE_C7_RESIDENCY 0x000003fe
4006 * MSR_PKG_C2_RESIDENCY 0x0000060d
4007 */
Len Brown103a8fe2010-10-22 23:53:03 -04004008
Len Brownd7899442015-01-23 00:12:33 -05004009int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04004010{
4011 if (!genuine_intel)
4012 return 0;
4013
4014 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004015 case INTEL_FAM6_SANDYBRIDGE:
4016 case INTEL_FAM6_SANDYBRIDGE_X:
4017 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4018 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4019 case INTEL_FAM6_HASWELL_CORE: /* HSW */
4020 case INTEL_FAM6_HASWELL_X: /* HSW */
4021 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4022 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
4023 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4024 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
4025 case INTEL_FAM6_BROADWELL_X: /* BDX */
4026 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
4027 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4028 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4029 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4030 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004031 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04004032 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4033 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004034 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08004035 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04004036 return 1;
4037 }
4038 return 0;
4039}
4040
Len Brownd7899442015-01-23 00:12:33 -05004041/*
4042 * HSW adds support for additional MSRs:
4043 *
Len Brown5a634262016-04-06 17:15:55 -04004044 * MSR_PKG_C8_RESIDENCY 0x00000630
4045 * MSR_PKG_C9_RESIDENCY 0x00000631
4046 * MSR_PKG_C10_RESIDENCY 0x00000632
4047 *
4048 * MSR_PKGC8_IRTL 0x00000633
4049 * MSR_PKGC9_IRTL 0x00000634
4050 * MSR_PKGC10_IRTL 0x00000635
4051 *
Len Brownd7899442015-01-23 00:12:33 -05004052 */
4053int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004054{
4055 if (!genuine_intel)
4056 return 0;
4057
4058 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004059 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4060 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4061 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4062 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4063 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4064 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004065 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04004066 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004067 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004068 return 1;
4069 }
4070 return 0;
4071}
4072
Len Brown0b2bb692015-03-26 00:50:30 -04004073/*
4074 * SKL adds support for additional MSRS:
4075 *
4076 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4077 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4078 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4079 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4080 */
4081int has_skl_msrs(unsigned int family, unsigned int model)
4082{
4083 if (!genuine_intel)
4084 return 0;
4085
4086 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004087 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4088 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4089 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4090 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004091 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown0b2bb692015-03-26 00:50:30 -04004092 return 1;
4093 }
4094 return 0;
4095}
4096
Len Brown144b44b2013-11-09 00:30:16 -05004097int is_slm(unsigned int family, unsigned int model)
4098{
4099 if (!genuine_intel)
4100 return 0;
4101 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004102 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
4103 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05004104 return 1;
4105 }
4106 return 0;
4107}
4108
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004109int is_knl(unsigned int family, unsigned int model)
4110{
4111 if (!genuine_intel)
4112 return 0;
4113 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004114 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05004115 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004116 return 1;
4117 }
4118 return 0;
4119}
4120
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004121int is_cnl(unsigned int family, unsigned int model)
4122{
4123 if (!genuine_intel)
4124 return 0;
4125
4126 switch (model) {
4127 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4128 return 1;
4129 }
4130
4131 return 0;
4132}
4133
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004134unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4135{
4136 if (is_knl(family, model))
4137 return 1024;
4138 return 1;
4139}
4140
Len Brown144b44b2013-11-09 00:30:16 -05004141#define SLM_BCLK_FREQS 5
4142double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4143
4144double slm_bclk(void)
4145{
4146 unsigned long long msr = 3;
4147 unsigned int i;
4148 double freq;
4149
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004150 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05004151 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05004152
4153 i = msr & 0xf;
4154 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05004155 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01004156 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05004157 }
4158 freq = slm_freq_table[i];
4159
Len Brown96e47152017-01-21 02:26:00 -05004160 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05004161 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05004162
4163 return freq;
4164}
4165
Len Brown103a8fe2010-10-22 23:53:03 -04004166double discover_bclk(unsigned int family, unsigned int model)
4167{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01004168 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04004169 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05004170 else if (is_slm(family, model))
4171 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04004172 else
4173 return 133.33;
4174}
4175
Len Brown889facb2012-11-08 00:48:57 -05004176/*
4177 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4178 * the Thermal Control Circuit (TCC) activates.
4179 * This is usually equal to tjMax.
4180 *
4181 * Older processors do not have this MSR, so there we guess,
4182 * but also allow cmdline over-ride with -T.
4183 *
4184 * Several MSR temperature values are in units of degrees-C
4185 * below this value, including the Digital Thermal Sensor (DTS),
4186 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4187 */
4188int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4189{
4190 unsigned long long msr;
4191 unsigned int target_c_local;
4192 int cpu;
4193
4194 /* tcc_activation_temp is used only for dts or ptm */
4195 if (!(do_dts || do_ptm))
4196 return 0;
4197
4198 /* this is a per-package concept */
4199 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4200 return 0;
4201
4202 cpu = t->cpu_id;
4203 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004204 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004205 return -1;
4206 }
4207
4208 if (tcc_activation_temp_override != 0) {
4209 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004210 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004211 cpu, tcc_activation_temp);
4212 return 0;
4213 }
4214
4215 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004216 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004217 goto guess;
4218
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004219 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004220 goto guess;
4221
Jean Delvare34821242014-05-01 11:40:19 +02004222 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004223
Len Brown96e47152017-01-21 02:26:00 -05004224 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004225 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004226 cpu, msr, target_c_local);
4227
Jean Delvare34821242014-05-01 11:40:19 +02004228 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004229 goto guess;
4230
4231 tcc_activation_temp = target_c_local;
4232
4233 return 0;
4234
4235guess:
4236 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004237 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004238 cpu, tcc_activation_temp);
4239
4240 return 0;
4241}
Len Brown69807a62015-11-21 12:22:47 -05004242
Len Brownaa8d8cc2016-03-11 13:26:03 -05004243void decode_feature_control_msr(void)
4244{
4245 unsigned long long msr;
4246
4247 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4248 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4249 base_cpu, msr,
4250 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4251 msr & (1 << 18) ? "SGX" : "");
4252}
4253
Len Brown69807a62015-11-21 12:22:47 -05004254void decode_misc_enable_msr(void)
4255{
4256 unsigned long long msr;
4257
Len Brownf26b1512017-06-23 20:45:54 -07004258 if (!genuine_intel)
4259 return;
4260
Len Brown69807a62015-11-21 12:22:47 -05004261 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004262 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004263 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004264 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4265 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004266 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004267 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4268 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004269}
4270
Len Brown33148d62017-01-21 01:26:16 -05004271void decode_misc_feature_control(void)
4272{
4273 unsigned long long msr;
4274
4275 if (!has_misc_feature_control)
4276 return;
4277
4278 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4279 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4280 base_cpu, msr,
4281 msr & (0 << 0) ? "No-" : "",
4282 msr & (1 << 0) ? "No-" : "",
4283 msr & (2 << 0) ? "No-" : "",
4284 msr & (3 << 0) ? "No-" : "");
4285}
Len Brownf0057312015-12-03 01:35:36 -05004286/*
4287 * Decode MSR_MISC_PWR_MGMT
4288 *
4289 * Decode the bits according to the Nehalem documentation
4290 * bit[0] seems to continue to have same meaning going forward
4291 * bit[1] less so...
4292 */
4293void decode_misc_pwr_mgmt_msr(void)
4294{
4295 unsigned long long msr;
4296
4297 if (!do_nhm_platform_info)
4298 return;
4299
Len Browncf4cbe52017-01-01 13:08:33 -05004300 if (no_MSR_MISC_PWR_MGMT)
4301 return;
4302
Len Brownf0057312015-12-03 01:35:36 -05004303 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004304 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 -05004305 base_cpu, msr,
4306 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004307 msr & (1 << 1) ? "EN" : "DIS",
4308 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004309}
Len Brown71616c82017-01-07 22:37:48 -05004310/*
4311 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4312 *
4313 * This MSRs are present on Silvermont processors,
4314 * Intel Atom processor E3000 series (Baytrail), and friends.
4315 */
4316void decode_c6_demotion_policy_msr(void)
4317{
4318 unsigned long long msr;
4319
4320 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4321 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4322 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4323
4324 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4325 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4326 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4327}
Len Brown7f5c2582015-12-01 01:36:39 -05004328
Len Brownfcd17212015-03-23 20:29:09 -04004329void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004330{
Len Brown61a87ba2015-11-23 02:30:51 -05004331 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004332 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004333 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004334
4335 eax = ebx = ecx = edx = 0;
4336
Len Brown5aea2f72016-03-13 03:14:35 -04004337 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004338
4339 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4340 genuine_intel = 1;
4341
Len Brown96e47152017-01-21 02:26:00 -05004342 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004343 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004344 (char *)&ebx, (char *)&edx, (char *)&ecx);
4345
Len Brown5aea2f72016-03-13 03:14:35 -04004346 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004347 family = (fms >> 8) & 0xf;
4348 model = (fms >> 4) & 0xf;
4349 stepping = fms & 0xf;
4350 if (family == 6 || family == 0xf)
4351 model += ((fms >> 16) & 0xf) << 4;
4352
Len Brown96e47152017-01-21 02:26:00 -05004353 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004354 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004355 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004356 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004357 ecx & (1 << 0) ? "SSE3" : "-",
4358 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004359 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004360 ecx & (1 << 7) ? "EIST" : "-",
4361 ecx & (1 << 8) ? "TM2" : "-",
4362 edx & (1 << 4) ? "TSC" : "-",
4363 edx & (1 << 5) ? "MSR" : "-",
4364 edx & (1 << 22) ? "ACPI-TM" : "-",
4365 edx & (1 << 29) ? "TM" : "-");
4366 }
Len Brown103a8fe2010-10-22 23:53:03 -04004367
Josh Triplettb2c95d92013-08-20 17:20:18 -07004368 if (!(edx & (1 << 5)))
4369 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004370
4371 /*
4372 * check max extended function levels of CPUID.
4373 * This is needed to check for invariant TSC.
4374 * This check is valid for both Intel and AMD.
4375 */
4376 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004377 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004378
Len Brown61a87ba2015-11-23 02:30:51 -05004379 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004380
Len Brownd7899442015-01-23 00:12:33 -05004381 /*
4382 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4383 * this check is valid for both Intel and AMD
4384 */
Len Brown5aea2f72016-03-13 03:14:35 -04004385 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004386 has_invariant_tsc = edx & (1 << 8);
4387 }
Len Brown103a8fe2010-10-22 23:53:03 -04004388
4389 /*
4390 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4391 * this check is valid for both Intel and AMD
4392 */
4393
Len Brown5aea2f72016-03-13 03:14:35 -04004394 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004395 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004396 if (has_aperf) {
4397 BIC_PRESENT(BIC_Avg_MHz);
4398 BIC_PRESENT(BIC_Busy);
4399 BIC_PRESENT(BIC_Bzy_MHz);
4400 }
Len Brown889facb2012-11-08 00:48:57 -05004401 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004402 if (do_dts)
4403 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004404 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004405 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004406 if (do_ptm)
4407 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004408 has_hwp = eax & (1 << 7);
4409 has_hwp_notify = eax & (1 << 8);
4410 has_hwp_activity_window = eax & (1 << 9);
4411 has_hwp_epp = eax & (1 << 10);
4412 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004413 has_epb = ecx & (1 << 3);
4414
Len Brown96e47152017-01-21 02:26:00 -05004415 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004416 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004417 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4418 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004419 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004420 do_dts ? "" : "No-",
4421 do_ptm ? "" : "No-",
4422 has_hwp ? "" : "No-",
4423 has_hwp_notify ? "" : "No-",
4424 has_hwp_activity_window ? "" : "No-",
4425 has_hwp_epp ? "" : "No-",
4426 has_hwp_pkg ? "" : "No-",
4427 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004428
Len Brown96e47152017-01-21 02:26:00 -05004429 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004430 decode_misc_enable_msr();
4431
Len Brown33148d62017-01-21 01:26:16 -05004432
Len Brown96e47152017-01-21 02:26:00 -05004433 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004434 int has_sgx;
4435
4436 ecx = 0;
4437
4438 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4439
4440 has_sgx = ebx & (1 << 2);
4441 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4442
4443 if (has_sgx)
4444 decode_feature_control_msr();
4445 }
4446
Len Brown61a87ba2015-11-23 02:30:51 -05004447 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004448 unsigned int eax_crystal;
4449 unsigned int ebx_tsc;
4450
4451 /*
4452 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4453 */
4454 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004455 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004456
4457 if (ebx_tsc != 0) {
4458
Len Brown96e47152017-01-21 02:26:00 -05004459 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004460 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004461 eax_crystal, ebx_tsc, crystal_hz);
4462
4463 if (crystal_hz == 0)
4464 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004465 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4466 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4467 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4468 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004469 crystal_hz = 24000000; /* 24.0 MHz */
4470 break;
Len Brown7268d402016-12-01 23:10:39 -05004471 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004472 crystal_hz = 25000000; /* 25.0 MHz */
4473 break;
Len Brown869ce692016-06-16 23:22:37 -04004474 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004475 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004476 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004477 break;
4478 default:
4479 crystal_hz = 0;
4480 }
4481
4482 if (crystal_hz) {
4483 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004484 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004485 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004486 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4487 }
4488 }
4489 }
Len Brown61a87ba2015-11-23 02:30:51 -05004490 if (max_level >= 0x16) {
4491 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4492
4493 /*
4494 * CPUID 16H Base MHz, Max MHz, Bus MHz
4495 */
4496 base_mhz = max_mhz = bus_mhz = edx = 0;
4497
Len Brown5aea2f72016-03-13 03:14:35 -04004498 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004499 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004500 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004501 base_mhz, max_mhz, bus_mhz);
4502 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004503
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004504 if (has_aperf)
4505 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4506
Len Brown812db3f2017-02-10 00:25:41 -05004507 BIC_PRESENT(BIC_IRQ);
4508 BIC_PRESENT(BIC_TSC_MHz);
4509
4510 if (probe_nhm_msrs(family, model)) {
4511 do_nhm_platform_info = 1;
4512 BIC_PRESENT(BIC_CPU_c1);
4513 BIC_PRESENT(BIC_CPU_c3);
4514 BIC_PRESENT(BIC_CPU_c6);
4515 BIC_PRESENT(BIC_SMI);
4516 }
Len Brownd7899442015-01-23 00:12:33 -05004517 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004518
4519 if (do_snb_cstates)
4520 BIC_PRESENT(BIC_CPU_c7);
4521
Len Brown5a634262016-04-06 17:15:55 -04004522 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004523 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4524 BIC_PRESENT(BIC_Pkgpc2);
4525 if (pkg_cstate_limit >= PCL__3)
4526 BIC_PRESENT(BIC_Pkgpc3);
4527 if (pkg_cstate_limit >= PCL__6)
4528 BIC_PRESENT(BIC_Pkgpc6);
4529 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4530 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004531 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004532 BIC_NOT_PRESENT(BIC_Pkgpc2);
4533 BIC_NOT_PRESENT(BIC_Pkgpc3);
4534 BIC_PRESENT(BIC_Pkgpc6);
4535 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004536 BIC_PRESENT(BIC_Mod_c6);
4537 use_c1_residency_msr = 1;
4538 }
Len Brown7170a372017-01-27 02:13:27 -05004539 if (is_dnv(family, model)) {
4540 BIC_PRESENT(BIC_CPU_c1);
4541 BIC_NOT_PRESENT(BIC_CPU_c3);
4542 BIC_NOT_PRESENT(BIC_Pkgpc3);
4543 BIC_NOT_PRESENT(BIC_CPU_c7);
4544 BIC_NOT_PRESENT(BIC_Pkgpc7);
4545 use_c1_residency_msr = 1;
4546 }
Len Brown34c761972017-01-27 02:36:41 -05004547 if (is_skx(family, model)) {
4548 BIC_NOT_PRESENT(BIC_CPU_c3);
4549 BIC_NOT_PRESENT(BIC_Pkgpc3);
4550 BIC_NOT_PRESENT(BIC_CPU_c7);
4551 BIC_NOT_PRESENT(BIC_Pkgpc7);
4552 }
Len Brownade0eba2017-02-10 01:56:47 -05004553 if (is_bdx(family, model)) {
4554 BIC_NOT_PRESENT(BIC_CPU_c7);
4555 BIC_NOT_PRESENT(BIC_Pkgpc7);
4556 }
Len Brown0f47c082017-01-27 00:50:45 -05004557 if (has_hsw_msrs(family, model)) {
4558 BIC_PRESENT(BIC_Pkgpc8);
4559 BIC_PRESENT(BIC_Pkgpc9);
4560 BIC_PRESENT(BIC_Pkgpc10);
4561 }
Len Brown5a634262016-04-06 17:15:55 -04004562 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004563 if (has_skl_msrs(family, model)) {
4564 BIC_PRESENT(BIC_Totl_c0);
4565 BIC_PRESENT(BIC_Any_c0);
4566 BIC_PRESENT(BIC_GFX_c0);
4567 BIC_PRESENT(BIC_CPUGFX);
4568 }
Len Brown144b44b2013-11-09 00:30:16 -05004569 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004570 do_knl_cstates = is_knl(family, model);
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004571 do_cnl_cstates = is_cnl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004572
Len Brown96e47152017-01-21 02:26:00 -05004573 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004574 decode_misc_pwr_mgmt_msr();
4575
Len Brown96e47152017-01-21 02:26:00 -05004576 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004577 decode_c6_demotion_policy_msr();
4578
Len Brown889facb2012-11-08 00:48:57 -05004579 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004580 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004581 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004582
Len Brown96e47152017-01-21 02:26:00 -05004583 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004584 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004585
Len Brown41618e62017-02-09 18:25:22 -05004586 if (!quiet)
4587 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004588 if (!quiet)
4589 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004590
Len Browna2b7b742015-09-26 00:12:38 -04004591 if (has_skl_msrs(family, model))
4592 calculate_tsc_tweak();
4593
Len Brown812db3f2017-02-10 00:25:41 -05004594 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4595 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004596
Len Brown812db3f2017-02-10 00:25:41 -05004597 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4598 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004599
Len Brownbe0e54c2018-06-01 12:35:53 -04004600 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4601 BIC_PRESENT(BIC_CPU_LPI);
4602 else
4603 BIC_NOT_PRESENT(BIC_CPU_LPI);
4604
4605 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4606 BIC_PRESENT(BIC_SYS_LPI);
4607 else
4608 BIC_NOT_PRESENT(BIC_SYS_LPI);
4609
Len Brown96e47152017-01-21 02:26:00 -05004610 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004611 decode_misc_feature_control();
4612
Len Brown889facb2012-11-08 00:48:57 -05004613 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004614}
4615
Len Brown103a8fe2010-10-22 23:53:03 -04004616
4617/*
4618 * in /dev/cpu/ return success for names that are numbers
4619 * ie. filter out ".", "..", "microcode".
4620 */
4621int dir_filter(const struct dirent *dirp)
4622{
4623 if (isdigit(dirp->d_name[0]))
4624 return 1;
4625 else
4626 return 0;
4627}
4628
4629int open_dev_cpu_msr(int dummy1)
4630{
4631 return 0;
4632}
4633
Len Brownc98d5d92012-06-04 00:56:40 -04004634void topology_probe()
4635{
4636 int i;
4637 int max_core_id = 0;
4638 int max_package_id = 0;
4639 int max_siblings = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004640
4641 /* Initialize num_cpus, max_cpu_num */
Prarit Bhargava843c5792018-06-01 10:04:28 -04004642 set_max_cpu_num();
Len Brownc98d5d92012-06-04 00:56:40 -04004643 topo.num_cpus = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004644 for_all_proc_cpus(count_cpus);
4645 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004646 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004647
Len Brownd8af6f52015-02-10 01:56:38 -05004648 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004649 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004650
4651 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004652 if (cpus == NULL)
4653 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004654
4655 /*
4656 * Allocate and initialize cpu_present_set
4657 */
4658 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004659 if (cpu_present_set == NULL)
4660 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004661 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4662 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4663 for_all_proc_cpus(mark_cpu_present);
4664
4665 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004666 * Validate that all cpus in cpu_subset are also in cpu_present_set
4667 */
4668 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4669 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4670 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4671 err(1, "cpu%d not present", i);
4672 }
4673
4674 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004675 * Allocate and initialize cpu_affinity_set
4676 */
4677 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004678 if (cpu_affinity_set == NULL)
4679 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004680 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4681 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4682
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004683 for_all_proc_cpus(init_thread_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004684
4685 /*
4686 * For online cpus
4687 * find max_core_id, max_package_id
4688 */
4689 for (i = 0; i <= topo.max_cpu_num; ++i) {
4690 int siblings;
4691
4692 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004693 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004694 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004695 continue;
4696 }
Len Brownc98d5d92012-06-04 00:56:40 -04004697
Len Brown0e2d8f02018-06-01 22:08:58 -04004698 cpus[i].logical_cpu_id = i;
4699
4700 /* get package information */
Len Brownc98d5d92012-06-04 00:56:40 -04004701 cpus[i].physical_package_id = get_physical_package_id(i);
4702 if (cpus[i].physical_package_id > max_package_id)
4703 max_package_id = cpus[i].physical_package_id;
4704
Len Brown0e2d8f02018-06-01 22:08:58 -04004705 /* get numa node information */
Prarit Bhargavaef605742018-06-01 10:04:30 -04004706 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4707 if (cpus[i].physical_node_id > topo.max_node_num)
4708 topo.max_node_num = cpus[i].physical_node_id;
Len Brown0e2d8f02018-06-01 22:08:58 -04004709
4710 /* get core information */
4711 cpus[i].physical_core_id = get_core_id(i);
4712 if (cpus[i].physical_core_id > max_core_id)
4713 max_core_id = cpus[i].physical_core_id;
4714
4715 /* get thread information */
4716 siblings = get_thread_siblings(&cpus[i]);
Len Brownc98d5d92012-06-04 00:56:40 -04004717 if (siblings > max_siblings)
4718 max_siblings = siblings;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004719 if (cpus[i].thread_id != -1)
4720 topo.num_cores++;
Len Brown0e2d8f02018-06-01 22:08:58 -04004721
Len Brownd8af6f52015-02-10 01:56:38 -05004722 if (debug > 1)
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004723 fprintf(outf,
4724 "cpu %d pkg %d node %d core %d thread %d\n",
Len Brown0e2d8f02018-06-01 22:08:58 -04004725 i, cpus[i].physical_package_id,
Prarit Bhargavaef605742018-06-01 10:04:30 -04004726 cpus[i].physical_node_id,
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004727 cpus[i].physical_core_id,
4728 cpus[i].thread_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004729 }
Prarit Bhargavaef605742018-06-01 10:04:30 -04004730
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004731 topo.cores_per_node = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004732 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004733 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004734 max_core_id, topo.cores_per_node);
4735 if (!summary_only && topo.cores_per_node > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004736 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004737
4738 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004739 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004740 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004741 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004742 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004743 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004744
Prarit Bhargavaef605742018-06-01 10:04:30 -04004745 set_node_data();
4746 if (debug > 1)
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004747 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
Prarit Bhargavaef605742018-06-01 10:04:30 -04004748
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004749 topo.threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004750 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004751 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004752}
4753
4754void
4755allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4756{
4757 int i;
4758
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004759 *t = calloc(topo.threads_per_core * topo.cores_per_node *
Len Brown678a3bd2017-02-09 22:22:13 -05004760 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004761 if (*t == NULL)
4762 goto error;
4763
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004764 for (i = 0; i < topo.threads_per_core *
4765 topo.cores_per_node * topo.num_packages; i++)
Len Brownc98d5d92012-06-04 00:56:40 -04004766 (*t)[i].cpu_id = -1;
4767
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004768 *c = calloc(topo.cores_per_node * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004769 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004770 if (*c == NULL)
4771 goto error;
4772
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004773 for (i = 0; i < topo.cores_per_node * topo.num_packages; i++)
Len Brownc98d5d92012-06-04 00:56:40 -04004774 (*c)[i].core_id = -1;
4775
Len Brown678a3bd2017-02-09 22:22:13 -05004776 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004777 if (*p == NULL)
4778 goto error;
4779
4780 for (i = 0; i < topo.num_packages; i++)
4781 (*p)[i].package_id = i;
4782
4783 return;
4784error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004785 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004786}
4787/*
4788 * init_counter()
4789 *
Len Brownc98d5d92012-06-04 00:56:40 -04004790 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
Len Brownc98d5d92012-06-04 00:56:40 -04004791 */
4792void init_counter(struct thread_data *thread_base, struct core_data *core_base,
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004793 struct pkg_data *pkg_base, int cpu_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004794{
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004795 int pkg_id = cpus[cpu_id].physical_package_id;
4796 int core_id = cpus[cpu_id].physical_core_id;
4797 int thread_id = cpus[cpu_id].thread_id;
Len Brownc98d5d92012-06-04 00:56:40 -04004798 struct thread_data *t;
4799 struct core_data *c;
4800 struct pkg_data *p;
4801
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004802 t = GET_THREAD(thread_base, thread_id, core_id, pkg_id);
4803 c = GET_CORE(core_base, core_id, pkg_id);
4804 p = GET_PKG(pkg_base, pkg_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004805
4806 t->cpu_id = cpu_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004807 if (thread_id == 0) {
Len Brownc98d5d92012-06-04 00:56:40 -04004808 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4809 if (cpu_is_first_core_in_package(cpu_id))
4810 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4811 }
4812
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004813 c->core_id = core_id;
4814 p->package_id = pkg_id;
Len Brownc98d5d92012-06-04 00:56:40 -04004815}
4816
4817
4818int initialize_counters(int cpu_id)
4819{
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004820 init_counter(EVEN_COUNTERS, cpu_id);
4821 init_counter(ODD_COUNTERS, cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004822 return 0;
4823}
4824
4825void allocate_output_buffer()
4826{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004827 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004828 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004829 if (outp == NULL)
4830 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004831}
Len Brown36229892016-02-26 20:51:02 -05004832void allocate_fd_percpu(void)
4833{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004834 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004835 if (fd_percpu == NULL)
4836 err(-1, "calloc fd_percpu");
4837}
Len Brown562a2d32016-02-26 23:48:05 -05004838void allocate_irq_buffers(void)
4839{
4840 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4841 if (irq_column_2_cpu == NULL)
4842 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004843
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004844 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004845 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004846 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004847}
Len Brownc98d5d92012-06-04 00:56:40 -04004848void setup_all_buffers(void)
4849{
4850 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004851 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004852 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004853 allocate_counters(&thread_even, &core_even, &package_even);
4854 allocate_counters(&thread_odd, &core_odd, &package_odd);
4855 allocate_output_buffer();
4856 for_all_proc_cpus(initialize_counters);
4857}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004858
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004859void set_base_cpu(void)
4860{
4861 base_cpu = sched_getcpu();
4862 if (base_cpu < 0)
4863 err(-ENODEV, "No valid cpus found");
4864
4865 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004866 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004867}
4868
Len Brown103a8fe2010-10-22 23:53:03 -04004869void turbostat_init()
4870{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004871 setup_all_buffers();
4872 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004873 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004874 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004875 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004876
Len Brown103a8fe2010-10-22 23:53:03 -04004877
Len Brown96e47152017-01-21 02:26:00 -05004878 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004879 for_all_cpus(print_hwp, ODD_COUNTERS);
4880
Len Brown96e47152017-01-21 02:26:00 -05004881 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004882 for_all_cpus(print_epb, ODD_COUNTERS);
4883
Len Brown96e47152017-01-21 02:26:00 -05004884 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004885 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4886
Len Brown96e47152017-01-21 02:26:00 -05004887 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004888 for_all_cpus(print_rapl, ODD_COUNTERS);
4889
4890 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4891
Len Brown96e47152017-01-21 02:26:00 -05004892 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004893 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004894
Len Brown96e47152017-01-21 02:26:00 -05004895 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004896 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004897}
4898
4899int fork_it(char **argv)
4900{
Len Brown103a8fe2010-10-22 23:53:03 -04004901 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004902 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004903
Len Brown218f0e82017-02-14 22:07:52 -05004904 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004905 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4906 if (status)
4907 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004908 /* clear affinity side-effect of get_counters() */
4909 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004910 gettimeofday(&tv_even, (struct timezone *)NULL);
4911
4912 child_pid = fork();
4913 if (!child_pid) {
4914 /* child */
4915 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004916 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004917 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004918
4919 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004920 if (child_pid == -1)
4921 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004922
4923 signal(SIGINT, SIG_IGN);
4924 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004925 if (waitpid(child_pid, &status, 0) == -1)
4926 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004927 }
Len Brownc98d5d92012-06-04 00:56:40 -04004928 /*
4929 * n.b. fork_it() does not check for errors from for_all_cpus()
4930 * because re-starting is problematic when forking
4931 */
Len Brown218f0e82017-02-14 22:07:52 -05004932 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004933 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004934 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004935 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004936 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4937 fprintf(outf, "%s: Counter reset detected\n", progname);
4938 else {
4939 compute_average(EVEN_COUNTERS);
4940 format_all_counters(EVEN_COUNTERS);
4941 }
Len Brown103a8fe2010-10-22 23:53:03 -04004942
Len Brownb7d8c142016-02-13 23:36:17 -05004943 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4944
4945 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004946
Len Brownd91bb172012-11-01 00:08:19 -04004947 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004948}
4949
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004950int get_and_dump_counters(void)
4951{
4952 int status;
4953
Len Brown218f0e82017-02-14 22:07:52 -05004954 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004955 status = for_all_cpus(get_counters, ODD_COUNTERS);
4956 if (status)
4957 return status;
4958
4959 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4960 if (status)
4961 return status;
4962
Len Brownb7d8c142016-02-13 23:36:17 -05004963 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004964
4965 return status;
4966}
4967
Len Brownd8af6f52015-02-10 01:56:38 -05004968void print_version() {
Len Brownf7d44a82017-05-27 21:24:58 -07004969 fprintf(outf, "turbostat version 17.06.23"
Len Brownd8af6f52015-02-10 01:56:38 -05004970 " - Len Brown <lenb@kernel.org>\n");
4971}
4972
Len Brown495c76542017-02-08 02:41:51 -05004973int add_counter(unsigned int msr_num, char *path, char *name,
4974 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004975 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004976{
4977 struct msr_counter *msrp;
4978
4979 msrp = calloc(1, sizeof(struct msr_counter));
4980 if (msrp == NULL) {
4981 perror("calloc");
4982 exit(1);
4983 }
4984
4985 msrp->msr_num = msr_num;
4986 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004987 if (path)
4988 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004989 msrp->width = width;
4990 msrp->type = type;
4991 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004992 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004993
4994 switch (scope) {
4995
4996 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004997 msrp->next = sys.tp;
4998 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004999 sys.added_thread_counters++;
Len Brown0748eaf2018-06-01 12:38:29 -04005000 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
Len Brown678a3bd2017-02-09 22:22:13 -05005001 fprintf(stderr, "exceeded max %d added thread counters\n",
5002 MAX_ADDED_COUNTERS);
5003 exit(-1);
5004 }
Len Brown388e9c82016-12-22 23:57:55 -05005005 break;
5006
5007 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05005008 msrp->next = sys.cp;
5009 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005010 sys.added_core_counters++;
5011 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5012 fprintf(stderr, "exceeded max %d added core counters\n",
5013 MAX_ADDED_COUNTERS);
5014 exit(-1);
5015 }
Len Brown388e9c82016-12-22 23:57:55 -05005016 break;
5017
5018 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05005019 msrp->next = sys.pp;
5020 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005021 sys.added_package_counters++;
5022 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5023 fprintf(stderr, "exceeded max %d added package counters\n",
5024 MAX_ADDED_COUNTERS);
5025 exit(-1);
5026 }
Len Brown388e9c82016-12-22 23:57:55 -05005027 break;
5028 }
5029
5030 return 0;
5031}
5032
5033void parse_add_command(char *add_command)
5034{
5035 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05005036 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05005037 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05005038 int width = 64;
5039 int fail = 0;
5040 enum counter_scope scope = SCOPE_CPU;
5041 enum counter_type type = COUNTER_CYCLES;
5042 enum counter_format format = FORMAT_DELTA;
5043
5044 while (add_command) {
5045
5046 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5047 goto next;
5048
5049 if (sscanf(add_command, "msr%d", &msr_num) == 1)
5050 goto next;
5051
Len Brown495c76542017-02-08 02:41:51 -05005052 if (*add_command == '/') {
5053 path = add_command;
5054 goto next;
5055 }
5056
Len Brown388e9c82016-12-22 23:57:55 -05005057 if (sscanf(add_command, "u%d", &width) == 1) {
5058 if ((width == 32) || (width == 64))
5059 goto next;
5060 width = 64;
5061 }
5062 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5063 scope = SCOPE_CPU;
5064 goto next;
5065 }
5066 if (!strncmp(add_command, "core", strlen("core"))) {
5067 scope = SCOPE_CORE;
5068 goto next;
5069 }
5070 if (!strncmp(add_command, "package", strlen("package"))) {
5071 scope = SCOPE_PACKAGE;
5072 goto next;
5073 }
5074 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5075 type = COUNTER_CYCLES;
5076 goto next;
5077 }
5078 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5079 type = COUNTER_SECONDS;
5080 goto next;
5081 }
Len Brown41618e62017-02-09 18:25:22 -05005082 if (!strncmp(add_command, "usec", strlen("usec"))) {
5083 type = COUNTER_USEC;
5084 goto next;
5085 }
Len Brown388e9c82016-12-22 23:57:55 -05005086 if (!strncmp(add_command, "raw", strlen("raw"))) {
5087 format = FORMAT_RAW;
5088 goto next;
5089 }
5090 if (!strncmp(add_command, "delta", strlen("delta"))) {
5091 format = FORMAT_DELTA;
5092 goto next;
5093 }
5094 if (!strncmp(add_command, "percent", strlen("percent"))) {
5095 format = FORMAT_PERCENT;
5096 goto next;
5097 }
5098
5099 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
5100 char *eos;
5101
5102 eos = strchr(name_buffer, ',');
5103 if (eos)
5104 *eos = '\0';
5105 goto next;
5106 }
5107
5108next:
5109 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05005110 if (add_command) {
5111 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05005112 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05005113 }
Len Brown388e9c82016-12-22 23:57:55 -05005114
5115 }
Len Brown495c76542017-02-08 02:41:51 -05005116 if ((msr_num == 0) && (path == NULL)) {
5117 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05005118 fail++;
5119 }
5120
5121 /* generate default column header */
5122 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05005123 if (width == 32)
5124 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5125 else
5126 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05005127 }
5128
Len Brown41618e62017-02-09 18:25:22 -05005129 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05005130 fail++;
5131
5132 if (fail) {
5133 help();
5134 exit(1);
5135 }
5136}
Len Brown41618e62017-02-09 18:25:22 -05005137
Len Browndd778a52017-02-21 23:21:13 -05005138int is_deferred_skip(char *name)
5139{
5140 int i;
5141
5142 for (i = 0; i < deferred_skip_index; ++i)
5143 if (!strcmp(name, deferred_skip_names[i]))
5144 return 1;
5145 return 0;
5146}
5147
Len Brown41618e62017-02-09 18:25:22 -05005148void probe_sysfs(void)
5149{
5150 char path[64];
5151 char name_buf[16];
5152 FILE *input;
5153 int state;
5154 char *sp;
5155
5156 if (!DO_BIC(BIC_sysfs))
5157 return;
5158
Len Brown0748eaf2018-06-01 12:38:29 -04005159 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005160
5161 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5162 base_cpu, state);
5163 input = fopen(path, "r");
5164 if (input == NULL)
5165 continue;
5166 fgets(name_buf, sizeof(name_buf), input);
5167
5168 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5169 sp = strchr(name_buf, '-');
5170 if (!sp)
5171 sp = strchrnul(name_buf, '\n');
5172 *sp = '%';
5173 *(sp + 1) = '\0';
5174
5175 fclose(input);
5176
5177 sprintf(path, "cpuidle/state%d/time", state);
5178
Len Browndd778a52017-02-21 23:21:13 -05005179 if (is_deferred_skip(name_buf))
5180 continue;
5181
Len Brown41618e62017-02-09 18:25:22 -05005182 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
5183 FORMAT_PERCENT, SYSFS_PERCPU);
5184 }
5185
Len Brown0748eaf2018-06-01 12:38:29 -04005186 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005187
5188 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5189 base_cpu, state);
5190 input = fopen(path, "r");
5191 if (input == NULL)
5192 continue;
5193 fgets(name_buf, sizeof(name_buf), input);
5194 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5195 sp = strchr(name_buf, '-');
5196 if (!sp)
5197 sp = strchrnul(name_buf, '\n');
5198 *sp = '\0';
5199 fclose(input);
5200
5201 sprintf(path, "cpuidle/state%d/usage", state);
5202
Len Browndd778a52017-02-21 23:21:13 -05005203 if (is_deferred_skip(name_buf))
5204 continue;
5205
Len Brown41618e62017-02-09 18:25:22 -05005206 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
5207 FORMAT_DELTA, SYSFS_PERCPU);
5208 }
5209
5210}
5211
Len Brown1ef7d212017-02-10 23:54:15 -05005212
5213/*
5214 * parse cpuset with following syntax
5215 * 1,2,4..6,8-10 and set bits in cpu_subset
5216 */
5217void parse_cpu_command(char *optarg)
5218{
5219 unsigned int start, end;
5220 char *next;
5221
Len Brown4e4e1e72017-02-21 22:33:42 -05005222 if (!strcmp(optarg, "core")) {
5223 if (cpu_subset)
5224 goto error;
5225 show_core_only++;
5226 return;
5227 }
5228 if (!strcmp(optarg, "package")) {
5229 if (cpu_subset)
5230 goto error;
5231 show_pkg_only++;
5232 return;
5233 }
5234 if (show_core_only || show_pkg_only)
5235 goto error;
5236
Len Brown1ef7d212017-02-10 23:54:15 -05005237 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5238 if (cpu_subset == NULL)
5239 err(3, "CPU_ALLOC");
5240 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5241
5242 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5243
5244 next = optarg;
5245
5246 while (next && *next) {
5247
5248 if (*next == '-') /* no negative cpu numbers */
5249 goto error;
5250
5251 start = strtoul(next, &next, 10);
5252
5253 if (start >= CPU_SUBSET_MAXCPUS)
5254 goto error;
5255 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5256
5257 if (*next == '\0')
5258 break;
5259
5260 if (*next == ',') {
5261 next += 1;
5262 continue;
5263 }
5264
5265 if (*next == '-') {
5266 next += 1; /* start range */
5267 } else if (*next == '.') {
5268 next += 1;
5269 if (*next == '.')
5270 next += 1; /* start range */
5271 else
5272 goto error;
5273 }
5274
5275 end = strtoul(next, &next, 10);
5276 if (end <= start)
5277 goto error;
5278
5279 while (++start <= end) {
5280 if (start >= CPU_SUBSET_MAXCPUS)
5281 goto error;
5282 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5283 }
5284
5285 if (*next == ',')
5286 next += 1;
5287 else if (*next != '\0')
5288 goto error;
5289 }
5290
5291 return;
5292
5293error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005294 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5295 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005296 exit(-1);
5297}
5298
Len Brown812db3f2017-02-10 00:25:41 -05005299
Len Brown103a8fe2010-10-22 23:53:03 -04005300void cmdline(int argc, char **argv)
5301{
5302 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005303 int option_index = 0;
5304 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005305 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005306 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005307 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005308 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005309 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005310 {"interval", required_argument, 0, 'i'},
Chen Yu023fe0a2018-04-26 08:41:03 +08005311 {"num_iterations", required_argument, 0, 'n'},
Len Brownd8af6f52015-02-10 01:56:38 -05005312 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005313 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005314 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005315 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005316 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005317 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005318 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005319 {"Summary", no_argument, 0, 'S'},
5320 {"TCC", required_argument, 0, 'T'},
5321 {"version", no_argument, 0, 'v' },
5322 {0, 0, 0, 0 }
5323 };
Len Brown103a8fe2010-10-22 23:53:03 -04005324
5325 progname = argv[0];
5326
Chen Yu023fe0a2018-04-26 08:41:03 +08005327 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005328 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005329 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005330 case 'a':
5331 parse_add_command(optarg);
5332 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005333 case 'c':
5334 parse_cpu_command(optarg);
5335 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005336 case 'D':
5337 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005338 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005339 case 'e':
5340 /* --enable specified counter */
5341 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5342 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005343 case 'd':
5344 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005345 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005346 break;
Len Brown812db3f2017-02-10 00:25:41 -05005347 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005348 /*
5349 * --hide: do not show those specified
5350 * multiple invocations simply clear more bits in enabled mask
5351 */
5352 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005353 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005354 case 'h':
5355 default:
5356 help();
5357 exit(1);
5358 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005359 {
5360 double interval = strtod(optarg, NULL);
5361
5362 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005363 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005364 interval);
5365 exit(2);
5366 }
5367
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005368 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005369 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005370 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005371 }
Len Brown889facb2012-11-08 00:48:57 -05005372 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005373 case 'J':
5374 rapl_joules++;
5375 break;
Len Brownc8ade362017-02-15 17:15:11 -05005376 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005377 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005378 list_header_only++;
5379 quiet++;
5380 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005381 case 'o':
5382 outf = fopen_or_die(optarg, "w");
5383 break;
Len Brown96e47152017-01-21 02:26:00 -05005384 case 'q':
5385 quiet = 1;
5386 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08005387 case 'n':
5388 num_iterations = strtod(optarg, NULL);
5389
5390 if (num_iterations <= 0) {
5391 fprintf(outf, "iterations %d should be positive number\n",
5392 num_iterations);
5393 exit(2);
5394 }
5395 break;
Len Brown812db3f2017-02-10 00:25:41 -05005396 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005397 /*
5398 * --show: show only those specified
5399 * The 1st invocation will clear and replace the enabled mask
5400 * subsequent invocations can add to it.
5401 */
5402 if (shown == 0)
5403 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5404 else
5405 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5406 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005407 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005408 case 'S':
5409 summary_only++;
5410 break;
5411 case 'T':
5412 tcc_activation_temp_override = atoi(optarg);
5413 break;
5414 case 'v':
5415 print_version();
5416 exit(0);
5417 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005418 }
5419 }
5420}
5421
5422int main(int argc, char **argv)
5423{
Len Brownb7d8c142016-02-13 23:36:17 -05005424 outf = stderr;
5425
Len Brown103a8fe2010-10-22 23:53:03 -04005426 cmdline(argc, argv);
5427
Len Brown96e47152017-01-21 02:26:00 -05005428 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005429 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005430
Len Brown41618e62017-02-09 18:25:22 -05005431 probe_sysfs();
5432
Len Brown103a8fe2010-10-22 23:53:03 -04005433 turbostat_init();
5434
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005435 /* dump counters and exit */
5436 if (dump_only)
5437 return get_and_dump_counters();
5438
Len Brownc8ade362017-02-15 17:15:11 -05005439 /* list header and exit */
5440 if (list_header_only) {
5441 print_header(",");
5442 flush_output_stdout();
5443 return 0;
5444 }
5445
Len Brown103a8fe2010-10-22 23:53:03 -04005446 /*
5447 * if any params left, it must be a command to fork
5448 */
5449 if (argc - optind)
5450 return fork_it(argv + optind);
5451 else
5452 turbostat_loop();
5453
5454 return 0;
5455}