blob: 42273019da10e752d9d52187a7645bc816bd41d6 [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)
Len Brown812db3f2017-02-10 00:25:41 -0500488
Len Brown3f44a5c2017-10-17 15:42:56 -0400489#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
490
491unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
492unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500493
494#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400495#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500496#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500497#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500498
Len Brown3f44a5c2017-10-17 15:42:56 -0400499
Len Browndd778a52017-02-21 23:21:13 -0500500#define MAX_DEFERRED 16
501char *deferred_skip_names[MAX_DEFERRED];
502int deferred_skip_index;
503
504/*
505 * HIDE_LIST - hide this list of counters, show the rest [default]
506 * SHOW_LIST - show this list of counters, hide the rest
507 */
508enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
509
510void help(void)
511{
512 fprintf(outf,
513 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
514 "\n"
515 "Turbostat forks the specified COMMAND and prints statistics\n"
516 "when COMMAND completes.\n"
517 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
518 "to print statistics, until interrupted.\n"
519 "--add add a counter\n"
520 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
521 "--cpu cpu-set limit output to summary plus cpu-set:\n"
522 " {core | package | j,k,l..m,n-p }\n"
523 "--quiet skip decoding system configuration header\n"
Len Brownb9ad8ee2017-07-19 19:28:37 -0400524 "--interval sec.subsec Override default 5-second measurement interval\n"
Len Browndd778a52017-02-21 23:21:13 -0500525 "--help print this help message\n"
526 "--list list column headers only\n"
Chen Yu023fe0a2018-04-26 08:41:03 +0800527 "--num_iterations num number of the measurement iterations\n"
Len Browndd778a52017-02-21 23:21:13 -0500528 "--out file create or truncate \"file\" for all output\n"
529 "--version print version information\n"
530 "\n"
531 "For more help, run \"man turbostat\"\n");
532}
533
Len Brown812db3f2017-02-10 00:25:41 -0500534/*
535 * bic_lookup
536 * for all the strings in comma separate name_list,
537 * set the approprate bit in return value.
538 */
Len Browndd778a52017-02-21 23:21:13 -0500539unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500540{
541 int i;
542 unsigned long long retval = 0;
543
544 while (name_list) {
545 char *comma;
546
547 comma = strchr(name_list, ',');
548
549 if (comma)
550 *comma = '\0';
551
Len Brown3f44a5c2017-10-17 15:42:56 -0400552 if (!strcmp(name_list, "all"))
553 return ~0;
554
Len Brown812db3f2017-02-10 00:25:41 -0500555 for (i = 0; i < MAX_BIC; ++i) {
556 if (!strcmp(name_list, bic[i].name)) {
557 retval |= (1ULL << i);
558 break;
559 }
560 }
561 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500562 if (mode == SHOW_LIST) {
563 fprintf(stderr, "Invalid counter name: %s\n", name_list);
564 exit(-1);
565 }
566 deferred_skip_names[deferred_skip_index++] = name_list;
567 if (debug)
568 fprintf(stderr, "deferred \"%s\"\n", name_list);
569 if (deferred_skip_index >= MAX_DEFERRED) {
570 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
571 MAX_DEFERRED, name_list);
572 help();
573 exit(1);
574 }
Len Brown812db3f2017-02-10 00:25:41 -0500575 }
576
577 name_list = comma;
578 if (name_list)
579 name_list++;
580
581 }
582 return retval;
583}
Len Brownfc04cc62014-02-06 00:55:19 -0500584
Len Browndd778a52017-02-21 23:21:13 -0500585
Len Brownc8ade362017-02-15 17:15:11 -0500586void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400587{
Len Brown388e9c82016-12-22 23:57:55 -0500588 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500589 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500590
Len Brown3f44a5c2017-10-17 15:42:56 -0400591 if (DO_BIC(BIC_USEC))
592 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
593 if (DO_BIC(BIC_TOD))
594 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500595 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500596 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500597 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500598 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500599 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500600 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500601 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500602 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500603 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500604 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500605 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500606 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500607 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500608 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500609
Len Brown0de6c0d2017-02-15 21:45:40 -0500610 if (DO_BIC(BIC_IRQ)) {
611 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500612 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500613 else
Len Brown6168c2e2017-02-16 23:07:51 -0500614 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500615 }
616
Len Brown812db3f2017-02-10 00:25:41 -0500617 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500618 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500619
Len Brown388e9c82016-12-22 23:57:55 -0500620 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500621
Len Brown388e9c82016-12-22 23:57:55 -0500622 if (mp->format == FORMAT_RAW) {
623 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500624 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500625 else
Len Browndd778a52017-02-21 23:21:13 -0500626 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500627 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500628 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500629 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500630 else
Len Browndd778a52017-02-21 23:21:13 -0500631 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500632 }
633 }
634
Len Brown41618e62017-02-09 18:25:22 -0500635 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500636 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700637 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500638 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500639 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500640 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500641 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500642 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500643
Len Brown0539ba12017-02-10 00:27:20 -0500644 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500645 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500646
Len Brown812db3f2017-02-10 00:25:41 -0500647 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500648 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500649
650 for (mp = sys.cp; mp; mp = mp->next) {
651 if (mp->format == FORMAT_RAW) {
652 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500653 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500654 else
Len Brownc8ade362017-02-15 17:15:11 -0500655 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500656 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500657 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
658 outp += sprintf(outp, "%s%8s", delim, mp->name);
659 else
660 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500661 }
662 }
663
Len Brown812db3f2017-02-10 00:25:41 -0500664 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500665 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500666
Len Brown812db3f2017-02-10 00:25:41 -0500667 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500668 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500669
Len Brown812db3f2017-02-10 00:25:41 -0500670 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500671 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500672
Len Browna99d8732017-05-20 20:11:55 -0400673 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500674 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400675 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500676 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400677 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500678 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400679 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500680 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400681
Len Brown0f47c082017-01-27 00:50:45 -0500682 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500683 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500684 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500685 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500686 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500687 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500688 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500689 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500690 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500691 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500692 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500693 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500694 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500695 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brownbe0e54c2018-06-01 12:35:53 -0400696 if (DO_BIC(BIC_CPU_LPI))
697 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
698 if (DO_BIC(BIC_SYS_LPI))
699 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400700
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800701 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500702 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500703 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500704 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500705 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500706 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500707 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500708 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500709 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500710 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500711 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500712 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500713 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500714 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500715 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500716 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500717 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500718 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500719 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500720 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500721 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500722 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500723 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500724 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500725 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500726 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800727 }
Len Brown388e9c82016-12-22 23:57:55 -0500728 for (mp = sys.pp; mp; mp = mp->next) {
729 if (mp->format == FORMAT_RAW) {
730 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500731 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500732 else
Len Brownc8ade362017-02-15 17:15:11 -0500733 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500734 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500735 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
736 outp += sprintf(outp, "%s%8s", delim, mp->name);
737 else
738 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500739 }
740 }
741
Len Brownc98d5d92012-06-04 00:56:40 -0400742 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400743}
744
Len Brownc98d5d92012-06-04 00:56:40 -0400745int dump_counters(struct thread_data *t, struct core_data *c,
746 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400747{
Len Brown388e9c82016-12-22 23:57:55 -0500748 int i;
749 struct msr_counter *mp;
750
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200751 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400752
Len Brownc98d5d92012-06-04 00:56:40 -0400753 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200754 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
755 t->cpu_id, t->flags);
756 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
757 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
758 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
759 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500760
Len Brown812db3f2017-02-10 00:25:41 -0500761 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500762 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500763 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500764 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500765
766 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
767 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
768 i, mp->msr_num, t->counter[i]);
769 }
Len Brownc98d5d92012-06-04 00:56:40 -0400770 }
Len Brown103a8fe2010-10-22 23:53:03 -0400771
Len Brownc98d5d92012-06-04 00:56:40 -0400772 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200773 outp += sprintf(outp, "core: %d\n", c->core_id);
774 outp += sprintf(outp, "c3: %016llX\n", c->c3);
775 outp += sprintf(outp, "c6: %016llX\n", c->c6);
776 outp += sprintf(outp, "c7: %016llX\n", c->c7);
777 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500778
779 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
780 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
781 i, mp->msr_num, c->counter[i]);
782 }
Len Brown0539ba12017-02-10 00:27:20 -0500783 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400784 }
785
786 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200787 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400788
789 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
790 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
791 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
792 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
793
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200794 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500795 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500796 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500797 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500798 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500799 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500800 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200801 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
802 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
803 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
Len Brownbe0e54c2018-06-01 12:35:53 -0400804 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
805 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
806 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200807 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
808 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
809 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
810 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
811 outp += sprintf(outp, "Throttle PKG: %0X\n",
812 p->rapl_pkg_perf_status);
813 outp += sprintf(outp, "Throttle RAM: %0X\n",
814 p->rapl_dram_perf_status);
815 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500816
817 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
818 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
819 i, mp->msr_num, p->counter[i]);
820 }
Len Brownc98d5d92012-06-04 00:56:40 -0400821 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200822
823 outp += sprintf(outp, "\n");
824
Len Brownc98d5d92012-06-04 00:56:40 -0400825 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400826}
827
Len Browne23da032012-02-06 18:37:16 -0500828/*
829 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500830 */
Len Brownc98d5d92012-06-04 00:56:40 -0400831int format_counters(struct thread_data *t, struct core_data *c,
832 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400833{
Len Brown008d396e2017-02-10 00:29:51 -0500834 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500835 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500836 int i;
837 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500838 char *delim = "\t";
839 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400840
Len Brownc98d5d92012-06-04 00:56:40 -0400841 /* if showing only 1st thread in core and this isn't one, bail out */
842 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
843 return 0;
844
845 /* if showing only 1st thread in pkg and this isn't one, bail out */
846 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
847 return 0;
848
Len Brown1ef7d212017-02-10 23:54:15 -0500849 /*if not summary line and --cpu is used */
850 if ((t != &average.threads) &&
851 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
852 return 0;
853
Len Brown3f44a5c2017-10-17 15:42:56 -0400854 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700855 /* on each row, print how many usec each timestamp took to gather */
856 struct timeval tv;
857
858 timersub(&t->tv_end, &t->tv_begin, &tv);
859 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
860 }
861
Len Brown3f44a5c2017-10-17 15:42:56 -0400862 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
863 if (DO_BIC(BIC_TOD))
864 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
865
Len Brown103a8fe2010-10-22 23:53:03 -0400866 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
867
Len Brown008d396e2017-02-10 00:29:51 -0500868 tsc = t->tsc * tsc_tweak;
869
Len Brownc98d5d92012-06-04 00:56:40 -0400870 /* topo columns, print blanks on 1st (average) line */
871 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500872 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500873 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500874 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500875 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500876 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500877 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400878 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500879 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400880 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500881 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400882 else
Len Brown6168c2e2017-02-16 23:07:51 -0500883 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400884 }
Len Brown812db3f2017-02-10 00:25:41 -0500885 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400886 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500887 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400888 else
Len Brown6168c2e2017-02-16 23:07:51 -0500889 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400890 }
Len Brown812db3f2017-02-10 00:25:41 -0500891 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500892 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400893 }
Len Brownfc04cc62014-02-06 00:55:19 -0500894
Len Brown812db3f2017-02-10 00:25:41 -0500895 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500896 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500897 1.0 / units * t->aperf / interval_float);
898
Len Brown812db3f2017-02-10 00:25:41 -0500899 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500900 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400901
Len Brown812db3f2017-02-10 00:25:41 -0500902 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400903 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500904 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400905 else
Len Brown6168c2e2017-02-16 23:07:51 -0500906 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500907 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400908 }
Len Brown103a8fe2010-10-22 23:53:03 -0400909
Len Brown812db3f2017-02-10 00:25:41 -0500910 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500911 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400912
Len Brown562a2d32016-02-26 23:48:05 -0500913 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500914 if (DO_BIC(BIC_IRQ)) {
915 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500916 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500917 else
Len Brown6168c2e2017-02-16 23:07:51 -0500918 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500919 }
Len Brown562a2d32016-02-26 23:48:05 -0500920
Len Brown1cc21f72015-02-23 00:34:57 -0500921 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500922 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500923 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500924
Len Brown678a3bd2017-02-09 22:22:13 -0500925 /* Added counters */
926 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
927 if (mp->format == FORMAT_RAW) {
928 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500929 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500930 else
Len Brown6168c2e2017-02-16 23:07:51 -0500931 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500932 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500933 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500934 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500935 else
Len Brown6168c2e2017-02-16 23:07:51 -0500936 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500937 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500938 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500939 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500940 else
Len Brown6168c2e2017-02-16 23:07:51 -0500941 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500942 }
943 }
944
Len Brown41618e62017-02-09 18:25:22 -0500945 /* C1 */
946 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500947 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500948
949
Len Brownc98d5d92012-06-04 00:56:40 -0400950 /* print per-core data only for 1st thread in core */
951 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
952 goto done;
953
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700954 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500955 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500956 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500957 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500958 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500959 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400960
Len Brown0539ba12017-02-10 00:27:20 -0500961 /* Mod%c6 */
962 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500963 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500964
Len Brown812db3f2017-02-10 00:25:41 -0500965 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500966 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500967
Len Brown388e9c82016-12-22 23:57:55 -0500968 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
969 if (mp->format == FORMAT_RAW) {
970 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500971 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500972 else
Len Brown6168c2e2017-02-16 23:07:51 -0500973 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500974 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500975 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500976 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500977 else
Len Brown6168c2e2017-02-16 23:07:51 -0500978 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500979 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500980 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500981 }
982 }
983
Len Brownc98d5d92012-06-04 00:56:40 -0400984 /* print per-package data only for 1st core in package */
985 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
986 goto done;
987
Len Brown0b2bb692015-03-26 00:50:30 -0400988 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500989 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500990 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500991
Len Brownfdf676e2016-02-27 01:28:12 -0500992 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500993 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400994 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500995 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400996 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500997 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400998 p->gfx_rc6_ms / 10.0 / interval_float);
999 }
1000 }
Len Brownfdf676e2016-02-27 01:28:12 -05001001
Len Brown27d47352016-02-27 00:37:54 -05001002 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -05001003 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -05001004 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -05001005
Len Brown0b2bb692015-03-26 00:50:30 -04001006 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -04001007 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001008 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001009 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001010 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001011 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -05001012 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -04001013 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -05001014 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -04001015
Len Brown0f47c082017-01-27 00:50:45 -05001016 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -05001017 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001018 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -05001019 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001020 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -05001021 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001022 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -05001023 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001024 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -05001025 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001026 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -05001027 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001028 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -05001029 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -05001030
Len Brownbe0e54c2018-06-01 12:35:53 -04001031 if (DO_BIC(BIC_CPU_LPI))
1032 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1033 if (DO_BIC(BIC_SYS_LPI))
1034 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1035
Len Brown889facb2012-11-08 00:48:57 -05001036 /*
1037 * If measurement interval exceeds minimum RAPL Joule Counter range,
1038 * indicate that results are suspect by printing "**" in fraction place.
1039 */
Len Brownfc04cc62014-02-06 00:55:19 -05001040 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -05001041 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -05001042 else
Len Browne975db52016-04-06 23:56:02 -04001043 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -05001044
Len Brown812db3f2017-02-10 00:25:41 -05001045 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001046 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001047 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001048 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001049 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001050 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001051 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001052 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001053 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001054 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001055 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001056 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001057 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001058 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001059 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001060 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001061 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001062 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 -05001063 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001064 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 -05001065
Len Brown388e9c82016-12-22 23:57:55 -05001066 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1067 if (mp->format == FORMAT_RAW) {
1068 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001069 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001070 else
Len Brown6168c2e2017-02-16 23:07:51 -05001071 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001072 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001073 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001074 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001075 else
Len Brown6168c2e2017-02-16 23:07:51 -05001076 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001077 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001078 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001079 }
1080 }
1081
Len Brownc98d5d92012-06-04 00:56:40 -04001082done:
Len Brown94d6ab42018-01-27 22:39:21 -05001083 if (*(outp - 1) != '\n')
1084 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001085
1086 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001087}
1088
Len Brownb7d8c142016-02-13 23:36:17 -05001089void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001090{
Len Brownb7d8c142016-02-13 23:36:17 -05001091 FILE *filep;
1092
1093 if (outf == stderr)
1094 filep = stdout;
1095 else
1096 filep = outf;
1097
1098 fputs(output_buffer, filep);
1099 fflush(filep);
1100
Len Brownc98d5d92012-06-04 00:56:40 -04001101 outp = output_buffer;
1102}
Len Brownb7d8c142016-02-13 23:36:17 -05001103void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001104{
Len Brownb7d8c142016-02-13 23:36:17 -05001105 fputs(output_buffer, outf);
1106 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001107 outp = output_buffer;
1108}
1109void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1110{
Len Browne23da032012-02-06 18:37:16 -05001111 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001112
Len Browne23da032012-02-06 18:37:16 -05001113 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001114 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001115
Len Brownc98d5d92012-06-04 00:56:40 -04001116 if (topo.num_cpus > 1)
1117 format_counters(&average.threads, &average.cores,
1118 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001119
Len Browne23da032012-02-06 18:37:16 -05001120 printed = 1;
1121
1122 if (summary_only)
1123 return;
1124
Len Brownc98d5d92012-06-04 00:56:40 -04001125 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001126}
1127
Len Brown889facb2012-11-08 00:48:57 -05001128#define DELTA_WRAP32(new, old) \
1129 if (new > old) { \
1130 old = new - old; \
1131 } else { \
1132 old = 0x100000000 + new - old; \
1133 }
1134
Len Brownba3dec92016-04-22 20:31:46 -04001135int
Len Brownc98d5d92012-06-04 00:56:40 -04001136delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001137{
Len Brown388e9c82016-12-22 23:57:55 -05001138 int i;
1139 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001140
Len Browna99d8732017-05-20 20:11:55 -04001141
1142 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001143 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001144 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001145 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001146 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001147 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001148 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001149 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 -04001150
Len Brownc98d5d92012-06-04 00:56:40 -04001151 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001152 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001153 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001154 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001155 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001156 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001157 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001158 old->pc8 = new->pc8 - old->pc8;
1159 old->pc9 = new->pc9 - old->pc9;
1160 old->pc10 = new->pc10 - old->pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -04001161 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1162 old->sys_lpi = new->sys_lpi - old->sys_lpi;
Len Brown889facb2012-11-08 00:48:57 -05001163 old->pkg_temp_c = new->pkg_temp_c;
1164
Len Brown9185e982016-04-06 17:16:00 -04001165 /* flag an error when rc6 counter resets/wraps */
1166 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1167 old->gfx_rc6_ms = -1;
1168 else
1169 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1170
Len Brown27d47352016-02-27 00:37:54 -05001171 old->gfx_mhz = new->gfx_mhz;
1172
Len Brown889facb2012-11-08 00:48:57 -05001173 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1174 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1175 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1176 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1177 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1178 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001179
Len Brown388e9c82016-12-22 23:57:55 -05001180 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1181 if (mp->format == FORMAT_RAW)
1182 old->counter[i] = new->counter[i];
1183 else
1184 old->counter[i] = new->counter[i] - old->counter[i];
1185 }
1186
Len Brownba3dec92016-04-22 20:31:46 -04001187 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001188}
Len Brown103a8fe2010-10-22 23:53:03 -04001189
Len Brownc98d5d92012-06-04 00:56:40 -04001190void
1191delta_core(struct core_data *new, struct core_data *old)
1192{
Len Brown388e9c82016-12-22 23:57:55 -05001193 int i;
1194 struct msr_counter *mp;
1195
Len Brownc98d5d92012-06-04 00:56:40 -04001196 old->c3 = new->c3 - old->c3;
1197 old->c6 = new->c6 - old->c6;
1198 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001199 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001200 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001201
1202 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1203 if (mp->format == FORMAT_RAW)
1204 old->counter[i] = new->counter[i];
1205 else
1206 old->counter[i] = new->counter[i] - old->counter[i];
1207 }
Len Brownc98d5d92012-06-04 00:56:40 -04001208}
Len Brown103a8fe2010-10-22 23:53:03 -04001209
Len Brownc3ae3312012-06-13 21:31:46 -04001210/*
1211 * old = new - old
1212 */
Len Brownba3dec92016-04-22 20:31:46 -04001213int
Len Brownc98d5d92012-06-04 00:56:40 -04001214delta_thread(struct thread_data *new, struct thread_data *old,
1215 struct core_data *core_delta)
1216{
Len Brown388e9c82016-12-22 23:57:55 -05001217 int i;
1218 struct msr_counter *mp;
1219
Len Brown3f44a5c2017-10-17 15:42:56 -04001220 /*
1221 * the timestamps from start of measurement interval are in "old"
1222 * the timestamp from end of measurement interval are in "new"
1223 * over-write old w/ new so we can print end of interval values
1224 */
1225
1226 old->tv_begin = new->tv_begin;
1227 old->tv_end = new->tv_end;
1228
Len Brownc98d5d92012-06-04 00:56:40 -04001229 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001230
Len Brownc98d5d92012-06-04 00:56:40 -04001231 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001232 if (old->tsc < (1000 * 1000))
1233 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1234 "You can disable all c-states by booting with \"idle=poll\"\n"
1235 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001236
Len Brownc98d5d92012-06-04 00:56:40 -04001237 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001238
Len Brown812db3f2017-02-10 00:25:41 -05001239 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001240 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1241 old->aperf = new->aperf - old->aperf;
1242 old->mperf = new->mperf - old->mperf;
1243 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001244 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001245 }
Len Brownc98d5d92012-06-04 00:56:40 -04001246 }
Len Brown103a8fe2010-10-22 23:53:03 -04001247
Len Brown103a8fe2010-10-22 23:53:03 -04001248
Len Brown144b44b2013-11-09 00:30:16 -05001249 if (use_c1_residency_msr) {
1250 /*
1251 * Some models have a dedicated C1 residency MSR,
1252 * which should be more accurate than the derivation below.
1253 */
1254 } else {
1255 /*
1256 * As counter collection is not atomic,
1257 * it is possible for mperf's non-halted cycles + idle states
1258 * to exceed TSC's all cycles: show c1 = 0% in that case.
1259 */
Len Brown95149362017-04-12 19:44:51 -04001260 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001261 old->c1 = 0;
1262 else {
1263 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001264 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001265 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001266 }
Len Brownc98d5d92012-06-04 00:56:40 -04001267 }
Len Brownc3ae3312012-06-13 21:31:46 -04001268
Len Brownc98d5d92012-06-04 00:56:40 -04001269 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001270 if (debug > 1)
1271 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001272 old->mperf = 1; /* divide by 0 protection */
1273 }
1274
Len Brown812db3f2017-02-10 00:25:41 -05001275 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001276 old->irq_count = new->irq_count - old->irq_count;
1277
Len Brown812db3f2017-02-10 00:25:41 -05001278 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001279 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001280
Len Brown388e9c82016-12-22 23:57:55 -05001281 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1282 if (mp->format == FORMAT_RAW)
1283 old->counter[i] = new->counter[i];
1284 else
1285 old->counter[i] = new->counter[i] - old->counter[i];
1286 }
Len Brownba3dec92016-04-22 20:31:46 -04001287 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001288}
1289
1290int delta_cpu(struct thread_data *t, struct core_data *c,
1291 struct pkg_data *p, struct thread_data *t2,
1292 struct core_data *c2, struct pkg_data *p2)
1293{
Len Brownba3dec92016-04-22 20:31:46 -04001294 int retval = 0;
1295
Len Brownc98d5d92012-06-04 00:56:40 -04001296 /* calculate core delta only for 1st thread in core */
1297 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1298 delta_core(c, c2);
1299
1300 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001301 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1302 if (retval)
1303 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001304
1305 /* calculate package delta only for 1st core in package */
1306 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001307 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001308
Len Brownba3dec92016-04-22 20:31:46 -04001309 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001310}
1311
1312void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1313{
Len Brown388e9c82016-12-22 23:57:55 -05001314 int i;
1315 struct msr_counter *mp;
1316
Len Brown3f44a5c2017-10-17 15:42:56 -04001317 t->tv_begin.tv_sec = 0;
1318 t->tv_begin.tv_usec = 0;
1319 t->tv_end.tv_sec = 0;
1320 t->tv_end.tv_usec = 0;
1321
Len Brownc98d5d92012-06-04 00:56:40 -04001322 t->tsc = 0;
1323 t->aperf = 0;
1324 t->mperf = 0;
1325 t->c1 = 0;
1326
Len Brown562a2d32016-02-26 23:48:05 -05001327 t->irq_count = 0;
1328 t->smi_count = 0;
1329
Len Brownc98d5d92012-06-04 00:56:40 -04001330 /* tells format_counters to dump all fields from this set */
1331 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1332
1333 c->c3 = 0;
1334 c->c6 = 0;
1335 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001336 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001337 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001338
Len Brown0b2bb692015-03-26 00:50:30 -04001339 p->pkg_wtd_core_c0 = 0;
1340 p->pkg_any_core_c0 = 0;
1341 p->pkg_any_gfxe_c0 = 0;
1342 p->pkg_both_core_gfxe_c0 = 0;
1343
Len Brownc98d5d92012-06-04 00:56:40 -04001344 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001345 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001346 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001347 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001348 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001349 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001350 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001351 p->pc8 = 0;
1352 p->pc9 = 0;
1353 p->pc10 = 0;
Len Brownbe0e54c2018-06-01 12:35:53 -04001354 p->cpu_lpi = 0;
1355 p->sys_lpi = 0;
Len Brown889facb2012-11-08 00:48:57 -05001356
1357 p->energy_pkg = 0;
1358 p->energy_dram = 0;
1359 p->energy_cores = 0;
1360 p->energy_gfx = 0;
1361 p->rapl_pkg_perf_status = 0;
1362 p->rapl_dram_perf_status = 0;
1363 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001364
Len Brownfdf676e2016-02-27 01:28:12 -05001365 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001366 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001367 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1368 t->counter[i] = 0;
1369
1370 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1371 c->counter[i] = 0;
1372
1373 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1374 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001375}
1376int sum_counters(struct thread_data *t, struct core_data *c,
1377 struct pkg_data *p)
1378{
Len Brown388e9c82016-12-22 23:57:55 -05001379 int i;
1380 struct msr_counter *mp;
1381
Len Brown3f44a5c2017-10-17 15:42:56 -04001382 /* remember first tv_begin */
1383 if (average.threads.tv_begin.tv_sec == 0)
1384 average.threads.tv_begin = t->tv_begin;
1385
1386 /* remember last tv_end */
1387 average.threads.tv_end = t->tv_end;
1388
Len Brownc98d5d92012-06-04 00:56:40 -04001389 average.threads.tsc += t->tsc;
1390 average.threads.aperf += t->aperf;
1391 average.threads.mperf += t->mperf;
1392 average.threads.c1 += t->c1;
1393
Len Brown562a2d32016-02-26 23:48:05 -05001394 average.threads.irq_count += t->irq_count;
1395 average.threads.smi_count += t->smi_count;
1396
Len Brown388e9c82016-12-22 23:57:55 -05001397 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1398 if (mp->format == FORMAT_RAW)
1399 continue;
1400 average.threads.counter[i] += t->counter[i];
1401 }
1402
Len Brownc98d5d92012-06-04 00:56:40 -04001403 /* sum per-core values only for 1st thread in core */
1404 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1405 return 0;
1406
1407 average.cores.c3 += c->c3;
1408 average.cores.c6 += c->c6;
1409 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001410 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001411
Len Brown889facb2012-11-08 00:48:57 -05001412 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1413
Len Brown388e9c82016-12-22 23:57:55 -05001414 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1415 if (mp->format == FORMAT_RAW)
1416 continue;
1417 average.cores.counter[i] += c->counter[i];
1418 }
1419
Len Brownc98d5d92012-06-04 00:56:40 -04001420 /* sum per-pkg values only for 1st core in pkg */
1421 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1422 return 0;
1423
Len Browna99d8732017-05-20 20:11:55 -04001424 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001425 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001426 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001427 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001428 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001429 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001430 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001431 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001432
Len Brownc98d5d92012-06-04 00:56:40 -04001433 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001434 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001435 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001436 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001437 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001438 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001439 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001440 average.packages.pc8 += p->pc8;
1441 average.packages.pc9 += p->pc9;
1442 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001443
Len Brownbe0e54c2018-06-01 12:35:53 -04001444 average.packages.cpu_lpi = p->cpu_lpi;
1445 average.packages.sys_lpi = p->sys_lpi;
1446
Len Brown889facb2012-11-08 00:48:57 -05001447 average.packages.energy_pkg += p->energy_pkg;
1448 average.packages.energy_dram += p->energy_dram;
1449 average.packages.energy_cores += p->energy_cores;
1450 average.packages.energy_gfx += p->energy_gfx;
1451
Len Brownfdf676e2016-02-27 01:28:12 -05001452 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001453 average.packages.gfx_mhz = p->gfx_mhz;
1454
Len Brown889facb2012-11-08 00:48:57 -05001455 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1456
1457 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1458 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001459
1460 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1461 if (mp->format == FORMAT_RAW)
1462 continue;
1463 average.packages.counter[i] += p->counter[i];
1464 }
Len Brownc98d5d92012-06-04 00:56:40 -04001465 return 0;
1466}
1467/*
1468 * sum the counters for all cpus in the system
1469 * compute the weighted average
1470 */
1471void compute_average(struct thread_data *t, struct core_data *c,
1472 struct pkg_data *p)
1473{
Len Brown388e9c82016-12-22 23:57:55 -05001474 int i;
1475 struct msr_counter *mp;
1476
Len Brownc98d5d92012-06-04 00:56:40 -04001477 clear_counters(&average.threads, &average.cores, &average.packages);
1478
1479 for_all_cpus(sum_counters, t, c, p);
1480
1481 average.threads.tsc /= topo.num_cpus;
1482 average.threads.aperf /= topo.num_cpus;
1483 average.threads.mperf /= topo.num_cpus;
1484 average.threads.c1 /= topo.num_cpus;
1485
Len Brown0de6c0d2017-02-15 21:45:40 -05001486 if (average.threads.irq_count > 9999999)
1487 sums_need_wide_columns = 1;
1488
Len Brownc98d5d92012-06-04 00:56:40 -04001489 average.cores.c3 /= topo.num_cores;
1490 average.cores.c6 /= topo.num_cores;
1491 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001492 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001493
Len Browna99d8732017-05-20 20:11:55 -04001494 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001495 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001496 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001497 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001498 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001499 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001500 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001501 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001502
Len Brownc98d5d92012-06-04 00:56:40 -04001503 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001504 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001505 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001506 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001507 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001508 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001509 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001510
1511 average.packages.pc8 /= topo.num_packages;
1512 average.packages.pc9 /= topo.num_packages;
1513 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001514
1515 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1516 if (mp->format == FORMAT_RAW)
1517 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001518 if (mp->type == COUNTER_ITEMS) {
1519 if (average.threads.counter[i] > 9999999)
1520 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001521 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001522 }
Len Brown388e9c82016-12-22 23:57:55 -05001523 average.threads.counter[i] /= topo.num_cpus;
1524 }
1525 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1526 if (mp->format == FORMAT_RAW)
1527 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001528 if (mp->type == COUNTER_ITEMS) {
1529 if (average.cores.counter[i] > 9999999)
1530 sums_need_wide_columns = 1;
1531 }
Len Brown388e9c82016-12-22 23:57:55 -05001532 average.cores.counter[i] /= topo.num_cores;
1533 }
1534 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1535 if (mp->format == FORMAT_RAW)
1536 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001537 if (mp->type == COUNTER_ITEMS) {
1538 if (average.packages.counter[i] > 9999999)
1539 sums_need_wide_columns = 1;
1540 }
Len Brown388e9c82016-12-22 23:57:55 -05001541 average.packages.counter[i] /= topo.num_packages;
1542 }
Len Brownc98d5d92012-06-04 00:56:40 -04001543}
1544
1545static unsigned long long rdtsc(void)
1546{
1547 unsigned int low, high;
1548
1549 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1550
1551 return low | ((unsigned long long)high) << 32;
1552}
1553
Len Brownc98d5d92012-06-04 00:56:40 -04001554/*
Len Brown495c76542017-02-08 02:41:51 -05001555 * Open a file, and exit on failure
1556 */
1557FILE *fopen_or_die(const char *path, const char *mode)
1558{
1559 FILE *filep = fopen(path, mode);
1560
1561 if (!filep)
1562 err(1, "%s: open failed", path);
1563 return filep;
1564}
1565/*
1566 * snapshot_sysfs_counter()
1567 *
1568 * return snapshot of given counter
1569 */
1570unsigned long long snapshot_sysfs_counter(char *path)
1571{
1572 FILE *fp;
1573 int retval;
1574 unsigned long long counter;
1575
1576 fp = fopen_or_die(path, "r");
1577
1578 retval = fscanf(fp, "%lld", &counter);
1579 if (retval != 1)
1580 err(1, "snapshot_sysfs_counter(%s)", path);
1581
1582 fclose(fp);
1583
1584 return counter;
1585}
1586
1587int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1588{
1589 if (mp->msr_num != 0) {
1590 if (get_msr(cpu, mp->msr_num, counterp))
1591 return -1;
1592 } else {
Len Brown46c27972017-12-08 17:38:17 -05001593 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001594
1595 if (mp->flags & SYSFS_PERCPU) {
1596 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1597 cpu, mp->path);
1598
1599 *counterp = snapshot_sysfs_counter(path);
1600 } else {
1601 *counterp = snapshot_sysfs_counter(mp->path);
1602 }
Len Brown495c76542017-02-08 02:41:51 -05001603 }
1604
1605 return 0;
1606}
1607
1608/*
Len Brownc98d5d92012-06-04 00:56:40 -04001609 * get_counters(...)
1610 * migrate to cpu
1611 * acquire and record local counters for that cpu
1612 */
1613int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1614{
1615 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001616 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001617 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001618 struct msr_counter *mp;
1619 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001620
Len Brownf4fdf2b2017-05-27 21:06:55 -07001621
1622 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1623
Len Browne52966c2012-11-08 22:38:05 -05001624 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001625 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001626 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001627 }
Len Brownc98d5d92012-06-04 00:56:40 -04001628
Len Brown0102b062016-02-27 03:11:29 -05001629retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001630 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1631
Len Brown812db3f2017-02-10 00:25:41 -05001632 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001633 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1634
1635 /*
1636 * The TSC, APERF and MPERF must be read together for
1637 * APERF/MPERF and MPERF/TSC to give accurate results.
1638 *
1639 * Unfortunately, APERF and MPERF are read by
1640 * individual system call, so delays may occur
1641 * between them. If the time to read them
1642 * varies by a large amount, we re-read them.
1643 */
1644
1645 /*
1646 * This initial dummy APERF read has been seen to
1647 * reduce jitter in the subsequent reads.
1648 */
1649
Len Brown9c63a652012-10-31 01:29:52 -04001650 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001651 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001652
1653 t->tsc = rdtsc(); /* re-read close to APERF */
1654
1655 tsc_before = t->tsc;
1656
1657 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1658 return -3;
1659
1660 tsc_between = rdtsc();
1661
Len Brown9c63a652012-10-31 01:29:52 -04001662 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001663 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001664
1665 tsc_after = rdtsc();
1666
1667 aperf_time = tsc_between - tsc_before;
1668 mperf_time = tsc_after - tsc_between;
1669
1670 /*
1671 * If the system call latency to read APERF and MPERF
1672 * differ by more than 2x, then try again.
1673 */
1674 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1675 aperf_mperf_retry_count++;
1676 if (aperf_mperf_retry_count < 5)
1677 goto retry;
1678 else
1679 warnx("cpu%d jitter %lld %lld",
1680 cpu, aperf_time, mperf_time);
1681 }
1682 aperf_mperf_retry_count = 0;
1683
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001684 t->aperf = t->aperf * aperf_mperf_multiplier;
1685 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001686 }
1687
Len Brown812db3f2017-02-10 00:25:41 -05001688 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001689 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001690 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001691 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1692 return -5;
1693 t->smi_count = msr & 0xFFFFFFFF;
1694 }
Len Brown0539ba12017-02-10 00:27:20 -05001695 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001696 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1697 return -6;
1698 }
1699
Len Brown388e9c82016-12-22 23:57:55 -05001700 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001701 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001702 return -10;
1703 }
1704
Len Brownc98d5d92012-06-04 00:56:40 -04001705 /* collect core counters only for 1st thread in core */
1706 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001707 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001708
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07001709 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001710 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1711 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001712 }
1713
Len Brown812db3f2017-02-10 00:25:41 -05001714 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001715 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1716 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001717 } else if (do_knl_cstates) {
1718 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1719 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001720 }
1721
Len Brown812db3f2017-02-10 00:25:41 -05001722 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001723 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1724 return -8;
1725
Len Brown0539ba12017-02-10 00:27:20 -05001726 if (DO_BIC(BIC_Mod_c6))
1727 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1728 return -8;
1729
Len Brown812db3f2017-02-10 00:25:41 -05001730 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001731 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1732 return -9;
1733 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1734 }
1735
Len Brown388e9c82016-12-22 23:57:55 -05001736 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001737 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001738 return -10;
1739 }
Len Brown889facb2012-11-08 00:48:57 -05001740
Len Brownc98d5d92012-06-04 00:56:40 -04001741 /* collect package counters only for 1st core in package */
1742 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001743 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001744
Len Browna99d8732017-05-20 20:11:55 -04001745 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001746 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1747 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001748 }
1749 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001750 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1751 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001752 }
1753 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001754 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1755 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001756 }
1757 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001758 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1759 return -13;
1760 }
Len Brown0f47c082017-01-27 00:50:45 -05001761 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001762 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1763 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001764 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001765 if (do_slm_cstates) {
1766 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1767 return -10;
1768 } else {
1769 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1770 return -10;
1771 }
1772 }
1773
Len Brown0f47c082017-01-27 00:50:45 -05001774 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001775 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1776 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001777 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001778 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1779 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001780 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001781 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1782 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001783 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001784 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1785 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001786 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001787 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1788 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001789
Len Brownbe0e54c2018-06-01 12:35:53 -04001790 if (DO_BIC(BIC_CPU_LPI))
1791 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1792 if (DO_BIC(BIC_SYS_LPI))
1793 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1794
Len Brown889facb2012-11-08 00:48:57 -05001795 if (do_rapl & RAPL_PKG) {
1796 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1797 return -13;
1798 p->energy_pkg = msr & 0xFFFFFFFF;
1799 }
Jacob Pan91484942016-06-16 09:48:20 -07001800 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001801 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1802 return -14;
1803 p->energy_cores = msr & 0xFFFFFFFF;
1804 }
1805 if (do_rapl & RAPL_DRAM) {
1806 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1807 return -15;
1808 p->energy_dram = msr & 0xFFFFFFFF;
1809 }
1810 if (do_rapl & RAPL_GFX) {
1811 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1812 return -16;
1813 p->energy_gfx = msr & 0xFFFFFFFF;
1814 }
1815 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1816 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1817 return -16;
1818 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1819 }
1820 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1821 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1822 return -16;
1823 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1824 }
Len Brown812db3f2017-02-10 00:25:41 -05001825 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001826 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1827 return -17;
1828 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1829 }
Len Brownfdf676e2016-02-27 01:28:12 -05001830
Len Brown812db3f2017-02-10 00:25:41 -05001831 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001832 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1833
Len Brown812db3f2017-02-10 00:25:41 -05001834 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001835 p->gfx_mhz = gfx_cur_mhz;
1836
Len Brown388e9c82016-12-22 23:57:55 -05001837 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001838 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001839 return -10;
1840 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001841done:
1842 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001843
Len Brown103a8fe2010-10-22 23:53:03 -04001844 return 0;
1845}
1846
Len Brownee7e38e2015-02-09 23:39:45 -05001847/*
1848 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1849 * If you change the values, note they are used both in comparisons
1850 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1851 */
1852
1853#define PCLUKN 0 /* Unknown */
1854#define PCLRSV 1 /* Reserved */
1855#define PCL__0 2 /* PC0 */
1856#define PCL__1 3 /* PC1 */
1857#define PCL__2 4 /* PC2 */
1858#define PCL__3 5 /* PC3 */
1859#define PCL__4 6 /* PC4 */
1860#define PCL__6 7 /* PC6 */
1861#define PCL_6N 8 /* PC6 No Retention */
1862#define PCL_6R 9 /* PC6 Retention */
1863#define PCL__7 10 /* PC7 */
1864#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001865#define PCL__8 12 /* PC8 */
1866#define PCL__9 13 /* PC9 */
1867#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001868
1869int pkg_cstate_limit = PCLUKN;
1870char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001871 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001872
Len Browne9257f52015-04-01 21:02:57 -04001873int 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};
1874int 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};
1875int 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 -05001876int 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 -05001877int 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 -04001878int 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 -04001879int 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 +03001880int 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 -05001881
Len Browna2b7b742015-09-26 00:12:38 -04001882
1883static void
1884calculate_tsc_tweak()
1885{
Len Browna2b7b742015-09-26 00:12:38 -04001886 tsc_tweak = base_hz / tsc_hz;
1887}
1888
Len Brownfcd17212015-03-23 20:29:09 -04001889static void
1890dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001891{
1892 unsigned long long msr;
1893 unsigned int ratio;
1894
Len Brownec0adc52015-11-12 02:42:31 -05001895 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001896
Len Brownb7d8c142016-02-13 23:36:17 -05001897 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001898
Len Brown103a8fe2010-10-22 23:53:03 -04001899 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001900 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001901 ratio, bclk, ratio * bclk);
1902
1903 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001904 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001905 ratio, bclk, ratio * bclk);
1906
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001907 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001908 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001909 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001910
Len Brownfcd17212015-03-23 20:29:09 -04001911 return;
1912}
1913
1914static void
1915dump_hsw_turbo_ratio_limits(void)
1916{
1917 unsigned long long msr;
1918 unsigned int ratio;
1919
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001920 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001921
Len Brownb7d8c142016-02-13 23:36:17 -05001922 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001923
1924 ratio = (msr >> 8) & 0xFF;
1925 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001926 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001927 ratio, bclk, ratio * bclk);
1928
1929 ratio = (msr >> 0) & 0xFF;
1930 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001931 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001932 ratio, bclk, ratio * bclk);
1933 return;
1934}
1935
1936static void
1937dump_ivt_turbo_ratio_limits(void)
1938{
1939 unsigned long long msr;
1940 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001941
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001942 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001943
Len Brownb7d8c142016-02-13 23:36:17 -05001944 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001945
1946 ratio = (msr >> 56) & 0xFF;
1947 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001948 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001949 ratio, bclk, ratio * bclk);
1950
1951 ratio = (msr >> 48) & 0xFF;
1952 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001953 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001954 ratio, bclk, ratio * bclk);
1955
1956 ratio = (msr >> 40) & 0xFF;
1957 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001958 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001959 ratio, bclk, ratio * bclk);
1960
1961 ratio = (msr >> 32) & 0xFF;
1962 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001963 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001964 ratio, bclk, ratio * bclk);
1965
1966 ratio = (msr >> 24) & 0xFF;
1967 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001968 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001969 ratio, bclk, ratio * bclk);
1970
1971 ratio = (msr >> 16) & 0xFF;
1972 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001973 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001974 ratio, bclk, ratio * bclk);
1975
1976 ratio = (msr >> 8) & 0xFF;
1977 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001978 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001979 ratio, bclk, ratio * bclk);
1980
1981 ratio = (msr >> 0) & 0xFF;
1982 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001983 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001984 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001985 return;
1986}
Len Brown31e07522017-01-31 23:07:49 -05001987int has_turbo_ratio_group_limits(int family, int model)
1988{
1989
1990 if (!genuine_intel)
1991 return 0;
1992
1993 switch (model) {
1994 case INTEL_FAM6_ATOM_GOLDMONT:
1995 case INTEL_FAM6_SKYLAKE_X:
1996 case INTEL_FAM6_ATOM_DENVERTON:
1997 return 1;
1998 }
1999 return 0;
2000}
Len Brown6574a5d2012-09-21 00:01:31 -04002001
Len Brownfcd17212015-03-23 20:29:09 -04002002static void
Len Brown31e07522017-01-31 23:07:49 -05002003dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04002004{
Len Brown31e07522017-01-31 23:07:49 -05002005 unsigned long long msr, core_counts;
2006 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04002007
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002008 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002009 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04002010
Len Brown31e07522017-01-31 23:07:49 -05002011 if (has_turbo_ratio_group_limits(family, model)) {
2012 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
2013 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
2014 } else {
2015 core_counts = 0x0807060504030201;
2016 }
2017
Len Brown6574a5d2012-09-21 00:01:31 -04002018 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002019 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002020 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002021 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2022 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002023
2024 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002025 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002026 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002027 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2028 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002029
2030 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002031 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002032 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002033 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2034 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002035
2036 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002037 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002038 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002039 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2040 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002041
Len Brown103a8fe2010-10-22 23:53:03 -04002042 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002043 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002044 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002045 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2046 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002047
2048 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002049 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002050 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002051 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2052 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002053
2054 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002055 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002056 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002057 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2058 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002059
2060 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002061 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002062 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002063 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2064 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002065 return;
2066}
Len Brown3a9a9412014-08-15 02:39:52 -04002067
Len Brownfcd17212015-03-23 20:29:09 -04002068static void
Len Brown0f7887c2017-01-12 23:49:18 -05002069dump_atom_turbo_ratio_limits(void)
2070{
2071 unsigned long long msr;
2072 unsigned int ratio;
2073
2074 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2075 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2076
2077 ratio = (msr >> 0) & 0x3F;
2078 if (ratio)
2079 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2080 ratio, bclk, ratio * bclk);
2081
2082 ratio = (msr >> 8) & 0x3F;
2083 if (ratio)
2084 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2085 ratio, bclk, ratio * bclk);
2086
2087 ratio = (msr >> 16) & 0x3F;
2088 if (ratio)
2089 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2090 ratio, bclk, ratio * bclk);
2091
2092 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2093 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2094
2095 ratio = (msr >> 24) & 0x3F;
2096 if (ratio)
2097 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2098 ratio, bclk, ratio * bclk);
2099
2100 ratio = (msr >> 16) & 0x3F;
2101 if (ratio)
2102 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2103 ratio, bclk, ratio * bclk);
2104
2105 ratio = (msr >> 8) & 0x3F;
2106 if (ratio)
2107 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2108 ratio, bclk, ratio * bclk);
2109
2110 ratio = (msr >> 0) & 0x3F;
2111 if (ratio)
2112 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2113 ratio, bclk, ratio * bclk);
2114}
2115
2116static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002117dump_knl_turbo_ratio_limits(void)
2118{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002119 const unsigned int buckets_no = 7;
2120
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002121 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002122 int delta_cores, delta_ratio;
2123 int i, b_nr;
2124 unsigned int cores[buckets_no];
2125 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002126
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002127 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002128
Len Brownb7d8c142016-02-13 23:36:17 -05002129 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002130 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002131
2132 /**
2133 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002134 * [0] -- Reserved
2135 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002136 * [15:8] -- Base value of freq ratio of bucket 1.
2137 * [20:16] -- +ve delta of number of active cores of bucket 2.
2138 * i.e. active cores of bucket 2 =
2139 * active cores of bucket 1 + delta
2140 * [23:21] -- Negative delta of freq ratio of bucket 2.
2141 * i.e. freq ratio of bucket 2 =
2142 * freq ratio of bucket 1 - delta
2143 * [28:24]-- +ve delta of number of active cores of bucket 3.
2144 * [31:29]-- -ve delta of freq ratio of bucket 3.
2145 * [36:32]-- +ve delta of number of active cores of bucket 4.
2146 * [39:37]-- -ve delta of freq ratio of bucket 4.
2147 * [44:40]-- +ve delta of number of active cores of bucket 5.
2148 * [47:45]-- -ve delta of freq ratio of bucket 5.
2149 * [52:48]-- +ve delta of number of active cores of bucket 6.
2150 * [55:53]-- -ve delta of freq ratio of bucket 6.
2151 * [60:56]-- +ve delta of number of active cores of bucket 7.
2152 * [63:61]-- -ve delta of freq ratio of bucket 7.
2153 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002154
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002155 b_nr = 0;
2156 cores[b_nr] = (msr & 0xFF) >> 1;
2157 ratio[b_nr] = (msr >> 8) & 0xFF;
2158
2159 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002160 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002161 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002162
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002163 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2164 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2165 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002166 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002167
2168 for (i = buckets_no - 1; i >= 0; i--)
2169 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002170 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002171 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002172 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002173}
2174
2175static void
Len Brownfcd17212015-03-23 20:29:09 -04002176dump_nhm_cst_cfg(void)
2177{
2178 unsigned long long msr;
2179
Len Brown1df2e552017-01-07 23:24:57 -05002180 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002181
Len Brown1df2e552017-01-07 23:24:57 -05002182 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002183
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002184 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002185 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2186 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2187 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2188 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2189 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002190 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002191 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002192
2193#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2194 if (has_automatic_cstate_conversion) {
2195 fprintf(outf, ", automatic c-state conversion=%s",
2196 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2197 }
2198
2199 fprintf(outf, ")\n");
2200
Len Brownfcd17212015-03-23 20:29:09 -04002201 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002202}
2203
Len Brown6fb31432015-06-17 16:23:45 -04002204static void
2205dump_config_tdp(void)
2206{
2207 unsigned long long msr;
2208
2209 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002210 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002211 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002212
2213 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002214 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002215 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002216 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2217 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2218 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2219 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002220 }
Len Brownb7d8c142016-02-13 23:36:17 -05002221 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002222
2223 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002224 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002225 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002226 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2227 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2228 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2229 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002230 }
Len Brownb7d8c142016-02-13 23:36:17 -05002231 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002232
2233 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002234 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002235 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002236 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2237 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2238 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002239
Len Brown6fb31432015-06-17 16:23:45 -04002240 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002241 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002242 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002243 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2244 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002245}
Len Brown5a634262016-04-06 17:15:55 -04002246
2247unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2248
2249void print_irtl(void)
2250{
2251 unsigned long long msr;
2252
2253 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2254 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2255 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2256 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2257
2258 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2259 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2260 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2261 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2262
2263 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2264 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2265 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2266 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2267
2268 if (!do_irtl_hsw)
2269 return;
2270
2271 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2272 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2273 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2274 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2275
2276 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2277 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2278 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2279 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2280
2281 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2282 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2283 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2284 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2285
2286}
Len Brown36229892016-02-26 20:51:02 -05002287void free_fd_percpu(void)
2288{
2289 int i;
2290
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002291 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002292 if (fd_percpu[i] != 0)
2293 close(fd_percpu[i]);
2294 }
2295
2296 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002297}
2298
Len Brownc98d5d92012-06-04 00:56:40 -04002299void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002300{
Len Brown0e2d8f02018-06-01 22:08:58 -04002301 int i;
2302
Len Brownc98d5d92012-06-04 00:56:40 -04002303 CPU_FREE(cpu_present_set);
2304 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002305 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002306
Len Brownc98d5d92012-06-04 00:56:40 -04002307 CPU_FREE(cpu_affinity_set);
2308 cpu_affinity_set = NULL;
2309 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002310
Len Brownc98d5d92012-06-04 00:56:40 -04002311 free(thread_even);
2312 free(core_even);
2313 free(package_even);
2314
2315 thread_even = NULL;
2316 core_even = NULL;
2317 package_even = NULL;
2318
2319 free(thread_odd);
2320 free(core_odd);
2321 free(package_odd);
2322
2323 thread_odd = NULL;
2324 core_odd = NULL;
2325 package_odd = NULL;
2326
2327 free(output_buffer);
2328 output_buffer = NULL;
2329 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002330
2331 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002332
2333 free(irq_column_2_cpu);
2334 free(irqs_per_cpu);
Len Brown0e2d8f02018-06-01 22:08:58 -04002335
2336 for (i = 0; i <= topo.max_cpu_num; ++i) {
2337 if (cpus[i].put_ids)
2338 CPU_FREE(cpus[i].put_ids);
2339 }
2340 free(cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002341}
2342
Josh Triplett57a42a32013-08-20 17:20:17 -07002343
2344/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002345 * Parse a file containing a single int.
2346 */
2347int parse_int_file(const char *fmt, ...)
2348{
2349 va_list args;
2350 char path[PATH_MAX];
2351 FILE *filep;
2352 int value;
2353
2354 va_start(args, fmt);
2355 vsnprintf(path, sizeof(path), fmt, args);
2356 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002357 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002358 if (fscanf(filep, "%d", &value) != 1)
2359 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002360 fclose(filep);
2361 return value;
2362}
2363
2364/*
Len Brownc98d5d92012-06-04 00:56:40 -04002365 * cpu_is_first_core_in_package(cpu)
2366 * return 1 if given CPU is 1st core in package
2367 */
2368int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002369{
Josh Triplett95aebc42013-08-20 17:20:16 -07002370 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002371}
2372
2373int get_physical_package_id(int cpu)
2374{
Josh Triplett95aebc42013-08-20 17:20:16 -07002375 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002376}
2377
2378int get_core_id(int cpu)
2379{
Josh Triplett95aebc42013-08-20 17:20:16 -07002380 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002381}
2382
Prarit Bhargavaef605742018-06-01 10:04:30 -04002383void set_node_data(void)
2384{
2385 char path[80];
2386 FILE *filep;
2387 int pkg, node, cpu;
2388
2389 struct pkg_node_info {
2390 int count;
2391 int min;
2392 } *pni;
2393
2394 pni = calloc(topo.num_packages, sizeof(struct pkg_node_info));
2395 if (!pni)
2396 err(1, "calloc pkg_node_count");
2397
2398 for (pkg = 0; pkg < topo.num_packages; pkg++)
2399 pni[pkg].min = topo.num_cpus;
2400
2401 for (node = 0; node <= topo.max_node_num; node++) {
2402 /* find the "first" cpu in the node */
2403 sprintf(path, "/sys/bus/node/devices/node%d/cpulist", node);
2404 filep = fopen(path, "r");
2405 if (!filep)
2406 continue;
2407 fscanf(filep, "%d", &cpu);
2408 fclose(filep);
2409
2410 pkg = cpus[cpu].physical_package_id;
2411 pni[pkg].count++;
2412
2413 if (node < pni[pkg].min)
2414 pni[pkg].min = node;
2415 }
2416
2417 for (pkg = 0; pkg < topo.num_packages; pkg++)
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04002418 if (pni[pkg].count > topo.nodes_per_pkg)
2419 topo.nodes_per_pkg = pni[0].count;
Prarit Bhargavaef605742018-06-01 10:04:30 -04002420
2421 for (cpu = 0; cpu < topo.num_cpus; cpu++) {
2422 pkg = cpus[cpu].physical_package_id;
2423 node = cpus[cpu].physical_node_id;
2424 cpus[cpu].logical_node_id = node - pni[pkg].min;
2425 }
2426 free(pni);
2427
2428}
2429
2430int get_physical_node_id(struct cpu_topology *thiscpu)
Len Brownc98d5d92012-06-04 00:56:40 -04002431{
2432 char path[80];
2433 FILE *filep;
Len Brown0e2d8f02018-06-01 22:08:58 -04002434 int i;
2435 int cpu = thiscpu->logical_cpu_id;
Len Brownc98d5d92012-06-04 00:56:40 -04002436
Len Brown0e2d8f02018-06-01 22:08:58 -04002437 for (i = 0; i <= topo.max_cpu_num; i++) {
2438 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2439 cpu, i);
2440 filep = fopen(path, "r");
2441 if (!filep)
2442 continue;
2443 fclose(filep);
2444 return i;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002445 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002446 return -1;
2447}
Len Brownc98d5d92012-06-04 00:56:40 -04002448
Len Brown0e2d8f02018-06-01 22:08:58 -04002449int get_thread_siblings(struct cpu_topology *thiscpu)
2450{
2451 char path[80], character;
2452 FILE *filep;
2453 unsigned long map;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002454 int so, shift, sib_core;
Len Brown0e2d8f02018-06-01 22:08:58 -04002455 int cpu = thiscpu->logical_cpu_id;
2456 int offset = topo.max_cpu_num + 1;
2457 size_t size;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002458 int thread_id = 0;
Len Brown0e2d8f02018-06-01 22:08:58 -04002459
2460 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002461 if (thiscpu->thread_id < 0)
2462 thiscpu->thread_id = thread_id++;
Len Brown0e2d8f02018-06-01 22:08:58 -04002463 if (!thiscpu->put_ids)
2464 return -1;
2465
2466 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2467 CPU_ZERO_S(size, thiscpu->put_ids);
2468
2469 sprintf(path,
2470 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2471 filep = fopen_or_die(path, "r");
2472 do {
2473 offset -= BITMASK_SIZE;
2474 fscanf(filep, "%lx%c", &map, &character);
2475 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2476 if ((map >> shift) & 0x1) {
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002477 so = shift + offset;
2478 sib_core = get_core_id(so);
2479 if (sib_core == thiscpu->physical_core_id) {
2480 CPU_SET_S(so, size, thiscpu->put_ids);
2481 if ((so != cpu) &&
2482 (cpus[so].thread_id < 0))
2483 cpus[so].thread_id =
2484 thread_id++;
2485 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002486 }
2487 }
2488 } while (!strncmp(&character, ",", 1));
Len Brownc98d5d92012-06-04 00:56:40 -04002489 fclose(filep);
Len Brown0e2d8f02018-06-01 22:08:58 -04002490
2491 return CPU_COUNT_S(size, thiscpu->put_ids);
Len Brownc98d5d92012-06-04 00:56:40 -04002492}
2493
Len Brown103a8fe2010-10-22 23:53:03 -04002494/*
Len Brownc98d5d92012-06-04 00:56:40 -04002495 * run func(thread, core, package) in topology order
2496 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002497 */
2498
Len Brownc98d5d92012-06-04 00:56:40 -04002499int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2500 struct pkg_data *, struct thread_data *, struct core_data *,
2501 struct pkg_data *), struct thread_data *thread_base,
2502 struct core_data *core_base, struct pkg_data *pkg_base,
2503 struct thread_data *thread_base2, struct core_data *core_base2,
2504 struct pkg_data *pkg_base2)
2505{
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002506 int retval, pkg_no, node_no, core_no, thread_no;
Len Brownc98d5d92012-06-04 00:56:40 -04002507
2508 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002509 for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
2510 for (core_no = 0; core_no < topo.cores_per_node;
2511 ++core_no) {
2512 for (thread_no = 0; thread_no <
2513 topo.threads_per_core; ++thread_no) {
2514 struct thread_data *t, *t2;
2515 struct core_data *c, *c2;
2516 struct pkg_data *p, *p2;
Len Brownc98d5d92012-06-04 00:56:40 -04002517
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002518 t = GET_THREAD(thread_base, thread_no,
2519 core_no, node_no,
2520 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002521
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002522 if (cpu_is_not_present(t->cpu_id))
2523 continue;
Len Brownc98d5d92012-06-04 00:56:40 -04002524
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002525 t2 = GET_THREAD(thread_base2, thread_no,
2526 core_no, node_no,
2527 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002528
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002529 c = GET_CORE(core_base, core_no,
2530 node_no, pkg_no);
2531 c2 = GET_CORE(core_base2, core_no,
2532 node_no,
2533 pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002534
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002535 p = GET_PKG(pkg_base, pkg_no);
2536 p2 = GET_PKG(pkg_base2, pkg_no);
Len Brownc98d5d92012-06-04 00:56:40 -04002537
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04002538 retval = func(t, c, p, t2, c2, p2);
2539 if (retval)
2540 return retval;
2541 }
Len Brownc98d5d92012-06-04 00:56:40 -04002542 }
2543 }
2544 }
2545 return 0;
2546}
2547
2548/*
2549 * run func(cpu) on every cpu in /proc/stat
2550 * return max_cpu number
2551 */
2552int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002553{
2554 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002555 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002556 int retval;
2557
Josh Triplett57a42a32013-08-20 17:20:17 -07002558 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002559
2560 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002561 if (retval != 0)
2562 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002563
Len Brownc98d5d92012-06-04 00:56:40 -04002564 while (1) {
2565 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 -04002566 if (retval != 1)
2567 break;
2568
Len Brownc98d5d92012-06-04 00:56:40 -04002569 retval = func(cpu_num);
2570 if (retval) {
2571 fclose(fp);
2572 return(retval);
2573 }
Len Brown103a8fe2010-10-22 23:53:03 -04002574 }
2575 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002576 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002577}
2578
2579void re_initialize(void)
2580{
Len Brownc98d5d92012-06-04 00:56:40 -04002581 free_all_buffers();
2582 setup_all_buffers();
2583 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002584}
2585
Prarit Bhargava843c5792018-06-01 10:04:28 -04002586void set_max_cpu_num(void)
2587{
2588 FILE *filep;
2589 unsigned long dummy;
2590
2591 topo.max_cpu_num = 0;
2592 filep = fopen_or_die(
2593 "/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2594 "r");
2595 while (fscanf(filep, "%lx,", &dummy) == 1)
Len Brown0e2d8f02018-06-01 22:08:58 -04002596 topo.max_cpu_num += BITMASK_SIZE;
Prarit Bhargava843c5792018-06-01 10:04:28 -04002597 fclose(filep);
2598 topo.max_cpu_num--; /* 0 based */
2599}
Len Brownc98d5d92012-06-04 00:56:40 -04002600
Len Brown103a8fe2010-10-22 23:53:03 -04002601/*
Len Brownc98d5d92012-06-04 00:56:40 -04002602 * count_cpus()
2603 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002604 */
Len Brownc98d5d92012-06-04 00:56:40 -04002605int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002606{
Prarit Bhargava843c5792018-06-01 10:04:28 -04002607 topo.num_cpus++;
Len Brownc98d5d92012-06-04 00:56:40 -04002608 return 0;
2609}
2610int mark_cpu_present(int cpu)
2611{
2612 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002613 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002614}
2615
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04002616int init_thread_id(int cpu)
2617{
2618 cpus[cpu].thread_id = -1;
2619 return 0;
2620}
2621
Len Brown562a2d32016-02-26 23:48:05 -05002622/*
2623 * snapshot_proc_interrupts()
2624 *
2625 * read and record summary of /proc/interrupts
2626 *
2627 * return 1 if config change requires a restart, else return 0
2628 */
2629int snapshot_proc_interrupts(void)
2630{
2631 static FILE *fp;
2632 int column, retval;
2633
2634 if (fp == NULL)
2635 fp = fopen_or_die("/proc/interrupts", "r");
2636 else
2637 rewind(fp);
2638
2639 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2640 for (column = 0; column < topo.num_cpus; ++column) {
2641 int cpu_number;
2642
2643 retval = fscanf(fp, " CPU%d", &cpu_number);
2644 if (retval != 1)
2645 break;
2646
2647 if (cpu_number > topo.max_cpu_num) {
2648 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2649 return 1;
2650 }
2651
2652 irq_column_2_cpu[column] = cpu_number;
2653 irqs_per_cpu[cpu_number] = 0;
2654 }
2655
2656 /* read /proc/interrupt count lines and sum up irqs per cpu */
2657 while (1) {
2658 int column;
2659 char buf[64];
2660
2661 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2662 if (retval != 1)
2663 break;
2664
2665 /* read the count per cpu */
2666 for (column = 0; column < topo.num_cpus; ++column) {
2667
2668 int cpu_number, irq_count;
2669
2670 retval = fscanf(fp, " %d", &irq_count);
2671 if (retval != 1)
2672 break;
2673
2674 cpu_number = irq_column_2_cpu[column];
2675 irqs_per_cpu[cpu_number] += irq_count;
2676
2677 }
2678
2679 while (getc(fp) != '\n')
2680 ; /* flush interrupt description */
2681
2682 }
2683 return 0;
2684}
Len Brown27d47352016-02-27 00:37:54 -05002685/*
Len Brownfdf676e2016-02-27 01:28:12 -05002686 * snapshot_gfx_rc6_ms()
2687 *
2688 * record snapshot of
2689 * /sys/class/drm/card0/power/rc6_residency_ms
2690 *
2691 * return 1 if config change requires a restart, else return 0
2692 */
2693int snapshot_gfx_rc6_ms(void)
2694{
2695 FILE *fp;
2696 int retval;
2697
2698 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2699
2700 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2701 if (retval != 1)
2702 err(1, "GFX rc6");
2703
2704 fclose(fp);
2705
2706 return 0;
2707}
2708/*
Len Brown27d47352016-02-27 00:37:54 -05002709 * snapshot_gfx_mhz()
2710 *
2711 * record snapshot of
2712 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2713 *
2714 * return 1 if config change requires a restart, else return 0
2715 */
2716int snapshot_gfx_mhz(void)
2717{
2718 static FILE *fp;
2719 int retval;
2720
2721 if (fp == NULL)
2722 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002723 else {
Len Brown27d47352016-02-27 00:37:54 -05002724 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002725 fflush(fp);
2726 }
Len Brown27d47352016-02-27 00:37:54 -05002727
2728 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2729 if (retval != 1)
2730 err(1, "GFX MHz");
2731
2732 return 0;
2733}
Len Brown562a2d32016-02-26 23:48:05 -05002734
2735/*
Len Brownbe0e54c2018-06-01 12:35:53 -04002736 * snapshot_cpu_lpi()
2737 *
2738 * record snapshot of
2739 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2740 *
2741 * return 1 if config change requires a restart, else return 0
2742 */
2743int snapshot_cpu_lpi_us(void)
2744{
2745 FILE *fp;
2746 int retval;
2747
2748 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2749
2750 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2751 if (retval != 1)
2752 err(1, "CPU LPI");
2753
2754 fclose(fp);
2755
2756 return 0;
2757}
2758/*
2759 * snapshot_sys_lpi()
2760 *
2761 * record snapshot of
2762 * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2763 *
2764 * return 1 if config change requires a restart, else return 0
2765 */
2766int snapshot_sys_lpi_us(void)
2767{
2768 FILE *fp;
2769 int retval;
2770
2771 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2772
2773 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2774 if (retval != 1)
2775 err(1, "SYS LPI");
2776
2777 fclose(fp);
2778
2779 return 0;
2780}
2781/*
Len Brown562a2d32016-02-26 23:48:05 -05002782 * snapshot /proc and /sys files
2783 *
2784 * return 1 if configuration restart needed, else return 0
2785 */
2786int snapshot_proc_sysfs_files(void)
2787{
Len Brown218f0e82017-02-14 22:07:52 -05002788 if (DO_BIC(BIC_IRQ))
2789 if (snapshot_proc_interrupts())
2790 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002791
Len Brown812db3f2017-02-10 00:25:41 -05002792 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002793 snapshot_gfx_rc6_ms();
2794
Len Brown812db3f2017-02-10 00:25:41 -05002795 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002796 snapshot_gfx_mhz();
2797
Len Brownbe0e54c2018-06-01 12:35:53 -04002798 if (DO_BIC(BIC_CPU_LPI))
2799 snapshot_cpu_lpi_us();
2800
2801 if (DO_BIC(BIC_SYS_LPI))
2802 snapshot_sys_lpi_us();
2803
Len Brown562a2d32016-02-26 23:48:05 -05002804 return 0;
2805}
2806
Len Brown8aa2ed02017-07-15 14:57:37 -04002807int exit_requested;
2808
2809static void signal_handler (int signal)
2810{
2811 switch (signal) {
2812 case SIGINT:
2813 exit_requested = 1;
2814 if (debug)
2815 fprintf(stderr, " SIGINT\n");
2816 break;
Len Brown07211962017-07-15 15:51:26 -04002817 case SIGUSR1:
2818 if (debug > 1)
2819 fprintf(stderr, "SIGUSR1\n");
2820 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002821 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002822 /* make sure this manually-invoked interval is at least 1ms long */
2823 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002824}
2825
2826void setup_signal_handler(void)
2827{
2828 struct sigaction sa;
2829
2830 memset(&sa, 0, sizeof(sa));
2831
2832 sa.sa_handler = &signal_handler;
2833
2834 if (sigaction(SIGINT, &sa, NULL) < 0)
2835 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002836 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2837 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002838}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002839
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002840void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002841{
2842 struct timeval select_timeout;
2843 fd_set readfds;
2844 int retval;
2845
2846 FD_ZERO(&readfds);
2847 FD_SET(0, &readfds);
2848
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002849 if (!isatty(fileno(stdin))) {
2850 nanosleep(&interval_ts, NULL);
2851 return;
2852 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002853
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002854 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002855 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2856
2857 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002858 switch (getc(stdin)) {
2859 case 'q':
2860 exit_requested = 1;
2861 break;
2862 }
2863 /* make sure this manually-invoked interval is at least 1ms long */
2864 nanosleep(&one_msec, NULL);
2865 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002866}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002867
Len Brown103a8fe2010-10-22 23:53:03 -04002868void turbostat_loop()
2869{
Len Brownc98d5d92012-06-04 00:56:40 -04002870 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002871 int restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002872 int done_iters = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002873
Len Brown8aa2ed02017-07-15 14:57:37 -04002874 setup_signal_handler();
2875
Len Brown103a8fe2010-10-22 23:53:03 -04002876restart:
Len Browne52966c2012-11-08 22:38:05 -05002877 restarted++;
2878
Len Brown562a2d32016-02-26 23:48:05 -05002879 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002880 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002881 if (retval < -1) {
2882 exit(retval);
2883 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002884 if (restarted > 1) {
2885 exit(retval);
2886 }
Len Brownc98d5d92012-06-04 00:56:40 -04002887 re_initialize();
2888 goto restart;
2889 }
Len Browne52966c2012-11-08 22:38:05 -05002890 restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002891 done_iters = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002892 gettimeofday(&tv_even, (struct timezone *)NULL);
2893
2894 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002895 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002896 re_initialize();
2897 goto restart;
2898 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002899 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002900 if (snapshot_proc_sysfs_files())
2901 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002902 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002903 if (retval < -1) {
2904 exit(retval);
2905 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002906 re_initialize();
2907 goto restart;
2908 }
Len Brown103a8fe2010-10-22 23:53:03 -04002909 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002910 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002911 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2912 re_initialize();
2913 goto restart;
2914 }
Len Brownc98d5d92012-06-04 00:56:40 -04002915 compute_average(EVEN_COUNTERS);
2916 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002917 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002918 if (exit_requested)
2919 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002920 if (num_iterations && ++done_iters >= num_iterations)
2921 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002922 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002923 if (snapshot_proc_sysfs_files())
2924 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002925 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002926 if (retval < -1) {
2927 exit(retval);
2928 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002929 re_initialize();
2930 goto restart;
2931 }
Len Brown103a8fe2010-10-22 23:53:03 -04002932 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002933 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002934 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2935 re_initialize();
2936 goto restart;
2937 }
Len Brownc98d5d92012-06-04 00:56:40 -04002938 compute_average(ODD_COUNTERS);
2939 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002940 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002941 if (exit_requested)
2942 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002943 if (num_iterations && ++done_iters >= num_iterations)
2944 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002945 }
2946}
2947
2948void check_dev_msr()
2949{
2950 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002951 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002952
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002953 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2954 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002955 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2956 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002957}
2958
Len Brown98481e72014-08-15 00:36:50 -04002959void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002960{
Len Brown98481e72014-08-15 00:36:50 -04002961 struct __user_cap_header_struct cap_header_data;
2962 cap_user_header_t cap_header = &cap_header_data;
2963 struct __user_cap_data_struct cap_data_data;
2964 cap_user_data_t cap_data = &cap_data_data;
2965 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2966 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002967 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002968
2969 /* check for CAP_SYS_RAWIO */
2970 cap_header->pid = getpid();
2971 cap_header->version = _LINUX_CAPABILITY_VERSION;
2972 if (capget(cap_header, cap_data) < 0)
2973 err(-6, "capget(2) failed");
2974
2975 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2976 do_exit++;
2977 warnx("capget(CAP_SYS_RAWIO) failed,"
2978 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2979 }
2980
2981 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002982 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2983 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002984 do_exit++;
2985 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2986 }
2987
2988 /* if all else fails, thell them to be root */
2989 if (do_exit)
2990 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002991 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002992
2993 if (do_exit)
2994 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002995}
2996
Len Brownd7899442015-01-23 00:12:33 -05002997/*
2998 * NHM adds support for additional MSRs:
2999 *
3000 * MSR_SMI_COUNT 0x00000034
3001 *
Len Brownec0adc52015-11-12 02:42:31 -05003002 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05003003 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05003004 *
Len Browncf4cbe52017-01-01 13:08:33 -05003005 * MSR_MISC_PWR_MGMT 0x000001aa
3006 *
Len Brownd7899442015-01-23 00:12:33 -05003007 * MSR_PKG_C3_RESIDENCY 0x000003f8
3008 * MSR_PKG_C6_RESIDENCY 0x000003f9
3009 * MSR_CORE_C3_RESIDENCY 0x000003fc
3010 * MSR_CORE_C6_RESIDENCY 0x000003fd
3011 *
Len Brownee7e38e2015-02-09 23:39:45 -05003012 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05003013 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05003014 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05003015 */
Len Brownee7e38e2015-02-09 23:39:45 -05003016int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003017{
Len Brownee7e38e2015-02-09 23:39:45 -05003018 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04003019 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05003020 int *pkg_cstate_limits;
3021
Len Brown103a8fe2010-10-22 23:53:03 -04003022 if (!genuine_intel)
3023 return 0;
3024
3025 if (family != 6)
3026 return 0;
3027
Len Brown21ed5572015-10-19 22:37:40 -04003028 bclk = discover_bclk(family, model);
3029
Len Brown103a8fe2010-10-22 23:53:03 -04003030 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003031 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3032 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04003033 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04003034 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
3035 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
3036 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3037 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05003038 pkg_cstate_limits = nhm_pkg_cstate_limits;
3039 break;
Len Brown869ce692016-06-16 23:22:37 -04003040 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3041 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3042 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3043 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05003044 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003045 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003046 break;
Len Brown869ce692016-06-16 23:22:37 -04003047 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3048 case INTEL_FAM6_HASWELL_X: /* HSX */
3049 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3050 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3051 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3052 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3053 case INTEL_FAM6_BROADWELL_X: /* BDX */
3054 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3055 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3056 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3057 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3058 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003059 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brownee7e38e2015-02-09 23:39:45 -05003060 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003061 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003062 break;
Len Brownd8ebb442016-12-01 20:27:46 -05003063 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3064 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003065 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05003066 break;
Len Brown869ce692016-06-16 23:22:37 -04003067 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05003068 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04003069 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05003070 pkg_cstate_limits = slv_pkg_cstate_limits;
3071 break;
Len Brown869ce692016-06-16 23:22:37 -04003072 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05003073 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05003074 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003075 break;
Len Brown869ce692016-06-16 23:22:37 -04003076 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05003077 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05003078 pkg_cstate_limits = phi_pkg_cstate_limits;
3079 break;
Len Brown869ce692016-06-16 23:22:37 -04003080 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003081 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04003082 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04003083 pkg_cstate_limits = bxt_pkg_cstate_limits;
3084 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003085 default:
3086 return 0;
3087 }
Len Brown1df2e552017-01-07 23:24:57 -05003088 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04003089 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05003090
Len Brownec0adc52015-11-12 02:42:31 -05003091 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04003092 base_ratio = (msr >> 8) & 0xFF;
3093
3094 base_hz = base_ratio * bclk * 1000000;
3095 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003096 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04003097}
Len Brown0f7887c2017-01-12 23:49:18 -05003098/*
Len Brown495c76542017-02-08 02:41:51 -05003099 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05003100 *
3101 * MSR_CC6_DEMOTION_POLICY_CONFIG
3102 * MSR_MC6_DEMOTION_POLICY_CONFIG
3103 */
3104
3105int has_slv_msrs(unsigned int family, unsigned int model)
3106{
3107 if (!genuine_intel)
3108 return 0;
3109
3110 switch (model) {
3111 case INTEL_FAM6_ATOM_SILVERMONT1:
3112 case INTEL_FAM6_ATOM_MERRIFIELD:
3113 case INTEL_FAM6_ATOM_MOOREFIELD:
3114 return 1;
3115 }
3116 return 0;
3117}
Len Brown7170a372017-01-27 02:13:27 -05003118int is_dnv(unsigned int family, unsigned int model)
3119{
3120
3121 if (!genuine_intel)
3122 return 0;
3123
3124 switch (model) {
3125 case INTEL_FAM6_ATOM_DENVERTON:
3126 return 1;
3127 }
3128 return 0;
3129}
Len Brownade0eba2017-02-10 01:56:47 -05003130int is_bdx(unsigned int family, unsigned int model)
3131{
3132
3133 if (!genuine_intel)
3134 return 0;
3135
3136 switch (model) {
3137 case INTEL_FAM6_BROADWELL_X:
3138 case INTEL_FAM6_BROADWELL_XEON_D:
3139 return 1;
3140 }
3141 return 0;
3142}
Len Brown34c761972017-01-27 02:36:41 -05003143int is_skx(unsigned int family, unsigned int model)
3144{
3145
3146 if (!genuine_intel)
3147 return 0;
3148
3149 switch (model) {
3150 case INTEL_FAM6_SKYLAKE_X:
3151 return 1;
3152 }
3153 return 0;
3154}
Len Brown0f7887c2017-01-12 23:49:18 -05003155
Len Brown31e07522017-01-31 23:07:49 -05003156int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05003157{
Len Brown0f7887c2017-01-12 23:49:18 -05003158 if (has_slv_msrs(family, model))
3159 return 0;
3160
Len Brownd7899442015-01-23 00:12:33 -05003161 switch (model) {
3162 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04003163 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3164 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3165 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05003166 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05003167 return 0;
3168 default:
3169 return 1;
3170 }
3171}
Len Brown0f7887c2017-01-12 23:49:18 -05003172int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3173{
3174 if (has_slv_msrs(family, model))
3175 return 1;
3176
3177 return 0;
3178}
Len Brown6574a5d2012-09-21 00:01:31 -04003179int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3180{
3181 if (!genuine_intel)
3182 return 0;
3183
3184 if (family != 6)
3185 return 0;
3186
3187 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003188 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3189 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04003190 return 1;
3191 default:
3192 return 0;
3193 }
3194}
Len Brownfcd17212015-03-23 20:29:09 -04003195int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3196{
3197 if (!genuine_intel)
3198 return 0;
3199
3200 if (family != 6)
3201 return 0;
3202
3203 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003204 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003205 return 1;
3206 default:
3207 return 0;
3208 }
3209}
3210
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003211int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3212{
3213 if (!genuine_intel)
3214 return 0;
3215
3216 if (family != 6)
3217 return 0;
3218
3219 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003220 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003221 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003222 return 1;
3223 default:
3224 return 0;
3225 }
3226}
Len Brown31e07522017-01-31 23:07:49 -05003227int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3228{
3229 if (!genuine_intel)
3230 return 0;
3231
3232 if (family != 6)
3233 return 0;
3234
3235 switch (model) {
3236 case INTEL_FAM6_ATOM_GOLDMONT:
3237 case INTEL_FAM6_SKYLAKE_X:
3238 return 1;
3239 default:
3240 return 0;
3241 }
3242}
Len Brown6fb31432015-06-17 16:23:45 -04003243int has_config_tdp(unsigned int family, unsigned int model)
3244{
3245 if (!genuine_intel)
3246 return 0;
3247
3248 if (family != 6)
3249 return 0;
3250
3251 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003252 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3253 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3254 case INTEL_FAM6_HASWELL_X: /* HSX */
3255 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3256 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3257 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3258 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3259 case INTEL_FAM6_BROADWELL_X: /* BDX */
3260 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3261 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3262 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3263 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3264 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003265 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04003266 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003267
Len Brown869ce692016-06-16 23:22:37 -04003268 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003269 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003270 return 1;
3271 default:
3272 return 0;
3273 }
3274}
3275
Len Brownfcd17212015-03-23 20:29:09 -04003276static void
Colin Ian King1b693172016-03-02 13:50:25 +00003277dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003278{
3279 if (!do_nhm_platform_info)
3280 return;
3281
3282 dump_nhm_platform_info();
3283
3284 if (has_hsw_turbo_ratio_limit(family, model))
3285 dump_hsw_turbo_ratio_limits();
3286
3287 if (has_ivt_turbo_ratio_limit(family, model))
3288 dump_ivt_turbo_ratio_limits();
3289
Len Brown31e07522017-01-31 23:07:49 -05003290 if (has_turbo_ratio_limit(family, model))
3291 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003292
Len Brown0f7887c2017-01-12 23:49:18 -05003293 if (has_atom_turbo_ratio_limit(family, model))
3294 dump_atom_turbo_ratio_limits();
3295
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003296 if (has_knl_turbo_ratio_limit(family, model))
3297 dump_knl_turbo_ratio_limits();
3298
Len Brown6fb31432015-06-17 16:23:45 -04003299 if (has_config_tdp(family, model))
3300 dump_config_tdp();
3301
Len Brownfcd17212015-03-23 20:29:09 -04003302 dump_nhm_cst_cfg();
3303}
3304
Len Brown41618e62017-02-09 18:25:22 -05003305static void
3306dump_sysfs_cstate_config(void)
3307{
3308 char path[64];
3309 char name_buf[16];
3310 char desc[64];
3311 FILE *input;
3312 int state;
3313 char *sp;
3314
3315 if (!DO_BIC(BIC_sysfs))
3316 return;
3317
3318 for (state = 0; state < 10; ++state) {
3319
3320 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3321 base_cpu, state);
3322 input = fopen(path, "r");
3323 if (input == NULL)
3324 continue;
3325 fgets(name_buf, sizeof(name_buf), input);
3326
3327 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3328 sp = strchr(name_buf, '-');
3329 if (!sp)
3330 sp = strchrnul(name_buf, '\n');
3331 *sp = '\0';
3332
3333 fclose(input);
3334
3335 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3336 base_cpu, state);
3337 input = fopen(path, "r");
3338 if (input == NULL)
3339 continue;
3340 fgets(desc, sizeof(desc), input);
3341
3342 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3343 fclose(input);
3344 }
3345}
Len Brown7293fcc2017-02-22 00:11:12 -05003346static void
3347dump_sysfs_pstate_config(void)
3348{
3349 char path[64];
3350 char driver_buf[64];
3351 char governor_buf[64];
3352 FILE *input;
3353 int turbo;
3354
3355 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3356 base_cpu);
3357 input = fopen(path, "r");
3358 if (input == NULL) {
3359 fprintf(stderr, "NSFOD %s\n", path);
3360 return;
3361 }
3362 fgets(driver_buf, sizeof(driver_buf), input);
3363 fclose(input);
3364
3365 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3366 base_cpu);
3367 input = fopen(path, "r");
3368 if (input == NULL) {
3369 fprintf(stderr, "NSFOD %s\n", path);
3370 return;
3371 }
3372 fgets(governor_buf, sizeof(governor_buf), input);
3373 fclose(input);
3374
3375 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3376 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3377
3378 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3379 input = fopen(path, "r");
3380 if (input != NULL) {
3381 fscanf(input, "%d", &turbo);
3382 fprintf(outf, "cpufreq boost: %d\n", turbo);
3383 fclose(input);
3384 }
3385
3386 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3387 input = fopen(path, "r");
3388 if (input != NULL) {
3389 fscanf(input, "%d", &turbo);
3390 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3391 fclose(input);
3392 }
3393}
Len Brown41618e62017-02-09 18:25:22 -05003394
Len Brown6574a5d2012-09-21 00:01:31 -04003395
Len Brown889facb2012-11-08 00:48:57 -05003396/*
3397 * print_epb()
3398 * Decode the ENERGY_PERF_BIAS MSR
3399 */
3400int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3401{
3402 unsigned long long msr;
3403 char *epb_string;
3404 int cpu;
3405
3406 if (!has_epb)
3407 return 0;
3408
3409 cpu = t->cpu_id;
3410
3411 /* EPB is per-package */
3412 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3413 return 0;
3414
3415 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003416 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003417 return -1;
3418 }
3419
3420 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3421 return 0;
3422
Len Browne9be7dd2015-05-26 12:19:37 -04003423 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003424 case ENERGY_PERF_BIAS_PERFORMANCE:
3425 epb_string = "performance";
3426 break;
3427 case ENERGY_PERF_BIAS_NORMAL:
3428 epb_string = "balanced";
3429 break;
3430 case ENERGY_PERF_BIAS_POWERSAVE:
3431 epb_string = "powersave";
3432 break;
3433 default:
3434 epb_string = "custom";
3435 break;
3436 }
Len Brownb7d8c142016-02-13 23:36:17 -05003437 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003438
3439 return 0;
3440}
Len Brown7f5c2582015-12-01 01:36:39 -05003441/*
3442 * print_hwp()
3443 * Decode the MSR_HWP_CAPABILITIES
3444 */
3445int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3446{
3447 unsigned long long msr;
3448 int cpu;
3449
3450 if (!has_hwp)
3451 return 0;
3452
3453 cpu = t->cpu_id;
3454
3455 /* MSR_HWP_CAPABILITIES is per-package */
3456 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3457 return 0;
3458
3459 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003460 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003461 return -1;
3462 }
3463
3464 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3465 return 0;
3466
Len Brownb7d8c142016-02-13 23:36:17 -05003467 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003468 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3469
3470 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3471 if ((msr & (1 << 0)) == 0)
3472 return 0;
3473
3474 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3475 return 0;
3476
Len Brownb7d8c142016-02-13 23:36:17 -05003477 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003478 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003479 cpu, msr,
3480 (unsigned int)HWP_HIGHEST_PERF(msr),
3481 (unsigned int)HWP_GUARANTEED_PERF(msr),
3482 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3483 (unsigned int)HWP_LOWEST_PERF(msr));
3484
3485 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3486 return 0;
3487
Len Brownb7d8c142016-02-13 23:36:17 -05003488 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003489 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003490 cpu, msr,
3491 (unsigned int)(((msr) >> 0) & 0xff),
3492 (unsigned int)(((msr) >> 8) & 0xff),
3493 (unsigned int)(((msr) >> 16) & 0xff),
3494 (unsigned int)(((msr) >> 24) & 0xff),
3495 (unsigned int)(((msr) >> 32) & 0xff3),
3496 (unsigned int)(((msr) >> 42) & 0x1));
3497
3498 if (has_hwp_pkg) {
3499 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3500 return 0;
3501
Len Brownb7d8c142016-02-13 23:36:17 -05003502 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003503 "(min %d max %d des %d epp 0x%x window 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 }
3511 if (has_hwp_notify) {
3512 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3513 return 0;
3514
Len Brownb7d8c142016-02-13 23:36:17 -05003515 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003516 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3517 cpu, msr,
3518 ((msr) & 0x1) ? "EN" : "Dis",
3519 ((msr) & 0x2) ? "EN" : "Dis");
3520 }
3521 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3522 return 0;
3523
Len Brownb7d8c142016-02-13 23:36:17 -05003524 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003525 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3526 cpu, msr,
3527 ((msr) & 0x1) ? "" : "No-",
3528 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003529
3530 return 0;
3531}
3532
Len Brown3a9a9412014-08-15 02:39:52 -04003533/*
3534 * print_perf_limit()
3535 */
3536int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3537{
3538 unsigned long long msr;
3539 int cpu;
3540
3541 cpu = t->cpu_id;
3542
3543 /* per-package */
3544 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3545 return 0;
3546
3547 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003548 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003549 return -1;
3550 }
3551
3552 if (do_core_perf_limit_reasons) {
3553 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003554 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3555 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003556 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003557 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003558 (msr & 1 << 13) ? "Transitions, " : "",
3559 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3560 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3561 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3562 (msr & 1 << 9) ? "CorePwr, " : "",
3563 (msr & 1 << 8) ? "Amps, " : "",
3564 (msr & 1 << 6) ? "VR-Therm, " : "",
3565 (msr & 1 << 5) ? "Auto-HWP, " : "",
3566 (msr & 1 << 4) ? "Graphics, " : "",
3567 (msr & 1 << 2) ? "bit2, " : "",
3568 (msr & 1 << 1) ? "ThermStatus, " : "",
3569 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003570 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 -04003571 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003572 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003573 (msr & 1 << 29) ? "Transitions, " : "",
3574 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3575 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3576 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3577 (msr & 1 << 25) ? "CorePwr, " : "",
3578 (msr & 1 << 24) ? "Amps, " : "",
3579 (msr & 1 << 22) ? "VR-Therm, " : "",
3580 (msr & 1 << 21) ? "Auto-HWP, " : "",
3581 (msr & 1 << 20) ? "Graphics, " : "",
3582 (msr & 1 << 18) ? "bit18, " : "",
3583 (msr & 1 << 17) ? "ThermStatus, " : "",
3584 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003585
3586 }
3587 if (do_gfx_perf_limit_reasons) {
3588 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003589 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3590 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003591 (msr & 1 << 0) ? "PROCHOT, " : "",
3592 (msr & 1 << 1) ? "ThermStatus, " : "",
3593 (msr & 1 << 4) ? "Graphics, " : "",
3594 (msr & 1 << 6) ? "VR-Therm, " : "",
3595 (msr & 1 << 8) ? "Amps, " : "",
3596 (msr & 1 << 9) ? "GFXPwr, " : "",
3597 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3598 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003599 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003600 (msr & 1 << 16) ? "PROCHOT, " : "",
3601 (msr & 1 << 17) ? "ThermStatus, " : "",
3602 (msr & 1 << 20) ? "Graphics, " : "",
3603 (msr & 1 << 22) ? "VR-Therm, " : "",
3604 (msr & 1 << 24) ? "Amps, " : "",
3605 (msr & 1 << 25) ? "GFXPwr, " : "",
3606 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3607 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3608 }
3609 if (do_ring_perf_limit_reasons) {
3610 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003611 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3612 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003613 (msr & 1 << 0) ? "PROCHOT, " : "",
3614 (msr & 1 << 1) ? "ThermStatus, " : "",
3615 (msr & 1 << 6) ? "VR-Therm, " : "",
3616 (msr & 1 << 8) ? "Amps, " : "",
3617 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3618 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003619 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003620 (msr & 1 << 16) ? "PROCHOT, " : "",
3621 (msr & 1 << 17) ? "ThermStatus, " : "",
3622 (msr & 1 << 22) ? "VR-Therm, " : "",
3623 (msr & 1 << 24) ? "Amps, " : "",
3624 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3625 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3626 }
3627 return 0;
3628}
3629
Len Brown889facb2012-11-08 00:48:57 -05003630#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3631#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3632
Colin Ian King1b693172016-03-02 13:50:25 +00003633double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003634{
3635 unsigned long long msr;
3636
3637 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003638 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003639 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3640
3641 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003642 case INTEL_FAM6_ATOM_SILVERMONT1:
3643 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003644 return 30.0;
3645 default:
3646 return 135.0;
3647 }
3648}
3649
Andrey Semin40ee8e32014-12-05 00:07:00 -05003650/*
3651 * rapl_dram_energy_units_probe()
3652 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3653 */
3654static double
3655rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3656{
3657 /* only called for genuine_intel, family 6 */
3658
3659 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003660 case INTEL_FAM6_HASWELL_X: /* HSX */
3661 case INTEL_FAM6_BROADWELL_X: /* BDX */
3662 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3663 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003664 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003665 return (rapl_dram_energy_units = 15.3 / 1000000);
3666 default:
3667 return (rapl_energy_units);
3668 }
3669}
3670
Len Brown144b44b2013-11-09 00:30:16 -05003671
Len Brown889facb2012-11-08 00:48:57 -05003672/*
3673 * rapl_probe()
3674 *
Len Brown144b44b2013-11-09 00:30:16 -05003675 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003676 */
3677void rapl_probe(unsigned int family, unsigned int model)
3678{
3679 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003680 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003681 double tdp;
3682
3683 if (!genuine_intel)
3684 return;
3685
3686 if (family != 6)
3687 return;
3688
3689 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003690 case INTEL_FAM6_SANDYBRIDGE:
3691 case INTEL_FAM6_IVYBRIDGE:
3692 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3693 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3694 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3695 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3696 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003697 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003698 if (rapl_joules) {
3699 BIC_PRESENT(BIC_Pkg_J);
3700 BIC_PRESENT(BIC_Cor_J);
3701 BIC_PRESENT(BIC_GFX_J);
3702 } else {
3703 BIC_PRESENT(BIC_PkgWatt);
3704 BIC_PRESENT(BIC_CorWatt);
3705 BIC_PRESENT(BIC_GFXWatt);
3706 }
Len Brown889facb2012-11-08 00:48:57 -05003707 break;
Len Brown869ce692016-06-16 23:22:37 -04003708 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003709 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003710 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003711 if (rapl_joules)
3712 BIC_PRESENT(BIC_Pkg_J);
3713 else
3714 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003715 break;
Len Brown869ce692016-06-16 23:22:37 -04003716 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3717 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3718 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3719 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003720 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown81824922017-03-04 17:23:07 -05003721 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 -05003722 BIC_PRESENT(BIC_PKG__);
3723 BIC_PRESENT(BIC_RAM__);
3724 if (rapl_joules) {
3725 BIC_PRESENT(BIC_Pkg_J);
3726 BIC_PRESENT(BIC_Cor_J);
3727 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003728 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003729 } else {
3730 BIC_PRESENT(BIC_PkgWatt);
3731 BIC_PRESENT(BIC_CorWatt);
3732 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003733 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003734 }
Len Brown0b2bb692015-03-26 00:50:30 -04003735 break;
Len Brown869ce692016-06-16 23:22:37 -04003736 case INTEL_FAM6_HASWELL_X: /* HSX */
3737 case INTEL_FAM6_BROADWELL_X: /* BDX */
3738 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3739 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3740 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003741 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003742 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 -05003743 BIC_PRESENT(BIC_PKG__);
3744 BIC_PRESENT(BIC_RAM__);
3745 if (rapl_joules) {
3746 BIC_PRESENT(BIC_Pkg_J);
3747 BIC_PRESENT(BIC_RAM_J);
3748 } else {
3749 BIC_PRESENT(BIC_PkgWatt);
3750 BIC_PRESENT(BIC_RAMWatt);
3751 }
Len Browne6f9bb32013-12-03 02:19:19 -05003752 break;
Len Brown869ce692016-06-16 23:22:37 -04003753 case INTEL_FAM6_SANDYBRIDGE_X:
3754 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003755 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 -05003756 BIC_PRESENT(BIC_PKG__);
3757 BIC_PRESENT(BIC_RAM__);
3758 if (rapl_joules) {
3759 BIC_PRESENT(BIC_Pkg_J);
3760 BIC_PRESENT(BIC_Cor_J);
3761 BIC_PRESENT(BIC_RAM_J);
3762 } else {
3763 BIC_PRESENT(BIC_PkgWatt);
3764 BIC_PRESENT(BIC_CorWatt);
3765 BIC_PRESENT(BIC_RAMWatt);
3766 }
Len Brown144b44b2013-11-09 00:30:16 -05003767 break;
Len Brown869ce692016-06-16 23:22:37 -04003768 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3769 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003770 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003771 if (rapl_joules) {
3772 BIC_PRESENT(BIC_Pkg_J);
3773 BIC_PRESENT(BIC_Cor_J);
3774 } else {
3775 BIC_PRESENT(BIC_PkgWatt);
3776 BIC_PRESENT(BIC_CorWatt);
3777 }
Len Brown889facb2012-11-08 00:48:57 -05003778 break;
Len Brown869ce692016-06-16 23:22:37 -04003779 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003780 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 -05003781 BIC_PRESENT(BIC_PKG__);
3782 BIC_PRESENT(BIC_RAM__);
3783 if (rapl_joules) {
3784 BIC_PRESENT(BIC_Pkg_J);
3785 BIC_PRESENT(BIC_Cor_J);
3786 BIC_PRESENT(BIC_RAM_J);
3787 } else {
3788 BIC_PRESENT(BIC_PkgWatt);
3789 BIC_PRESENT(BIC_CorWatt);
3790 BIC_PRESENT(BIC_RAMWatt);
3791 }
Jacob Pan0f644902016-06-16 09:48:22 -07003792 break;
Len Brown889facb2012-11-08 00:48:57 -05003793 default:
3794 return;
3795 }
3796
3797 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003798 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003799 return;
3800
3801 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003802 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003803 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3804 else
3805 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003806
Andrey Semin40ee8e32014-12-05 00:07:00 -05003807 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3808
Len Brown144b44b2013-11-09 00:30:16 -05003809 time_unit = msr >> 16 & 0xF;
3810 if (time_unit == 0)
3811 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003812
Len Brown144b44b2013-11-09 00:30:16 -05003813 rapl_time_units = 1.0 / (1 << (time_unit));
3814
3815 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003816
3817 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003818 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003819 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003820
3821 return;
3822}
3823
Colin Ian King1b693172016-03-02 13:50:25 +00003824void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003825{
3826 if (!genuine_intel)
3827 return;
3828
3829 if (family != 6)
3830 return;
3831
3832 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003833 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3834 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3835 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003836 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003837 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003838 do_core_perf_limit_reasons = 1;
3839 do_ring_perf_limit_reasons = 1;
3840 default:
3841 return;
3842 }
3843}
3844
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003845void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3846{
3847 if (is_skx(family, model) || is_bdx(family, model))
3848 has_automatic_cstate_conversion = 1;
3849}
3850
Len Brown889facb2012-11-08 00:48:57 -05003851int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3852{
3853 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003854 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003855 int cpu;
3856
3857 if (!(do_dts || do_ptm))
3858 return 0;
3859
3860 cpu = t->cpu_id;
3861
3862 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003863 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003864 return 0;
3865
3866 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003867 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003868 return -1;
3869 }
3870
3871 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3872 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3873 return 0;
3874
3875 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003876 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003877 cpu, msr, tcc_activation_temp - dts);
3878
Len Brown889facb2012-11-08 00:48:57 -05003879 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3880 return 0;
3881
3882 dts = (msr >> 16) & 0x7F;
3883 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003884 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003885 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003886 }
3887
3888
Len Brownf4896fa52017-03-04 18:10:45 -05003889 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003890 unsigned int resolution;
3891
3892 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3893 return 0;
3894
3895 dts = (msr >> 16) & 0x7F;
3896 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003897 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003898 cpu, msr, tcc_activation_temp - dts, resolution);
3899
Len Brown889facb2012-11-08 00:48:57 -05003900 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3901 return 0;
3902
3903 dts = (msr >> 16) & 0x7F;
3904 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003905 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003906 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003907 }
3908
3909 return 0;
3910}
Len Brown36229892016-02-26 20:51:02 -05003911
Len Brown889facb2012-11-08 00:48:57 -05003912void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3913{
Len Brownb7d8c142016-02-13 23:36:17 -05003914 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003915 cpu, label,
3916 ((msr >> 15) & 1) ? "EN" : "DIS",
3917 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3918 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3919 (((msr >> 16) & 1) ? "EN" : "DIS"));
3920
3921 return;
3922}
3923
3924int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3925{
3926 unsigned long long msr;
3927 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003928
3929 if (!do_rapl)
3930 return 0;
3931
3932 /* RAPL counters are per package, so print only for 1st thread/package */
3933 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3934 return 0;
3935
3936 cpu = t->cpu_id;
3937 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003938 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003939 return -1;
3940 }
3941
3942 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3943 return -1;
3944
Len Brown96e47152017-01-21 02:26:00 -05003945 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3946 rapl_power_units, rapl_energy_units, rapl_time_units);
3947
Len Brown144b44b2013-11-09 00:30:16 -05003948 if (do_rapl & RAPL_PKG_POWER_INFO) {
3949
Len Brown889facb2012-11-08 00:48:57 -05003950 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3951 return -5;
3952
3953
Len Brownb7d8c142016-02-13 23:36:17 -05003954 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 -05003955 cpu, msr,
3956 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3957 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3958 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3959 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3960
Len Brown144b44b2013-11-09 00:30:16 -05003961 }
3962 if (do_rapl & RAPL_PKG) {
3963
Len Brown889facb2012-11-08 00:48:57 -05003964 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3965 return -9;
3966
Len Brownb7d8c142016-02-13 23:36:17 -05003967 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003968 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003969
3970 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003971 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003972 cpu,
3973 ((msr >> 47) & 1) ? "EN" : "DIS",
3974 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3975 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3976 ((msr >> 48) & 1) ? "EN" : "DIS");
3977 }
3978
Len Brown0b2bb692015-03-26 00:50:30 -04003979 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003980 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3981 return -6;
3982
Len Brownb7d8c142016-02-13 23:36:17 -05003983 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 -05003984 cpu, msr,
3985 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3986 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3987 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3988 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003989 }
3990 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003991 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3992 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003993 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003994 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003995
3996 print_power_limit_msr(cpu, msr, "DRAM Limit");
3997 }
Len Brown144b44b2013-11-09 00:30:16 -05003998 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003999 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4000 return -7;
Len Brown889facb2012-11-08 00:48:57 -05004001
Len Brown96e47152017-01-21 02:26:00 -05004002 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05004003 }
Jacob Pan91484942016-06-16 09:48:20 -07004004 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05004005 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4006 return -9;
4007 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4008 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4009 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05004010 }
4011 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05004012 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4013 return -8;
Len Brown889facb2012-11-08 00:48:57 -05004014
Len Brown96e47152017-01-21 02:26:00 -05004015 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05004016
Len Brown96e47152017-01-21 02:26:00 -05004017 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4018 return -9;
4019 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4020 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4021 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05004022 }
4023 return 0;
4024}
4025
Len Brownd7899442015-01-23 00:12:33 -05004026/*
4027 * SNB adds support for additional MSRs:
4028 *
4029 * MSR_PKG_C7_RESIDENCY 0x000003fa
4030 * MSR_CORE_C7_RESIDENCY 0x000003fe
4031 * MSR_PKG_C2_RESIDENCY 0x0000060d
4032 */
Len Brown103a8fe2010-10-22 23:53:03 -04004033
Len Brownd7899442015-01-23 00:12:33 -05004034int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04004035{
4036 if (!genuine_intel)
4037 return 0;
4038
4039 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004040 case INTEL_FAM6_SANDYBRIDGE:
4041 case INTEL_FAM6_SANDYBRIDGE_X:
4042 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4043 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4044 case INTEL_FAM6_HASWELL_CORE: /* HSW */
4045 case INTEL_FAM6_HASWELL_X: /* HSW */
4046 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4047 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
4048 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4049 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
4050 case INTEL_FAM6_BROADWELL_X: /* BDX */
4051 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
4052 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4053 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4054 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4055 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004056 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04004057 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4058 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004059 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08004060 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04004061 return 1;
4062 }
4063 return 0;
4064}
4065
Len Brownd7899442015-01-23 00:12:33 -05004066/*
4067 * HSW adds support for additional MSRs:
4068 *
Len Brown5a634262016-04-06 17:15:55 -04004069 * MSR_PKG_C8_RESIDENCY 0x00000630
4070 * MSR_PKG_C9_RESIDENCY 0x00000631
4071 * MSR_PKG_C10_RESIDENCY 0x00000632
4072 *
4073 * MSR_PKGC8_IRTL 0x00000633
4074 * MSR_PKGC9_IRTL 0x00000634
4075 * MSR_PKGC10_IRTL 0x00000635
4076 *
Len Brownd7899442015-01-23 00:12:33 -05004077 */
4078int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004079{
4080 if (!genuine_intel)
4081 return 0;
4082
4083 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004084 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4085 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4086 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4087 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4088 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4089 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004090 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04004091 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004092 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004093 return 1;
4094 }
4095 return 0;
4096}
4097
Len Brown0b2bb692015-03-26 00:50:30 -04004098/*
4099 * SKL adds support for additional MSRS:
4100 *
4101 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4102 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4103 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4104 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4105 */
4106int has_skl_msrs(unsigned int family, unsigned int model)
4107{
4108 if (!genuine_intel)
4109 return 0;
4110
4111 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004112 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4113 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4114 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4115 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004116 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown0b2bb692015-03-26 00:50:30 -04004117 return 1;
4118 }
4119 return 0;
4120}
4121
Len Brown144b44b2013-11-09 00:30:16 -05004122int is_slm(unsigned int family, unsigned int model)
4123{
4124 if (!genuine_intel)
4125 return 0;
4126 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004127 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
4128 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05004129 return 1;
4130 }
4131 return 0;
4132}
4133
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004134int is_knl(unsigned int family, unsigned int model)
4135{
4136 if (!genuine_intel)
4137 return 0;
4138 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004139 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05004140 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004141 return 1;
4142 }
4143 return 0;
4144}
4145
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004146int is_cnl(unsigned int family, unsigned int model)
4147{
4148 if (!genuine_intel)
4149 return 0;
4150
4151 switch (model) {
4152 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4153 return 1;
4154 }
4155
4156 return 0;
4157}
4158
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004159unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4160{
4161 if (is_knl(family, model))
4162 return 1024;
4163 return 1;
4164}
4165
Len Brown144b44b2013-11-09 00:30:16 -05004166#define SLM_BCLK_FREQS 5
4167double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4168
4169double slm_bclk(void)
4170{
4171 unsigned long long msr = 3;
4172 unsigned int i;
4173 double freq;
4174
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004175 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05004176 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05004177
4178 i = msr & 0xf;
4179 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05004180 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01004181 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05004182 }
4183 freq = slm_freq_table[i];
4184
Len Brown96e47152017-01-21 02:26:00 -05004185 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05004186 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05004187
4188 return freq;
4189}
4190
Len Brown103a8fe2010-10-22 23:53:03 -04004191double discover_bclk(unsigned int family, unsigned int model)
4192{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01004193 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04004194 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05004195 else if (is_slm(family, model))
4196 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04004197 else
4198 return 133.33;
4199}
4200
Len Brown889facb2012-11-08 00:48:57 -05004201/*
4202 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4203 * the Thermal Control Circuit (TCC) activates.
4204 * This is usually equal to tjMax.
4205 *
4206 * Older processors do not have this MSR, so there we guess,
4207 * but also allow cmdline over-ride with -T.
4208 *
4209 * Several MSR temperature values are in units of degrees-C
4210 * below this value, including the Digital Thermal Sensor (DTS),
4211 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4212 */
4213int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4214{
4215 unsigned long long msr;
4216 unsigned int target_c_local;
4217 int cpu;
4218
4219 /* tcc_activation_temp is used only for dts or ptm */
4220 if (!(do_dts || do_ptm))
4221 return 0;
4222
4223 /* this is a per-package concept */
4224 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4225 return 0;
4226
4227 cpu = t->cpu_id;
4228 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004229 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004230 return -1;
4231 }
4232
4233 if (tcc_activation_temp_override != 0) {
4234 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004235 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004236 cpu, tcc_activation_temp);
4237 return 0;
4238 }
4239
4240 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004241 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004242 goto guess;
4243
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004244 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004245 goto guess;
4246
Jean Delvare34821242014-05-01 11:40:19 +02004247 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004248
Len Brown96e47152017-01-21 02:26:00 -05004249 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004250 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004251 cpu, msr, target_c_local);
4252
Jean Delvare34821242014-05-01 11:40:19 +02004253 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004254 goto guess;
4255
4256 tcc_activation_temp = target_c_local;
4257
4258 return 0;
4259
4260guess:
4261 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004262 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004263 cpu, tcc_activation_temp);
4264
4265 return 0;
4266}
Len Brown69807a62015-11-21 12:22:47 -05004267
Len Brownaa8d8cc2016-03-11 13:26:03 -05004268void decode_feature_control_msr(void)
4269{
4270 unsigned long long msr;
4271
4272 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4273 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4274 base_cpu, msr,
4275 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4276 msr & (1 << 18) ? "SGX" : "");
4277}
4278
Len Brown69807a62015-11-21 12:22:47 -05004279void decode_misc_enable_msr(void)
4280{
4281 unsigned long long msr;
4282
Len Brownf26b1512017-06-23 20:45:54 -07004283 if (!genuine_intel)
4284 return;
4285
Len Brown69807a62015-11-21 12:22:47 -05004286 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004287 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004288 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004289 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4290 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004291 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004292 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4293 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004294}
4295
Len Brown33148d62017-01-21 01:26:16 -05004296void decode_misc_feature_control(void)
4297{
4298 unsigned long long msr;
4299
4300 if (!has_misc_feature_control)
4301 return;
4302
4303 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4304 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4305 base_cpu, msr,
4306 msr & (0 << 0) ? "No-" : "",
4307 msr & (1 << 0) ? "No-" : "",
4308 msr & (2 << 0) ? "No-" : "",
4309 msr & (3 << 0) ? "No-" : "");
4310}
Len Brownf0057312015-12-03 01:35:36 -05004311/*
4312 * Decode MSR_MISC_PWR_MGMT
4313 *
4314 * Decode the bits according to the Nehalem documentation
4315 * bit[0] seems to continue to have same meaning going forward
4316 * bit[1] less so...
4317 */
4318void decode_misc_pwr_mgmt_msr(void)
4319{
4320 unsigned long long msr;
4321
4322 if (!do_nhm_platform_info)
4323 return;
4324
Len Browncf4cbe52017-01-01 13:08:33 -05004325 if (no_MSR_MISC_PWR_MGMT)
4326 return;
4327
Len Brownf0057312015-12-03 01:35:36 -05004328 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004329 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 -05004330 base_cpu, msr,
4331 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004332 msr & (1 << 1) ? "EN" : "DIS",
4333 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004334}
Len Brown71616c82017-01-07 22:37:48 -05004335/*
4336 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4337 *
4338 * This MSRs are present on Silvermont processors,
4339 * Intel Atom processor E3000 series (Baytrail), and friends.
4340 */
4341void decode_c6_demotion_policy_msr(void)
4342{
4343 unsigned long long msr;
4344
4345 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4346 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4347 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4348
4349 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4350 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4351 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4352}
Len Brown7f5c2582015-12-01 01:36:39 -05004353
Len Brownfcd17212015-03-23 20:29:09 -04004354void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004355{
Len Brown61a87ba2015-11-23 02:30:51 -05004356 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004357 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004358 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004359
4360 eax = ebx = ecx = edx = 0;
4361
Len Brown5aea2f72016-03-13 03:14:35 -04004362 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004363
4364 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4365 genuine_intel = 1;
4366
Len Brown96e47152017-01-21 02:26:00 -05004367 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004368 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004369 (char *)&ebx, (char *)&edx, (char *)&ecx);
4370
Len Brown5aea2f72016-03-13 03:14:35 -04004371 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004372 family = (fms >> 8) & 0xf;
4373 model = (fms >> 4) & 0xf;
4374 stepping = fms & 0xf;
4375 if (family == 6 || family == 0xf)
4376 model += ((fms >> 16) & 0xf) << 4;
4377
Len Brown96e47152017-01-21 02:26:00 -05004378 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004379 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004380 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004381 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004382 ecx & (1 << 0) ? "SSE3" : "-",
4383 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004384 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004385 ecx & (1 << 7) ? "EIST" : "-",
4386 ecx & (1 << 8) ? "TM2" : "-",
4387 edx & (1 << 4) ? "TSC" : "-",
4388 edx & (1 << 5) ? "MSR" : "-",
4389 edx & (1 << 22) ? "ACPI-TM" : "-",
4390 edx & (1 << 29) ? "TM" : "-");
4391 }
Len Brown103a8fe2010-10-22 23:53:03 -04004392
Josh Triplettb2c95d92013-08-20 17:20:18 -07004393 if (!(edx & (1 << 5)))
4394 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004395
4396 /*
4397 * check max extended function levels of CPUID.
4398 * This is needed to check for invariant TSC.
4399 * This check is valid for both Intel and AMD.
4400 */
4401 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004402 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004403
Len Brown61a87ba2015-11-23 02:30:51 -05004404 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004405
Len Brownd7899442015-01-23 00:12:33 -05004406 /*
4407 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4408 * this check is valid for both Intel and AMD
4409 */
Len Brown5aea2f72016-03-13 03:14:35 -04004410 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004411 has_invariant_tsc = edx & (1 << 8);
4412 }
Len Brown103a8fe2010-10-22 23:53:03 -04004413
4414 /*
4415 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4416 * this check is valid for both Intel and AMD
4417 */
4418
Len Brown5aea2f72016-03-13 03:14:35 -04004419 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004420 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004421 if (has_aperf) {
4422 BIC_PRESENT(BIC_Avg_MHz);
4423 BIC_PRESENT(BIC_Busy);
4424 BIC_PRESENT(BIC_Bzy_MHz);
4425 }
Len Brown889facb2012-11-08 00:48:57 -05004426 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004427 if (do_dts)
4428 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004429 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004430 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004431 if (do_ptm)
4432 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004433 has_hwp = eax & (1 << 7);
4434 has_hwp_notify = eax & (1 << 8);
4435 has_hwp_activity_window = eax & (1 << 9);
4436 has_hwp_epp = eax & (1 << 10);
4437 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004438 has_epb = ecx & (1 << 3);
4439
Len Brown96e47152017-01-21 02:26:00 -05004440 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004441 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004442 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4443 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004444 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004445 do_dts ? "" : "No-",
4446 do_ptm ? "" : "No-",
4447 has_hwp ? "" : "No-",
4448 has_hwp_notify ? "" : "No-",
4449 has_hwp_activity_window ? "" : "No-",
4450 has_hwp_epp ? "" : "No-",
4451 has_hwp_pkg ? "" : "No-",
4452 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004453
Len Brown96e47152017-01-21 02:26:00 -05004454 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004455 decode_misc_enable_msr();
4456
Len Brown33148d62017-01-21 01:26:16 -05004457
Len Brown96e47152017-01-21 02:26:00 -05004458 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004459 int has_sgx;
4460
4461 ecx = 0;
4462
4463 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4464
4465 has_sgx = ebx & (1 << 2);
4466 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4467
4468 if (has_sgx)
4469 decode_feature_control_msr();
4470 }
4471
Len Brown61a87ba2015-11-23 02:30:51 -05004472 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004473 unsigned int eax_crystal;
4474 unsigned int ebx_tsc;
4475
4476 /*
4477 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4478 */
4479 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004480 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004481
4482 if (ebx_tsc != 0) {
4483
Len Brown96e47152017-01-21 02:26:00 -05004484 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004485 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004486 eax_crystal, ebx_tsc, crystal_hz);
4487
4488 if (crystal_hz == 0)
4489 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004490 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4491 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4492 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4493 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004494 crystal_hz = 24000000; /* 24.0 MHz */
4495 break;
Len Brown7268d402016-12-01 23:10:39 -05004496 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004497 crystal_hz = 25000000; /* 25.0 MHz */
4498 break;
Len Brown869ce692016-06-16 23:22:37 -04004499 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004500 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004501 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004502 break;
4503 default:
4504 crystal_hz = 0;
4505 }
4506
4507 if (crystal_hz) {
4508 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004509 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004510 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004511 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4512 }
4513 }
4514 }
Len Brown61a87ba2015-11-23 02:30:51 -05004515 if (max_level >= 0x16) {
4516 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4517
4518 /*
4519 * CPUID 16H Base MHz, Max MHz, Bus MHz
4520 */
4521 base_mhz = max_mhz = bus_mhz = edx = 0;
4522
Len Brown5aea2f72016-03-13 03:14:35 -04004523 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004524 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004525 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004526 base_mhz, max_mhz, bus_mhz);
4527 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004528
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004529 if (has_aperf)
4530 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4531
Len Brown812db3f2017-02-10 00:25:41 -05004532 BIC_PRESENT(BIC_IRQ);
4533 BIC_PRESENT(BIC_TSC_MHz);
4534
4535 if (probe_nhm_msrs(family, model)) {
4536 do_nhm_platform_info = 1;
4537 BIC_PRESENT(BIC_CPU_c1);
4538 BIC_PRESENT(BIC_CPU_c3);
4539 BIC_PRESENT(BIC_CPU_c6);
4540 BIC_PRESENT(BIC_SMI);
4541 }
Len Brownd7899442015-01-23 00:12:33 -05004542 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004543
4544 if (do_snb_cstates)
4545 BIC_PRESENT(BIC_CPU_c7);
4546
Len Brown5a634262016-04-06 17:15:55 -04004547 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004548 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4549 BIC_PRESENT(BIC_Pkgpc2);
4550 if (pkg_cstate_limit >= PCL__3)
4551 BIC_PRESENT(BIC_Pkgpc3);
4552 if (pkg_cstate_limit >= PCL__6)
4553 BIC_PRESENT(BIC_Pkgpc6);
4554 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4555 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004556 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004557 BIC_NOT_PRESENT(BIC_Pkgpc2);
4558 BIC_NOT_PRESENT(BIC_Pkgpc3);
4559 BIC_PRESENT(BIC_Pkgpc6);
4560 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004561 BIC_PRESENT(BIC_Mod_c6);
4562 use_c1_residency_msr = 1;
4563 }
Len Brown7170a372017-01-27 02:13:27 -05004564 if (is_dnv(family, model)) {
4565 BIC_PRESENT(BIC_CPU_c1);
4566 BIC_NOT_PRESENT(BIC_CPU_c3);
4567 BIC_NOT_PRESENT(BIC_Pkgpc3);
4568 BIC_NOT_PRESENT(BIC_CPU_c7);
4569 BIC_NOT_PRESENT(BIC_Pkgpc7);
4570 use_c1_residency_msr = 1;
4571 }
Len Brown34c761972017-01-27 02:36:41 -05004572 if (is_skx(family, model)) {
4573 BIC_NOT_PRESENT(BIC_CPU_c3);
4574 BIC_NOT_PRESENT(BIC_Pkgpc3);
4575 BIC_NOT_PRESENT(BIC_CPU_c7);
4576 BIC_NOT_PRESENT(BIC_Pkgpc7);
4577 }
Len Brownade0eba2017-02-10 01:56:47 -05004578 if (is_bdx(family, model)) {
4579 BIC_NOT_PRESENT(BIC_CPU_c7);
4580 BIC_NOT_PRESENT(BIC_Pkgpc7);
4581 }
Len Brown0f47c082017-01-27 00:50:45 -05004582 if (has_hsw_msrs(family, model)) {
4583 BIC_PRESENT(BIC_Pkgpc8);
4584 BIC_PRESENT(BIC_Pkgpc9);
4585 BIC_PRESENT(BIC_Pkgpc10);
4586 }
Len Brown5a634262016-04-06 17:15:55 -04004587 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004588 if (has_skl_msrs(family, model)) {
4589 BIC_PRESENT(BIC_Totl_c0);
4590 BIC_PRESENT(BIC_Any_c0);
4591 BIC_PRESENT(BIC_GFX_c0);
4592 BIC_PRESENT(BIC_CPUGFX);
4593 }
Len Brown144b44b2013-11-09 00:30:16 -05004594 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004595 do_knl_cstates = is_knl(family, model);
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004596 do_cnl_cstates = is_cnl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004597
Len Brown96e47152017-01-21 02:26:00 -05004598 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004599 decode_misc_pwr_mgmt_msr();
4600
Len Brown96e47152017-01-21 02:26:00 -05004601 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004602 decode_c6_demotion_policy_msr();
4603
Len Brown889facb2012-11-08 00:48:57 -05004604 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004605 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004606 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004607
Len Brown96e47152017-01-21 02:26:00 -05004608 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004609 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004610
Len Brown41618e62017-02-09 18:25:22 -05004611 if (!quiet)
4612 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004613 if (!quiet)
4614 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004615
Len Browna2b7b742015-09-26 00:12:38 -04004616 if (has_skl_msrs(family, model))
4617 calculate_tsc_tweak();
4618
Len Brown812db3f2017-02-10 00:25:41 -05004619 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4620 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004621
Len Brown812db3f2017-02-10 00:25:41 -05004622 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4623 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004624
Len Brownbe0e54c2018-06-01 12:35:53 -04004625 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4626 BIC_PRESENT(BIC_CPU_LPI);
4627 else
4628 BIC_NOT_PRESENT(BIC_CPU_LPI);
4629
4630 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4631 BIC_PRESENT(BIC_SYS_LPI);
4632 else
4633 BIC_NOT_PRESENT(BIC_SYS_LPI);
4634
Len Brown96e47152017-01-21 02:26:00 -05004635 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004636 decode_misc_feature_control();
4637
Len Brown889facb2012-11-08 00:48:57 -05004638 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004639}
4640
Len Brown103a8fe2010-10-22 23:53:03 -04004641
4642/*
4643 * in /dev/cpu/ return success for names that are numbers
4644 * ie. filter out ".", "..", "microcode".
4645 */
4646int dir_filter(const struct dirent *dirp)
4647{
4648 if (isdigit(dirp->d_name[0]))
4649 return 1;
4650 else
4651 return 0;
4652}
4653
4654int open_dev_cpu_msr(int dummy1)
4655{
4656 return 0;
4657}
4658
Len Brownc98d5d92012-06-04 00:56:40 -04004659void topology_probe()
4660{
4661 int i;
4662 int max_core_id = 0;
4663 int max_package_id = 0;
4664 int max_siblings = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004665
4666 /* Initialize num_cpus, max_cpu_num */
Prarit Bhargava843c5792018-06-01 10:04:28 -04004667 set_max_cpu_num();
Len Brownc98d5d92012-06-04 00:56:40 -04004668 topo.num_cpus = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004669 for_all_proc_cpus(count_cpus);
4670 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004671 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004672
Len Brownd8af6f52015-02-10 01:56:38 -05004673 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004674 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004675
4676 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004677 if (cpus == NULL)
4678 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004679
4680 /*
4681 * Allocate and initialize cpu_present_set
4682 */
4683 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004684 if (cpu_present_set == NULL)
4685 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004686 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4687 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4688 for_all_proc_cpus(mark_cpu_present);
4689
4690 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004691 * Validate that all cpus in cpu_subset are also in cpu_present_set
4692 */
4693 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4694 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4695 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4696 err(1, "cpu%d not present", i);
4697 }
4698
4699 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004700 * Allocate and initialize cpu_affinity_set
4701 */
4702 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004703 if (cpu_affinity_set == NULL)
4704 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004705 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4706 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4707
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004708 for_all_proc_cpus(init_thread_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004709
4710 /*
4711 * For online cpus
4712 * find max_core_id, max_package_id
4713 */
4714 for (i = 0; i <= topo.max_cpu_num; ++i) {
4715 int siblings;
4716
4717 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004718 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004719 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004720 continue;
4721 }
Len Brownc98d5d92012-06-04 00:56:40 -04004722
Len Brown0e2d8f02018-06-01 22:08:58 -04004723 cpus[i].logical_cpu_id = i;
4724
4725 /* get package information */
Len Brownc98d5d92012-06-04 00:56:40 -04004726 cpus[i].physical_package_id = get_physical_package_id(i);
4727 if (cpus[i].physical_package_id > max_package_id)
4728 max_package_id = cpus[i].physical_package_id;
4729
Len Brown0e2d8f02018-06-01 22:08:58 -04004730 /* get numa node information */
Prarit Bhargavaef605742018-06-01 10:04:30 -04004731 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4732 if (cpus[i].physical_node_id > topo.max_node_num)
4733 topo.max_node_num = cpus[i].physical_node_id;
Len Brown0e2d8f02018-06-01 22:08:58 -04004734
4735 /* get core information */
4736 cpus[i].physical_core_id = get_core_id(i);
4737 if (cpus[i].physical_core_id > max_core_id)
4738 max_core_id = cpus[i].physical_core_id;
4739
4740 /* get thread information */
4741 siblings = get_thread_siblings(&cpus[i]);
Len Brownc98d5d92012-06-04 00:56:40 -04004742 if (siblings > max_siblings)
4743 max_siblings = siblings;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004744 if (cpus[i].thread_id != -1)
4745 topo.num_cores++;
Len Brown0e2d8f02018-06-01 22:08:58 -04004746
Len Brownd8af6f52015-02-10 01:56:38 -05004747 if (debug > 1)
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004748 fprintf(outf,
4749 "cpu %d pkg %d node %d core %d thread %d\n",
Len Brown0e2d8f02018-06-01 22:08:58 -04004750 i, cpus[i].physical_package_id,
Prarit Bhargavaef605742018-06-01 10:04:30 -04004751 cpus[i].physical_node_id,
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004752 cpus[i].physical_core_id,
4753 cpus[i].thread_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004754 }
Prarit Bhargavaef605742018-06-01 10:04:30 -04004755
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004756 topo.cores_per_node = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004757 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004758 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004759 max_core_id, topo.cores_per_node);
4760 if (!summary_only && topo.cores_per_node > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004761 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004762
4763 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004764 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004765 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004766 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004767 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004768 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004769
Prarit Bhargavaef605742018-06-01 10:04:30 -04004770 set_node_data();
4771 if (debug > 1)
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004772 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
Prarit Bhargavaef605742018-06-01 10:04:30 -04004773
Prarit Bhargava70a9c6e2018-06-01 10:04:33 -04004774 topo.threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004775 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004776 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004777}
4778
4779void
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004780allocate_counters(struct thread_data **t, struct core_data **c,
4781 struct pkg_data **p)
Len Brownc98d5d92012-06-04 00:56:40 -04004782{
4783 int i;
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004784 int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
4785 topo.num_packages;
4786 int num_threads = topo.threads_per_core * num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04004787
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004788 *t = calloc(num_threads, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004789 if (*t == NULL)
4790 goto error;
4791
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004792 for (i = 0; i < num_threads; i++)
Len Brownc98d5d92012-06-04 00:56:40 -04004793 (*t)[i].cpu_id = -1;
4794
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004795 *c = calloc(num_cores, sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004796 if (*c == NULL)
4797 goto error;
4798
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004799 for (i = 0; i < num_cores; i++)
Len Brownc98d5d92012-06-04 00:56:40 -04004800 (*c)[i].core_id = -1;
4801
Len Brown678a3bd2017-02-09 22:22:13 -05004802 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004803 if (*p == NULL)
4804 goto error;
4805
4806 for (i = 0; i < topo.num_packages; i++)
4807 (*p)[i].package_id = i;
4808
4809 return;
4810error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004811 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004812}
4813/*
4814 * init_counter()
4815 *
Len Brownc98d5d92012-06-04 00:56:40 -04004816 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
Len Brownc98d5d92012-06-04 00:56:40 -04004817 */
4818void init_counter(struct thread_data *thread_base, struct core_data *core_base,
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004819 struct pkg_data *pkg_base, int cpu_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004820{
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004821 int pkg_id = cpus[cpu_id].physical_package_id;
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004822 int node_id = cpus[cpu_id].logical_node_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004823 int core_id = cpus[cpu_id].physical_core_id;
4824 int thread_id = cpus[cpu_id].thread_id;
Len Brownc98d5d92012-06-04 00:56:40 -04004825 struct thread_data *t;
4826 struct core_data *c;
4827 struct pkg_data *p;
4828
Prarit Bhargava40f5cfe2018-06-01 10:04:34 -04004829 t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
4830 c = GET_CORE(core_base, core_id, node_id, pkg_id);
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004831 p = GET_PKG(pkg_base, pkg_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004832
4833 t->cpu_id = cpu_id;
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004834 if (thread_id == 0) {
Len Brownc98d5d92012-06-04 00:56:40 -04004835 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4836 if (cpu_is_first_core_in_package(cpu_id))
4837 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4838 }
4839
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004840 c->core_id = core_id;
4841 p->package_id = pkg_id;
Len Brownc98d5d92012-06-04 00:56:40 -04004842}
4843
4844
4845int initialize_counters(int cpu_id)
4846{
Prarit Bhargava8cb48b32018-06-01 10:04:31 -04004847 init_counter(EVEN_COUNTERS, cpu_id);
4848 init_counter(ODD_COUNTERS, cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004849 return 0;
4850}
4851
4852void allocate_output_buffer()
4853{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004854 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004855 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004856 if (outp == NULL)
4857 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004858}
Len Brown36229892016-02-26 20:51:02 -05004859void allocate_fd_percpu(void)
4860{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004861 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004862 if (fd_percpu == NULL)
4863 err(-1, "calloc fd_percpu");
4864}
Len Brown562a2d32016-02-26 23:48:05 -05004865void allocate_irq_buffers(void)
4866{
4867 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4868 if (irq_column_2_cpu == NULL)
4869 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004870
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004871 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004872 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004873 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004874}
Len Brownc98d5d92012-06-04 00:56:40 -04004875void setup_all_buffers(void)
4876{
4877 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004878 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004879 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004880 allocate_counters(&thread_even, &core_even, &package_even);
4881 allocate_counters(&thread_odd, &core_odd, &package_odd);
4882 allocate_output_buffer();
4883 for_all_proc_cpus(initialize_counters);
4884}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004885
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004886void set_base_cpu(void)
4887{
4888 base_cpu = sched_getcpu();
4889 if (base_cpu < 0)
4890 err(-ENODEV, "No valid cpus found");
4891
4892 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004893 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004894}
4895
Len Brown103a8fe2010-10-22 23:53:03 -04004896void turbostat_init()
4897{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004898 setup_all_buffers();
4899 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004900 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004901 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004902 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004903
Len Brown103a8fe2010-10-22 23:53:03 -04004904
Len Brown96e47152017-01-21 02:26:00 -05004905 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004906 for_all_cpus(print_hwp, ODD_COUNTERS);
4907
Len Brown96e47152017-01-21 02:26:00 -05004908 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004909 for_all_cpus(print_epb, ODD_COUNTERS);
4910
Len Brown96e47152017-01-21 02:26:00 -05004911 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004912 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4913
Len Brown96e47152017-01-21 02:26:00 -05004914 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004915 for_all_cpus(print_rapl, ODD_COUNTERS);
4916
4917 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4918
Len Brown96e47152017-01-21 02:26:00 -05004919 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004920 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004921
Len Brown96e47152017-01-21 02:26:00 -05004922 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004923 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004924}
4925
4926int fork_it(char **argv)
4927{
Len Brown103a8fe2010-10-22 23:53:03 -04004928 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004929 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004930
Len Brown218f0e82017-02-14 22:07:52 -05004931 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004932 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4933 if (status)
4934 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004935 /* clear affinity side-effect of get_counters() */
4936 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004937 gettimeofday(&tv_even, (struct timezone *)NULL);
4938
4939 child_pid = fork();
4940 if (!child_pid) {
4941 /* child */
4942 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004943 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004944 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004945
4946 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004947 if (child_pid == -1)
4948 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004949
4950 signal(SIGINT, SIG_IGN);
4951 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004952 if (waitpid(child_pid, &status, 0) == -1)
4953 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004954 }
Len Brownc98d5d92012-06-04 00:56:40 -04004955 /*
4956 * n.b. fork_it() does not check for errors from for_all_cpus()
4957 * because re-starting is problematic when forking
4958 */
Len Brown218f0e82017-02-14 22:07:52 -05004959 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004960 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004961 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004962 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004963 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4964 fprintf(outf, "%s: Counter reset detected\n", progname);
4965 else {
4966 compute_average(EVEN_COUNTERS);
4967 format_all_counters(EVEN_COUNTERS);
4968 }
Len Brown103a8fe2010-10-22 23:53:03 -04004969
Len Brownb7d8c142016-02-13 23:36:17 -05004970 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4971
4972 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004973
Len Brownd91bb172012-11-01 00:08:19 -04004974 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004975}
4976
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004977int get_and_dump_counters(void)
4978{
4979 int status;
4980
Len Brown218f0e82017-02-14 22:07:52 -05004981 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004982 status = for_all_cpus(get_counters, ODD_COUNTERS);
4983 if (status)
4984 return status;
4985
4986 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4987 if (status)
4988 return status;
4989
Len Brownb7d8c142016-02-13 23:36:17 -05004990 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004991
4992 return status;
4993}
4994
Len Brownd8af6f52015-02-10 01:56:38 -05004995void print_version() {
Len Brownf7d44a82017-05-27 21:24:58 -07004996 fprintf(outf, "turbostat version 17.06.23"
Len Brownd8af6f52015-02-10 01:56:38 -05004997 " - Len Brown <lenb@kernel.org>\n");
4998}
4999
Len Brown495c76542017-02-08 02:41:51 -05005000int add_counter(unsigned int msr_num, char *path, char *name,
5001 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05005002 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05005003{
5004 struct msr_counter *msrp;
5005
5006 msrp = calloc(1, sizeof(struct msr_counter));
5007 if (msrp == NULL) {
5008 perror("calloc");
5009 exit(1);
5010 }
5011
5012 msrp->msr_num = msr_num;
5013 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05005014 if (path)
5015 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05005016 msrp->width = width;
5017 msrp->type = type;
5018 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05005019 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05005020
5021 switch (scope) {
5022
5023 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05005024 msrp->next = sys.tp;
5025 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005026 sys.added_thread_counters++;
Len Brown0748eaf2018-06-01 12:38:29 -04005027 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
Len Brown678a3bd2017-02-09 22:22:13 -05005028 fprintf(stderr, "exceeded max %d added thread counters\n",
5029 MAX_ADDED_COUNTERS);
5030 exit(-1);
5031 }
Len Brown388e9c82016-12-22 23:57:55 -05005032 break;
5033
5034 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05005035 msrp->next = sys.cp;
5036 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005037 sys.added_core_counters++;
5038 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5039 fprintf(stderr, "exceeded max %d added core counters\n",
5040 MAX_ADDED_COUNTERS);
5041 exit(-1);
5042 }
Len Brown388e9c82016-12-22 23:57:55 -05005043 break;
5044
5045 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05005046 msrp->next = sys.pp;
5047 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05005048 sys.added_package_counters++;
5049 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5050 fprintf(stderr, "exceeded max %d added package counters\n",
5051 MAX_ADDED_COUNTERS);
5052 exit(-1);
5053 }
Len Brown388e9c82016-12-22 23:57:55 -05005054 break;
5055 }
5056
5057 return 0;
5058}
5059
5060void parse_add_command(char *add_command)
5061{
5062 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05005063 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05005064 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05005065 int width = 64;
5066 int fail = 0;
5067 enum counter_scope scope = SCOPE_CPU;
5068 enum counter_type type = COUNTER_CYCLES;
5069 enum counter_format format = FORMAT_DELTA;
5070
5071 while (add_command) {
5072
5073 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5074 goto next;
5075
5076 if (sscanf(add_command, "msr%d", &msr_num) == 1)
5077 goto next;
5078
Len Brown495c76542017-02-08 02:41:51 -05005079 if (*add_command == '/') {
5080 path = add_command;
5081 goto next;
5082 }
5083
Len Brown388e9c82016-12-22 23:57:55 -05005084 if (sscanf(add_command, "u%d", &width) == 1) {
5085 if ((width == 32) || (width == 64))
5086 goto next;
5087 width = 64;
5088 }
5089 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5090 scope = SCOPE_CPU;
5091 goto next;
5092 }
5093 if (!strncmp(add_command, "core", strlen("core"))) {
5094 scope = SCOPE_CORE;
5095 goto next;
5096 }
5097 if (!strncmp(add_command, "package", strlen("package"))) {
5098 scope = SCOPE_PACKAGE;
5099 goto next;
5100 }
5101 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5102 type = COUNTER_CYCLES;
5103 goto next;
5104 }
5105 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5106 type = COUNTER_SECONDS;
5107 goto next;
5108 }
Len Brown41618e62017-02-09 18:25:22 -05005109 if (!strncmp(add_command, "usec", strlen("usec"))) {
5110 type = COUNTER_USEC;
5111 goto next;
5112 }
Len Brown388e9c82016-12-22 23:57:55 -05005113 if (!strncmp(add_command, "raw", strlen("raw"))) {
5114 format = FORMAT_RAW;
5115 goto next;
5116 }
5117 if (!strncmp(add_command, "delta", strlen("delta"))) {
5118 format = FORMAT_DELTA;
5119 goto next;
5120 }
5121 if (!strncmp(add_command, "percent", strlen("percent"))) {
5122 format = FORMAT_PERCENT;
5123 goto next;
5124 }
5125
5126 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
5127 char *eos;
5128
5129 eos = strchr(name_buffer, ',');
5130 if (eos)
5131 *eos = '\0';
5132 goto next;
5133 }
5134
5135next:
5136 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05005137 if (add_command) {
5138 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05005139 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05005140 }
Len Brown388e9c82016-12-22 23:57:55 -05005141
5142 }
Len Brown495c76542017-02-08 02:41:51 -05005143 if ((msr_num == 0) && (path == NULL)) {
5144 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05005145 fail++;
5146 }
5147
5148 /* generate default column header */
5149 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05005150 if (width == 32)
5151 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5152 else
5153 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05005154 }
5155
Len Brown41618e62017-02-09 18:25:22 -05005156 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05005157 fail++;
5158
5159 if (fail) {
5160 help();
5161 exit(1);
5162 }
5163}
Len Brown41618e62017-02-09 18:25:22 -05005164
Len Browndd778a52017-02-21 23:21:13 -05005165int is_deferred_skip(char *name)
5166{
5167 int i;
5168
5169 for (i = 0; i < deferred_skip_index; ++i)
5170 if (!strcmp(name, deferred_skip_names[i]))
5171 return 1;
5172 return 0;
5173}
5174
Len Brown41618e62017-02-09 18:25:22 -05005175void probe_sysfs(void)
5176{
5177 char path[64];
5178 char name_buf[16];
5179 FILE *input;
5180 int state;
5181 char *sp;
5182
5183 if (!DO_BIC(BIC_sysfs))
5184 return;
5185
Len Brown0748eaf2018-06-01 12:38:29 -04005186 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005187
5188 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5189 base_cpu, state);
5190 input = fopen(path, "r");
5191 if (input == NULL)
5192 continue;
5193 fgets(name_buf, sizeof(name_buf), input);
5194
5195 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5196 sp = strchr(name_buf, '-');
5197 if (!sp)
5198 sp = strchrnul(name_buf, '\n');
5199 *sp = '%';
5200 *(sp + 1) = '\0';
5201
5202 fclose(input);
5203
5204 sprintf(path, "cpuidle/state%d/time", state);
5205
Len Browndd778a52017-02-21 23:21:13 -05005206 if (is_deferred_skip(name_buf))
5207 continue;
5208
Len Brown41618e62017-02-09 18:25:22 -05005209 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
5210 FORMAT_PERCENT, SYSFS_PERCPU);
5211 }
5212
Len Brown0748eaf2018-06-01 12:38:29 -04005213 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005214
5215 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5216 base_cpu, state);
5217 input = fopen(path, "r");
5218 if (input == NULL)
5219 continue;
5220 fgets(name_buf, sizeof(name_buf), input);
5221 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5222 sp = strchr(name_buf, '-');
5223 if (!sp)
5224 sp = strchrnul(name_buf, '\n');
5225 *sp = '\0';
5226 fclose(input);
5227
5228 sprintf(path, "cpuidle/state%d/usage", state);
5229
Len Browndd778a52017-02-21 23:21:13 -05005230 if (is_deferred_skip(name_buf))
5231 continue;
5232
Len Brown41618e62017-02-09 18:25:22 -05005233 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
5234 FORMAT_DELTA, SYSFS_PERCPU);
5235 }
5236
5237}
5238
Len Brown1ef7d212017-02-10 23:54:15 -05005239
5240/*
5241 * parse cpuset with following syntax
5242 * 1,2,4..6,8-10 and set bits in cpu_subset
5243 */
5244void parse_cpu_command(char *optarg)
5245{
5246 unsigned int start, end;
5247 char *next;
5248
Len Brown4e4e1e72017-02-21 22:33:42 -05005249 if (!strcmp(optarg, "core")) {
5250 if (cpu_subset)
5251 goto error;
5252 show_core_only++;
5253 return;
5254 }
5255 if (!strcmp(optarg, "package")) {
5256 if (cpu_subset)
5257 goto error;
5258 show_pkg_only++;
5259 return;
5260 }
5261 if (show_core_only || show_pkg_only)
5262 goto error;
5263
Len Brown1ef7d212017-02-10 23:54:15 -05005264 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5265 if (cpu_subset == NULL)
5266 err(3, "CPU_ALLOC");
5267 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5268
5269 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5270
5271 next = optarg;
5272
5273 while (next && *next) {
5274
5275 if (*next == '-') /* no negative cpu numbers */
5276 goto error;
5277
5278 start = strtoul(next, &next, 10);
5279
5280 if (start >= CPU_SUBSET_MAXCPUS)
5281 goto error;
5282 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5283
5284 if (*next == '\0')
5285 break;
5286
5287 if (*next == ',') {
5288 next += 1;
5289 continue;
5290 }
5291
5292 if (*next == '-') {
5293 next += 1; /* start range */
5294 } else if (*next == '.') {
5295 next += 1;
5296 if (*next == '.')
5297 next += 1; /* start range */
5298 else
5299 goto error;
5300 }
5301
5302 end = strtoul(next, &next, 10);
5303 if (end <= start)
5304 goto error;
5305
5306 while (++start <= end) {
5307 if (start >= CPU_SUBSET_MAXCPUS)
5308 goto error;
5309 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5310 }
5311
5312 if (*next == ',')
5313 next += 1;
5314 else if (*next != '\0')
5315 goto error;
5316 }
5317
5318 return;
5319
5320error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005321 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5322 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005323 exit(-1);
5324}
5325
Len Brown812db3f2017-02-10 00:25:41 -05005326
Len Brown103a8fe2010-10-22 23:53:03 -04005327void cmdline(int argc, char **argv)
5328{
5329 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005330 int option_index = 0;
5331 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005332 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005333 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005334 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005335 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005336 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005337 {"interval", required_argument, 0, 'i'},
Chen Yu023fe0a2018-04-26 08:41:03 +08005338 {"num_iterations", required_argument, 0, 'n'},
Len Brownd8af6f52015-02-10 01:56:38 -05005339 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005340 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005341 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005342 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005343 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005344 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005345 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005346 {"Summary", no_argument, 0, 'S'},
5347 {"TCC", required_argument, 0, 'T'},
5348 {"version", no_argument, 0, 'v' },
5349 {0, 0, 0, 0 }
5350 };
Len Brown103a8fe2010-10-22 23:53:03 -04005351
5352 progname = argv[0];
5353
Chen Yu023fe0a2018-04-26 08:41:03 +08005354 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005355 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005356 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005357 case 'a':
5358 parse_add_command(optarg);
5359 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005360 case 'c':
5361 parse_cpu_command(optarg);
5362 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005363 case 'D':
5364 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005365 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005366 case 'e':
5367 /* --enable specified counter */
5368 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5369 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005370 case 'd':
5371 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005372 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005373 break;
Len Brown812db3f2017-02-10 00:25:41 -05005374 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005375 /*
5376 * --hide: do not show those specified
5377 * multiple invocations simply clear more bits in enabled mask
5378 */
5379 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005380 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005381 case 'h':
5382 default:
5383 help();
5384 exit(1);
5385 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005386 {
5387 double interval = strtod(optarg, NULL);
5388
5389 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005390 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005391 interval);
5392 exit(2);
5393 }
5394
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005395 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005396 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005397 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005398 }
Len Brown889facb2012-11-08 00:48:57 -05005399 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005400 case 'J':
5401 rapl_joules++;
5402 break;
Len Brownc8ade362017-02-15 17:15:11 -05005403 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005404 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005405 list_header_only++;
5406 quiet++;
5407 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005408 case 'o':
5409 outf = fopen_or_die(optarg, "w");
5410 break;
Len Brown96e47152017-01-21 02:26:00 -05005411 case 'q':
5412 quiet = 1;
5413 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08005414 case 'n':
5415 num_iterations = strtod(optarg, NULL);
5416
5417 if (num_iterations <= 0) {
5418 fprintf(outf, "iterations %d should be positive number\n",
5419 num_iterations);
5420 exit(2);
5421 }
5422 break;
Len Brown812db3f2017-02-10 00:25:41 -05005423 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005424 /*
5425 * --show: show only those specified
5426 * The 1st invocation will clear and replace the enabled mask
5427 * subsequent invocations can add to it.
5428 */
5429 if (shown == 0)
5430 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5431 else
5432 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5433 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005434 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005435 case 'S':
5436 summary_only++;
5437 break;
5438 case 'T':
5439 tcc_activation_temp_override = atoi(optarg);
5440 break;
5441 case 'v':
5442 print_version();
5443 exit(0);
5444 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005445 }
5446 }
5447}
5448
5449int main(int argc, char **argv)
5450{
Len Brownb7d8c142016-02-13 23:36:17 -05005451 outf = stderr;
5452
Len Brown103a8fe2010-10-22 23:53:03 -04005453 cmdline(argc, argv);
5454
Len Brown96e47152017-01-21 02:26:00 -05005455 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005456 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005457
Len Brown41618e62017-02-09 18:25:22 -05005458 probe_sysfs();
5459
Len Brown103a8fe2010-10-22 23:53:03 -04005460 turbostat_init();
5461
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005462 /* dump counters and exit */
5463 if (dump_only)
5464 return get_and_dump_counters();
5465
Len Brownc8ade362017-02-15 17:15:11 -05005466 /* list header and exit */
5467 if (list_header_only) {
5468 print_header(",");
5469 flush_output_stdout();
5470 return 0;
5471 }
5472
Len Brown103a8fe2010-10-22 23:53:03 -04005473 /*
5474 * if any params left, it must be a command to fork
5475 */
5476 if (argc - optind)
5477 return fork_it(argv + optind);
5478 else
5479 turbostat_loop();
5480
5481 return 0;
5482}