blob: d6cff3070ebde60d2fa9a54deec6c147b6bda484 [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
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400219#define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \
220 ((thread_base) + \
221 ((pkg_no) * \
222 topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
223 ((node_no) * topo.cores_per_node * topo.threads_per_core) + \
224 ((core_no) * topo.threads_per_core) + \
225 (thread_no))
226
227#define GET_CORE(core_base, core_no, node_no, pkg_no) \
228 ((core_base) + \
229 ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \
230 ((node_no) * topo.cores_per_node) + \
231 (core_no))
232
233
Len Brownc98d5d92012-06-04 00:56:40 -0400234#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
235
Len Brown388e9c82016-12-22 23:57:55 -0500236enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500237enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500238enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
239
240struct msr_counter {
241 unsigned int msr_num;
242 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500243 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500244 unsigned int width;
245 enum counter_type type;
246 enum counter_format format;
247 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500248 unsigned int flags;
249#define FLAGS_HIDE (1 << 0)
250#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500251#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500252};
253
254struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500255 unsigned int added_thread_counters;
256 unsigned int added_core_counters;
257 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500258 struct msr_counter *tp;
259 struct msr_counter *cp;
260 struct msr_counter *pp;
261} sys;
262
Len Brownc98d5d92012-06-04 00:56:40 -0400263struct system_summary {
264 struct thread_data threads;
265 struct core_data cores;
266 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500267} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400268
Len Brown0e2d8f02018-06-01 22:08:58 -0400269struct cpu_topology {
270 int physical_package_id;
271 int logical_cpu_id;
Prarit Bhargavaef605742018-06-01 10:04:30 -0400272 int physical_node_id;
273 int logical_node_id; /* 0-based count within the package */
Len Brown0e2d8f02018-06-01 22:08:58 -0400274 int physical_core_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -0400275 int thread_id;
Len Brown0e2d8f02018-06-01 22:08:58 -0400276 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
277} *cpus;
Len Brownc98d5d92012-06-04 00:56:40 -0400278
279struct topo_params {
280 int num_packages;
281 int num_cpus;
282 int num_cores;
283 int max_cpu_num;
Prarit Bhargavaef605742018-06-01 10:04:30 -0400284 int max_node_num;
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400285 int nodes_per_pkg;
286 int cores_per_node;
287 int threads_per_core;
Len Brownc98d5d92012-06-04 00:56:40 -0400288} topo;
289
290struct timeval tv_even, tv_odd, tv_delta;
291
Len Brown562a2d32016-02-26 23:48:05 -0500292int *irq_column_2_cpu; /* /proc/interrupts column numbers */
293int *irqs_per_cpu; /* indexed by cpu_num */
294
Len Brownc98d5d92012-06-04 00:56:40 -0400295void setup_all_buffers(void);
296
297int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400298{
Len Brownc98d5d92012-06-04 00:56:40 -0400299 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400300}
Len Brown88c32812012-03-29 21:44:40 -0400301/*
Len Brownc98d5d92012-06-04 00:56:40 -0400302 * run func(thread, core, package) in topology order
303 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400304 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400305
Len Brownc98d5d92012-06-04 00:56:40 -0400306int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
307 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400308{
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400309 int retval, pkg_no, core_no, thread_no, node_no;
Len Brownc98d5d92012-06-04 00:56:40 -0400310
311 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -0400312 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400313 for (node_no = 0; node_no < topo.nodes_per_pkg;
314 node_no++) {
315 for (thread_no = 0; thread_no <
316 topo.threads_per_core; ++thread_no) {
317 struct thread_data *t;
318 struct core_data *c;
319 struct pkg_data *p;
Len Brownc98d5d92012-06-04 00:56:40 -0400320
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400321 t = GET_THREAD(thread_base, thread_no,
322 core_no, node_no,
323 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -0400324
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400325 if (cpu_is_not_present(t->cpu_id))
326 continue;
Len Brownc98d5d92012-06-04 00:56:40 -0400327
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400328 c = GET_CORE(core_base, core_no,
329 node_no, pkg_no);
330 p = GET_PKG(pkg_base, pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -0400331
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -0400332 retval = func(t, c, p);
333 if (retval)
334 return retval;
335 }
Len Brownc98d5d92012-06-04 00:56:40 -0400336 }
337 }
338 }
339 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400340}
341
342int cpu_migrate(int cpu)
343{
Len Brownc98d5d92012-06-04 00:56:40 -0400344 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
345 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
346 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400347 return -1;
348 else
349 return 0;
350}
Len Brown36229892016-02-26 20:51:02 -0500351int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400352{
Len Brown103a8fe2010-10-22 23:53:03 -0400353 char pathname[32];
354 int fd;
355
Len Brown36229892016-02-26 20:51:02 -0500356 fd = fd_percpu[cpu];
357
358 if (fd)
359 return fd;
360
Len Brown103a8fe2010-10-22 23:53:03 -0400361 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
362 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400363 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400364 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 -0400365
Len Brown36229892016-02-26 20:51:02 -0500366 fd_percpu[cpu] = fd;
367
368 return fd;
369}
370
371int get_msr(int cpu, off_t offset, unsigned long long *msr)
372{
373 ssize_t retval;
374
375 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400376
Len Brown98481e72014-08-15 00:36:50 -0400377 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500378 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400379
380 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400381}
382
Len Brownfc04cc62014-02-06 00:55:19 -0500383/*
Len Brown812db3f2017-02-10 00:25:41 -0500384 * Each string in this array is compared in --show and --hide cmdline.
385 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500386 */
Len Brown812db3f2017-02-10 00:25:41 -0500387struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400388 { 0x0, "usec" },
389 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500390 { 0x0, "Package" },
391 { 0x0, "Avg_MHz" },
392 { 0x0, "Bzy_MHz" },
393 { 0x0, "TSC_MHz" },
394 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500395 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500396 { 0x0, "Busy%" },
397 { 0x0, "CPU%c1" },
398 { 0x0, "CPU%c3" },
399 { 0x0, "CPU%c6" },
400 { 0x0, "CPU%c7" },
401 { 0x0, "ThreadC" },
402 { 0x0, "CoreTmp" },
403 { 0x0, "CoreCnt" },
404 { 0x0, "PkgTmp" },
405 { 0x0, "GFX%rc6" },
406 { 0x0, "GFXMHz" },
407 { 0x0, "Pkg%pc2" },
408 { 0x0, "Pkg%pc3" },
409 { 0x0, "Pkg%pc6" },
410 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500411 { 0x0, "Pkg%pc8" },
412 { 0x0, "Pkg%pc9" },
Len Brown4bd1f8f2018-01-28 23:42:42 -0500413 { 0x0, "Pk%pc10" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400414 { 0x0, "CPU%LPI" },
415 { 0x0, "SYS%LPI" },
Len Brown812db3f2017-02-10 00:25:41 -0500416 { 0x0, "PkgWatt" },
417 { 0x0, "CorWatt" },
418 { 0x0, "GFXWatt" },
419 { 0x0, "PkgCnt" },
420 { 0x0, "RAMWatt" },
421 { 0x0, "PKG_%" },
422 { 0x0, "RAM_%" },
423 { 0x0, "Pkg_J" },
424 { 0x0, "Cor_J" },
425 { 0x0, "GFX_J" },
426 { 0x0, "RAM_J" },
427 { 0x0, "Core" },
428 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500429 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500430 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400431 { 0x0, "Totl%C0" },
432 { 0x0, "Any%C0" },
433 { 0x0, "GFX%C0" },
434 { 0x0, "CPUGFX%" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400435 { 0x0, "Node%" },
Len Brown812db3f2017-02-10 00:25:41 -0500436};
437
Len Browna99d8732017-05-20 20:11:55 -0400438
439
Len Brown812db3f2017-02-10 00:25:41 -0500440#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400441#define BIC_USEC (1ULL << 0)
442#define BIC_TOD (1ULL << 1)
443#define BIC_Package (1ULL << 2)
444#define BIC_Avg_MHz (1ULL << 3)
445#define BIC_Bzy_MHz (1ULL << 4)
446#define BIC_TSC_MHz (1ULL << 5)
447#define BIC_IRQ (1ULL << 6)
448#define BIC_SMI (1ULL << 7)
449#define BIC_Busy (1ULL << 8)
450#define BIC_CPU_c1 (1ULL << 9)
451#define BIC_CPU_c3 (1ULL << 10)
452#define BIC_CPU_c6 (1ULL << 11)
453#define BIC_CPU_c7 (1ULL << 12)
454#define BIC_ThreadC (1ULL << 13)
455#define BIC_CoreTmp (1ULL << 14)
456#define BIC_CoreCnt (1ULL << 15)
457#define BIC_PkgTmp (1ULL << 16)
458#define BIC_GFX_rc6 (1ULL << 17)
459#define BIC_GFXMHz (1ULL << 18)
460#define BIC_Pkgpc2 (1ULL << 19)
461#define BIC_Pkgpc3 (1ULL << 20)
462#define BIC_Pkgpc6 (1ULL << 21)
463#define BIC_Pkgpc7 (1ULL << 22)
464#define BIC_Pkgpc8 (1ULL << 23)
465#define BIC_Pkgpc9 (1ULL << 24)
466#define BIC_Pkgpc10 (1ULL << 25)
Len Brownbe0e54c2018-06-01 12:35:53 -0400467#define BIC_CPU_LPI (1ULL << 26)
468#define BIC_SYS_LPI (1ULL << 27)
Len Brown3f44a5c2017-10-17 15:42:56 -0400469#define BIC_PkgWatt (1ULL << 26)
470#define BIC_CorWatt (1ULL << 27)
471#define BIC_GFXWatt (1ULL << 28)
472#define BIC_PkgCnt (1ULL << 29)
473#define BIC_RAMWatt (1ULL << 30)
474#define BIC_PKG__ (1ULL << 31)
475#define BIC_RAM__ (1ULL << 32)
476#define BIC_Pkg_J (1ULL << 33)
477#define BIC_Cor_J (1ULL << 34)
478#define BIC_GFX_J (1ULL << 35)
479#define BIC_RAM_J (1ULL << 36)
480#define BIC_Core (1ULL << 37)
481#define BIC_CPU (1ULL << 38)
482#define BIC_Mod_c6 (1ULL << 39)
483#define BIC_sysfs (1ULL << 40)
484#define BIC_Totl_c0 (1ULL << 41)
485#define BIC_Any_c0 (1ULL << 42)
486#define BIC_GFX_c0 (1ULL << 43)
487#define BIC_CPUGFX (1ULL << 44)
Prarit Bhargava01235042018-06-01 10:04:35 -0400488#define BIC_Node (1ULL << 45)
Len Brown812db3f2017-02-10 00:25:41 -0500489
Len Brown3f44a5c2017-10-17 15:42:56 -0400490#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
491
492unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
493unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500494
495#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400496#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500497#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500498#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500499
Len Brown3f44a5c2017-10-17 15:42:56 -0400500
Len Browndd778a52017-02-21 23:21:13 -0500501#define MAX_DEFERRED 16
502char *deferred_skip_names[MAX_DEFERRED];
503int deferred_skip_index;
504
505/*
506 * HIDE_LIST - hide this list of counters, show the rest [default]
507 * SHOW_LIST - show this list of counters, hide the rest
508 */
509enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
510
511void help(void)
512{
513 fprintf(outf,
514 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
515 "\n"
516 "Turbostat forks the specified COMMAND and prints statistics\n"
517 "when COMMAND completes.\n"
518 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
519 "to print statistics, until interrupted.\n"
520 "--add add a counter\n"
521 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
522 "--cpu cpu-set limit output to summary plus cpu-set:\n"
523 " {core | package | j,k,l..m,n-p }\n"
524 "--quiet skip decoding system configuration header\n"
Len Brownb9ad8ee2017-07-19 19:28:37 -0400525 "--interval sec.subsec Override default 5-second measurement interval\n"
Len Browndd778a52017-02-21 23:21:13 -0500526 "--help print this help message\n"
527 "--list list column headers only\n"
Chen Yu023fe0a2018-04-26 08:41:03 +0800528 "--num_iterations num number of the measurement iterations\n"
Len Browndd778a52017-02-21 23:21:13 -0500529 "--out file create or truncate \"file\" for all output\n"
530 "--version print version information\n"
531 "\n"
532 "For more help, run \"man turbostat\"\n");
533}
534
Len Brown812db3f2017-02-10 00:25:41 -0500535/*
536 * bic_lookup
537 * for all the strings in comma separate name_list,
538 * set the approprate bit in return value.
539 */
Len Browndd778a52017-02-21 23:21:13 -0500540unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500541{
542 int i;
543 unsigned long long retval = 0;
544
545 while (name_list) {
546 char *comma;
547
548 comma = strchr(name_list, ',');
549
550 if (comma)
551 *comma = '\0';
552
Len Brown3f44a5c2017-10-17 15:42:56 -0400553 if (!strcmp(name_list, "all"))
554 return ~0;
555
Len Brown812db3f2017-02-10 00:25:41 -0500556 for (i = 0; i < MAX_BIC; ++i) {
557 if (!strcmp(name_list, bic[i].name)) {
558 retval |= (1ULL << i);
559 break;
560 }
561 }
562 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500563 if (mode == SHOW_LIST) {
564 fprintf(stderr, "Invalid counter name: %s\n", name_list);
565 exit(-1);
566 }
567 deferred_skip_names[deferred_skip_index++] = name_list;
568 if (debug)
569 fprintf(stderr, "deferred \"%s\"\n", name_list);
570 if (deferred_skip_index >= MAX_DEFERRED) {
571 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
572 MAX_DEFERRED, name_list);
573 help();
574 exit(1);
575 }
Len Brown812db3f2017-02-10 00:25:41 -0500576 }
577
578 name_list = comma;
579 if (name_list)
580 name_list++;
581
582 }
583 return retval;
584}
Len Brownfc04cc62014-02-06 00:55:19 -0500585
Len Browndd778a52017-02-21 23:21:13 -0500586
Len Brownc8ade362017-02-15 17:15:11 -0500587void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400588{
Len Brown388e9c82016-12-22 23:57:55 -0500589 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500590 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500591
Len Brown3f44a5c2017-10-17 15:42:56 -0400592 if (DO_BIC(BIC_USEC))
593 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
594 if (DO_BIC(BIC_TOD))
595 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500596 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500597 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Prarit Bhargava01235042018-06-01 10:04:35 -0400598 if (DO_BIC(BIC_Node))
599 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500600 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500601 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500602 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500603 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500604 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500605 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500606 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500607 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500608 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500609 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500610 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500611 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500612
Len Brown0de6c0d2017-02-15 21:45:40 -0500613 if (DO_BIC(BIC_IRQ)) {
614 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500615 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500616 else
Len Brown6168c2e2017-02-16 23:07:51 -0500617 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500618 }
619
Len Brown812db3f2017-02-10 00:25:41 -0500620 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500621 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500622
Len Brown388e9c82016-12-22 23:57:55 -0500623 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500624
Len Brown388e9c82016-12-22 23:57:55 -0500625 if (mp->format == FORMAT_RAW) {
626 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500627 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500628 else
Len Browndd778a52017-02-21 23:21:13 -0500629 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500630 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500631 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500632 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500633 else
Len Browndd778a52017-02-21 23:21:13 -0500634 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500635 }
636 }
637
Len Brown41618e62017-02-09 18:25:22 -0500638 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500639 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700640 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500641 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500642 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500643 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500644 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500645 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500646
Len Brown0539ba12017-02-10 00:27:20 -0500647 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500648 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500649
Len Brown812db3f2017-02-10 00:25:41 -0500650 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500651 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500652
653 for (mp = sys.cp; mp; mp = mp->next) {
654 if (mp->format == FORMAT_RAW) {
655 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500656 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500657 else
Len Brownc8ade362017-02-15 17:15:11 -0500658 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500659 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500660 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
661 outp += sprintf(outp, "%s%8s", delim, mp->name);
662 else
663 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500664 }
665 }
666
Len Brown812db3f2017-02-10 00:25:41 -0500667 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500668 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500669
Len Brown812db3f2017-02-10 00:25:41 -0500670 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500671 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500672
Len Brown812db3f2017-02-10 00:25:41 -0500673 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500674 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500675
Len Browna99d8732017-05-20 20:11:55 -0400676 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500677 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400678 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500679 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400680 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500681 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400682 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500683 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400684
Len Brown0f47c082017-01-27 00:50:45 -0500685 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500686 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500687 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500688 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500689 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500690 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500691 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500692 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500693 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500694 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500695 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500696 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500697 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500698 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brownbe0e54c2018-06-01 12:35:53 -0400699 if (DO_BIC(BIC_CPU_LPI))
700 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
701 if (DO_BIC(BIC_SYS_LPI))
702 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400703
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800704 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500705 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500706 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500707 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500708 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500709 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500710 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500711 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500712 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500713 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500714 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500715 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500716 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500717 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500718 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500719 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500720 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500721 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500722 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500723 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500724 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500725 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500726 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500727 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500728 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500729 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800730 }
Len Brown388e9c82016-12-22 23:57:55 -0500731 for (mp = sys.pp; mp; mp = mp->next) {
732 if (mp->format == FORMAT_RAW) {
733 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500734 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500735 else
Len Brownc8ade362017-02-15 17:15:11 -0500736 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500737 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500738 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
739 outp += sprintf(outp, "%s%8s", delim, mp->name);
740 else
741 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500742 }
743 }
744
Len Brownc98d5d92012-06-04 00:56:40 -0400745 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400746}
747
Len Brownc98d5d92012-06-04 00:56:40 -0400748int dump_counters(struct thread_data *t, struct core_data *c,
749 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400750{
Len Brown388e9c82016-12-22 23:57:55 -0500751 int i;
752 struct msr_counter *mp;
753
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200754 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400755
Len Brownc98d5d92012-06-04 00:56:40 -0400756 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200757 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
758 t->cpu_id, t->flags);
759 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
760 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
761 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
762 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500763
Len Brown812db3f2017-02-10 00:25:41 -0500764 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500765 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500766 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500767 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500768
769 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
770 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
771 i, mp->msr_num, t->counter[i]);
772 }
Len Brownc98d5d92012-06-04 00:56:40 -0400773 }
Len Brown103a8fe2010-10-22 23:53:03 -0400774
Len Brownc98d5d92012-06-04 00:56:40 -0400775 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200776 outp += sprintf(outp, "core: %d\n", c->core_id);
777 outp += sprintf(outp, "c3: %016llX\n", c->c3);
778 outp += sprintf(outp, "c6: %016llX\n", c->c6);
779 outp += sprintf(outp, "c7: %016llX\n", c->c7);
780 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500781
782 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
783 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
784 i, mp->msr_num, c->counter[i]);
785 }
Len Brown0539ba12017-02-10 00:27:20 -0500786 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400787 }
788
789 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200790 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400791
792 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
793 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
794 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
795 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
796
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200797 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500798 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500799 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500800 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500801 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500802 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500803 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200804 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
805 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
806 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
Len Brownbe0e54c2018-06-01 12:35:53 -0400807 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
808 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
809 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200810 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
811 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
812 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
813 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
814 outp += sprintf(outp, "Throttle PKG: %0X\n",
815 p->rapl_pkg_perf_status);
816 outp += sprintf(outp, "Throttle RAM: %0X\n",
817 p->rapl_dram_perf_status);
818 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500819
820 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
821 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
822 i, mp->msr_num, p->counter[i]);
823 }
Len Brownc98d5d92012-06-04 00:56:40 -0400824 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200825
826 outp += sprintf(outp, "\n");
827
Len Brownc98d5d92012-06-04 00:56:40 -0400828 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400829}
830
Len Browne23da032012-02-06 18:37:16 -0500831/*
832 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500833 */
Len Brownc98d5d92012-06-04 00:56:40 -0400834int format_counters(struct thread_data *t, struct core_data *c,
835 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400836{
Len Brown008d396e2017-02-10 00:29:51 -0500837 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500838 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500839 int i;
840 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500841 char *delim = "\t";
842 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400843
Len Brownc98d5d92012-06-04 00:56:40 -0400844 /* if showing only 1st thread in core and this isn't one, bail out */
845 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
846 return 0;
847
848 /* if showing only 1st thread in pkg and this isn't one, bail out */
849 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
850 return 0;
851
Len Brown1ef7d212017-02-10 23:54:15 -0500852 /*if not summary line and --cpu is used */
853 if ((t != &average.threads) &&
854 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
855 return 0;
856
Len Brown3f44a5c2017-10-17 15:42:56 -0400857 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700858 /* on each row, print how many usec each timestamp took to gather */
859 struct timeval tv;
860
861 timersub(&t->tv_end, &t->tv_begin, &tv);
862 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
863 }
864
Len Brown3f44a5c2017-10-17 15:42:56 -0400865 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
866 if (DO_BIC(BIC_TOD))
867 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
868
Len Brown103a8fe2010-10-22 23:53:03 -0400869 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
870
Len Brown008d396e2017-02-10 00:29:51 -0500871 tsc = t->tsc * tsc_tweak;
872
Len Brownc98d5d92012-06-04 00:56:40 -0400873 /* topo columns, print blanks on 1st (average) line */
874 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500875 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500876 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Prarit Bhargava01235042018-06-01 10:04:35 -0400877 if (DO_BIC(BIC_Node))
878 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500879 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500880 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500881 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500882 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400883 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500884 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400885 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500886 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400887 else
Len Brown6168c2e2017-02-16 23:07:51 -0500888 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400889 }
Prarit Bhargava01235042018-06-01 10:04:35 -0400890 if (DO_BIC(BIC_Node)) {
891 if (t)
892 outp += sprintf(outp, "%s%d",
893 (printed++ ? delim : ""),
894 cpus[t->cpu_id].physical_node_id);
895 else
896 outp += sprintf(outp, "%s-",
897 (printed++ ? delim : ""));
898 }
Len Brown812db3f2017-02-10 00:25:41 -0500899 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400900 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500901 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400902 else
Len Brown6168c2e2017-02-16 23:07:51 -0500903 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400904 }
Len Brown812db3f2017-02-10 00:25:41 -0500905 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500906 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400907 }
Len Brownfc04cc62014-02-06 00:55:19 -0500908
Len Brown812db3f2017-02-10 00:25:41 -0500909 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500910 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500911 1.0 / units * t->aperf / interval_float);
912
Len Brown812db3f2017-02-10 00:25:41 -0500913 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500914 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400915
Len Brown812db3f2017-02-10 00:25:41 -0500916 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400917 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500918 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400919 else
Len Brown6168c2e2017-02-16 23:07:51 -0500920 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500921 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400922 }
Len Brown103a8fe2010-10-22 23:53:03 -0400923
Len Brown812db3f2017-02-10 00:25:41 -0500924 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500925 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400926
Len Brown562a2d32016-02-26 23:48:05 -0500927 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500928 if (DO_BIC(BIC_IRQ)) {
929 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500930 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500931 else
Len Brown6168c2e2017-02-16 23:07:51 -0500932 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500933 }
Len Brown562a2d32016-02-26 23:48:05 -0500934
Len Brown1cc21f72015-02-23 00:34:57 -0500935 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500936 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500937 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500938
Len Brown678a3bd2017-02-09 22:22:13 -0500939 /* Added counters */
940 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
941 if (mp->format == FORMAT_RAW) {
942 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500943 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500944 else
Len Brown6168c2e2017-02-16 23:07:51 -0500945 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500946 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500947 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500948 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500949 else
Len Brown6168c2e2017-02-16 23:07:51 -0500950 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500951 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500952 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500953 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500954 else
Len Brown6168c2e2017-02-16 23:07:51 -0500955 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500956 }
957 }
958
Len Brown41618e62017-02-09 18:25:22 -0500959 /* C1 */
960 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500961 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500962
963
Len Brownc98d5d92012-06-04 00:56:40 -0400964 /* print per-core data only for 1st thread in core */
965 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
966 goto done;
967
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700968 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500969 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500970 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500971 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500972 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500973 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400974
Len Brown0539ba12017-02-10 00:27:20 -0500975 /* Mod%c6 */
976 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500977 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500978
Len Brown812db3f2017-02-10 00:25:41 -0500979 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500980 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500981
Len Brown388e9c82016-12-22 23:57:55 -0500982 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
983 if (mp->format == FORMAT_RAW) {
984 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500985 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500986 else
Len Brown6168c2e2017-02-16 23:07:51 -0500987 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500988 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500989 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500990 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500991 else
Len Brown6168c2e2017-02-16 23:07:51 -0500992 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500993 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500994 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500995 }
996 }
997
Len Brownc98d5d92012-06-04 00:56:40 -0400998 /* print per-package data only for 1st core in package */
999 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1000 goto done;
1001
Len Brown0b2bb692015-03-26 00:50:30 -04001002 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -05001003 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -05001004 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -05001005
Len Brownfdf676e2016-02-27 01:28:12 -05001006 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -05001007 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -04001008 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -05001009 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -04001010 } else {
Len Brown6168c2e2017-02-16 23:07:51 -05001011 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -04001012 p->gfx_rc6_ms / 10.0 / interval_float);
1013 }
1014 }
Len Brownfdf676e2016-02-27 01:28:12 -05001015
Len Brown27d47352016-02-27 00:37:54 -05001016 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -05001017 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -05001018 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -05001019
Len Brown0b2bb692015-03-26 00:50:30 -04001020 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -04001021 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001022 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001023 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001024 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001025 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001026 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001027 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -05001028 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -04001029
Len Brown0f47c082017-01-27 00:50:45 -05001030 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -05001031 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001032 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -05001033 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001034 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -05001035 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001036 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -05001037 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001038 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -05001039 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001040 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -05001041 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001042 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -05001043 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -05001044
Len Brownbe0e54c2018-06-01 12:35:53 -04001045 if (DO_BIC(BIC_CPU_LPI))
1046 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1047 if (DO_BIC(BIC_SYS_LPI))
1048 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1049
Len Brown889facb2012-11-08 00:48:57 -05001050 /*
1051 * If measurement interval exceeds minimum RAPL Joule Counter range,
1052 * indicate that results are suspect by printing "**" in fraction place.
1053 */
Len Brownfc04cc62014-02-06 00:55:19 -05001054 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -05001055 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -05001056 else
Len Browne975db52016-04-06 23:56:02 -04001057 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -05001058
Len Brown812db3f2017-02-10 00:25:41 -05001059 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001060 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001061 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001062 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001063 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001064 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001065 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001066 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001067 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001068 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001069 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001070 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001071 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001072 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001073 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001074 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001075 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001076 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 -05001077 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001078 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 -05001079
Len Brown388e9c82016-12-22 23:57:55 -05001080 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1081 if (mp->format == FORMAT_RAW) {
1082 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001083 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001084 else
Len Brown6168c2e2017-02-16 23:07:51 -05001085 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001086 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001087 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001088 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001089 else
Len Brown6168c2e2017-02-16 23:07:51 -05001090 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001091 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001092 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001093 }
1094 }
1095
Len Brownc98d5d92012-06-04 00:56:40 -04001096done:
Len Brown94d6ab42018-01-27 22:39:21 -05001097 if (*(outp - 1) != '\n')
1098 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001099
1100 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001101}
1102
Len Brownb7d8c142016-02-13 23:36:17 -05001103void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001104{
Len Brownb7d8c142016-02-13 23:36:17 -05001105 FILE *filep;
1106
1107 if (outf == stderr)
1108 filep = stdout;
1109 else
1110 filep = outf;
1111
1112 fputs(output_buffer, filep);
1113 fflush(filep);
1114
Len Brownc98d5d92012-06-04 00:56:40 -04001115 outp = output_buffer;
1116}
Len Brownb7d8c142016-02-13 23:36:17 -05001117void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001118{
Len Brownb7d8c142016-02-13 23:36:17 -05001119 fputs(output_buffer, outf);
1120 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001121 outp = output_buffer;
1122}
1123void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1124{
Len Browne23da032012-02-06 18:37:16 -05001125 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001126
Len Browne23da032012-02-06 18:37:16 -05001127 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001128 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001129
Len Brownc98d5d92012-06-04 00:56:40 -04001130 if (topo.num_cpus > 1)
1131 format_counters(&average.threads, &average.cores,
1132 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001133
Len Browne23da032012-02-06 18:37:16 -05001134 printed = 1;
1135
1136 if (summary_only)
1137 return;
1138
Len Brownc98d5d92012-06-04 00:56:40 -04001139 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001140}
1141
Len Brown889facb2012-11-08 00:48:57 -05001142#define DELTA_WRAP32(new, old) \
1143 if (new > old) { \
1144 old = new - old; \
1145 } else { \
1146 old = 0x100000000 + new - old; \
1147 }
1148
Len Brownba3dec92016-04-22 20:31:46 -04001149int
Len Brownc98d5d92012-06-04 00:56:40 -04001150delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001151{
Len Brown388e9c82016-12-22 23:57:55 -05001152 int i;
1153 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001154
Len Browna99d8732017-05-20 20:11:55 -04001155
1156 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001157 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001158 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001159 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001160 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001161 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001162 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001163 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 -04001164
Len Brownc98d5d92012-06-04 00:56:40 -04001165 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001166 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001167 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001168 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001169 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001170 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001171 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001172 old->pc8 = new->pc8 - old->pc8;
1173 old->pc9 = new->pc9 - old->pc9;
1174 old->pc10 = new->pc10 - old->pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -04001175 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1176 old->sys_lpi = new->sys_lpi - old->sys_lpi;
Len Brown889facb2012-11-08 00:48:57 -05001177 old->pkg_temp_c = new->pkg_temp_c;
1178
Len Brown9185e982016-04-06 17:16:00 -04001179 /* flag an error when rc6 counter resets/wraps */
1180 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1181 old->gfx_rc6_ms = -1;
1182 else
1183 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1184
Len Brown27d47352016-02-27 00:37:54 -05001185 old->gfx_mhz = new->gfx_mhz;
1186
Len Brown889facb2012-11-08 00:48:57 -05001187 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1188 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1189 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1190 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1191 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1192 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001193
Len Brown388e9c82016-12-22 23:57:55 -05001194 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1195 if (mp->format == FORMAT_RAW)
1196 old->counter[i] = new->counter[i];
1197 else
1198 old->counter[i] = new->counter[i] - old->counter[i];
1199 }
1200
Len Brownba3dec92016-04-22 20:31:46 -04001201 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001202}
Len Brown103a8fe2010-10-22 23:53:03 -04001203
Len Brownc98d5d92012-06-04 00:56:40 -04001204void
1205delta_core(struct core_data *new, struct core_data *old)
1206{
Len Brown388e9c82016-12-22 23:57:55 -05001207 int i;
1208 struct msr_counter *mp;
1209
Len Brownc98d5d92012-06-04 00:56:40 -04001210 old->c3 = new->c3 - old->c3;
1211 old->c6 = new->c6 - old->c6;
1212 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001213 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001214 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001215
1216 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1217 if (mp->format == FORMAT_RAW)
1218 old->counter[i] = new->counter[i];
1219 else
1220 old->counter[i] = new->counter[i] - old->counter[i];
1221 }
Len Brownc98d5d92012-06-04 00:56:40 -04001222}
Len Brown103a8fe2010-10-22 23:53:03 -04001223
Len Brownc3ae3312012-06-13 21:31:46 -04001224/*
1225 * old = new - old
1226 */
Len Brownba3dec92016-04-22 20:31:46 -04001227int
Len Brownc98d5d92012-06-04 00:56:40 -04001228delta_thread(struct thread_data *new, struct thread_data *old,
1229 struct core_data *core_delta)
1230{
Len Brown388e9c82016-12-22 23:57:55 -05001231 int i;
1232 struct msr_counter *mp;
1233
Len Brown3f44a5c2017-10-17 15:42:56 -04001234 /*
1235 * the timestamps from start of measurement interval are in "old"
1236 * the timestamp from end of measurement interval are in "new"
1237 * over-write old w/ new so we can print end of interval values
1238 */
1239
1240 old->tv_begin = new->tv_begin;
1241 old->tv_end = new->tv_end;
1242
Len Brownc98d5d92012-06-04 00:56:40 -04001243 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001244
Len Brownc98d5d92012-06-04 00:56:40 -04001245 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001246 if (old->tsc < (1000 * 1000))
1247 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1248 "You can disable all c-states by booting with \"idle=poll\"\n"
1249 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001250
Len Brownc98d5d92012-06-04 00:56:40 -04001251 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001252
Len Brown812db3f2017-02-10 00:25:41 -05001253 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001254 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1255 old->aperf = new->aperf - old->aperf;
1256 old->mperf = new->mperf - old->mperf;
1257 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001258 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001259 }
Len Brownc98d5d92012-06-04 00:56:40 -04001260 }
Len Brown103a8fe2010-10-22 23:53:03 -04001261
Len Brown103a8fe2010-10-22 23:53:03 -04001262
Len Brown144b44b2013-11-09 00:30:16 -05001263 if (use_c1_residency_msr) {
1264 /*
1265 * Some models have a dedicated C1 residency MSR,
1266 * which should be more accurate than the derivation below.
1267 */
1268 } else {
1269 /*
1270 * As counter collection is not atomic,
1271 * it is possible for mperf's non-halted cycles + idle states
1272 * to exceed TSC's all cycles: show c1 = 0% in that case.
1273 */
Len Brown95149362017-04-12 19:44:51 -04001274 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001275 old->c1 = 0;
1276 else {
1277 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001278 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001279 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001280 }
Len Brownc98d5d92012-06-04 00:56:40 -04001281 }
Len Brownc3ae3312012-06-13 21:31:46 -04001282
Len Brownc98d5d92012-06-04 00:56:40 -04001283 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001284 if (debug > 1)
1285 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001286 old->mperf = 1; /* divide by 0 protection */
1287 }
1288
Len Brown812db3f2017-02-10 00:25:41 -05001289 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001290 old->irq_count = new->irq_count - old->irq_count;
1291
Len Brown812db3f2017-02-10 00:25:41 -05001292 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001293 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001294
Len Brown388e9c82016-12-22 23:57:55 -05001295 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1296 if (mp->format == FORMAT_RAW)
1297 old->counter[i] = new->counter[i];
1298 else
1299 old->counter[i] = new->counter[i] - old->counter[i];
1300 }
Len Brownba3dec92016-04-22 20:31:46 -04001301 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001302}
1303
1304int delta_cpu(struct thread_data *t, struct core_data *c,
1305 struct pkg_data *p, struct thread_data *t2,
1306 struct core_data *c2, struct pkg_data *p2)
1307{
Len Brownba3dec92016-04-22 20:31:46 -04001308 int retval = 0;
1309
Len Brownc98d5d92012-06-04 00:56:40 -04001310 /* calculate core delta only for 1st thread in core */
1311 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1312 delta_core(c, c2);
1313
1314 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001315 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1316 if (retval)
1317 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001318
1319 /* calculate package delta only for 1st core in package */
1320 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001321 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001322
Len Brownba3dec92016-04-22 20:31:46 -04001323 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001324}
1325
1326void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1327{
Len Brown388e9c82016-12-22 23:57:55 -05001328 int i;
1329 struct msr_counter *mp;
1330
Len Brown3f44a5c2017-10-17 15:42:56 -04001331 t->tv_begin.tv_sec = 0;
1332 t->tv_begin.tv_usec = 0;
1333 t->tv_end.tv_sec = 0;
1334 t->tv_end.tv_usec = 0;
1335
Len Brownc98d5d92012-06-04 00:56:40 -04001336 t->tsc = 0;
1337 t->aperf = 0;
1338 t->mperf = 0;
1339 t->c1 = 0;
1340
Len Brown562a2d32016-02-26 23:48:05 -05001341 t->irq_count = 0;
1342 t->smi_count = 0;
1343
Len Brownc98d5d92012-06-04 00:56:40 -04001344 /* tells format_counters to dump all fields from this set */
1345 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1346
1347 c->c3 = 0;
1348 c->c6 = 0;
1349 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001350 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001351 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001352
Len Brown0b2bb692015-03-26 00:50:30 -04001353 p->pkg_wtd_core_c0 = 0;
1354 p->pkg_any_core_c0 = 0;
1355 p->pkg_any_gfxe_c0 = 0;
1356 p->pkg_both_core_gfxe_c0 = 0;
1357
Len Brownc98d5d92012-06-04 00:56:40 -04001358 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001359 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001360 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001361 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001362 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001363 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001364 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001365 p->pc8 = 0;
1366 p->pc9 = 0;
1367 p->pc10 = 0;
Len Brownbe0e54c2018-06-01 12:35:53 -04001368 p->cpu_lpi = 0;
1369 p->sys_lpi = 0;
Len Brown889facb2012-11-08 00:48:57 -05001370
1371 p->energy_pkg = 0;
1372 p->energy_dram = 0;
1373 p->energy_cores = 0;
1374 p->energy_gfx = 0;
1375 p->rapl_pkg_perf_status = 0;
1376 p->rapl_dram_perf_status = 0;
1377 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001378
Len Brownfdf676e2016-02-27 01:28:12 -05001379 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001380 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001381 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1382 t->counter[i] = 0;
1383
1384 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1385 c->counter[i] = 0;
1386
1387 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1388 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001389}
1390int sum_counters(struct thread_data *t, struct core_data *c,
1391 struct pkg_data *p)
1392{
Len Brown388e9c82016-12-22 23:57:55 -05001393 int i;
1394 struct msr_counter *mp;
1395
Len Brown3f44a5c2017-10-17 15:42:56 -04001396 /* remember first tv_begin */
1397 if (average.threads.tv_begin.tv_sec == 0)
1398 average.threads.tv_begin = t->tv_begin;
1399
1400 /* remember last tv_end */
1401 average.threads.tv_end = t->tv_end;
1402
Len Brownc98d5d92012-06-04 00:56:40 -04001403 average.threads.tsc += t->tsc;
1404 average.threads.aperf += t->aperf;
1405 average.threads.mperf += t->mperf;
1406 average.threads.c1 += t->c1;
1407
Len Brown562a2d32016-02-26 23:48:05 -05001408 average.threads.irq_count += t->irq_count;
1409 average.threads.smi_count += t->smi_count;
1410
Len Brown388e9c82016-12-22 23:57:55 -05001411 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1412 if (mp->format == FORMAT_RAW)
1413 continue;
1414 average.threads.counter[i] += t->counter[i];
1415 }
1416
Len Brownc98d5d92012-06-04 00:56:40 -04001417 /* sum per-core values only for 1st thread in core */
1418 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1419 return 0;
1420
1421 average.cores.c3 += c->c3;
1422 average.cores.c6 += c->c6;
1423 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001424 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001425
Len Brown889facb2012-11-08 00:48:57 -05001426 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1427
Len Brown388e9c82016-12-22 23:57:55 -05001428 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1429 if (mp->format == FORMAT_RAW)
1430 continue;
1431 average.cores.counter[i] += c->counter[i];
1432 }
1433
Len Brownc98d5d92012-06-04 00:56:40 -04001434 /* sum per-pkg values only for 1st core in pkg */
1435 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1436 return 0;
1437
Len Browna99d8732017-05-20 20:11:55 -04001438 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001439 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001440 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001441 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001442 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001443 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001444 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001445 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001446
Len Brownc98d5d92012-06-04 00:56:40 -04001447 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001448 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001449 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001450 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001451 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001452 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001453 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001454 average.packages.pc8 += p->pc8;
1455 average.packages.pc9 += p->pc9;
1456 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001457
Len Brownbe0e54c2018-06-01 12:35:53 -04001458 average.packages.cpu_lpi = p->cpu_lpi;
1459 average.packages.sys_lpi = p->sys_lpi;
1460
Len Brown889facb2012-11-08 00:48:57 -05001461 average.packages.energy_pkg += p->energy_pkg;
1462 average.packages.energy_dram += p->energy_dram;
1463 average.packages.energy_cores += p->energy_cores;
1464 average.packages.energy_gfx += p->energy_gfx;
1465
Len Brownfdf676e2016-02-27 01:28:12 -05001466 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001467 average.packages.gfx_mhz = p->gfx_mhz;
1468
Len Brown889facb2012-11-08 00:48:57 -05001469 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1470
1471 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1472 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001473
1474 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1475 if (mp->format == FORMAT_RAW)
1476 continue;
1477 average.packages.counter[i] += p->counter[i];
1478 }
Len Brownc98d5d92012-06-04 00:56:40 -04001479 return 0;
1480}
1481/*
1482 * sum the counters for all cpus in the system
1483 * compute the weighted average
1484 */
1485void compute_average(struct thread_data *t, struct core_data *c,
1486 struct pkg_data *p)
1487{
Len Brown388e9c82016-12-22 23:57:55 -05001488 int i;
1489 struct msr_counter *mp;
1490
Len Brownc98d5d92012-06-04 00:56:40 -04001491 clear_counters(&average.threads, &average.cores, &average.packages);
1492
1493 for_all_cpus(sum_counters, t, c, p);
1494
1495 average.threads.tsc /= topo.num_cpus;
1496 average.threads.aperf /= topo.num_cpus;
1497 average.threads.mperf /= topo.num_cpus;
1498 average.threads.c1 /= topo.num_cpus;
1499
Len Brown0de6c0d2017-02-15 21:45:40 -05001500 if (average.threads.irq_count > 9999999)
1501 sums_need_wide_columns = 1;
1502
Len Brownc98d5d92012-06-04 00:56:40 -04001503 average.cores.c3 /= topo.num_cores;
1504 average.cores.c6 /= topo.num_cores;
1505 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001506 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001507
Len Browna99d8732017-05-20 20:11:55 -04001508 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001509 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001510 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001511 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001512 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001513 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001514 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001515 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001516
Len Brownc98d5d92012-06-04 00:56:40 -04001517 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001518 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001519 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001520 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001521 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001522 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001523 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001524
1525 average.packages.pc8 /= topo.num_packages;
1526 average.packages.pc9 /= topo.num_packages;
1527 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001528
1529 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1530 if (mp->format == FORMAT_RAW)
1531 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001532 if (mp->type == COUNTER_ITEMS) {
1533 if (average.threads.counter[i] > 9999999)
1534 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001535 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001536 }
Len Brown388e9c82016-12-22 23:57:55 -05001537 average.threads.counter[i] /= topo.num_cpus;
1538 }
1539 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1540 if (mp->format == FORMAT_RAW)
1541 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001542 if (mp->type == COUNTER_ITEMS) {
1543 if (average.cores.counter[i] > 9999999)
1544 sums_need_wide_columns = 1;
1545 }
Len Brown388e9c82016-12-22 23:57:55 -05001546 average.cores.counter[i] /= topo.num_cores;
1547 }
1548 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1549 if (mp->format == FORMAT_RAW)
1550 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001551 if (mp->type == COUNTER_ITEMS) {
1552 if (average.packages.counter[i] > 9999999)
1553 sums_need_wide_columns = 1;
1554 }
Len Brown388e9c82016-12-22 23:57:55 -05001555 average.packages.counter[i] /= topo.num_packages;
1556 }
Len Brownc98d5d92012-06-04 00:56:40 -04001557}
1558
1559static unsigned long long rdtsc(void)
1560{
1561 unsigned int low, high;
1562
1563 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1564
1565 return low | ((unsigned long long)high) << 32;
1566}
1567
Len Brownc98d5d92012-06-04 00:56:40 -04001568/*
Len Brown495c76542017-02-08 02:41:51 -05001569 * Open a file, and exit on failure
1570 */
1571FILE *fopen_or_die(const char *path, const char *mode)
1572{
1573 FILE *filep = fopen(path, mode);
1574
1575 if (!filep)
1576 err(1, "%s: open failed", path);
1577 return filep;
1578}
1579/*
1580 * snapshot_sysfs_counter()
1581 *
1582 * return snapshot of given counter
1583 */
1584unsigned long long snapshot_sysfs_counter(char *path)
1585{
1586 FILE *fp;
1587 int retval;
1588 unsigned long long counter;
1589
1590 fp = fopen_or_die(path, "r");
1591
1592 retval = fscanf(fp, "%lld", &counter);
1593 if (retval != 1)
1594 err(1, "snapshot_sysfs_counter(%s)", path);
1595
1596 fclose(fp);
1597
1598 return counter;
1599}
1600
1601int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1602{
1603 if (mp->msr_num != 0) {
1604 if (get_msr(cpu, mp->msr_num, counterp))
1605 return -1;
1606 } else {
Len Brown46c27972017-12-08 17:38:17 -05001607 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001608
1609 if (mp->flags & SYSFS_PERCPU) {
1610 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1611 cpu, mp->path);
1612
1613 *counterp = snapshot_sysfs_counter(path);
1614 } else {
1615 *counterp = snapshot_sysfs_counter(mp->path);
1616 }
Len Brown495c76542017-02-08 02:41:51 -05001617 }
1618
1619 return 0;
1620}
1621
1622/*
Len Brownc98d5d92012-06-04 00:56:40 -04001623 * get_counters(...)
1624 * migrate to cpu
1625 * acquire and record local counters for that cpu
1626 */
1627int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1628{
1629 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001630 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001631 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001632 struct msr_counter *mp;
1633 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001634
Len Brownf4fdf2b2017-05-27 21:06:55 -07001635
1636 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1637
Len Browne52966c2012-11-08 22:38:05 -05001638 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001639 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001640 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001641 }
Len Brownc98d5d92012-06-04 00:56:40 -04001642
Len Brown0102b062016-02-27 03:11:29 -05001643retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001644 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1645
Len Brown812db3f2017-02-10 00:25:41 -05001646 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001647 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1648
1649 /*
1650 * The TSC, APERF and MPERF must be read together for
1651 * APERF/MPERF and MPERF/TSC to give accurate results.
1652 *
1653 * Unfortunately, APERF and MPERF are read by
1654 * individual system call, so delays may occur
1655 * between them. If the time to read them
1656 * varies by a large amount, we re-read them.
1657 */
1658
1659 /*
1660 * This initial dummy APERF read has been seen to
1661 * reduce jitter in the subsequent reads.
1662 */
1663
Len Brown9c63a652012-10-31 01:29:52 -04001664 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001665 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001666
1667 t->tsc = rdtsc(); /* re-read close to APERF */
1668
1669 tsc_before = t->tsc;
1670
1671 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1672 return -3;
1673
1674 tsc_between = rdtsc();
1675
Len Brown9c63a652012-10-31 01:29:52 -04001676 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001677 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001678
1679 tsc_after = rdtsc();
1680
1681 aperf_time = tsc_between - tsc_before;
1682 mperf_time = tsc_after - tsc_between;
1683
1684 /*
1685 * If the system call latency to read APERF and MPERF
1686 * differ by more than 2x, then try again.
1687 */
1688 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1689 aperf_mperf_retry_count++;
1690 if (aperf_mperf_retry_count < 5)
1691 goto retry;
1692 else
1693 warnx("cpu%d jitter %lld %lld",
1694 cpu, aperf_time, mperf_time);
1695 }
1696 aperf_mperf_retry_count = 0;
1697
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001698 t->aperf = t->aperf * aperf_mperf_multiplier;
1699 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001700 }
1701
Len Brown812db3f2017-02-10 00:25:41 -05001702 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001703 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001704 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001705 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1706 return -5;
1707 t->smi_count = msr & 0xFFFFFFFF;
1708 }
Len Brown0539ba12017-02-10 00:27:20 -05001709 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001710 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1711 return -6;
1712 }
1713
Len Brown388e9c82016-12-22 23:57:55 -05001714 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001715 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001716 return -10;
1717 }
1718
Len Brownc98d5d92012-06-04 00:56:40 -04001719 /* collect core counters only for 1st thread in core */
1720 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001721 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001722
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07001723 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001724 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1725 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001726 }
1727
Len Brown812db3f2017-02-10 00:25:41 -05001728 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001729 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1730 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001731 } else if (do_knl_cstates) {
1732 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1733 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001734 }
1735
Len Brown812db3f2017-02-10 00:25:41 -05001736 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001737 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1738 return -8;
1739
Len Brown0539ba12017-02-10 00:27:20 -05001740 if (DO_BIC(BIC_Mod_c6))
1741 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1742 return -8;
1743
Len Brown812db3f2017-02-10 00:25:41 -05001744 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001745 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1746 return -9;
1747 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1748 }
1749
Len Brown388e9c82016-12-22 23:57:55 -05001750 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001751 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001752 return -10;
1753 }
Len Brown889facb2012-11-08 00:48:57 -05001754
Len Brownc98d5d92012-06-04 00:56:40 -04001755 /* collect package counters only for 1st core in package */
1756 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001757 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001758
Len Browna99d8732017-05-20 20:11:55 -04001759 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001760 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1761 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001762 }
1763 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001764 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1765 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001766 }
1767 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001768 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1769 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001770 }
1771 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001772 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1773 return -13;
1774 }
Len Brown0f47c082017-01-27 00:50:45 -05001775 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001776 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1777 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001778 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001779 if (do_slm_cstates) {
1780 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1781 return -10;
1782 } else {
1783 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1784 return -10;
1785 }
1786 }
1787
Len Brown0f47c082017-01-27 00:50:45 -05001788 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001789 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1790 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001791 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001792 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1793 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001794 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001795 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1796 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001797 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001798 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1799 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001800 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001801 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1802 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001803
Len Brownbe0e54c2018-06-01 12:35:53 -04001804 if (DO_BIC(BIC_CPU_LPI))
1805 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1806 if (DO_BIC(BIC_SYS_LPI))
1807 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1808
Len Brown889facb2012-11-08 00:48:57 -05001809 if (do_rapl & RAPL_PKG) {
1810 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1811 return -13;
1812 p->energy_pkg = msr & 0xFFFFFFFF;
1813 }
Jacob Pan91484942016-06-16 09:48:20 -07001814 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001815 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1816 return -14;
1817 p->energy_cores = msr & 0xFFFFFFFF;
1818 }
1819 if (do_rapl & RAPL_DRAM) {
1820 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1821 return -15;
1822 p->energy_dram = msr & 0xFFFFFFFF;
1823 }
1824 if (do_rapl & RAPL_GFX) {
1825 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1826 return -16;
1827 p->energy_gfx = msr & 0xFFFFFFFF;
1828 }
1829 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1830 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1831 return -16;
1832 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1833 }
1834 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1835 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1836 return -16;
1837 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1838 }
Len Brown812db3f2017-02-10 00:25:41 -05001839 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001840 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1841 return -17;
1842 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1843 }
Len Brownfdf676e2016-02-27 01:28:12 -05001844
Len Brown812db3f2017-02-10 00:25:41 -05001845 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001846 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1847
Len Brown812db3f2017-02-10 00:25:41 -05001848 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001849 p->gfx_mhz = gfx_cur_mhz;
1850
Len Brown388e9c82016-12-22 23:57:55 -05001851 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001852 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001853 return -10;
1854 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001855done:
1856 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001857
Len Brown103a8fe2010-10-22 23:53:03 -04001858 return 0;
1859}
1860
Len Brownee7e38e2015-02-09 23:39:45 -05001861/*
1862 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1863 * If you change the values, note they are used both in comparisons
1864 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1865 */
1866
1867#define PCLUKN 0 /* Unknown */
1868#define PCLRSV 1 /* Reserved */
1869#define PCL__0 2 /* PC0 */
1870#define PCL__1 3 /* PC1 */
1871#define PCL__2 4 /* PC2 */
1872#define PCL__3 5 /* PC3 */
1873#define PCL__4 6 /* PC4 */
1874#define PCL__6 7 /* PC6 */
1875#define PCL_6N 8 /* PC6 No Retention */
1876#define PCL_6R 9 /* PC6 Retention */
1877#define PCL__7 10 /* PC7 */
1878#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001879#define PCL__8 12 /* PC8 */
1880#define PCL__9 13 /* PC9 */
1881#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001882
1883int pkg_cstate_limit = PCLUKN;
1884char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001885 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001886
Len Browne9257f52015-04-01 21:02:57 -04001887int 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};
1888int 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};
1889int 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 -05001890int 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 -05001891int 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 -04001892int 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 -04001893int 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 +03001894int 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 -05001895
Len Browna2b7b742015-09-26 00:12:38 -04001896
1897static void
1898calculate_tsc_tweak()
1899{
Len Browna2b7b742015-09-26 00:12:38 -04001900 tsc_tweak = base_hz / tsc_hz;
1901}
1902
Len Brownfcd17212015-03-23 20:29:09 -04001903static void
1904dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001905{
1906 unsigned long long msr;
1907 unsigned int ratio;
1908
Len Brownec0adc52015-11-12 02:42:31 -05001909 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001910
Len Brownb7d8c142016-02-13 23:36:17 -05001911 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001912
Len Brown103a8fe2010-10-22 23:53:03 -04001913 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001914 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001915 ratio, bclk, ratio * bclk);
1916
1917 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001918 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001919 ratio, bclk, ratio * bclk);
1920
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001921 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001922 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001923 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001924
Len Brownfcd17212015-03-23 20:29:09 -04001925 return;
1926}
1927
1928static void
1929dump_hsw_turbo_ratio_limits(void)
1930{
1931 unsigned long long msr;
1932 unsigned int ratio;
1933
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001934 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001935
Len Brownb7d8c142016-02-13 23:36:17 -05001936 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001937
1938 ratio = (msr >> 8) & 0xFF;
1939 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001940 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001941 ratio, bclk, ratio * bclk);
1942
1943 ratio = (msr >> 0) & 0xFF;
1944 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001945 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001946 ratio, bclk, ratio * bclk);
1947 return;
1948}
1949
1950static void
1951dump_ivt_turbo_ratio_limits(void)
1952{
1953 unsigned long long msr;
1954 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001955
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001956 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001957
Len Brownb7d8c142016-02-13 23:36:17 -05001958 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001959
1960 ratio = (msr >> 56) & 0xFF;
1961 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001962 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001963 ratio, bclk, ratio * bclk);
1964
1965 ratio = (msr >> 48) & 0xFF;
1966 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001967 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001968 ratio, bclk, ratio * bclk);
1969
1970 ratio = (msr >> 40) & 0xFF;
1971 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001972 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001973 ratio, bclk, ratio * bclk);
1974
1975 ratio = (msr >> 32) & 0xFF;
1976 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001977 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001978 ratio, bclk, ratio * bclk);
1979
1980 ratio = (msr >> 24) & 0xFF;
1981 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001982 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001983 ratio, bclk, ratio * bclk);
1984
1985 ratio = (msr >> 16) & 0xFF;
1986 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001987 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001988 ratio, bclk, ratio * bclk);
1989
1990 ratio = (msr >> 8) & 0xFF;
1991 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001992 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001993 ratio, bclk, ratio * bclk);
1994
1995 ratio = (msr >> 0) & 0xFF;
1996 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001997 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001998 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001999 return;
2000}
Len Brown31e07522017-01-31 23:07:49 -05002001int has_turbo_ratio_group_limits(int family, int model)
2002{
2003
2004 if (!genuine_intel)
2005 return 0;
2006
2007 switch (model) {
2008 case INTEL_FAM6_ATOM_GOLDMONT:
2009 case INTEL_FAM6_SKYLAKE_X:
2010 case INTEL_FAM6_ATOM_DENVERTON:
2011 return 1;
2012 }
2013 return 0;
2014}
Len Brown6574a5d2012-09-21 00:01:31 -04002015
Len Brownfcd17212015-03-23 20:29:09 -04002016static void
Len Brown31e07522017-01-31 23:07:49 -05002017dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04002018{
Len Brown31e07522017-01-31 23:07:49 -05002019 unsigned long long msr, core_counts;
2020 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04002021
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002022 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002023 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04002024
Len Brown31e07522017-01-31 23:07:49 -05002025 if (has_turbo_ratio_group_limits(family, model)) {
2026 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
2027 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
2028 } else {
2029 core_counts = 0x0807060504030201;
2030 }
2031
Len Brown6574a5d2012-09-21 00:01:31 -04002032 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002033 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002034 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002035 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2036 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002037
2038 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002039 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002040 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002041 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2042 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002043
2044 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002045 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002046 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002047 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2048 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002049
2050 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002051 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002052 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002053 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2054 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002055
Len Brown103a8fe2010-10-22 23:53:03 -04002056 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002057 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002058 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002059 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2060 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002061
2062 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002063 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002064 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002065 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2066 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002067
2068 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002069 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002070 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002071 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2072 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002073
2074 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002075 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002076 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002077 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2078 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002079 return;
2080}
Len Brown3a9a9412014-08-15 02:39:52 -04002081
Len Brownfcd17212015-03-23 20:29:09 -04002082static void
Len Brown0f7887c2017-01-12 23:49:18 -05002083dump_atom_turbo_ratio_limits(void)
2084{
2085 unsigned long long msr;
2086 unsigned int ratio;
2087
2088 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2089 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2090
2091 ratio = (msr >> 0) & 0x3F;
2092 if (ratio)
2093 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2094 ratio, bclk, ratio * bclk);
2095
2096 ratio = (msr >> 8) & 0x3F;
2097 if (ratio)
2098 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2099 ratio, bclk, ratio * bclk);
2100
2101 ratio = (msr >> 16) & 0x3F;
2102 if (ratio)
2103 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2104 ratio, bclk, ratio * bclk);
2105
2106 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2107 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2108
2109 ratio = (msr >> 24) & 0x3F;
2110 if (ratio)
2111 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2112 ratio, bclk, ratio * bclk);
2113
2114 ratio = (msr >> 16) & 0x3F;
2115 if (ratio)
2116 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2117 ratio, bclk, ratio * bclk);
2118
2119 ratio = (msr >> 8) & 0x3F;
2120 if (ratio)
2121 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2122 ratio, bclk, ratio * bclk);
2123
2124 ratio = (msr >> 0) & 0x3F;
2125 if (ratio)
2126 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2127 ratio, bclk, ratio * bclk);
2128}
2129
2130static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002131dump_knl_turbo_ratio_limits(void)
2132{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002133 const unsigned int buckets_no = 7;
2134
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002135 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002136 int delta_cores, delta_ratio;
2137 int i, b_nr;
2138 unsigned int cores[buckets_no];
2139 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002140
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002141 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002142
Len Brownb7d8c142016-02-13 23:36:17 -05002143 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002144 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002145
2146 /**
2147 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002148 * [0] -- Reserved
2149 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002150 * [15:8] -- Base value of freq ratio of bucket 1.
2151 * [20:16] -- +ve delta of number of active cores of bucket 2.
2152 * i.e. active cores of bucket 2 =
2153 * active cores of bucket 1 + delta
2154 * [23:21] -- Negative delta of freq ratio of bucket 2.
2155 * i.e. freq ratio of bucket 2 =
2156 * freq ratio of bucket 1 - delta
2157 * [28:24]-- +ve delta of number of active cores of bucket 3.
2158 * [31:29]-- -ve delta of freq ratio of bucket 3.
2159 * [36:32]-- +ve delta of number of active cores of bucket 4.
2160 * [39:37]-- -ve delta of freq ratio of bucket 4.
2161 * [44:40]-- +ve delta of number of active cores of bucket 5.
2162 * [47:45]-- -ve delta of freq ratio of bucket 5.
2163 * [52:48]-- +ve delta of number of active cores of bucket 6.
2164 * [55:53]-- -ve delta of freq ratio of bucket 6.
2165 * [60:56]-- +ve delta of number of active cores of bucket 7.
2166 * [63:61]-- -ve delta of freq ratio of bucket 7.
2167 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002168
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002169 b_nr = 0;
2170 cores[b_nr] = (msr & 0xFF) >> 1;
2171 ratio[b_nr] = (msr >> 8) & 0xFF;
2172
2173 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002174 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002175 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002176
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002177 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2178 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2179 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002180 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002181
2182 for (i = buckets_no - 1; i >= 0; i--)
2183 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002184 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002185 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002186 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002187}
2188
2189static void
Len Brownfcd17212015-03-23 20:29:09 -04002190dump_nhm_cst_cfg(void)
2191{
2192 unsigned long long msr;
2193
Len Brown1df2e552017-01-07 23:24:57 -05002194 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002195
Len Brown1df2e552017-01-07 23:24:57 -05002196 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002197
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002198 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002199 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2200 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2201 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2202 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2203 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002204 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002205 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002206
2207#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2208 if (has_automatic_cstate_conversion) {
2209 fprintf(outf, ", automatic c-state conversion=%s",
2210 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2211 }
2212
2213 fprintf(outf, ")\n");
2214
Len Brownfcd17212015-03-23 20:29:09 -04002215 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002216}
2217
Len Brown6fb31432015-06-17 16:23:45 -04002218static void
2219dump_config_tdp(void)
2220{
2221 unsigned long long msr;
2222
2223 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002224 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002225 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002226
2227 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002228 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002229 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002230 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2231 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2232 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2233 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002234 }
Len Brownb7d8c142016-02-13 23:36:17 -05002235 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002236
2237 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002238 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002239 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002240 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2241 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2242 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2243 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002244 }
Len Brownb7d8c142016-02-13 23:36:17 -05002245 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002246
2247 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002248 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002249 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002250 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2251 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2252 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002253
Len Brown6fb31432015-06-17 16:23:45 -04002254 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002255 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002256 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002257 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2258 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002259}
Len Brown5a634262016-04-06 17:15:55 -04002260
2261unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2262
2263void print_irtl(void)
2264{
2265 unsigned long long msr;
2266
2267 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2268 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2269 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2270 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2271
2272 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2273 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2274 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2275 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2276
2277 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2278 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2279 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2280 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2281
2282 if (!do_irtl_hsw)
2283 return;
2284
2285 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2286 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2287 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2288 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2289
2290 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2291 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2292 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2293 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2294
2295 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2296 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2297 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2298 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2299
2300}
Len Brown36229892016-02-26 20:51:02 -05002301void free_fd_percpu(void)
2302{
2303 int i;
2304
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002305 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002306 if (fd_percpu[i] != 0)
2307 close(fd_percpu[i]);
2308 }
2309
2310 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002311}
2312
Len Brownc98d5d92012-06-04 00:56:40 -04002313void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002314{
Len Brown0e2d8f02018-06-01 22:08:58 -04002315 int i;
2316
Len Brownc98d5d92012-06-04 00:56:40 -04002317 CPU_FREE(cpu_present_set);
2318 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002319 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002320
Len Brownc98d5d92012-06-04 00:56:40 -04002321 CPU_FREE(cpu_affinity_set);
2322 cpu_affinity_set = NULL;
2323 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002324
Len Brownc98d5d92012-06-04 00:56:40 -04002325 free(thread_even);
2326 free(core_even);
2327 free(package_even);
2328
2329 thread_even = NULL;
2330 core_even = NULL;
2331 package_even = NULL;
2332
2333 free(thread_odd);
2334 free(core_odd);
2335 free(package_odd);
2336
2337 thread_odd = NULL;
2338 core_odd = NULL;
2339 package_odd = NULL;
2340
2341 free(output_buffer);
2342 output_buffer = NULL;
2343 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002344
2345 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002346
2347 free(irq_column_2_cpu);
2348 free(irqs_per_cpu);
Len Brown0e2d8f02018-06-01 22:08:58 -04002349
2350 for (i = 0; i <= topo.max_cpu_num; ++i) {
2351 if (cpus[i].put_ids)
2352 CPU_FREE(cpus[i].put_ids);
2353 }
2354 free(cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002355}
2356
Josh Triplett57a42a32013-08-20 17:20:17 -07002357
2358/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002359 * Parse a file containing a single int.
2360 */
2361int parse_int_file(const char *fmt, ...)
2362{
2363 va_list args;
2364 char path[PATH_MAX];
2365 FILE *filep;
2366 int value;
2367
2368 va_start(args, fmt);
2369 vsnprintf(path, sizeof(path), fmt, args);
2370 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002371 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002372 if (fscanf(filep, "%d", &value) != 1)
2373 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002374 fclose(filep);
2375 return value;
2376}
2377
2378/*
Len Brownc98d5d92012-06-04 00:56:40 -04002379 * cpu_is_first_core_in_package(cpu)
2380 * return 1 if given CPU is 1st core in package
2381 */
2382int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002383{
Josh Triplett95aebc42013-08-20 17:20:16 -07002384 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002385}
2386
2387int get_physical_package_id(int cpu)
2388{
Josh Triplett95aebc42013-08-20 17:20:16 -07002389 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002390}
2391
2392int get_core_id(int cpu)
2393{
Josh Triplett95aebc42013-08-20 17:20:16 -07002394 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002395}
2396
Prarit Bhargavaef605742018-06-01 10:04:30 -04002397void set_node_data(void)
2398{
2399 char path[80];
2400 FILE *filep;
2401 int pkg, node, cpu;
2402
2403 struct pkg_node_info {
2404 int count;
2405 int min;
2406 } *pni;
2407
2408 pni = calloc(topo.num_packages, sizeof(struct pkg_node_info));
2409 if (!pni)
2410 err(1, "calloc pkg_node_count");
2411
2412 for (pkg = 0; pkg < topo.num_packages; pkg++)
2413 pni[pkg].min = topo.num_cpus;
2414
2415 for (node = 0; node <= topo.max_node_num; node++) {
2416 /* find the "first" cpu in the node */
2417 sprintf(path, "/sys/bus/node/devices/node%d/cpulist", node);
2418 filep = fopen(path, "r");
2419 if (!filep)
2420 continue;
2421 fscanf(filep, "%d", &cpu);
2422 fclose(filep);
2423
2424 pkg = cpus[cpu].physical_package_id;
2425 pni[pkg].count++;
2426
2427 if (node < pni[pkg].min)
2428 pni[pkg].min = node;
2429 }
2430
2431 for (pkg = 0; pkg < topo.num_packages; pkg++)
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04002432 if (pni[pkg].count > topo.nodes_per_pkg)
2433 topo.nodes_per_pkg = pni[0].count;
Prarit Bhargavaef605742018-06-01 10:04:30 -04002434
2435 for (cpu = 0; cpu < topo.num_cpus; cpu++) {
2436 pkg = cpus[cpu].physical_package_id;
2437 node = cpus[cpu].physical_node_id;
2438 cpus[cpu].logical_node_id = node - pni[pkg].min;
2439 }
2440 free(pni);
2441
2442}
2443
2444int get_physical_node_id(struct cpu_topology *thiscpu)
Len Brownc98d5d92012-06-04 00:56:40 -04002445{
2446 char path[80];
2447 FILE *filep;
Len Brown0e2d8f02018-06-01 22:08:58 -04002448 int i;
2449 int cpu = thiscpu->logical_cpu_id;
Len Brownc98d5d92012-06-04 00:56:40 -04002450
Len Brown0e2d8f02018-06-01 22:08:58 -04002451 for (i = 0; i <= topo.max_cpu_num; i++) {
2452 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2453 cpu, i);
2454 filep = fopen(path, "r");
2455 if (!filep)
2456 continue;
2457 fclose(filep);
2458 return i;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002459 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002460 return -1;
2461}
Len Brownc98d5d92012-06-04 00:56:40 -04002462
Len Brown0e2d8f02018-06-01 22:08:58 -04002463int get_thread_siblings(struct cpu_topology *thiscpu)
2464{
2465 char path[80], character;
2466 FILE *filep;
2467 unsigned long map;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002468 int so, shift, sib_core;
Len Brown0e2d8f02018-06-01 22:08:58 -04002469 int cpu = thiscpu->logical_cpu_id;
2470 int offset = topo.max_cpu_num + 1;
2471 size_t size;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002472 int thread_id = 0;
Len Brown0e2d8f02018-06-01 22:08:58 -04002473
2474 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002475 if (thiscpu->thread_id < 0)
2476 thiscpu->thread_id = thread_id++;
Len Brown0e2d8f02018-06-01 22:08:58 -04002477 if (!thiscpu->put_ids)
2478 return -1;
2479
2480 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2481 CPU_ZERO_S(size, thiscpu->put_ids);
2482
2483 sprintf(path,
2484 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2485 filep = fopen_or_die(path, "r");
2486 do {
2487 offset -= BITMASK_SIZE;
2488 fscanf(filep, "%lx%c", &map, &character);
2489 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2490 if ((map >> shift) & 0x1) {
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002491 so = shift + offset;
2492 sib_core = get_core_id(so);
2493 if (sib_core == thiscpu->physical_core_id) {
2494 CPU_SET_S(so, size, thiscpu->put_ids);
2495 if ((so != cpu) &&
2496 (cpus[so].thread_id < 0))
2497 cpus[so].thread_id =
2498 thread_id++;
2499 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002500 }
2501 }
2502 } while (!strncmp(&character, ",", 1));
Len Brownc98d5d92012-06-04 00:56:40 -04002503 fclose(filep);
Len Brown0e2d8f02018-06-01 22:08:58 -04002504
2505 return CPU_COUNT_S(size, thiscpu->put_ids);
Len Brownc98d5d92012-06-04 00:56:40 -04002506}
2507
Len Brown103a8fe2010-10-22 23:53:03 -04002508/*
Len Brownc98d5d92012-06-04 00:56:40 -04002509 * run func(thread, core, package) in topology order
2510 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002511 */
2512
Len Brownc98d5d92012-06-04 00:56:40 -04002513int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2514 struct pkg_data *, struct thread_data *, struct core_data *,
2515 struct pkg_data *), struct thread_data *thread_base,
2516 struct core_data *core_base, struct pkg_data *pkg_base,
2517 struct thread_data *thread_base2, struct core_data *core_base2,
2518 struct pkg_data *pkg_base2)
2519{
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002520 int retval, pkg_no, node_no, core_no, thread_no;
Len Brownc98d5d92012-06-04 00:56:40 -04002521
2522 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002523 for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
2524 for (core_no = 0; core_no < topo.cores_per_node;
2525 ++core_no) {
2526 for (thread_no = 0; thread_no <
2527 topo.threads_per_core; ++thread_no) {
2528 struct thread_data *t, *t2;
2529 struct core_data *c, *c2;
2530 struct pkg_data *p, *p2;
Len Brownc98d5d92012-06-04 00:56:40 -04002531
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002532 t = GET_THREAD(thread_base, thread_no,
2533 core_no, node_no,
2534 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002535
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002536 if (cpu_is_not_present(t->cpu_id))
2537 continue;
Len Brownc98d5d92012-06-04 00:56:40 -04002538
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002539 t2 = GET_THREAD(thread_base2, thread_no,
2540 core_no, node_no,
2541 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002542
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002543 c = GET_CORE(core_base, core_no,
2544 node_no, pkg_no);
2545 c2 = GET_CORE(core_base2, core_no,
2546 node_no,
2547 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002548
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002549 p = GET_PKG(pkg_base, pkg_no);
2550 p2 = GET_PKG(pkg_base2, pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002551
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002552 retval = func(t, c, p, t2, c2, p2);
2553 if (retval)
2554 return retval;
2555 }
Len Brownc98d5d92012-06-04 00:56:40 -04002556 }
2557 }
2558 }
2559 return 0;
2560}
2561
2562/*
2563 * run func(cpu) on every cpu in /proc/stat
2564 * return max_cpu number
2565 */
2566int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002567{
2568 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002569 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002570 int retval;
2571
Josh Triplett57a42a32013-08-20 17:20:17 -07002572 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002573
2574 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002575 if (retval != 0)
2576 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002577
Len Brownc98d5d92012-06-04 00:56:40 -04002578 while (1) {
2579 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 -04002580 if (retval != 1)
2581 break;
2582
Len Brownc98d5d92012-06-04 00:56:40 -04002583 retval = func(cpu_num);
2584 if (retval) {
2585 fclose(fp);
2586 return(retval);
2587 }
Len Brown103a8fe2010-10-22 23:53:03 -04002588 }
2589 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002590 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002591}
2592
2593void re_initialize(void)
2594{
Len Brownc98d5d92012-06-04 00:56:40 -04002595 free_all_buffers();
2596 setup_all_buffers();
2597 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002598}
2599
Prarit Bhargava843c5792018-06-01 10:04:28 -04002600void set_max_cpu_num(void)
2601{
2602 FILE *filep;
2603 unsigned long dummy;
2604
2605 topo.max_cpu_num = 0;
2606 filep = fopen_or_die(
2607 "/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2608 "r");
2609 while (fscanf(filep, "%lx,", &dummy) == 1)
Len Brown0e2d8f02018-06-01 22:08:58 -04002610 topo.max_cpu_num += BITMASK_SIZE;
Prarit Bhargava843c5792018-06-01 10:04:28 -04002611 fclose(filep);
2612 topo.max_cpu_num--; /* 0 based */
2613}
Len Brownc98d5d92012-06-04 00:56:40 -04002614
Len Brown103a8fe2010-10-22 23:53:03 -04002615/*
Len Brownc98d5d92012-06-04 00:56:40 -04002616 * count_cpus()
2617 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002618 */
Len Brownc98d5d92012-06-04 00:56:40 -04002619int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002620{
Prarit Bhargava843c5792018-06-01 10:04:28 -04002621 topo.num_cpus++;
Len Brownc98d5d92012-06-04 00:56:40 -04002622 return 0;
2623}
2624int mark_cpu_present(int cpu)
2625{
2626 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002627 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002628}
2629
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002630int init_thread_id(int cpu)
2631{
2632 cpus[cpu].thread_id = -1;
2633 return 0;
2634}
2635
Len Brown562a2d32016-02-26 23:48:05 -05002636/*
2637 * snapshot_proc_interrupts()
2638 *
2639 * read and record summary of /proc/interrupts
2640 *
2641 * return 1 if config change requires a restart, else return 0
2642 */
2643int snapshot_proc_interrupts(void)
2644{
2645 static FILE *fp;
2646 int column, retval;
2647
2648 if (fp == NULL)
2649 fp = fopen_or_die("/proc/interrupts", "r");
2650 else
2651 rewind(fp);
2652
2653 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2654 for (column = 0; column < topo.num_cpus; ++column) {
2655 int cpu_number;
2656
2657 retval = fscanf(fp, " CPU%d", &cpu_number);
2658 if (retval != 1)
2659 break;
2660
2661 if (cpu_number > topo.max_cpu_num) {
2662 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2663 return 1;
2664 }
2665
2666 irq_column_2_cpu[column] = cpu_number;
2667 irqs_per_cpu[cpu_number] = 0;
2668 }
2669
2670 /* read /proc/interrupt count lines and sum up irqs per cpu */
2671 while (1) {
2672 int column;
2673 char buf[64];
2674
2675 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2676 if (retval != 1)
2677 break;
2678
2679 /* read the count per cpu */
2680 for (column = 0; column < topo.num_cpus; ++column) {
2681
2682 int cpu_number, irq_count;
2683
2684 retval = fscanf(fp, " %d", &irq_count);
2685 if (retval != 1)
2686 break;
2687
2688 cpu_number = irq_column_2_cpu[column];
2689 irqs_per_cpu[cpu_number] += irq_count;
2690
2691 }
2692
2693 while (getc(fp) != '\n')
2694 ; /* flush interrupt description */
2695
2696 }
2697 return 0;
2698}
Len Brown27d47352016-02-27 00:37:54 -05002699/*
Len Brownfdf676e2016-02-27 01:28:12 -05002700 * snapshot_gfx_rc6_ms()
2701 *
2702 * record snapshot of
2703 * /sys/class/drm/card0/power/rc6_residency_ms
2704 *
2705 * return 1 if config change requires a restart, else return 0
2706 */
2707int snapshot_gfx_rc6_ms(void)
2708{
2709 FILE *fp;
2710 int retval;
2711
2712 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2713
2714 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2715 if (retval != 1)
2716 err(1, "GFX rc6");
2717
2718 fclose(fp);
2719
2720 return 0;
2721}
2722/*
Len Brown27d47352016-02-27 00:37:54 -05002723 * snapshot_gfx_mhz()
2724 *
2725 * record snapshot of
2726 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2727 *
2728 * return 1 if config change requires a restart, else return 0
2729 */
2730int snapshot_gfx_mhz(void)
2731{
2732 static FILE *fp;
2733 int retval;
2734
2735 if (fp == NULL)
2736 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002737 else {
Len Brown27d47352016-02-27 00:37:54 -05002738 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002739 fflush(fp);
2740 }
Len Brown27d47352016-02-27 00:37:54 -05002741
2742 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2743 if (retval != 1)
2744 err(1, "GFX MHz");
2745
2746 return 0;
2747}
Len Brown562a2d32016-02-26 23:48:05 -05002748
2749/*
Len Brownbe0e54c2018-06-01 12:35:53 -04002750 * snapshot_cpu_lpi()
2751 *
2752 * record snapshot of
2753 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2754 *
2755 * return 1 if config change requires a restart, else return 0
2756 */
2757int snapshot_cpu_lpi_us(void)
2758{
2759 FILE *fp;
2760 int retval;
2761
2762 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2763
2764 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2765 if (retval != 1)
2766 err(1, "CPU LPI");
2767
2768 fclose(fp);
2769
2770 return 0;
2771}
2772/*
2773 * snapshot_sys_lpi()
2774 *
2775 * record snapshot of
2776 * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2777 *
2778 * return 1 if config change requires a restart, else return 0
2779 */
2780int snapshot_sys_lpi_us(void)
2781{
2782 FILE *fp;
2783 int retval;
2784
2785 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2786
2787 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2788 if (retval != 1)
2789 err(1, "SYS LPI");
2790
2791 fclose(fp);
2792
2793 return 0;
2794}
2795/*
Len Brown562a2d32016-02-26 23:48:05 -05002796 * snapshot /proc and /sys files
2797 *
2798 * return 1 if configuration restart needed, else return 0
2799 */
2800int snapshot_proc_sysfs_files(void)
2801{
Len Brown218f0e82017-02-14 22:07:52 -05002802 if (DO_BIC(BIC_IRQ))
2803 if (snapshot_proc_interrupts())
2804 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002805
Len Brown812db3f2017-02-10 00:25:41 -05002806 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002807 snapshot_gfx_rc6_ms();
2808
Len Brown812db3f2017-02-10 00:25:41 -05002809 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002810 snapshot_gfx_mhz();
2811
Len Brownbe0e54c2018-06-01 12:35:53 -04002812 if (DO_BIC(BIC_CPU_LPI))
2813 snapshot_cpu_lpi_us();
2814
2815 if (DO_BIC(BIC_SYS_LPI))
2816 snapshot_sys_lpi_us();
2817
Len Brown562a2d32016-02-26 23:48:05 -05002818 return 0;
2819}
2820
Len Brown8aa2ed02017-07-15 14:57:37 -04002821int exit_requested;
2822
2823static void signal_handler (int signal)
2824{
2825 switch (signal) {
2826 case SIGINT:
2827 exit_requested = 1;
2828 if (debug)
2829 fprintf(stderr, " SIGINT\n");
2830 break;
Len Brown07211962017-07-15 15:51:26 -04002831 case SIGUSR1:
2832 if (debug > 1)
2833 fprintf(stderr, "SIGUSR1\n");
2834 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002835 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002836 /* make sure this manually-invoked interval is at least 1ms long */
2837 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002838}
2839
2840void setup_signal_handler(void)
2841{
2842 struct sigaction sa;
2843
2844 memset(&sa, 0, sizeof(sa));
2845
2846 sa.sa_handler = &signal_handler;
2847
2848 if (sigaction(SIGINT, &sa, NULL) < 0)
2849 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002850 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2851 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002852}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002853
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002854void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002855{
2856 struct timeval select_timeout;
2857 fd_set readfds;
2858 int retval;
2859
2860 FD_ZERO(&readfds);
2861 FD_SET(0, &readfds);
2862
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002863 if (!isatty(fileno(stdin))) {
2864 nanosleep(&interval_ts, NULL);
2865 return;
2866 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002867
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002868 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002869 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2870
2871 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002872 switch (getc(stdin)) {
2873 case 'q':
2874 exit_requested = 1;
2875 break;
2876 }
2877 /* make sure this manually-invoked interval is at least 1ms long */
2878 nanosleep(&one_msec, NULL);
2879 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002880}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002881
Len Brown103a8fe2010-10-22 23:53:03 -04002882void turbostat_loop()
2883{
Len Brownc98d5d92012-06-04 00:56:40 -04002884 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002885 int restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002886 int done_iters = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002887
Len Brown8aa2ed02017-07-15 14:57:37 -04002888 setup_signal_handler();
2889
Len Brown103a8fe2010-10-22 23:53:03 -04002890restart:
Len Browne52966c2012-11-08 22:38:05 -05002891 restarted++;
2892
Len Brown562a2d32016-02-26 23:48:05 -05002893 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002894 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002895 if (retval < -1) {
2896 exit(retval);
2897 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002898 if (restarted > 1) {
2899 exit(retval);
2900 }
Len Brownc98d5d92012-06-04 00:56:40 -04002901 re_initialize();
2902 goto restart;
2903 }
Len Browne52966c2012-11-08 22:38:05 -05002904 restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002905 done_iters = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002906 gettimeofday(&tv_even, (struct timezone *)NULL);
2907
2908 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002909 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002910 re_initialize();
2911 goto restart;
2912 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002913 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002914 if (snapshot_proc_sysfs_files())
2915 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002916 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002917 if (retval < -1) {
2918 exit(retval);
2919 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002920 re_initialize();
2921 goto restart;
2922 }
Len Brown103a8fe2010-10-22 23:53:03 -04002923 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002924 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002925 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2926 re_initialize();
2927 goto restart;
2928 }
Len Brownc98d5d92012-06-04 00:56:40 -04002929 compute_average(EVEN_COUNTERS);
2930 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002931 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002932 if (exit_requested)
2933 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002934 if (num_iterations && ++done_iters >= num_iterations)
2935 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002936 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002937 if (snapshot_proc_sysfs_files())
2938 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002939 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002940 if (retval < -1) {
2941 exit(retval);
2942 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002943 re_initialize();
2944 goto restart;
2945 }
Len Brown103a8fe2010-10-22 23:53:03 -04002946 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002947 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002948 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2949 re_initialize();
2950 goto restart;
2951 }
Len Brownc98d5d92012-06-04 00:56:40 -04002952 compute_average(ODD_COUNTERS);
2953 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002954 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002955 if (exit_requested)
2956 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002957 if (num_iterations && ++done_iters >= num_iterations)
2958 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002959 }
2960}
2961
2962void check_dev_msr()
2963{
2964 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002965 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002966
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002967 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2968 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002969 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2970 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002971}
2972
Len Brown98481e72014-08-15 00:36:50 -04002973void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002974{
Len Brown98481e72014-08-15 00:36:50 -04002975 struct __user_cap_header_struct cap_header_data;
2976 cap_user_header_t cap_header = &cap_header_data;
2977 struct __user_cap_data_struct cap_data_data;
2978 cap_user_data_t cap_data = &cap_data_data;
2979 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2980 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002981 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002982
2983 /* check for CAP_SYS_RAWIO */
2984 cap_header->pid = getpid();
2985 cap_header->version = _LINUX_CAPABILITY_VERSION;
2986 if (capget(cap_header, cap_data) < 0)
2987 err(-6, "capget(2) failed");
2988
2989 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2990 do_exit++;
2991 warnx("capget(CAP_SYS_RAWIO) failed,"
2992 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2993 }
2994
2995 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002996 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2997 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002998 do_exit++;
2999 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
3000 }
3001
3002 /* if all else fails, thell them to be root */
3003 if (do_exit)
3004 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05003005 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04003006
3007 if (do_exit)
3008 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04003009}
3010
Len Brownd7899442015-01-23 00:12:33 -05003011/*
3012 * NHM adds support for additional MSRs:
3013 *
3014 * MSR_SMI_COUNT 0x00000034
3015 *
Len Brownec0adc52015-11-12 02:42:31 -05003016 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05003017 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05003018 *
Len Browncf4cbe52017-01-01 13:08:33 -05003019 * MSR_MISC_PWR_MGMT 0x000001aa
3020 *
Len Brownd7899442015-01-23 00:12:33 -05003021 * MSR_PKG_C3_RESIDENCY 0x000003f8
3022 * MSR_PKG_C6_RESIDENCY 0x000003f9
3023 * MSR_CORE_C3_RESIDENCY 0x000003fc
3024 * MSR_CORE_C6_RESIDENCY 0x000003fd
3025 *
Len Brownee7e38e2015-02-09 23:39:45 -05003026 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05003027 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05003028 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05003029 */
Len Brownee7e38e2015-02-09 23:39:45 -05003030int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003031{
Len Brownee7e38e2015-02-09 23:39:45 -05003032 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04003033 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05003034 int *pkg_cstate_limits;
3035
Len Brown103a8fe2010-10-22 23:53:03 -04003036 if (!genuine_intel)
3037 return 0;
3038
3039 if (family != 6)
3040 return 0;
3041
Len Brown21ed5572015-10-19 22:37:40 -04003042 bclk = discover_bclk(family, model);
3043
Len Brown103a8fe2010-10-22 23:53:03 -04003044 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003045 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3046 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04003047 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04003048 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
3049 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
3050 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3051 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05003052 pkg_cstate_limits = nhm_pkg_cstate_limits;
3053 break;
Len Brown869ce692016-06-16 23:22:37 -04003054 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3055 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3056 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3057 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05003058 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003059 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003060 break;
Len Brown869ce692016-06-16 23:22:37 -04003061 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3062 case INTEL_FAM6_HASWELL_X: /* HSX */
3063 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3064 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3065 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3066 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3067 case INTEL_FAM6_BROADWELL_X: /* BDX */
3068 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3069 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3070 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3071 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3072 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003073 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brownee7e38e2015-02-09 23:39:45 -05003074 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003075 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003076 break;
Len Brownd8ebb442016-12-01 20:27:46 -05003077 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3078 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003079 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05003080 break;
Len Brown869ce692016-06-16 23:22:37 -04003081 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05003082 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04003083 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05003084 pkg_cstate_limits = slv_pkg_cstate_limits;
3085 break;
Len Brown869ce692016-06-16 23:22:37 -04003086 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05003087 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05003088 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003089 break;
Len Brown869ce692016-06-16 23:22:37 -04003090 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05003091 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05003092 pkg_cstate_limits = phi_pkg_cstate_limits;
3093 break;
Len Brown869ce692016-06-16 23:22:37 -04003094 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003095 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04003096 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04003097 pkg_cstate_limits = bxt_pkg_cstate_limits;
3098 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003099 default:
3100 return 0;
3101 }
Len Brown1df2e552017-01-07 23:24:57 -05003102 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04003103 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05003104
Len Brownec0adc52015-11-12 02:42:31 -05003105 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04003106 base_ratio = (msr >> 8) & 0xFF;
3107
3108 base_hz = base_ratio * bclk * 1000000;
3109 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003110 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04003111}
Len Brown0f7887c2017-01-12 23:49:18 -05003112/*
Len Brown495c76542017-02-08 02:41:51 -05003113 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05003114 *
3115 * MSR_CC6_DEMOTION_POLICY_CONFIG
3116 * MSR_MC6_DEMOTION_POLICY_CONFIG
3117 */
3118
3119int has_slv_msrs(unsigned int family, unsigned int model)
3120{
3121 if (!genuine_intel)
3122 return 0;
3123
3124 switch (model) {
3125 case INTEL_FAM6_ATOM_SILVERMONT1:
3126 case INTEL_FAM6_ATOM_MERRIFIELD:
3127 case INTEL_FAM6_ATOM_MOOREFIELD:
3128 return 1;
3129 }
3130 return 0;
3131}
Len Brown7170a372017-01-27 02:13:27 -05003132int is_dnv(unsigned int family, unsigned int model)
3133{
3134
3135 if (!genuine_intel)
3136 return 0;
3137
3138 switch (model) {
3139 case INTEL_FAM6_ATOM_DENVERTON:
3140 return 1;
3141 }
3142 return 0;
3143}
Len Brownade0eba2017-02-10 01:56:47 -05003144int is_bdx(unsigned int family, unsigned int model)
3145{
3146
3147 if (!genuine_intel)
3148 return 0;
3149
3150 switch (model) {
3151 case INTEL_FAM6_BROADWELL_X:
3152 case INTEL_FAM6_BROADWELL_XEON_D:
3153 return 1;
3154 }
3155 return 0;
3156}
Len Brown34c761972017-01-27 02:36:41 -05003157int is_skx(unsigned int family, unsigned int model)
3158{
3159
3160 if (!genuine_intel)
3161 return 0;
3162
3163 switch (model) {
3164 case INTEL_FAM6_SKYLAKE_X:
3165 return 1;
3166 }
3167 return 0;
3168}
Len Brown0f7887c2017-01-12 23:49:18 -05003169
Len Brown31e07522017-01-31 23:07:49 -05003170int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05003171{
Len Brown0f7887c2017-01-12 23:49:18 -05003172 if (has_slv_msrs(family, model))
3173 return 0;
3174
Len Brownd7899442015-01-23 00:12:33 -05003175 switch (model) {
3176 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04003177 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3178 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3179 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05003180 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05003181 return 0;
3182 default:
3183 return 1;
3184 }
3185}
Len Brown0f7887c2017-01-12 23:49:18 -05003186int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3187{
3188 if (has_slv_msrs(family, model))
3189 return 1;
3190
3191 return 0;
3192}
Len Brown6574a5d2012-09-21 00:01:31 -04003193int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3194{
3195 if (!genuine_intel)
3196 return 0;
3197
3198 if (family != 6)
3199 return 0;
3200
3201 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003202 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3203 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04003204 return 1;
3205 default:
3206 return 0;
3207 }
3208}
Len Brownfcd17212015-03-23 20:29:09 -04003209int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3210{
3211 if (!genuine_intel)
3212 return 0;
3213
3214 if (family != 6)
3215 return 0;
3216
3217 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003218 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003219 return 1;
3220 default:
3221 return 0;
3222 }
3223}
3224
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003225int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3226{
3227 if (!genuine_intel)
3228 return 0;
3229
3230 if (family != 6)
3231 return 0;
3232
3233 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003234 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003235 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003236 return 1;
3237 default:
3238 return 0;
3239 }
3240}
Len Brown31e07522017-01-31 23:07:49 -05003241int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3242{
3243 if (!genuine_intel)
3244 return 0;
3245
3246 if (family != 6)
3247 return 0;
3248
3249 switch (model) {
3250 case INTEL_FAM6_ATOM_GOLDMONT:
3251 case INTEL_FAM6_SKYLAKE_X:
3252 return 1;
3253 default:
3254 return 0;
3255 }
3256}
Len Brown6fb31432015-06-17 16:23:45 -04003257int has_config_tdp(unsigned int family, unsigned int model)
3258{
3259 if (!genuine_intel)
3260 return 0;
3261
3262 if (family != 6)
3263 return 0;
3264
3265 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003266 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3267 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3268 case INTEL_FAM6_HASWELL_X: /* HSX */
3269 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3270 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3271 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3272 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3273 case INTEL_FAM6_BROADWELL_X: /* BDX */
3274 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3275 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3276 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3277 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3278 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003279 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04003280 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003281
Len Brown869ce692016-06-16 23:22:37 -04003282 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003283 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003284 return 1;
3285 default:
3286 return 0;
3287 }
3288}
3289
Len Brownfcd17212015-03-23 20:29:09 -04003290static void
Colin Ian King1b693172016-03-02 13:50:25 +00003291dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003292{
3293 if (!do_nhm_platform_info)
3294 return;
3295
3296 dump_nhm_platform_info();
3297
3298 if (has_hsw_turbo_ratio_limit(family, model))
3299 dump_hsw_turbo_ratio_limits();
3300
3301 if (has_ivt_turbo_ratio_limit(family, model))
3302 dump_ivt_turbo_ratio_limits();
3303
Len Brown31e07522017-01-31 23:07:49 -05003304 if (has_turbo_ratio_limit(family, model))
3305 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003306
Len Brown0f7887c2017-01-12 23:49:18 -05003307 if (has_atom_turbo_ratio_limit(family, model))
3308 dump_atom_turbo_ratio_limits();
3309
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003310 if (has_knl_turbo_ratio_limit(family, model))
3311 dump_knl_turbo_ratio_limits();
3312
Len Brown6fb31432015-06-17 16:23:45 -04003313 if (has_config_tdp(family, model))
3314 dump_config_tdp();
3315
Len Brownfcd17212015-03-23 20:29:09 -04003316 dump_nhm_cst_cfg();
3317}
3318
Len Brown41618e62017-02-09 18:25:22 -05003319static void
3320dump_sysfs_cstate_config(void)
3321{
3322 char path[64];
3323 char name_buf[16];
3324 char desc[64];
3325 FILE *input;
3326 int state;
3327 char *sp;
3328
3329 if (!DO_BIC(BIC_sysfs))
3330 return;
3331
3332 for (state = 0; state < 10; ++state) {
3333
3334 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3335 base_cpu, state);
3336 input = fopen(path, "r");
3337 if (input == NULL)
3338 continue;
3339 fgets(name_buf, sizeof(name_buf), input);
3340
3341 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3342 sp = strchr(name_buf, '-');
3343 if (!sp)
3344 sp = strchrnul(name_buf, '\n');
3345 *sp = '\0';
3346
3347 fclose(input);
3348
3349 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3350 base_cpu, state);
3351 input = fopen(path, "r");
3352 if (input == NULL)
3353 continue;
3354 fgets(desc, sizeof(desc), input);
3355
3356 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3357 fclose(input);
3358 }
3359}
Len Brown7293fcc2017-02-22 00:11:12 -05003360static void
3361dump_sysfs_pstate_config(void)
3362{
3363 char path[64];
3364 char driver_buf[64];
3365 char governor_buf[64];
3366 FILE *input;
3367 int turbo;
3368
3369 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3370 base_cpu);
3371 input = fopen(path, "r");
3372 if (input == NULL) {
3373 fprintf(stderr, "NSFOD %s\n", path);
3374 return;
3375 }
3376 fgets(driver_buf, sizeof(driver_buf), input);
3377 fclose(input);
3378
3379 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3380 base_cpu);
3381 input = fopen(path, "r");
3382 if (input == NULL) {
3383 fprintf(stderr, "NSFOD %s\n", path);
3384 return;
3385 }
3386 fgets(governor_buf, sizeof(governor_buf), input);
3387 fclose(input);
3388
3389 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3390 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3391
3392 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3393 input = fopen(path, "r");
3394 if (input != NULL) {
3395 fscanf(input, "%d", &turbo);
3396 fprintf(outf, "cpufreq boost: %d\n", turbo);
3397 fclose(input);
3398 }
3399
3400 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3401 input = fopen(path, "r");
3402 if (input != NULL) {
3403 fscanf(input, "%d", &turbo);
3404 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3405 fclose(input);
3406 }
3407}
Len Brown41618e62017-02-09 18:25:22 -05003408
Len Brown6574a5d2012-09-21 00:01:31 -04003409
Len Brown889facb2012-11-08 00:48:57 -05003410/*
3411 * print_epb()
3412 * Decode the ENERGY_PERF_BIAS MSR
3413 */
3414int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3415{
3416 unsigned long long msr;
3417 char *epb_string;
3418 int cpu;
3419
3420 if (!has_epb)
3421 return 0;
3422
3423 cpu = t->cpu_id;
3424
3425 /* EPB is per-package */
3426 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3427 return 0;
3428
3429 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003430 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003431 return -1;
3432 }
3433
3434 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3435 return 0;
3436
Len Browne9be7dd2015-05-26 12:19:37 -04003437 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003438 case ENERGY_PERF_BIAS_PERFORMANCE:
3439 epb_string = "performance";
3440 break;
3441 case ENERGY_PERF_BIAS_NORMAL:
3442 epb_string = "balanced";
3443 break;
3444 case ENERGY_PERF_BIAS_POWERSAVE:
3445 epb_string = "powersave";
3446 break;
3447 default:
3448 epb_string = "custom";
3449 break;
3450 }
Len Brownb7d8c142016-02-13 23:36:17 -05003451 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003452
3453 return 0;
3454}
Len Brown7f5c2582015-12-01 01:36:39 -05003455/*
3456 * print_hwp()
3457 * Decode the MSR_HWP_CAPABILITIES
3458 */
3459int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3460{
3461 unsigned long long msr;
3462 int cpu;
3463
3464 if (!has_hwp)
3465 return 0;
3466
3467 cpu = t->cpu_id;
3468
3469 /* MSR_HWP_CAPABILITIES is per-package */
3470 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3471 return 0;
3472
3473 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003474 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003475 return -1;
3476 }
3477
3478 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3479 return 0;
3480
Len Brownb7d8c142016-02-13 23:36:17 -05003481 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003482 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3483
3484 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3485 if ((msr & (1 << 0)) == 0)
3486 return 0;
3487
3488 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3489 return 0;
3490
Len Brownb7d8c142016-02-13 23:36:17 -05003491 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003492 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003493 cpu, msr,
3494 (unsigned int)HWP_HIGHEST_PERF(msr),
3495 (unsigned int)HWP_GUARANTEED_PERF(msr),
3496 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3497 (unsigned int)HWP_LOWEST_PERF(msr));
3498
3499 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3500 return 0;
3501
Len Brownb7d8c142016-02-13 23:36:17 -05003502 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003503 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003504 cpu, msr,
3505 (unsigned int)(((msr) >> 0) & 0xff),
3506 (unsigned int)(((msr) >> 8) & 0xff),
3507 (unsigned int)(((msr) >> 16) & 0xff),
3508 (unsigned int)(((msr) >> 24) & 0xff),
3509 (unsigned int)(((msr) >> 32) & 0xff3),
3510 (unsigned int)(((msr) >> 42) & 0x1));
3511
3512 if (has_hwp_pkg) {
3513 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3514 return 0;
3515
Len Brownb7d8c142016-02-13 23:36:17 -05003516 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003517 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003518 cpu, msr,
3519 (unsigned int)(((msr) >> 0) & 0xff),
3520 (unsigned int)(((msr) >> 8) & 0xff),
3521 (unsigned int)(((msr) >> 16) & 0xff),
3522 (unsigned int)(((msr) >> 24) & 0xff),
3523 (unsigned int)(((msr) >> 32) & 0xff3));
3524 }
3525 if (has_hwp_notify) {
3526 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3527 return 0;
3528
Len Brownb7d8c142016-02-13 23:36:17 -05003529 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003530 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3531 cpu, msr,
3532 ((msr) & 0x1) ? "EN" : "Dis",
3533 ((msr) & 0x2) ? "EN" : "Dis");
3534 }
3535 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3536 return 0;
3537
Len Brownb7d8c142016-02-13 23:36:17 -05003538 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003539 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3540 cpu, msr,
3541 ((msr) & 0x1) ? "" : "No-",
3542 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003543
3544 return 0;
3545}
3546
Len Brown3a9a9412014-08-15 02:39:52 -04003547/*
3548 * print_perf_limit()
3549 */
3550int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3551{
3552 unsigned long long msr;
3553 int cpu;
3554
3555 cpu = t->cpu_id;
3556
3557 /* per-package */
3558 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3559 return 0;
3560
3561 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003562 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003563 return -1;
3564 }
3565
3566 if (do_core_perf_limit_reasons) {
3567 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003568 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3569 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003570 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003571 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003572 (msr & 1 << 13) ? "Transitions, " : "",
3573 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3574 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3575 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3576 (msr & 1 << 9) ? "CorePwr, " : "",
3577 (msr & 1 << 8) ? "Amps, " : "",
3578 (msr & 1 << 6) ? "VR-Therm, " : "",
3579 (msr & 1 << 5) ? "Auto-HWP, " : "",
3580 (msr & 1 << 4) ? "Graphics, " : "",
3581 (msr & 1 << 2) ? "bit2, " : "",
3582 (msr & 1 << 1) ? "ThermStatus, " : "",
3583 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003584 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 -04003585 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003586 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003587 (msr & 1 << 29) ? "Transitions, " : "",
3588 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3589 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3590 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3591 (msr & 1 << 25) ? "CorePwr, " : "",
3592 (msr & 1 << 24) ? "Amps, " : "",
3593 (msr & 1 << 22) ? "VR-Therm, " : "",
3594 (msr & 1 << 21) ? "Auto-HWP, " : "",
3595 (msr & 1 << 20) ? "Graphics, " : "",
3596 (msr & 1 << 18) ? "bit18, " : "",
3597 (msr & 1 << 17) ? "ThermStatus, " : "",
3598 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003599
3600 }
3601 if (do_gfx_perf_limit_reasons) {
3602 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003603 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3604 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003605 (msr & 1 << 0) ? "PROCHOT, " : "",
3606 (msr & 1 << 1) ? "ThermStatus, " : "",
3607 (msr & 1 << 4) ? "Graphics, " : "",
3608 (msr & 1 << 6) ? "VR-Therm, " : "",
3609 (msr & 1 << 8) ? "Amps, " : "",
3610 (msr & 1 << 9) ? "GFXPwr, " : "",
3611 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3612 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003613 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003614 (msr & 1 << 16) ? "PROCHOT, " : "",
3615 (msr & 1 << 17) ? "ThermStatus, " : "",
3616 (msr & 1 << 20) ? "Graphics, " : "",
3617 (msr & 1 << 22) ? "VR-Therm, " : "",
3618 (msr & 1 << 24) ? "Amps, " : "",
3619 (msr & 1 << 25) ? "GFXPwr, " : "",
3620 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3621 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3622 }
3623 if (do_ring_perf_limit_reasons) {
3624 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003625 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3626 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003627 (msr & 1 << 0) ? "PROCHOT, " : "",
3628 (msr & 1 << 1) ? "ThermStatus, " : "",
3629 (msr & 1 << 6) ? "VR-Therm, " : "",
3630 (msr & 1 << 8) ? "Amps, " : "",
3631 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3632 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003633 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003634 (msr & 1 << 16) ? "PROCHOT, " : "",
3635 (msr & 1 << 17) ? "ThermStatus, " : "",
3636 (msr & 1 << 22) ? "VR-Therm, " : "",
3637 (msr & 1 << 24) ? "Amps, " : "",
3638 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3639 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3640 }
3641 return 0;
3642}
3643
Len Brown889facb2012-11-08 00:48:57 -05003644#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3645#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3646
Colin Ian King1b693172016-03-02 13:50:25 +00003647double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003648{
3649 unsigned long long msr;
3650
3651 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003652 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003653 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3654
3655 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003656 case INTEL_FAM6_ATOM_SILVERMONT1:
3657 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003658 return 30.0;
3659 default:
3660 return 135.0;
3661 }
3662}
3663
Andrey Semin40ee8e32014-12-05 00:07:00 -05003664/*
3665 * rapl_dram_energy_units_probe()
3666 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3667 */
3668static double
3669rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3670{
3671 /* only called for genuine_intel, family 6 */
3672
3673 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003674 case INTEL_FAM6_HASWELL_X: /* HSX */
3675 case INTEL_FAM6_BROADWELL_X: /* BDX */
3676 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3677 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003678 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003679 return (rapl_dram_energy_units = 15.3 / 1000000);
3680 default:
3681 return (rapl_energy_units);
3682 }
3683}
3684
Len Brown144b44b2013-11-09 00:30:16 -05003685
Len Brown889facb2012-11-08 00:48:57 -05003686/*
3687 * rapl_probe()
3688 *
Len Brown144b44b2013-11-09 00:30:16 -05003689 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003690 */
3691void rapl_probe(unsigned int family, unsigned int model)
3692{
3693 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003694 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003695 double tdp;
3696
3697 if (!genuine_intel)
3698 return;
3699
3700 if (family != 6)
3701 return;
3702
3703 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003704 case INTEL_FAM6_SANDYBRIDGE:
3705 case INTEL_FAM6_IVYBRIDGE:
3706 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3707 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3708 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3709 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3710 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003711 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003712 if (rapl_joules) {
3713 BIC_PRESENT(BIC_Pkg_J);
3714 BIC_PRESENT(BIC_Cor_J);
3715 BIC_PRESENT(BIC_GFX_J);
3716 } else {
3717 BIC_PRESENT(BIC_PkgWatt);
3718 BIC_PRESENT(BIC_CorWatt);
3719 BIC_PRESENT(BIC_GFXWatt);
3720 }
Len Brown889facb2012-11-08 00:48:57 -05003721 break;
Len Brown869ce692016-06-16 23:22:37 -04003722 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003723 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003724 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003725 if (rapl_joules)
3726 BIC_PRESENT(BIC_Pkg_J);
3727 else
3728 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003729 break;
Len Brown869ce692016-06-16 23:22:37 -04003730 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3731 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3732 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3733 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003734 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown81824922017-03-04 17:23:07 -05003735 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 -05003736 BIC_PRESENT(BIC_PKG__);
3737 BIC_PRESENT(BIC_RAM__);
3738 if (rapl_joules) {
3739 BIC_PRESENT(BIC_Pkg_J);
3740 BIC_PRESENT(BIC_Cor_J);
3741 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003742 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003743 } else {
3744 BIC_PRESENT(BIC_PkgWatt);
3745 BIC_PRESENT(BIC_CorWatt);
3746 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003747 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003748 }
Len Brown0b2bb692015-03-26 00:50:30 -04003749 break;
Len Brown869ce692016-06-16 23:22:37 -04003750 case INTEL_FAM6_HASWELL_X: /* HSX */
3751 case INTEL_FAM6_BROADWELL_X: /* BDX */
3752 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3753 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3754 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003755 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003756 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 -05003757 BIC_PRESENT(BIC_PKG__);
3758 BIC_PRESENT(BIC_RAM__);
3759 if (rapl_joules) {
3760 BIC_PRESENT(BIC_Pkg_J);
3761 BIC_PRESENT(BIC_RAM_J);
3762 } else {
3763 BIC_PRESENT(BIC_PkgWatt);
3764 BIC_PRESENT(BIC_RAMWatt);
3765 }
Len Browne6f9bb32013-12-03 02:19:19 -05003766 break;
Len Brown869ce692016-06-16 23:22:37 -04003767 case INTEL_FAM6_SANDYBRIDGE_X:
3768 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003769 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 -05003770 BIC_PRESENT(BIC_PKG__);
3771 BIC_PRESENT(BIC_RAM__);
3772 if (rapl_joules) {
3773 BIC_PRESENT(BIC_Pkg_J);
3774 BIC_PRESENT(BIC_Cor_J);
3775 BIC_PRESENT(BIC_RAM_J);
3776 } else {
3777 BIC_PRESENT(BIC_PkgWatt);
3778 BIC_PRESENT(BIC_CorWatt);
3779 BIC_PRESENT(BIC_RAMWatt);
3780 }
Len Brown144b44b2013-11-09 00:30:16 -05003781 break;
Len Brown869ce692016-06-16 23:22:37 -04003782 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3783 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003784 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003785 if (rapl_joules) {
3786 BIC_PRESENT(BIC_Pkg_J);
3787 BIC_PRESENT(BIC_Cor_J);
3788 } else {
3789 BIC_PRESENT(BIC_PkgWatt);
3790 BIC_PRESENT(BIC_CorWatt);
3791 }
Len Brown889facb2012-11-08 00:48:57 -05003792 break;
Len Brown869ce692016-06-16 23:22:37 -04003793 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003794 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 -05003795 BIC_PRESENT(BIC_PKG__);
3796 BIC_PRESENT(BIC_RAM__);
3797 if (rapl_joules) {
3798 BIC_PRESENT(BIC_Pkg_J);
3799 BIC_PRESENT(BIC_Cor_J);
3800 BIC_PRESENT(BIC_RAM_J);
3801 } else {
3802 BIC_PRESENT(BIC_PkgWatt);
3803 BIC_PRESENT(BIC_CorWatt);
3804 BIC_PRESENT(BIC_RAMWatt);
3805 }
Jacob Pan0f644902016-06-16 09:48:22 -07003806 break;
Len Brown889facb2012-11-08 00:48:57 -05003807 default:
3808 return;
3809 }
3810
3811 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003812 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003813 return;
3814
3815 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003816 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003817 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3818 else
3819 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003820
Andrey Semin40ee8e32014-12-05 00:07:00 -05003821 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3822
Len Brown144b44b2013-11-09 00:30:16 -05003823 time_unit = msr >> 16 & 0xF;
3824 if (time_unit == 0)
3825 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003826
Len Brown144b44b2013-11-09 00:30:16 -05003827 rapl_time_units = 1.0 / (1 << (time_unit));
3828
3829 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003830
3831 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003832 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003833 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003834
3835 return;
3836}
3837
Colin Ian King1b693172016-03-02 13:50:25 +00003838void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003839{
3840 if (!genuine_intel)
3841 return;
3842
3843 if (family != 6)
3844 return;
3845
3846 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003847 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3848 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3849 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003850 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003851 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003852 do_core_perf_limit_reasons = 1;
3853 do_ring_perf_limit_reasons = 1;
3854 default:
3855 return;
3856 }
3857}
3858
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003859void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3860{
3861 if (is_skx(family, model) || is_bdx(family, model))
3862 has_automatic_cstate_conversion = 1;
3863}
3864
Len Brown889facb2012-11-08 00:48:57 -05003865int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3866{
3867 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003868 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003869 int cpu;
3870
3871 if (!(do_dts || do_ptm))
3872 return 0;
3873
3874 cpu = t->cpu_id;
3875
3876 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003877 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003878 return 0;
3879
3880 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003881 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003882 return -1;
3883 }
3884
3885 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3886 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3887 return 0;
3888
3889 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003890 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003891 cpu, msr, tcc_activation_temp - dts);
3892
Len Brown889facb2012-11-08 00:48:57 -05003893 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3894 return 0;
3895
3896 dts = (msr >> 16) & 0x7F;
3897 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003898 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003899 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003900 }
3901
3902
Len Brownf4896fa52017-03-04 18:10:45 -05003903 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003904 unsigned int resolution;
3905
3906 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3907 return 0;
3908
3909 dts = (msr >> 16) & 0x7F;
3910 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003911 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003912 cpu, msr, tcc_activation_temp - dts, resolution);
3913
Len Brown889facb2012-11-08 00:48:57 -05003914 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3915 return 0;
3916
3917 dts = (msr >> 16) & 0x7F;
3918 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003919 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003920 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003921 }
3922
3923 return 0;
3924}
Len Brown36229892016-02-26 20:51:02 -05003925
Len Brown889facb2012-11-08 00:48:57 -05003926void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3927{
Len Brownb7d8c142016-02-13 23:36:17 -05003928 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003929 cpu, label,
3930 ((msr >> 15) & 1) ? "EN" : "DIS",
3931 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3932 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3933 (((msr >> 16) & 1) ? "EN" : "DIS"));
3934
3935 return;
3936}
3937
3938int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3939{
3940 unsigned long long msr;
3941 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003942
3943 if (!do_rapl)
3944 return 0;
3945
3946 /* RAPL counters are per package, so print only for 1st thread/package */
3947 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3948 return 0;
3949
3950 cpu = t->cpu_id;
3951 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003952 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003953 return -1;
3954 }
3955
3956 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3957 return -1;
3958
Len Brown96e47152017-01-21 02:26:00 -05003959 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3960 rapl_power_units, rapl_energy_units, rapl_time_units);
3961
Len Brown144b44b2013-11-09 00:30:16 -05003962 if (do_rapl & RAPL_PKG_POWER_INFO) {
3963
Len Brown889facb2012-11-08 00:48:57 -05003964 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3965 return -5;
3966
3967
Len Brownb7d8c142016-02-13 23:36:17 -05003968 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 -05003969 cpu, msr,
3970 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3971 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3972 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3973 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3974
Len Brown144b44b2013-11-09 00:30:16 -05003975 }
3976 if (do_rapl & RAPL_PKG) {
3977
Len Brown889facb2012-11-08 00:48:57 -05003978 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3979 return -9;
3980
Len Brownb7d8c142016-02-13 23:36:17 -05003981 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003982 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003983
3984 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003985 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003986 cpu,
3987 ((msr >> 47) & 1) ? "EN" : "DIS",
3988 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3989 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3990 ((msr >> 48) & 1) ? "EN" : "DIS");
3991 }
3992
Len Brown0b2bb692015-03-26 00:50:30 -04003993 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003994 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3995 return -6;
3996
Len Brownb7d8c142016-02-13 23:36:17 -05003997 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 -05003998 cpu, msr,
3999 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4000 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4001 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4002 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04004003 }
4004 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05004005 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4006 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05004007 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05004008 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05004009
4010 print_power_limit_msr(cpu, msr, "DRAM Limit");
4011 }
Len Brown144b44b2013-11-09 00:30:16 -05004012 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05004013 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4014 return -7;
Len Brown889facb2012-11-08 00:48:57 -05004015
Len Brown96e47152017-01-21 02:26:00 -05004016 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05004017 }
Jacob Pan91484942016-06-16 09:48:20 -07004018 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05004019 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4020 return -9;
4021 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4022 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4023 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05004024 }
4025 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05004026 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4027 return -8;
Len Brown889facb2012-11-08 00:48:57 -05004028
Len Brown96e47152017-01-21 02:26:00 -05004029 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05004030
Len Brown96e47152017-01-21 02:26:00 -05004031 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4032 return -9;
4033 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4034 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4035 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05004036 }
4037 return 0;
4038}
4039
Len Brownd7899442015-01-23 00:12:33 -05004040/*
4041 * SNB adds support for additional MSRs:
4042 *
4043 * MSR_PKG_C7_RESIDENCY 0x000003fa
4044 * MSR_CORE_C7_RESIDENCY 0x000003fe
4045 * MSR_PKG_C2_RESIDENCY 0x0000060d
4046 */
Len Brown103a8fe2010-10-22 23:53:03 -04004047
Len Brownd7899442015-01-23 00:12:33 -05004048int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04004049{
4050 if (!genuine_intel)
4051 return 0;
4052
4053 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004054 case INTEL_FAM6_SANDYBRIDGE:
4055 case INTEL_FAM6_SANDYBRIDGE_X:
4056 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4057 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4058 case INTEL_FAM6_HASWELL_CORE: /* HSW */
4059 case INTEL_FAM6_HASWELL_X: /* HSW */
4060 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4061 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
4062 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4063 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
4064 case INTEL_FAM6_BROADWELL_X: /* BDX */
4065 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
4066 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4067 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4068 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4069 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004070 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04004071 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4072 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004073 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08004074 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04004075 return 1;
4076 }
4077 return 0;
4078}
4079
Len Brownd7899442015-01-23 00:12:33 -05004080/*
4081 * HSW adds support for additional MSRs:
4082 *
Len Brown5a634262016-04-06 17:15:55 -04004083 * MSR_PKG_C8_RESIDENCY 0x00000630
4084 * MSR_PKG_C9_RESIDENCY 0x00000631
4085 * MSR_PKG_C10_RESIDENCY 0x00000632
4086 *
4087 * MSR_PKGC8_IRTL 0x00000633
4088 * MSR_PKGC9_IRTL 0x00000634
4089 * MSR_PKGC10_IRTL 0x00000635
4090 *
Len Brownd7899442015-01-23 00:12:33 -05004091 */
4092int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004093{
4094 if (!genuine_intel)
4095 return 0;
4096
4097 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004098 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4099 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4100 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4101 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4102 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4103 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004104 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04004105 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004106 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004107 return 1;
4108 }
4109 return 0;
4110}
4111
Len Brown0b2bb692015-03-26 00:50:30 -04004112/*
4113 * SKL adds support for additional MSRS:
4114 *
4115 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4116 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4117 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4118 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4119 */
4120int has_skl_msrs(unsigned int family, unsigned int model)
4121{
4122 if (!genuine_intel)
4123 return 0;
4124
4125 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004126 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4127 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4128 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4129 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004130 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown0b2bb692015-03-26 00:50:30 -04004131 return 1;
4132 }
4133 return 0;
4134}
4135
Len Brown144b44b2013-11-09 00:30:16 -05004136int is_slm(unsigned int family, unsigned int model)
4137{
4138 if (!genuine_intel)
4139 return 0;
4140 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004141 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
4142 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05004143 return 1;
4144 }
4145 return 0;
4146}
4147
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004148int is_knl(unsigned int family, unsigned int model)
4149{
4150 if (!genuine_intel)
4151 return 0;
4152 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004153 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05004154 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004155 return 1;
4156 }
4157 return 0;
4158}
4159
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004160int is_cnl(unsigned int family, unsigned int model)
4161{
4162 if (!genuine_intel)
4163 return 0;
4164
4165 switch (model) {
4166 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4167 return 1;
4168 }
4169
4170 return 0;
4171}
4172
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004173unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4174{
4175 if (is_knl(family, model))
4176 return 1024;
4177 return 1;
4178}
4179
Len Brown144b44b2013-11-09 00:30:16 -05004180#define SLM_BCLK_FREQS 5
4181double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4182
4183double slm_bclk(void)
4184{
4185 unsigned long long msr = 3;
4186 unsigned int i;
4187 double freq;
4188
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004189 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05004190 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05004191
4192 i = msr & 0xf;
4193 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05004194 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01004195 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05004196 }
4197 freq = slm_freq_table[i];
4198
Len Brown96e47152017-01-21 02:26:00 -05004199 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05004200 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05004201
4202 return freq;
4203}
4204
Len Brown103a8fe2010-10-22 23:53:03 -04004205double discover_bclk(unsigned int family, unsigned int model)
4206{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01004207 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04004208 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05004209 else if (is_slm(family, model))
4210 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04004211 else
4212 return 133.33;
4213}
4214
Len Brown889facb2012-11-08 00:48:57 -05004215/*
4216 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4217 * the Thermal Control Circuit (TCC) activates.
4218 * This is usually equal to tjMax.
4219 *
4220 * Older processors do not have this MSR, so there we guess,
4221 * but also allow cmdline over-ride with -T.
4222 *
4223 * Several MSR temperature values are in units of degrees-C
4224 * below this value, including the Digital Thermal Sensor (DTS),
4225 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4226 */
4227int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4228{
4229 unsigned long long msr;
4230 unsigned int target_c_local;
4231 int cpu;
4232
4233 /* tcc_activation_temp is used only for dts or ptm */
4234 if (!(do_dts || do_ptm))
4235 return 0;
4236
4237 /* this is a per-package concept */
4238 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4239 return 0;
4240
4241 cpu = t->cpu_id;
4242 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004243 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004244 return -1;
4245 }
4246
4247 if (tcc_activation_temp_override != 0) {
4248 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004249 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004250 cpu, tcc_activation_temp);
4251 return 0;
4252 }
4253
4254 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004255 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004256 goto guess;
4257
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004258 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004259 goto guess;
4260
Jean Delvare34821242014-05-01 11:40:19 +02004261 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004262
Len Brown96e47152017-01-21 02:26:00 -05004263 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004264 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004265 cpu, msr, target_c_local);
4266
Jean Delvare34821242014-05-01 11:40:19 +02004267 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004268 goto guess;
4269
4270 tcc_activation_temp = target_c_local;
4271
4272 return 0;
4273
4274guess:
4275 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004276 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004277 cpu, tcc_activation_temp);
4278
4279 return 0;
4280}
Len Brown69807a62015-11-21 12:22:47 -05004281
Len Brownaa8d8cc2016-03-11 13:26:03 -05004282void decode_feature_control_msr(void)
4283{
4284 unsigned long long msr;
4285
4286 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4287 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4288 base_cpu, msr,
4289 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4290 msr & (1 << 18) ? "SGX" : "");
4291}
4292
Len Brown69807a62015-11-21 12:22:47 -05004293void decode_misc_enable_msr(void)
4294{
4295 unsigned long long msr;
4296
Len Brownf26b1512017-06-23 20:45:54 -07004297 if (!genuine_intel)
4298 return;
4299
Len Brown69807a62015-11-21 12:22:47 -05004300 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004301 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004302 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004303 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4304 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004305 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004306 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4307 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004308}
4309
Len Brown33148d62017-01-21 01:26:16 -05004310void decode_misc_feature_control(void)
4311{
4312 unsigned long long msr;
4313
4314 if (!has_misc_feature_control)
4315 return;
4316
4317 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4318 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4319 base_cpu, msr,
4320 msr & (0 << 0) ? "No-" : "",
4321 msr & (1 << 0) ? "No-" : "",
4322 msr & (2 << 0) ? "No-" : "",
4323 msr & (3 << 0) ? "No-" : "");
4324}
Len Brownf0057312015-12-03 01:35:36 -05004325/*
4326 * Decode MSR_MISC_PWR_MGMT
4327 *
4328 * Decode the bits according to the Nehalem documentation
4329 * bit[0] seems to continue to have same meaning going forward
4330 * bit[1] less so...
4331 */
4332void decode_misc_pwr_mgmt_msr(void)
4333{
4334 unsigned long long msr;
4335
4336 if (!do_nhm_platform_info)
4337 return;
4338
Len Browncf4cbe52017-01-01 13:08:33 -05004339 if (no_MSR_MISC_PWR_MGMT)
4340 return;
4341
Len Brownf0057312015-12-03 01:35:36 -05004342 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004343 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 -05004344 base_cpu, msr,
4345 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004346 msr & (1 << 1) ? "EN" : "DIS",
4347 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004348}
Len Brown71616c82017-01-07 22:37:48 -05004349/*
4350 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4351 *
4352 * This MSRs are present on Silvermont processors,
4353 * Intel Atom processor E3000 series (Baytrail), and friends.
4354 */
4355void decode_c6_demotion_policy_msr(void)
4356{
4357 unsigned long long msr;
4358
4359 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4360 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4361 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4362
4363 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4364 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4365 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4366}
Len Brown7f5c2582015-12-01 01:36:39 -05004367
Len Brownfcd17212015-03-23 20:29:09 -04004368void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004369{
Len Brown61a87ba2015-11-23 02:30:51 -05004370 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004371 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004372 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004373
4374 eax = ebx = ecx = edx = 0;
4375
Len Brown5aea2f72016-03-13 03:14:35 -04004376 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004377
4378 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4379 genuine_intel = 1;
4380
Len Brown96e47152017-01-21 02:26:00 -05004381 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004382 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004383 (char *)&ebx, (char *)&edx, (char *)&ecx);
4384
Len Brown5aea2f72016-03-13 03:14:35 -04004385 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004386 family = (fms >> 8) & 0xf;
4387 model = (fms >> 4) & 0xf;
4388 stepping = fms & 0xf;
4389 if (family == 6 || family == 0xf)
4390 model += ((fms >> 16) & 0xf) << 4;
4391
Len Brown96e47152017-01-21 02:26:00 -05004392 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004393 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004394 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004395 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004396 ecx & (1 << 0) ? "SSE3" : "-",
4397 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004398 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004399 ecx & (1 << 7) ? "EIST" : "-",
4400 ecx & (1 << 8) ? "TM2" : "-",
4401 edx & (1 << 4) ? "TSC" : "-",
4402 edx & (1 << 5) ? "MSR" : "-",
4403 edx & (1 << 22) ? "ACPI-TM" : "-",
4404 edx & (1 << 29) ? "TM" : "-");
4405 }
Len Brown103a8fe2010-10-22 23:53:03 -04004406
Josh Triplettb2c95d92013-08-20 17:20:18 -07004407 if (!(edx & (1 << 5)))
4408 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004409
4410 /*
4411 * check max extended function levels of CPUID.
4412 * This is needed to check for invariant TSC.
4413 * This check is valid for both Intel and AMD.
4414 */
4415 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004416 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004417
Len Brown61a87ba2015-11-23 02:30:51 -05004418 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004419
Len Brownd7899442015-01-23 00:12:33 -05004420 /*
4421 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4422 * this check is valid for both Intel and AMD
4423 */
Len Brown5aea2f72016-03-13 03:14:35 -04004424 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004425 has_invariant_tsc = edx & (1 << 8);
4426 }
Len Brown103a8fe2010-10-22 23:53:03 -04004427
4428 /*
4429 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4430 * this check is valid for both Intel and AMD
4431 */
4432
Len Brown5aea2f72016-03-13 03:14:35 -04004433 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004434 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004435 if (has_aperf) {
4436 BIC_PRESENT(BIC_Avg_MHz);
4437 BIC_PRESENT(BIC_Busy);
4438 BIC_PRESENT(BIC_Bzy_MHz);
4439 }
Len Brown889facb2012-11-08 00:48:57 -05004440 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004441 if (do_dts)
4442 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004443 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004444 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004445 if (do_ptm)
4446 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004447 has_hwp = eax & (1 << 7);
4448 has_hwp_notify = eax & (1 << 8);
4449 has_hwp_activity_window = eax & (1 << 9);
4450 has_hwp_epp = eax & (1 << 10);
4451 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004452 has_epb = ecx & (1 << 3);
4453
Len Brown96e47152017-01-21 02:26:00 -05004454 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004455 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004456 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4457 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004458 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004459 do_dts ? "" : "No-",
4460 do_ptm ? "" : "No-",
4461 has_hwp ? "" : "No-",
4462 has_hwp_notify ? "" : "No-",
4463 has_hwp_activity_window ? "" : "No-",
4464 has_hwp_epp ? "" : "No-",
4465 has_hwp_pkg ? "" : "No-",
4466 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004467
Len Brown96e47152017-01-21 02:26:00 -05004468 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004469 decode_misc_enable_msr();
4470
Len Brown33148d62017-01-21 01:26:16 -05004471
Len Brown96e47152017-01-21 02:26:00 -05004472 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004473 int has_sgx;
4474
4475 ecx = 0;
4476
4477 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4478
4479 has_sgx = ebx & (1 << 2);
4480 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4481
4482 if (has_sgx)
4483 decode_feature_control_msr();
4484 }
4485
Len Brown61a87ba2015-11-23 02:30:51 -05004486 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004487 unsigned int eax_crystal;
4488 unsigned int ebx_tsc;
4489
4490 /*
4491 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4492 */
4493 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004494 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004495
4496 if (ebx_tsc != 0) {
4497
Len Brown96e47152017-01-21 02:26:00 -05004498 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004499 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004500 eax_crystal, ebx_tsc, crystal_hz);
4501
4502 if (crystal_hz == 0)
4503 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004504 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4505 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4506 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4507 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004508 crystal_hz = 24000000; /* 24.0 MHz */
4509 break;
Len Brown7268d402016-12-01 23:10:39 -05004510 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004511 crystal_hz = 25000000; /* 25.0 MHz */
4512 break;
Len Brown869ce692016-06-16 23:22:37 -04004513 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004514 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004515 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004516 break;
4517 default:
4518 crystal_hz = 0;
4519 }
4520
4521 if (crystal_hz) {
4522 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004523 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004524 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004525 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4526 }
4527 }
4528 }
Len Brown61a87ba2015-11-23 02:30:51 -05004529 if (max_level >= 0x16) {
4530 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4531
4532 /*
4533 * CPUID 16H Base MHz, Max MHz, Bus MHz
4534 */
4535 base_mhz = max_mhz = bus_mhz = edx = 0;
4536
Len Brown5aea2f72016-03-13 03:14:35 -04004537 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004538 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004539 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004540 base_mhz, max_mhz, bus_mhz);
4541 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004542
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004543 if (has_aperf)
4544 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4545
Len Brown812db3f2017-02-10 00:25:41 -05004546 BIC_PRESENT(BIC_IRQ);
4547 BIC_PRESENT(BIC_TSC_MHz);
4548
4549 if (probe_nhm_msrs(family, model)) {
4550 do_nhm_platform_info = 1;
4551 BIC_PRESENT(BIC_CPU_c1);
4552 BIC_PRESENT(BIC_CPU_c3);
4553 BIC_PRESENT(BIC_CPU_c6);
4554 BIC_PRESENT(BIC_SMI);
4555 }
Len Brownd7899442015-01-23 00:12:33 -05004556 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004557
4558 if (do_snb_cstates)
4559 BIC_PRESENT(BIC_CPU_c7);
4560
Len Brown5a634262016-04-06 17:15:55 -04004561 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004562 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4563 BIC_PRESENT(BIC_Pkgpc2);
4564 if (pkg_cstate_limit >= PCL__3)
4565 BIC_PRESENT(BIC_Pkgpc3);
4566 if (pkg_cstate_limit >= PCL__6)
4567 BIC_PRESENT(BIC_Pkgpc6);
4568 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4569 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004570 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004571 BIC_NOT_PRESENT(BIC_Pkgpc2);
4572 BIC_NOT_PRESENT(BIC_Pkgpc3);
4573 BIC_PRESENT(BIC_Pkgpc6);
4574 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004575 BIC_PRESENT(BIC_Mod_c6);
4576 use_c1_residency_msr = 1;
4577 }
Len Brown7170a372017-01-27 02:13:27 -05004578 if (is_dnv(family, model)) {
4579 BIC_PRESENT(BIC_CPU_c1);
4580 BIC_NOT_PRESENT(BIC_CPU_c3);
4581 BIC_NOT_PRESENT(BIC_Pkgpc3);
4582 BIC_NOT_PRESENT(BIC_CPU_c7);
4583 BIC_NOT_PRESENT(BIC_Pkgpc7);
4584 use_c1_residency_msr = 1;
4585 }
Len Brown34c761972017-01-27 02:36:41 -05004586 if (is_skx(family, model)) {
4587 BIC_NOT_PRESENT(BIC_CPU_c3);
4588 BIC_NOT_PRESENT(BIC_Pkgpc3);
4589 BIC_NOT_PRESENT(BIC_CPU_c7);
4590 BIC_NOT_PRESENT(BIC_Pkgpc7);
4591 }
Len Brownade0eba2017-02-10 01:56:47 -05004592 if (is_bdx(family, model)) {
4593 BIC_NOT_PRESENT(BIC_CPU_c7);
4594 BIC_NOT_PRESENT(BIC_Pkgpc7);
4595 }
Len Brown0f47c082017-01-27 00:50:45 -05004596 if (has_hsw_msrs(family, model)) {
4597 BIC_PRESENT(BIC_Pkgpc8);
4598 BIC_PRESENT(BIC_Pkgpc9);
4599 BIC_PRESENT(BIC_Pkgpc10);
4600 }
Len Brown5a634262016-04-06 17:15:55 -04004601 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004602 if (has_skl_msrs(family, model)) {
4603 BIC_PRESENT(BIC_Totl_c0);
4604 BIC_PRESENT(BIC_Any_c0);
4605 BIC_PRESENT(BIC_GFX_c0);
4606 BIC_PRESENT(BIC_CPUGFX);
4607 }
Len Brown144b44b2013-11-09 00:30:16 -05004608 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004609 do_knl_cstates = is_knl(family, model);
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004610 do_cnl_cstates = is_cnl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004611
Len Brown96e47152017-01-21 02:26:00 -05004612 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004613 decode_misc_pwr_mgmt_msr();
4614
Len Brown96e47152017-01-21 02:26:00 -05004615 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004616 decode_c6_demotion_policy_msr();
4617
Len Brown889facb2012-11-08 00:48:57 -05004618 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004619 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004620 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004621
Len Brown96e47152017-01-21 02:26:00 -05004622 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004623 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004624
Len Brown41618e62017-02-09 18:25:22 -05004625 if (!quiet)
4626 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004627 if (!quiet)
4628 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004629
Len Browna2b7b742015-09-26 00:12:38 -04004630 if (has_skl_msrs(family, model))
4631 calculate_tsc_tweak();
4632
Len Brown812db3f2017-02-10 00:25:41 -05004633 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4634 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004635
Len Brown812db3f2017-02-10 00:25:41 -05004636 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4637 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004638
Len Brownbe0e54c2018-06-01 12:35:53 -04004639 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4640 BIC_PRESENT(BIC_CPU_LPI);
4641 else
4642 BIC_NOT_PRESENT(BIC_CPU_LPI);
4643
4644 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4645 BIC_PRESENT(BIC_SYS_LPI);
4646 else
4647 BIC_NOT_PRESENT(BIC_SYS_LPI);
4648
Len Brown96e47152017-01-21 02:26:00 -05004649 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004650 decode_misc_feature_control();
4651
Len Brown889facb2012-11-08 00:48:57 -05004652 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004653}
4654
Len Brown103a8fe2010-10-22 23:53:03 -04004655
4656/*
4657 * in /dev/cpu/ return success for names that are numbers
4658 * ie. filter out ".", "..", "microcode".
4659 */
4660int dir_filter(const struct dirent *dirp)
4661{
4662 if (isdigit(dirp->d_name[0]))
4663 return 1;
4664 else
4665 return 0;
4666}
4667
4668int open_dev_cpu_msr(int dummy1)
4669{
4670 return 0;
4671}
4672
Len Brownc98d5d92012-06-04 00:56:40 -04004673void topology_probe()
4674{
4675 int i;
4676 int max_core_id = 0;
4677 int max_package_id = 0;
4678 int max_siblings = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004679
4680 /* Initialize num_cpus, max_cpu_num */
Prarit Bhargava843c5792018-06-01 10:04:28 -04004681 set_max_cpu_num();
Len Brownc98d5d92012-06-04 00:56:40 -04004682 topo.num_cpus = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004683 for_all_proc_cpus(count_cpus);
4684 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004685 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004686
Len Brownd8af6f52015-02-10 01:56:38 -05004687 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004688 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004689
4690 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004691 if (cpus == NULL)
4692 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004693
4694 /*
4695 * Allocate and initialize cpu_present_set
4696 */
4697 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004698 if (cpu_present_set == NULL)
4699 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004700 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4701 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4702 for_all_proc_cpus(mark_cpu_present);
4703
4704 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004705 * Validate that all cpus in cpu_subset are also in cpu_present_set
4706 */
4707 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4708 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4709 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4710 err(1, "cpu%d not present", i);
4711 }
4712
4713 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004714 * Allocate and initialize cpu_affinity_set
4715 */
4716 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004717 if (cpu_affinity_set == NULL)
4718 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004719 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4720 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4721
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004722 for_all_proc_cpus(init_thread_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004723
4724 /*
4725 * For online cpus
4726 * find max_core_id, max_package_id
4727 */
4728 for (i = 0; i <= topo.max_cpu_num; ++i) {
4729 int siblings;
4730
4731 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004732 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004733 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004734 continue;
4735 }
Len Brownc98d5d92012-06-04 00:56:40 -04004736
Len Brown0e2d8f02018-06-01 22:08:58 -04004737 cpus[i].logical_cpu_id = i;
4738
4739 /* get package information */
Len Brownc98d5d92012-06-04 00:56:40 -04004740 cpus[i].physical_package_id = get_physical_package_id(i);
4741 if (cpus[i].physical_package_id > max_package_id)
4742 max_package_id = cpus[i].physical_package_id;
4743
Len Brown0e2d8f02018-06-01 22:08:58 -04004744 /* get numa node information */
Prarit Bhargavaef605742018-06-01 10:04:30 -04004745 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4746 if (cpus[i].physical_node_id > topo.max_node_num)
4747 topo.max_node_num = cpus[i].physical_node_id;
Len Brown0e2d8f02018-06-01 22:08:58 -04004748
4749 /* get core information */
4750 cpus[i].physical_core_id = get_core_id(i);
4751 if (cpus[i].physical_core_id > max_core_id)
4752 max_core_id = cpus[i].physical_core_id;
4753
4754 /* get thread information */
4755 siblings = get_thread_siblings(&cpus[i]);
Len Brownc98d5d92012-06-04 00:56:40 -04004756 if (siblings > max_siblings)
4757 max_siblings = siblings;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004758 if (cpus[i].thread_id != -1)
4759 topo.num_cores++;
Len Brown0e2d8f02018-06-01 22:08:58 -04004760
Len Brownd8af6f52015-02-10 01:56:38 -05004761 if (debug > 1)
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004762 fprintf(outf,
4763 "cpu %d pkg %d node %d core %d thread %d\n",
Len Brown0e2d8f02018-06-01 22:08:58 -04004764 i, cpus[i].physical_package_id,
Prarit Bhargavaef605742018-06-01 10:04:30 -04004765 cpus[i].physical_node_id,
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004766 cpus[i].physical_core_id,
4767 cpus[i].thread_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004768 }
Prarit Bhargavaef605742018-06-01 10:04:30 -04004769
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004770 topo.cores_per_node = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004771 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004772 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004773 max_core_id, topo.cores_per_node);
4774 if (!summary_only && topo.cores_per_node > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004775 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004776
4777 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004778 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004779 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004780 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004781 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004782 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004783
Prarit Bhargavaef605742018-06-01 10:04:30 -04004784 set_node_data();
4785 if (debug > 1)
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004786 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
Prarit Bhargava01235042018-06-01 10:04:35 -04004787 if (!summary_only && topo.nodes_per_pkg > 1)
4788 BIC_PRESENT(BIC_Node);
Prarit Bhargavaef605742018-06-01 10:04:30 -04004789
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004790 topo.threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004791 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004792 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004793}
4794
4795void
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004796allocate_counters(struct thread_data **t, struct core_data **c,
4797 struct pkg_data **p)
Len Brownc98d5d92012-06-04 00:56:40 -04004798{
4799 int i;
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004800 int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
4801 topo.num_packages;
4802 int num_threads = topo.threads_per_core * num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04004803
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004804 *t = calloc(num_threads, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004805 if (*t == NULL)
4806 goto error;
4807
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004808 for (i = 0; i < num_threads; i++)
Len Brownc98d5d92012-06-04 00:56:40 -04004809 (*t)[i].cpu_id = -1;
4810
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004811 *c = calloc(num_cores, sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004812 if (*c == NULL)
4813 goto error;
4814
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004815 for (i = 0; i < num_cores; i++)
Len Brownc98d5d92012-06-04 00:56:40 -04004816 (*c)[i].core_id = -1;
4817
Len Brown678a3bd2017-02-09 22:22:13 -05004818 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004819 if (*p == NULL)
4820 goto error;
4821
4822 for (i = 0; i < topo.num_packages; i++)
4823 (*p)[i].package_id = i;
4824
4825 return;
4826error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004827 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004828}
4829/*
4830 * init_counter()
4831 *
Len Brownc98d5d92012-06-04 00:56:40 -04004832 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
Len Brownc98d5d92012-06-04 00:56:40 -04004833 */
4834void init_counter(struct thread_data *thread_base, struct core_data *core_base,
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004835 struct pkg_data *pkg_base, int cpu_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004836{
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004837 int pkg_id = cpus[cpu_id].physical_package_id;
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004838 int node_id = cpus[cpu_id].logical_node_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004839 int core_id = cpus[cpu_id].physical_core_id;
4840 int thread_id = cpus[cpu_id].thread_id;
Len Brownc98d5d92012-06-04 00:56:40 -04004841 struct thread_data *t;
4842 struct core_data *c;
4843 struct pkg_data *p;
4844
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004845 t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
4846 c = GET_CORE(core_base, core_id, node_id, pkg_id);
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004847 p = GET_PKG(pkg_base, pkg_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004848
4849 t->cpu_id = cpu_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004850 if (thread_id == 0) {
Len Brownc98d5d92012-06-04 00:56:40 -04004851 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4852 if (cpu_is_first_core_in_package(cpu_id))
4853 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4854 }
4855
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004856 c->core_id = core_id;
4857 p->package_id = pkg_id;
Len Brownc98d5d92012-06-04 00:56:40 -04004858}
4859
4860
4861int initialize_counters(int cpu_id)
4862{
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004863 init_counter(EVEN_COUNTERS, cpu_id);
4864 init_counter(ODD_COUNTERS, cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004865 return 0;
4866}
4867
4868void allocate_output_buffer()
4869{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004870 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004871 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004872 if (outp == NULL)
4873 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004874}
Len Brown36229892016-02-26 20:51:02 -05004875void allocate_fd_percpu(void)
4876{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004877 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004878 if (fd_percpu == NULL)
4879 err(-1, "calloc fd_percpu");
4880}
Len Brown562a2d32016-02-26 23:48:05 -05004881void allocate_irq_buffers(void)
4882{
4883 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4884 if (irq_column_2_cpu == NULL)
4885 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004886
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004887 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004888 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004889 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004890}
Len Brownc98d5d92012-06-04 00:56:40 -04004891void setup_all_buffers(void)
4892{
4893 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004894 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004895 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004896 allocate_counters(&thread_even, &core_even, &package_even);
4897 allocate_counters(&thread_odd, &core_odd, &package_odd);
4898 allocate_output_buffer();
4899 for_all_proc_cpus(initialize_counters);
4900}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004901
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004902void set_base_cpu(void)
4903{
4904 base_cpu = sched_getcpu();
4905 if (base_cpu < 0)
4906 err(-ENODEV, "No valid cpus found");
4907
4908 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004909 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004910}
4911
Len Brown103a8fe2010-10-22 23:53:03 -04004912void turbostat_init()
4913{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004914 setup_all_buffers();
4915 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004916 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004917 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004918 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004919
Len Brown103a8fe2010-10-22 23:53:03 -04004920
Len Brown96e47152017-01-21 02:26:00 -05004921 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004922 for_all_cpus(print_hwp, ODD_COUNTERS);
4923
Len Brown96e47152017-01-21 02:26:00 -05004924 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004925 for_all_cpus(print_epb, ODD_COUNTERS);
4926
Len Brown96e47152017-01-21 02:26:00 -05004927 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004928 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4929
Len Brown96e47152017-01-21 02:26:00 -05004930 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004931 for_all_cpus(print_rapl, ODD_COUNTERS);
4932
4933 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4934
Len Brown96e47152017-01-21 02:26:00 -05004935 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004936 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004937
Len Brown96e47152017-01-21 02:26:00 -05004938 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004939 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004940}
4941
4942int fork_it(char **argv)
4943{
Len Brown103a8fe2010-10-22 23:53:03 -04004944 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004945 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004946
Len Brown218f0e82017-02-14 22:07:52 -05004947 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004948 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4949 if (status)
4950 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004951 /* clear affinity side-effect of get_counters() */
4952 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004953 gettimeofday(&tv_even, (struct timezone *)NULL);
4954
4955 child_pid = fork();
4956 if (!child_pid) {
4957 /* child */
4958 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004959 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004960 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004961
4962 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004963 if (child_pid == -1)
4964 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004965
4966 signal(SIGINT, SIG_IGN);
4967 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004968 if (waitpid(child_pid, &status, 0) == -1)
4969 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004970 }
Len Brownc98d5d92012-06-04 00:56:40 -04004971 /*
4972 * n.b. fork_it() does not check for errors from for_all_cpus()
4973 * because re-starting is problematic when forking
4974 */
Len Brown218f0e82017-02-14 22:07:52 -05004975 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004976 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004977 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004978 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004979 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4980 fprintf(outf, "%s: Counter reset detected\n", progname);
4981 else {
4982 compute_average(EVEN_COUNTERS);
4983 format_all_counters(EVEN_COUNTERS);
4984 }
Len Brown103a8fe2010-10-22 23:53:03 -04004985
Len Brownb7d8c142016-02-13 23:36:17 -05004986 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4987
4988 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004989
Len Brownd91bb172012-11-01 00:08:19 -04004990 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004991}
4992
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004993int get_and_dump_counters(void)
4994{
4995 int status;
4996
Len Brown218f0e82017-02-14 22:07:52 -05004997 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004998 status = for_all_cpus(get_counters, ODD_COUNTERS);
4999 if (status)
5000 return status;
5001
5002 status = for_all_cpus(dump_counters, ODD_COUNTERS);
5003 if (status)
5004 return status;
5005
Len Brownb7d8c142016-02-13 23:36:17 -05005006 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005007
5008 return status;
5009}
5010
Len Brownd8af6f52015-02-10 01:56:38 -05005011void print_version() {
Len Brown201d4f52018-01-28 21:54:28 -05005012 fprintf(outf, "turbostat version 18.06.01"
Len Brownd8af6f52015-02-10 01:56:38 -05005013 " - Len Brown <lenb@kernel.org>\n");
5014}
5015
Len Brown495c76542017-02-08 02:41:51 -05005016int add_counter(unsigned int msr_num, char *path, char *name,
5017 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05005018 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05005019{
5020 struct msr_counter *msrp;
5021
5022 msrp = calloc(1, sizeof(struct msr_counter));
5023 if (msrp == NULL) {
5024 perror("calloc");
5025 exit(1);
5026 }
5027
5028 msrp->msr_num = msr_num;
5029 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05005030 if (path)
5031 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05005032 msrp->width = width;
5033 msrp->type = type;
5034 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05005035 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05005036
5037 switch (scope) {
5038
5039 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05005040 msrp->next = sys.tp;
5041 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005042 sys.added_thread_counters++;
Len Brown0748eaf2018-06-01 12:38:29 -04005043 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
Len Brown678a3bd2017-02-09 22:22:13 -05005044 fprintf(stderr, "exceeded max %d added thread counters\n",
5045 MAX_ADDED_COUNTERS);
5046 exit(-1);
5047 }
Len Brown388e9c82016-12-22 23:57:55 -05005048 break;
5049
5050 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05005051 msrp->next = sys.cp;
5052 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005053 sys.added_core_counters++;
5054 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5055 fprintf(stderr, "exceeded max %d added core counters\n",
5056 MAX_ADDED_COUNTERS);
5057 exit(-1);
5058 }
Len Brown388e9c82016-12-22 23:57:55 -05005059 break;
5060
5061 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05005062 msrp->next = sys.pp;
5063 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005064 sys.added_package_counters++;
5065 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5066 fprintf(stderr, "exceeded max %d added package counters\n",
5067 MAX_ADDED_COUNTERS);
5068 exit(-1);
5069 }
Len Brown388e9c82016-12-22 23:57:55 -05005070 break;
5071 }
5072
5073 return 0;
5074}
5075
5076void parse_add_command(char *add_command)
5077{
5078 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05005079 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05005080 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05005081 int width = 64;
5082 int fail = 0;
5083 enum counter_scope scope = SCOPE_CPU;
5084 enum counter_type type = COUNTER_CYCLES;
5085 enum counter_format format = FORMAT_DELTA;
5086
5087 while (add_command) {
5088
5089 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5090 goto next;
5091
5092 if (sscanf(add_command, "msr%d", &msr_num) == 1)
5093 goto next;
5094
Len Brown495c76542017-02-08 02:41:51 -05005095 if (*add_command == '/') {
5096 path = add_command;
5097 goto next;
5098 }
5099
Len Brown388e9c82016-12-22 23:57:55 -05005100 if (sscanf(add_command, "u%d", &width) == 1) {
5101 if ((width == 32) || (width == 64))
5102 goto next;
5103 width = 64;
5104 }
5105 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5106 scope = SCOPE_CPU;
5107 goto next;
5108 }
5109 if (!strncmp(add_command, "core", strlen("core"))) {
5110 scope = SCOPE_CORE;
5111 goto next;
5112 }
5113 if (!strncmp(add_command, "package", strlen("package"))) {
5114 scope = SCOPE_PACKAGE;
5115 goto next;
5116 }
5117 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5118 type = COUNTER_CYCLES;
5119 goto next;
5120 }
5121 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5122 type = COUNTER_SECONDS;
5123 goto next;
5124 }
Len Brown41618e62017-02-09 18:25:22 -05005125 if (!strncmp(add_command, "usec", strlen("usec"))) {
5126 type = COUNTER_USEC;
5127 goto next;
5128 }
Len Brown388e9c82016-12-22 23:57:55 -05005129 if (!strncmp(add_command, "raw", strlen("raw"))) {
5130 format = FORMAT_RAW;
5131 goto next;
5132 }
5133 if (!strncmp(add_command, "delta", strlen("delta"))) {
5134 format = FORMAT_DELTA;
5135 goto next;
5136 }
5137 if (!strncmp(add_command, "percent", strlen("percent"))) {
5138 format = FORMAT_PERCENT;
5139 goto next;
5140 }
5141
5142 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
5143 char *eos;
5144
5145 eos = strchr(name_buffer, ',');
5146 if (eos)
5147 *eos = '\0';
5148 goto next;
5149 }
5150
5151next:
5152 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05005153 if (add_command) {
5154 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05005155 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05005156 }
Len Brown388e9c82016-12-22 23:57:55 -05005157
5158 }
Len Brown495c76542017-02-08 02:41:51 -05005159 if ((msr_num == 0) && (path == NULL)) {
5160 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05005161 fail++;
5162 }
5163
5164 /* generate default column header */
5165 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05005166 if (width == 32)
5167 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5168 else
5169 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05005170 }
5171
Len Brown41618e62017-02-09 18:25:22 -05005172 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05005173 fail++;
5174
5175 if (fail) {
5176 help();
5177 exit(1);
5178 }
5179}
Len Brown41618e62017-02-09 18:25:22 -05005180
Len Browndd778a52017-02-21 23:21:13 -05005181int is_deferred_skip(char *name)
5182{
5183 int i;
5184
5185 for (i = 0; i < deferred_skip_index; ++i)
5186 if (!strcmp(name, deferred_skip_names[i]))
5187 return 1;
5188 return 0;
5189}
5190
Len Brown41618e62017-02-09 18:25:22 -05005191void probe_sysfs(void)
5192{
5193 char path[64];
5194 char name_buf[16];
5195 FILE *input;
5196 int state;
5197 char *sp;
5198
5199 if (!DO_BIC(BIC_sysfs))
5200 return;
5201
Len Brown0748eaf2018-06-01 12:38:29 -04005202 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005203
5204 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5205 base_cpu, state);
5206 input = fopen(path, "r");
5207 if (input == NULL)
5208 continue;
5209 fgets(name_buf, sizeof(name_buf), input);
5210
5211 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5212 sp = strchr(name_buf, '-');
5213 if (!sp)
5214 sp = strchrnul(name_buf, '\n');
5215 *sp = '%';
5216 *(sp + 1) = '\0';
5217
5218 fclose(input);
5219
5220 sprintf(path, "cpuidle/state%d/time", state);
5221
Len Browndd778a52017-02-21 23:21:13 -05005222 if (is_deferred_skip(name_buf))
5223 continue;
5224
Len Brown41618e62017-02-09 18:25:22 -05005225 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
5226 FORMAT_PERCENT, SYSFS_PERCPU);
5227 }
5228
Len Brown0748eaf2018-06-01 12:38:29 -04005229 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005230
5231 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5232 base_cpu, state);
5233 input = fopen(path, "r");
5234 if (input == NULL)
5235 continue;
5236 fgets(name_buf, sizeof(name_buf), input);
5237 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5238 sp = strchr(name_buf, '-');
5239 if (!sp)
5240 sp = strchrnul(name_buf, '\n');
5241 *sp = '\0';
5242 fclose(input);
5243
5244 sprintf(path, "cpuidle/state%d/usage", state);
5245
Len Browndd778a52017-02-21 23:21:13 -05005246 if (is_deferred_skip(name_buf))
5247 continue;
5248
Len Brown41618e62017-02-09 18:25:22 -05005249 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
5250 FORMAT_DELTA, SYSFS_PERCPU);
5251 }
5252
5253}
5254
Len Brown1ef7d212017-02-10 23:54:15 -05005255
5256/*
5257 * parse cpuset with following syntax
5258 * 1,2,4..6,8-10 and set bits in cpu_subset
5259 */
5260void parse_cpu_command(char *optarg)
5261{
5262 unsigned int start, end;
5263 char *next;
5264
Len Brown4e4e1e72017-02-21 22:33:42 -05005265 if (!strcmp(optarg, "core")) {
5266 if (cpu_subset)
5267 goto error;
5268 show_core_only++;
5269 return;
5270 }
5271 if (!strcmp(optarg, "package")) {
5272 if (cpu_subset)
5273 goto error;
5274 show_pkg_only++;
5275 return;
5276 }
5277 if (show_core_only || show_pkg_only)
5278 goto error;
5279
Len Brown1ef7d212017-02-10 23:54:15 -05005280 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5281 if (cpu_subset == NULL)
5282 err(3, "CPU_ALLOC");
5283 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5284
5285 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5286
5287 next = optarg;
5288
5289 while (next && *next) {
5290
5291 if (*next == '-') /* no negative cpu numbers */
5292 goto error;
5293
5294 start = strtoul(next, &next, 10);
5295
5296 if (start >= CPU_SUBSET_MAXCPUS)
5297 goto error;
5298 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5299
5300 if (*next == '\0')
5301 break;
5302
5303 if (*next == ',') {
5304 next += 1;
5305 continue;
5306 }
5307
5308 if (*next == '-') {
5309 next += 1; /* start range */
5310 } else if (*next == '.') {
5311 next += 1;
5312 if (*next == '.')
5313 next += 1; /* start range */
5314 else
5315 goto error;
5316 }
5317
5318 end = strtoul(next, &next, 10);
5319 if (end <= start)
5320 goto error;
5321
5322 while (++start <= end) {
5323 if (start >= CPU_SUBSET_MAXCPUS)
5324 goto error;
5325 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5326 }
5327
5328 if (*next == ',')
5329 next += 1;
5330 else if (*next != '\0')
5331 goto error;
5332 }
5333
5334 return;
5335
5336error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005337 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5338 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005339 exit(-1);
5340}
5341
Len Brown812db3f2017-02-10 00:25:41 -05005342
Len Brown103a8fe2010-10-22 23:53:03 -04005343void cmdline(int argc, char **argv)
5344{
5345 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005346 int option_index = 0;
5347 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005348 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005349 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005350 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005351 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005352 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005353 {"interval", required_argument, 0, 'i'},
Chen Yu023fe0a2018-04-26 08:41:03 +08005354 {"num_iterations", required_argument, 0, 'n'},
Len Brownd8af6f52015-02-10 01:56:38 -05005355 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005356 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005357 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005358 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005359 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005360 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005361 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005362 {"Summary", no_argument, 0, 'S'},
5363 {"TCC", required_argument, 0, 'T'},
5364 {"version", no_argument, 0, 'v' },
5365 {0, 0, 0, 0 }
5366 };
Len Brown103a8fe2010-10-22 23:53:03 -04005367
5368 progname = argv[0];
5369
Chen Yu023fe0a2018-04-26 08:41:03 +08005370 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005371 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005372 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005373 case 'a':
5374 parse_add_command(optarg);
5375 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005376 case 'c':
5377 parse_cpu_command(optarg);
5378 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005379 case 'D':
5380 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005381 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005382 case 'e':
5383 /* --enable specified counter */
5384 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5385 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005386 case 'd':
5387 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005388 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005389 break;
Len Brown812db3f2017-02-10 00:25:41 -05005390 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005391 /*
5392 * --hide: do not show those specified
5393 * multiple invocations simply clear more bits in enabled mask
5394 */
5395 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005396 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005397 case 'h':
5398 default:
5399 help();
5400 exit(1);
5401 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005402 {
5403 double interval = strtod(optarg, NULL);
5404
5405 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005406 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005407 interval);
5408 exit(2);
5409 }
5410
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005411 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005412 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005413 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005414 }
Len Brown889facb2012-11-08 00:48:57 -05005415 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005416 case 'J':
5417 rapl_joules++;
5418 break;
Len Brownc8ade362017-02-15 17:15:11 -05005419 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005420 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005421 list_header_only++;
5422 quiet++;
5423 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005424 case 'o':
5425 outf = fopen_or_die(optarg, "w");
5426 break;
Len Brown96e47152017-01-21 02:26:00 -05005427 case 'q':
5428 quiet = 1;
5429 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08005430 case 'n':
5431 num_iterations = strtod(optarg, NULL);
5432
5433 if (num_iterations <= 0) {
5434 fprintf(outf, "iterations %d should be positive number\n",
5435 num_iterations);
5436 exit(2);
5437 }
5438 break;
Len Brown812db3f2017-02-10 00:25:41 -05005439 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005440 /*
5441 * --show: show only those specified
5442 * The 1st invocation will clear and replace the enabled mask
5443 * subsequent invocations can add to it.
5444 */
5445 if (shown == 0)
5446 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5447 else
5448 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5449 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005450 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005451 case 'S':
5452 summary_only++;
5453 break;
5454 case 'T':
5455 tcc_activation_temp_override = atoi(optarg);
5456 break;
5457 case 'v':
5458 print_version();
5459 exit(0);
5460 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005461 }
5462 }
5463}
5464
5465int main(int argc, char **argv)
5466{
Len Brownb7d8c142016-02-13 23:36:17 -05005467 outf = stderr;
5468
Len Brown103a8fe2010-10-22 23:53:03 -04005469 cmdline(argc, argv);
5470
Len Brown96e47152017-01-21 02:26:00 -05005471 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005472 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005473
Len Brown41618e62017-02-09 18:25:22 -05005474 probe_sysfs();
5475
Len Brown103a8fe2010-10-22 23:53:03 -04005476 turbostat_init();
5477
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005478 /* dump counters and exit */
5479 if (dump_only)
5480 return get_and_dump_counters();
5481
Len Brownc8ade362017-02-15 17:15:11 -05005482 /* list header and exit */
5483 if (list_header_only) {
5484 print_header(",");
5485 flush_output_stdout();
5486 return 0;
5487 }
5488
Len Brown103a8fe2010-10-22 23:53:03 -04005489 /*
5490 * if any params left, it must be a command to fork
5491 */
5492 if (argc - optind)
5493 return fork_it(argv + optind);
5494 else
5495 turbostat_loop();
5496
5497 return 0;
5498}