blob: 96390f7e48987d671cf2163d4335d7154ae33dd2 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce69e2016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
Len Brownb9ad8ee2017-07-19 19:28:37 -040032#include <sys/select.h>
Len Brown103a8fe2010-10-22 23:53:03 -040033#include <sys/resource.h>
34#include <fcntl.h>
35#include <signal.h>
36#include <sys/time.h>
37#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050038#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040039#include <dirent.h>
40#include <string.h>
41#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040042#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050043#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070044#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040045#include <linux/capability.h>
46#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040047
Len Brown103a8fe2010-10-22 23:53:03 -040048char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050049FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050050int *fd_percpu;
Len Brownb9ad8ee2017-07-19 19:28:37 -040051struct timeval interval_tv = {5, 0};
Artem Bityutskiy47936f92017-10-04 15:01:47 +030052struct timespec interval_ts = {5, 0};
Len Brownb9ad8ee2017-07-19 19:28:37 -040053struct timespec one_msec = {0, 1000000};
Chen Yu023fe0a2018-04-26 08:41:03 +080054unsigned int num_iterations;
Len Brownd8af6f52015-02-10 01:56:38 -050055unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050056unsigned int quiet;
Len Brown3f44a5c2017-10-17 15:42:56 -040057unsigned int shown;
Len Brown0de6c0d2017-02-15 21:45:40 -050058unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050059unsigned int rapl_joules;
60unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050061unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050062unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040063unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070064unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050065unsigned int do_slm_cstates;
Srinivas Pandruvada997e5392018-05-31 10:39:07 -070066unsigned int do_cnl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050067unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040068unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050069unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040070unsigned int do_irtl_snb;
71unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050072unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040073unsigned int genuine_intel;
74unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050075unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050076unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020077unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040078double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040079double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040080unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040081double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040082unsigned int show_pkg_only;
83unsigned int show_core_only;
84char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050085unsigned int do_rapl;
86unsigned int do_dts;
87unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050088unsigned long long gfx_cur_rc6_ms;
Len Brownbe0e54c2018-06-01 12:35:53 -040089unsigned long long cpuidle_cur_cpu_lpi_us;
90unsigned long long cpuidle_cur_sys_lpi_us;
Len Brown27d47352016-02-27 00:37:54 -050091unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050092unsigned int tcc_activation_temp;
93unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050094double rapl_power_units, rapl_time_units;
95double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050096double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040097unsigned int do_core_perf_limit_reasons;
Artem Bityutskiyac980e12017-09-05 15:14:08 +030098unsigned int has_automatic_cstate_conversion;
Len Brown3a9a9412014-08-15 02:39:52 -040099unsigned int do_gfx_perf_limit_reasons;
100unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -0400101unsigned int crystal_hz;
102unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400103int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400104double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500105unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
106 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
107unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
108unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
109unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
110unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500111unsigned int has_misc_feature_control;
Len 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 Brown0539ba112017-02-10 00:27:20 -0500183 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500184 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400185 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500186 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400187} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400188
Len Brownc98d5d92012-06-04 00:56:40 -0400189struct pkg_data {
190 unsigned long long pc2;
191 unsigned long long pc3;
192 unsigned long long pc6;
193 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800194 unsigned long long pc8;
195 unsigned long long pc9;
196 unsigned long long pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -0400197 unsigned long long cpu_lpi;
198 unsigned long long sys_lpi;
Len Brown0b2bb692015-03-26 00:50:30 -0400199 unsigned long long pkg_wtd_core_c0;
200 unsigned long long pkg_any_core_c0;
201 unsigned long long pkg_any_gfxe_c0;
202 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400203 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500204 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400205 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500206 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
207 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
208 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
209 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
210 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
211 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
212 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500213 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400214} *package_even, *package_odd;
215
216#define ODD_COUNTERS thread_odd, core_odd, package_odd
217#define EVEN_COUNTERS thread_even, core_even, package_even
218
219#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
220 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
221 topo.num_threads_per_core + \
222 (core_no) * topo.num_threads_per_core + (thread_no))
223#define GET_CORE(core_base, core_no, pkg_no) \
224 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
225#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
226
Len Brown388e9c82016-12-22 23:57:55 -0500227enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500228enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500229enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
230
231struct msr_counter {
232 unsigned int msr_num;
233 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500234 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500235 unsigned int width;
236 enum counter_type type;
237 enum counter_format format;
238 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500239 unsigned int flags;
240#define FLAGS_HIDE (1 << 0)
241#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500242#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500243};
244
245struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500246 unsigned int added_thread_counters;
247 unsigned int added_core_counters;
248 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500249 struct msr_counter *tp;
250 struct msr_counter *cp;
251 struct msr_counter *pp;
252} sys;
253
Len Brownc98d5d92012-06-04 00:56:40 -0400254struct system_summary {
255 struct thread_data threads;
256 struct core_data cores;
257 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500258} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400259
Len Brown0e2d8f02018-06-01 22:08:58 -0400260struct cpu_topology {
261 int physical_package_id;
262 int logical_cpu_id;
Prarit Bhargavaef605742018-06-01 10:04:30 -0400263 int physical_node_id;
264 int logical_node_id; /* 0-based count within the package */
Len Brown0e2d8f02018-06-01 22:08:58 -0400265 int physical_core_id;
266 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
267} *cpus;
Len Brownc98d5d92012-06-04 00:56:40 -0400268
269struct topo_params {
270 int num_packages;
271 int num_cpus;
272 int num_cores;
273 int max_cpu_num;
Prarit Bhargavaef605742018-06-01 10:04:30 -0400274 int max_node_num;
275 int num_nodes_per_pkg;
Len Brownc98d5d92012-06-04 00:56:40 -0400276 int num_cores_per_pkg;
277 int num_threads_per_core;
278} topo;
279
280struct timeval tv_even, tv_odd, tv_delta;
281
Len Brown562a2d32016-02-26 23:48:05 -0500282int *irq_column_2_cpu; /* /proc/interrupts column numbers */
283int *irqs_per_cpu; /* indexed by cpu_num */
284
Len Brownc98d5d92012-06-04 00:56:40 -0400285void setup_all_buffers(void);
286
287int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400288{
Len Brownc98d5d92012-06-04 00:56:40 -0400289 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400290}
Len Brown88c32812012-03-29 21:44:40 -0400291/*
Len Brownc98d5d92012-06-04 00:56:40 -0400292 * run func(thread, core, package) in topology order
293 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400294 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400295
Len Brownc98d5d92012-06-04 00:56:40 -0400296int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
297 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400298{
Len Brownc98d5d92012-06-04 00:56:40 -0400299 int retval, pkg_no, core_no, thread_no;
300
301 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
302 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
303 for (thread_no = 0; thread_no <
304 topo.num_threads_per_core; ++thread_no) {
305 struct thread_data *t;
306 struct core_data *c;
307 struct pkg_data *p;
308
309 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
310
311 if (cpu_is_not_present(t->cpu_id))
312 continue;
313
314 c = GET_CORE(core_base, core_no, pkg_no);
315 p = GET_PKG(pkg_base, pkg_no);
316
317 retval = func(t, c, p);
318 if (retval)
319 return retval;
320 }
321 }
322 }
323 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400324}
325
326int cpu_migrate(int cpu)
327{
Len Brownc98d5d92012-06-04 00:56:40 -0400328 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
329 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
330 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400331 return -1;
332 else
333 return 0;
334}
Len Brown36229892016-02-26 20:51:02 -0500335int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400336{
Len Brown103a8fe2010-10-22 23:53:03 -0400337 char pathname[32];
338 int fd;
339
Len Brown36229892016-02-26 20:51:02 -0500340 fd = fd_percpu[cpu];
341
342 if (fd)
343 return fd;
344
Len Brown103a8fe2010-10-22 23:53:03 -0400345 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
346 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400347 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400348 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 -0400349
Len Brown36229892016-02-26 20:51:02 -0500350 fd_percpu[cpu] = fd;
351
352 return fd;
353}
354
355int get_msr(int cpu, off_t offset, unsigned long long *msr)
356{
357 ssize_t retval;
358
359 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400360
Len Brown98481e72014-08-15 00:36:50 -0400361 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500362 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400363
364 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400365}
366
Len Brownfc04cc62014-02-06 00:55:19 -0500367/*
Len Brown812db3f2017-02-10 00:25:41 -0500368 * Each string in this array is compared in --show and --hide cmdline.
369 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500370 */
Len Brown812db3f2017-02-10 00:25:41 -0500371struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400372 { 0x0, "usec" },
373 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500374 { 0x0, "Package" },
375 { 0x0, "Avg_MHz" },
376 { 0x0, "Bzy_MHz" },
377 { 0x0, "TSC_MHz" },
378 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500379 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500380 { 0x0, "Busy%" },
381 { 0x0, "CPU%c1" },
382 { 0x0, "CPU%c3" },
383 { 0x0, "CPU%c6" },
384 { 0x0, "CPU%c7" },
385 { 0x0, "ThreadC" },
386 { 0x0, "CoreTmp" },
387 { 0x0, "CoreCnt" },
388 { 0x0, "PkgTmp" },
389 { 0x0, "GFX%rc6" },
390 { 0x0, "GFXMHz" },
391 { 0x0, "Pkg%pc2" },
392 { 0x0, "Pkg%pc3" },
393 { 0x0, "Pkg%pc6" },
394 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500395 { 0x0, "Pkg%pc8" },
396 { 0x0, "Pkg%pc9" },
Len Brown4bd1f8f2018-01-28 23:42:42 -0500397 { 0x0, "Pk%pc10" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400398 { 0x0, "CPU%LPI" },
399 { 0x0, "SYS%LPI" },
Len Brown812db3f2017-02-10 00:25:41 -0500400 { 0x0, "PkgWatt" },
401 { 0x0, "CorWatt" },
402 { 0x0, "GFXWatt" },
403 { 0x0, "PkgCnt" },
404 { 0x0, "RAMWatt" },
405 { 0x0, "PKG_%" },
406 { 0x0, "RAM_%" },
407 { 0x0, "Pkg_J" },
408 { 0x0, "Cor_J" },
409 { 0x0, "GFX_J" },
410 { 0x0, "RAM_J" },
411 { 0x0, "Core" },
412 { 0x0, "CPU" },
Len Brown0539ba112017-02-10 00:27:20 -0500413 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500414 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400415 { 0x0, "Totl%C0" },
416 { 0x0, "Any%C0" },
417 { 0x0, "GFX%C0" },
418 { 0x0, "CPUGFX%" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400419 { 0x0, "Node%" },
Len Brown812db3f2017-02-10 00:25:41 -0500420};
421
Len Browna99d8732017-05-20 20:11:55 -0400422
423
Len Brown812db3f2017-02-10 00:25:41 -0500424#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400425#define BIC_USEC (1ULL << 0)
426#define BIC_TOD (1ULL << 1)
427#define BIC_Package (1ULL << 2)
428#define BIC_Avg_MHz (1ULL << 3)
429#define BIC_Bzy_MHz (1ULL << 4)
430#define BIC_TSC_MHz (1ULL << 5)
431#define BIC_IRQ (1ULL << 6)
432#define BIC_SMI (1ULL << 7)
433#define BIC_Busy (1ULL << 8)
434#define BIC_CPU_c1 (1ULL << 9)
435#define BIC_CPU_c3 (1ULL << 10)
436#define BIC_CPU_c6 (1ULL << 11)
437#define BIC_CPU_c7 (1ULL << 12)
438#define BIC_ThreadC (1ULL << 13)
439#define BIC_CoreTmp (1ULL << 14)
440#define BIC_CoreCnt (1ULL << 15)
441#define BIC_PkgTmp (1ULL << 16)
442#define BIC_GFX_rc6 (1ULL << 17)
443#define BIC_GFXMHz (1ULL << 18)
444#define BIC_Pkgpc2 (1ULL << 19)
445#define BIC_Pkgpc3 (1ULL << 20)
446#define BIC_Pkgpc6 (1ULL << 21)
447#define BIC_Pkgpc7 (1ULL << 22)
448#define BIC_Pkgpc8 (1ULL << 23)
449#define BIC_Pkgpc9 (1ULL << 24)
450#define BIC_Pkgpc10 (1ULL << 25)
Len Brownbe0e54c2018-06-01 12:35:53 -0400451#define BIC_CPU_LPI (1ULL << 26)
452#define BIC_SYS_LPI (1ULL << 27)
Len Brown3f44a5c2017-10-17 15:42:56 -0400453#define BIC_PkgWatt (1ULL << 26)
454#define BIC_CorWatt (1ULL << 27)
455#define BIC_GFXWatt (1ULL << 28)
456#define BIC_PkgCnt (1ULL << 29)
457#define BIC_RAMWatt (1ULL << 30)
458#define BIC_PKG__ (1ULL << 31)
459#define BIC_RAM__ (1ULL << 32)
460#define BIC_Pkg_J (1ULL << 33)
461#define BIC_Cor_J (1ULL << 34)
462#define BIC_GFX_J (1ULL << 35)
463#define BIC_RAM_J (1ULL << 36)
464#define BIC_Core (1ULL << 37)
465#define BIC_CPU (1ULL << 38)
466#define BIC_Mod_c6 (1ULL << 39)
467#define BIC_sysfs (1ULL << 40)
468#define BIC_Totl_c0 (1ULL << 41)
469#define BIC_Any_c0 (1ULL << 42)
470#define BIC_GFX_c0 (1ULL << 43)
471#define BIC_CPUGFX (1ULL << 44)
Len Brown812db3f2017-02-10 00:25:41 -0500472
Len Brown3f44a5c2017-10-17 15:42:56 -0400473#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
474
475unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
476unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500477
478#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400479#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500480#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500481#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500482
Len Brown3f44a5c2017-10-17 15:42:56 -0400483
Len Browndd778a52017-02-21 23:21:13 -0500484#define MAX_DEFERRED 16
485char *deferred_skip_names[MAX_DEFERRED];
486int deferred_skip_index;
487
488/*
489 * HIDE_LIST - hide this list of counters, show the rest [default]
490 * SHOW_LIST - show this list of counters, hide the rest
491 */
492enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
493
494void help(void)
495{
496 fprintf(outf,
497 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
498 "\n"
499 "Turbostat forks the specified COMMAND and prints statistics\n"
500 "when COMMAND completes.\n"
501 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
502 "to print statistics, until interrupted.\n"
503 "--add add a counter\n"
504 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
505 "--cpu cpu-set limit output to summary plus cpu-set:\n"
506 " {core | package | j,k,l..m,n-p }\n"
507 "--quiet skip decoding system configuration header\n"
Len Brownb9ad8ee2017-07-19 19:28:37 -0400508 "--interval sec.subsec Override default 5-second measurement interval\n"
Len Browndd778a52017-02-21 23:21:13 -0500509 "--help print this help message\n"
510 "--list list column headers only\n"
Chen Yu023fe0a2018-04-26 08:41:03 +0800511 "--num_iterations num number of the measurement iterations\n"
Len Browndd778a52017-02-21 23:21:13 -0500512 "--out file create or truncate \"file\" for all output\n"
513 "--version print version information\n"
514 "\n"
515 "For more help, run \"man turbostat\"\n");
516}
517
Len Brown812db3f2017-02-10 00:25:41 -0500518/*
519 * bic_lookup
520 * for all the strings in comma separate name_list,
521 * set the approprate bit in return value.
522 */
Len Browndd778a52017-02-21 23:21:13 -0500523unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500524{
525 int i;
526 unsigned long long retval = 0;
527
528 while (name_list) {
529 char *comma;
530
531 comma = strchr(name_list, ',');
532
533 if (comma)
534 *comma = '\0';
535
Len Brown3f44a5c2017-10-17 15:42:56 -0400536 if (!strcmp(name_list, "all"))
537 return ~0;
538
Len Brown812db3f2017-02-10 00:25:41 -0500539 for (i = 0; i < MAX_BIC; ++i) {
540 if (!strcmp(name_list, bic[i].name)) {
541 retval |= (1ULL << i);
542 break;
543 }
544 }
545 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500546 if (mode == SHOW_LIST) {
547 fprintf(stderr, "Invalid counter name: %s\n", name_list);
548 exit(-1);
549 }
550 deferred_skip_names[deferred_skip_index++] = name_list;
551 if (debug)
552 fprintf(stderr, "deferred \"%s\"\n", name_list);
553 if (deferred_skip_index >= MAX_DEFERRED) {
554 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
555 MAX_DEFERRED, name_list);
556 help();
557 exit(1);
558 }
Len Brown812db3f2017-02-10 00:25:41 -0500559 }
560
561 name_list = comma;
562 if (name_list)
563 name_list++;
564
565 }
566 return retval;
567}
Len Brownfc04cc62014-02-06 00:55:19 -0500568
Len Browndd778a52017-02-21 23:21:13 -0500569
Len Brownc8ade362017-02-15 17:15:11 -0500570void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400571{
Len Brown388e9c82016-12-22 23:57:55 -0500572 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500573 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500574
Len Brown3f44a5c2017-10-17 15:42:56 -0400575 if (DO_BIC(BIC_USEC))
576 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
577 if (DO_BIC(BIC_TOD))
578 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500579 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500580 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500581 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500582 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500583 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500584 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500585 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500586 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500587 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500588 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500589 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500590 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500591 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500592 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500593
Len Brown0de6c0d2017-02-15 21:45:40 -0500594 if (DO_BIC(BIC_IRQ)) {
595 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500596 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500597 else
Len Brown6168c2e2017-02-16 23:07:51 -0500598 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500599 }
600
Len Brown812db3f2017-02-10 00:25:41 -0500601 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500602 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500603
Len Brown388e9c82016-12-22 23:57:55 -0500604 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500605
Len Brown388e9c82016-12-22 23:57:55 -0500606 if (mp->format == FORMAT_RAW) {
607 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500608 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500609 else
Len Browndd778a52017-02-21 23:21:13 -0500610 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500611 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500612 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500613 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500614 else
Len Browndd778a52017-02-21 23:21:13 -0500615 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500616 }
617 }
618
Len Brown41618e62017-02-09 18:25:22 -0500619 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500620 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700621 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500622 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500623 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500624 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500625 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500626 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500627
Len Brown0539ba112017-02-10 00:27:20 -0500628 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500629 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500630
Len Brown812db3f2017-02-10 00:25:41 -0500631 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500632 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500633
634 for (mp = sys.cp; mp; mp = mp->next) {
635 if (mp->format == FORMAT_RAW) {
636 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500637 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500638 else
Len Brownc8ade362017-02-15 17:15:11 -0500639 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500640 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500641 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
642 outp += sprintf(outp, "%s%8s", delim, mp->name);
643 else
644 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500645 }
646 }
647
Len Brown812db3f2017-02-10 00:25:41 -0500648 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500649 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500650
Len Brown812db3f2017-02-10 00:25:41 -0500651 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500652 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500653
Len Brown812db3f2017-02-10 00:25:41 -0500654 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500655 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500656
Len Browna99d8732017-05-20 20:11:55 -0400657 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500658 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400659 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500660 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400661 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500662 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400663 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500664 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400665
Len Brown0f47c082017-01-27 00:50:45 -0500666 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500667 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500668 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500669 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500670 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500671 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500672 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500673 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500674 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500675 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500676 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500677 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500678 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500679 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brownbe0e54c2018-06-01 12:35:53 -0400680 if (DO_BIC(BIC_CPU_LPI))
681 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
682 if (DO_BIC(BIC_SYS_LPI))
683 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400684
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800685 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500686 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500687 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500688 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500689 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500690 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500691 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500692 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500693 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500694 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500695 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500696 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500697 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500698 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500699 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500700 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500701 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500702 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500703 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500704 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500705 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500706 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500707 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500708 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500709 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500710 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800711 }
Len Brown388e9c82016-12-22 23:57:55 -0500712 for (mp = sys.pp; mp; mp = mp->next) {
713 if (mp->format == FORMAT_RAW) {
714 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500715 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500716 else
Len Brownc8ade362017-02-15 17:15:11 -0500717 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500718 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500719 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
720 outp += sprintf(outp, "%s%8s", delim, mp->name);
721 else
722 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500723 }
724 }
725
Len Brownc98d5d92012-06-04 00:56:40 -0400726 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400727}
728
Len Brownc98d5d92012-06-04 00:56:40 -0400729int dump_counters(struct thread_data *t, struct core_data *c,
730 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400731{
Len Brown388e9c82016-12-22 23:57:55 -0500732 int i;
733 struct msr_counter *mp;
734
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200735 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400736
Len Brownc98d5d92012-06-04 00:56:40 -0400737 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200738 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
739 t->cpu_id, t->flags);
740 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
741 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
742 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
743 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500744
Len Brown812db3f2017-02-10 00:25:41 -0500745 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500746 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500747 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500748 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500749
750 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
751 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
752 i, mp->msr_num, t->counter[i]);
753 }
Len Brownc98d5d92012-06-04 00:56:40 -0400754 }
Len Brown103a8fe2010-10-22 23:53:03 -0400755
Len Brownc98d5d92012-06-04 00:56:40 -0400756 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200757 outp += sprintf(outp, "core: %d\n", c->core_id);
758 outp += sprintf(outp, "c3: %016llX\n", c->c3);
759 outp += sprintf(outp, "c6: %016llX\n", c->c6);
760 outp += sprintf(outp, "c7: %016llX\n", c->c7);
761 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500762
763 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
764 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
765 i, mp->msr_num, c->counter[i]);
766 }
Len Brown0539ba112017-02-10 00:27:20 -0500767 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400768 }
769
770 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200771 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400772
773 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
774 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
775 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
776 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
777
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200778 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500779 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500780 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500781 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500782 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500783 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500784 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200785 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
786 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
787 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
Len Brownbe0e54c2018-06-01 12:35:53 -0400788 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
789 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
790 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200791 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
792 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
793 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
794 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
795 outp += sprintf(outp, "Throttle PKG: %0X\n",
796 p->rapl_pkg_perf_status);
797 outp += sprintf(outp, "Throttle RAM: %0X\n",
798 p->rapl_dram_perf_status);
799 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500800
801 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
802 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
803 i, mp->msr_num, p->counter[i]);
804 }
Len Brownc98d5d92012-06-04 00:56:40 -0400805 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200806
807 outp += sprintf(outp, "\n");
808
Len Brownc98d5d92012-06-04 00:56:40 -0400809 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400810}
811
Len Browne23da032012-02-06 18:37:16 -0500812/*
813 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500814 */
Len Brownc98d5d92012-06-04 00:56:40 -0400815int format_counters(struct thread_data *t, struct core_data *c,
816 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400817{
Len Brown008d396e2017-02-10 00:29:51 -0500818 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500819 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500820 int i;
821 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500822 char *delim = "\t";
823 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400824
Len Brownc98d5d92012-06-04 00:56:40 -0400825 /* if showing only 1st thread in core and this isn't one, bail out */
826 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
827 return 0;
828
829 /* if showing only 1st thread in pkg and this isn't one, bail out */
830 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
831 return 0;
832
Len Brown1ef7d212017-02-10 23:54:15 -0500833 /*if not summary line and --cpu is used */
834 if ((t != &average.threads) &&
835 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
836 return 0;
837
Len Brown3f44a5c2017-10-17 15:42:56 -0400838 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700839 /* on each row, print how many usec each timestamp took to gather */
840 struct timeval tv;
841
842 timersub(&t->tv_end, &t->tv_begin, &tv);
843 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
844 }
845
Len Brown3f44a5c2017-10-17 15:42:56 -0400846 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
847 if (DO_BIC(BIC_TOD))
848 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
849
Len Brown103a8fe2010-10-22 23:53:03 -0400850 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
851
Len Brown008d396e2017-02-10 00:29:51 -0500852 tsc = t->tsc * tsc_tweak;
853
Len Brownc98d5d92012-06-04 00:56:40 -0400854 /* topo columns, print blanks on 1st (average) line */
855 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500856 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500857 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500858 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500859 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500860 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500861 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400862 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500863 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400864 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500865 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400866 else
Len Brown6168c2e2017-02-16 23:07:51 -0500867 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400868 }
Len Brown812db3f2017-02-10 00:25:41 -0500869 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400870 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500871 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400872 else
Len Brown6168c2e2017-02-16 23:07:51 -0500873 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400874 }
Len Brown812db3f2017-02-10 00:25:41 -0500875 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500876 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400877 }
Len Brownfc04cc62014-02-06 00:55:19 -0500878
Len Brown812db3f2017-02-10 00:25:41 -0500879 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500880 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500881 1.0 / units * t->aperf / interval_float);
882
Len Brown812db3f2017-02-10 00:25:41 -0500883 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500884 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400885
Len Brown812db3f2017-02-10 00:25:41 -0500886 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400887 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500888 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400889 else
Len Brown6168c2e2017-02-16 23:07:51 -0500890 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500891 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400892 }
Len Brown103a8fe2010-10-22 23:53:03 -0400893
Len Brown812db3f2017-02-10 00:25:41 -0500894 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500895 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400896
Len Brown562a2d32016-02-26 23:48:05 -0500897 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500898 if (DO_BIC(BIC_IRQ)) {
899 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500900 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500901 else
Len Brown6168c2e2017-02-16 23:07:51 -0500902 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500903 }
Len Brown562a2d32016-02-26 23:48:05 -0500904
Len Brown1cc21f72015-02-23 00:34:57 -0500905 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500906 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500907 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500908
Len Brown678a3bd2017-02-09 22:22:13 -0500909 /* Added counters */
910 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
911 if (mp->format == FORMAT_RAW) {
912 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500913 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500914 else
Len Brown6168c2e2017-02-16 23:07:51 -0500915 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500916 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500917 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500918 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500919 else
Len Brown6168c2e2017-02-16 23:07:51 -0500920 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500921 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500922 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500923 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500924 else
Len Brown6168c2e2017-02-16 23:07:51 -0500925 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500926 }
927 }
928
Len Brown41618e62017-02-09 18:25:22 -0500929 /* C1 */
930 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500931 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500932
933
Len Brownc98d5d92012-06-04 00:56:40 -0400934 /* print per-core data only for 1st thread in core */
935 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
936 goto done;
937
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700938 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500939 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500940 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500941 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500942 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500943 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400944
Len Brown0539ba112017-02-10 00:27:20 -0500945 /* Mod%c6 */
946 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500947 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba112017-02-10 00:27:20 -0500948
Len Brown812db3f2017-02-10 00:25:41 -0500949 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500950 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500951
Len Brown388e9c82016-12-22 23:57:55 -0500952 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
953 if (mp->format == FORMAT_RAW) {
954 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500955 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500956 else
Len Brown6168c2e2017-02-16 23:07:51 -0500957 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500958 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500959 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500960 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500961 else
Len Brown6168c2e2017-02-16 23:07:51 -0500962 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500963 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500964 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500965 }
966 }
967
Len Brownc98d5d92012-06-04 00:56:40 -0400968 /* print per-package data only for 1st core in package */
969 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
970 goto done;
971
Len Brown0b2bb692015-03-26 00:50:30 -0400972 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500973 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500974 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500975
Len Brownfdf676e2016-02-27 01:28:12 -0500976 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500977 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400978 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500979 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400980 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500981 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400982 p->gfx_rc6_ms / 10.0 / interval_float);
983 }
984 }
Len Brownfdf676e2016-02-27 01:28:12 -0500985
Len Brown27d47352016-02-27 00:37:54 -0500986 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500987 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500988 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500989
Len Brown0b2bb692015-03-26 00:50:30 -0400990 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -0400991 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500992 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400993 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500994 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400995 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500996 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400997 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500998 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400999
Len Brown0f47c082017-01-27 00:50:45 -05001000 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -05001001 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001002 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -05001003 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001004 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -05001005 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001006 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -05001007 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001008 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -05001009 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001010 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -05001011 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001012 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -05001013 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -05001014
Len Brownbe0e54c2018-06-01 12:35:53 -04001015 if (DO_BIC(BIC_CPU_LPI))
1016 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1017 if (DO_BIC(BIC_SYS_LPI))
1018 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1019
Len Brown889facb2012-11-08 00:48:57 -05001020 /*
1021 * If measurement interval exceeds minimum RAPL Joule Counter range,
1022 * indicate that results are suspect by printing "**" in fraction place.
1023 */
Len Brownfc04cc62014-02-06 00:55:19 -05001024 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -05001025 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -05001026 else
Len Browne975db52016-04-06 23:56:02 -04001027 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -05001028
Len Brown812db3f2017-02-10 00:25:41 -05001029 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001030 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001031 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001032 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001033 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001034 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001035 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001036 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001037 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001038 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001039 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001040 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001041 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001042 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001043 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001044 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001045 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001046 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 -05001047 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001048 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 -05001049
Len Brown388e9c82016-12-22 23:57:55 -05001050 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1051 if (mp->format == FORMAT_RAW) {
1052 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001053 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001054 else
Len Brown6168c2e2017-02-16 23:07:51 -05001055 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001056 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001057 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001058 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001059 else
Len Brown6168c2e2017-02-16 23:07:51 -05001060 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001061 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001062 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001063 }
1064 }
1065
Len Brownc98d5d92012-06-04 00:56:40 -04001066done:
Len Brown94d6ab42018-01-27 22:39:21 -05001067 if (*(outp - 1) != '\n')
1068 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001069
1070 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001071}
1072
Len Brownb7d8c142016-02-13 23:36:17 -05001073void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001074{
Len Brownb7d8c142016-02-13 23:36:17 -05001075 FILE *filep;
1076
1077 if (outf == stderr)
1078 filep = stdout;
1079 else
1080 filep = outf;
1081
1082 fputs(output_buffer, filep);
1083 fflush(filep);
1084
Len Brownc98d5d92012-06-04 00:56:40 -04001085 outp = output_buffer;
1086}
Len Brownb7d8c142016-02-13 23:36:17 -05001087void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001088{
Len Brownb7d8c142016-02-13 23:36:17 -05001089 fputs(output_buffer, outf);
1090 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001091 outp = output_buffer;
1092}
1093void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1094{
Len Browne23da032012-02-06 18:37:16 -05001095 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001096
Len Browne23da032012-02-06 18:37:16 -05001097 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001098 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001099
Len Brownc98d5d92012-06-04 00:56:40 -04001100 if (topo.num_cpus > 1)
1101 format_counters(&average.threads, &average.cores,
1102 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001103
Len Browne23da032012-02-06 18:37:16 -05001104 printed = 1;
1105
1106 if (summary_only)
1107 return;
1108
Len Brownc98d5d92012-06-04 00:56:40 -04001109 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001110}
1111
Len Brown889facb2012-11-08 00:48:57 -05001112#define DELTA_WRAP32(new, old) \
1113 if (new > old) { \
1114 old = new - old; \
1115 } else { \
1116 old = 0x100000000 + new - old; \
1117 }
1118
Len Brownba3dec92016-04-22 20:31:46 -04001119int
Len Brownc98d5d92012-06-04 00:56:40 -04001120delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001121{
Len Brown388e9c82016-12-22 23:57:55 -05001122 int i;
1123 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001124
Len Browna99d8732017-05-20 20:11:55 -04001125
1126 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001127 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001128 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001129 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001130 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001131 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001132 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001133 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 -04001134
Len Brownc98d5d92012-06-04 00:56:40 -04001135 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001136 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001137 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001138 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001139 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001140 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001141 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001142 old->pc8 = new->pc8 - old->pc8;
1143 old->pc9 = new->pc9 - old->pc9;
1144 old->pc10 = new->pc10 - old->pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -04001145 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1146 old->sys_lpi = new->sys_lpi - old->sys_lpi;
Len Brown889facb2012-11-08 00:48:57 -05001147 old->pkg_temp_c = new->pkg_temp_c;
1148
Len Brown9185e982016-04-06 17:16:00 -04001149 /* flag an error when rc6 counter resets/wraps */
1150 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1151 old->gfx_rc6_ms = -1;
1152 else
1153 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1154
Len Brown27d47352016-02-27 00:37:54 -05001155 old->gfx_mhz = new->gfx_mhz;
1156
Len Brown889facb2012-11-08 00:48:57 -05001157 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1158 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1159 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1160 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1161 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1162 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001163
Len Brown388e9c82016-12-22 23:57:55 -05001164 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1165 if (mp->format == FORMAT_RAW)
1166 old->counter[i] = new->counter[i];
1167 else
1168 old->counter[i] = new->counter[i] - old->counter[i];
1169 }
1170
Len Brownba3dec92016-04-22 20:31:46 -04001171 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001172}
Len Brown103a8fe2010-10-22 23:53:03 -04001173
Len Brownc98d5d92012-06-04 00:56:40 -04001174void
1175delta_core(struct core_data *new, struct core_data *old)
1176{
Len Brown388e9c82016-12-22 23:57:55 -05001177 int i;
1178 struct msr_counter *mp;
1179
Len Brownc98d5d92012-06-04 00:56:40 -04001180 old->c3 = new->c3 - old->c3;
1181 old->c6 = new->c6 - old->c6;
1182 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001183 old->core_temp_c = new->core_temp_c;
Len Brown0539ba112017-02-10 00:27:20 -05001184 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001185
1186 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1187 if (mp->format == FORMAT_RAW)
1188 old->counter[i] = new->counter[i];
1189 else
1190 old->counter[i] = new->counter[i] - old->counter[i];
1191 }
Len Brownc98d5d92012-06-04 00:56:40 -04001192}
Len Brown103a8fe2010-10-22 23:53:03 -04001193
Len Brownc3ae3312012-06-13 21:31:46 -04001194/*
1195 * old = new - old
1196 */
Len Brownba3dec92016-04-22 20:31:46 -04001197int
Len Brownc98d5d92012-06-04 00:56:40 -04001198delta_thread(struct thread_data *new, struct thread_data *old,
1199 struct core_data *core_delta)
1200{
Len Brown388e9c82016-12-22 23:57:55 -05001201 int i;
1202 struct msr_counter *mp;
1203
Len Brown3f44a5c2017-10-17 15:42:56 -04001204 /*
1205 * the timestamps from start of measurement interval are in "old"
1206 * the timestamp from end of measurement interval are in "new"
1207 * over-write old w/ new so we can print end of interval values
1208 */
1209
1210 old->tv_begin = new->tv_begin;
1211 old->tv_end = new->tv_end;
1212
Len Brownc98d5d92012-06-04 00:56:40 -04001213 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001214
Len Brownc98d5d92012-06-04 00:56:40 -04001215 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001216 if (old->tsc < (1000 * 1000))
1217 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1218 "You can disable all c-states by booting with \"idle=poll\"\n"
1219 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001220
Len Brownc98d5d92012-06-04 00:56:40 -04001221 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001222
Len Brown812db3f2017-02-10 00:25:41 -05001223 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001224 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1225 old->aperf = new->aperf - old->aperf;
1226 old->mperf = new->mperf - old->mperf;
1227 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001228 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001229 }
Len Brownc98d5d92012-06-04 00:56:40 -04001230 }
Len Brown103a8fe2010-10-22 23:53:03 -04001231
Len Brown103a8fe2010-10-22 23:53:03 -04001232
Len Brown144b44b2013-11-09 00:30:16 -05001233 if (use_c1_residency_msr) {
1234 /*
1235 * Some models have a dedicated C1 residency MSR,
1236 * which should be more accurate than the derivation below.
1237 */
1238 } else {
1239 /*
1240 * As counter collection is not atomic,
1241 * it is possible for mperf's non-halted cycles + idle states
1242 * to exceed TSC's all cycles: show c1 = 0% in that case.
1243 */
Len Brown95149362017-04-12 19:44:51 -04001244 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001245 old->c1 = 0;
1246 else {
1247 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001248 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001249 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001250 }
Len Brownc98d5d92012-06-04 00:56:40 -04001251 }
Len Brownc3ae3312012-06-13 21:31:46 -04001252
Len Brownc98d5d92012-06-04 00:56:40 -04001253 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001254 if (debug > 1)
1255 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001256 old->mperf = 1; /* divide by 0 protection */
1257 }
1258
Len Brown812db3f2017-02-10 00:25:41 -05001259 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001260 old->irq_count = new->irq_count - old->irq_count;
1261
Len Brown812db3f2017-02-10 00:25:41 -05001262 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001263 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001264
Len Brown388e9c82016-12-22 23:57:55 -05001265 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1266 if (mp->format == FORMAT_RAW)
1267 old->counter[i] = new->counter[i];
1268 else
1269 old->counter[i] = new->counter[i] - old->counter[i];
1270 }
Len Brownba3dec92016-04-22 20:31:46 -04001271 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001272}
1273
1274int delta_cpu(struct thread_data *t, struct core_data *c,
1275 struct pkg_data *p, struct thread_data *t2,
1276 struct core_data *c2, struct pkg_data *p2)
1277{
Len Brownba3dec92016-04-22 20:31:46 -04001278 int retval = 0;
1279
Len Brownc98d5d92012-06-04 00:56:40 -04001280 /* calculate core delta only for 1st thread in core */
1281 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1282 delta_core(c, c2);
1283
1284 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001285 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1286 if (retval)
1287 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001288
1289 /* calculate package delta only for 1st core in package */
1290 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001291 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001292
Len Brownba3dec92016-04-22 20:31:46 -04001293 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001294}
1295
1296void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1297{
Len Brown388e9c82016-12-22 23:57:55 -05001298 int i;
1299 struct msr_counter *mp;
1300
Len Brown3f44a5c2017-10-17 15:42:56 -04001301 t->tv_begin.tv_sec = 0;
1302 t->tv_begin.tv_usec = 0;
1303 t->tv_end.tv_sec = 0;
1304 t->tv_end.tv_usec = 0;
1305
Len Brownc98d5d92012-06-04 00:56:40 -04001306 t->tsc = 0;
1307 t->aperf = 0;
1308 t->mperf = 0;
1309 t->c1 = 0;
1310
Len Brown562a2d32016-02-26 23:48:05 -05001311 t->irq_count = 0;
1312 t->smi_count = 0;
1313
Len Brownc98d5d92012-06-04 00:56:40 -04001314 /* tells format_counters to dump all fields from this set */
1315 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1316
1317 c->c3 = 0;
1318 c->c6 = 0;
1319 c->c7 = 0;
Len Brown0539ba112017-02-10 00:27:20 -05001320 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001321 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001322
Len Brown0b2bb692015-03-26 00:50:30 -04001323 p->pkg_wtd_core_c0 = 0;
1324 p->pkg_any_core_c0 = 0;
1325 p->pkg_any_gfxe_c0 = 0;
1326 p->pkg_both_core_gfxe_c0 = 0;
1327
Len Brownc98d5d92012-06-04 00:56:40 -04001328 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001329 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001330 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001331 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001332 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001333 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001334 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001335 p->pc8 = 0;
1336 p->pc9 = 0;
1337 p->pc10 = 0;
Len Brownbe0e54c2018-06-01 12:35:53 -04001338 p->cpu_lpi = 0;
1339 p->sys_lpi = 0;
Len Brown889facb2012-11-08 00:48:57 -05001340
1341 p->energy_pkg = 0;
1342 p->energy_dram = 0;
1343 p->energy_cores = 0;
1344 p->energy_gfx = 0;
1345 p->rapl_pkg_perf_status = 0;
1346 p->rapl_dram_perf_status = 0;
1347 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001348
Len Brownfdf676e2016-02-27 01:28:12 -05001349 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001350 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001351 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1352 t->counter[i] = 0;
1353
1354 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1355 c->counter[i] = 0;
1356
1357 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1358 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001359}
1360int sum_counters(struct thread_data *t, struct core_data *c,
1361 struct pkg_data *p)
1362{
Len Brown388e9c82016-12-22 23:57:55 -05001363 int i;
1364 struct msr_counter *mp;
1365
Len Brown3f44a5c2017-10-17 15:42:56 -04001366 /* remember first tv_begin */
1367 if (average.threads.tv_begin.tv_sec == 0)
1368 average.threads.tv_begin = t->tv_begin;
1369
1370 /* remember last tv_end */
1371 average.threads.tv_end = t->tv_end;
1372
Len Brownc98d5d92012-06-04 00:56:40 -04001373 average.threads.tsc += t->tsc;
1374 average.threads.aperf += t->aperf;
1375 average.threads.mperf += t->mperf;
1376 average.threads.c1 += t->c1;
1377
Len Brown562a2d32016-02-26 23:48:05 -05001378 average.threads.irq_count += t->irq_count;
1379 average.threads.smi_count += t->smi_count;
1380
Len Brown388e9c82016-12-22 23:57:55 -05001381 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1382 if (mp->format == FORMAT_RAW)
1383 continue;
1384 average.threads.counter[i] += t->counter[i];
1385 }
1386
Len Brownc98d5d92012-06-04 00:56:40 -04001387 /* sum per-core values only for 1st thread in core */
1388 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1389 return 0;
1390
1391 average.cores.c3 += c->c3;
1392 average.cores.c6 += c->c6;
1393 average.cores.c7 += c->c7;
Len Brown0539ba112017-02-10 00:27:20 -05001394 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001395
Len Brown889facb2012-11-08 00:48:57 -05001396 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1397
Len Brown388e9c82016-12-22 23:57:55 -05001398 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1399 if (mp->format == FORMAT_RAW)
1400 continue;
1401 average.cores.counter[i] += c->counter[i];
1402 }
1403
Len Brownc98d5d92012-06-04 00:56:40 -04001404 /* sum per-pkg values only for 1st core in pkg */
1405 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1406 return 0;
1407
Len Browna99d8732017-05-20 20:11:55 -04001408 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001409 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001410 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001411 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001412 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001413 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001414 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001415 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001416
Len Brownc98d5d92012-06-04 00:56:40 -04001417 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001418 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001419 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001420 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001421 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001422 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001423 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001424 average.packages.pc8 += p->pc8;
1425 average.packages.pc9 += p->pc9;
1426 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001427
Len Brownbe0e54c2018-06-01 12:35:53 -04001428 average.packages.cpu_lpi = p->cpu_lpi;
1429 average.packages.sys_lpi = p->sys_lpi;
1430
Len Brown889facb2012-11-08 00:48:57 -05001431 average.packages.energy_pkg += p->energy_pkg;
1432 average.packages.energy_dram += p->energy_dram;
1433 average.packages.energy_cores += p->energy_cores;
1434 average.packages.energy_gfx += p->energy_gfx;
1435
Len Brownfdf676e2016-02-27 01:28:12 -05001436 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001437 average.packages.gfx_mhz = p->gfx_mhz;
1438
Len Brown889facb2012-11-08 00:48:57 -05001439 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1440
1441 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1442 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001443
1444 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1445 if (mp->format == FORMAT_RAW)
1446 continue;
1447 average.packages.counter[i] += p->counter[i];
1448 }
Len Brownc98d5d92012-06-04 00:56:40 -04001449 return 0;
1450}
1451/*
1452 * sum the counters for all cpus in the system
1453 * compute the weighted average
1454 */
1455void compute_average(struct thread_data *t, struct core_data *c,
1456 struct pkg_data *p)
1457{
Len Brown388e9c82016-12-22 23:57:55 -05001458 int i;
1459 struct msr_counter *mp;
1460
Len Brownc98d5d92012-06-04 00:56:40 -04001461 clear_counters(&average.threads, &average.cores, &average.packages);
1462
1463 for_all_cpus(sum_counters, t, c, p);
1464
1465 average.threads.tsc /= topo.num_cpus;
1466 average.threads.aperf /= topo.num_cpus;
1467 average.threads.mperf /= topo.num_cpus;
1468 average.threads.c1 /= topo.num_cpus;
1469
Len Brown0de6c0d2017-02-15 21:45:40 -05001470 if (average.threads.irq_count > 9999999)
1471 sums_need_wide_columns = 1;
1472
Len Brownc98d5d92012-06-04 00:56:40 -04001473 average.cores.c3 /= topo.num_cores;
1474 average.cores.c6 /= topo.num_cores;
1475 average.cores.c7 /= topo.num_cores;
Len Brown0539ba112017-02-10 00:27:20 -05001476 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001477
Len Browna99d8732017-05-20 20:11:55 -04001478 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001479 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001480 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001481 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001482 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001483 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001484 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001485 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001486
Len Brownc98d5d92012-06-04 00:56:40 -04001487 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001488 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001489 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001490 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001491 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001492 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001493 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001494
1495 average.packages.pc8 /= topo.num_packages;
1496 average.packages.pc9 /= topo.num_packages;
1497 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001498
1499 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1500 if (mp->format == FORMAT_RAW)
1501 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001502 if (mp->type == COUNTER_ITEMS) {
1503 if (average.threads.counter[i] > 9999999)
1504 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001505 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001506 }
Len Brown388e9c82016-12-22 23:57:55 -05001507 average.threads.counter[i] /= topo.num_cpus;
1508 }
1509 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1510 if (mp->format == FORMAT_RAW)
1511 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001512 if (mp->type == COUNTER_ITEMS) {
1513 if (average.cores.counter[i] > 9999999)
1514 sums_need_wide_columns = 1;
1515 }
Len Brown388e9c82016-12-22 23:57:55 -05001516 average.cores.counter[i] /= topo.num_cores;
1517 }
1518 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1519 if (mp->format == FORMAT_RAW)
1520 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001521 if (mp->type == COUNTER_ITEMS) {
1522 if (average.packages.counter[i] > 9999999)
1523 sums_need_wide_columns = 1;
1524 }
Len Brown388e9c82016-12-22 23:57:55 -05001525 average.packages.counter[i] /= topo.num_packages;
1526 }
Len Brownc98d5d92012-06-04 00:56:40 -04001527}
1528
1529static unsigned long long rdtsc(void)
1530{
1531 unsigned int low, high;
1532
1533 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1534
1535 return low | ((unsigned long long)high) << 32;
1536}
1537
Len Brownc98d5d92012-06-04 00:56:40 -04001538/*
Len Brown495c76542017-02-08 02:41:51 -05001539 * Open a file, and exit on failure
1540 */
1541FILE *fopen_or_die(const char *path, const char *mode)
1542{
1543 FILE *filep = fopen(path, mode);
1544
1545 if (!filep)
1546 err(1, "%s: open failed", path);
1547 return filep;
1548}
1549/*
1550 * snapshot_sysfs_counter()
1551 *
1552 * return snapshot of given counter
1553 */
1554unsigned long long snapshot_sysfs_counter(char *path)
1555{
1556 FILE *fp;
1557 int retval;
1558 unsigned long long counter;
1559
1560 fp = fopen_or_die(path, "r");
1561
1562 retval = fscanf(fp, "%lld", &counter);
1563 if (retval != 1)
1564 err(1, "snapshot_sysfs_counter(%s)", path);
1565
1566 fclose(fp);
1567
1568 return counter;
1569}
1570
1571int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1572{
1573 if (mp->msr_num != 0) {
1574 if (get_msr(cpu, mp->msr_num, counterp))
1575 return -1;
1576 } else {
Len Brown46c27972017-12-08 17:38:17 -05001577 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001578
1579 if (mp->flags & SYSFS_PERCPU) {
1580 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1581 cpu, mp->path);
1582
1583 *counterp = snapshot_sysfs_counter(path);
1584 } else {
1585 *counterp = snapshot_sysfs_counter(mp->path);
1586 }
Len Brown495c76542017-02-08 02:41:51 -05001587 }
1588
1589 return 0;
1590}
1591
1592/*
Len Brownc98d5d92012-06-04 00:56:40 -04001593 * get_counters(...)
1594 * migrate to cpu
1595 * acquire and record local counters for that cpu
1596 */
1597int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1598{
1599 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001600 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001601 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001602 struct msr_counter *mp;
1603 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001604
Len Brownf4fdf2b2017-05-27 21:06:55 -07001605
1606 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1607
Len Browne52966c2012-11-08 22:38:05 -05001608 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001609 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001610 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001611 }
Len Brownc98d5d92012-06-04 00:56:40 -04001612
Len Brown0102b062016-02-27 03:11:29 -05001613retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001614 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1615
Len Brown812db3f2017-02-10 00:25:41 -05001616 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001617 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1618
1619 /*
1620 * The TSC, APERF and MPERF must be read together for
1621 * APERF/MPERF and MPERF/TSC to give accurate results.
1622 *
1623 * Unfortunately, APERF and MPERF are read by
1624 * individual system call, so delays may occur
1625 * between them. If the time to read them
1626 * varies by a large amount, we re-read them.
1627 */
1628
1629 /*
1630 * This initial dummy APERF read has been seen to
1631 * reduce jitter in the subsequent reads.
1632 */
1633
Len Brown9c63a652012-10-31 01:29:52 -04001634 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001635 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001636
1637 t->tsc = rdtsc(); /* re-read close to APERF */
1638
1639 tsc_before = t->tsc;
1640
1641 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1642 return -3;
1643
1644 tsc_between = rdtsc();
1645
Len Brown9c63a652012-10-31 01:29:52 -04001646 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001647 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001648
1649 tsc_after = rdtsc();
1650
1651 aperf_time = tsc_between - tsc_before;
1652 mperf_time = tsc_after - tsc_between;
1653
1654 /*
1655 * If the system call latency to read APERF and MPERF
1656 * differ by more than 2x, then try again.
1657 */
1658 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1659 aperf_mperf_retry_count++;
1660 if (aperf_mperf_retry_count < 5)
1661 goto retry;
1662 else
1663 warnx("cpu%d jitter %lld %lld",
1664 cpu, aperf_time, mperf_time);
1665 }
1666 aperf_mperf_retry_count = 0;
1667
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001668 t->aperf = t->aperf * aperf_mperf_multiplier;
1669 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001670 }
1671
Len Brown812db3f2017-02-10 00:25:41 -05001672 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001673 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001674 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001675 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1676 return -5;
1677 t->smi_count = msr & 0xFFFFFFFF;
1678 }
Len Brown0539ba112017-02-10 00:27:20 -05001679 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001680 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1681 return -6;
1682 }
1683
Len Brown388e9c82016-12-22 23:57:55 -05001684 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001685 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001686 return -10;
1687 }
1688
Len Brownc98d5d92012-06-04 00:56:40 -04001689 /* collect core counters only for 1st thread in core */
1690 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001691 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001692
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07001693 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001694 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1695 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001696 }
1697
Len Brown812db3f2017-02-10 00:25:41 -05001698 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001699 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1700 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001701 } else if (do_knl_cstates) {
1702 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1703 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001704 }
1705
Len Brown812db3f2017-02-10 00:25:41 -05001706 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001707 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1708 return -8;
1709
Len Brown0539ba112017-02-10 00:27:20 -05001710 if (DO_BIC(BIC_Mod_c6))
1711 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1712 return -8;
1713
Len Brown812db3f2017-02-10 00:25:41 -05001714 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001715 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1716 return -9;
1717 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1718 }
1719
Len Brown388e9c82016-12-22 23:57:55 -05001720 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001721 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001722 return -10;
1723 }
Len Brown889facb2012-11-08 00:48:57 -05001724
Len Brownc98d5d92012-06-04 00:56:40 -04001725 /* collect package counters only for 1st core in package */
1726 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001727 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001728
Len Browna99d8732017-05-20 20:11:55 -04001729 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001730 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1731 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001732 }
1733 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001734 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1735 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001736 }
1737 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001738 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1739 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001740 }
1741 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001742 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1743 return -13;
1744 }
Len Brown0f47c082017-01-27 00:50:45 -05001745 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001746 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1747 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001748 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba112017-02-10 00:27:20 -05001749 if (do_slm_cstates) {
1750 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1751 return -10;
1752 } else {
1753 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1754 return -10;
1755 }
1756 }
1757
Len Brown0f47c082017-01-27 00:50:45 -05001758 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001759 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1760 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001761 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001762 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1763 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001764 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001765 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1766 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001767 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001768 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1769 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001770 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001771 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1772 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001773
Len Brownbe0e54c2018-06-01 12:35:53 -04001774 if (DO_BIC(BIC_CPU_LPI))
1775 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1776 if (DO_BIC(BIC_SYS_LPI))
1777 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1778
Len Brown889facb2012-11-08 00:48:57 -05001779 if (do_rapl & RAPL_PKG) {
1780 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1781 return -13;
1782 p->energy_pkg = msr & 0xFFFFFFFF;
1783 }
Jacob Pan91484942016-06-16 09:48:20 -07001784 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001785 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1786 return -14;
1787 p->energy_cores = msr & 0xFFFFFFFF;
1788 }
1789 if (do_rapl & RAPL_DRAM) {
1790 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1791 return -15;
1792 p->energy_dram = msr & 0xFFFFFFFF;
1793 }
1794 if (do_rapl & RAPL_GFX) {
1795 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1796 return -16;
1797 p->energy_gfx = msr & 0xFFFFFFFF;
1798 }
1799 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1800 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1801 return -16;
1802 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1803 }
1804 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1805 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1806 return -16;
1807 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1808 }
Len Brown812db3f2017-02-10 00:25:41 -05001809 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001810 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1811 return -17;
1812 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1813 }
Len Brownfdf676e2016-02-27 01:28:12 -05001814
Len Brown812db3f2017-02-10 00:25:41 -05001815 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001816 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1817
Len Brown812db3f2017-02-10 00:25:41 -05001818 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001819 p->gfx_mhz = gfx_cur_mhz;
1820
Len Brown388e9c82016-12-22 23:57:55 -05001821 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001822 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001823 return -10;
1824 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001825done:
1826 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001827
Len Brown103a8fe2010-10-22 23:53:03 -04001828 return 0;
1829}
1830
Len Brownee7e38e2015-02-09 23:39:45 -05001831/*
1832 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1833 * If you change the values, note they are used both in comparisons
1834 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1835 */
1836
1837#define PCLUKN 0 /* Unknown */
1838#define PCLRSV 1 /* Reserved */
1839#define PCL__0 2 /* PC0 */
1840#define PCL__1 3 /* PC1 */
1841#define PCL__2 4 /* PC2 */
1842#define PCL__3 5 /* PC3 */
1843#define PCL__4 6 /* PC4 */
1844#define PCL__6 7 /* PC6 */
1845#define PCL_6N 8 /* PC6 No Retention */
1846#define PCL_6R 9 /* PC6 Retention */
1847#define PCL__7 10 /* PC7 */
1848#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001849#define PCL__8 12 /* PC8 */
1850#define PCL__9 13 /* PC9 */
1851#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001852
1853int pkg_cstate_limit = PCLUKN;
1854char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001855 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001856
Len Browne9257f52015-04-01 21:02:57 -04001857int 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};
1858int 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};
1859int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brown0539ba112017-02-10 00:27:20 -05001860int 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 -05001861int 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 -04001862int 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 -04001863int 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 +03001864int 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 -05001865
Len Browna2b7b742015-09-26 00:12:38 -04001866
1867static void
1868calculate_tsc_tweak()
1869{
Len Browna2b7b742015-09-26 00:12:38 -04001870 tsc_tweak = base_hz / tsc_hz;
1871}
1872
Len Brownfcd17212015-03-23 20:29:09 -04001873static void
1874dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001875{
1876 unsigned long long msr;
1877 unsigned int ratio;
1878
Len Brownec0adc52015-11-12 02:42:31 -05001879 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001880
Len Brownb7d8c142016-02-13 23:36:17 -05001881 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001882
Len Brown103a8fe2010-10-22 23:53:03 -04001883 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001884 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001885 ratio, bclk, ratio * bclk);
1886
1887 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001888 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001889 ratio, bclk, ratio * bclk);
1890
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001891 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001892 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001893 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001894
Len Brownfcd17212015-03-23 20:29:09 -04001895 return;
1896}
1897
1898static void
1899dump_hsw_turbo_ratio_limits(void)
1900{
1901 unsigned long long msr;
1902 unsigned int ratio;
1903
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001904 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001905
Len Brownb7d8c142016-02-13 23:36:17 -05001906 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001907
1908 ratio = (msr >> 8) & 0xFF;
1909 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001910 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001911 ratio, bclk, ratio * bclk);
1912
1913 ratio = (msr >> 0) & 0xFF;
1914 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001915 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001916 ratio, bclk, ratio * bclk);
1917 return;
1918}
1919
1920static void
1921dump_ivt_turbo_ratio_limits(void)
1922{
1923 unsigned long long msr;
1924 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001925
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001926 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001927
Len Brownb7d8c142016-02-13 23:36:17 -05001928 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001929
1930 ratio = (msr >> 56) & 0xFF;
1931 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001932 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001933 ratio, bclk, ratio * bclk);
1934
1935 ratio = (msr >> 48) & 0xFF;
1936 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001937 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001938 ratio, bclk, ratio * bclk);
1939
1940 ratio = (msr >> 40) & 0xFF;
1941 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001942 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001943 ratio, bclk, ratio * bclk);
1944
1945 ratio = (msr >> 32) & 0xFF;
1946 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001947 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001948 ratio, bclk, ratio * bclk);
1949
1950 ratio = (msr >> 24) & 0xFF;
1951 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001952 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001953 ratio, bclk, ratio * bclk);
1954
1955 ratio = (msr >> 16) & 0xFF;
1956 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001957 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001958 ratio, bclk, ratio * bclk);
1959
1960 ratio = (msr >> 8) & 0xFF;
1961 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001962 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001963 ratio, bclk, ratio * bclk);
1964
1965 ratio = (msr >> 0) & 0xFF;
1966 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001967 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001968 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001969 return;
1970}
Len Brown31e07522017-01-31 23:07:49 -05001971int has_turbo_ratio_group_limits(int family, int model)
1972{
1973
1974 if (!genuine_intel)
1975 return 0;
1976
1977 switch (model) {
1978 case INTEL_FAM6_ATOM_GOLDMONT:
1979 case INTEL_FAM6_SKYLAKE_X:
1980 case INTEL_FAM6_ATOM_DENVERTON:
1981 return 1;
1982 }
1983 return 0;
1984}
Len Brown6574a5d2012-09-21 00:01:31 -04001985
Len Brownfcd17212015-03-23 20:29:09 -04001986static void
Len Brown31e07522017-01-31 23:07:49 -05001987dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001988{
Len Brown31e07522017-01-31 23:07:49 -05001989 unsigned long long msr, core_counts;
1990 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001991
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001992 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001993 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001994
Len Brown31e07522017-01-31 23:07:49 -05001995 if (has_turbo_ratio_group_limits(family, model)) {
1996 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1997 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1998 } else {
1999 core_counts = 0x0807060504030201;
2000 }
2001
Len Brown6574a5d2012-09-21 00:01:31 -04002002 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002003 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002004 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002005 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2006 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002007
2008 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002009 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002010 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002011 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2012 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002013
2014 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002015 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002016 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002017 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2018 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002019
2020 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002021 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002022 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002023 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2024 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002025
Len Brown103a8fe2010-10-22 23:53:03 -04002026 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002027 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002028 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002029 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2030 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002031
2032 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002033 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002034 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002035 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2036 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002037
2038 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002039 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002040 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002041 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2042 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002043
2044 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002045 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002046 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002047 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2048 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002049 return;
2050}
Len Brown3a9a9412014-08-15 02:39:52 -04002051
Len Brownfcd17212015-03-23 20:29:09 -04002052static void
Len Brown0f7887c2017-01-12 23:49:18 -05002053dump_atom_turbo_ratio_limits(void)
2054{
2055 unsigned long long msr;
2056 unsigned int ratio;
2057
2058 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2059 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2060
2061 ratio = (msr >> 0) & 0x3F;
2062 if (ratio)
2063 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2064 ratio, bclk, ratio * bclk);
2065
2066 ratio = (msr >> 8) & 0x3F;
2067 if (ratio)
2068 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2069 ratio, bclk, ratio * bclk);
2070
2071 ratio = (msr >> 16) & 0x3F;
2072 if (ratio)
2073 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2074 ratio, bclk, ratio * bclk);
2075
2076 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2077 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2078
2079 ratio = (msr >> 24) & 0x3F;
2080 if (ratio)
2081 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2082 ratio, bclk, ratio * bclk);
2083
2084 ratio = (msr >> 16) & 0x3F;
2085 if (ratio)
2086 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2087 ratio, bclk, ratio * bclk);
2088
2089 ratio = (msr >> 8) & 0x3F;
2090 if (ratio)
2091 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2092 ratio, bclk, ratio * bclk);
2093
2094 ratio = (msr >> 0) & 0x3F;
2095 if (ratio)
2096 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2097 ratio, bclk, ratio * bclk);
2098}
2099
2100static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002101dump_knl_turbo_ratio_limits(void)
2102{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002103 const unsigned int buckets_no = 7;
2104
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002105 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002106 int delta_cores, delta_ratio;
2107 int i, b_nr;
2108 unsigned int cores[buckets_no];
2109 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002110
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002111 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002112
Len Brownb7d8c142016-02-13 23:36:17 -05002113 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002114 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002115
2116 /**
2117 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002118 * [0] -- Reserved
2119 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002120 * [15:8] -- Base value of freq ratio of bucket 1.
2121 * [20:16] -- +ve delta of number of active cores of bucket 2.
2122 * i.e. active cores of bucket 2 =
2123 * active cores of bucket 1 + delta
2124 * [23:21] -- Negative delta of freq ratio of bucket 2.
2125 * i.e. freq ratio of bucket 2 =
2126 * freq ratio of bucket 1 - delta
2127 * [28:24]-- +ve delta of number of active cores of bucket 3.
2128 * [31:29]-- -ve delta of freq ratio of bucket 3.
2129 * [36:32]-- +ve delta of number of active cores of bucket 4.
2130 * [39:37]-- -ve delta of freq ratio of bucket 4.
2131 * [44:40]-- +ve delta of number of active cores of bucket 5.
2132 * [47:45]-- -ve delta of freq ratio of bucket 5.
2133 * [52:48]-- +ve delta of number of active cores of bucket 6.
2134 * [55:53]-- -ve delta of freq ratio of bucket 6.
2135 * [60:56]-- +ve delta of number of active cores of bucket 7.
2136 * [63:61]-- -ve delta of freq ratio of bucket 7.
2137 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002138
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002139 b_nr = 0;
2140 cores[b_nr] = (msr & 0xFF) >> 1;
2141 ratio[b_nr] = (msr >> 8) & 0xFF;
2142
2143 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002144 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002145 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002146
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002147 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2148 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2149 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002150 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002151
2152 for (i = buckets_no - 1; i >= 0; i--)
2153 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002154 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002155 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002156 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002157}
2158
2159static void
Len Brownfcd17212015-03-23 20:29:09 -04002160dump_nhm_cst_cfg(void)
2161{
2162 unsigned long long msr;
2163
Len Brown1df2e552017-01-07 23:24:57 -05002164 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002165
Len Brown1df2e552017-01-07 23:24:57 -05002166 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002167
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002168 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002169 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2170 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2171 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2172 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2173 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002174 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002175 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002176
2177#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2178 if (has_automatic_cstate_conversion) {
2179 fprintf(outf, ", automatic c-state conversion=%s",
2180 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2181 }
2182
2183 fprintf(outf, ")\n");
2184
Len Brownfcd17212015-03-23 20:29:09 -04002185 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002186}
2187
Len Brown6fb31432015-06-17 16:23:45 -04002188static void
2189dump_config_tdp(void)
2190{
2191 unsigned long long msr;
2192
2193 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002194 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002195 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002196
2197 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002198 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002199 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002200 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2201 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2202 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2203 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002204 }
Len Brownb7d8c142016-02-13 23:36:17 -05002205 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002206
2207 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002208 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002209 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002210 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2211 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2212 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2213 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002214 }
Len Brownb7d8c142016-02-13 23:36:17 -05002215 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002216
2217 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002218 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002219 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002220 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2221 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2222 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002223
Len Brown6fb31432015-06-17 16:23:45 -04002224 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002225 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002226 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002227 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2228 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002229}
Len Brown5a634262016-04-06 17:15:55 -04002230
2231unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2232
2233void print_irtl(void)
2234{
2235 unsigned long long msr;
2236
2237 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2238 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2239 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2240 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2241
2242 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2243 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2244 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2245 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2246
2247 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2248 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2249 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2250 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2251
2252 if (!do_irtl_hsw)
2253 return;
2254
2255 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2256 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2257 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2258 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2259
2260 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2261 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2262 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2263 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2264
2265 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2266 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2267 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2268 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2269
2270}
Len Brown36229892016-02-26 20:51:02 -05002271void free_fd_percpu(void)
2272{
2273 int i;
2274
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002275 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002276 if (fd_percpu[i] != 0)
2277 close(fd_percpu[i]);
2278 }
2279
2280 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002281}
2282
Len Brownc98d5d92012-06-04 00:56:40 -04002283void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002284{
Len Brown0e2d8f02018-06-01 22:08:58 -04002285 int i;
2286
Len Brownc98d5d92012-06-04 00:56:40 -04002287 CPU_FREE(cpu_present_set);
2288 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002289 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002290
Len Brownc98d5d92012-06-04 00:56:40 -04002291 CPU_FREE(cpu_affinity_set);
2292 cpu_affinity_set = NULL;
2293 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002294
Len Brownc98d5d92012-06-04 00:56:40 -04002295 free(thread_even);
2296 free(core_even);
2297 free(package_even);
2298
2299 thread_even = NULL;
2300 core_even = NULL;
2301 package_even = NULL;
2302
2303 free(thread_odd);
2304 free(core_odd);
2305 free(package_odd);
2306
2307 thread_odd = NULL;
2308 core_odd = NULL;
2309 package_odd = NULL;
2310
2311 free(output_buffer);
2312 output_buffer = NULL;
2313 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002314
2315 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002316
2317 free(irq_column_2_cpu);
2318 free(irqs_per_cpu);
Len Brown0e2d8f02018-06-01 22:08:58 -04002319
2320 for (i = 0; i <= topo.max_cpu_num; ++i) {
2321 if (cpus[i].put_ids)
2322 CPU_FREE(cpus[i].put_ids);
2323 }
2324 free(cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002325}
2326
Josh Triplett57a42a32013-08-20 17:20:17 -07002327
2328/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002329 * Parse a file containing a single int.
2330 */
2331int parse_int_file(const char *fmt, ...)
2332{
2333 va_list args;
2334 char path[PATH_MAX];
2335 FILE *filep;
2336 int value;
2337
2338 va_start(args, fmt);
2339 vsnprintf(path, sizeof(path), fmt, args);
2340 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002341 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002342 if (fscanf(filep, "%d", &value) != 1)
2343 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002344 fclose(filep);
2345 return value;
2346}
2347
2348/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002349 * get_cpu_position_in_core(cpu)
2350 * return the position of the CPU among its HT siblings in the core
2351 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002352 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002353int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002354{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002355 char path[64];
2356 FILE *filep;
2357 int this_cpu;
2358 char character;
2359 int i;
2360
2361 sprintf(path,
2362 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2363 cpu);
2364 filep = fopen(path, "r");
2365 if (filep == NULL) {
2366 perror(path);
2367 exit(1);
2368 }
2369
2370 for (i = 0; i < topo.num_threads_per_core; i++) {
2371 fscanf(filep, "%d", &this_cpu);
2372 if (this_cpu == cpu) {
2373 fclose(filep);
2374 return i;
2375 }
2376
2377 /* Account for no separator after last thread*/
2378 if (i != (topo.num_threads_per_core - 1))
2379 fscanf(filep, "%c", &character);
2380 }
2381
2382 fclose(filep);
2383 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002384}
2385
Len Brownc98d5d92012-06-04 00:56:40 -04002386/*
2387 * cpu_is_first_core_in_package(cpu)
2388 * return 1 if given CPU is 1st core in package
2389 */
2390int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002391{
Josh Triplett95aebc42013-08-20 17:20:16 -07002392 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002393}
2394
2395int get_physical_package_id(int cpu)
2396{
Josh Triplett95aebc42013-08-20 17:20:16 -07002397 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002398}
2399
2400int get_core_id(int cpu)
2401{
Josh Triplett95aebc42013-08-20 17:20:16 -07002402 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002403}
2404
Prarit Bhargavaef605742018-06-01 10:04:30 -04002405void set_node_data(void)
2406{
2407 char path[80];
2408 FILE *filep;
2409 int pkg, node, cpu;
2410
2411 struct pkg_node_info {
2412 int count;
2413 int min;
2414 } *pni;
2415
2416 pni = calloc(topo.num_packages, sizeof(struct pkg_node_info));
2417 if (!pni)
2418 err(1, "calloc pkg_node_count");
2419
2420 for (pkg = 0; pkg < topo.num_packages; pkg++)
2421 pni[pkg].min = topo.num_cpus;
2422
2423 for (node = 0; node <= topo.max_node_num; node++) {
2424 /* find the "first" cpu in the node */
2425 sprintf(path, "/sys/bus/node/devices/node%d/cpulist", node);
2426 filep = fopen(path, "r");
2427 if (!filep)
2428 continue;
2429 fscanf(filep, "%d", &cpu);
2430 fclose(filep);
2431
2432 pkg = cpus[cpu].physical_package_id;
2433 pni[pkg].count++;
2434
2435 if (node < pni[pkg].min)
2436 pni[pkg].min = node;
2437 }
2438
2439 for (pkg = 0; pkg < topo.num_packages; pkg++)
2440 if (pni[pkg].count > topo.num_nodes_per_pkg)
2441 topo.num_nodes_per_pkg = pni[0].count;
2442
2443 for (cpu = 0; cpu < topo.num_cpus; cpu++) {
2444 pkg = cpus[cpu].physical_package_id;
2445 node = cpus[cpu].physical_node_id;
2446 cpus[cpu].logical_node_id = node - pni[pkg].min;
2447 }
2448 free(pni);
2449
2450}
2451
2452int get_physical_node_id(struct cpu_topology *thiscpu)
Len Brownc98d5d92012-06-04 00:56:40 -04002453{
2454 char path[80];
2455 FILE *filep;
Len Brown0e2d8f02018-06-01 22:08:58 -04002456 int i;
2457 int cpu = thiscpu->logical_cpu_id;
Len Brownc98d5d92012-06-04 00:56:40 -04002458
Len Brown0e2d8f02018-06-01 22:08:58 -04002459 for (i = 0; i <= topo.max_cpu_num; i++) {
2460 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2461 cpu, i);
2462 filep = fopen(path, "r");
2463 if (!filep)
2464 continue;
2465 fclose(filep);
2466 return i;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002467 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002468 return -1;
2469}
Len Brownc98d5d92012-06-04 00:56:40 -04002470
Len Brown0e2d8f02018-06-01 22:08:58 -04002471int get_thread_siblings(struct cpu_topology *thiscpu)
2472{
2473 char path[80], character;
2474 FILE *filep;
2475 unsigned long map;
2476 int shift, sib_core;
2477 int cpu = thiscpu->logical_cpu_id;
2478 int offset = topo.max_cpu_num + 1;
2479 size_t size;
2480
2481 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
2482 if (!thiscpu->put_ids)
2483 return -1;
2484
2485 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2486 CPU_ZERO_S(size, thiscpu->put_ids);
2487
2488 sprintf(path,
2489 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2490 filep = fopen_or_die(path, "r");
2491 do {
2492 offset -= BITMASK_SIZE;
2493 fscanf(filep, "%lx%c", &map, &character);
2494 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2495 if ((map >> shift) & 0x1) {
2496 sib_core = get_core_id(shift + offset);
2497 if (sib_core == thiscpu->physical_core_id)
2498 CPU_SET_S(shift + offset, size,
2499 thiscpu->put_ids);
2500 }
2501 }
2502 } while (!strncmp(&character, ",", 1));
Len Brownc98d5d92012-06-04 00:56:40 -04002503 fclose(filep);
Len Brown0e2d8f02018-06-01 22:08:58 -04002504
2505 return CPU_COUNT_S(size, thiscpu->put_ids);
Len Brownc98d5d92012-06-04 00:56:40 -04002506}
2507
Len Brown103a8fe2010-10-22 23:53:03 -04002508/*
Len Brownc98d5d92012-06-04 00:56:40 -04002509 * run func(thread, core, package) in topology order
2510 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002511 */
2512
Len Brownc98d5d92012-06-04 00:56:40 -04002513int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2514 struct pkg_data *, struct thread_data *, struct core_data *,
2515 struct pkg_data *), struct thread_data *thread_base,
2516 struct core_data *core_base, struct pkg_data *pkg_base,
2517 struct thread_data *thread_base2, struct core_data *core_base2,
2518 struct pkg_data *pkg_base2)
2519{
2520 int retval, pkg_no, core_no, thread_no;
2521
2522 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2523 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2524 for (thread_no = 0; thread_no <
2525 topo.num_threads_per_core; ++thread_no) {
2526 struct thread_data *t, *t2;
2527 struct core_data *c, *c2;
2528 struct pkg_data *p, *p2;
2529
2530 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2531
2532 if (cpu_is_not_present(t->cpu_id))
2533 continue;
2534
2535 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2536
2537 c = GET_CORE(core_base, core_no, pkg_no);
2538 c2 = GET_CORE(core_base2, core_no, pkg_no);
2539
2540 p = GET_PKG(pkg_base, pkg_no);
2541 p2 = GET_PKG(pkg_base2, pkg_no);
2542
2543 retval = func(t, c, p, t2, c2, p2);
2544 if (retval)
2545 return retval;
2546 }
2547 }
2548 }
2549 return 0;
2550}
2551
2552/*
2553 * run func(cpu) on every cpu in /proc/stat
2554 * return max_cpu number
2555 */
2556int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002557{
2558 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002559 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002560 int retval;
2561
Josh Triplett57a42a32013-08-20 17:20:17 -07002562 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002563
2564 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002565 if (retval != 0)
2566 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002567
Len Brownc98d5d92012-06-04 00:56:40 -04002568 while (1) {
2569 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 -04002570 if (retval != 1)
2571 break;
2572
Len Brownc98d5d92012-06-04 00:56:40 -04002573 retval = func(cpu_num);
2574 if (retval) {
2575 fclose(fp);
2576 return(retval);
2577 }
Len Brown103a8fe2010-10-22 23:53:03 -04002578 }
2579 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002580 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002581}
2582
2583void re_initialize(void)
2584{
Len Brownc98d5d92012-06-04 00:56:40 -04002585 free_all_buffers();
2586 setup_all_buffers();
2587 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002588}
2589
Prarit Bhargava843c5792018-06-01 10:04:28 -04002590void set_max_cpu_num(void)
2591{
2592 FILE *filep;
2593 unsigned long dummy;
2594
2595 topo.max_cpu_num = 0;
2596 filep = fopen_or_die(
2597 "/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2598 "r");
2599 while (fscanf(filep, "%lx,", &dummy) == 1)
Len Brown0e2d8f02018-06-01 22:08:58 -04002600 topo.max_cpu_num += BITMASK_SIZE;
Prarit Bhargava843c5792018-06-01 10:04:28 -04002601 fclose(filep);
2602 topo.max_cpu_num--; /* 0 based */
2603}
Len Brownc98d5d92012-06-04 00:56:40 -04002604
Len Brown103a8fe2010-10-22 23:53:03 -04002605/*
Len Brownc98d5d92012-06-04 00:56:40 -04002606 * count_cpus()
2607 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002608 */
Len Brownc98d5d92012-06-04 00:56:40 -04002609int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002610{
Prarit Bhargava843c5792018-06-01 10:04:28 -04002611 topo.num_cpus++;
Len Brownc98d5d92012-06-04 00:56:40 -04002612 return 0;
2613}
2614int mark_cpu_present(int cpu)
2615{
2616 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002617 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002618}
2619
Len Brown562a2d32016-02-26 23:48:05 -05002620/*
2621 * snapshot_proc_interrupts()
2622 *
2623 * read and record summary of /proc/interrupts
2624 *
2625 * return 1 if config change requires a restart, else return 0
2626 */
2627int snapshot_proc_interrupts(void)
2628{
2629 static FILE *fp;
2630 int column, retval;
2631
2632 if (fp == NULL)
2633 fp = fopen_or_die("/proc/interrupts", "r");
2634 else
2635 rewind(fp);
2636
2637 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2638 for (column = 0; column < topo.num_cpus; ++column) {
2639 int cpu_number;
2640
2641 retval = fscanf(fp, " CPU%d", &cpu_number);
2642 if (retval != 1)
2643 break;
2644
2645 if (cpu_number > topo.max_cpu_num) {
2646 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2647 return 1;
2648 }
2649
2650 irq_column_2_cpu[column] = cpu_number;
2651 irqs_per_cpu[cpu_number] = 0;
2652 }
2653
2654 /* read /proc/interrupt count lines and sum up irqs per cpu */
2655 while (1) {
2656 int column;
2657 char buf[64];
2658
2659 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2660 if (retval != 1)
2661 break;
2662
2663 /* read the count per cpu */
2664 for (column = 0; column < topo.num_cpus; ++column) {
2665
2666 int cpu_number, irq_count;
2667
2668 retval = fscanf(fp, " %d", &irq_count);
2669 if (retval != 1)
2670 break;
2671
2672 cpu_number = irq_column_2_cpu[column];
2673 irqs_per_cpu[cpu_number] += irq_count;
2674
2675 }
2676
2677 while (getc(fp) != '\n')
2678 ; /* flush interrupt description */
2679
2680 }
2681 return 0;
2682}
Len Brown27d47352016-02-27 00:37:54 -05002683/*
Len Brownfdf676e2016-02-27 01:28:12 -05002684 * snapshot_gfx_rc6_ms()
2685 *
2686 * record snapshot of
2687 * /sys/class/drm/card0/power/rc6_residency_ms
2688 *
2689 * return 1 if config change requires a restart, else return 0
2690 */
2691int snapshot_gfx_rc6_ms(void)
2692{
2693 FILE *fp;
2694 int retval;
2695
2696 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2697
2698 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2699 if (retval != 1)
2700 err(1, "GFX rc6");
2701
2702 fclose(fp);
2703
2704 return 0;
2705}
2706/*
Len Brown27d47352016-02-27 00:37:54 -05002707 * snapshot_gfx_mhz()
2708 *
2709 * record snapshot of
2710 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2711 *
2712 * return 1 if config change requires a restart, else return 0
2713 */
2714int snapshot_gfx_mhz(void)
2715{
2716 static FILE *fp;
2717 int retval;
2718
2719 if (fp == NULL)
2720 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002721 else {
Len Brown27d47352016-02-27 00:37:54 -05002722 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002723 fflush(fp);
2724 }
Len Brown27d47352016-02-27 00:37:54 -05002725
2726 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2727 if (retval != 1)
2728 err(1, "GFX MHz");
2729
2730 return 0;
2731}
Len Brown562a2d32016-02-26 23:48:05 -05002732
2733/*
Len Brownbe0e54c2018-06-01 12:35:53 -04002734 * snapshot_cpu_lpi()
2735 *
2736 * record snapshot of
2737 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2738 *
2739 * return 1 if config change requires a restart, else return 0
2740 */
2741int snapshot_cpu_lpi_us(void)
2742{
2743 FILE *fp;
2744 int retval;
2745
2746 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2747
2748 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2749 if (retval != 1)
2750 err(1, "CPU LPI");
2751
2752 fclose(fp);
2753
2754 return 0;
2755}
2756/*
2757 * snapshot_sys_lpi()
2758 *
2759 * record snapshot of
2760 * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2761 *
2762 * return 1 if config change requires a restart, else return 0
2763 */
2764int snapshot_sys_lpi_us(void)
2765{
2766 FILE *fp;
2767 int retval;
2768
2769 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2770
2771 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2772 if (retval != 1)
2773 err(1, "SYS LPI");
2774
2775 fclose(fp);
2776
2777 return 0;
2778}
2779/*
Len Brown562a2d32016-02-26 23:48:05 -05002780 * snapshot /proc and /sys files
2781 *
2782 * return 1 if configuration restart needed, else return 0
2783 */
2784int snapshot_proc_sysfs_files(void)
2785{
Len Brown218f0e82017-02-14 22:07:52 -05002786 if (DO_BIC(BIC_IRQ))
2787 if (snapshot_proc_interrupts())
2788 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002789
Len Brown812db3f2017-02-10 00:25:41 -05002790 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002791 snapshot_gfx_rc6_ms();
2792
Len Brown812db3f2017-02-10 00:25:41 -05002793 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002794 snapshot_gfx_mhz();
2795
Len Brownbe0e54c2018-06-01 12:35:53 -04002796 if (DO_BIC(BIC_CPU_LPI))
2797 snapshot_cpu_lpi_us();
2798
2799 if (DO_BIC(BIC_SYS_LPI))
2800 snapshot_sys_lpi_us();
2801
Len Brown562a2d32016-02-26 23:48:05 -05002802 return 0;
2803}
2804
Len Brown8aa2ed02017-07-15 14:57:37 -04002805int exit_requested;
2806
2807static void signal_handler (int signal)
2808{
2809 switch (signal) {
2810 case SIGINT:
2811 exit_requested = 1;
2812 if (debug)
2813 fprintf(stderr, " SIGINT\n");
2814 break;
Len Brown07211962017-07-15 15:51:26 -04002815 case SIGUSR1:
2816 if (debug > 1)
2817 fprintf(stderr, "SIGUSR1\n");
2818 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002819 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002820 /* make sure this manually-invoked interval is at least 1ms long */
2821 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002822}
2823
2824void setup_signal_handler(void)
2825{
2826 struct sigaction sa;
2827
2828 memset(&sa, 0, sizeof(sa));
2829
2830 sa.sa_handler = &signal_handler;
2831
2832 if (sigaction(SIGINT, &sa, NULL) < 0)
2833 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002834 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2835 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002836}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002837
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002838void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002839{
2840 struct timeval select_timeout;
2841 fd_set readfds;
2842 int retval;
2843
2844 FD_ZERO(&readfds);
2845 FD_SET(0, &readfds);
2846
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002847 if (!isatty(fileno(stdin))) {
2848 nanosleep(&interval_ts, NULL);
2849 return;
2850 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002851
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002852 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002853 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2854
2855 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002856 switch (getc(stdin)) {
2857 case 'q':
2858 exit_requested = 1;
2859 break;
2860 }
2861 /* make sure this manually-invoked interval is at least 1ms long */
2862 nanosleep(&one_msec, NULL);
2863 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002864}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002865
Len Brown103a8fe2010-10-22 23:53:03 -04002866void turbostat_loop()
2867{
Len Brownc98d5d92012-06-04 00:56:40 -04002868 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002869 int restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002870 int done_iters = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002871
Len Brown8aa2ed02017-07-15 14:57:37 -04002872 setup_signal_handler();
2873
Len Brown103a8fe2010-10-22 23:53:03 -04002874restart:
Len Browne52966c2012-11-08 22:38:05 -05002875 restarted++;
2876
Len Brown562a2d32016-02-26 23:48:05 -05002877 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002878 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002879 if (retval < -1) {
2880 exit(retval);
2881 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002882 if (restarted > 1) {
2883 exit(retval);
2884 }
Len Brownc98d5d92012-06-04 00:56:40 -04002885 re_initialize();
2886 goto restart;
2887 }
Len Browne52966c2012-11-08 22:38:05 -05002888 restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002889 done_iters = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002890 gettimeofday(&tv_even, (struct timezone *)NULL);
2891
2892 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002893 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002894 re_initialize();
2895 goto restart;
2896 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002897 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002898 if (snapshot_proc_sysfs_files())
2899 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002900 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002901 if (retval < -1) {
2902 exit(retval);
2903 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002904 re_initialize();
2905 goto restart;
2906 }
Len Brown103a8fe2010-10-22 23:53:03 -04002907 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002908 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002909 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2910 re_initialize();
2911 goto restart;
2912 }
Len Brownc98d5d92012-06-04 00:56:40 -04002913 compute_average(EVEN_COUNTERS);
2914 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002915 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002916 if (exit_requested)
2917 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002918 if (num_iterations && ++done_iters >= num_iterations)
2919 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002920 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002921 if (snapshot_proc_sysfs_files())
2922 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002923 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002924 if (retval < -1) {
2925 exit(retval);
2926 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002927 re_initialize();
2928 goto restart;
2929 }
Len Brown103a8fe2010-10-22 23:53:03 -04002930 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002931 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002932 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2933 re_initialize();
2934 goto restart;
2935 }
Len Brownc98d5d92012-06-04 00:56:40 -04002936 compute_average(ODD_COUNTERS);
2937 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002938 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002939 if (exit_requested)
2940 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002941 if (num_iterations && ++done_iters >= num_iterations)
2942 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002943 }
2944}
2945
2946void check_dev_msr()
2947{
2948 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002949 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002950
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002951 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2952 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002953 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2954 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002955}
2956
Len Brown98481e72014-08-15 00:36:50 -04002957void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002958{
Len Brown98481e72014-08-15 00:36:50 -04002959 struct __user_cap_header_struct cap_header_data;
2960 cap_user_header_t cap_header = &cap_header_data;
2961 struct __user_cap_data_struct cap_data_data;
2962 cap_user_data_t cap_data = &cap_data_data;
2963 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2964 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002965 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002966
2967 /* check for CAP_SYS_RAWIO */
2968 cap_header->pid = getpid();
2969 cap_header->version = _LINUX_CAPABILITY_VERSION;
2970 if (capget(cap_header, cap_data) < 0)
2971 err(-6, "capget(2) failed");
2972
2973 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2974 do_exit++;
2975 warnx("capget(CAP_SYS_RAWIO) failed,"
2976 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2977 }
2978
2979 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002980 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2981 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002982 do_exit++;
2983 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2984 }
2985
2986 /* if all else fails, thell them to be root */
2987 if (do_exit)
2988 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002989 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002990
2991 if (do_exit)
2992 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002993}
2994
Len Brownd7899442015-01-23 00:12:33 -05002995/*
2996 * NHM adds support for additional MSRs:
2997 *
2998 * MSR_SMI_COUNT 0x00000034
2999 *
Len Brownec0adc52015-11-12 02:42:31 -05003000 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05003001 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05003002 *
Len Browncf4cbe52017-01-01 13:08:33 -05003003 * MSR_MISC_PWR_MGMT 0x000001aa
3004 *
Len Brownd7899442015-01-23 00:12:33 -05003005 * MSR_PKG_C3_RESIDENCY 0x000003f8
3006 * MSR_PKG_C6_RESIDENCY 0x000003f9
3007 * MSR_CORE_C3_RESIDENCY 0x000003fc
3008 * MSR_CORE_C6_RESIDENCY 0x000003fd
3009 *
Len Brownee7e38e2015-02-09 23:39:45 -05003010 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05003011 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05003012 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05003013 */
Len Brownee7e38e2015-02-09 23:39:45 -05003014int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003015{
Len Brownee7e38e2015-02-09 23:39:45 -05003016 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04003017 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05003018 int *pkg_cstate_limits;
3019
Len Brown103a8fe2010-10-22 23:53:03 -04003020 if (!genuine_intel)
3021 return 0;
3022
3023 if (family != 6)
3024 return 0;
3025
Len Brown21ed5572015-10-19 22:37:40 -04003026 bclk = discover_bclk(family, model);
3027
Len Brown103a8fe2010-10-22 23:53:03 -04003028 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003029 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3030 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04003031 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce69e2016-06-16 23:22:37 -04003032 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
3033 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
3034 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3035 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05003036 pkg_cstate_limits = nhm_pkg_cstate_limits;
3037 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003038 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3039 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3040 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3041 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05003042 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003043 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003044 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003045 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3046 case INTEL_FAM6_HASWELL_X: /* HSX */
3047 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3048 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3049 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3050 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3051 case INTEL_FAM6_BROADWELL_X: /* BDX */
3052 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3053 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3054 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3055 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3056 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003057 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brownee7e38e2015-02-09 23:39:45 -05003058 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003059 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003060 break;
Len Brownd8ebb442016-12-01 20:27:46 -05003061 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3062 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003063 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05003064 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003065 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05003066 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04003067 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05003068 pkg_cstate_limits = slv_pkg_cstate_limits;
3069 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003070 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05003071 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05003072 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003073 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003074 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05003075 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05003076 pkg_cstate_limits = phi_pkg_cstate_limits;
3077 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003078 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003079 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce69e2016-06-16 23:22:37 -04003080 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04003081 pkg_cstate_limits = bxt_pkg_cstate_limits;
3082 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003083 default:
3084 return 0;
3085 }
Len Brown1df2e552017-01-07 23:24:57 -05003086 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04003087 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05003088
Len Brownec0adc52015-11-12 02:42:31 -05003089 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04003090 base_ratio = (msr >> 8) & 0xFF;
3091
3092 base_hz = base_ratio * bclk * 1000000;
3093 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003094 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04003095}
Len Brown0f7887c2017-01-12 23:49:18 -05003096/*
Len Brown495c76542017-02-08 02:41:51 -05003097 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05003098 *
3099 * MSR_CC6_DEMOTION_POLICY_CONFIG
3100 * MSR_MC6_DEMOTION_POLICY_CONFIG
3101 */
3102
3103int has_slv_msrs(unsigned int family, unsigned int model)
3104{
3105 if (!genuine_intel)
3106 return 0;
3107
3108 switch (model) {
3109 case INTEL_FAM6_ATOM_SILVERMONT1:
3110 case INTEL_FAM6_ATOM_MERRIFIELD:
3111 case INTEL_FAM6_ATOM_MOOREFIELD:
3112 return 1;
3113 }
3114 return 0;
3115}
Len Brown7170a372017-01-27 02:13:27 -05003116int is_dnv(unsigned int family, unsigned int model)
3117{
3118
3119 if (!genuine_intel)
3120 return 0;
3121
3122 switch (model) {
3123 case INTEL_FAM6_ATOM_DENVERTON:
3124 return 1;
3125 }
3126 return 0;
3127}
Len Brownade0eba2017-02-10 01:56:47 -05003128int is_bdx(unsigned int family, unsigned int model)
3129{
3130
3131 if (!genuine_intel)
3132 return 0;
3133
3134 switch (model) {
3135 case INTEL_FAM6_BROADWELL_X:
3136 case INTEL_FAM6_BROADWELL_XEON_D:
3137 return 1;
3138 }
3139 return 0;
3140}
Len Brown34c761972017-01-27 02:36:41 -05003141int is_skx(unsigned int family, unsigned int model)
3142{
3143
3144 if (!genuine_intel)
3145 return 0;
3146
3147 switch (model) {
3148 case INTEL_FAM6_SKYLAKE_X:
3149 return 1;
3150 }
3151 return 0;
3152}
Len Brown0f7887c2017-01-12 23:49:18 -05003153
Len Brown31e07522017-01-31 23:07:49 -05003154int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05003155{
Len Brown0f7887c2017-01-12 23:49:18 -05003156 if (has_slv_msrs(family, model))
3157 return 0;
3158
Len Brownd7899442015-01-23 00:12:33 -05003159 switch (model) {
3160 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce69e2016-06-16 23:22:37 -04003161 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3162 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3163 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05003164 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05003165 return 0;
3166 default:
3167 return 1;
3168 }
3169}
Len Brown0f7887c2017-01-12 23:49:18 -05003170int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3171{
3172 if (has_slv_msrs(family, model))
3173 return 1;
3174
3175 return 0;
3176}
Len Brown6574a5d2012-09-21 00:01:31 -04003177int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3178{
3179 if (!genuine_intel)
3180 return 0;
3181
3182 if (family != 6)
3183 return 0;
3184
3185 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003186 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3187 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04003188 return 1;
3189 default:
3190 return 0;
3191 }
3192}
Len Brownfcd17212015-03-23 20:29:09 -04003193int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3194{
3195 if (!genuine_intel)
3196 return 0;
3197
3198 if (family != 6)
3199 return 0;
3200
3201 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003202 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003203 return 1;
3204 default:
3205 return 0;
3206 }
3207}
3208
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003209int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3210{
3211 if (!genuine_intel)
3212 return 0;
3213
3214 if (family != 6)
3215 return 0;
3216
3217 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003218 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003219 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003220 return 1;
3221 default:
3222 return 0;
3223 }
3224}
Len Brown31e07522017-01-31 23:07:49 -05003225int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3226{
3227 if (!genuine_intel)
3228 return 0;
3229
3230 if (family != 6)
3231 return 0;
3232
3233 switch (model) {
3234 case INTEL_FAM6_ATOM_GOLDMONT:
3235 case INTEL_FAM6_SKYLAKE_X:
3236 return 1;
3237 default:
3238 return 0;
3239 }
3240}
Len Brown6fb31432015-06-17 16:23:45 -04003241int has_config_tdp(unsigned int family, unsigned int model)
3242{
3243 if (!genuine_intel)
3244 return 0;
3245
3246 if (family != 6)
3247 return 0;
3248
3249 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003250 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3251 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3252 case INTEL_FAM6_HASWELL_X: /* HSX */
3253 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3254 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3255 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3256 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3257 case INTEL_FAM6_BROADWELL_X: /* BDX */
3258 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3259 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3260 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3261 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3262 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003263 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce69e2016-06-16 23:22:37 -04003264 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003265
Len Brown869ce69e2016-06-16 23:22:37 -04003266 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003267 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003268 return 1;
3269 default:
3270 return 0;
3271 }
3272}
3273
Len Brownfcd17212015-03-23 20:29:09 -04003274static void
Colin Ian King1b693172016-03-02 13:50:25 +00003275dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003276{
3277 if (!do_nhm_platform_info)
3278 return;
3279
3280 dump_nhm_platform_info();
3281
3282 if (has_hsw_turbo_ratio_limit(family, model))
3283 dump_hsw_turbo_ratio_limits();
3284
3285 if (has_ivt_turbo_ratio_limit(family, model))
3286 dump_ivt_turbo_ratio_limits();
3287
Len Brown31e07522017-01-31 23:07:49 -05003288 if (has_turbo_ratio_limit(family, model))
3289 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003290
Len Brown0f7887c2017-01-12 23:49:18 -05003291 if (has_atom_turbo_ratio_limit(family, model))
3292 dump_atom_turbo_ratio_limits();
3293
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003294 if (has_knl_turbo_ratio_limit(family, model))
3295 dump_knl_turbo_ratio_limits();
3296
Len Brown6fb31432015-06-17 16:23:45 -04003297 if (has_config_tdp(family, model))
3298 dump_config_tdp();
3299
Len Brownfcd17212015-03-23 20:29:09 -04003300 dump_nhm_cst_cfg();
3301}
3302
Len Brown41618e62017-02-09 18:25:22 -05003303static void
3304dump_sysfs_cstate_config(void)
3305{
3306 char path[64];
3307 char name_buf[16];
3308 char desc[64];
3309 FILE *input;
3310 int state;
3311 char *sp;
3312
3313 if (!DO_BIC(BIC_sysfs))
3314 return;
3315
3316 for (state = 0; state < 10; ++state) {
3317
3318 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3319 base_cpu, state);
3320 input = fopen(path, "r");
3321 if (input == NULL)
3322 continue;
3323 fgets(name_buf, sizeof(name_buf), input);
3324
3325 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3326 sp = strchr(name_buf, '-');
3327 if (!sp)
3328 sp = strchrnul(name_buf, '\n');
3329 *sp = '\0';
3330
3331 fclose(input);
3332
3333 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3334 base_cpu, state);
3335 input = fopen(path, "r");
3336 if (input == NULL)
3337 continue;
3338 fgets(desc, sizeof(desc), input);
3339
3340 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3341 fclose(input);
3342 }
3343}
Len Brown7293fcc2017-02-22 00:11:12 -05003344static void
3345dump_sysfs_pstate_config(void)
3346{
3347 char path[64];
3348 char driver_buf[64];
3349 char governor_buf[64];
3350 FILE *input;
3351 int turbo;
3352
3353 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3354 base_cpu);
3355 input = fopen(path, "r");
3356 if (input == NULL) {
3357 fprintf(stderr, "NSFOD %s\n", path);
3358 return;
3359 }
3360 fgets(driver_buf, sizeof(driver_buf), input);
3361 fclose(input);
3362
3363 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3364 base_cpu);
3365 input = fopen(path, "r");
3366 if (input == NULL) {
3367 fprintf(stderr, "NSFOD %s\n", path);
3368 return;
3369 }
3370 fgets(governor_buf, sizeof(governor_buf), input);
3371 fclose(input);
3372
3373 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3374 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3375
3376 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3377 input = fopen(path, "r");
3378 if (input != NULL) {
3379 fscanf(input, "%d", &turbo);
3380 fprintf(outf, "cpufreq boost: %d\n", turbo);
3381 fclose(input);
3382 }
3383
3384 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3385 input = fopen(path, "r");
3386 if (input != NULL) {
3387 fscanf(input, "%d", &turbo);
3388 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3389 fclose(input);
3390 }
3391}
Len Brown41618e62017-02-09 18:25:22 -05003392
Len Brown6574a5d2012-09-21 00:01:31 -04003393
Len Brown889facb2012-11-08 00:48:57 -05003394/*
3395 * print_epb()
3396 * Decode the ENERGY_PERF_BIAS MSR
3397 */
3398int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3399{
3400 unsigned long long msr;
3401 char *epb_string;
3402 int cpu;
3403
3404 if (!has_epb)
3405 return 0;
3406
3407 cpu = t->cpu_id;
3408
3409 /* EPB is per-package */
3410 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3411 return 0;
3412
3413 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003414 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003415 return -1;
3416 }
3417
3418 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3419 return 0;
3420
Len Browne9be7dd2015-05-26 12:19:37 -04003421 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003422 case ENERGY_PERF_BIAS_PERFORMANCE:
3423 epb_string = "performance";
3424 break;
3425 case ENERGY_PERF_BIAS_NORMAL:
3426 epb_string = "balanced";
3427 break;
3428 case ENERGY_PERF_BIAS_POWERSAVE:
3429 epb_string = "powersave";
3430 break;
3431 default:
3432 epb_string = "custom";
3433 break;
3434 }
Len Brownb7d8c142016-02-13 23:36:17 -05003435 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003436
3437 return 0;
3438}
Len Brown7f5c2582015-12-01 01:36:39 -05003439/*
3440 * print_hwp()
3441 * Decode the MSR_HWP_CAPABILITIES
3442 */
3443int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3444{
3445 unsigned long long msr;
3446 int cpu;
3447
3448 if (!has_hwp)
3449 return 0;
3450
3451 cpu = t->cpu_id;
3452
3453 /* MSR_HWP_CAPABILITIES is per-package */
3454 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3455 return 0;
3456
3457 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003458 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003459 return -1;
3460 }
3461
3462 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3463 return 0;
3464
Len Brownb7d8c142016-02-13 23:36:17 -05003465 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003466 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3467
3468 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3469 if ((msr & (1 << 0)) == 0)
3470 return 0;
3471
3472 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3473 return 0;
3474
Len Brownb7d8c142016-02-13 23:36:17 -05003475 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003476 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003477 cpu, msr,
3478 (unsigned int)HWP_HIGHEST_PERF(msr),
3479 (unsigned int)HWP_GUARANTEED_PERF(msr),
3480 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3481 (unsigned int)HWP_LOWEST_PERF(msr));
3482
3483 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3484 return 0;
3485
Len Brownb7d8c142016-02-13 23:36:17 -05003486 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003487 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003488 cpu, msr,
3489 (unsigned int)(((msr) >> 0) & 0xff),
3490 (unsigned int)(((msr) >> 8) & 0xff),
3491 (unsigned int)(((msr) >> 16) & 0xff),
3492 (unsigned int)(((msr) >> 24) & 0xff),
3493 (unsigned int)(((msr) >> 32) & 0xff3),
3494 (unsigned int)(((msr) >> 42) & 0x1));
3495
3496 if (has_hwp_pkg) {
3497 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3498 return 0;
3499
Len Brownb7d8c142016-02-13 23:36:17 -05003500 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003501 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003502 cpu, msr,
3503 (unsigned int)(((msr) >> 0) & 0xff),
3504 (unsigned int)(((msr) >> 8) & 0xff),
3505 (unsigned int)(((msr) >> 16) & 0xff),
3506 (unsigned int)(((msr) >> 24) & 0xff),
3507 (unsigned int)(((msr) >> 32) & 0xff3));
3508 }
3509 if (has_hwp_notify) {
3510 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3511 return 0;
3512
Len Brownb7d8c142016-02-13 23:36:17 -05003513 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003514 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3515 cpu, msr,
3516 ((msr) & 0x1) ? "EN" : "Dis",
3517 ((msr) & 0x2) ? "EN" : "Dis");
3518 }
3519 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3520 return 0;
3521
Len Brownb7d8c142016-02-13 23:36:17 -05003522 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003523 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3524 cpu, msr,
3525 ((msr) & 0x1) ? "" : "No-",
3526 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003527
3528 return 0;
3529}
3530
Len Brown3a9a9412014-08-15 02:39:52 -04003531/*
3532 * print_perf_limit()
3533 */
3534int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3535{
3536 unsigned long long msr;
3537 int cpu;
3538
3539 cpu = t->cpu_id;
3540
3541 /* per-package */
3542 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3543 return 0;
3544
3545 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003546 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003547 return -1;
3548 }
3549
3550 if (do_core_perf_limit_reasons) {
3551 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003552 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3553 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003554 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003555 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003556 (msr & 1 << 13) ? "Transitions, " : "",
3557 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3558 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3559 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3560 (msr & 1 << 9) ? "CorePwr, " : "",
3561 (msr & 1 << 8) ? "Amps, " : "",
3562 (msr & 1 << 6) ? "VR-Therm, " : "",
3563 (msr & 1 << 5) ? "Auto-HWP, " : "",
3564 (msr & 1 << 4) ? "Graphics, " : "",
3565 (msr & 1 << 2) ? "bit2, " : "",
3566 (msr & 1 << 1) ? "ThermStatus, " : "",
3567 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003568 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 -04003569 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003570 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003571 (msr & 1 << 29) ? "Transitions, " : "",
3572 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3573 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3574 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3575 (msr & 1 << 25) ? "CorePwr, " : "",
3576 (msr & 1 << 24) ? "Amps, " : "",
3577 (msr & 1 << 22) ? "VR-Therm, " : "",
3578 (msr & 1 << 21) ? "Auto-HWP, " : "",
3579 (msr & 1 << 20) ? "Graphics, " : "",
3580 (msr & 1 << 18) ? "bit18, " : "",
3581 (msr & 1 << 17) ? "ThermStatus, " : "",
3582 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003583
3584 }
3585 if (do_gfx_perf_limit_reasons) {
3586 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003587 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3588 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003589 (msr & 1 << 0) ? "PROCHOT, " : "",
3590 (msr & 1 << 1) ? "ThermStatus, " : "",
3591 (msr & 1 << 4) ? "Graphics, " : "",
3592 (msr & 1 << 6) ? "VR-Therm, " : "",
3593 (msr & 1 << 8) ? "Amps, " : "",
3594 (msr & 1 << 9) ? "GFXPwr, " : "",
3595 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3596 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003597 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003598 (msr & 1 << 16) ? "PROCHOT, " : "",
3599 (msr & 1 << 17) ? "ThermStatus, " : "",
3600 (msr & 1 << 20) ? "Graphics, " : "",
3601 (msr & 1 << 22) ? "VR-Therm, " : "",
3602 (msr & 1 << 24) ? "Amps, " : "",
3603 (msr & 1 << 25) ? "GFXPwr, " : "",
3604 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3605 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3606 }
3607 if (do_ring_perf_limit_reasons) {
3608 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003609 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3610 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003611 (msr & 1 << 0) ? "PROCHOT, " : "",
3612 (msr & 1 << 1) ? "ThermStatus, " : "",
3613 (msr & 1 << 6) ? "VR-Therm, " : "",
3614 (msr & 1 << 8) ? "Amps, " : "",
3615 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3616 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003617 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003618 (msr & 1 << 16) ? "PROCHOT, " : "",
3619 (msr & 1 << 17) ? "ThermStatus, " : "",
3620 (msr & 1 << 22) ? "VR-Therm, " : "",
3621 (msr & 1 << 24) ? "Amps, " : "",
3622 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3623 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3624 }
3625 return 0;
3626}
3627
Len Brown889facb2012-11-08 00:48:57 -05003628#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3629#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3630
Colin Ian King1b693172016-03-02 13:50:25 +00003631double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003632{
3633 unsigned long long msr;
3634
3635 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003636 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003637 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3638
3639 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003640 case INTEL_FAM6_ATOM_SILVERMONT1:
3641 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003642 return 30.0;
3643 default:
3644 return 135.0;
3645 }
3646}
3647
Andrey Semin40ee8e32014-12-05 00:07:00 -05003648/*
3649 * rapl_dram_energy_units_probe()
3650 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3651 */
3652static double
3653rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3654{
3655 /* only called for genuine_intel, family 6 */
3656
3657 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003658 case INTEL_FAM6_HASWELL_X: /* HSX */
3659 case INTEL_FAM6_BROADWELL_X: /* BDX */
3660 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3661 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003662 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003663 return (rapl_dram_energy_units = 15.3 / 1000000);
3664 default:
3665 return (rapl_energy_units);
3666 }
3667}
3668
Len Brown144b44b2013-11-09 00:30:16 -05003669
Len Brown889facb2012-11-08 00:48:57 -05003670/*
3671 * rapl_probe()
3672 *
Len Brown144b44b2013-11-09 00:30:16 -05003673 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003674 */
3675void rapl_probe(unsigned int family, unsigned int model)
3676{
3677 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003678 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003679 double tdp;
3680
3681 if (!genuine_intel)
3682 return;
3683
3684 if (family != 6)
3685 return;
3686
3687 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003688 case INTEL_FAM6_SANDYBRIDGE:
3689 case INTEL_FAM6_IVYBRIDGE:
3690 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3691 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3692 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3693 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3694 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003695 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003696 if (rapl_joules) {
3697 BIC_PRESENT(BIC_Pkg_J);
3698 BIC_PRESENT(BIC_Cor_J);
3699 BIC_PRESENT(BIC_GFX_J);
3700 } else {
3701 BIC_PRESENT(BIC_PkgWatt);
3702 BIC_PRESENT(BIC_CorWatt);
3703 BIC_PRESENT(BIC_GFXWatt);
3704 }
Len Brown889facb2012-11-08 00:48:57 -05003705 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003706 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003707 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003708 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003709 if (rapl_joules)
3710 BIC_PRESENT(BIC_Pkg_J);
3711 else
3712 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003713 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003714 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3715 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3716 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3717 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003718 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown81824922017-03-04 17:23:07 -05003719 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 -05003720 BIC_PRESENT(BIC_PKG__);
3721 BIC_PRESENT(BIC_RAM__);
3722 if (rapl_joules) {
3723 BIC_PRESENT(BIC_Pkg_J);
3724 BIC_PRESENT(BIC_Cor_J);
3725 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003726 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003727 } else {
3728 BIC_PRESENT(BIC_PkgWatt);
3729 BIC_PRESENT(BIC_CorWatt);
3730 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003731 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003732 }
Len Brown0b2bb692015-03-26 00:50:30 -04003733 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003734 case INTEL_FAM6_HASWELL_X: /* HSX */
3735 case INTEL_FAM6_BROADWELL_X: /* BDX */
3736 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3737 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3738 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003739 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003740 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 -05003741 BIC_PRESENT(BIC_PKG__);
3742 BIC_PRESENT(BIC_RAM__);
3743 if (rapl_joules) {
3744 BIC_PRESENT(BIC_Pkg_J);
3745 BIC_PRESENT(BIC_RAM_J);
3746 } else {
3747 BIC_PRESENT(BIC_PkgWatt);
3748 BIC_PRESENT(BIC_RAMWatt);
3749 }
Len Browne6f9bb32013-12-03 02:19:19 -05003750 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003751 case INTEL_FAM6_SANDYBRIDGE_X:
3752 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003753 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 -05003754 BIC_PRESENT(BIC_PKG__);
3755 BIC_PRESENT(BIC_RAM__);
3756 if (rapl_joules) {
3757 BIC_PRESENT(BIC_Pkg_J);
3758 BIC_PRESENT(BIC_Cor_J);
3759 BIC_PRESENT(BIC_RAM_J);
3760 } else {
3761 BIC_PRESENT(BIC_PkgWatt);
3762 BIC_PRESENT(BIC_CorWatt);
3763 BIC_PRESENT(BIC_RAMWatt);
3764 }
Len Brown144b44b2013-11-09 00:30:16 -05003765 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003766 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3767 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003768 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003769 if (rapl_joules) {
3770 BIC_PRESENT(BIC_Pkg_J);
3771 BIC_PRESENT(BIC_Cor_J);
3772 } else {
3773 BIC_PRESENT(BIC_PkgWatt);
3774 BIC_PRESENT(BIC_CorWatt);
3775 }
Len Brown889facb2012-11-08 00:48:57 -05003776 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003777 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003778 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 -05003779 BIC_PRESENT(BIC_PKG__);
3780 BIC_PRESENT(BIC_RAM__);
3781 if (rapl_joules) {
3782 BIC_PRESENT(BIC_Pkg_J);
3783 BIC_PRESENT(BIC_Cor_J);
3784 BIC_PRESENT(BIC_RAM_J);
3785 } else {
3786 BIC_PRESENT(BIC_PkgWatt);
3787 BIC_PRESENT(BIC_CorWatt);
3788 BIC_PRESENT(BIC_RAMWatt);
3789 }
Jacob Pan0f644902016-06-16 09:48:22 -07003790 break;
Len Brown889facb2012-11-08 00:48:57 -05003791 default:
3792 return;
3793 }
3794
3795 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003796 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003797 return;
3798
3799 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce69e2016-06-16 23:22:37 -04003800 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003801 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3802 else
3803 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003804
Andrey Semin40ee8e32014-12-05 00:07:00 -05003805 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3806
Len Brown144b44b2013-11-09 00:30:16 -05003807 time_unit = msr >> 16 & 0xF;
3808 if (time_unit == 0)
3809 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003810
Len Brown144b44b2013-11-09 00:30:16 -05003811 rapl_time_units = 1.0 / (1 << (time_unit));
3812
3813 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003814
3815 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003816 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003817 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003818
3819 return;
3820}
3821
Colin Ian King1b693172016-03-02 13:50:25 +00003822void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003823{
3824 if (!genuine_intel)
3825 return;
3826
3827 if (family != 6)
3828 return;
3829
3830 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003831 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3832 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3833 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003834 do_gfx_perf_limit_reasons = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04003835 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003836 do_core_perf_limit_reasons = 1;
3837 do_ring_perf_limit_reasons = 1;
3838 default:
3839 return;
3840 }
3841}
3842
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003843void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3844{
3845 if (is_skx(family, model) || is_bdx(family, model))
3846 has_automatic_cstate_conversion = 1;
3847}
3848
Len Brown889facb2012-11-08 00:48:57 -05003849int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3850{
3851 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003852 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003853 int cpu;
3854
3855 if (!(do_dts || do_ptm))
3856 return 0;
3857
3858 cpu = t->cpu_id;
3859
3860 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003861 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003862 return 0;
3863
3864 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003865 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003866 return -1;
3867 }
3868
3869 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3870 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3871 return 0;
3872
3873 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003874 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003875 cpu, msr, tcc_activation_temp - dts);
3876
Len Brown889facb2012-11-08 00:48:57 -05003877 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3878 return 0;
3879
3880 dts = (msr >> 16) & 0x7F;
3881 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003882 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003883 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003884 }
3885
3886
Len Brownf4896fa52017-03-04 18:10:45 -05003887 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003888 unsigned int resolution;
3889
3890 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3891 return 0;
3892
3893 dts = (msr >> 16) & 0x7F;
3894 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003895 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003896 cpu, msr, tcc_activation_temp - dts, resolution);
3897
Len Brown889facb2012-11-08 00:48:57 -05003898 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3899 return 0;
3900
3901 dts = (msr >> 16) & 0x7F;
3902 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003903 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003904 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003905 }
3906
3907 return 0;
3908}
Len Brown36229892016-02-26 20:51:02 -05003909
Len Brown889facb2012-11-08 00:48:57 -05003910void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3911{
Len Brownb7d8c142016-02-13 23:36:17 -05003912 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003913 cpu, label,
3914 ((msr >> 15) & 1) ? "EN" : "DIS",
3915 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3916 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3917 (((msr >> 16) & 1) ? "EN" : "DIS"));
3918
3919 return;
3920}
3921
3922int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3923{
3924 unsigned long long msr;
3925 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003926
3927 if (!do_rapl)
3928 return 0;
3929
3930 /* RAPL counters are per package, so print only for 1st thread/package */
3931 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3932 return 0;
3933
3934 cpu = t->cpu_id;
3935 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003936 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003937 return -1;
3938 }
3939
3940 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3941 return -1;
3942
Len Brown96e47152017-01-21 02:26:00 -05003943 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3944 rapl_power_units, rapl_energy_units, rapl_time_units);
3945
Len Brown144b44b2013-11-09 00:30:16 -05003946 if (do_rapl & RAPL_PKG_POWER_INFO) {
3947
Len Brown889facb2012-11-08 00:48:57 -05003948 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3949 return -5;
3950
3951
Len Brownb7d8c142016-02-13 23:36:17 -05003952 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 -05003953 cpu, msr,
3954 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3955 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3956 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3957 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3958
Len Brown144b44b2013-11-09 00:30:16 -05003959 }
3960 if (do_rapl & RAPL_PKG) {
3961
Len Brown889facb2012-11-08 00:48:57 -05003962 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3963 return -9;
3964
Len Brownb7d8c142016-02-13 23:36:17 -05003965 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003966 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003967
3968 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003969 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003970 cpu,
3971 ((msr >> 47) & 1) ? "EN" : "DIS",
3972 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3973 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3974 ((msr >> 48) & 1) ? "EN" : "DIS");
3975 }
3976
Len Brown0b2bb692015-03-26 00:50:30 -04003977 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003978 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3979 return -6;
3980
Len Brownb7d8c142016-02-13 23:36:17 -05003981 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 -05003982 cpu, msr,
3983 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3984 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3985 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3986 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003987 }
3988 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003989 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3990 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003991 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003992 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003993
3994 print_power_limit_msr(cpu, msr, "DRAM Limit");
3995 }
Len Brown144b44b2013-11-09 00:30:16 -05003996 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003997 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3998 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003999
Len Brown96e47152017-01-21 02:26:00 -05004000 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05004001 }
Jacob Pan91484942016-06-16 09:48:20 -07004002 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05004003 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4004 return -9;
4005 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4006 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4007 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05004008 }
4009 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05004010 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4011 return -8;
Len Brown889facb2012-11-08 00:48:57 -05004012
Len Brown96e47152017-01-21 02:26:00 -05004013 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05004014
Len Brown96e47152017-01-21 02:26:00 -05004015 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4016 return -9;
4017 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4018 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4019 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05004020 }
4021 return 0;
4022}
4023
Len Brownd7899442015-01-23 00:12:33 -05004024/*
4025 * SNB adds support for additional MSRs:
4026 *
4027 * MSR_PKG_C7_RESIDENCY 0x000003fa
4028 * MSR_CORE_C7_RESIDENCY 0x000003fe
4029 * MSR_PKG_C2_RESIDENCY 0x0000060d
4030 */
Len Brown103a8fe2010-10-22 23:53:03 -04004031
Len Brownd7899442015-01-23 00:12:33 -05004032int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04004033{
4034 if (!genuine_intel)
4035 return 0;
4036
4037 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004038 case INTEL_FAM6_SANDYBRIDGE:
4039 case INTEL_FAM6_SANDYBRIDGE_X:
4040 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4041 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4042 case INTEL_FAM6_HASWELL_CORE: /* HSW */
4043 case INTEL_FAM6_HASWELL_X: /* HSW */
4044 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4045 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
4046 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4047 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
4048 case INTEL_FAM6_BROADWELL_X: /* BDX */
4049 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
4050 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4051 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4052 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4053 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004054 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce69e2016-06-16 23:22:37 -04004055 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4056 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004057 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08004058 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04004059 return 1;
4060 }
4061 return 0;
4062}
4063
Len Brownd7899442015-01-23 00:12:33 -05004064/*
4065 * HSW adds support for additional MSRs:
4066 *
Len Brown5a634262016-04-06 17:15:55 -04004067 * MSR_PKG_C8_RESIDENCY 0x00000630
4068 * MSR_PKG_C9_RESIDENCY 0x00000631
4069 * MSR_PKG_C10_RESIDENCY 0x00000632
4070 *
4071 * MSR_PKGC8_IRTL 0x00000633
4072 * MSR_PKGC9_IRTL 0x00000634
4073 * MSR_PKGC10_IRTL 0x00000635
4074 *
Len Brownd7899442015-01-23 00:12:33 -05004075 */
4076int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004077{
4078 if (!genuine_intel)
4079 return 0;
4080
4081 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004082 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4083 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4084 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4085 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4086 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4087 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004088 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce69e2016-06-16 23:22:37 -04004089 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004090 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004091 return 1;
4092 }
4093 return 0;
4094}
4095
Len Brown0b2bb692015-03-26 00:50:30 -04004096/*
4097 * SKL adds support for additional MSRS:
4098 *
4099 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4100 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4101 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4102 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4103 */
4104int has_skl_msrs(unsigned int family, unsigned int model)
4105{
4106 if (!genuine_intel)
4107 return 0;
4108
4109 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004110 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4111 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4112 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4113 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004114 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown0b2bb692015-03-26 00:50:30 -04004115 return 1;
4116 }
4117 return 0;
4118}
4119
Len Brown144b44b2013-11-09 00:30:16 -05004120int is_slm(unsigned int family, unsigned int model)
4121{
4122 if (!genuine_intel)
4123 return 0;
4124 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004125 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
4126 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05004127 return 1;
4128 }
4129 return 0;
4130}
4131
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004132int is_knl(unsigned int family, unsigned int model)
4133{
4134 if (!genuine_intel)
4135 return 0;
4136 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004137 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05004138 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004139 return 1;
4140 }
4141 return 0;
4142}
4143
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004144int is_cnl(unsigned int family, unsigned int model)
4145{
4146 if (!genuine_intel)
4147 return 0;
4148
4149 switch (model) {
4150 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4151 return 1;
4152 }
4153
4154 return 0;
4155}
4156
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004157unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4158{
4159 if (is_knl(family, model))
4160 return 1024;
4161 return 1;
4162}
4163
Len Brown144b44b2013-11-09 00:30:16 -05004164#define SLM_BCLK_FREQS 5
4165double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4166
4167double slm_bclk(void)
4168{
4169 unsigned long long msr = 3;
4170 unsigned int i;
4171 double freq;
4172
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004173 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05004174 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05004175
4176 i = msr & 0xf;
4177 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05004178 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01004179 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05004180 }
4181 freq = slm_freq_table[i];
4182
Len Brown96e47152017-01-21 02:26:00 -05004183 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05004184 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05004185
4186 return freq;
4187}
4188
Len Brown103a8fe2010-10-22 23:53:03 -04004189double discover_bclk(unsigned int family, unsigned int model)
4190{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01004191 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04004192 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05004193 else if (is_slm(family, model))
4194 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04004195 else
4196 return 133.33;
4197}
4198
Len Brown889facb2012-11-08 00:48:57 -05004199/*
4200 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4201 * the Thermal Control Circuit (TCC) activates.
4202 * This is usually equal to tjMax.
4203 *
4204 * Older processors do not have this MSR, so there we guess,
4205 * but also allow cmdline over-ride with -T.
4206 *
4207 * Several MSR temperature values are in units of degrees-C
4208 * below this value, including the Digital Thermal Sensor (DTS),
4209 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4210 */
4211int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4212{
4213 unsigned long long msr;
4214 unsigned int target_c_local;
4215 int cpu;
4216
4217 /* tcc_activation_temp is used only for dts or ptm */
4218 if (!(do_dts || do_ptm))
4219 return 0;
4220
4221 /* this is a per-package concept */
4222 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4223 return 0;
4224
4225 cpu = t->cpu_id;
4226 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004227 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004228 return -1;
4229 }
4230
4231 if (tcc_activation_temp_override != 0) {
4232 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004233 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004234 cpu, tcc_activation_temp);
4235 return 0;
4236 }
4237
4238 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004239 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004240 goto guess;
4241
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004242 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004243 goto guess;
4244
Jean Delvare34821242014-05-01 11:40:19 +02004245 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004246
Len Brown96e47152017-01-21 02:26:00 -05004247 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004248 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004249 cpu, msr, target_c_local);
4250
Jean Delvare34821242014-05-01 11:40:19 +02004251 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004252 goto guess;
4253
4254 tcc_activation_temp = target_c_local;
4255
4256 return 0;
4257
4258guess:
4259 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004260 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004261 cpu, tcc_activation_temp);
4262
4263 return 0;
4264}
Len Brown69807a62015-11-21 12:22:47 -05004265
Len Brownaa8d8cc2016-03-11 13:26:03 -05004266void decode_feature_control_msr(void)
4267{
4268 unsigned long long msr;
4269
4270 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4271 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4272 base_cpu, msr,
4273 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4274 msr & (1 << 18) ? "SGX" : "");
4275}
4276
Len Brown69807a62015-11-21 12:22:47 -05004277void decode_misc_enable_msr(void)
4278{
4279 unsigned long long msr;
4280
Len Brownf26b1512017-06-23 20:45:54 -07004281 if (!genuine_intel)
4282 return;
4283
Len Brown69807a62015-11-21 12:22:47 -05004284 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004285 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004286 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004287 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4288 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004289 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004290 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4291 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004292}
4293
Len Brown33148d62017-01-21 01:26:16 -05004294void decode_misc_feature_control(void)
4295{
4296 unsigned long long msr;
4297
4298 if (!has_misc_feature_control)
4299 return;
4300
4301 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4302 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4303 base_cpu, msr,
4304 msr & (0 << 0) ? "No-" : "",
4305 msr & (1 << 0) ? "No-" : "",
4306 msr & (2 << 0) ? "No-" : "",
4307 msr & (3 << 0) ? "No-" : "");
4308}
Len Brownf0057312015-12-03 01:35:36 -05004309/*
4310 * Decode MSR_MISC_PWR_MGMT
4311 *
4312 * Decode the bits according to the Nehalem documentation
4313 * bit[0] seems to continue to have same meaning going forward
4314 * bit[1] less so...
4315 */
4316void decode_misc_pwr_mgmt_msr(void)
4317{
4318 unsigned long long msr;
4319
4320 if (!do_nhm_platform_info)
4321 return;
4322
Len Browncf4cbe52017-01-01 13:08:33 -05004323 if (no_MSR_MISC_PWR_MGMT)
4324 return;
4325
Len Brownf0057312015-12-03 01:35:36 -05004326 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004327 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 -05004328 base_cpu, msr,
4329 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004330 msr & (1 << 1) ? "EN" : "DIS",
4331 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004332}
Len Brown71616c82017-01-07 22:37:48 -05004333/*
4334 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4335 *
4336 * This MSRs are present on Silvermont processors,
4337 * Intel Atom processor E3000 series (Baytrail), and friends.
4338 */
4339void decode_c6_demotion_policy_msr(void)
4340{
4341 unsigned long long msr;
4342
4343 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4344 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4345 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4346
4347 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4348 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4349 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4350}
Len Brown7f5c2582015-12-01 01:36:39 -05004351
Len Brownfcd17212015-03-23 20:29:09 -04004352void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004353{
Len Brown61a87ba2015-11-23 02:30:51 -05004354 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004355 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004356 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004357
4358 eax = ebx = ecx = edx = 0;
4359
Len Brown5aea2f72016-03-13 03:14:35 -04004360 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004361
4362 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4363 genuine_intel = 1;
4364
Len Brown96e47152017-01-21 02:26:00 -05004365 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004366 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004367 (char *)&ebx, (char *)&edx, (char *)&ecx);
4368
Len Brown5aea2f72016-03-13 03:14:35 -04004369 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004370 family = (fms >> 8) & 0xf;
4371 model = (fms >> 4) & 0xf;
4372 stepping = fms & 0xf;
4373 if (family == 6 || family == 0xf)
4374 model += ((fms >> 16) & 0xf) << 4;
4375
Len Brown96e47152017-01-21 02:26:00 -05004376 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004377 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004378 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004379 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004380 ecx & (1 << 0) ? "SSE3" : "-",
4381 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004382 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004383 ecx & (1 << 7) ? "EIST" : "-",
4384 ecx & (1 << 8) ? "TM2" : "-",
4385 edx & (1 << 4) ? "TSC" : "-",
4386 edx & (1 << 5) ? "MSR" : "-",
4387 edx & (1 << 22) ? "ACPI-TM" : "-",
4388 edx & (1 << 29) ? "TM" : "-");
4389 }
Len Brown103a8fe2010-10-22 23:53:03 -04004390
Josh Triplettb2c95d92013-08-20 17:20:18 -07004391 if (!(edx & (1 << 5)))
4392 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004393
4394 /*
4395 * check max extended function levels of CPUID.
4396 * This is needed to check for invariant TSC.
4397 * This check is valid for both Intel and AMD.
4398 */
4399 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004400 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004401
Len Brown61a87ba2015-11-23 02:30:51 -05004402 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004403
Len Brownd7899442015-01-23 00:12:33 -05004404 /*
4405 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4406 * this check is valid for both Intel and AMD
4407 */
Len Brown5aea2f72016-03-13 03:14:35 -04004408 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004409 has_invariant_tsc = edx & (1 << 8);
4410 }
Len Brown103a8fe2010-10-22 23:53:03 -04004411
4412 /*
4413 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4414 * this check is valid for both Intel and AMD
4415 */
4416
Len Brown5aea2f72016-03-13 03:14:35 -04004417 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004418 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004419 if (has_aperf) {
4420 BIC_PRESENT(BIC_Avg_MHz);
4421 BIC_PRESENT(BIC_Busy);
4422 BIC_PRESENT(BIC_Bzy_MHz);
4423 }
Len Brown889facb2012-11-08 00:48:57 -05004424 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004425 if (do_dts)
4426 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004427 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004428 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004429 if (do_ptm)
4430 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004431 has_hwp = eax & (1 << 7);
4432 has_hwp_notify = eax & (1 << 8);
4433 has_hwp_activity_window = eax & (1 << 9);
4434 has_hwp_epp = eax & (1 << 10);
4435 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004436 has_epb = ecx & (1 << 3);
4437
Len Brown96e47152017-01-21 02:26:00 -05004438 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004439 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004440 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4441 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004442 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004443 do_dts ? "" : "No-",
4444 do_ptm ? "" : "No-",
4445 has_hwp ? "" : "No-",
4446 has_hwp_notify ? "" : "No-",
4447 has_hwp_activity_window ? "" : "No-",
4448 has_hwp_epp ? "" : "No-",
4449 has_hwp_pkg ? "" : "No-",
4450 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004451
Len Brown96e47152017-01-21 02:26:00 -05004452 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004453 decode_misc_enable_msr();
4454
Len Brown33148d62017-01-21 01:26:16 -05004455
Len Brown96e47152017-01-21 02:26:00 -05004456 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004457 int has_sgx;
4458
4459 ecx = 0;
4460
4461 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4462
4463 has_sgx = ebx & (1 << 2);
4464 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4465
4466 if (has_sgx)
4467 decode_feature_control_msr();
4468 }
4469
Len Brown61a87ba2015-11-23 02:30:51 -05004470 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004471 unsigned int eax_crystal;
4472 unsigned int ebx_tsc;
4473
4474 /*
4475 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4476 */
4477 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004478 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004479
4480 if (ebx_tsc != 0) {
4481
Len Brown96e47152017-01-21 02:26:00 -05004482 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004483 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004484 eax_crystal, ebx_tsc, crystal_hz);
4485
4486 if (crystal_hz == 0)
4487 switch(model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004488 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4489 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4490 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4491 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004492 crystal_hz = 24000000; /* 24.0 MHz */
4493 break;
Len Brown7268d402016-12-01 23:10:39 -05004494 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004495 crystal_hz = 25000000; /* 25.0 MHz */
4496 break;
Len Brown869ce69e2016-06-16 23:22:37 -04004497 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004498 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004499 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004500 break;
4501 default:
4502 crystal_hz = 0;
4503 }
4504
4505 if (crystal_hz) {
4506 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004507 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004508 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004509 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4510 }
4511 }
4512 }
Len Brown61a87ba2015-11-23 02:30:51 -05004513 if (max_level >= 0x16) {
4514 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4515
4516 /*
4517 * CPUID 16H Base MHz, Max MHz, Bus MHz
4518 */
4519 base_mhz = max_mhz = bus_mhz = edx = 0;
4520
Len Brown5aea2f72016-03-13 03:14:35 -04004521 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004522 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004523 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004524 base_mhz, max_mhz, bus_mhz);
4525 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004526
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004527 if (has_aperf)
4528 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4529
Len Brown812db3f2017-02-10 00:25:41 -05004530 BIC_PRESENT(BIC_IRQ);
4531 BIC_PRESENT(BIC_TSC_MHz);
4532
4533 if (probe_nhm_msrs(family, model)) {
4534 do_nhm_platform_info = 1;
4535 BIC_PRESENT(BIC_CPU_c1);
4536 BIC_PRESENT(BIC_CPU_c3);
4537 BIC_PRESENT(BIC_CPU_c6);
4538 BIC_PRESENT(BIC_SMI);
4539 }
Len Brownd7899442015-01-23 00:12:33 -05004540 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004541
4542 if (do_snb_cstates)
4543 BIC_PRESENT(BIC_CPU_c7);
4544
Len Brown5a634262016-04-06 17:15:55 -04004545 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004546 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4547 BIC_PRESENT(BIC_Pkgpc2);
4548 if (pkg_cstate_limit >= PCL__3)
4549 BIC_PRESENT(BIC_Pkgpc3);
4550 if (pkg_cstate_limit >= PCL__6)
4551 BIC_PRESENT(BIC_Pkgpc6);
4552 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4553 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba112017-02-10 00:27:20 -05004554 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004555 BIC_NOT_PRESENT(BIC_Pkgpc2);
4556 BIC_NOT_PRESENT(BIC_Pkgpc3);
4557 BIC_PRESENT(BIC_Pkgpc6);
4558 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba112017-02-10 00:27:20 -05004559 BIC_PRESENT(BIC_Mod_c6);
4560 use_c1_residency_msr = 1;
4561 }
Len Brown7170a372017-01-27 02:13:27 -05004562 if (is_dnv(family, model)) {
4563 BIC_PRESENT(BIC_CPU_c1);
4564 BIC_NOT_PRESENT(BIC_CPU_c3);
4565 BIC_NOT_PRESENT(BIC_Pkgpc3);
4566 BIC_NOT_PRESENT(BIC_CPU_c7);
4567 BIC_NOT_PRESENT(BIC_Pkgpc7);
4568 use_c1_residency_msr = 1;
4569 }
Len Brown34c761972017-01-27 02:36:41 -05004570 if (is_skx(family, model)) {
4571 BIC_NOT_PRESENT(BIC_CPU_c3);
4572 BIC_NOT_PRESENT(BIC_Pkgpc3);
4573 BIC_NOT_PRESENT(BIC_CPU_c7);
4574 BIC_NOT_PRESENT(BIC_Pkgpc7);
4575 }
Len Brownade0eba2017-02-10 01:56:47 -05004576 if (is_bdx(family, model)) {
4577 BIC_NOT_PRESENT(BIC_CPU_c7);
4578 BIC_NOT_PRESENT(BIC_Pkgpc7);
4579 }
Len Brown0f47c082017-01-27 00:50:45 -05004580 if (has_hsw_msrs(family, model)) {
4581 BIC_PRESENT(BIC_Pkgpc8);
4582 BIC_PRESENT(BIC_Pkgpc9);
4583 BIC_PRESENT(BIC_Pkgpc10);
4584 }
Len Brown5a634262016-04-06 17:15:55 -04004585 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004586 if (has_skl_msrs(family, model)) {
4587 BIC_PRESENT(BIC_Totl_c0);
4588 BIC_PRESENT(BIC_Any_c0);
4589 BIC_PRESENT(BIC_GFX_c0);
4590 BIC_PRESENT(BIC_CPUGFX);
4591 }
Len Brown144b44b2013-11-09 00:30:16 -05004592 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004593 do_knl_cstates = is_knl(family, model);
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004594 do_cnl_cstates = is_cnl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004595
Len Brown96e47152017-01-21 02:26:00 -05004596 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004597 decode_misc_pwr_mgmt_msr();
4598
Len Brown96e47152017-01-21 02:26:00 -05004599 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004600 decode_c6_demotion_policy_msr();
4601
Len Brown889facb2012-11-08 00:48:57 -05004602 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004603 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004604 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004605
Len Brown96e47152017-01-21 02:26:00 -05004606 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004607 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004608
Len Brown41618e62017-02-09 18:25:22 -05004609 if (!quiet)
4610 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004611 if (!quiet)
4612 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004613
Len Browna2b7b742015-09-26 00:12:38 -04004614 if (has_skl_msrs(family, model))
4615 calculate_tsc_tweak();
4616
Len Brown812db3f2017-02-10 00:25:41 -05004617 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4618 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004619
Len Brown812db3f2017-02-10 00:25:41 -05004620 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4621 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004622
Len Brownbe0e54c2018-06-01 12:35:53 -04004623 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4624 BIC_PRESENT(BIC_CPU_LPI);
4625 else
4626 BIC_NOT_PRESENT(BIC_CPU_LPI);
4627
4628 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4629 BIC_PRESENT(BIC_SYS_LPI);
4630 else
4631 BIC_NOT_PRESENT(BIC_SYS_LPI);
4632
Len Brown96e47152017-01-21 02:26:00 -05004633 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004634 decode_misc_feature_control();
4635
Len Brown889facb2012-11-08 00:48:57 -05004636 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004637}
4638
Len Brown103a8fe2010-10-22 23:53:03 -04004639
4640/*
4641 * in /dev/cpu/ return success for names that are numbers
4642 * ie. filter out ".", "..", "microcode".
4643 */
4644int dir_filter(const struct dirent *dirp)
4645{
4646 if (isdigit(dirp->d_name[0]))
4647 return 1;
4648 else
4649 return 0;
4650}
4651
4652int open_dev_cpu_msr(int dummy1)
4653{
4654 return 0;
4655}
4656
Len Brownc98d5d92012-06-04 00:56:40 -04004657void topology_probe()
4658{
4659 int i;
4660 int max_core_id = 0;
4661 int max_package_id = 0;
4662 int max_siblings = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004663
4664 /* Initialize num_cpus, max_cpu_num */
Prarit Bhargava843c5792018-06-01 10:04:28 -04004665 set_max_cpu_num();
Len Brownc98d5d92012-06-04 00:56:40 -04004666 topo.num_cpus = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004667 for_all_proc_cpus(count_cpus);
4668 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004669 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004670
Len Brownd8af6f52015-02-10 01:56:38 -05004671 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004672 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004673
4674 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004675 if (cpus == NULL)
4676 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004677
4678 /*
4679 * Allocate and initialize cpu_present_set
4680 */
4681 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004682 if (cpu_present_set == NULL)
4683 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004684 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4685 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4686 for_all_proc_cpus(mark_cpu_present);
4687
4688 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004689 * Validate that all cpus in cpu_subset are also in cpu_present_set
4690 */
4691 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4692 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4693 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4694 err(1, "cpu%d not present", i);
4695 }
4696
4697 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004698 * Allocate and initialize cpu_affinity_set
4699 */
4700 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004701 if (cpu_affinity_set == NULL)
4702 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004703 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4704 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4705
4706
4707 /*
4708 * For online cpus
4709 * find max_core_id, max_package_id
4710 */
4711 for (i = 0; i <= topo.max_cpu_num; ++i) {
4712 int siblings;
4713
4714 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004715 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004716 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004717 continue;
4718 }
Len Brownc98d5d92012-06-04 00:56:40 -04004719
Len Brown0e2d8f02018-06-01 22:08:58 -04004720 cpus[i].logical_cpu_id = i;
4721
4722 /* get package information */
Len Brownc98d5d92012-06-04 00:56:40 -04004723 cpus[i].physical_package_id = get_physical_package_id(i);
4724 if (cpus[i].physical_package_id > max_package_id)
4725 max_package_id = cpus[i].physical_package_id;
4726
Len Brown0e2d8f02018-06-01 22:08:58 -04004727 /* get numa node information */
Prarit Bhargavaef605742018-06-01 10:04:30 -04004728 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4729 if (cpus[i].physical_node_id > topo.max_node_num)
4730 topo.max_node_num = cpus[i].physical_node_id;
Len Brown0e2d8f02018-06-01 22:08:58 -04004731
4732 /* get core information */
4733 cpus[i].physical_core_id = get_core_id(i);
4734 if (cpus[i].physical_core_id > max_core_id)
4735 max_core_id = cpus[i].physical_core_id;
4736
4737 /* get thread information */
4738 siblings = get_thread_siblings(&cpus[i]);
Len Brownc98d5d92012-06-04 00:56:40 -04004739 if (siblings > max_siblings)
4740 max_siblings = siblings;
Len Brown0e2d8f02018-06-01 22:08:58 -04004741
Len Brownd8af6f52015-02-10 01:56:38 -05004742 if (debug > 1)
Len Brown0e2d8f02018-06-01 22:08:58 -04004743 fprintf(outf, "cpu %d pkg %d node %d core %d\n",
4744 i, cpus[i].physical_package_id,
Prarit Bhargavaef605742018-06-01 10:04:30 -04004745 cpus[i].physical_node_id,
Len Brown0e2d8f02018-06-01 22:08:58 -04004746 cpus[i].physical_core_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004747 }
Prarit Bhargavaef605742018-06-01 10:04:30 -04004748
Len Brownc98d5d92012-06-04 00:56:40 -04004749 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004750 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004751 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004752 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004753 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004754 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004755
4756 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004757 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004758 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004759 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004760 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004761 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004762
Prarit Bhargavaef605742018-06-01 10:04:30 -04004763 set_node_data();
4764 if (debug > 1)
4765 fprintf(outf, "num_nodes_per_pkg %d\n", topo.num_nodes_per_pkg);
4766
Len Brownc98d5d92012-06-04 00:56:40 -04004767 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004768 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004769 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004770}
4771
4772void
4773allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4774{
4775 int i;
4776
4777 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004778 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004779 if (*t == NULL)
4780 goto error;
4781
4782 for (i = 0; i < topo.num_threads_per_core *
4783 topo.num_cores_per_pkg * topo.num_packages; i++)
4784 (*t)[i].cpu_id = -1;
4785
4786 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004787 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004788 if (*c == NULL)
4789 goto error;
4790
4791 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4792 (*c)[i].core_id = -1;
4793
Len Brown678a3bd2017-02-09 22:22:13 -05004794 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004795 if (*p == NULL)
4796 goto error;
4797
4798 for (i = 0; i < topo.num_packages; i++)
4799 (*p)[i].package_id = i;
4800
4801 return;
4802error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004803 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004804}
4805/*
4806 * init_counter()
4807 *
4808 * set cpu_id, core_num, pkg_num
4809 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4810 *
4811 * increment topo.num_cores when 1st core in pkg seen
4812 */
4813void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4814 struct pkg_data *pkg_base, int thread_num, int core_num,
4815 int pkg_num, int cpu_id)
4816{
4817 struct thread_data *t;
4818 struct core_data *c;
4819 struct pkg_data *p;
4820
4821 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4822 c = GET_CORE(core_base, core_num, pkg_num);
4823 p = GET_PKG(pkg_base, pkg_num);
4824
4825 t->cpu_id = cpu_id;
4826 if (thread_num == 0) {
4827 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4828 if (cpu_is_first_core_in_package(cpu_id))
4829 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4830 }
4831
4832 c->core_id = core_num;
4833 p->package_id = pkg_num;
4834}
4835
4836
4837int initialize_counters(int cpu_id)
4838{
4839 int my_thread_id, my_core_id, my_package_id;
4840
4841 my_package_id = get_physical_package_id(cpu_id);
4842 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004843 my_thread_id = get_cpu_position_in_core(cpu_id);
4844 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004845 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004846
4847 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4848 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4849 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}