blob: 6bf4268137975503ed844ce4bb6ad0adf5047aec [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
Len Brownb9ad8ee2017-07-19 19:28:37 -040032#include <sys/select.h>
Len Brown103a8fe2010-10-22 23:53:03 -040033#include <sys/resource.h>
34#include <fcntl.h>
35#include <signal.h>
36#include <sys/time.h>
37#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050038#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040039#include <dirent.h>
40#include <string.h>
41#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040042#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050043#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070044#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040045#include <linux/capability.h>
46#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040047
Len Brown103a8fe2010-10-22 23:53:03 -040048char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050049FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050050int *fd_percpu;
Len Brownb9ad8ee2017-07-19 19:28:37 -040051struct timeval interval_tv = {5, 0};
Artem Bityutskiy47936f92017-10-04 15:01:47 +030052struct timespec interval_ts = {5, 0};
Len Brownb9ad8ee2017-07-19 19:28:37 -040053struct timespec one_msec = {0, 1000000};
Chen Yu023fe0a2018-04-26 08:41:03 +080054unsigned int num_iterations;
Len Brownd8af6f52015-02-10 01:56:38 -050055unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050056unsigned int quiet;
Len Brown3f44a5c2017-10-17 15:42:56 -040057unsigned int shown;
Len Brown0de6c0d2017-02-15 21:45:40 -050058unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050059unsigned int rapl_joules;
60unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050061unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050062unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040063unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070064unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050065unsigned int do_slm_cstates;
Srinivas Pandruvada997e5392018-05-31 10:39:07 -070066unsigned int do_cnl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050067unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040068unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050069unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040070unsigned int do_irtl_snb;
71unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050072unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040073unsigned int genuine_intel;
74unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050075unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050076unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020077unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040078double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040079double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040080unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040081double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040082unsigned int show_pkg_only;
83unsigned int show_core_only;
84char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050085unsigned int do_rapl;
86unsigned int do_dts;
87unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050088unsigned long long gfx_cur_rc6_ms;
Len Brownbe0e54c2018-06-01 12:35:53 -040089unsigned long long cpuidle_cur_cpu_lpi_us;
90unsigned long long cpuidle_cur_sys_lpi_us;
Len Brown27d47352016-02-27 00:37:54 -050091unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050092unsigned int tcc_activation_temp;
93unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050094double rapl_power_units, rapl_time_units;
95double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050096double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040097unsigned int do_core_perf_limit_reasons;
Artem Bityutskiyac980e12017-09-05 15:14:08 +030098unsigned int has_automatic_cstate_conversion;
Len Brown3a9a9412014-08-15 02:39:52 -040099unsigned int do_gfx_perf_limit_reasons;
100unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -0400101unsigned int crystal_hz;
102unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400103int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400104double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500105unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
106 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
107unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
108unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
109unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
110unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500111unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500112
Len Browne6f9bb32013-12-03 02:19:19 -0500113#define RAPL_PKG (1 << 0)
114 /* 0x610 MSR_PKG_POWER_LIMIT */
115 /* 0x611 MSR_PKG_ENERGY_STATUS */
116#define RAPL_PKG_PERF_STATUS (1 << 1)
117 /* 0x613 MSR_PKG_PERF_STATUS */
118#define RAPL_PKG_POWER_INFO (1 << 2)
119 /* 0x614 MSR_PKG_POWER_INFO */
120
121#define RAPL_DRAM (1 << 3)
122 /* 0x618 MSR_DRAM_POWER_LIMIT */
123 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500124#define RAPL_DRAM_PERF_STATUS (1 << 4)
125 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400126#define RAPL_DRAM_POWER_INFO (1 << 5)
127 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500128
Jacob Pan91484942016-06-16 09:48:20 -0700129#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500130 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400131#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500132 /* 0x63a MSR_PP0_POLICY */
133
Len Brown0b2bb692015-03-26 00:50:30 -0400134#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500135 /* 0x640 MSR_PP1_POWER_LIMIT */
136 /* 0x641 MSR_PP1_ENERGY_STATUS */
137 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700138
139#define RAPL_CORES_ENERGY_STATUS (1 << 9)
140 /* 0x639 MSR_PP0_ENERGY_STATUS */
141#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500142#define TJMAX_DEFAULT 100
143
144#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brown388e9c82016-12-22 23:57:55 -0500146/*
147 * buffer size used by sscanf() for added column names
148 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
149 */
150#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500151#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500152
Len Brown103a8fe2010-10-22 23:53:03 -0400153int backwards_count;
154char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400155
Len Brown1ef7d212017-02-10 23:54:15 -0500156#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
157cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
158size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown0748eaf2018-06-01 12:38:29 -0400159#define MAX_ADDED_COUNTERS 8
160#define MAX_ADDED_THREAD_COUNTERS 24
Len Brown0e2d8f02018-06-01 22:08:58 -0400161#define BITMASK_SIZE 32
Len Brown103a8fe2010-10-22 23:53:03 -0400162
Len Brownc98d5d92012-06-04 00:56:40 -0400163struct thread_data {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700164 struct timeval tv_begin;
165 struct timeval tv_end;
Len Brownc98d5d92012-06-04 00:56:40 -0400166 unsigned long long tsc;
167 unsigned long long aperf;
168 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500169 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500170 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500171 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400172 unsigned int cpu_id;
173 unsigned int flags;
174#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
175#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown0748eaf2018-06-01 12:38:29 -0400176 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400177} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400178
Len Brownc98d5d92012-06-04 00:56:40 -0400179struct core_data {
180 unsigned long long c3;
181 unsigned long long c6;
182 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500183 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500184 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400185 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500186 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400187} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400188
Len Brownc98d5d92012-06-04 00:56:40 -0400189struct pkg_data {
190 unsigned long long pc2;
191 unsigned long long pc3;
192 unsigned long long pc6;
193 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800194 unsigned long long pc8;
195 unsigned long long pc9;
196 unsigned long long pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -0400197 unsigned long long cpu_lpi;
198 unsigned long long sys_lpi;
Len Brown0b2bb692015-03-26 00:50:30 -0400199 unsigned long long pkg_wtd_core_c0;
200 unsigned long long pkg_any_core_c0;
201 unsigned long long pkg_any_gfxe_c0;
202 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400203 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500204 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400205 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500206 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
207 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
208 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
209 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
210 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
211 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
212 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500213 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400214} *package_even, *package_odd;
215
216#define ODD_COUNTERS thread_odd, core_odd, package_odd
217#define EVEN_COUNTERS thread_even, core_even, package_even
218
219#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
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;
263 int node_id;
264 int physical_core_id;
265 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
266} *cpus;
Len Brownc98d5d92012-06-04 00:56:40 -0400267
268struct topo_params {
269 int num_packages;
270 int num_cpus;
271 int num_cores;
272 int max_cpu_num;
273 int num_cores_per_pkg;
274 int num_threads_per_core;
275} topo;
276
277struct timeval tv_even, tv_odd, tv_delta;
278
Len Brown562a2d32016-02-26 23:48:05 -0500279int *irq_column_2_cpu; /* /proc/interrupts column numbers */
280int *irqs_per_cpu; /* indexed by cpu_num */
281
Len Brownc98d5d92012-06-04 00:56:40 -0400282void setup_all_buffers(void);
283
284int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400285{
Len Brownc98d5d92012-06-04 00:56:40 -0400286 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400287}
Len Brown88c32812012-03-29 21:44:40 -0400288/*
Len Brownc98d5d92012-06-04 00:56:40 -0400289 * run func(thread, core, package) in topology order
290 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400291 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400292
Len Brownc98d5d92012-06-04 00:56:40 -0400293int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
294 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400295{
Len Brownc98d5d92012-06-04 00:56:40 -0400296 int retval, pkg_no, core_no, thread_no;
297
298 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
299 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
300 for (thread_no = 0; thread_no <
301 topo.num_threads_per_core; ++thread_no) {
302 struct thread_data *t;
303 struct core_data *c;
304 struct pkg_data *p;
305
306 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
307
308 if (cpu_is_not_present(t->cpu_id))
309 continue;
310
311 c = GET_CORE(core_base, core_no, pkg_no);
312 p = GET_PKG(pkg_base, pkg_no);
313
314 retval = func(t, c, p);
315 if (retval)
316 return retval;
317 }
318 }
319 }
320 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400321}
322
323int cpu_migrate(int cpu)
324{
Len Brownc98d5d92012-06-04 00:56:40 -0400325 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
326 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
327 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400328 return -1;
329 else
330 return 0;
331}
Len Brown36229892016-02-26 20:51:02 -0500332int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400333{
Len Brown103a8fe2010-10-22 23:53:03 -0400334 char pathname[32];
335 int fd;
336
Len Brown36229892016-02-26 20:51:02 -0500337 fd = fd_percpu[cpu];
338
339 if (fd)
340 return fd;
341
Len Brown103a8fe2010-10-22 23:53:03 -0400342 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
343 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400344 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400345 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 -0400346
Len Brown36229892016-02-26 20:51:02 -0500347 fd_percpu[cpu] = fd;
348
349 return fd;
350}
351
352int get_msr(int cpu, off_t offset, unsigned long long *msr)
353{
354 ssize_t retval;
355
356 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400357
Len Brown98481e72014-08-15 00:36:50 -0400358 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500359 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400360
361 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400362}
363
Len Brownfc04cc62014-02-06 00:55:19 -0500364/*
Len Brown812db3f2017-02-10 00:25:41 -0500365 * Each string in this array is compared in --show and --hide cmdline.
366 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500367 */
Len Brown812db3f2017-02-10 00:25:41 -0500368struct msr_counter bic[] = {
Len Brown3f44a5c2017-10-17 15:42:56 -0400369 { 0x0, "usec" },
370 { 0x0, "Time_Of_Day_Seconds" },
Len Brown812db3f2017-02-10 00:25:41 -0500371 { 0x0, "Package" },
372 { 0x0, "Avg_MHz" },
373 { 0x0, "Bzy_MHz" },
374 { 0x0, "TSC_MHz" },
375 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500376 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500377 { 0x0, "Busy%" },
378 { 0x0, "CPU%c1" },
379 { 0x0, "CPU%c3" },
380 { 0x0, "CPU%c6" },
381 { 0x0, "CPU%c7" },
382 { 0x0, "ThreadC" },
383 { 0x0, "CoreTmp" },
384 { 0x0, "CoreCnt" },
385 { 0x0, "PkgTmp" },
386 { 0x0, "GFX%rc6" },
387 { 0x0, "GFXMHz" },
388 { 0x0, "Pkg%pc2" },
389 { 0x0, "Pkg%pc3" },
390 { 0x0, "Pkg%pc6" },
391 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500392 { 0x0, "Pkg%pc8" },
393 { 0x0, "Pkg%pc9" },
Len Brown4bd1f8f2018-01-28 23:42:42 -0500394 { 0x0, "Pk%pc10" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400395 { 0x0, "CPU%LPI" },
396 { 0x0, "SYS%LPI" },
Len Brown812db3f2017-02-10 00:25:41 -0500397 { 0x0, "PkgWatt" },
398 { 0x0, "CorWatt" },
399 { 0x0, "GFXWatt" },
400 { 0x0, "PkgCnt" },
401 { 0x0, "RAMWatt" },
402 { 0x0, "PKG_%" },
403 { 0x0, "RAM_%" },
404 { 0x0, "Pkg_J" },
405 { 0x0, "Cor_J" },
406 { 0x0, "GFX_J" },
407 { 0x0, "RAM_J" },
408 { 0x0, "Core" },
409 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500410 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500411 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400412 { 0x0, "Totl%C0" },
413 { 0x0, "Any%C0" },
414 { 0x0, "GFX%C0" },
415 { 0x0, "CPUGFX%" },
Len Brownbe0e54c2018-06-01 12:35:53 -0400416 { 0x0, "Node%" },
Len Brown812db3f2017-02-10 00:25:41 -0500417};
418
Len Browna99d8732017-05-20 20:11:55 -0400419
420
Len Brown812db3f2017-02-10 00:25:41 -0500421#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
Len Brown3f44a5c2017-10-17 15:42:56 -0400422#define BIC_USEC (1ULL << 0)
423#define BIC_TOD (1ULL << 1)
424#define BIC_Package (1ULL << 2)
425#define BIC_Avg_MHz (1ULL << 3)
426#define BIC_Bzy_MHz (1ULL << 4)
427#define BIC_TSC_MHz (1ULL << 5)
428#define BIC_IRQ (1ULL << 6)
429#define BIC_SMI (1ULL << 7)
430#define BIC_Busy (1ULL << 8)
431#define BIC_CPU_c1 (1ULL << 9)
432#define BIC_CPU_c3 (1ULL << 10)
433#define BIC_CPU_c6 (1ULL << 11)
434#define BIC_CPU_c7 (1ULL << 12)
435#define BIC_ThreadC (1ULL << 13)
436#define BIC_CoreTmp (1ULL << 14)
437#define BIC_CoreCnt (1ULL << 15)
438#define BIC_PkgTmp (1ULL << 16)
439#define BIC_GFX_rc6 (1ULL << 17)
440#define BIC_GFXMHz (1ULL << 18)
441#define BIC_Pkgpc2 (1ULL << 19)
442#define BIC_Pkgpc3 (1ULL << 20)
443#define BIC_Pkgpc6 (1ULL << 21)
444#define BIC_Pkgpc7 (1ULL << 22)
445#define BIC_Pkgpc8 (1ULL << 23)
446#define BIC_Pkgpc9 (1ULL << 24)
447#define BIC_Pkgpc10 (1ULL << 25)
Len Brownbe0e54c2018-06-01 12:35:53 -0400448#define BIC_CPU_LPI (1ULL << 26)
449#define BIC_SYS_LPI (1ULL << 27)
Len Brown3f44a5c2017-10-17 15:42:56 -0400450#define BIC_PkgWatt (1ULL << 26)
451#define BIC_CorWatt (1ULL << 27)
452#define BIC_GFXWatt (1ULL << 28)
453#define BIC_PkgCnt (1ULL << 29)
454#define BIC_RAMWatt (1ULL << 30)
455#define BIC_PKG__ (1ULL << 31)
456#define BIC_RAM__ (1ULL << 32)
457#define BIC_Pkg_J (1ULL << 33)
458#define BIC_Cor_J (1ULL << 34)
459#define BIC_GFX_J (1ULL << 35)
460#define BIC_RAM_J (1ULL << 36)
461#define BIC_Core (1ULL << 37)
462#define BIC_CPU (1ULL << 38)
463#define BIC_Mod_c6 (1ULL << 39)
464#define BIC_sysfs (1ULL << 40)
465#define BIC_Totl_c0 (1ULL << 41)
466#define BIC_Any_c0 (1ULL << 42)
467#define BIC_GFX_c0 (1ULL << 43)
468#define BIC_CPUGFX (1ULL << 44)
Len Brown812db3f2017-02-10 00:25:41 -0500469
Len Brown3f44a5c2017-10-17 15:42:56 -0400470#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
471
472unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
473unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500474
475#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
Len Brown3f44a5c2017-10-17 15:42:56 -0400476#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
Len Brown812db3f2017-02-10 00:25:41 -0500477#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500478#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500479
Len Brown3f44a5c2017-10-17 15:42:56 -0400480
Len Browndd778a52017-02-21 23:21:13 -0500481#define MAX_DEFERRED 16
482char *deferred_skip_names[MAX_DEFERRED];
483int deferred_skip_index;
484
485/*
486 * HIDE_LIST - hide this list of counters, show the rest [default]
487 * SHOW_LIST - show this list of counters, hide the rest
488 */
489enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
490
491void help(void)
492{
493 fprintf(outf,
494 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
495 "\n"
496 "Turbostat forks the specified COMMAND and prints statistics\n"
497 "when COMMAND completes.\n"
498 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
499 "to print statistics, until interrupted.\n"
500 "--add add a counter\n"
501 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
502 "--cpu cpu-set limit output to summary plus cpu-set:\n"
503 " {core | package | j,k,l..m,n-p }\n"
504 "--quiet skip decoding system configuration header\n"
Len Brownb9ad8ee2017-07-19 19:28:37 -0400505 "--interval sec.subsec Override default 5-second measurement interval\n"
Len Browndd778a52017-02-21 23:21:13 -0500506 "--help print this help message\n"
507 "--list list column headers only\n"
Chen Yu023fe0a2018-04-26 08:41:03 +0800508 "--num_iterations num number of the measurement iterations\n"
Len Browndd778a52017-02-21 23:21:13 -0500509 "--out file create or truncate \"file\" for all output\n"
510 "--version print version information\n"
511 "\n"
512 "For more help, run \"man turbostat\"\n");
513}
514
Len Brown812db3f2017-02-10 00:25:41 -0500515/*
516 * bic_lookup
517 * for all the strings in comma separate name_list,
518 * set the approprate bit in return value.
519 */
Len Browndd778a52017-02-21 23:21:13 -0500520unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500521{
522 int i;
523 unsigned long long retval = 0;
524
525 while (name_list) {
526 char *comma;
527
528 comma = strchr(name_list, ',');
529
530 if (comma)
531 *comma = '\0';
532
Len Brown3f44a5c2017-10-17 15:42:56 -0400533 if (!strcmp(name_list, "all"))
534 return ~0;
535
Len Brown812db3f2017-02-10 00:25:41 -0500536 for (i = 0; i < MAX_BIC; ++i) {
537 if (!strcmp(name_list, bic[i].name)) {
538 retval |= (1ULL << i);
539 break;
540 }
541 }
542 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500543 if (mode == SHOW_LIST) {
544 fprintf(stderr, "Invalid counter name: %s\n", name_list);
545 exit(-1);
546 }
547 deferred_skip_names[deferred_skip_index++] = name_list;
548 if (debug)
549 fprintf(stderr, "deferred \"%s\"\n", name_list);
550 if (deferred_skip_index >= MAX_DEFERRED) {
551 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
552 MAX_DEFERRED, name_list);
553 help();
554 exit(1);
555 }
Len Brown812db3f2017-02-10 00:25:41 -0500556 }
557
558 name_list = comma;
559 if (name_list)
560 name_list++;
561
562 }
563 return retval;
564}
Len Brownfc04cc62014-02-06 00:55:19 -0500565
Len Browndd778a52017-02-21 23:21:13 -0500566
Len Brownc8ade362017-02-15 17:15:11 -0500567void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400568{
Len Brown388e9c82016-12-22 23:57:55 -0500569 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500570 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500571
Len Brown3f44a5c2017-10-17 15:42:56 -0400572 if (DO_BIC(BIC_USEC))
573 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
574 if (DO_BIC(BIC_TOD))
575 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500576 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500577 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500578 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500579 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500580 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500581 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500582 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500583 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500584 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500585 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500586 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500587 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500588 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500589 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500590
Len Brown0de6c0d2017-02-15 21:45:40 -0500591 if (DO_BIC(BIC_IRQ)) {
592 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500593 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500594 else
Len Brown6168c2e2017-02-16 23:07:51 -0500595 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500596 }
597
Len Brown812db3f2017-02-10 00:25:41 -0500598 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500599 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500600
Len Brown388e9c82016-12-22 23:57:55 -0500601 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500602
Len Brown388e9c82016-12-22 23:57:55 -0500603 if (mp->format == FORMAT_RAW) {
604 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500605 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500606 else
Len Browndd778a52017-02-21 23:21:13 -0500607 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500608 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500609 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500610 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500611 else
Len Browndd778a52017-02-21 23:21:13 -0500612 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500613 }
614 }
615
Len Brown41618e62017-02-09 18:25:22 -0500616 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500617 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700618 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500619 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500620 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500621 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500622 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500623 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500624
Len Brown0539ba12017-02-10 00:27:20 -0500625 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500626 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500627
Len Brown812db3f2017-02-10 00:25:41 -0500628 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500629 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500630
631 for (mp = sys.cp; mp; mp = mp->next) {
632 if (mp->format == FORMAT_RAW) {
633 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500634 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500635 else
Len Brownc8ade362017-02-15 17:15:11 -0500636 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500637 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500638 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
639 outp += sprintf(outp, "%s%8s", delim, mp->name);
640 else
641 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500642 }
643 }
644
Len Brown812db3f2017-02-10 00:25:41 -0500645 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500646 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500647
Len Brown812db3f2017-02-10 00:25:41 -0500648 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500649 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500650
Len Brown812db3f2017-02-10 00:25:41 -0500651 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500652 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500653
Len Browna99d8732017-05-20 20:11:55 -0400654 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500655 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400656 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500657 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400658 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500659 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400660 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500661 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400662
Len Brown0f47c082017-01-27 00:50:45 -0500663 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500664 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500665 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500666 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500667 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500668 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500669 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500670 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500671 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500672 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500673 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500674 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500675 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500676 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brownbe0e54c2018-06-01 12:35:53 -0400677 if (DO_BIC(BIC_CPU_LPI))
678 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
679 if (DO_BIC(BIC_SYS_LPI))
680 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400681
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800682 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500683 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500684 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500685 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500686 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500687 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500688 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500689 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500690 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500691 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500692 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500693 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500694 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500695 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500696 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500697 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500698 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500699 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500700 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500701 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500702 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500703 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500704 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500705 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500706 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500707 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800708 }
Len Brown388e9c82016-12-22 23:57:55 -0500709 for (mp = sys.pp; mp; mp = mp->next) {
710 if (mp->format == FORMAT_RAW) {
711 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500712 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500713 else
Len Brownc8ade362017-02-15 17:15:11 -0500714 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500715 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500716 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
717 outp += sprintf(outp, "%s%8s", delim, mp->name);
718 else
719 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500720 }
721 }
722
Len Brownc98d5d92012-06-04 00:56:40 -0400723 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400724}
725
Len Brownc98d5d92012-06-04 00:56:40 -0400726int dump_counters(struct thread_data *t, struct core_data *c,
727 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400728{
Len Brown388e9c82016-12-22 23:57:55 -0500729 int i;
730 struct msr_counter *mp;
731
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200732 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400733
Len Brownc98d5d92012-06-04 00:56:40 -0400734 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200735 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
736 t->cpu_id, t->flags);
737 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
738 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
739 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
740 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500741
Len Brown812db3f2017-02-10 00:25:41 -0500742 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500743 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500744 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500745 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500746
747 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
748 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
749 i, mp->msr_num, t->counter[i]);
750 }
Len Brownc98d5d92012-06-04 00:56:40 -0400751 }
Len Brown103a8fe2010-10-22 23:53:03 -0400752
Len Brownc98d5d92012-06-04 00:56:40 -0400753 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200754 outp += sprintf(outp, "core: %d\n", c->core_id);
755 outp += sprintf(outp, "c3: %016llX\n", c->c3);
756 outp += sprintf(outp, "c6: %016llX\n", c->c6);
757 outp += sprintf(outp, "c7: %016llX\n", c->c7);
758 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500759
760 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
761 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
762 i, mp->msr_num, c->counter[i]);
763 }
Len Brown0539ba12017-02-10 00:27:20 -0500764 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400765 }
766
767 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200768 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400769
770 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
771 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
772 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
773 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
774
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200775 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500776 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500777 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500778 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500779 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500780 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500781 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200782 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
783 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
784 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
Len Brownbe0e54c2018-06-01 12:35:53 -0400785 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
786 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
787 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200788 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
789 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
790 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
791 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
792 outp += sprintf(outp, "Throttle PKG: %0X\n",
793 p->rapl_pkg_perf_status);
794 outp += sprintf(outp, "Throttle RAM: %0X\n",
795 p->rapl_dram_perf_status);
796 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500797
798 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
799 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
800 i, mp->msr_num, p->counter[i]);
801 }
Len Brownc98d5d92012-06-04 00:56:40 -0400802 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200803
804 outp += sprintf(outp, "\n");
805
Len Brownc98d5d92012-06-04 00:56:40 -0400806 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400807}
808
Len Browne23da032012-02-06 18:37:16 -0500809/*
810 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500811 */
Len Brownc98d5d92012-06-04 00:56:40 -0400812int format_counters(struct thread_data *t, struct core_data *c,
813 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400814{
Len Brown008d396e2017-02-10 00:29:51 -0500815 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500816 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500817 int i;
818 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500819 char *delim = "\t";
820 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400821
Len Brownc98d5d92012-06-04 00:56:40 -0400822 /* if showing only 1st thread in core and this isn't one, bail out */
823 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
824 return 0;
825
826 /* if showing only 1st thread in pkg and this isn't one, bail out */
827 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
828 return 0;
829
Len Brown1ef7d212017-02-10 23:54:15 -0500830 /*if not summary line and --cpu is used */
831 if ((t != &average.threads) &&
832 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
833 return 0;
834
Len Brown3f44a5c2017-10-17 15:42:56 -0400835 if (DO_BIC(BIC_USEC)) {
Len Brownf4fdf2b2017-05-27 21:06:55 -0700836 /* on each row, print how many usec each timestamp took to gather */
837 struct timeval tv;
838
839 timersub(&t->tv_end, &t->tv_begin, &tv);
840 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
841 }
842
Len Brown3f44a5c2017-10-17 15:42:56 -0400843 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
844 if (DO_BIC(BIC_TOD))
845 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
846
Len Brown103a8fe2010-10-22 23:53:03 -0400847 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
848
Len Brown008d396e2017-02-10 00:29:51 -0500849 tsc = t->tsc * tsc_tweak;
850
Len Brownc98d5d92012-06-04 00:56:40 -0400851 /* topo columns, print blanks on 1st (average) line */
852 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500853 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500854 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500855 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500856 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500857 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500858 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400859 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500860 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400861 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500862 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400863 else
Len Brown6168c2e2017-02-16 23:07:51 -0500864 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400865 }
Len Brown812db3f2017-02-10 00:25:41 -0500866 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400867 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500868 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400869 else
Len Brown6168c2e2017-02-16 23:07:51 -0500870 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400871 }
Len Brown812db3f2017-02-10 00:25:41 -0500872 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500873 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400874 }
Len Brownfc04cc62014-02-06 00:55:19 -0500875
Len Brown812db3f2017-02-10 00:25:41 -0500876 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500877 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500878 1.0 / units * t->aperf / interval_float);
879
Len Brown812db3f2017-02-10 00:25:41 -0500880 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500881 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400882
Len Brown812db3f2017-02-10 00:25:41 -0500883 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400884 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500885 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400886 else
Len Brown6168c2e2017-02-16 23:07:51 -0500887 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500888 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400889 }
Len Brown103a8fe2010-10-22 23:53:03 -0400890
Len Brown812db3f2017-02-10 00:25:41 -0500891 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500892 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400893
Len Brown562a2d32016-02-26 23:48:05 -0500894 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500895 if (DO_BIC(BIC_IRQ)) {
896 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500897 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500898 else
Len Brown6168c2e2017-02-16 23:07:51 -0500899 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500900 }
Len Brown562a2d32016-02-26 23:48:05 -0500901
Len Brown1cc21f72015-02-23 00:34:57 -0500902 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500903 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500904 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500905
Len Brown678a3bd2017-02-09 22:22:13 -0500906 /* Added counters */
907 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
908 if (mp->format == FORMAT_RAW) {
909 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500910 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500911 else
Len Brown6168c2e2017-02-16 23:07:51 -0500912 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500913 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500914 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500915 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500916 else
Len Brown6168c2e2017-02-16 23:07:51 -0500917 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500918 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500919 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500920 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500921 else
Len Brown6168c2e2017-02-16 23:07:51 -0500922 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500923 }
924 }
925
Len Brown41618e62017-02-09 18:25:22 -0500926 /* C1 */
927 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500928 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500929
930
Len Brownc98d5d92012-06-04 00:56:40 -0400931 /* print per-core data only for 1st thread in core */
932 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
933 goto done;
934
Srinivas Pandruvada997e5392018-05-31 10:39:07 -0700935 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500936 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500937 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500938 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500939 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500940 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400941
Len Brown0539ba12017-02-10 00:27:20 -0500942 /* Mod%c6 */
943 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500944 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500945
Len Brown812db3f2017-02-10 00:25:41 -0500946 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500947 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500948
Len Brown388e9c82016-12-22 23:57:55 -0500949 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
950 if (mp->format == FORMAT_RAW) {
951 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500952 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500953 else
Len Brown6168c2e2017-02-16 23:07:51 -0500954 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500955 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500956 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500957 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500958 else
Len Brown6168c2e2017-02-16 23:07:51 -0500959 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500960 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500961 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500962 }
963 }
964
Len Brownc98d5d92012-06-04 00:56:40 -0400965 /* print per-package data only for 1st core in package */
966 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
967 goto done;
968
Len Brown0b2bb692015-03-26 00:50:30 -0400969 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500970 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500971 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500972
Len Brownfdf676e2016-02-27 01:28:12 -0500973 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500974 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400975 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500976 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400977 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500978 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400979 p->gfx_rc6_ms / 10.0 / interval_float);
980 }
981 }
Len Brownfdf676e2016-02-27 01:28:12 -0500982
Len Brown27d47352016-02-27 00:37:54 -0500983 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500984 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500985 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500986
Len Brown0b2bb692015-03-26 00:50:30 -0400987 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -0400988 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500989 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400990 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500991 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400992 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500993 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400994 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500995 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400996
Len Brown0f47c082017-01-27 00:50:45 -0500997 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500998 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500999 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -05001000 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001001 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -05001002 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001003 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -05001004 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001005 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -05001006 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001007 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -05001008 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -05001009 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -05001010 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -05001011
Len Brownbe0e54c2018-06-01 12:35:53 -04001012 if (DO_BIC(BIC_CPU_LPI))
1013 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1014 if (DO_BIC(BIC_SYS_LPI))
1015 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1016
Len Brown889facb2012-11-08 00:48:57 -05001017 /*
1018 * If measurement interval exceeds minimum RAPL Joule Counter range,
1019 * indicate that results are suspect by printing "**" in fraction place.
1020 */
Len Brownfc04cc62014-02-06 00:55:19 -05001021 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -05001022 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -05001023 else
Len Browne975db52016-04-06 23:56:02 -04001024 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -05001025
Len Brown812db3f2017-02-10 00:25:41 -05001026 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001027 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001028 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001029 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001030 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001031 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001032 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -05001033 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -05001034 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001035 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001036 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001037 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001038 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001039 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001040 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -05001041 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -05001042 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -05001043 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 -05001044 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -05001045 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 -05001046
Len Brown388e9c82016-12-22 23:57:55 -05001047 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1048 if (mp->format == FORMAT_RAW) {
1049 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -05001050 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001051 else
Len Brown6168c2e2017-02-16 23:07:51 -05001052 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001053 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -05001054 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -05001055 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -05001056 else
Len Brown6168c2e2017-02-16 23:07:51 -05001057 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -05001058 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -05001059 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -05001060 }
1061 }
1062
Len Brownc98d5d92012-06-04 00:56:40 -04001063done:
Len Brown94d6ab42018-01-27 22:39:21 -05001064 if (*(outp - 1) != '\n')
1065 outp += sprintf(outp, "\n");
Len Brownc98d5d92012-06-04 00:56:40 -04001066
1067 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001068}
1069
Len Brownb7d8c142016-02-13 23:36:17 -05001070void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001071{
Len Brownb7d8c142016-02-13 23:36:17 -05001072 FILE *filep;
1073
1074 if (outf == stderr)
1075 filep = stdout;
1076 else
1077 filep = outf;
1078
1079 fputs(output_buffer, filep);
1080 fflush(filep);
1081
Len Brownc98d5d92012-06-04 00:56:40 -04001082 outp = output_buffer;
1083}
Len Brownb7d8c142016-02-13 23:36:17 -05001084void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001085{
Len Brownb7d8c142016-02-13 23:36:17 -05001086 fputs(output_buffer, outf);
1087 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001088 outp = output_buffer;
1089}
1090void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1091{
Len Browne23da032012-02-06 18:37:16 -05001092 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001093
Len Browne23da032012-02-06 18:37:16 -05001094 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001095 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001096
Len Brownc98d5d92012-06-04 00:56:40 -04001097 if (topo.num_cpus > 1)
1098 format_counters(&average.threads, &average.cores,
1099 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001100
Len Browne23da032012-02-06 18:37:16 -05001101 printed = 1;
1102
1103 if (summary_only)
1104 return;
1105
Len Brownc98d5d92012-06-04 00:56:40 -04001106 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001107}
1108
Len Brown889facb2012-11-08 00:48:57 -05001109#define DELTA_WRAP32(new, old) \
1110 if (new > old) { \
1111 old = new - old; \
1112 } else { \
1113 old = 0x100000000 + new - old; \
1114 }
1115
Len Brownba3dec92016-04-22 20:31:46 -04001116int
Len Brownc98d5d92012-06-04 00:56:40 -04001117delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001118{
Len Brown388e9c82016-12-22 23:57:55 -05001119 int i;
1120 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001121
Len Browna99d8732017-05-20 20:11:55 -04001122
1123 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001124 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001125 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001126 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001127 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001128 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001129 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001130 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 -04001131
Len Brownc98d5d92012-06-04 00:56:40 -04001132 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001133 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001134 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001135 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001136 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001137 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001138 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001139 old->pc8 = new->pc8 - old->pc8;
1140 old->pc9 = new->pc9 - old->pc9;
1141 old->pc10 = new->pc10 - old->pc10;
Len Brownbe0e54c2018-06-01 12:35:53 -04001142 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1143 old->sys_lpi = new->sys_lpi - old->sys_lpi;
Len Brown889facb2012-11-08 00:48:57 -05001144 old->pkg_temp_c = new->pkg_temp_c;
1145
Len Brown9185e982016-04-06 17:16:00 -04001146 /* flag an error when rc6 counter resets/wraps */
1147 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1148 old->gfx_rc6_ms = -1;
1149 else
1150 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1151
Len Brown27d47352016-02-27 00:37:54 -05001152 old->gfx_mhz = new->gfx_mhz;
1153
Len Brown889facb2012-11-08 00:48:57 -05001154 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1155 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1156 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1157 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1158 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1159 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001160
Len Brown388e9c82016-12-22 23:57:55 -05001161 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1162 if (mp->format == FORMAT_RAW)
1163 old->counter[i] = new->counter[i];
1164 else
1165 old->counter[i] = new->counter[i] - old->counter[i];
1166 }
1167
Len Brownba3dec92016-04-22 20:31:46 -04001168 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001169}
Len Brown103a8fe2010-10-22 23:53:03 -04001170
Len Brownc98d5d92012-06-04 00:56:40 -04001171void
1172delta_core(struct core_data *new, struct core_data *old)
1173{
Len Brown388e9c82016-12-22 23:57:55 -05001174 int i;
1175 struct msr_counter *mp;
1176
Len Brownc98d5d92012-06-04 00:56:40 -04001177 old->c3 = new->c3 - old->c3;
1178 old->c6 = new->c6 - old->c6;
1179 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001180 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001181 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001182
1183 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1184 if (mp->format == FORMAT_RAW)
1185 old->counter[i] = new->counter[i];
1186 else
1187 old->counter[i] = new->counter[i] - old->counter[i];
1188 }
Len Brownc98d5d92012-06-04 00:56:40 -04001189}
Len Brown103a8fe2010-10-22 23:53:03 -04001190
Len Brownc3ae3312012-06-13 21:31:46 -04001191/*
1192 * old = new - old
1193 */
Len Brownba3dec92016-04-22 20:31:46 -04001194int
Len Brownc98d5d92012-06-04 00:56:40 -04001195delta_thread(struct thread_data *new, struct thread_data *old,
1196 struct core_data *core_delta)
1197{
Len Brown388e9c82016-12-22 23:57:55 -05001198 int i;
1199 struct msr_counter *mp;
1200
Len Brown3f44a5c2017-10-17 15:42:56 -04001201 /*
1202 * the timestamps from start of measurement interval are in "old"
1203 * the timestamp from end of measurement interval are in "new"
1204 * over-write old w/ new so we can print end of interval values
1205 */
1206
1207 old->tv_begin = new->tv_begin;
1208 old->tv_end = new->tv_end;
1209
Len Brownc98d5d92012-06-04 00:56:40 -04001210 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001211
Len Brownc98d5d92012-06-04 00:56:40 -04001212 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001213 if (old->tsc < (1000 * 1000))
1214 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1215 "You can disable all c-states by booting with \"idle=poll\"\n"
1216 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001217
Len Brownc98d5d92012-06-04 00:56:40 -04001218 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001219
Len Brown812db3f2017-02-10 00:25:41 -05001220 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001221 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1222 old->aperf = new->aperf - old->aperf;
1223 old->mperf = new->mperf - old->mperf;
1224 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001225 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001226 }
Len Brownc98d5d92012-06-04 00:56:40 -04001227 }
Len Brown103a8fe2010-10-22 23:53:03 -04001228
Len Brown103a8fe2010-10-22 23:53:03 -04001229
Len Brown144b44b2013-11-09 00:30:16 -05001230 if (use_c1_residency_msr) {
1231 /*
1232 * Some models have a dedicated C1 residency MSR,
1233 * which should be more accurate than the derivation below.
1234 */
1235 } else {
1236 /*
1237 * As counter collection is not atomic,
1238 * it is possible for mperf's non-halted cycles + idle states
1239 * to exceed TSC's all cycles: show c1 = 0% in that case.
1240 */
Len Brown95149362017-04-12 19:44:51 -04001241 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001242 old->c1 = 0;
1243 else {
1244 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001245 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001246 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001247 }
Len Brownc98d5d92012-06-04 00:56:40 -04001248 }
Len Brownc3ae3312012-06-13 21:31:46 -04001249
Len Brownc98d5d92012-06-04 00:56:40 -04001250 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001251 if (debug > 1)
1252 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001253 old->mperf = 1; /* divide by 0 protection */
1254 }
1255
Len Brown812db3f2017-02-10 00:25:41 -05001256 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001257 old->irq_count = new->irq_count - old->irq_count;
1258
Len Brown812db3f2017-02-10 00:25:41 -05001259 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001260 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001261
Len Brown388e9c82016-12-22 23:57:55 -05001262 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1263 if (mp->format == FORMAT_RAW)
1264 old->counter[i] = new->counter[i];
1265 else
1266 old->counter[i] = new->counter[i] - old->counter[i];
1267 }
Len Brownba3dec92016-04-22 20:31:46 -04001268 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001269}
1270
1271int delta_cpu(struct thread_data *t, struct core_data *c,
1272 struct pkg_data *p, struct thread_data *t2,
1273 struct core_data *c2, struct pkg_data *p2)
1274{
Len Brownba3dec92016-04-22 20:31:46 -04001275 int retval = 0;
1276
Len Brownc98d5d92012-06-04 00:56:40 -04001277 /* calculate core delta only for 1st thread in core */
1278 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1279 delta_core(c, c2);
1280
1281 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001282 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1283 if (retval)
1284 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001285
1286 /* calculate package delta only for 1st core in package */
1287 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001288 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001289
Len Brownba3dec92016-04-22 20:31:46 -04001290 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001291}
1292
1293void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1294{
Len Brown388e9c82016-12-22 23:57:55 -05001295 int i;
1296 struct msr_counter *mp;
1297
Len Brown3f44a5c2017-10-17 15:42:56 -04001298 t->tv_begin.tv_sec = 0;
1299 t->tv_begin.tv_usec = 0;
1300 t->tv_end.tv_sec = 0;
1301 t->tv_end.tv_usec = 0;
1302
Len Brownc98d5d92012-06-04 00:56:40 -04001303 t->tsc = 0;
1304 t->aperf = 0;
1305 t->mperf = 0;
1306 t->c1 = 0;
1307
Len Brown562a2d32016-02-26 23:48:05 -05001308 t->irq_count = 0;
1309 t->smi_count = 0;
1310
Len Brownc98d5d92012-06-04 00:56:40 -04001311 /* tells format_counters to dump all fields from this set */
1312 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1313
1314 c->c3 = 0;
1315 c->c6 = 0;
1316 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001317 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001318 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001319
Len Brown0b2bb692015-03-26 00:50:30 -04001320 p->pkg_wtd_core_c0 = 0;
1321 p->pkg_any_core_c0 = 0;
1322 p->pkg_any_gfxe_c0 = 0;
1323 p->pkg_both_core_gfxe_c0 = 0;
1324
Len Brownc98d5d92012-06-04 00:56:40 -04001325 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001326 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001327 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001328 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001329 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001330 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001331 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001332 p->pc8 = 0;
1333 p->pc9 = 0;
1334 p->pc10 = 0;
Len Brownbe0e54c2018-06-01 12:35:53 -04001335 p->cpu_lpi = 0;
1336 p->sys_lpi = 0;
Len Brown889facb2012-11-08 00:48:57 -05001337
1338 p->energy_pkg = 0;
1339 p->energy_dram = 0;
1340 p->energy_cores = 0;
1341 p->energy_gfx = 0;
1342 p->rapl_pkg_perf_status = 0;
1343 p->rapl_dram_perf_status = 0;
1344 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001345
Len Brownfdf676e2016-02-27 01:28:12 -05001346 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001347 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001348 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1349 t->counter[i] = 0;
1350
1351 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1352 c->counter[i] = 0;
1353
1354 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1355 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001356}
1357int sum_counters(struct thread_data *t, struct core_data *c,
1358 struct pkg_data *p)
1359{
Len Brown388e9c82016-12-22 23:57:55 -05001360 int i;
1361 struct msr_counter *mp;
1362
Len Brown3f44a5c2017-10-17 15:42:56 -04001363 /* remember first tv_begin */
1364 if (average.threads.tv_begin.tv_sec == 0)
1365 average.threads.tv_begin = t->tv_begin;
1366
1367 /* remember last tv_end */
1368 average.threads.tv_end = t->tv_end;
1369
Len Brownc98d5d92012-06-04 00:56:40 -04001370 average.threads.tsc += t->tsc;
1371 average.threads.aperf += t->aperf;
1372 average.threads.mperf += t->mperf;
1373 average.threads.c1 += t->c1;
1374
Len Brown562a2d32016-02-26 23:48:05 -05001375 average.threads.irq_count += t->irq_count;
1376 average.threads.smi_count += t->smi_count;
1377
Len Brown388e9c82016-12-22 23:57:55 -05001378 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1379 if (mp->format == FORMAT_RAW)
1380 continue;
1381 average.threads.counter[i] += t->counter[i];
1382 }
1383
Len Brownc98d5d92012-06-04 00:56:40 -04001384 /* sum per-core values only for 1st thread in core */
1385 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1386 return 0;
1387
1388 average.cores.c3 += c->c3;
1389 average.cores.c6 += c->c6;
1390 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001391 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001392
Len Brown889facb2012-11-08 00:48:57 -05001393 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1394
Len Brown388e9c82016-12-22 23:57:55 -05001395 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1396 if (mp->format == FORMAT_RAW)
1397 continue;
1398 average.cores.counter[i] += c->counter[i];
1399 }
1400
Len Brownc98d5d92012-06-04 00:56:40 -04001401 /* sum per-pkg values only for 1st core in pkg */
1402 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1403 return 0;
1404
Len Browna99d8732017-05-20 20:11:55 -04001405 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001406 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001407 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001408 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001409 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001410 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001411 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001412 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001413
Len Brownc98d5d92012-06-04 00:56:40 -04001414 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001415 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001416 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001417 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001418 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001419 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001420 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001421 average.packages.pc8 += p->pc8;
1422 average.packages.pc9 += p->pc9;
1423 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001424
Len Brownbe0e54c2018-06-01 12:35:53 -04001425 average.packages.cpu_lpi = p->cpu_lpi;
1426 average.packages.sys_lpi = p->sys_lpi;
1427
Len Brown889facb2012-11-08 00:48:57 -05001428 average.packages.energy_pkg += p->energy_pkg;
1429 average.packages.energy_dram += p->energy_dram;
1430 average.packages.energy_cores += p->energy_cores;
1431 average.packages.energy_gfx += p->energy_gfx;
1432
Len Brownfdf676e2016-02-27 01:28:12 -05001433 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001434 average.packages.gfx_mhz = p->gfx_mhz;
1435
Len Brown889facb2012-11-08 00:48:57 -05001436 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1437
1438 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1439 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001440
1441 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1442 if (mp->format == FORMAT_RAW)
1443 continue;
1444 average.packages.counter[i] += p->counter[i];
1445 }
Len Brownc98d5d92012-06-04 00:56:40 -04001446 return 0;
1447}
1448/*
1449 * sum the counters for all cpus in the system
1450 * compute the weighted average
1451 */
1452void compute_average(struct thread_data *t, struct core_data *c,
1453 struct pkg_data *p)
1454{
Len Brown388e9c82016-12-22 23:57:55 -05001455 int i;
1456 struct msr_counter *mp;
1457
Len Brownc98d5d92012-06-04 00:56:40 -04001458 clear_counters(&average.threads, &average.cores, &average.packages);
1459
1460 for_all_cpus(sum_counters, t, c, p);
1461
1462 average.threads.tsc /= topo.num_cpus;
1463 average.threads.aperf /= topo.num_cpus;
1464 average.threads.mperf /= topo.num_cpus;
1465 average.threads.c1 /= topo.num_cpus;
1466
Len Brown0de6c0d2017-02-15 21:45:40 -05001467 if (average.threads.irq_count > 9999999)
1468 sums_need_wide_columns = 1;
1469
Len Brownc98d5d92012-06-04 00:56:40 -04001470 average.cores.c3 /= topo.num_cores;
1471 average.cores.c6 /= topo.num_cores;
1472 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001473 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001474
Len Browna99d8732017-05-20 20:11:55 -04001475 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001476 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001477 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001478 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001479 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001480 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001481 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001482 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001483
Len Brownc98d5d92012-06-04 00:56:40 -04001484 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001485 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001486 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001487 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001488 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001489 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001490 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001491
1492 average.packages.pc8 /= topo.num_packages;
1493 average.packages.pc9 /= topo.num_packages;
1494 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001495
1496 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1497 if (mp->format == FORMAT_RAW)
1498 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001499 if (mp->type == COUNTER_ITEMS) {
1500 if (average.threads.counter[i] > 9999999)
1501 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001502 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001503 }
Len Brown388e9c82016-12-22 23:57:55 -05001504 average.threads.counter[i] /= topo.num_cpus;
1505 }
1506 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1507 if (mp->format == FORMAT_RAW)
1508 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001509 if (mp->type == COUNTER_ITEMS) {
1510 if (average.cores.counter[i] > 9999999)
1511 sums_need_wide_columns = 1;
1512 }
Len Brown388e9c82016-12-22 23:57:55 -05001513 average.cores.counter[i] /= topo.num_cores;
1514 }
1515 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1516 if (mp->format == FORMAT_RAW)
1517 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001518 if (mp->type == COUNTER_ITEMS) {
1519 if (average.packages.counter[i] > 9999999)
1520 sums_need_wide_columns = 1;
1521 }
Len Brown388e9c82016-12-22 23:57:55 -05001522 average.packages.counter[i] /= topo.num_packages;
1523 }
Len Brownc98d5d92012-06-04 00:56:40 -04001524}
1525
1526static unsigned long long rdtsc(void)
1527{
1528 unsigned int low, high;
1529
1530 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1531
1532 return low | ((unsigned long long)high) << 32;
1533}
1534
Len Brownc98d5d92012-06-04 00:56:40 -04001535/*
Len Brown495c76542017-02-08 02:41:51 -05001536 * Open a file, and exit on failure
1537 */
1538FILE *fopen_or_die(const char *path, const char *mode)
1539{
1540 FILE *filep = fopen(path, mode);
1541
1542 if (!filep)
1543 err(1, "%s: open failed", path);
1544 return filep;
1545}
1546/*
1547 * snapshot_sysfs_counter()
1548 *
1549 * return snapshot of given counter
1550 */
1551unsigned long long snapshot_sysfs_counter(char *path)
1552{
1553 FILE *fp;
1554 int retval;
1555 unsigned long long counter;
1556
1557 fp = fopen_or_die(path, "r");
1558
1559 retval = fscanf(fp, "%lld", &counter);
1560 if (retval != 1)
1561 err(1, "snapshot_sysfs_counter(%s)", path);
1562
1563 fclose(fp);
1564
1565 return counter;
1566}
1567
1568int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1569{
1570 if (mp->msr_num != 0) {
1571 if (get_msr(cpu, mp->msr_num, counterp))
1572 return -1;
1573 } else {
Len Brown46c27972017-12-08 17:38:17 -05001574 char path[128 + PATH_BYTES];
Len Brown41618e62017-02-09 18:25:22 -05001575
1576 if (mp->flags & SYSFS_PERCPU) {
1577 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1578 cpu, mp->path);
1579
1580 *counterp = snapshot_sysfs_counter(path);
1581 } else {
1582 *counterp = snapshot_sysfs_counter(mp->path);
1583 }
Len Brown495c76542017-02-08 02:41:51 -05001584 }
1585
1586 return 0;
1587}
1588
1589/*
Len Brownc98d5d92012-06-04 00:56:40 -04001590 * get_counters(...)
1591 * migrate to cpu
1592 * acquire and record local counters for that cpu
1593 */
1594int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1595{
1596 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001597 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001598 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001599 struct msr_counter *mp;
1600 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001601
Len Brownf4fdf2b2017-05-27 21:06:55 -07001602
1603 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1604
Len Browne52966c2012-11-08 22:38:05 -05001605 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001606 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001607 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001608 }
Len Brownc98d5d92012-06-04 00:56:40 -04001609
Len Brown0102b062016-02-27 03:11:29 -05001610retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001611 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1612
Len Brown812db3f2017-02-10 00:25:41 -05001613 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001614 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1615
1616 /*
1617 * The TSC, APERF and MPERF must be read together for
1618 * APERF/MPERF and MPERF/TSC to give accurate results.
1619 *
1620 * Unfortunately, APERF and MPERF are read by
1621 * individual system call, so delays may occur
1622 * between them. If the time to read them
1623 * varies by a large amount, we re-read them.
1624 */
1625
1626 /*
1627 * This initial dummy APERF read has been seen to
1628 * reduce jitter in the subsequent reads.
1629 */
1630
Len Brown9c63a652012-10-31 01:29:52 -04001631 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001632 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001633
1634 t->tsc = rdtsc(); /* re-read close to APERF */
1635
1636 tsc_before = t->tsc;
1637
1638 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1639 return -3;
1640
1641 tsc_between = rdtsc();
1642
Len Brown9c63a652012-10-31 01:29:52 -04001643 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001644 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001645
1646 tsc_after = rdtsc();
1647
1648 aperf_time = tsc_between - tsc_before;
1649 mperf_time = tsc_after - tsc_between;
1650
1651 /*
1652 * If the system call latency to read APERF and MPERF
1653 * differ by more than 2x, then try again.
1654 */
1655 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1656 aperf_mperf_retry_count++;
1657 if (aperf_mperf_retry_count < 5)
1658 goto retry;
1659 else
1660 warnx("cpu%d jitter %lld %lld",
1661 cpu, aperf_time, mperf_time);
1662 }
1663 aperf_mperf_retry_count = 0;
1664
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001665 t->aperf = t->aperf * aperf_mperf_multiplier;
1666 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001667 }
1668
Len Brown812db3f2017-02-10 00:25:41 -05001669 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001670 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001671 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001672 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1673 return -5;
1674 t->smi_count = msr & 0xFFFFFFFF;
1675 }
Len Brown0539ba12017-02-10 00:27:20 -05001676 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001677 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1678 return -6;
1679 }
1680
Len Brown388e9c82016-12-22 23:57:55 -05001681 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001682 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001683 return -10;
1684 }
1685
Len Brownc98d5d92012-06-04 00:56:40 -04001686 /* collect core counters only for 1st thread in core */
1687 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001688 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001689
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07001690 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001691 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1692 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001693 }
1694
Len Brown812db3f2017-02-10 00:25:41 -05001695 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001696 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1697 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001698 } else if (do_knl_cstates) {
1699 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1700 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001701 }
1702
Len Brown812db3f2017-02-10 00:25:41 -05001703 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001704 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1705 return -8;
1706
Len Brown0539ba12017-02-10 00:27:20 -05001707 if (DO_BIC(BIC_Mod_c6))
1708 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1709 return -8;
1710
Len Brown812db3f2017-02-10 00:25:41 -05001711 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001712 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1713 return -9;
1714 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1715 }
1716
Len Brown388e9c82016-12-22 23:57:55 -05001717 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001718 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001719 return -10;
1720 }
Len Brown889facb2012-11-08 00:48:57 -05001721
Len Brownc98d5d92012-06-04 00:56:40 -04001722 /* collect package counters only for 1st core in package */
1723 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
Len Brownf4fdf2b2017-05-27 21:06:55 -07001724 goto done;
Len Brownc98d5d92012-06-04 00:56:40 -04001725
Len Browna99d8732017-05-20 20:11:55 -04001726 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001727 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1728 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001729 }
1730 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001731 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1732 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001733 }
1734 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001735 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1736 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001737 }
1738 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001739 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1740 return -13;
1741 }
Len Brown0f47c082017-01-27 00:50:45 -05001742 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001743 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1744 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001745 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001746 if (do_slm_cstates) {
1747 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1748 return -10;
1749 } else {
1750 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1751 return -10;
1752 }
1753 }
1754
Len Brown0f47c082017-01-27 00:50:45 -05001755 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001756 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1757 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001758 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001759 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1760 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001761 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001762 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1763 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001764 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001765 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1766 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001767 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001768 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1769 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001770
Len Brownbe0e54c2018-06-01 12:35:53 -04001771 if (DO_BIC(BIC_CPU_LPI))
1772 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1773 if (DO_BIC(BIC_SYS_LPI))
1774 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1775
Len Brown889facb2012-11-08 00:48:57 -05001776 if (do_rapl & RAPL_PKG) {
1777 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1778 return -13;
1779 p->energy_pkg = msr & 0xFFFFFFFF;
1780 }
Jacob Pan91484942016-06-16 09:48:20 -07001781 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001782 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1783 return -14;
1784 p->energy_cores = msr & 0xFFFFFFFF;
1785 }
1786 if (do_rapl & RAPL_DRAM) {
1787 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1788 return -15;
1789 p->energy_dram = msr & 0xFFFFFFFF;
1790 }
1791 if (do_rapl & RAPL_GFX) {
1792 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1793 return -16;
1794 p->energy_gfx = msr & 0xFFFFFFFF;
1795 }
1796 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1797 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1798 return -16;
1799 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1800 }
1801 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1802 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1803 return -16;
1804 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1805 }
Len Brown812db3f2017-02-10 00:25:41 -05001806 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001807 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1808 return -17;
1809 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1810 }
Len Brownfdf676e2016-02-27 01:28:12 -05001811
Len Brown812db3f2017-02-10 00:25:41 -05001812 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001813 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1814
Len Brown812db3f2017-02-10 00:25:41 -05001815 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001816 p->gfx_mhz = gfx_cur_mhz;
1817
Len Brown388e9c82016-12-22 23:57:55 -05001818 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001819 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001820 return -10;
1821 }
Len Brownf4fdf2b2017-05-27 21:06:55 -07001822done:
1823 gettimeofday(&t->tv_end, (struct timezone *)NULL);
Len Brown388e9c82016-12-22 23:57:55 -05001824
Len Brown103a8fe2010-10-22 23:53:03 -04001825 return 0;
1826}
1827
Len Brownee7e38e2015-02-09 23:39:45 -05001828/*
1829 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1830 * If you change the values, note they are used both in comparisons
1831 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1832 */
1833
1834#define PCLUKN 0 /* Unknown */
1835#define PCLRSV 1 /* Reserved */
1836#define PCL__0 2 /* PC0 */
1837#define PCL__1 3 /* PC1 */
1838#define PCL__2 4 /* PC2 */
1839#define PCL__3 5 /* PC3 */
1840#define PCL__4 6 /* PC4 */
1841#define PCL__6 7 /* PC6 */
1842#define PCL_6N 8 /* PC6 No Retention */
1843#define PCL_6R 9 /* PC6 Retention */
1844#define PCL__7 10 /* PC7 */
1845#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001846#define PCL__8 12 /* PC8 */
1847#define PCL__9 13 /* PC9 */
1848#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001849
1850int pkg_cstate_limit = PCLUKN;
1851char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001852 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001853
Len Browne9257f52015-04-01 21:02:57 -04001854int 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};
1855int 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};
1856int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brown0539ba12017-02-10 00:27:20 -05001857int 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 -05001858int 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 -04001859int 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 -04001860int 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 +03001861int 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 -05001862
Len Browna2b7b742015-09-26 00:12:38 -04001863
1864static void
1865calculate_tsc_tweak()
1866{
Len Browna2b7b742015-09-26 00:12:38 -04001867 tsc_tweak = base_hz / tsc_hz;
1868}
1869
Len Brownfcd17212015-03-23 20:29:09 -04001870static void
1871dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001872{
1873 unsigned long long msr;
1874 unsigned int ratio;
1875
Len Brownec0adc52015-11-12 02:42:31 -05001876 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001877
Len Brownb7d8c142016-02-13 23:36:17 -05001878 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001879
Len Brown103a8fe2010-10-22 23:53:03 -04001880 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001881 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001882 ratio, bclk, ratio * bclk);
1883
1884 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001885 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001886 ratio, bclk, ratio * bclk);
1887
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001888 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001889 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001890 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001891
Len Brownfcd17212015-03-23 20:29:09 -04001892 return;
1893}
1894
1895static void
1896dump_hsw_turbo_ratio_limits(void)
1897{
1898 unsigned long long msr;
1899 unsigned int ratio;
1900
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001901 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001902
Len Brownb7d8c142016-02-13 23:36:17 -05001903 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001904
1905 ratio = (msr >> 8) & 0xFF;
1906 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001907 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001908 ratio, bclk, ratio * bclk);
1909
1910 ratio = (msr >> 0) & 0xFF;
1911 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001912 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001913 ratio, bclk, ratio * bclk);
1914 return;
1915}
1916
1917static void
1918dump_ivt_turbo_ratio_limits(void)
1919{
1920 unsigned long long msr;
1921 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001922
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001923 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001924
Len Brownb7d8c142016-02-13 23:36:17 -05001925 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001926
1927 ratio = (msr >> 56) & 0xFF;
1928 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001929 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001930 ratio, bclk, ratio * bclk);
1931
1932 ratio = (msr >> 48) & 0xFF;
1933 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001934 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001935 ratio, bclk, ratio * bclk);
1936
1937 ratio = (msr >> 40) & 0xFF;
1938 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001939 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001940 ratio, bclk, ratio * bclk);
1941
1942 ratio = (msr >> 32) & 0xFF;
1943 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001944 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001945 ratio, bclk, ratio * bclk);
1946
1947 ratio = (msr >> 24) & 0xFF;
1948 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001949 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001950 ratio, bclk, ratio * bclk);
1951
1952 ratio = (msr >> 16) & 0xFF;
1953 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001954 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001955 ratio, bclk, ratio * bclk);
1956
1957 ratio = (msr >> 8) & 0xFF;
1958 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001959 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001960 ratio, bclk, ratio * bclk);
1961
1962 ratio = (msr >> 0) & 0xFF;
1963 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001964 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001965 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001966 return;
1967}
Len Brown31e07522017-01-31 23:07:49 -05001968int has_turbo_ratio_group_limits(int family, int model)
1969{
1970
1971 if (!genuine_intel)
1972 return 0;
1973
1974 switch (model) {
1975 case INTEL_FAM6_ATOM_GOLDMONT:
1976 case INTEL_FAM6_SKYLAKE_X:
1977 case INTEL_FAM6_ATOM_DENVERTON:
1978 return 1;
1979 }
1980 return 0;
1981}
Len Brown6574a5d2012-09-21 00:01:31 -04001982
Len Brownfcd17212015-03-23 20:29:09 -04001983static void
Len Brown31e07522017-01-31 23:07:49 -05001984dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001985{
Len Brown31e07522017-01-31 23:07:49 -05001986 unsigned long long msr, core_counts;
1987 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001988
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001989 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001990 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001991
Len Brown31e07522017-01-31 23:07:49 -05001992 if (has_turbo_ratio_group_limits(family, model)) {
1993 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1994 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1995 } else {
1996 core_counts = 0x0807060504030201;
1997 }
1998
Len Brown6574a5d2012-09-21 00:01:31 -04001999 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002000 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002001 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002002 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2003 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002004
2005 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002006 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002007 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002008 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2009 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002010
2011 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002012 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002013 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002014 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2015 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002016
2017 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002018 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04002019 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002020 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2021 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04002022
Len Brown103a8fe2010-10-22 23:53:03 -04002023 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002024 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002025 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002026 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2027 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002028
2029 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002030 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002031 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002032 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2033 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002034
2035 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002036 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002037 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002038 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2039 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04002040
2041 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05002042 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04002043 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05002044 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2045 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04002046 return;
2047}
Len Brown3a9a9412014-08-15 02:39:52 -04002048
Len Brownfcd17212015-03-23 20:29:09 -04002049static void
Len Brown0f7887c2017-01-12 23:49:18 -05002050dump_atom_turbo_ratio_limits(void)
2051{
2052 unsigned long long msr;
2053 unsigned int ratio;
2054
2055 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2056 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2057
2058 ratio = (msr >> 0) & 0x3F;
2059 if (ratio)
2060 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2061 ratio, bclk, ratio * bclk);
2062
2063 ratio = (msr >> 8) & 0x3F;
2064 if (ratio)
2065 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2066 ratio, bclk, ratio * bclk);
2067
2068 ratio = (msr >> 16) & 0x3F;
2069 if (ratio)
2070 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2071 ratio, bclk, ratio * bclk);
2072
2073 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2074 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2075
2076 ratio = (msr >> 24) & 0x3F;
2077 if (ratio)
2078 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2079 ratio, bclk, ratio * bclk);
2080
2081 ratio = (msr >> 16) & 0x3F;
2082 if (ratio)
2083 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2084 ratio, bclk, ratio * bclk);
2085
2086 ratio = (msr >> 8) & 0x3F;
2087 if (ratio)
2088 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2089 ratio, bclk, ratio * bclk);
2090
2091 ratio = (msr >> 0) & 0x3F;
2092 if (ratio)
2093 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2094 ratio, bclk, ratio * bclk);
2095}
2096
2097static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002098dump_knl_turbo_ratio_limits(void)
2099{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002100 const unsigned int buckets_no = 7;
2101
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002102 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002103 int delta_cores, delta_ratio;
2104 int i, b_nr;
2105 unsigned int cores[buckets_no];
2106 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002107
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002108 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002109
Len Brownb7d8c142016-02-13 23:36:17 -05002110 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002111 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002112
2113 /**
2114 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002115 * [0] -- Reserved
2116 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002117 * [15:8] -- Base value of freq ratio of bucket 1.
2118 * [20:16] -- +ve delta of number of active cores of bucket 2.
2119 * i.e. active cores of bucket 2 =
2120 * active cores of bucket 1 + delta
2121 * [23:21] -- Negative delta of freq ratio of bucket 2.
2122 * i.e. freq ratio of bucket 2 =
2123 * freq ratio of bucket 1 - delta
2124 * [28:24]-- +ve delta of number of active cores of bucket 3.
2125 * [31:29]-- -ve delta of freq ratio of bucket 3.
2126 * [36:32]-- +ve delta of number of active cores of bucket 4.
2127 * [39:37]-- -ve delta of freq ratio of bucket 4.
2128 * [44:40]-- +ve delta of number of active cores of bucket 5.
2129 * [47:45]-- -ve delta of freq ratio of bucket 5.
2130 * [52:48]-- +ve delta of number of active cores of bucket 6.
2131 * [55:53]-- -ve delta of freq ratio of bucket 6.
2132 * [60:56]-- +ve delta of number of active cores of bucket 7.
2133 * [63:61]-- -ve delta of freq ratio of bucket 7.
2134 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002135
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002136 b_nr = 0;
2137 cores[b_nr] = (msr & 0xFF) >> 1;
2138 ratio[b_nr] = (msr >> 8) & 0xFF;
2139
2140 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002141 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002142 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002143
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002144 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2145 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2146 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002147 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002148
2149 for (i = buckets_no - 1; i >= 0; i--)
2150 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002151 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002152 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002153 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002154}
2155
2156static void
Len Brownfcd17212015-03-23 20:29:09 -04002157dump_nhm_cst_cfg(void)
2158{
2159 unsigned long long msr;
2160
Len Brown1df2e552017-01-07 23:24:57 -05002161 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002162
Len Brown1df2e552017-01-07 23:24:57 -05002163 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002164
Artem Bityutskiy3e8b62b2017-09-05 15:25:42 +03002165 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
Len Brownfcd17212015-03-23 20:29:09 -04002166 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2167 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2168 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2169 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2170 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002171 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002172 pkg_cstate_limit_strings[pkg_cstate_limit]);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03002173
2174#define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2175 if (has_automatic_cstate_conversion) {
2176 fprintf(outf, ", automatic c-state conversion=%s",
2177 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2178 }
2179
2180 fprintf(outf, ")\n");
2181
Len Brownfcd17212015-03-23 20:29:09 -04002182 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002183}
2184
Len Brown6fb31432015-06-17 16:23:45 -04002185static void
2186dump_config_tdp(void)
2187{
2188 unsigned long long msr;
2189
2190 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002191 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002192 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002193
2194 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002195 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002196 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002197 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2198 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2199 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2200 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002201 }
Len Brownb7d8c142016-02-13 23:36:17 -05002202 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002203
2204 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002205 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002206 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002207 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2208 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2209 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2210 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002211 }
Len Brownb7d8c142016-02-13 23:36:17 -05002212 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002213
2214 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002215 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002216 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002217 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2218 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2219 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002220
Len Brown6fb31432015-06-17 16:23:45 -04002221 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002222 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002223 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002224 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2225 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002226}
Len Brown5a634262016-04-06 17:15:55 -04002227
2228unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2229
2230void print_irtl(void)
2231{
2232 unsigned long long msr;
2233
2234 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2235 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2236 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2237 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2238
2239 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2240 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2241 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2242 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2243
2244 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2245 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2246 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2247 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2248
2249 if (!do_irtl_hsw)
2250 return;
2251
2252 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2253 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2254 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2255 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2256
2257 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2258 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2259 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2260 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2261
2262 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2263 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2264 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2265 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2266
2267}
Len Brown36229892016-02-26 20:51:02 -05002268void free_fd_percpu(void)
2269{
2270 int i;
2271
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002272 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002273 if (fd_percpu[i] != 0)
2274 close(fd_percpu[i]);
2275 }
2276
2277 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002278}
2279
Len Brownc98d5d92012-06-04 00:56:40 -04002280void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002281{
Len Brown0e2d8f02018-06-01 22:08:58 -04002282 int i;
2283
Len Brownc98d5d92012-06-04 00:56:40 -04002284 CPU_FREE(cpu_present_set);
2285 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002286 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002287
Len Brownc98d5d92012-06-04 00:56:40 -04002288 CPU_FREE(cpu_affinity_set);
2289 cpu_affinity_set = NULL;
2290 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002291
Len Brownc98d5d92012-06-04 00:56:40 -04002292 free(thread_even);
2293 free(core_even);
2294 free(package_even);
2295
2296 thread_even = NULL;
2297 core_even = NULL;
2298 package_even = NULL;
2299
2300 free(thread_odd);
2301 free(core_odd);
2302 free(package_odd);
2303
2304 thread_odd = NULL;
2305 core_odd = NULL;
2306 package_odd = NULL;
2307
2308 free(output_buffer);
2309 output_buffer = NULL;
2310 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002311
2312 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002313
2314 free(irq_column_2_cpu);
2315 free(irqs_per_cpu);
Len Brown0e2d8f02018-06-01 22:08:58 -04002316
2317 for (i = 0; i <= topo.max_cpu_num; ++i) {
2318 if (cpus[i].put_ids)
2319 CPU_FREE(cpus[i].put_ids);
2320 }
2321 free(cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002322}
2323
Josh Triplett57a42a32013-08-20 17:20:17 -07002324
2325/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002326 * Parse a file containing a single int.
2327 */
2328int parse_int_file(const char *fmt, ...)
2329{
2330 va_list args;
2331 char path[PATH_MAX];
2332 FILE *filep;
2333 int value;
2334
2335 va_start(args, fmt);
2336 vsnprintf(path, sizeof(path), fmt, args);
2337 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002338 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002339 if (fscanf(filep, "%d", &value) != 1)
2340 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002341 fclose(filep);
2342 return value;
2343}
2344
2345/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002346 * get_cpu_position_in_core(cpu)
2347 * return the position of the CPU among its HT siblings in the core
2348 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002349 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002350int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002351{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002352 char path[64];
2353 FILE *filep;
2354 int this_cpu;
2355 char character;
2356 int i;
2357
2358 sprintf(path,
2359 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2360 cpu);
2361 filep = fopen(path, "r");
2362 if (filep == NULL) {
2363 perror(path);
2364 exit(1);
2365 }
2366
2367 for (i = 0; i < topo.num_threads_per_core; i++) {
2368 fscanf(filep, "%d", &this_cpu);
2369 if (this_cpu == cpu) {
2370 fclose(filep);
2371 return i;
2372 }
2373
2374 /* Account for no separator after last thread*/
2375 if (i != (topo.num_threads_per_core - 1))
2376 fscanf(filep, "%c", &character);
2377 }
2378
2379 fclose(filep);
2380 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002381}
2382
Len Brownc98d5d92012-06-04 00:56:40 -04002383/*
2384 * cpu_is_first_core_in_package(cpu)
2385 * return 1 if given CPU is 1st core in package
2386 */
2387int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002388{
Josh Triplett95aebc42013-08-20 17:20:16 -07002389 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002390}
2391
2392int get_physical_package_id(int cpu)
2393{
Josh Triplett95aebc42013-08-20 17:20:16 -07002394 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002395}
2396
2397int get_core_id(int cpu)
2398{
Josh Triplett95aebc42013-08-20 17:20:16 -07002399 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002400}
2401
Len Brown0e2d8f02018-06-01 22:08:58 -04002402int get_node_id(struct cpu_topology *thiscpu)
Len Brownc98d5d92012-06-04 00:56:40 -04002403{
2404 char path[80];
2405 FILE *filep;
Len Brown0e2d8f02018-06-01 22:08:58 -04002406 int i;
2407 int cpu = thiscpu->logical_cpu_id;
Len Brownc98d5d92012-06-04 00:56:40 -04002408
Len Brown0e2d8f02018-06-01 22:08:58 -04002409 for (i = 0; i <= topo.max_cpu_num; i++) {
2410 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2411 cpu, i);
2412 filep = fopen(path, "r");
2413 if (!filep)
2414 continue;
2415 fclose(filep);
2416 return i;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002417 }
Len Brown0e2d8f02018-06-01 22:08:58 -04002418 return -1;
2419}
Len Brownc98d5d92012-06-04 00:56:40 -04002420
Len Brown0e2d8f02018-06-01 22:08:58 -04002421int get_thread_siblings(struct cpu_topology *thiscpu)
2422{
2423 char path[80], character;
2424 FILE *filep;
2425 unsigned long map;
2426 int shift, sib_core;
2427 int cpu = thiscpu->logical_cpu_id;
2428 int offset = topo.max_cpu_num + 1;
2429 size_t size;
2430
2431 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
2432 if (!thiscpu->put_ids)
2433 return -1;
2434
2435 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2436 CPU_ZERO_S(size, thiscpu->put_ids);
2437
2438 sprintf(path,
2439 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2440 filep = fopen_or_die(path, "r");
2441 do {
2442 offset -= BITMASK_SIZE;
2443 fscanf(filep, "%lx%c", &map, &character);
2444 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2445 if ((map >> shift) & 0x1) {
2446 sib_core = get_core_id(shift + offset);
2447 if (sib_core == thiscpu->physical_core_id)
2448 CPU_SET_S(shift + offset, size,
2449 thiscpu->put_ids);
2450 }
2451 }
2452 } while (!strncmp(&character, ",", 1));
Len Brownc98d5d92012-06-04 00:56:40 -04002453 fclose(filep);
Len Brown0e2d8f02018-06-01 22:08:58 -04002454
2455 return CPU_COUNT_S(size, thiscpu->put_ids);
Len Brownc98d5d92012-06-04 00:56:40 -04002456}
2457
Len Brown103a8fe2010-10-22 23:53:03 -04002458/*
Len Brownc98d5d92012-06-04 00:56:40 -04002459 * run func(thread, core, package) in topology order
2460 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002461 */
2462
Len Brownc98d5d92012-06-04 00:56:40 -04002463int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2464 struct pkg_data *, struct thread_data *, struct core_data *,
2465 struct pkg_data *), struct thread_data *thread_base,
2466 struct core_data *core_base, struct pkg_data *pkg_base,
2467 struct thread_data *thread_base2, struct core_data *core_base2,
2468 struct pkg_data *pkg_base2)
2469{
2470 int retval, pkg_no, core_no, thread_no;
2471
2472 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2473 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2474 for (thread_no = 0; thread_no <
2475 topo.num_threads_per_core; ++thread_no) {
2476 struct thread_data *t, *t2;
2477 struct core_data *c, *c2;
2478 struct pkg_data *p, *p2;
2479
2480 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2481
2482 if (cpu_is_not_present(t->cpu_id))
2483 continue;
2484
2485 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2486
2487 c = GET_CORE(core_base, core_no, pkg_no);
2488 c2 = GET_CORE(core_base2, core_no, pkg_no);
2489
2490 p = GET_PKG(pkg_base, pkg_no);
2491 p2 = GET_PKG(pkg_base2, pkg_no);
2492
2493 retval = func(t, c, p, t2, c2, p2);
2494 if (retval)
2495 return retval;
2496 }
2497 }
2498 }
2499 return 0;
2500}
2501
2502/*
2503 * run func(cpu) on every cpu in /proc/stat
2504 * return max_cpu number
2505 */
2506int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002507{
2508 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002509 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002510 int retval;
2511
Josh Triplett57a42a32013-08-20 17:20:17 -07002512 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002513
2514 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002515 if (retval != 0)
2516 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002517
Len Brownc98d5d92012-06-04 00:56:40 -04002518 while (1) {
2519 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 -04002520 if (retval != 1)
2521 break;
2522
Len Brownc98d5d92012-06-04 00:56:40 -04002523 retval = func(cpu_num);
2524 if (retval) {
2525 fclose(fp);
2526 return(retval);
2527 }
Len Brown103a8fe2010-10-22 23:53:03 -04002528 }
2529 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002530 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002531}
2532
2533void re_initialize(void)
2534{
Len Brownc98d5d92012-06-04 00:56:40 -04002535 free_all_buffers();
2536 setup_all_buffers();
2537 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002538}
2539
Prarit Bhargava843c5792018-06-01 10:04:28 -04002540void set_max_cpu_num(void)
2541{
2542 FILE *filep;
2543 unsigned long dummy;
2544
2545 topo.max_cpu_num = 0;
2546 filep = fopen_or_die(
2547 "/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2548 "r");
2549 while (fscanf(filep, "%lx,", &dummy) == 1)
Len Brown0e2d8f02018-06-01 22:08:58 -04002550 topo.max_cpu_num += BITMASK_SIZE;
Prarit Bhargava843c5792018-06-01 10:04:28 -04002551 fclose(filep);
2552 topo.max_cpu_num--; /* 0 based */
2553}
Len Brownc98d5d92012-06-04 00:56:40 -04002554
Len Brown103a8fe2010-10-22 23:53:03 -04002555/*
Len Brownc98d5d92012-06-04 00:56:40 -04002556 * count_cpus()
2557 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002558 */
Len Brownc98d5d92012-06-04 00:56:40 -04002559int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002560{
Prarit Bhargava843c5792018-06-01 10:04:28 -04002561 topo.num_cpus++;
Len Brownc98d5d92012-06-04 00:56:40 -04002562 return 0;
2563}
2564int mark_cpu_present(int cpu)
2565{
2566 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002567 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002568}
2569
Len Brown562a2d32016-02-26 23:48:05 -05002570/*
2571 * snapshot_proc_interrupts()
2572 *
2573 * read and record summary of /proc/interrupts
2574 *
2575 * return 1 if config change requires a restart, else return 0
2576 */
2577int snapshot_proc_interrupts(void)
2578{
2579 static FILE *fp;
2580 int column, retval;
2581
2582 if (fp == NULL)
2583 fp = fopen_or_die("/proc/interrupts", "r");
2584 else
2585 rewind(fp);
2586
2587 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2588 for (column = 0; column < topo.num_cpus; ++column) {
2589 int cpu_number;
2590
2591 retval = fscanf(fp, " CPU%d", &cpu_number);
2592 if (retval != 1)
2593 break;
2594
2595 if (cpu_number > topo.max_cpu_num) {
2596 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2597 return 1;
2598 }
2599
2600 irq_column_2_cpu[column] = cpu_number;
2601 irqs_per_cpu[cpu_number] = 0;
2602 }
2603
2604 /* read /proc/interrupt count lines and sum up irqs per cpu */
2605 while (1) {
2606 int column;
2607 char buf[64];
2608
2609 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2610 if (retval != 1)
2611 break;
2612
2613 /* read the count per cpu */
2614 for (column = 0; column < topo.num_cpus; ++column) {
2615
2616 int cpu_number, irq_count;
2617
2618 retval = fscanf(fp, " %d", &irq_count);
2619 if (retval != 1)
2620 break;
2621
2622 cpu_number = irq_column_2_cpu[column];
2623 irqs_per_cpu[cpu_number] += irq_count;
2624
2625 }
2626
2627 while (getc(fp) != '\n')
2628 ; /* flush interrupt description */
2629
2630 }
2631 return 0;
2632}
Len Brown27d47352016-02-27 00:37:54 -05002633/*
Len Brownfdf676e2016-02-27 01:28:12 -05002634 * snapshot_gfx_rc6_ms()
2635 *
2636 * record snapshot of
2637 * /sys/class/drm/card0/power/rc6_residency_ms
2638 *
2639 * return 1 if config change requires a restart, else return 0
2640 */
2641int snapshot_gfx_rc6_ms(void)
2642{
2643 FILE *fp;
2644 int retval;
2645
2646 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2647
2648 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2649 if (retval != 1)
2650 err(1, "GFX rc6");
2651
2652 fclose(fp);
2653
2654 return 0;
2655}
2656/*
Len Brown27d47352016-02-27 00:37:54 -05002657 * snapshot_gfx_mhz()
2658 *
2659 * record snapshot of
2660 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2661 *
2662 * return 1 if config change requires a restart, else return 0
2663 */
2664int snapshot_gfx_mhz(void)
2665{
2666 static FILE *fp;
2667 int retval;
2668
2669 if (fp == NULL)
2670 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002671 else {
Len Brown27d47352016-02-27 00:37:54 -05002672 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002673 fflush(fp);
2674 }
Len Brown27d47352016-02-27 00:37:54 -05002675
2676 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2677 if (retval != 1)
2678 err(1, "GFX MHz");
2679
2680 return 0;
2681}
Len Brown562a2d32016-02-26 23:48:05 -05002682
2683/*
Len Brownbe0e54c2018-06-01 12:35:53 -04002684 * snapshot_cpu_lpi()
2685 *
2686 * record snapshot of
2687 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2688 *
2689 * return 1 if config change requires a restart, else return 0
2690 */
2691int snapshot_cpu_lpi_us(void)
2692{
2693 FILE *fp;
2694 int retval;
2695
2696 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2697
2698 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2699 if (retval != 1)
2700 err(1, "CPU LPI");
2701
2702 fclose(fp);
2703
2704 return 0;
2705}
2706/*
2707 * snapshot_sys_lpi()
2708 *
2709 * record snapshot of
2710 * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2711 *
2712 * return 1 if config change requires a restart, else return 0
2713 */
2714int snapshot_sys_lpi_us(void)
2715{
2716 FILE *fp;
2717 int retval;
2718
2719 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2720
2721 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2722 if (retval != 1)
2723 err(1, "SYS LPI");
2724
2725 fclose(fp);
2726
2727 return 0;
2728}
2729/*
Len Brown562a2d32016-02-26 23:48:05 -05002730 * snapshot /proc and /sys files
2731 *
2732 * return 1 if configuration restart needed, else return 0
2733 */
2734int snapshot_proc_sysfs_files(void)
2735{
Len Brown218f0e82017-02-14 22:07:52 -05002736 if (DO_BIC(BIC_IRQ))
2737 if (snapshot_proc_interrupts())
2738 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002739
Len Brown812db3f2017-02-10 00:25:41 -05002740 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002741 snapshot_gfx_rc6_ms();
2742
Len Brown812db3f2017-02-10 00:25:41 -05002743 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002744 snapshot_gfx_mhz();
2745
Len Brownbe0e54c2018-06-01 12:35:53 -04002746 if (DO_BIC(BIC_CPU_LPI))
2747 snapshot_cpu_lpi_us();
2748
2749 if (DO_BIC(BIC_SYS_LPI))
2750 snapshot_sys_lpi_us();
2751
Len Brown562a2d32016-02-26 23:48:05 -05002752 return 0;
2753}
2754
Len Brown8aa2ed02017-07-15 14:57:37 -04002755int exit_requested;
2756
2757static void signal_handler (int signal)
2758{
2759 switch (signal) {
2760 case SIGINT:
2761 exit_requested = 1;
2762 if (debug)
2763 fprintf(stderr, " SIGINT\n");
2764 break;
Len Brown07211962017-07-15 15:51:26 -04002765 case SIGUSR1:
2766 if (debug > 1)
2767 fprintf(stderr, "SIGUSR1\n");
2768 break;
Len Brown8aa2ed02017-07-15 14:57:37 -04002769 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002770 /* make sure this manually-invoked interval is at least 1ms long */
2771 nanosleep(&one_msec, NULL);
Len Brown8aa2ed02017-07-15 14:57:37 -04002772}
2773
2774void setup_signal_handler(void)
2775{
2776 struct sigaction sa;
2777
2778 memset(&sa, 0, sizeof(sa));
2779
2780 sa.sa_handler = &signal_handler;
2781
2782 if (sigaction(SIGINT, &sa, NULL) < 0)
2783 err(1, "sigaction SIGINT");
Len Brown07211962017-07-15 15:51:26 -04002784 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2785 err(1, "sigaction SIGUSR1");
Len Brown8aa2ed02017-07-15 14:57:37 -04002786}
Len Brownb9ad8ee2017-07-19 19:28:37 -04002787
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002788void do_sleep(void)
Len Brownb9ad8ee2017-07-19 19:28:37 -04002789{
2790 struct timeval select_timeout;
2791 fd_set readfds;
2792 int retval;
2793
2794 FD_ZERO(&readfds);
2795 FD_SET(0, &readfds);
2796
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002797 if (!isatty(fileno(stdin))) {
2798 nanosleep(&interval_ts, NULL);
2799 return;
2800 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002801
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002802 select_timeout = interval_tv;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002803 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2804
2805 if (retval == 1) {
Len Brownb9ad8ee2017-07-19 19:28:37 -04002806 switch (getc(stdin)) {
2807 case 'q':
2808 exit_requested = 1;
2809 break;
2810 }
2811 /* make sure this manually-invoked interval is at least 1ms long */
2812 nanosleep(&one_msec, NULL);
2813 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002814}
Artem Bityutskiy47936f92017-10-04 15:01:47 +03002815
Len Brown103a8fe2010-10-22 23:53:03 -04002816void turbostat_loop()
2817{
Len Brownc98d5d92012-06-04 00:56:40 -04002818 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002819 int restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002820 int done_iters = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002821
Len Brown8aa2ed02017-07-15 14:57:37 -04002822 setup_signal_handler();
2823
Len Brown103a8fe2010-10-22 23:53:03 -04002824restart:
Len Browne52966c2012-11-08 22:38:05 -05002825 restarted++;
2826
Len Brown562a2d32016-02-26 23:48:05 -05002827 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002828 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002829 if (retval < -1) {
2830 exit(retval);
2831 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002832 if (restarted > 1) {
2833 exit(retval);
2834 }
Len Brownc98d5d92012-06-04 00:56:40 -04002835 re_initialize();
2836 goto restart;
2837 }
Len Browne52966c2012-11-08 22:38:05 -05002838 restarted = 0;
Chen Yu023fe0a2018-04-26 08:41:03 +08002839 done_iters = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002840 gettimeofday(&tv_even, (struct timezone *)NULL);
2841
2842 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002843 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002844 re_initialize();
2845 goto restart;
2846 }
Len Brownb9ad8ee2017-07-19 19:28:37 -04002847 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002848 if (snapshot_proc_sysfs_files())
2849 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002850 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002851 if (retval < -1) {
2852 exit(retval);
2853 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002854 re_initialize();
2855 goto restart;
2856 }
Len Brown103a8fe2010-10-22 23:53:03 -04002857 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002858 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002859 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2860 re_initialize();
2861 goto restart;
2862 }
Len Brownc98d5d92012-06-04 00:56:40 -04002863 compute_average(EVEN_COUNTERS);
2864 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002865 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002866 if (exit_requested)
2867 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002868 if (num_iterations && ++done_iters >= num_iterations)
2869 break;
Len Brownb9ad8ee2017-07-19 19:28:37 -04002870 do_sleep();
Len Brown562a2d32016-02-26 23:48:05 -05002871 if (snapshot_proc_sysfs_files())
2872 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002873 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002874 if (retval < -1) {
2875 exit(retval);
2876 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002877 re_initialize();
2878 goto restart;
2879 }
Len Brown103a8fe2010-10-22 23:53:03 -04002880 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002881 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002882 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2883 re_initialize();
2884 goto restart;
2885 }
Len Brownc98d5d92012-06-04 00:56:40 -04002886 compute_average(ODD_COUNTERS);
2887 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002888 flush_output_stdout();
Len Brown8aa2ed02017-07-15 14:57:37 -04002889 if (exit_requested)
2890 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08002891 if (num_iterations && ++done_iters >= num_iterations)
2892 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002893 }
2894}
2895
2896void check_dev_msr()
2897{
2898 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002899 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002900
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002901 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2902 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002903 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2904 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002905}
2906
Len Brown98481e72014-08-15 00:36:50 -04002907void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002908{
Len Brown98481e72014-08-15 00:36:50 -04002909 struct __user_cap_header_struct cap_header_data;
2910 cap_user_header_t cap_header = &cap_header_data;
2911 struct __user_cap_data_struct cap_data_data;
2912 cap_user_data_t cap_data = &cap_data_data;
2913 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2914 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002915 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002916
2917 /* check for CAP_SYS_RAWIO */
2918 cap_header->pid = getpid();
2919 cap_header->version = _LINUX_CAPABILITY_VERSION;
2920 if (capget(cap_header, cap_data) < 0)
2921 err(-6, "capget(2) failed");
2922
2923 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2924 do_exit++;
2925 warnx("capget(CAP_SYS_RAWIO) failed,"
2926 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2927 }
2928
2929 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002930 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2931 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002932 do_exit++;
2933 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2934 }
2935
2936 /* if all else fails, thell them to be root */
2937 if (do_exit)
2938 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002939 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002940
2941 if (do_exit)
2942 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002943}
2944
Len Brownd7899442015-01-23 00:12:33 -05002945/*
2946 * NHM adds support for additional MSRs:
2947 *
2948 * MSR_SMI_COUNT 0x00000034
2949 *
Len Brownec0adc52015-11-12 02:42:31 -05002950 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002951 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002952 *
Len Browncf4cbe52017-01-01 13:08:33 -05002953 * MSR_MISC_PWR_MGMT 0x000001aa
2954 *
Len Brownd7899442015-01-23 00:12:33 -05002955 * MSR_PKG_C3_RESIDENCY 0x000003f8
2956 * MSR_PKG_C6_RESIDENCY 0x000003f9
2957 * MSR_CORE_C3_RESIDENCY 0x000003fc
2958 * MSR_CORE_C6_RESIDENCY 0x000003fd
2959 *
Len Brownee7e38e2015-02-09 23:39:45 -05002960 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002961 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002962 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002963 */
Len Brownee7e38e2015-02-09 23:39:45 -05002964int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002965{
Len Brownee7e38e2015-02-09 23:39:45 -05002966 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002967 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002968 int *pkg_cstate_limits;
2969
Len Brown103a8fe2010-10-22 23:53:03 -04002970 if (!genuine_intel)
2971 return 0;
2972
2973 if (family != 6)
2974 return 0;
2975
Len Brown21ed5572015-10-19 22:37:40 -04002976 bclk = discover_bclk(family, model);
2977
Len Brown103a8fe2010-10-22 23:53:03 -04002978 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002979 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2980 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002981 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002982 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2983 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2984 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2985 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002986 pkg_cstate_limits = nhm_pkg_cstate_limits;
2987 break;
Len Brown869ce692016-06-16 23:22:37 -04002988 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2989 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2990 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2991 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002992 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002993 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002994 break;
Len Brown869ce692016-06-16 23:22:37 -04002995 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2996 case INTEL_FAM6_HASWELL_X: /* HSX */
2997 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2998 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2999 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3000 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3001 case INTEL_FAM6_BROADWELL_X: /* BDX */
3002 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3003 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3004 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3005 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3006 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003007 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brownee7e38e2015-02-09 23:39:45 -05003008 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003009 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003010 break;
Len Brownd8ebb442016-12-01 20:27:46 -05003011 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3012 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05003013 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05003014 break;
Len Brown869ce692016-06-16 23:22:37 -04003015 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05003016 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04003017 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05003018 pkg_cstate_limits = slv_pkg_cstate_limits;
3019 break;
Len Brown869ce692016-06-16 23:22:37 -04003020 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05003021 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05003022 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003023 break;
Len Brown869ce692016-06-16 23:22:37 -04003024 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05003025 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05003026 pkg_cstate_limits = phi_pkg_cstate_limits;
3027 break;
Len Brown869ce692016-06-16 23:22:37 -04003028 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003029 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04003030 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04003031 pkg_cstate_limits = bxt_pkg_cstate_limits;
3032 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003033 default:
3034 return 0;
3035 }
Len Brown1df2e552017-01-07 23:24:57 -05003036 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04003037 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05003038
Len Brownec0adc52015-11-12 02:42:31 -05003039 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04003040 base_ratio = (msr >> 8) & 0xFF;
3041
3042 base_hz = base_ratio * bclk * 1000000;
3043 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05003044 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04003045}
Len Brown0f7887c2017-01-12 23:49:18 -05003046/*
Len Brown495c76542017-02-08 02:41:51 -05003047 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05003048 *
3049 * MSR_CC6_DEMOTION_POLICY_CONFIG
3050 * MSR_MC6_DEMOTION_POLICY_CONFIG
3051 */
3052
3053int has_slv_msrs(unsigned int family, unsigned int model)
3054{
3055 if (!genuine_intel)
3056 return 0;
3057
3058 switch (model) {
3059 case INTEL_FAM6_ATOM_SILVERMONT1:
3060 case INTEL_FAM6_ATOM_MERRIFIELD:
3061 case INTEL_FAM6_ATOM_MOOREFIELD:
3062 return 1;
3063 }
3064 return 0;
3065}
Len Brown7170a372017-01-27 02:13:27 -05003066int is_dnv(unsigned int family, unsigned int model)
3067{
3068
3069 if (!genuine_intel)
3070 return 0;
3071
3072 switch (model) {
3073 case INTEL_FAM6_ATOM_DENVERTON:
3074 return 1;
3075 }
3076 return 0;
3077}
Len Brownade0eba2017-02-10 01:56:47 -05003078int is_bdx(unsigned int family, unsigned int model)
3079{
3080
3081 if (!genuine_intel)
3082 return 0;
3083
3084 switch (model) {
3085 case INTEL_FAM6_BROADWELL_X:
3086 case INTEL_FAM6_BROADWELL_XEON_D:
3087 return 1;
3088 }
3089 return 0;
3090}
Len Brown34c761972017-01-27 02:36:41 -05003091int is_skx(unsigned int family, unsigned int model)
3092{
3093
3094 if (!genuine_intel)
3095 return 0;
3096
3097 switch (model) {
3098 case INTEL_FAM6_SKYLAKE_X:
3099 return 1;
3100 }
3101 return 0;
3102}
Len Brown0f7887c2017-01-12 23:49:18 -05003103
Len Brown31e07522017-01-31 23:07:49 -05003104int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05003105{
Len Brown0f7887c2017-01-12 23:49:18 -05003106 if (has_slv_msrs(family, model))
3107 return 0;
3108
Len Brownd7899442015-01-23 00:12:33 -05003109 switch (model) {
3110 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04003111 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3112 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3113 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05003114 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05003115 return 0;
3116 default:
3117 return 1;
3118 }
3119}
Len Brown0f7887c2017-01-12 23:49:18 -05003120int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3121{
3122 if (has_slv_msrs(family, model))
3123 return 1;
3124
3125 return 0;
3126}
Len Brown6574a5d2012-09-21 00:01:31 -04003127int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3128{
3129 if (!genuine_intel)
3130 return 0;
3131
3132 if (family != 6)
3133 return 0;
3134
3135 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003136 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3137 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04003138 return 1;
3139 default:
3140 return 0;
3141 }
3142}
Len Brownfcd17212015-03-23 20:29:09 -04003143int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3144{
3145 if (!genuine_intel)
3146 return 0;
3147
3148 if (family != 6)
3149 return 0;
3150
3151 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003152 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04003153 return 1;
3154 default:
3155 return 0;
3156 }
3157}
3158
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003159int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3160{
3161 if (!genuine_intel)
3162 return 0;
3163
3164 if (family != 6)
3165 return 0;
3166
3167 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003168 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003169 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003170 return 1;
3171 default:
3172 return 0;
3173 }
3174}
Len Brown31e07522017-01-31 23:07:49 -05003175int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3176{
3177 if (!genuine_intel)
3178 return 0;
3179
3180 if (family != 6)
3181 return 0;
3182
3183 switch (model) {
3184 case INTEL_FAM6_ATOM_GOLDMONT:
3185 case INTEL_FAM6_SKYLAKE_X:
3186 return 1;
3187 default:
3188 return 0;
3189 }
3190}
Len Brown6fb31432015-06-17 16:23:45 -04003191int has_config_tdp(unsigned int family, unsigned int model)
3192{
3193 if (!genuine_intel)
3194 return 0;
3195
3196 if (family != 6)
3197 return 0;
3198
3199 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003200 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3201 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3202 case INTEL_FAM6_HASWELL_X: /* HSX */
3203 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3204 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3205 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3206 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3207 case INTEL_FAM6_BROADWELL_X: /* BDX */
3208 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3209 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3210 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3211 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3212 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003213 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04003214 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04003215
Len Brown869ce692016-06-16 23:22:37 -04003216 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05003217 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04003218 return 1;
3219 default:
3220 return 0;
3221 }
3222}
3223
Len Brownfcd17212015-03-23 20:29:09 -04003224static void
Colin Ian King1b693172016-03-02 13:50:25 +00003225dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04003226{
3227 if (!do_nhm_platform_info)
3228 return;
3229
3230 dump_nhm_platform_info();
3231
3232 if (has_hsw_turbo_ratio_limit(family, model))
3233 dump_hsw_turbo_ratio_limits();
3234
3235 if (has_ivt_turbo_ratio_limit(family, model))
3236 dump_ivt_turbo_ratio_limits();
3237
Len Brown31e07522017-01-31 23:07:49 -05003238 if (has_turbo_ratio_limit(family, model))
3239 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003240
Len Brown0f7887c2017-01-12 23:49:18 -05003241 if (has_atom_turbo_ratio_limit(family, model))
3242 dump_atom_turbo_ratio_limits();
3243
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003244 if (has_knl_turbo_ratio_limit(family, model))
3245 dump_knl_turbo_ratio_limits();
3246
Len Brown6fb31432015-06-17 16:23:45 -04003247 if (has_config_tdp(family, model))
3248 dump_config_tdp();
3249
Len Brownfcd17212015-03-23 20:29:09 -04003250 dump_nhm_cst_cfg();
3251}
3252
Len Brown41618e62017-02-09 18:25:22 -05003253static void
3254dump_sysfs_cstate_config(void)
3255{
3256 char path[64];
3257 char name_buf[16];
3258 char desc[64];
3259 FILE *input;
3260 int state;
3261 char *sp;
3262
3263 if (!DO_BIC(BIC_sysfs))
3264 return;
3265
3266 for (state = 0; state < 10; ++state) {
3267
3268 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3269 base_cpu, state);
3270 input = fopen(path, "r");
3271 if (input == NULL)
3272 continue;
3273 fgets(name_buf, sizeof(name_buf), input);
3274
3275 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3276 sp = strchr(name_buf, '-');
3277 if (!sp)
3278 sp = strchrnul(name_buf, '\n');
3279 *sp = '\0';
3280
3281 fclose(input);
3282
3283 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3284 base_cpu, state);
3285 input = fopen(path, "r");
3286 if (input == NULL)
3287 continue;
3288 fgets(desc, sizeof(desc), input);
3289
3290 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3291 fclose(input);
3292 }
3293}
Len Brown7293fcc2017-02-22 00:11:12 -05003294static void
3295dump_sysfs_pstate_config(void)
3296{
3297 char path[64];
3298 char driver_buf[64];
3299 char governor_buf[64];
3300 FILE *input;
3301 int turbo;
3302
3303 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3304 base_cpu);
3305 input = fopen(path, "r");
3306 if (input == NULL) {
3307 fprintf(stderr, "NSFOD %s\n", path);
3308 return;
3309 }
3310 fgets(driver_buf, sizeof(driver_buf), input);
3311 fclose(input);
3312
3313 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3314 base_cpu);
3315 input = fopen(path, "r");
3316 if (input == NULL) {
3317 fprintf(stderr, "NSFOD %s\n", path);
3318 return;
3319 }
3320 fgets(governor_buf, sizeof(governor_buf), input);
3321 fclose(input);
3322
3323 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3324 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3325
3326 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3327 input = fopen(path, "r");
3328 if (input != NULL) {
3329 fscanf(input, "%d", &turbo);
3330 fprintf(outf, "cpufreq boost: %d\n", turbo);
3331 fclose(input);
3332 }
3333
3334 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3335 input = fopen(path, "r");
3336 if (input != NULL) {
3337 fscanf(input, "%d", &turbo);
3338 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3339 fclose(input);
3340 }
3341}
Len Brown41618e62017-02-09 18:25:22 -05003342
Len Brown6574a5d2012-09-21 00:01:31 -04003343
Len Brown889facb2012-11-08 00:48:57 -05003344/*
3345 * print_epb()
3346 * Decode the ENERGY_PERF_BIAS MSR
3347 */
3348int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3349{
3350 unsigned long long msr;
3351 char *epb_string;
3352 int cpu;
3353
3354 if (!has_epb)
3355 return 0;
3356
3357 cpu = t->cpu_id;
3358
3359 /* EPB is per-package */
3360 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3361 return 0;
3362
3363 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003364 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003365 return -1;
3366 }
3367
3368 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3369 return 0;
3370
Len Browne9be7dd2015-05-26 12:19:37 -04003371 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003372 case ENERGY_PERF_BIAS_PERFORMANCE:
3373 epb_string = "performance";
3374 break;
3375 case ENERGY_PERF_BIAS_NORMAL:
3376 epb_string = "balanced";
3377 break;
3378 case ENERGY_PERF_BIAS_POWERSAVE:
3379 epb_string = "powersave";
3380 break;
3381 default:
3382 epb_string = "custom";
3383 break;
3384 }
Len Brownb7d8c142016-02-13 23:36:17 -05003385 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003386
3387 return 0;
3388}
Len Brown7f5c2582015-12-01 01:36:39 -05003389/*
3390 * print_hwp()
3391 * Decode the MSR_HWP_CAPABILITIES
3392 */
3393int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3394{
3395 unsigned long long msr;
3396 int cpu;
3397
3398 if (!has_hwp)
3399 return 0;
3400
3401 cpu = t->cpu_id;
3402
3403 /* MSR_HWP_CAPABILITIES is per-package */
3404 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3405 return 0;
3406
3407 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003408 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003409 return -1;
3410 }
3411
3412 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3413 return 0;
3414
Len Brownb7d8c142016-02-13 23:36:17 -05003415 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003416 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3417
3418 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3419 if ((msr & (1 << 0)) == 0)
3420 return 0;
3421
3422 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3423 return 0;
3424
Len Brownb7d8c142016-02-13 23:36:17 -05003425 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003426 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003427 cpu, msr,
3428 (unsigned int)HWP_HIGHEST_PERF(msr),
3429 (unsigned int)HWP_GUARANTEED_PERF(msr),
3430 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3431 (unsigned int)HWP_LOWEST_PERF(msr));
3432
3433 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3434 return 0;
3435
Len Brownb7d8c142016-02-13 23:36:17 -05003436 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003437 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003438 cpu, msr,
3439 (unsigned int)(((msr) >> 0) & 0xff),
3440 (unsigned int)(((msr) >> 8) & 0xff),
3441 (unsigned int)(((msr) >> 16) & 0xff),
3442 (unsigned int)(((msr) >> 24) & 0xff),
3443 (unsigned int)(((msr) >> 32) & 0xff3),
3444 (unsigned int)(((msr) >> 42) & 0x1));
3445
3446 if (has_hwp_pkg) {
3447 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3448 return 0;
3449
Len Brownb7d8c142016-02-13 23:36:17 -05003450 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003451 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003452 cpu, msr,
3453 (unsigned int)(((msr) >> 0) & 0xff),
3454 (unsigned int)(((msr) >> 8) & 0xff),
3455 (unsigned int)(((msr) >> 16) & 0xff),
3456 (unsigned int)(((msr) >> 24) & 0xff),
3457 (unsigned int)(((msr) >> 32) & 0xff3));
3458 }
3459 if (has_hwp_notify) {
3460 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3461 return 0;
3462
Len Brownb7d8c142016-02-13 23:36:17 -05003463 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003464 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3465 cpu, msr,
3466 ((msr) & 0x1) ? "EN" : "Dis",
3467 ((msr) & 0x2) ? "EN" : "Dis");
3468 }
3469 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3470 return 0;
3471
Len Brownb7d8c142016-02-13 23:36:17 -05003472 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003473 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3474 cpu, msr,
3475 ((msr) & 0x1) ? "" : "No-",
3476 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003477
3478 return 0;
3479}
3480
Len Brown3a9a9412014-08-15 02:39:52 -04003481/*
3482 * print_perf_limit()
3483 */
3484int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3485{
3486 unsigned long long msr;
3487 int cpu;
3488
3489 cpu = t->cpu_id;
3490
3491 /* per-package */
3492 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3493 return 0;
3494
3495 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003496 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003497 return -1;
3498 }
3499
3500 if (do_core_perf_limit_reasons) {
3501 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003502 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3503 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003504 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003505 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003506 (msr & 1 << 13) ? "Transitions, " : "",
3507 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3508 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3509 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3510 (msr & 1 << 9) ? "CorePwr, " : "",
3511 (msr & 1 << 8) ? "Amps, " : "",
3512 (msr & 1 << 6) ? "VR-Therm, " : "",
3513 (msr & 1 << 5) ? "Auto-HWP, " : "",
3514 (msr & 1 << 4) ? "Graphics, " : "",
3515 (msr & 1 << 2) ? "bit2, " : "",
3516 (msr & 1 << 1) ? "ThermStatus, " : "",
3517 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003518 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 -04003519 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003520 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003521 (msr & 1 << 29) ? "Transitions, " : "",
3522 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3523 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3524 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3525 (msr & 1 << 25) ? "CorePwr, " : "",
3526 (msr & 1 << 24) ? "Amps, " : "",
3527 (msr & 1 << 22) ? "VR-Therm, " : "",
3528 (msr & 1 << 21) ? "Auto-HWP, " : "",
3529 (msr & 1 << 20) ? "Graphics, " : "",
3530 (msr & 1 << 18) ? "bit18, " : "",
3531 (msr & 1 << 17) ? "ThermStatus, " : "",
3532 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003533
3534 }
3535 if (do_gfx_perf_limit_reasons) {
3536 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003537 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3538 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003539 (msr & 1 << 0) ? "PROCHOT, " : "",
3540 (msr & 1 << 1) ? "ThermStatus, " : "",
3541 (msr & 1 << 4) ? "Graphics, " : "",
3542 (msr & 1 << 6) ? "VR-Therm, " : "",
3543 (msr & 1 << 8) ? "Amps, " : "",
3544 (msr & 1 << 9) ? "GFXPwr, " : "",
3545 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3546 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003547 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003548 (msr & 1 << 16) ? "PROCHOT, " : "",
3549 (msr & 1 << 17) ? "ThermStatus, " : "",
3550 (msr & 1 << 20) ? "Graphics, " : "",
3551 (msr & 1 << 22) ? "VR-Therm, " : "",
3552 (msr & 1 << 24) ? "Amps, " : "",
3553 (msr & 1 << 25) ? "GFXPwr, " : "",
3554 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3555 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3556 }
3557 if (do_ring_perf_limit_reasons) {
3558 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003559 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3560 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003561 (msr & 1 << 0) ? "PROCHOT, " : "",
3562 (msr & 1 << 1) ? "ThermStatus, " : "",
3563 (msr & 1 << 6) ? "VR-Therm, " : "",
3564 (msr & 1 << 8) ? "Amps, " : "",
3565 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3566 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003567 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003568 (msr & 1 << 16) ? "PROCHOT, " : "",
3569 (msr & 1 << 17) ? "ThermStatus, " : "",
3570 (msr & 1 << 22) ? "VR-Therm, " : "",
3571 (msr & 1 << 24) ? "Amps, " : "",
3572 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3573 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3574 }
3575 return 0;
3576}
3577
Len Brown889facb2012-11-08 00:48:57 -05003578#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3579#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3580
Colin Ian King1b693172016-03-02 13:50:25 +00003581double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003582{
3583 unsigned long long msr;
3584
3585 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003586 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003587 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3588
3589 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003590 case INTEL_FAM6_ATOM_SILVERMONT1:
3591 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003592 return 30.0;
3593 default:
3594 return 135.0;
3595 }
3596}
3597
Andrey Semin40ee8e32014-12-05 00:07:00 -05003598/*
3599 * rapl_dram_energy_units_probe()
3600 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3601 */
3602static double
3603rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3604{
3605 /* only called for genuine_intel, family 6 */
3606
3607 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003608 case INTEL_FAM6_HASWELL_X: /* HSX */
3609 case INTEL_FAM6_BROADWELL_X: /* BDX */
3610 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3611 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003612 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003613 return (rapl_dram_energy_units = 15.3 / 1000000);
3614 default:
3615 return (rapl_energy_units);
3616 }
3617}
3618
Len Brown144b44b2013-11-09 00:30:16 -05003619
Len Brown889facb2012-11-08 00:48:57 -05003620/*
3621 * rapl_probe()
3622 *
Len Brown144b44b2013-11-09 00:30:16 -05003623 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003624 */
3625void rapl_probe(unsigned int family, unsigned int model)
3626{
3627 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003628 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003629 double tdp;
3630
3631 if (!genuine_intel)
3632 return;
3633
3634 if (family != 6)
3635 return;
3636
3637 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003638 case INTEL_FAM6_SANDYBRIDGE:
3639 case INTEL_FAM6_IVYBRIDGE:
3640 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3641 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3642 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3643 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3644 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003645 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003646 if (rapl_joules) {
3647 BIC_PRESENT(BIC_Pkg_J);
3648 BIC_PRESENT(BIC_Cor_J);
3649 BIC_PRESENT(BIC_GFX_J);
3650 } else {
3651 BIC_PRESENT(BIC_PkgWatt);
3652 BIC_PRESENT(BIC_CorWatt);
3653 BIC_PRESENT(BIC_GFXWatt);
3654 }
Len Brown889facb2012-11-08 00:48:57 -05003655 break;
Len Brown869ce692016-06-16 23:22:37 -04003656 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003657 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003658 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003659 if (rapl_joules)
3660 BIC_PRESENT(BIC_Pkg_J);
3661 else
3662 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003663 break;
Len Brown869ce692016-06-16 23:22:37 -04003664 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3665 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3666 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3667 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07003668 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown81824922017-03-04 17:23:07 -05003669 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 -05003670 BIC_PRESENT(BIC_PKG__);
3671 BIC_PRESENT(BIC_RAM__);
3672 if (rapl_joules) {
3673 BIC_PRESENT(BIC_Pkg_J);
3674 BIC_PRESENT(BIC_Cor_J);
3675 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003676 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003677 } else {
3678 BIC_PRESENT(BIC_PkgWatt);
3679 BIC_PRESENT(BIC_CorWatt);
3680 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003681 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003682 }
Len Brown0b2bb692015-03-26 00:50:30 -04003683 break;
Len Brown869ce692016-06-16 23:22:37 -04003684 case INTEL_FAM6_HASWELL_X: /* HSX */
3685 case INTEL_FAM6_BROADWELL_X: /* BDX */
3686 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3687 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3688 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003689 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003690 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 -05003691 BIC_PRESENT(BIC_PKG__);
3692 BIC_PRESENT(BIC_RAM__);
3693 if (rapl_joules) {
3694 BIC_PRESENT(BIC_Pkg_J);
3695 BIC_PRESENT(BIC_RAM_J);
3696 } else {
3697 BIC_PRESENT(BIC_PkgWatt);
3698 BIC_PRESENT(BIC_RAMWatt);
3699 }
Len Browne6f9bb32013-12-03 02:19:19 -05003700 break;
Len Brown869ce692016-06-16 23:22:37 -04003701 case INTEL_FAM6_SANDYBRIDGE_X:
3702 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003703 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 -05003704 BIC_PRESENT(BIC_PKG__);
3705 BIC_PRESENT(BIC_RAM__);
3706 if (rapl_joules) {
3707 BIC_PRESENT(BIC_Pkg_J);
3708 BIC_PRESENT(BIC_Cor_J);
3709 BIC_PRESENT(BIC_RAM_J);
3710 } else {
3711 BIC_PRESENT(BIC_PkgWatt);
3712 BIC_PRESENT(BIC_CorWatt);
3713 BIC_PRESENT(BIC_RAMWatt);
3714 }
Len Brown144b44b2013-11-09 00:30:16 -05003715 break;
Len Brown869ce692016-06-16 23:22:37 -04003716 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3717 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003718 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003719 if (rapl_joules) {
3720 BIC_PRESENT(BIC_Pkg_J);
3721 BIC_PRESENT(BIC_Cor_J);
3722 } else {
3723 BIC_PRESENT(BIC_PkgWatt);
3724 BIC_PRESENT(BIC_CorWatt);
3725 }
Len Brown889facb2012-11-08 00:48:57 -05003726 break;
Len Brown869ce692016-06-16 23:22:37 -04003727 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003728 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 -05003729 BIC_PRESENT(BIC_PKG__);
3730 BIC_PRESENT(BIC_RAM__);
3731 if (rapl_joules) {
3732 BIC_PRESENT(BIC_Pkg_J);
3733 BIC_PRESENT(BIC_Cor_J);
3734 BIC_PRESENT(BIC_RAM_J);
3735 } else {
3736 BIC_PRESENT(BIC_PkgWatt);
3737 BIC_PRESENT(BIC_CorWatt);
3738 BIC_PRESENT(BIC_RAMWatt);
3739 }
Jacob Pan0f644902016-06-16 09:48:22 -07003740 break;
Len Brown889facb2012-11-08 00:48:57 -05003741 default:
3742 return;
3743 }
3744
3745 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003746 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003747 return;
3748
3749 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003750 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003751 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3752 else
3753 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003754
Andrey Semin40ee8e32014-12-05 00:07:00 -05003755 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3756
Len Brown144b44b2013-11-09 00:30:16 -05003757 time_unit = msr >> 16 & 0xF;
3758 if (time_unit == 0)
3759 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003760
Len Brown144b44b2013-11-09 00:30:16 -05003761 rapl_time_units = 1.0 / (1 << (time_unit));
3762
3763 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003764
3765 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003766 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003767 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003768
3769 return;
3770}
3771
Colin Ian King1b693172016-03-02 13:50:25 +00003772void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003773{
3774 if (!genuine_intel)
3775 return;
3776
3777 if (family != 6)
3778 return;
3779
3780 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003781 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3782 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3783 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003784 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003785 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003786 do_core_perf_limit_reasons = 1;
3787 do_ring_perf_limit_reasons = 1;
3788 default:
3789 return;
3790 }
3791}
3792
Artem Bityutskiyac980e12017-09-05 15:14:08 +03003793void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3794{
3795 if (is_skx(family, model) || is_bdx(family, model))
3796 has_automatic_cstate_conversion = 1;
3797}
3798
Len Brown889facb2012-11-08 00:48:57 -05003799int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3800{
3801 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003802 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003803 int cpu;
3804
3805 if (!(do_dts || do_ptm))
3806 return 0;
3807
3808 cpu = t->cpu_id;
3809
3810 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003811 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003812 return 0;
3813
3814 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003815 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003816 return -1;
3817 }
3818
3819 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3820 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3821 return 0;
3822
3823 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003824 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003825 cpu, msr, tcc_activation_temp - dts);
3826
Len Brown889facb2012-11-08 00:48:57 -05003827 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3828 return 0;
3829
3830 dts = (msr >> 16) & 0x7F;
3831 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003832 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003833 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003834 }
3835
3836
Len Brownf4896fa52017-03-04 18:10:45 -05003837 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003838 unsigned int resolution;
3839
3840 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3841 return 0;
3842
3843 dts = (msr >> 16) & 0x7F;
3844 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003845 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003846 cpu, msr, tcc_activation_temp - dts, resolution);
3847
Len Brown889facb2012-11-08 00:48:57 -05003848 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3849 return 0;
3850
3851 dts = (msr >> 16) & 0x7F;
3852 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003853 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003854 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003855 }
3856
3857 return 0;
3858}
Len Brown36229892016-02-26 20:51:02 -05003859
Len Brown889facb2012-11-08 00:48:57 -05003860void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3861{
Len Brownb7d8c142016-02-13 23:36:17 -05003862 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003863 cpu, label,
3864 ((msr >> 15) & 1) ? "EN" : "DIS",
3865 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3866 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3867 (((msr >> 16) & 1) ? "EN" : "DIS"));
3868
3869 return;
3870}
3871
3872int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3873{
3874 unsigned long long msr;
3875 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003876
3877 if (!do_rapl)
3878 return 0;
3879
3880 /* RAPL counters are per package, so print only for 1st thread/package */
3881 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3882 return 0;
3883
3884 cpu = t->cpu_id;
3885 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003886 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003887 return -1;
3888 }
3889
3890 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3891 return -1;
3892
Len Brown96e47152017-01-21 02:26:00 -05003893 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3894 rapl_power_units, rapl_energy_units, rapl_time_units);
3895
Len Brown144b44b2013-11-09 00:30:16 -05003896 if (do_rapl & RAPL_PKG_POWER_INFO) {
3897
Len Brown889facb2012-11-08 00:48:57 -05003898 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3899 return -5;
3900
3901
Len Brownb7d8c142016-02-13 23:36:17 -05003902 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 -05003903 cpu, msr,
3904 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3905 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3906 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3907 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3908
Len Brown144b44b2013-11-09 00:30:16 -05003909 }
3910 if (do_rapl & RAPL_PKG) {
3911
Len Brown889facb2012-11-08 00:48:57 -05003912 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3913 return -9;
3914
Len Brownb7d8c142016-02-13 23:36:17 -05003915 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003916 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003917
3918 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003919 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003920 cpu,
3921 ((msr >> 47) & 1) ? "EN" : "DIS",
3922 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3923 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3924 ((msr >> 48) & 1) ? "EN" : "DIS");
3925 }
3926
Len Brown0b2bb692015-03-26 00:50:30 -04003927 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003928 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3929 return -6;
3930
Len Brownb7d8c142016-02-13 23:36:17 -05003931 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 -05003932 cpu, msr,
3933 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3934 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3935 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3936 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003937 }
3938 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003939 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3940 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003941 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003942 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003943
3944 print_power_limit_msr(cpu, msr, "DRAM Limit");
3945 }
Len Brown144b44b2013-11-09 00:30:16 -05003946 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003947 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3948 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003949
Len Brown96e47152017-01-21 02:26:00 -05003950 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003951 }
Jacob Pan91484942016-06-16 09:48:20 -07003952 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003953 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3954 return -9;
3955 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3956 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3957 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003958 }
3959 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003960 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3961 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003962
Len Brown96e47152017-01-21 02:26:00 -05003963 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003964
Len Brown96e47152017-01-21 02:26:00 -05003965 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3966 return -9;
3967 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3968 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3969 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003970 }
3971 return 0;
3972}
3973
Len Brownd7899442015-01-23 00:12:33 -05003974/*
3975 * SNB adds support for additional MSRs:
3976 *
3977 * MSR_PKG_C7_RESIDENCY 0x000003fa
3978 * MSR_CORE_C7_RESIDENCY 0x000003fe
3979 * MSR_PKG_C2_RESIDENCY 0x0000060d
3980 */
Len Brown103a8fe2010-10-22 23:53:03 -04003981
Len Brownd7899442015-01-23 00:12:33 -05003982int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003983{
3984 if (!genuine_intel)
3985 return 0;
3986
3987 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003988 case INTEL_FAM6_SANDYBRIDGE:
3989 case INTEL_FAM6_SANDYBRIDGE_X:
3990 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3991 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3992 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3993 case INTEL_FAM6_HASWELL_X: /* HSW */
3994 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3995 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3996 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3997 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3998 case INTEL_FAM6_BROADWELL_X: /* BDX */
3999 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
4000 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4001 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4002 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4003 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004004 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04004005 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4006 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004007 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08004008 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04004009 return 1;
4010 }
4011 return 0;
4012}
4013
Len Brownd7899442015-01-23 00:12:33 -05004014/*
4015 * HSW adds support for additional MSRs:
4016 *
Len Brown5a634262016-04-06 17:15:55 -04004017 * MSR_PKG_C8_RESIDENCY 0x00000630
4018 * MSR_PKG_C9_RESIDENCY 0x00000631
4019 * MSR_PKG_C10_RESIDENCY 0x00000632
4020 *
4021 * MSR_PKGC8_IRTL 0x00000633
4022 * MSR_PKGC9_IRTL 0x00000634
4023 * MSR_PKGC10_IRTL 0x00000635
4024 *
Len Brownd7899442015-01-23 00:12:33 -05004025 */
4026int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004027{
4028 if (!genuine_intel)
4029 return 0;
4030
4031 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004032 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4033 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4034 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4035 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4036 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4037 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004038 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown869ce692016-06-16 23:22:37 -04004039 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004040 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08004041 return 1;
4042 }
4043 return 0;
4044}
4045
Len Brown0b2bb692015-03-26 00:50:30 -04004046/*
4047 * SKL adds support for additional MSRS:
4048 *
4049 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4050 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4051 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4052 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4053 */
4054int has_skl_msrs(unsigned int family, unsigned int model)
4055{
4056 if (!genuine_intel)
4057 return 0;
4058
4059 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004060 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4061 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4062 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4063 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004064 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
Len Brown0b2bb692015-03-26 00:50:30 -04004065 return 1;
4066 }
4067 return 0;
4068}
4069
Len Brown144b44b2013-11-09 00:30:16 -05004070int is_slm(unsigned int family, unsigned int model)
4071{
4072 if (!genuine_intel)
4073 return 0;
4074 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004075 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
4076 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05004077 return 1;
4078 }
4079 return 0;
4080}
4081
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004082int is_knl(unsigned int family, unsigned int model)
4083{
4084 if (!genuine_intel)
4085 return 0;
4086 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04004087 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05004088 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004089 return 1;
4090 }
4091 return 0;
4092}
4093
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004094int is_cnl(unsigned int family, unsigned int model)
4095{
4096 if (!genuine_intel)
4097 return 0;
4098
4099 switch (model) {
4100 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4101 return 1;
4102 }
4103
4104 return 0;
4105}
4106
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004107unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4108{
4109 if (is_knl(family, model))
4110 return 1024;
4111 return 1;
4112}
4113
Len Brown144b44b2013-11-09 00:30:16 -05004114#define SLM_BCLK_FREQS 5
4115double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4116
4117double slm_bclk(void)
4118{
4119 unsigned long long msr = 3;
4120 unsigned int i;
4121 double freq;
4122
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004123 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05004124 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05004125
4126 i = msr & 0xf;
4127 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05004128 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01004129 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05004130 }
4131 freq = slm_freq_table[i];
4132
Len Brown96e47152017-01-21 02:26:00 -05004133 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05004134 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05004135
4136 return freq;
4137}
4138
Len Brown103a8fe2010-10-22 23:53:03 -04004139double discover_bclk(unsigned int family, unsigned int model)
4140{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01004141 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04004142 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05004143 else if (is_slm(family, model))
4144 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04004145 else
4146 return 133.33;
4147}
4148
Len Brown889facb2012-11-08 00:48:57 -05004149/*
4150 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4151 * the Thermal Control Circuit (TCC) activates.
4152 * This is usually equal to tjMax.
4153 *
4154 * Older processors do not have this MSR, so there we guess,
4155 * but also allow cmdline over-ride with -T.
4156 *
4157 * Several MSR temperature values are in units of degrees-C
4158 * below this value, including the Digital Thermal Sensor (DTS),
4159 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4160 */
4161int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4162{
4163 unsigned long long msr;
4164 unsigned int target_c_local;
4165 int cpu;
4166
4167 /* tcc_activation_temp is used only for dts or ptm */
4168 if (!(do_dts || do_ptm))
4169 return 0;
4170
4171 /* this is a per-package concept */
4172 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4173 return 0;
4174
4175 cpu = t->cpu_id;
4176 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05004177 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05004178 return -1;
4179 }
4180
4181 if (tcc_activation_temp_override != 0) {
4182 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05004183 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004184 cpu, tcc_activation_temp);
4185 return 0;
4186 }
4187
4188 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05004189 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05004190 goto guess;
4191
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004192 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05004193 goto guess;
4194
Jean Delvare34821242014-05-01 11:40:19 +02004195 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05004196
Len Brown96e47152017-01-21 02:26:00 -05004197 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004198 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05004199 cpu, msr, target_c_local);
4200
Jean Delvare34821242014-05-01 11:40:19 +02004201 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05004202 goto guess;
4203
4204 tcc_activation_temp = target_c_local;
4205
4206 return 0;
4207
4208guess:
4209 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05004210 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05004211 cpu, tcc_activation_temp);
4212
4213 return 0;
4214}
Len Brown69807a62015-11-21 12:22:47 -05004215
Len Brownaa8d8cc2016-03-11 13:26:03 -05004216void decode_feature_control_msr(void)
4217{
4218 unsigned long long msr;
4219
4220 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4221 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4222 base_cpu, msr,
4223 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4224 msr & (1 << 18) ? "SGX" : "");
4225}
4226
Len Brown69807a62015-11-21 12:22:47 -05004227void decode_misc_enable_msr(void)
4228{
4229 unsigned long long msr;
4230
Len Brownf26b1512017-06-23 20:45:54 -07004231 if (!genuine_intel)
4232 return;
4233
Len Brown69807a62015-11-21 12:22:47 -05004234 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05004235 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05004236 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05004237 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4238 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
Len Brownfd3933c2017-11-08 23:15:42 -05004239 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
Len Browne6512622017-01-11 23:17:24 -05004240 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4241 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05004242}
4243
Len Brown33148d62017-01-21 01:26:16 -05004244void decode_misc_feature_control(void)
4245{
4246 unsigned long long msr;
4247
4248 if (!has_misc_feature_control)
4249 return;
4250
4251 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4252 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4253 base_cpu, msr,
4254 msr & (0 << 0) ? "No-" : "",
4255 msr & (1 << 0) ? "No-" : "",
4256 msr & (2 << 0) ? "No-" : "",
4257 msr & (3 << 0) ? "No-" : "");
4258}
Len Brownf0057312015-12-03 01:35:36 -05004259/*
4260 * Decode MSR_MISC_PWR_MGMT
4261 *
4262 * Decode the bits according to the Nehalem documentation
4263 * bit[0] seems to continue to have same meaning going forward
4264 * bit[1] less so...
4265 */
4266void decode_misc_pwr_mgmt_msr(void)
4267{
4268 unsigned long long msr;
4269
4270 if (!do_nhm_platform_info)
4271 return;
4272
Len Browncf4cbe52017-01-01 13:08:33 -05004273 if (no_MSR_MISC_PWR_MGMT)
4274 return;
4275
Len Brownf0057312015-12-03 01:35:36 -05004276 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004277 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 -05004278 base_cpu, msr,
4279 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08004280 msr & (1 << 1) ? "EN" : "DIS",
4281 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05004282}
Len Brown71616c82017-01-07 22:37:48 -05004283/*
4284 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4285 *
4286 * This MSRs are present on Silvermont processors,
4287 * Intel Atom processor E3000 series (Baytrail), and friends.
4288 */
4289void decode_c6_demotion_policy_msr(void)
4290{
4291 unsigned long long msr;
4292
4293 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4294 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4295 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4296
4297 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4298 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4299 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4300}
Len Brown7f5c2582015-12-01 01:36:39 -05004301
Len Brownfcd17212015-03-23 20:29:09 -04004302void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04004303{
Len Brown61a87ba2015-11-23 02:30:51 -05004304 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04004305 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05004306 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04004307
4308 eax = ebx = ecx = edx = 0;
4309
Len Brown5aea2f72016-03-13 03:14:35 -04004310 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004311
4312 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4313 genuine_intel = 1;
4314
Len Brown96e47152017-01-21 02:26:00 -05004315 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004316 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004317 (char *)&ebx, (char *)&edx, (char *)&ecx);
4318
Len Brown5aea2f72016-03-13 03:14:35 -04004319 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004320 family = (fms >> 8) & 0xf;
4321 model = (fms >> 4) & 0xf;
4322 stepping = fms & 0xf;
4323 if (family == 6 || family == 0xf)
4324 model += ((fms >> 16) & 0xf) << 4;
4325
Len Brown96e47152017-01-21 02:26:00 -05004326 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004327 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004328 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004329 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004330 ecx & (1 << 0) ? "SSE3" : "-",
4331 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004332 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004333 ecx & (1 << 7) ? "EIST" : "-",
4334 ecx & (1 << 8) ? "TM2" : "-",
4335 edx & (1 << 4) ? "TSC" : "-",
4336 edx & (1 << 5) ? "MSR" : "-",
4337 edx & (1 << 22) ? "ACPI-TM" : "-",
4338 edx & (1 << 29) ? "TM" : "-");
4339 }
Len Brown103a8fe2010-10-22 23:53:03 -04004340
Josh Triplettb2c95d92013-08-20 17:20:18 -07004341 if (!(edx & (1 << 5)))
4342 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004343
4344 /*
4345 * check max extended function levels of CPUID.
4346 * This is needed to check for invariant TSC.
4347 * This check is valid for both Intel and AMD.
4348 */
4349 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004350 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004351
Len Brown61a87ba2015-11-23 02:30:51 -05004352 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004353
Len Brownd7899442015-01-23 00:12:33 -05004354 /*
4355 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4356 * this check is valid for both Intel and AMD
4357 */
Len Brown5aea2f72016-03-13 03:14:35 -04004358 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004359 has_invariant_tsc = edx & (1 << 8);
4360 }
Len Brown103a8fe2010-10-22 23:53:03 -04004361
4362 /*
4363 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4364 * this check is valid for both Intel and AMD
4365 */
4366
Len Brown5aea2f72016-03-13 03:14:35 -04004367 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004368 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004369 if (has_aperf) {
4370 BIC_PRESENT(BIC_Avg_MHz);
4371 BIC_PRESENT(BIC_Busy);
4372 BIC_PRESENT(BIC_Bzy_MHz);
4373 }
Len Brown889facb2012-11-08 00:48:57 -05004374 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004375 if (do_dts)
4376 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004377 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004378 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004379 if (do_ptm)
4380 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004381 has_hwp = eax & (1 << 7);
4382 has_hwp_notify = eax & (1 << 8);
4383 has_hwp_activity_window = eax & (1 << 9);
4384 has_hwp_epp = eax & (1 << 10);
4385 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004386 has_epb = ecx & (1 << 3);
4387
Len Brown96e47152017-01-21 02:26:00 -05004388 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004389 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004390 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4391 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004392 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004393 do_dts ? "" : "No-",
4394 do_ptm ? "" : "No-",
4395 has_hwp ? "" : "No-",
4396 has_hwp_notify ? "" : "No-",
4397 has_hwp_activity_window ? "" : "No-",
4398 has_hwp_epp ? "" : "No-",
4399 has_hwp_pkg ? "" : "No-",
4400 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004401
Len Brown96e47152017-01-21 02:26:00 -05004402 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004403 decode_misc_enable_msr();
4404
Len Brown33148d62017-01-21 01:26:16 -05004405
Len Brown96e47152017-01-21 02:26:00 -05004406 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004407 int has_sgx;
4408
4409 ecx = 0;
4410
4411 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4412
4413 has_sgx = ebx & (1 << 2);
4414 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4415
4416 if (has_sgx)
4417 decode_feature_control_msr();
4418 }
4419
Len Brown61a87ba2015-11-23 02:30:51 -05004420 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004421 unsigned int eax_crystal;
4422 unsigned int ebx_tsc;
4423
4424 /*
4425 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4426 */
4427 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004428 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004429
4430 if (ebx_tsc != 0) {
4431
Len Brown96e47152017-01-21 02:26:00 -05004432 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004433 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004434 eax_crystal, ebx_tsc, crystal_hz);
4435
4436 if (crystal_hz == 0)
4437 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04004438 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4439 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4440 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4441 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004442 crystal_hz = 24000000; /* 24.0 MHz */
4443 break;
Len Brown7268d402016-12-01 23:10:39 -05004444 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004445 crystal_hz = 25000000; /* 25.0 MHz */
4446 break;
Len Brown869ce692016-06-16 23:22:37 -04004447 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004448 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004449 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004450 break;
4451 default:
4452 crystal_hz = 0;
4453 }
4454
4455 if (crystal_hz) {
4456 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004457 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004458 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004459 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4460 }
4461 }
4462 }
Len Brown61a87ba2015-11-23 02:30:51 -05004463 if (max_level >= 0x16) {
4464 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4465
4466 /*
4467 * CPUID 16H Base MHz, Max MHz, Bus MHz
4468 */
4469 base_mhz = max_mhz = bus_mhz = edx = 0;
4470
Len Brown5aea2f72016-03-13 03:14:35 -04004471 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004472 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004473 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004474 base_mhz, max_mhz, bus_mhz);
4475 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004476
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004477 if (has_aperf)
4478 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4479
Len Brown812db3f2017-02-10 00:25:41 -05004480 BIC_PRESENT(BIC_IRQ);
4481 BIC_PRESENT(BIC_TSC_MHz);
4482
4483 if (probe_nhm_msrs(family, model)) {
4484 do_nhm_platform_info = 1;
4485 BIC_PRESENT(BIC_CPU_c1);
4486 BIC_PRESENT(BIC_CPU_c3);
4487 BIC_PRESENT(BIC_CPU_c6);
4488 BIC_PRESENT(BIC_SMI);
4489 }
Len Brownd7899442015-01-23 00:12:33 -05004490 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004491
4492 if (do_snb_cstates)
4493 BIC_PRESENT(BIC_CPU_c7);
4494
Len Brown5a634262016-04-06 17:15:55 -04004495 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004496 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4497 BIC_PRESENT(BIC_Pkgpc2);
4498 if (pkg_cstate_limit >= PCL__3)
4499 BIC_PRESENT(BIC_Pkgpc3);
4500 if (pkg_cstate_limit >= PCL__6)
4501 BIC_PRESENT(BIC_Pkgpc6);
4502 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4503 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004504 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004505 BIC_NOT_PRESENT(BIC_Pkgpc2);
4506 BIC_NOT_PRESENT(BIC_Pkgpc3);
4507 BIC_PRESENT(BIC_Pkgpc6);
4508 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004509 BIC_PRESENT(BIC_Mod_c6);
4510 use_c1_residency_msr = 1;
4511 }
Len Brown7170a372017-01-27 02:13:27 -05004512 if (is_dnv(family, model)) {
4513 BIC_PRESENT(BIC_CPU_c1);
4514 BIC_NOT_PRESENT(BIC_CPU_c3);
4515 BIC_NOT_PRESENT(BIC_Pkgpc3);
4516 BIC_NOT_PRESENT(BIC_CPU_c7);
4517 BIC_NOT_PRESENT(BIC_Pkgpc7);
4518 use_c1_residency_msr = 1;
4519 }
Len Brown34c761972017-01-27 02:36:41 -05004520 if (is_skx(family, model)) {
4521 BIC_NOT_PRESENT(BIC_CPU_c3);
4522 BIC_NOT_PRESENT(BIC_Pkgpc3);
4523 BIC_NOT_PRESENT(BIC_CPU_c7);
4524 BIC_NOT_PRESENT(BIC_Pkgpc7);
4525 }
Len Brownade0eba2017-02-10 01:56:47 -05004526 if (is_bdx(family, model)) {
4527 BIC_NOT_PRESENT(BIC_CPU_c7);
4528 BIC_NOT_PRESENT(BIC_Pkgpc7);
4529 }
Len Brown0f47c082017-01-27 00:50:45 -05004530 if (has_hsw_msrs(family, model)) {
4531 BIC_PRESENT(BIC_Pkgpc8);
4532 BIC_PRESENT(BIC_Pkgpc9);
4533 BIC_PRESENT(BIC_Pkgpc10);
4534 }
Len Brown5a634262016-04-06 17:15:55 -04004535 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004536 if (has_skl_msrs(family, model)) {
4537 BIC_PRESENT(BIC_Totl_c0);
4538 BIC_PRESENT(BIC_Any_c0);
4539 BIC_PRESENT(BIC_GFX_c0);
4540 BIC_PRESENT(BIC_CPUGFX);
4541 }
Len Brown144b44b2013-11-09 00:30:16 -05004542 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004543 do_knl_cstates = is_knl(family, model);
Srinivas Pandruvada997e5392018-05-31 10:39:07 -07004544 do_cnl_cstates = is_cnl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004545
Len Brown96e47152017-01-21 02:26:00 -05004546 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004547 decode_misc_pwr_mgmt_msr();
4548
Len Brown96e47152017-01-21 02:26:00 -05004549 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004550 decode_c6_demotion_policy_msr();
4551
Len Brown889facb2012-11-08 00:48:57 -05004552 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004553 perf_limit_reasons_probe(family, model);
Artem Bityutskiyac980e12017-09-05 15:14:08 +03004554 automatic_cstate_conversion_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004555
Len Brown96e47152017-01-21 02:26:00 -05004556 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004557 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004558
Len Brown41618e62017-02-09 18:25:22 -05004559 if (!quiet)
4560 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004561 if (!quiet)
4562 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004563
Len Browna2b7b742015-09-26 00:12:38 -04004564 if (has_skl_msrs(family, model))
4565 calculate_tsc_tweak();
4566
Len Brown812db3f2017-02-10 00:25:41 -05004567 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4568 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004569
Len Brown812db3f2017-02-10 00:25:41 -05004570 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4571 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004572
Len Brownbe0e54c2018-06-01 12:35:53 -04004573 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4574 BIC_PRESENT(BIC_CPU_LPI);
4575 else
4576 BIC_NOT_PRESENT(BIC_CPU_LPI);
4577
4578 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4579 BIC_PRESENT(BIC_SYS_LPI);
4580 else
4581 BIC_NOT_PRESENT(BIC_SYS_LPI);
4582
Len Brown96e47152017-01-21 02:26:00 -05004583 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004584 decode_misc_feature_control();
4585
Len Brown889facb2012-11-08 00:48:57 -05004586 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004587}
4588
Len Brown103a8fe2010-10-22 23:53:03 -04004589
4590/*
4591 * in /dev/cpu/ return success for names that are numbers
4592 * ie. filter out ".", "..", "microcode".
4593 */
4594int dir_filter(const struct dirent *dirp)
4595{
4596 if (isdigit(dirp->d_name[0]))
4597 return 1;
4598 else
4599 return 0;
4600}
4601
4602int open_dev_cpu_msr(int dummy1)
4603{
4604 return 0;
4605}
4606
Len Brownc98d5d92012-06-04 00:56:40 -04004607void topology_probe()
4608{
4609 int i;
4610 int max_core_id = 0;
4611 int max_package_id = 0;
4612 int max_siblings = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004613
4614 /* Initialize num_cpus, max_cpu_num */
Prarit Bhargava843c5792018-06-01 10:04:28 -04004615 set_max_cpu_num();
Len Brownc98d5d92012-06-04 00:56:40 -04004616 topo.num_cpus = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04004617 for_all_proc_cpus(count_cpus);
4618 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004619 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004620
Len Brownd8af6f52015-02-10 01:56:38 -05004621 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004622 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004623
4624 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004625 if (cpus == NULL)
4626 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004627
4628 /*
4629 * Allocate and initialize cpu_present_set
4630 */
4631 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004632 if (cpu_present_set == NULL)
4633 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004634 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4635 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4636 for_all_proc_cpus(mark_cpu_present);
4637
4638 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004639 * Validate that all cpus in cpu_subset are also in cpu_present_set
4640 */
4641 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4642 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4643 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4644 err(1, "cpu%d not present", i);
4645 }
4646
4647 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004648 * Allocate and initialize cpu_affinity_set
4649 */
4650 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004651 if (cpu_affinity_set == NULL)
4652 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004653 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4654 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4655
4656
4657 /*
4658 * For online cpus
4659 * find max_core_id, max_package_id
4660 */
4661 for (i = 0; i <= topo.max_cpu_num; ++i) {
4662 int siblings;
4663
4664 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004665 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004666 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004667 continue;
4668 }
Len Brownc98d5d92012-06-04 00:56:40 -04004669
Len Brown0e2d8f02018-06-01 22:08:58 -04004670 cpus[i].logical_cpu_id = i;
4671
4672 /* get package information */
Len Brownc98d5d92012-06-04 00:56:40 -04004673 cpus[i].physical_package_id = get_physical_package_id(i);
4674 if (cpus[i].physical_package_id > max_package_id)
4675 max_package_id = cpus[i].physical_package_id;
4676
Len Brown0e2d8f02018-06-01 22:08:58 -04004677 /* get numa node information */
4678 cpus[i].node_id = get_node_id(&cpus[i]);
4679
4680 /* get core information */
4681 cpus[i].physical_core_id = get_core_id(i);
4682 if (cpus[i].physical_core_id > max_core_id)
4683 max_core_id = cpus[i].physical_core_id;
4684
4685 /* get thread information */
4686 siblings = get_thread_siblings(&cpus[i]);
Len Brownc98d5d92012-06-04 00:56:40 -04004687 if (siblings > max_siblings)
4688 max_siblings = siblings;
Len Brown0e2d8f02018-06-01 22:08:58 -04004689
Len Brownd8af6f52015-02-10 01:56:38 -05004690 if (debug > 1)
Len Brown0e2d8f02018-06-01 22:08:58 -04004691 fprintf(outf, "cpu %d pkg %d node %d core %d\n",
4692 i, cpus[i].physical_package_id,
4693 cpus[i].node_id,
4694 cpus[i].physical_core_id);
Len Brownc98d5d92012-06-04 00:56:40 -04004695 }
4696 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004697 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004698 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004699 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004700 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004701 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004702
4703 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004704 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004705 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004706 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004707 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004708 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004709
4710 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004711 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004712 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004713}
4714
4715void
4716allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4717{
4718 int i;
4719
4720 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004721 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004722 if (*t == NULL)
4723 goto error;
4724
4725 for (i = 0; i < topo.num_threads_per_core *
4726 topo.num_cores_per_pkg * topo.num_packages; i++)
4727 (*t)[i].cpu_id = -1;
4728
4729 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004730 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004731 if (*c == NULL)
4732 goto error;
4733
4734 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4735 (*c)[i].core_id = -1;
4736
Len Brown678a3bd2017-02-09 22:22:13 -05004737 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004738 if (*p == NULL)
4739 goto error;
4740
4741 for (i = 0; i < topo.num_packages; i++)
4742 (*p)[i].package_id = i;
4743
4744 return;
4745error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004746 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004747}
4748/*
4749 * init_counter()
4750 *
4751 * set cpu_id, core_num, pkg_num
4752 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4753 *
4754 * increment topo.num_cores when 1st core in pkg seen
4755 */
4756void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4757 struct pkg_data *pkg_base, int thread_num, int core_num,
4758 int pkg_num, int cpu_id)
4759{
4760 struct thread_data *t;
4761 struct core_data *c;
4762 struct pkg_data *p;
4763
4764 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4765 c = GET_CORE(core_base, core_num, pkg_num);
4766 p = GET_PKG(pkg_base, pkg_num);
4767
4768 t->cpu_id = cpu_id;
4769 if (thread_num == 0) {
4770 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4771 if (cpu_is_first_core_in_package(cpu_id))
4772 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4773 }
4774
4775 c->core_id = core_num;
4776 p->package_id = pkg_num;
4777}
4778
4779
4780int initialize_counters(int cpu_id)
4781{
4782 int my_thread_id, my_core_id, my_package_id;
4783
4784 my_package_id = get_physical_package_id(cpu_id);
4785 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004786 my_thread_id = get_cpu_position_in_core(cpu_id);
4787 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004788 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004789
4790 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4791 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4792 return 0;
4793}
4794
4795void allocate_output_buffer()
4796{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004797 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004798 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004799 if (outp == NULL)
4800 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004801}
Len Brown36229892016-02-26 20:51:02 -05004802void allocate_fd_percpu(void)
4803{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004804 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004805 if (fd_percpu == NULL)
4806 err(-1, "calloc fd_percpu");
4807}
Len Brown562a2d32016-02-26 23:48:05 -05004808void allocate_irq_buffers(void)
4809{
4810 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4811 if (irq_column_2_cpu == NULL)
4812 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004813
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004814 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004815 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004816 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004817}
Len Brownc98d5d92012-06-04 00:56:40 -04004818void setup_all_buffers(void)
4819{
4820 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004821 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004822 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004823 allocate_counters(&thread_even, &core_even, &package_even);
4824 allocate_counters(&thread_odd, &core_odd, &package_odd);
4825 allocate_output_buffer();
4826 for_all_proc_cpus(initialize_counters);
4827}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004828
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004829void set_base_cpu(void)
4830{
4831 base_cpu = sched_getcpu();
4832 if (base_cpu < 0)
4833 err(-ENODEV, "No valid cpus found");
4834
4835 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004836 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004837}
4838
Len Brown103a8fe2010-10-22 23:53:03 -04004839void turbostat_init()
4840{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004841 setup_all_buffers();
4842 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004843 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004844 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004845 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004846
Len Brown103a8fe2010-10-22 23:53:03 -04004847
Len Brown96e47152017-01-21 02:26:00 -05004848 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004849 for_all_cpus(print_hwp, ODD_COUNTERS);
4850
Len Brown96e47152017-01-21 02:26:00 -05004851 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004852 for_all_cpus(print_epb, ODD_COUNTERS);
4853
Len Brown96e47152017-01-21 02:26:00 -05004854 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004855 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4856
Len Brown96e47152017-01-21 02:26:00 -05004857 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004858 for_all_cpus(print_rapl, ODD_COUNTERS);
4859
4860 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4861
Len Brown96e47152017-01-21 02:26:00 -05004862 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004863 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004864
Len Brown96e47152017-01-21 02:26:00 -05004865 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004866 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004867}
4868
4869int fork_it(char **argv)
4870{
Len Brown103a8fe2010-10-22 23:53:03 -04004871 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004872 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004873
Len Brown218f0e82017-02-14 22:07:52 -05004874 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004875 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4876 if (status)
4877 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004878 /* clear affinity side-effect of get_counters() */
4879 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004880 gettimeofday(&tv_even, (struct timezone *)NULL);
4881
4882 child_pid = fork();
4883 if (!child_pid) {
4884 /* child */
4885 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004886 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004887 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004888
4889 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004890 if (child_pid == -1)
4891 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004892
4893 signal(SIGINT, SIG_IGN);
4894 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004895 if (waitpid(child_pid, &status, 0) == -1)
4896 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004897 }
Len Brownc98d5d92012-06-04 00:56:40 -04004898 /*
4899 * n.b. fork_it() does not check for errors from for_all_cpus()
4900 * because re-starting is problematic when forking
4901 */
Len Brown218f0e82017-02-14 22:07:52 -05004902 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004903 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004904 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004905 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004906 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4907 fprintf(outf, "%s: Counter reset detected\n", progname);
4908 else {
4909 compute_average(EVEN_COUNTERS);
4910 format_all_counters(EVEN_COUNTERS);
4911 }
Len Brown103a8fe2010-10-22 23:53:03 -04004912
Len Brownb7d8c142016-02-13 23:36:17 -05004913 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4914
4915 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004916
Len Brownd91bb172012-11-01 00:08:19 -04004917 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004918}
4919
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004920int get_and_dump_counters(void)
4921{
4922 int status;
4923
Len Brown218f0e82017-02-14 22:07:52 -05004924 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004925 status = for_all_cpus(get_counters, ODD_COUNTERS);
4926 if (status)
4927 return status;
4928
4929 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4930 if (status)
4931 return status;
4932
Len Brownb7d8c142016-02-13 23:36:17 -05004933 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004934
4935 return status;
4936}
4937
Len Brownd8af6f52015-02-10 01:56:38 -05004938void print_version() {
Len Brownf7d44a82017-05-27 21:24:58 -07004939 fprintf(outf, "turbostat version 17.06.23"
Len Brownd8af6f52015-02-10 01:56:38 -05004940 " - Len Brown <lenb@kernel.org>\n");
4941}
4942
Len Brown495c76542017-02-08 02:41:51 -05004943int add_counter(unsigned int msr_num, char *path, char *name,
4944 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004945 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004946{
4947 struct msr_counter *msrp;
4948
4949 msrp = calloc(1, sizeof(struct msr_counter));
4950 if (msrp == NULL) {
4951 perror("calloc");
4952 exit(1);
4953 }
4954
4955 msrp->msr_num = msr_num;
4956 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004957 if (path)
4958 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004959 msrp->width = width;
4960 msrp->type = type;
4961 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004962 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004963
4964 switch (scope) {
4965
4966 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004967 msrp->next = sys.tp;
4968 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004969 sys.added_thread_counters++;
Len Brown0748eaf2018-06-01 12:38:29 -04004970 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
Len Brown678a3bd2017-02-09 22:22:13 -05004971 fprintf(stderr, "exceeded max %d added thread counters\n",
4972 MAX_ADDED_COUNTERS);
4973 exit(-1);
4974 }
Len Brown388e9c82016-12-22 23:57:55 -05004975 break;
4976
4977 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004978 msrp->next = sys.cp;
4979 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004980 sys.added_core_counters++;
4981 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4982 fprintf(stderr, "exceeded max %d added core counters\n",
4983 MAX_ADDED_COUNTERS);
4984 exit(-1);
4985 }
Len Brown388e9c82016-12-22 23:57:55 -05004986 break;
4987
4988 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004989 msrp->next = sys.pp;
4990 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004991 sys.added_package_counters++;
4992 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4993 fprintf(stderr, "exceeded max %d added package counters\n",
4994 MAX_ADDED_COUNTERS);
4995 exit(-1);
4996 }
Len Brown388e9c82016-12-22 23:57:55 -05004997 break;
4998 }
4999
5000 return 0;
5001}
5002
5003void parse_add_command(char *add_command)
5004{
5005 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05005006 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05005007 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05005008 int width = 64;
5009 int fail = 0;
5010 enum counter_scope scope = SCOPE_CPU;
5011 enum counter_type type = COUNTER_CYCLES;
5012 enum counter_format format = FORMAT_DELTA;
5013
5014 while (add_command) {
5015
5016 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5017 goto next;
5018
5019 if (sscanf(add_command, "msr%d", &msr_num) == 1)
5020 goto next;
5021
Len Brown495c76542017-02-08 02:41:51 -05005022 if (*add_command == '/') {
5023 path = add_command;
5024 goto next;
5025 }
5026
Len Brown388e9c82016-12-22 23:57:55 -05005027 if (sscanf(add_command, "u%d", &width) == 1) {
5028 if ((width == 32) || (width == 64))
5029 goto next;
5030 width = 64;
5031 }
5032 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5033 scope = SCOPE_CPU;
5034 goto next;
5035 }
5036 if (!strncmp(add_command, "core", strlen("core"))) {
5037 scope = SCOPE_CORE;
5038 goto next;
5039 }
5040 if (!strncmp(add_command, "package", strlen("package"))) {
5041 scope = SCOPE_PACKAGE;
5042 goto next;
5043 }
5044 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5045 type = COUNTER_CYCLES;
5046 goto next;
5047 }
5048 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5049 type = COUNTER_SECONDS;
5050 goto next;
5051 }
Len Brown41618e62017-02-09 18:25:22 -05005052 if (!strncmp(add_command, "usec", strlen("usec"))) {
5053 type = COUNTER_USEC;
5054 goto next;
5055 }
Len Brown388e9c82016-12-22 23:57:55 -05005056 if (!strncmp(add_command, "raw", strlen("raw"))) {
5057 format = FORMAT_RAW;
5058 goto next;
5059 }
5060 if (!strncmp(add_command, "delta", strlen("delta"))) {
5061 format = FORMAT_DELTA;
5062 goto next;
5063 }
5064 if (!strncmp(add_command, "percent", strlen("percent"))) {
5065 format = FORMAT_PERCENT;
5066 goto next;
5067 }
5068
5069 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
5070 char *eos;
5071
5072 eos = strchr(name_buffer, ',');
5073 if (eos)
5074 *eos = '\0';
5075 goto next;
5076 }
5077
5078next:
5079 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05005080 if (add_command) {
5081 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05005082 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05005083 }
Len Brown388e9c82016-12-22 23:57:55 -05005084
5085 }
Len Brown495c76542017-02-08 02:41:51 -05005086 if ((msr_num == 0) && (path == NULL)) {
5087 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05005088 fail++;
5089 }
5090
5091 /* generate default column header */
5092 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05005093 if (width == 32)
5094 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5095 else
5096 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05005097 }
5098
Len Brown41618e62017-02-09 18:25:22 -05005099 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05005100 fail++;
5101
5102 if (fail) {
5103 help();
5104 exit(1);
5105 }
5106}
Len Brown41618e62017-02-09 18:25:22 -05005107
Len Browndd778a52017-02-21 23:21:13 -05005108int is_deferred_skip(char *name)
5109{
5110 int i;
5111
5112 for (i = 0; i < deferred_skip_index; ++i)
5113 if (!strcmp(name, deferred_skip_names[i]))
5114 return 1;
5115 return 0;
5116}
5117
Len Brown41618e62017-02-09 18:25:22 -05005118void probe_sysfs(void)
5119{
5120 char path[64];
5121 char name_buf[16];
5122 FILE *input;
5123 int state;
5124 char *sp;
5125
5126 if (!DO_BIC(BIC_sysfs))
5127 return;
5128
Len Brown0748eaf2018-06-01 12:38:29 -04005129 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005130
5131 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5132 base_cpu, state);
5133 input = fopen(path, "r");
5134 if (input == NULL)
5135 continue;
5136 fgets(name_buf, sizeof(name_buf), input);
5137
5138 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5139 sp = strchr(name_buf, '-');
5140 if (!sp)
5141 sp = strchrnul(name_buf, '\n');
5142 *sp = '%';
5143 *(sp + 1) = '\0';
5144
5145 fclose(input);
5146
5147 sprintf(path, "cpuidle/state%d/time", state);
5148
Len Browndd778a52017-02-21 23:21:13 -05005149 if (is_deferred_skip(name_buf))
5150 continue;
5151
Len Brown41618e62017-02-09 18:25:22 -05005152 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
5153 FORMAT_PERCENT, SYSFS_PERCPU);
5154 }
5155
Len Brown0748eaf2018-06-01 12:38:29 -04005156 for (state = 10; state >= 0; --state) {
Len Brown41618e62017-02-09 18:25:22 -05005157
5158 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5159 base_cpu, state);
5160 input = fopen(path, "r");
5161 if (input == NULL)
5162 continue;
5163 fgets(name_buf, sizeof(name_buf), input);
5164 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5165 sp = strchr(name_buf, '-');
5166 if (!sp)
5167 sp = strchrnul(name_buf, '\n');
5168 *sp = '\0';
5169 fclose(input);
5170
5171 sprintf(path, "cpuidle/state%d/usage", state);
5172
Len Browndd778a52017-02-21 23:21:13 -05005173 if (is_deferred_skip(name_buf))
5174 continue;
5175
Len Brown41618e62017-02-09 18:25:22 -05005176 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
5177 FORMAT_DELTA, SYSFS_PERCPU);
5178 }
5179
5180}
5181
Len Brown1ef7d212017-02-10 23:54:15 -05005182
5183/*
5184 * parse cpuset with following syntax
5185 * 1,2,4..6,8-10 and set bits in cpu_subset
5186 */
5187void parse_cpu_command(char *optarg)
5188{
5189 unsigned int start, end;
5190 char *next;
5191
Len Brown4e4e1e72017-02-21 22:33:42 -05005192 if (!strcmp(optarg, "core")) {
5193 if (cpu_subset)
5194 goto error;
5195 show_core_only++;
5196 return;
5197 }
5198 if (!strcmp(optarg, "package")) {
5199 if (cpu_subset)
5200 goto error;
5201 show_pkg_only++;
5202 return;
5203 }
5204 if (show_core_only || show_pkg_only)
5205 goto error;
5206
Len Brown1ef7d212017-02-10 23:54:15 -05005207 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5208 if (cpu_subset == NULL)
5209 err(3, "CPU_ALLOC");
5210 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5211
5212 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5213
5214 next = optarg;
5215
5216 while (next && *next) {
5217
5218 if (*next == '-') /* no negative cpu numbers */
5219 goto error;
5220
5221 start = strtoul(next, &next, 10);
5222
5223 if (start >= CPU_SUBSET_MAXCPUS)
5224 goto error;
5225 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5226
5227 if (*next == '\0')
5228 break;
5229
5230 if (*next == ',') {
5231 next += 1;
5232 continue;
5233 }
5234
5235 if (*next == '-') {
5236 next += 1; /* start range */
5237 } else if (*next == '.') {
5238 next += 1;
5239 if (*next == '.')
5240 next += 1; /* start range */
5241 else
5242 goto error;
5243 }
5244
5245 end = strtoul(next, &next, 10);
5246 if (end <= start)
5247 goto error;
5248
5249 while (++start <= end) {
5250 if (start >= CPU_SUBSET_MAXCPUS)
5251 goto error;
5252 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5253 }
5254
5255 if (*next == ',')
5256 next += 1;
5257 else if (*next != '\0')
5258 goto error;
5259 }
5260
5261 return;
5262
5263error:
Len Brown4e4e1e72017-02-21 22:33:42 -05005264 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5265 help();
Len Brown1ef7d212017-02-10 23:54:15 -05005266 exit(-1);
5267}
5268
Len Brown812db3f2017-02-10 00:25:41 -05005269
Len Brown103a8fe2010-10-22 23:53:03 -04005270void cmdline(int argc, char **argv)
5271{
5272 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05005273 int option_index = 0;
5274 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05005275 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05005276 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05005277 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05005278 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brown3f44a5c2017-10-17 15:42:56 -04005279 {"enable", required_argument, 0, 'e'},
Len Brownd8af6f52015-02-10 01:56:38 -05005280 {"interval", required_argument, 0, 'i'},
Chen Yu023fe0a2018-04-26 08:41:03 +08005281 {"num_iterations", required_argument, 0, 'n'},
Len Brownd8af6f52015-02-10 01:56:38 -05005282 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05005283 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05005284 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05005285 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05005286 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05005287 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05005288 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05005289 {"Summary", no_argument, 0, 'S'},
5290 {"TCC", required_argument, 0, 'T'},
5291 {"version", no_argument, 0, 'v' },
5292 {0, 0, 0, 0 }
5293 };
Len Brown103a8fe2010-10-22 23:53:03 -04005294
5295 progname = argv[0];
5296
Chen Yu023fe0a2018-04-26 08:41:03 +08005297 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05005298 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04005299 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005300 case 'a':
5301 parse_add_command(optarg);
5302 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005303 case 'c':
5304 parse_cpu_command(optarg);
5305 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005306 case 'D':
5307 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005308 break;
Len Brown3f44a5c2017-10-17 15:42:56 -04005309 case 'e':
5310 /* --enable specified counter */
5311 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5312 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005313 case 'd':
5314 debug++;
Len Brown3f44a5c2017-10-17 15:42:56 -04005315 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brown2f32edf2012-09-21 23:45:46 -04005316 break;
Len Brown812db3f2017-02-10 00:25:41 -05005317 case 'H':
Len Brown3f44a5c2017-10-17 15:42:56 -04005318 /*
5319 * --hide: do not show those specified
5320 * multiple invocations simply clear more bits in enabled mask
5321 */
5322 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
Len Brown812db3f2017-02-10 00:25:41 -05005323 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005324 case 'h':
5325 default:
5326 help();
5327 exit(1);
5328 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005329 {
5330 double interval = strtod(optarg, NULL);
5331
5332 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005333 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005334 interval);
5335 exit(2);
5336 }
5337
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005338 interval_tv.tv_sec = interval_ts.tv_sec = interval;
Len Brownb9ad8ee2017-07-19 19:28:37 -04005339 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
Artem Bityutskiy47936f92017-10-04 15:01:47 +03005340 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
Len Brown2a0609c2016-02-12 22:44:48 -05005341 }
Len Brown889facb2012-11-08 00:48:57 -05005342 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005343 case 'J':
5344 rapl_joules++;
5345 break;
Len Brownc8ade362017-02-15 17:15:11 -05005346 case 'l':
Len Brown3f44a5c2017-10-17 15:42:56 -04005347 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
Len Brownc8ade362017-02-15 17:15:11 -05005348 list_header_only++;
5349 quiet++;
5350 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005351 case 'o':
5352 outf = fopen_or_die(optarg, "w");
5353 break;
Len Brown96e47152017-01-21 02:26:00 -05005354 case 'q':
5355 quiet = 1;
5356 break;
Chen Yu023fe0a2018-04-26 08:41:03 +08005357 case 'n':
5358 num_iterations = strtod(optarg, NULL);
5359
5360 if (num_iterations <= 0) {
5361 fprintf(outf, "iterations %d should be positive number\n",
5362 num_iterations);
5363 exit(2);
5364 }
5365 break;
Len Brown812db3f2017-02-10 00:25:41 -05005366 case 's':
Len Brown3f44a5c2017-10-17 15:42:56 -04005367 /*
5368 * --show: show only those specified
5369 * The 1st invocation will clear and replace the enabled mask
5370 * subsequent invocations can add to it.
5371 */
5372 if (shown == 0)
5373 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5374 else
5375 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5376 shown = 1;
Len Brown812db3f2017-02-10 00:25:41 -05005377 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005378 case 'S':
5379 summary_only++;
5380 break;
5381 case 'T':
5382 tcc_activation_temp_override = atoi(optarg);
5383 break;
5384 case 'v':
5385 print_version();
5386 exit(0);
5387 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005388 }
5389 }
5390}
5391
5392int main(int argc, char **argv)
5393{
Len Brownb7d8c142016-02-13 23:36:17 -05005394 outf = stderr;
5395
Len Brown103a8fe2010-10-22 23:53:03 -04005396 cmdline(argc, argv);
5397
Len Brown96e47152017-01-21 02:26:00 -05005398 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005399 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005400
Len Brown41618e62017-02-09 18:25:22 -05005401 probe_sysfs();
5402
Len Brown103a8fe2010-10-22 23:53:03 -04005403 turbostat_init();
5404
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005405 /* dump counters and exit */
5406 if (dump_only)
5407 return get_and_dump_counters();
5408
Len Brownc8ade362017-02-15 17:15:11 -05005409 /* list header and exit */
5410 if (list_header_only) {
5411 print_header(",");
5412 flush_output_stdout();
5413 return 0;
5414 }
5415
Len Brown103a8fe2010-10-22 23:53:03 -04005416 /*
5417 * if any params left, it must be a command to fork
5418 */
5419 if (argc - optind)
5420 return fork_it(argv + optind);
5421 else
5422 turbostat_loop();
5423
5424 return 0;
5425}