blob: 2b25727bd3d7f6ba7685029230ef4259d9a8fdf5 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce69e2016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
32#include <sys/resource.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <sys/time.h>
36#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050037#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038#include <dirent.h>
39#include <string.h>
40#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040041#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050042#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070043#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040044#include <linux/capability.h>
45#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040046
Len Brown103a8fe2010-10-22 23:53:03 -040047char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050048FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050049int *fd_percpu;
Len Brown2a0609c2016-02-12 22:44:48 -050050struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050051unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050052unsigned int quiet;
Len Brown0de6c0d2017-02-15 21:45:40 -050053unsigned int sums_need_wide_columns;
Len Brownd8af6f52015-02-10 01:56:38 -050054unsigned int rapl_joules;
55unsigned int summary_only;
Len Brownc8ade362017-02-15 17:15:11 -050056unsigned int list_header_only;
Len Brownd8af6f52015-02-10 01:56:38 -050057unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040058unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070059unsigned int do_knl_cstates;
Len Brown144b44b2013-11-09 00:30:16 -050060unsigned int do_slm_cstates;
61unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040062unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050063unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040064unsigned int do_irtl_snb;
65unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050066unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040067unsigned int genuine_intel;
68unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050069unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050070unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020071unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040072double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040073double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040074unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040075double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040076unsigned int show_pkg_only;
77unsigned int show_core_only;
78char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050079unsigned int do_rapl;
80unsigned int do_dts;
81unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050082unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050083unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050084unsigned int tcc_activation_temp;
85unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050086double rapl_power_units, rapl_time_units;
87double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050088double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040089unsigned int do_core_perf_limit_reasons;
90unsigned int do_gfx_perf_limit_reasons;
91unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040092unsigned int crystal_hz;
93unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040094int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040095double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050096unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
97 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
98unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
99unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
100unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
101unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500102unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500103
Len Browne6f9bb32013-12-03 02:19:19 -0500104#define RAPL_PKG (1 << 0)
105 /* 0x610 MSR_PKG_POWER_LIMIT */
106 /* 0x611 MSR_PKG_ENERGY_STATUS */
107#define RAPL_PKG_PERF_STATUS (1 << 1)
108 /* 0x613 MSR_PKG_PERF_STATUS */
109#define RAPL_PKG_POWER_INFO (1 << 2)
110 /* 0x614 MSR_PKG_POWER_INFO */
111
112#define RAPL_DRAM (1 << 3)
113 /* 0x618 MSR_DRAM_POWER_LIMIT */
114 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500115#define RAPL_DRAM_PERF_STATUS (1 << 4)
116 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400117#define RAPL_DRAM_POWER_INFO (1 << 5)
118 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500119
Jacob Pan91484942016-06-16 09:48:20 -0700120#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500121 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400122#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500123 /* 0x63a MSR_PP0_POLICY */
124
Len Brown0b2bb692015-03-26 00:50:30 -0400125#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500126 /* 0x640 MSR_PP1_POWER_LIMIT */
127 /* 0x641 MSR_PP1_ENERGY_STATUS */
128 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700129
130#define RAPL_CORES_ENERGY_STATUS (1 << 9)
131 /* 0x639 MSR_PP0_ENERGY_STATUS */
132#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500133#define TJMAX_DEFAULT 100
134
135#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400136
Len Brown388e9c82016-12-22 23:57:55 -0500137/*
138 * buffer size used by sscanf() for added column names
139 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
140 */
141#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500142#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500143
Len Brown103a8fe2010-10-22 23:53:03 -0400144int backwards_count;
145char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400146
Len Brown1ef7d212017-02-10 23:54:15 -0500147#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
148cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
149size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
Len Brown678a3bd2017-02-09 22:22:13 -0500150#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400151
Len Brownc98d5d92012-06-04 00:56:40 -0400152struct thread_data {
153 unsigned long long tsc;
154 unsigned long long aperf;
155 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500156 unsigned long long c1;
Len Brown0de6c0d2017-02-15 21:45:40 -0500157 unsigned long long irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500158 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400159 unsigned int cpu_id;
160 unsigned int flags;
161#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
162#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500163 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400164} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400165
Len Brownc98d5d92012-06-04 00:56:40 -0400166struct core_data {
167 unsigned long long c3;
168 unsigned long long c6;
169 unsigned long long c7;
Len Brown0539ba112017-02-10 00:27:20 -0500170 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500171 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400172 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500173 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400174} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400175
Len Brownc98d5d92012-06-04 00:56:40 -0400176struct pkg_data {
177 unsigned long long pc2;
178 unsigned long long pc3;
179 unsigned long long pc6;
180 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800181 unsigned long long pc8;
182 unsigned long long pc9;
183 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400184 unsigned long long pkg_wtd_core_c0;
185 unsigned long long pkg_any_core_c0;
186 unsigned long long pkg_any_gfxe_c0;
187 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400188 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500189 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400190 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500191 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
192 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
193 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
194 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
195 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
196 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
197 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500198 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400199} *package_even, *package_odd;
200
201#define ODD_COUNTERS thread_odd, core_odd, package_odd
202#define EVEN_COUNTERS thread_even, core_even, package_even
203
204#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
205 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
206 topo.num_threads_per_core + \
207 (core_no) * topo.num_threads_per_core + (thread_no))
208#define GET_CORE(core_base, core_no, pkg_no) \
209 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
210#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
211
Len Brown388e9c82016-12-22 23:57:55 -0500212enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500213enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500214enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
215
216struct msr_counter {
217 unsigned int msr_num;
218 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500219 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500220 unsigned int width;
221 enum counter_type type;
222 enum counter_format format;
223 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500224 unsigned int flags;
225#define FLAGS_HIDE (1 << 0)
226#define FLAGS_SHOW (1 << 1)
Len Brown41618e62017-02-09 18:25:22 -0500227#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500228};
229
230struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500231 unsigned int added_thread_counters;
232 unsigned int added_core_counters;
233 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500234 struct msr_counter *tp;
235 struct msr_counter *cp;
236 struct msr_counter *pp;
237} sys;
238
Len Brownc98d5d92012-06-04 00:56:40 -0400239struct system_summary {
240 struct thread_data threads;
241 struct core_data cores;
242 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500243} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400244
245
246struct topo_params {
247 int num_packages;
248 int num_cpus;
249 int num_cores;
250 int max_cpu_num;
251 int num_cores_per_pkg;
252 int num_threads_per_core;
253} topo;
254
255struct timeval tv_even, tv_odd, tv_delta;
256
Len Brown562a2d32016-02-26 23:48:05 -0500257int *irq_column_2_cpu; /* /proc/interrupts column numbers */
258int *irqs_per_cpu; /* indexed by cpu_num */
259
Len Brownc98d5d92012-06-04 00:56:40 -0400260void setup_all_buffers(void);
261
262int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400263{
Len Brownc98d5d92012-06-04 00:56:40 -0400264 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400265}
Len Brown88c32812012-03-29 21:44:40 -0400266/*
Len Brownc98d5d92012-06-04 00:56:40 -0400267 * run func(thread, core, package) in topology order
268 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400269 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400270
Len Brownc98d5d92012-06-04 00:56:40 -0400271int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
272 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400273{
Len Brownc98d5d92012-06-04 00:56:40 -0400274 int retval, pkg_no, core_no, thread_no;
275
276 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
277 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
278 for (thread_no = 0; thread_no <
279 topo.num_threads_per_core; ++thread_no) {
280 struct thread_data *t;
281 struct core_data *c;
282 struct pkg_data *p;
283
284 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
285
286 if (cpu_is_not_present(t->cpu_id))
287 continue;
288
289 c = GET_CORE(core_base, core_no, pkg_no);
290 p = GET_PKG(pkg_base, pkg_no);
291
292 retval = func(t, c, p);
293 if (retval)
294 return retval;
295 }
296 }
297 }
298 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400299}
300
301int cpu_migrate(int cpu)
302{
Len Brownc98d5d92012-06-04 00:56:40 -0400303 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
304 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
305 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400306 return -1;
307 else
308 return 0;
309}
Len Brown36229892016-02-26 20:51:02 -0500310int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400311{
Len Brown103a8fe2010-10-22 23:53:03 -0400312 char pathname[32];
313 int fd;
314
Len Brown36229892016-02-26 20:51:02 -0500315 fd = fd_percpu[cpu];
316
317 if (fd)
318 return fd;
319
Len Brown103a8fe2010-10-22 23:53:03 -0400320 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
321 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400322 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400323 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 -0400324
Len Brown36229892016-02-26 20:51:02 -0500325 fd_percpu[cpu] = fd;
326
327 return fd;
328}
329
330int get_msr(int cpu, off_t offset, unsigned long long *msr)
331{
332 ssize_t retval;
333
334 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400335
Len Brown98481e72014-08-15 00:36:50 -0400336 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500337 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400338
339 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400340}
341
Len Brownfc04cc62014-02-06 00:55:19 -0500342/*
Len Brown812db3f2017-02-10 00:25:41 -0500343 * Each string in this array is compared in --show and --hide cmdline.
344 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500345 */
Len Brown812db3f2017-02-10 00:25:41 -0500346struct msr_counter bic[] = {
347 { 0x0, "Package" },
348 { 0x0, "Avg_MHz" },
349 { 0x0, "Bzy_MHz" },
350 { 0x0, "TSC_MHz" },
351 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500352 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500353 { 0x0, "Busy%" },
354 { 0x0, "CPU%c1" },
355 { 0x0, "CPU%c3" },
356 { 0x0, "CPU%c6" },
357 { 0x0, "CPU%c7" },
358 { 0x0, "ThreadC" },
359 { 0x0, "CoreTmp" },
360 { 0x0, "CoreCnt" },
361 { 0x0, "PkgTmp" },
362 { 0x0, "GFX%rc6" },
363 { 0x0, "GFXMHz" },
364 { 0x0, "Pkg%pc2" },
365 { 0x0, "Pkg%pc3" },
366 { 0x0, "Pkg%pc6" },
367 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500368 { 0x0, "Pkg%pc8" },
369 { 0x0, "Pkg%pc9" },
370 { 0x0, "Pkg%pc10" },
Len Brown812db3f2017-02-10 00:25:41 -0500371 { 0x0, "PkgWatt" },
372 { 0x0, "CorWatt" },
373 { 0x0, "GFXWatt" },
374 { 0x0, "PkgCnt" },
375 { 0x0, "RAMWatt" },
376 { 0x0, "PKG_%" },
377 { 0x0, "RAM_%" },
378 { 0x0, "Pkg_J" },
379 { 0x0, "Cor_J" },
380 { 0x0, "GFX_J" },
381 { 0x0, "RAM_J" },
382 { 0x0, "Core" },
383 { 0x0, "CPU" },
Len Brown0539ba112017-02-10 00:27:20 -0500384 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500385 { 0x0, "sysfs" },
Len Browna99d8732017-05-20 20:11:55 -0400386 { 0x0, "Totl%C0" },
387 { 0x0, "Any%C0" },
388 { 0x0, "GFX%C0" },
389 { 0x0, "CPUGFX%" },
Len Brown812db3f2017-02-10 00:25:41 -0500390};
391
Len Browna99d8732017-05-20 20:11:55 -0400392
393
Len Brown812db3f2017-02-10 00:25:41 -0500394#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
395#define BIC_Package (1ULL << 0)
396#define BIC_Avg_MHz (1ULL << 1)
397#define BIC_Bzy_MHz (1ULL << 2)
398#define BIC_TSC_MHz (1ULL << 3)
399#define BIC_IRQ (1ULL << 4)
400#define BIC_SMI (1ULL << 5)
401#define BIC_Busy (1ULL << 6)
402#define BIC_CPU_c1 (1ULL << 7)
403#define BIC_CPU_c3 (1ULL << 8)
404#define BIC_CPU_c6 (1ULL << 9)
405#define BIC_CPU_c7 (1ULL << 10)
406#define BIC_ThreadC (1ULL << 11)
407#define BIC_CoreTmp (1ULL << 12)
408#define BIC_CoreCnt (1ULL << 13)
409#define BIC_PkgTmp (1ULL << 14)
410#define BIC_GFX_rc6 (1ULL << 15)
411#define BIC_GFXMHz (1ULL << 16)
412#define BIC_Pkgpc2 (1ULL << 17)
413#define BIC_Pkgpc3 (1ULL << 18)
414#define BIC_Pkgpc6 (1ULL << 19)
415#define BIC_Pkgpc7 (1ULL << 20)
Len Brown0f47c082017-01-27 00:50:45 -0500416#define BIC_Pkgpc8 (1ULL << 21)
417#define BIC_Pkgpc9 (1ULL << 22)
418#define BIC_Pkgpc10 (1ULL << 23)
419#define BIC_PkgWatt (1ULL << 24)
420#define BIC_CorWatt (1ULL << 25)
421#define BIC_GFXWatt (1ULL << 26)
422#define BIC_PkgCnt (1ULL << 27)
423#define BIC_RAMWatt (1ULL << 28)
424#define BIC_PKG__ (1ULL << 29)
425#define BIC_RAM__ (1ULL << 30)
426#define BIC_Pkg_J (1ULL << 31)
427#define BIC_Cor_J (1ULL << 32)
428#define BIC_GFX_J (1ULL << 33)
429#define BIC_RAM_J (1ULL << 34)
430#define BIC_Core (1ULL << 35)
431#define BIC_CPU (1ULL << 36)
432#define BIC_Mod_c6 (1ULL << 37)
Len Brown41618e62017-02-09 18:25:22 -0500433#define BIC_sysfs (1ULL << 38)
Len Browna99d8732017-05-20 20:11:55 -0400434#define BIC_Totl_c0 (1ULL << 39)
435#define BIC_Any_c0 (1ULL << 40)
436#define BIC_GFX_c0 (1ULL << 41)
437#define BIC_CPUGFX (1ULL << 42)
Len Brown812db3f2017-02-10 00:25:41 -0500438
439unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
Len Brown41618e62017-02-09 18:25:22 -0500440unsigned long long bic_present = BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500441
442#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
443#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500444#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500445
Len Browndd778a52017-02-21 23:21:13 -0500446#define MAX_DEFERRED 16
447char *deferred_skip_names[MAX_DEFERRED];
448int deferred_skip_index;
449
450/*
451 * HIDE_LIST - hide this list of counters, show the rest [default]
452 * SHOW_LIST - show this list of counters, hide the rest
453 */
454enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
455
456void help(void)
457{
458 fprintf(outf,
459 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
460 "\n"
461 "Turbostat forks the specified COMMAND and prints statistics\n"
462 "when COMMAND completes.\n"
463 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
464 "to print statistics, until interrupted.\n"
465 "--add add a counter\n"
466 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
467 "--cpu cpu-set limit output to summary plus cpu-set:\n"
468 " {core | package | j,k,l..m,n-p }\n"
469 "--quiet skip decoding system configuration header\n"
470 "--interval sec Override default 5-second measurement interval\n"
471 "--help print this help message\n"
472 "--list list column headers only\n"
473 "--out file create or truncate \"file\" for all output\n"
474 "--version print version information\n"
475 "\n"
476 "For more help, run \"man turbostat\"\n");
477}
478
Len Brown812db3f2017-02-10 00:25:41 -0500479/*
480 * bic_lookup
481 * for all the strings in comma separate name_list,
482 * set the approprate bit in return value.
483 */
Len Browndd778a52017-02-21 23:21:13 -0500484unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
Len Brown812db3f2017-02-10 00:25:41 -0500485{
486 int i;
487 unsigned long long retval = 0;
488
489 while (name_list) {
490 char *comma;
491
492 comma = strchr(name_list, ',');
493
494 if (comma)
495 *comma = '\0';
496
497 for (i = 0; i < MAX_BIC; ++i) {
498 if (!strcmp(name_list, bic[i].name)) {
499 retval |= (1ULL << i);
500 break;
501 }
502 }
503 if (i == MAX_BIC) {
Len Browndd778a52017-02-21 23:21:13 -0500504 if (mode == SHOW_LIST) {
505 fprintf(stderr, "Invalid counter name: %s\n", name_list);
506 exit(-1);
507 }
508 deferred_skip_names[deferred_skip_index++] = name_list;
509 if (debug)
510 fprintf(stderr, "deferred \"%s\"\n", name_list);
511 if (deferred_skip_index >= MAX_DEFERRED) {
512 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
513 MAX_DEFERRED, name_list);
514 help();
515 exit(1);
516 }
Len Brown812db3f2017-02-10 00:25:41 -0500517 }
518
519 name_list = comma;
520 if (name_list)
521 name_list++;
522
523 }
524 return retval;
525}
Len Brownfc04cc62014-02-06 00:55:19 -0500526
Len Browndd778a52017-02-21 23:21:13 -0500527
Len Brownc8ade362017-02-15 17:15:11 -0500528void print_header(char *delim)
Len Brown103a8fe2010-10-22 23:53:03 -0400529{
Len Brown388e9c82016-12-22 23:57:55 -0500530 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500531 int printed = 0;
Len Brown388e9c82016-12-22 23:57:55 -0500532
Len Brown812db3f2017-02-10 00:25:41 -0500533 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500534 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500535 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500536 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500537 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500538 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500539 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500540 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500541 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500542 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500543 if (DO_BIC(BIC_Bzy_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500544 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500545 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500546 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500547
Len Brown0de6c0d2017-02-15 21:45:40 -0500548 if (DO_BIC(BIC_IRQ)) {
549 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500550 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500551 else
Len Brown6168c2e2017-02-16 23:07:51 -0500552 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
Len Brown0de6c0d2017-02-15 21:45:40 -0500553 }
554
Len Brown812db3f2017-02-10 00:25:41 -0500555 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500556 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
Len Brown1cc21f72015-02-23 00:34:57 -0500557
Len Brown388e9c82016-12-22 23:57:55 -0500558 for (mp = sys.tp; mp; mp = mp->next) {
Len Browndd778a52017-02-21 23:21:13 -0500559
Len Brown388e9c82016-12-22 23:57:55 -0500560 if (mp->format == FORMAT_RAW) {
561 if (mp->width == 64)
Len Browndd778a52017-02-21 23:21:13 -0500562 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500563 else
Len Browndd778a52017-02-21 23:21:13 -0500564 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500565 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500566 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Browndd778a52017-02-21 23:21:13 -0500567 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
Len Brown0de6c0d2017-02-15 21:45:40 -0500568 else
Len Browndd778a52017-02-21 23:21:13 -0500569 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500570 }
571 }
572
Len Brown41618e62017-02-09 18:25:22 -0500573 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500574 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500575 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500576 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500577 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500578 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500579 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500580 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500581
Len Brown0539ba112017-02-10 00:27:20 -0500582 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500583 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
Len Brown678a3bd2017-02-09 22:22:13 -0500584
Len Brown812db3f2017-02-10 00:25:41 -0500585 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500586 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
Len Brown388e9c82016-12-22 23:57:55 -0500587
588 for (mp = sys.cp; mp; mp = mp->next) {
589 if (mp->format == FORMAT_RAW) {
590 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500591 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500592 else
Len Brownc8ade362017-02-15 17:15:11 -0500593 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500594 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500595 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
596 outp += sprintf(outp, "%s%8s", delim, mp->name);
597 else
598 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500599 }
600 }
601
Len Brown812db3f2017-02-10 00:25:41 -0500602 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500603 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
Len Brown889facb2012-11-08 00:48:57 -0500604
Len Brown812db3f2017-02-10 00:25:41 -0500605 if (DO_BIC(BIC_GFX_rc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500606 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
Len Brownfdf676e2016-02-27 01:28:12 -0500607
Len Brown812db3f2017-02-10 00:25:41 -0500608 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500609 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
Len Brown27d47352016-02-27 00:37:54 -0500610
Len Browna99d8732017-05-20 20:11:55 -0400611 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500612 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400613 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500614 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400615 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500616 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
Len Browna99d8732017-05-20 20:11:55 -0400617 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500618 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
Len Brown0b2bb692015-03-26 00:50:30 -0400619
Len Brown0f47c082017-01-27 00:50:45 -0500620 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500621 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500622 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500623 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500624 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500625 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500626 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500627 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500628 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500629 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500630 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500631 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
Len Brown0f47c082017-01-27 00:50:45 -0500632 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500633 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400634
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800635 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500636 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500637 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500638 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500639 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500640 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500641 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500642 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500643 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500644 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500645 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500646 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500647 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Len Brownd7899442015-01-23 00:12:33 -0500648 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500649 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500650 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500651 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500652 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500653 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500654 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500655 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500656 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500657 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500658 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500659 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500660 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800661 }
Len Brown388e9c82016-12-22 23:57:55 -0500662 for (mp = sys.pp; mp; mp = mp->next) {
663 if (mp->format == FORMAT_RAW) {
664 if (mp->width == 64)
Len Brownc8ade362017-02-15 17:15:11 -0500665 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500666 else
Len Brownc8ade362017-02-15 17:15:11 -0500667 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500668 } else {
Len Brown0de6c0d2017-02-15 21:45:40 -0500669 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
670 outp += sprintf(outp, "%s%8s", delim, mp->name);
671 else
672 outp += sprintf(outp, "%s%s", delim, mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500673 }
674 }
675
Len Brownc98d5d92012-06-04 00:56:40 -0400676 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400677}
678
Len Brownc98d5d92012-06-04 00:56:40 -0400679int dump_counters(struct thread_data *t, struct core_data *c,
680 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400681{
Len Brown388e9c82016-12-22 23:57:55 -0500682 int i;
683 struct msr_counter *mp;
684
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200685 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400686
Len Brownc98d5d92012-06-04 00:56:40 -0400687 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200688 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
689 t->cpu_id, t->flags);
690 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
691 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
692 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
693 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500694
Len Brown812db3f2017-02-10 00:25:41 -0500695 if (DO_BIC(BIC_IRQ))
Len Brown0de6c0d2017-02-15 21:45:40 -0500696 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500697 if (DO_BIC(BIC_SMI))
Len Brown218f0e82017-02-14 22:07:52 -0500698 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500699
700 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
701 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
702 i, mp->msr_num, t->counter[i]);
703 }
Len Brownc98d5d92012-06-04 00:56:40 -0400704 }
Len Brown103a8fe2010-10-22 23:53:03 -0400705
Len Brownc98d5d92012-06-04 00:56:40 -0400706 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200707 outp += sprintf(outp, "core: %d\n", c->core_id);
708 outp += sprintf(outp, "c3: %016llX\n", c->c3);
709 outp += sprintf(outp, "c6: %016llX\n", c->c6);
710 outp += sprintf(outp, "c7: %016llX\n", c->c7);
711 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500712
713 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
714 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
715 i, mp->msr_num, c->counter[i]);
716 }
Len Brown0539ba112017-02-10 00:27:20 -0500717 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400718 }
719
720 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200721 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400722
723 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
724 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
725 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
726 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
727
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200728 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500729 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500730 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500731 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500732 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500733 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500734 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200735 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
736 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
737 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
738 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
739 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
740 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
741 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
742 outp += sprintf(outp, "Throttle PKG: %0X\n",
743 p->rapl_pkg_perf_status);
744 outp += sprintf(outp, "Throttle RAM: %0X\n",
745 p->rapl_dram_perf_status);
746 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500747
748 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
749 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
750 i, mp->msr_num, p->counter[i]);
751 }
Len Brownc98d5d92012-06-04 00:56:40 -0400752 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200753
754 outp += sprintf(outp, "\n");
755
Len Brownc98d5d92012-06-04 00:56:40 -0400756 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400757}
758
Len Browne23da032012-02-06 18:37:16 -0500759/*
760 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500761 */
Len Brownc98d5d92012-06-04 00:56:40 -0400762int format_counters(struct thread_data *t, struct core_data *c,
763 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400764{
Len Brown008d396e2017-02-10 00:29:51 -0500765 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500766 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500767 int i;
768 struct msr_counter *mp;
Len Brown6168c2e2017-02-16 23:07:51 -0500769 char *delim = "\t";
770 int printed = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400771
Len Brownc98d5d92012-06-04 00:56:40 -0400772 /* if showing only 1st thread in core and this isn't one, bail out */
773 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
774 return 0;
775
776 /* if showing only 1st thread in pkg and this isn't one, bail out */
777 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
778 return 0;
779
Len Brown1ef7d212017-02-10 23:54:15 -0500780 /*if not summary line and --cpu is used */
781 if ((t != &average.threads) &&
782 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
783 return 0;
784
Len Brown103a8fe2010-10-22 23:53:03 -0400785 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
786
Len Brown008d396e2017-02-10 00:29:51 -0500787 tsc = t->tsc * tsc_tweak;
788
Len Brownc98d5d92012-06-04 00:56:40 -0400789 /* topo columns, print blanks on 1st (average) line */
790 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500791 if (DO_BIC(BIC_Package))
Len Brown6168c2e2017-02-16 23:07:51 -0500792 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500793 if (DO_BIC(BIC_Core))
Len Brown6168c2e2017-02-16 23:07:51 -0500794 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown812db3f2017-02-10 00:25:41 -0500795 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500796 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brown103a8fe2010-10-22 23:53:03 -0400797 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500798 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400799 if (p)
Len Brown6168c2e2017-02-16 23:07:51 -0500800 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400801 else
Len Brown6168c2e2017-02-16 23:07:51 -0500802 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400803 }
Len Brown812db3f2017-02-10 00:25:41 -0500804 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400805 if (c)
Len Brown6168c2e2017-02-16 23:07:51 -0500806 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400807 else
Len Brown6168c2e2017-02-16 23:07:51 -0500808 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
Len Brownc98d5d92012-06-04 00:56:40 -0400809 }
Len Brown812db3f2017-02-10 00:25:41 -0500810 if (DO_BIC(BIC_CPU))
Len Brown6168c2e2017-02-16 23:07:51 -0500811 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400812 }
Len Brownfc04cc62014-02-06 00:55:19 -0500813
Len Brown812db3f2017-02-10 00:25:41 -0500814 if (DO_BIC(BIC_Avg_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500815 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brownfc04cc62014-02-06 00:55:19 -0500816 1.0 / units * t->aperf / interval_float);
817
Len Brown812db3f2017-02-10 00:25:41 -0500818 if (DO_BIC(BIC_Busy))
Len Brown6168c2e2017-02-16 23:07:51 -0500819 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400820
Len Brown812db3f2017-02-10 00:25:41 -0500821 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400822 if (has_base_hz)
Len Brown6168c2e2017-02-16 23:07:51 -0500823 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400824 else
Len Brown6168c2e2017-02-16 23:07:51 -0500825 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
Len Brown008d396e2017-02-10 00:29:51 -0500826 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400827 }
Len Brown103a8fe2010-10-22 23:53:03 -0400828
Len Brown812db3f2017-02-10 00:25:41 -0500829 if (DO_BIC(BIC_TSC_MHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500830 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400831
Len Brown562a2d32016-02-26 23:48:05 -0500832 /* IRQ */
Len Brown0de6c0d2017-02-15 21:45:40 -0500833 if (DO_BIC(BIC_IRQ)) {
834 if (sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500835 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500836 else
Len Brown6168c2e2017-02-16 23:07:51 -0500837 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
Len Brown0de6c0d2017-02-15 21:45:40 -0500838 }
Len Brown562a2d32016-02-26 23:48:05 -0500839
Len Brown1cc21f72015-02-23 00:34:57 -0500840 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500841 if (DO_BIC(BIC_SMI))
Len Brown6168c2e2017-02-16 23:07:51 -0500842 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500843
Len Brown678a3bd2017-02-09 22:22:13 -0500844 /* Added counters */
845 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
846 if (mp->format == FORMAT_RAW) {
847 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500848 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500849 else
Len Brown6168c2e2017-02-16 23:07:51 -0500850 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500851 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500852 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500853 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500854 else
Len Brown6168c2e2017-02-16 23:07:51 -0500855 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
Len Brown678a3bd2017-02-09 22:22:13 -0500856 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500857 if (mp->type == COUNTER_USEC)
Len Brown6168c2e2017-02-16 23:07:51 -0500858 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
Len Brown41618e62017-02-09 18:25:22 -0500859 else
Len Brown6168c2e2017-02-16 23:07:51 -0500860 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500861 }
862 }
863
Len Brown41618e62017-02-09 18:25:22 -0500864 /* C1 */
865 if (DO_BIC(BIC_CPU_c1))
Len Brown6168c2e2017-02-16 23:07:51 -0500866 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
Len Brown41618e62017-02-09 18:25:22 -0500867
868
Len Brownc98d5d92012-06-04 00:56:40 -0400869 /* print per-core data only for 1st thread in core */
870 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
871 goto done;
872
Len Brown812db3f2017-02-10 00:25:41 -0500873 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown6168c2e2017-02-16 23:07:51 -0500874 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500875 if (DO_BIC(BIC_CPU_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500876 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500877 if (DO_BIC(BIC_CPU_c7))
Len Brown6168c2e2017-02-16 23:07:51 -0500878 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400879
Len Brown0539ba112017-02-10 00:27:20 -0500880 /* Mod%c6 */
881 if (DO_BIC(BIC_Mod_c6))
Len Brown6168c2e2017-02-16 23:07:51 -0500882 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
Len Brown0539ba112017-02-10 00:27:20 -0500883
Len Brown812db3f2017-02-10 00:25:41 -0500884 if (DO_BIC(BIC_CoreTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500885 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500886
Len Brown388e9c82016-12-22 23:57:55 -0500887 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
888 if (mp->format == FORMAT_RAW) {
889 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500890 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500891 else
Len Brown6168c2e2017-02-16 23:07:51 -0500892 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500893 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500894 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500895 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500896 else
Len Brown6168c2e2017-02-16 23:07:51 -0500897 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500898 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500899 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500900 }
901 }
902
Len Brownc98d5d92012-06-04 00:56:40 -0400903 /* print per-package data only for 1st core in package */
904 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
905 goto done;
906
Len Brown0b2bb692015-03-26 00:50:30 -0400907 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500908 if (DO_BIC(BIC_PkgTmp))
Len Brown6168c2e2017-02-16 23:07:51 -0500909 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500910
Len Brownfdf676e2016-02-27 01:28:12 -0500911 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500912 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400913 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown6168c2e2017-02-16 23:07:51 -0500914 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
Len Brown9185e982016-04-06 17:16:00 -0400915 } else {
Len Brown6168c2e2017-02-16 23:07:51 -0500916 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
Len Brown9185e982016-04-06 17:16:00 -0400917 p->gfx_rc6_ms / 10.0 / interval_float);
918 }
919 }
Len Brownfdf676e2016-02-27 01:28:12 -0500920
Len Brown27d47352016-02-27 00:37:54 -0500921 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500922 if (DO_BIC(BIC_GFXMHz))
Len Brown6168c2e2017-02-16 23:07:51 -0500923 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500924
Len Brown0b2bb692015-03-26 00:50:30 -0400925 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
Len Browna99d8732017-05-20 20:11:55 -0400926 if (DO_BIC(BIC_Totl_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500927 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400928 if (DO_BIC(BIC_Any_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500929 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400930 if (DO_BIC(BIC_GFX_c0))
Len Brown6168c2e2017-02-16 23:07:51 -0500931 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
Len Browna99d8732017-05-20 20:11:55 -0400932 if (DO_BIC(BIC_CPUGFX))
Len Brown6168c2e2017-02-16 23:07:51 -0500933 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400934
Len Brown0f47c082017-01-27 00:50:45 -0500935 if (DO_BIC(BIC_Pkgpc2))
Len Brown6168c2e2017-02-16 23:07:51 -0500936 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500937 if (DO_BIC(BIC_Pkgpc3))
Len Brown6168c2e2017-02-16 23:07:51 -0500938 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500939 if (DO_BIC(BIC_Pkgpc6))
Len Brown6168c2e2017-02-16 23:07:51 -0500940 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500941 if (DO_BIC(BIC_Pkgpc7))
Len Brown6168c2e2017-02-16 23:07:51 -0500942 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500943 if (DO_BIC(BIC_Pkgpc8))
Len Brown6168c2e2017-02-16 23:07:51 -0500944 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500945 if (DO_BIC(BIC_Pkgpc9))
Len Brown6168c2e2017-02-16 23:07:51 -0500946 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500947 if (DO_BIC(BIC_Pkgpc10))
Len Brown6168c2e2017-02-16 23:07:51 -0500948 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500949
950 /*
951 * If measurement interval exceeds minimum RAPL Joule Counter range,
952 * indicate that results are suspect by printing "**" in fraction place.
953 */
Len Brownfc04cc62014-02-06 00:55:19 -0500954 if (interval_float < rapl_joule_counter_range)
Len Brown6168c2e2017-02-16 23:07:51 -0500955 fmt8 = "%s%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500956 else
Len Browne975db52016-04-06 23:56:02 -0400957 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500958
Len Brown812db3f2017-02-10 00:25:41 -0500959 if (DO_BIC(BIC_PkgWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500960 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500961 if (DO_BIC(BIC_CorWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500962 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500963 if (DO_BIC(BIC_GFXWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500964 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500965 if (DO_BIC(BIC_RAMWatt))
Len Brown6168c2e2017-02-16 23:07:51 -0500966 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
Len Brown812db3f2017-02-10 00:25:41 -0500967 if (DO_BIC(BIC_Pkg_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500968 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500969 if (DO_BIC(BIC_Cor_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500970 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500971 if (DO_BIC(BIC_GFX_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500972 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500973 if (DO_BIC(BIC_RAM_J))
Len Brown6168c2e2017-02-16 23:07:51 -0500974 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
Len Brown812db3f2017-02-10 00:25:41 -0500975 if (DO_BIC(BIC_PKG__))
Len Brown6168c2e2017-02-16 23:07:51 -0500976 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 -0500977 if (DO_BIC(BIC_RAM__))
Len Brown6168c2e2017-02-16 23:07:51 -0500978 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 -0500979
Len Brown388e9c82016-12-22 23:57:55 -0500980 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
981 if (mp->format == FORMAT_RAW) {
982 if (mp->width == 32)
Len Brown5f3aea52017-02-23 18:10:27 -0500983 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500984 else
Len Brown6168c2e2017-02-16 23:07:51 -0500985 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500986 } else if (mp->format == FORMAT_DELTA) {
Len Brown0de6c0d2017-02-15 21:45:40 -0500987 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
Len Brown6168c2e2017-02-16 23:07:51 -0500988 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown0de6c0d2017-02-15 21:45:40 -0500989 else
Len Brown6168c2e2017-02-16 23:07:51 -0500990 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500991 } else if (mp->format == FORMAT_PERCENT) {
Len Brown6168c2e2017-02-16 23:07:51 -0500992 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500993 }
994 }
995
Len Brownc98d5d92012-06-04 00:56:40 -0400996done:
Len Brownc98d5d92012-06-04 00:56:40 -0400997 outp += sprintf(outp, "\n");
998
999 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001000}
1001
Len Brownb7d8c142016-02-13 23:36:17 -05001002void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001003{
Len Brownb7d8c142016-02-13 23:36:17 -05001004 FILE *filep;
1005
1006 if (outf == stderr)
1007 filep = stdout;
1008 else
1009 filep = outf;
1010
1011 fputs(output_buffer, filep);
1012 fflush(filep);
1013
Len Brownc98d5d92012-06-04 00:56:40 -04001014 outp = output_buffer;
1015}
Len Brownb7d8c142016-02-13 23:36:17 -05001016void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -04001017{
Len Brownb7d8c142016-02-13 23:36:17 -05001018 fputs(output_buffer, outf);
1019 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -04001020 outp = output_buffer;
1021}
1022void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1023{
Len Browne23da032012-02-06 18:37:16 -05001024 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -04001025
Len Browne23da032012-02-06 18:37:16 -05001026 if (!printed || !summary_only)
Len Brownc8ade362017-02-15 17:15:11 -05001027 print_header("\t");
Len Brown103a8fe2010-10-22 23:53:03 -04001028
Len Brownc98d5d92012-06-04 00:56:40 -04001029 if (topo.num_cpus > 1)
1030 format_counters(&average.threads, &average.cores,
1031 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -04001032
Len Browne23da032012-02-06 18:37:16 -05001033 printed = 1;
1034
1035 if (summary_only)
1036 return;
1037
Len Brownc98d5d92012-06-04 00:56:40 -04001038 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -04001039}
1040
Len Brown889facb2012-11-08 00:48:57 -05001041#define DELTA_WRAP32(new, old) \
1042 if (new > old) { \
1043 old = new - old; \
1044 } else { \
1045 old = 0x100000000 + new - old; \
1046 }
1047
Len Brownba3dec92016-04-22 20:31:46 -04001048int
Len Brownc98d5d92012-06-04 00:56:40 -04001049delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -04001050{
Len Brown388e9c82016-12-22 23:57:55 -05001051 int i;
1052 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -04001053
Len Browna99d8732017-05-20 20:11:55 -04001054
1055 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001056 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001057 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001058 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001059 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001060 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001061 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001062 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 -04001063
Len Brownc98d5d92012-06-04 00:56:40 -04001064 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001065 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001066 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001067 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001068 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001069 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001070 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001071 old->pc8 = new->pc8 - old->pc8;
1072 old->pc9 = new->pc9 - old->pc9;
1073 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -05001074 old->pkg_temp_c = new->pkg_temp_c;
1075
Len Brown9185e982016-04-06 17:16:00 -04001076 /* flag an error when rc6 counter resets/wraps */
1077 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1078 old->gfx_rc6_ms = -1;
1079 else
1080 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1081
Len Brown27d47352016-02-27 00:37:54 -05001082 old->gfx_mhz = new->gfx_mhz;
1083
Len Brown889facb2012-11-08 00:48:57 -05001084 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1085 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1086 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1087 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1088 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1089 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -04001090
Len Brown388e9c82016-12-22 23:57:55 -05001091 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1092 if (mp->format == FORMAT_RAW)
1093 old->counter[i] = new->counter[i];
1094 else
1095 old->counter[i] = new->counter[i] - old->counter[i];
1096 }
1097
Len Brownba3dec92016-04-22 20:31:46 -04001098 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001099}
Len Brown103a8fe2010-10-22 23:53:03 -04001100
Len Brownc98d5d92012-06-04 00:56:40 -04001101void
1102delta_core(struct core_data *new, struct core_data *old)
1103{
Len Brown388e9c82016-12-22 23:57:55 -05001104 int i;
1105 struct msr_counter *mp;
1106
Len Brownc98d5d92012-06-04 00:56:40 -04001107 old->c3 = new->c3 - old->c3;
1108 old->c6 = new->c6 - old->c6;
1109 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001110 old->core_temp_c = new->core_temp_c;
Len Brown0539ba112017-02-10 00:27:20 -05001111 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001112
1113 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1114 if (mp->format == FORMAT_RAW)
1115 old->counter[i] = new->counter[i];
1116 else
1117 old->counter[i] = new->counter[i] - old->counter[i];
1118 }
Len Brownc98d5d92012-06-04 00:56:40 -04001119}
Len Brown103a8fe2010-10-22 23:53:03 -04001120
Len Brownc3ae3312012-06-13 21:31:46 -04001121/*
1122 * old = new - old
1123 */
Len Brownba3dec92016-04-22 20:31:46 -04001124int
Len Brownc98d5d92012-06-04 00:56:40 -04001125delta_thread(struct thread_data *new, struct thread_data *old,
1126 struct core_data *core_delta)
1127{
Len Brown388e9c82016-12-22 23:57:55 -05001128 int i;
1129 struct msr_counter *mp;
1130
Len Brownc98d5d92012-06-04 00:56:40 -04001131 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001132
Len Brownc98d5d92012-06-04 00:56:40 -04001133 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001134 if (old->tsc < (1000 * 1000))
1135 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1136 "You can disable all c-states by booting with \"idle=poll\"\n"
1137 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001138
Len Brownc98d5d92012-06-04 00:56:40 -04001139 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001140
Len Brown812db3f2017-02-10 00:25:41 -05001141 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001142 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1143 old->aperf = new->aperf - old->aperf;
1144 old->mperf = new->mperf - old->mperf;
1145 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001146 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001147 }
Len Brownc98d5d92012-06-04 00:56:40 -04001148 }
Len Brown103a8fe2010-10-22 23:53:03 -04001149
Len Brown103a8fe2010-10-22 23:53:03 -04001150
Len Brown144b44b2013-11-09 00:30:16 -05001151 if (use_c1_residency_msr) {
1152 /*
1153 * Some models have a dedicated C1 residency MSR,
1154 * which should be more accurate than the derivation below.
1155 */
1156 } else {
1157 /*
1158 * As counter collection is not atomic,
1159 * it is possible for mperf's non-halted cycles + idle states
1160 * to exceed TSC's all cycles: show c1 = 0% in that case.
1161 */
Len Brown95149362017-04-12 19:44:51 -04001162 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
Len Brown144b44b2013-11-09 00:30:16 -05001163 old->c1 = 0;
1164 else {
1165 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001166 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001167 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001168 }
Len Brownc98d5d92012-06-04 00:56:40 -04001169 }
Len Brownc3ae3312012-06-13 21:31:46 -04001170
Len Brownc98d5d92012-06-04 00:56:40 -04001171 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001172 if (debug > 1)
1173 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001174 old->mperf = 1; /* divide by 0 protection */
1175 }
1176
Len Brown812db3f2017-02-10 00:25:41 -05001177 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001178 old->irq_count = new->irq_count - old->irq_count;
1179
Len Brown812db3f2017-02-10 00:25:41 -05001180 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001181 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001182
Len Brown388e9c82016-12-22 23:57:55 -05001183 for (i = 0, mp = sys.tp; 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 Brownba3dec92016-04-22 20:31:46 -04001189 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001190}
1191
1192int delta_cpu(struct thread_data *t, struct core_data *c,
1193 struct pkg_data *p, struct thread_data *t2,
1194 struct core_data *c2, struct pkg_data *p2)
1195{
Len Brownba3dec92016-04-22 20:31:46 -04001196 int retval = 0;
1197
Len Brownc98d5d92012-06-04 00:56:40 -04001198 /* calculate core delta only for 1st thread in core */
1199 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1200 delta_core(c, c2);
1201
1202 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001203 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1204 if (retval)
1205 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001206
1207 /* calculate package delta only for 1st core in package */
1208 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001209 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001210
Len Brownba3dec92016-04-22 20:31:46 -04001211 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001212}
1213
1214void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1215{
Len Brown388e9c82016-12-22 23:57:55 -05001216 int i;
1217 struct msr_counter *mp;
1218
Len Brownc98d5d92012-06-04 00:56:40 -04001219 t->tsc = 0;
1220 t->aperf = 0;
1221 t->mperf = 0;
1222 t->c1 = 0;
1223
Len Brown562a2d32016-02-26 23:48:05 -05001224 t->irq_count = 0;
1225 t->smi_count = 0;
1226
Len Brownc98d5d92012-06-04 00:56:40 -04001227 /* tells format_counters to dump all fields from this set */
1228 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1229
1230 c->c3 = 0;
1231 c->c6 = 0;
1232 c->c7 = 0;
Len Brown0539ba112017-02-10 00:27:20 -05001233 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001234 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001235
Len Brown0b2bb692015-03-26 00:50:30 -04001236 p->pkg_wtd_core_c0 = 0;
1237 p->pkg_any_core_c0 = 0;
1238 p->pkg_any_gfxe_c0 = 0;
1239 p->pkg_both_core_gfxe_c0 = 0;
1240
Len Brownc98d5d92012-06-04 00:56:40 -04001241 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001242 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001243 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001244 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001245 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001246 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001247 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001248 p->pc8 = 0;
1249 p->pc9 = 0;
1250 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001251
1252 p->energy_pkg = 0;
1253 p->energy_dram = 0;
1254 p->energy_cores = 0;
1255 p->energy_gfx = 0;
1256 p->rapl_pkg_perf_status = 0;
1257 p->rapl_dram_perf_status = 0;
1258 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001259
Len Brownfdf676e2016-02-27 01:28:12 -05001260 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001261 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001262 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1263 t->counter[i] = 0;
1264
1265 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1266 c->counter[i] = 0;
1267
1268 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1269 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001270}
1271int sum_counters(struct thread_data *t, struct core_data *c,
1272 struct pkg_data *p)
1273{
Len Brown388e9c82016-12-22 23:57:55 -05001274 int i;
1275 struct msr_counter *mp;
1276
Len Brownc98d5d92012-06-04 00:56:40 -04001277 average.threads.tsc += t->tsc;
1278 average.threads.aperf += t->aperf;
1279 average.threads.mperf += t->mperf;
1280 average.threads.c1 += t->c1;
1281
Len Brown562a2d32016-02-26 23:48:05 -05001282 average.threads.irq_count += t->irq_count;
1283 average.threads.smi_count += t->smi_count;
1284
Len Brown388e9c82016-12-22 23:57:55 -05001285 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1286 if (mp->format == FORMAT_RAW)
1287 continue;
1288 average.threads.counter[i] += t->counter[i];
1289 }
1290
Len Brownc98d5d92012-06-04 00:56:40 -04001291 /* sum per-core values only for 1st thread in core */
1292 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1293 return 0;
1294
1295 average.cores.c3 += c->c3;
1296 average.cores.c6 += c->c6;
1297 average.cores.c7 += c->c7;
Len Brown0539ba112017-02-10 00:27:20 -05001298 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001299
Len Brown889facb2012-11-08 00:48:57 -05001300 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1301
Len Brown388e9c82016-12-22 23:57:55 -05001302 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1303 if (mp->format == FORMAT_RAW)
1304 continue;
1305 average.cores.counter[i] += c->counter[i];
1306 }
1307
Len Brownc98d5d92012-06-04 00:56:40 -04001308 /* sum per-pkg values only for 1st core in pkg */
1309 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1310 return 0;
1311
Len Browna99d8732017-05-20 20:11:55 -04001312 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001313 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001314 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001315 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
Len Browna99d8732017-05-20 20:11:55 -04001316 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001317 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
Len Browna99d8732017-05-20 20:11:55 -04001318 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001319 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
Len Brown0b2bb692015-03-26 00:50:30 -04001320
Len Brownc98d5d92012-06-04 00:56:40 -04001321 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001322 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001323 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001324 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001325 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001326 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001327 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001328 average.packages.pc8 += p->pc8;
1329 average.packages.pc9 += p->pc9;
1330 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001331
Len Brown889facb2012-11-08 00:48:57 -05001332 average.packages.energy_pkg += p->energy_pkg;
1333 average.packages.energy_dram += p->energy_dram;
1334 average.packages.energy_cores += p->energy_cores;
1335 average.packages.energy_gfx += p->energy_gfx;
1336
Len Brownfdf676e2016-02-27 01:28:12 -05001337 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001338 average.packages.gfx_mhz = p->gfx_mhz;
1339
Len Brown889facb2012-11-08 00:48:57 -05001340 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1341
1342 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1343 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001344
1345 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1346 if (mp->format == FORMAT_RAW)
1347 continue;
1348 average.packages.counter[i] += p->counter[i];
1349 }
Len Brownc98d5d92012-06-04 00:56:40 -04001350 return 0;
1351}
1352/*
1353 * sum the counters for all cpus in the system
1354 * compute the weighted average
1355 */
1356void compute_average(struct thread_data *t, struct core_data *c,
1357 struct pkg_data *p)
1358{
Len Brown388e9c82016-12-22 23:57:55 -05001359 int i;
1360 struct msr_counter *mp;
1361
Len Brownc98d5d92012-06-04 00:56:40 -04001362 clear_counters(&average.threads, &average.cores, &average.packages);
1363
1364 for_all_cpus(sum_counters, t, c, p);
1365
1366 average.threads.tsc /= topo.num_cpus;
1367 average.threads.aperf /= topo.num_cpus;
1368 average.threads.mperf /= topo.num_cpus;
1369 average.threads.c1 /= topo.num_cpus;
1370
Len Brown0de6c0d2017-02-15 21:45:40 -05001371 if (average.threads.irq_count > 9999999)
1372 sums_need_wide_columns = 1;
1373
Len Brownc98d5d92012-06-04 00:56:40 -04001374 average.cores.c3 /= topo.num_cores;
1375 average.cores.c6 /= topo.num_cores;
1376 average.cores.c7 /= topo.num_cores;
Len Brown0539ba112017-02-10 00:27:20 -05001377 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001378
Len Browna99d8732017-05-20 20:11:55 -04001379 if (DO_BIC(BIC_Totl_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001380 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001381 if (DO_BIC(BIC_Any_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001382 average.packages.pkg_any_core_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001383 if (DO_BIC(BIC_GFX_c0))
Len Brown0b2bb692015-03-26 00:50:30 -04001384 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
Len Browna99d8732017-05-20 20:11:55 -04001385 if (DO_BIC(BIC_CPUGFX))
Len Brown0b2bb692015-03-26 00:50:30 -04001386 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
Len Brown0b2bb692015-03-26 00:50:30 -04001387
Len Brownc98d5d92012-06-04 00:56:40 -04001388 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001389 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001390 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001391 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001392 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001393 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001394 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001395
1396 average.packages.pc8 /= topo.num_packages;
1397 average.packages.pc9 /= topo.num_packages;
1398 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001399
1400 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1401 if (mp->format == FORMAT_RAW)
1402 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001403 if (mp->type == COUNTER_ITEMS) {
1404 if (average.threads.counter[i] > 9999999)
1405 sums_need_wide_columns = 1;
Len Brown41618e62017-02-09 18:25:22 -05001406 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001407 }
Len Brown388e9c82016-12-22 23:57:55 -05001408 average.threads.counter[i] /= topo.num_cpus;
1409 }
1410 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1411 if (mp->format == FORMAT_RAW)
1412 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001413 if (mp->type == COUNTER_ITEMS) {
1414 if (average.cores.counter[i] > 9999999)
1415 sums_need_wide_columns = 1;
1416 }
Len Brown388e9c82016-12-22 23:57:55 -05001417 average.cores.counter[i] /= topo.num_cores;
1418 }
1419 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1420 if (mp->format == FORMAT_RAW)
1421 continue;
Len Brown0de6c0d2017-02-15 21:45:40 -05001422 if (mp->type == COUNTER_ITEMS) {
1423 if (average.packages.counter[i] > 9999999)
1424 sums_need_wide_columns = 1;
1425 }
Len Brown388e9c82016-12-22 23:57:55 -05001426 average.packages.counter[i] /= topo.num_packages;
1427 }
Len Brownc98d5d92012-06-04 00:56:40 -04001428}
1429
1430static unsigned long long rdtsc(void)
1431{
1432 unsigned int low, high;
1433
1434 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1435
1436 return low | ((unsigned long long)high) << 32;
1437}
1438
Len Brownc98d5d92012-06-04 00:56:40 -04001439/*
Len Brown495c76542017-02-08 02:41:51 -05001440 * Open a file, and exit on failure
1441 */
1442FILE *fopen_or_die(const char *path, const char *mode)
1443{
1444 FILE *filep = fopen(path, mode);
1445
1446 if (!filep)
1447 err(1, "%s: open failed", path);
1448 return filep;
1449}
1450/*
1451 * snapshot_sysfs_counter()
1452 *
1453 * return snapshot of given counter
1454 */
1455unsigned long long snapshot_sysfs_counter(char *path)
1456{
1457 FILE *fp;
1458 int retval;
1459 unsigned long long counter;
1460
1461 fp = fopen_or_die(path, "r");
1462
1463 retval = fscanf(fp, "%lld", &counter);
1464 if (retval != 1)
1465 err(1, "snapshot_sysfs_counter(%s)", path);
1466
1467 fclose(fp);
1468
1469 return counter;
1470}
1471
1472int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1473{
1474 if (mp->msr_num != 0) {
1475 if (get_msr(cpu, mp->msr_num, counterp))
1476 return -1;
1477 } else {
Len Brown41618e62017-02-09 18:25:22 -05001478 char path[128];
1479
1480 if (mp->flags & SYSFS_PERCPU) {
1481 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1482 cpu, mp->path);
1483
1484 *counterp = snapshot_sysfs_counter(path);
1485 } else {
1486 *counterp = snapshot_sysfs_counter(mp->path);
1487 }
Len Brown495c76542017-02-08 02:41:51 -05001488 }
1489
1490 return 0;
1491}
1492
1493/*
Len Brownc98d5d92012-06-04 00:56:40 -04001494 * get_counters(...)
1495 * migrate to cpu
1496 * acquire and record local counters for that cpu
1497 */
1498int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1499{
1500 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001501 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001502 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001503 struct msr_counter *mp;
1504 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001505
Len Browne52966c2012-11-08 22:38:05 -05001506 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001507 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001508 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001509 }
Len Brownc98d5d92012-06-04 00:56:40 -04001510
Len Brown0102b062016-02-27 03:11:29 -05001511retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001512 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1513
Len Brown812db3f2017-02-10 00:25:41 -05001514 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001515 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1516
1517 /*
1518 * The TSC, APERF and MPERF must be read together for
1519 * APERF/MPERF and MPERF/TSC to give accurate results.
1520 *
1521 * Unfortunately, APERF and MPERF are read by
1522 * individual system call, so delays may occur
1523 * between them. If the time to read them
1524 * varies by a large amount, we re-read them.
1525 */
1526
1527 /*
1528 * This initial dummy APERF read has been seen to
1529 * reduce jitter in the subsequent reads.
1530 */
1531
Len Brown9c63a652012-10-31 01:29:52 -04001532 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001533 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001534
1535 t->tsc = rdtsc(); /* re-read close to APERF */
1536
1537 tsc_before = t->tsc;
1538
1539 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1540 return -3;
1541
1542 tsc_between = rdtsc();
1543
Len Brown9c63a652012-10-31 01:29:52 -04001544 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001545 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001546
1547 tsc_after = rdtsc();
1548
1549 aperf_time = tsc_between - tsc_before;
1550 mperf_time = tsc_after - tsc_between;
1551
1552 /*
1553 * If the system call latency to read APERF and MPERF
1554 * differ by more than 2x, then try again.
1555 */
1556 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1557 aperf_mperf_retry_count++;
1558 if (aperf_mperf_retry_count < 5)
1559 goto retry;
1560 else
1561 warnx("cpu%d jitter %lld %lld",
1562 cpu, aperf_time, mperf_time);
1563 }
1564 aperf_mperf_retry_count = 0;
1565
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001566 t->aperf = t->aperf * aperf_mperf_multiplier;
1567 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001568 }
1569
Len Brown812db3f2017-02-10 00:25:41 -05001570 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001571 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001572 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001573 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1574 return -5;
1575 t->smi_count = msr & 0xFFFFFFFF;
1576 }
Len Brown0539ba112017-02-10 00:27:20 -05001577 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001578 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1579 return -6;
1580 }
1581
Len Brown388e9c82016-12-22 23:57:55 -05001582 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001583 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001584 return -10;
1585 }
1586
Len Brownc98d5d92012-06-04 00:56:40 -04001587 /* collect core counters only for 1st thread in core */
1588 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1589 return 0;
1590
Len Brown812db3f2017-02-10 00:25:41 -05001591 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001592 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1593 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001594 }
1595
Len Brown812db3f2017-02-10 00:25:41 -05001596 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001597 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1598 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001599 } else if (do_knl_cstates) {
1600 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1601 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001602 }
1603
Len Brown812db3f2017-02-10 00:25:41 -05001604 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001605 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1606 return -8;
1607
Len Brown0539ba112017-02-10 00:27:20 -05001608 if (DO_BIC(BIC_Mod_c6))
1609 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1610 return -8;
1611
Len Brown812db3f2017-02-10 00:25:41 -05001612 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001613 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1614 return -9;
1615 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1616 }
1617
Len Brown388e9c82016-12-22 23:57:55 -05001618 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001619 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001620 return -10;
1621 }
Len Brown889facb2012-11-08 00:48:57 -05001622
Len Brownc98d5d92012-06-04 00:56:40 -04001623 /* collect package counters only for 1st core in package */
1624 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1625 return 0;
1626
Len Browna99d8732017-05-20 20:11:55 -04001627 if (DO_BIC(BIC_Totl_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001628 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1629 return -10;
Len Browna99d8732017-05-20 20:11:55 -04001630 }
1631 if (DO_BIC(BIC_Any_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001632 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1633 return -11;
Len Browna99d8732017-05-20 20:11:55 -04001634 }
1635 if (DO_BIC(BIC_GFX_c0)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001636 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1637 return -12;
Len Browna99d8732017-05-20 20:11:55 -04001638 }
1639 if (DO_BIC(BIC_CPUGFX)) {
Len Brown0b2bb692015-03-26 00:50:30 -04001640 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1641 return -13;
1642 }
Len Brown0f47c082017-01-27 00:50:45 -05001643 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001644 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1645 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001646 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba112017-02-10 00:27:20 -05001647 if (do_slm_cstates) {
1648 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1649 return -10;
1650 } else {
1651 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1652 return -10;
1653 }
1654 }
1655
Len Brown0f47c082017-01-27 00:50:45 -05001656 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001657 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1658 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001659 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001660 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1661 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001662 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001663 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1664 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001665 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001666 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1667 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001668 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001669 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1670 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001671
Len Brown889facb2012-11-08 00:48:57 -05001672 if (do_rapl & RAPL_PKG) {
1673 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1674 return -13;
1675 p->energy_pkg = msr & 0xFFFFFFFF;
1676 }
Jacob Pan91484942016-06-16 09:48:20 -07001677 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001678 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1679 return -14;
1680 p->energy_cores = msr & 0xFFFFFFFF;
1681 }
1682 if (do_rapl & RAPL_DRAM) {
1683 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1684 return -15;
1685 p->energy_dram = msr & 0xFFFFFFFF;
1686 }
1687 if (do_rapl & RAPL_GFX) {
1688 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1689 return -16;
1690 p->energy_gfx = msr & 0xFFFFFFFF;
1691 }
1692 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1693 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1694 return -16;
1695 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1696 }
1697 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1698 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1699 return -16;
1700 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1701 }
Len Brown812db3f2017-02-10 00:25:41 -05001702 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001703 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1704 return -17;
1705 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1706 }
Len Brownfdf676e2016-02-27 01:28:12 -05001707
Len Brown812db3f2017-02-10 00:25:41 -05001708 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001709 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1710
Len Brown812db3f2017-02-10 00:25:41 -05001711 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001712 p->gfx_mhz = gfx_cur_mhz;
1713
Len Brown388e9c82016-12-22 23:57:55 -05001714 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001715 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001716 return -10;
1717 }
1718
Len Brown103a8fe2010-10-22 23:53:03 -04001719 return 0;
1720}
1721
Len Brownee7e38e2015-02-09 23:39:45 -05001722/*
1723 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1724 * If you change the values, note they are used both in comparisons
1725 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1726 */
1727
1728#define PCLUKN 0 /* Unknown */
1729#define PCLRSV 1 /* Reserved */
1730#define PCL__0 2 /* PC0 */
1731#define PCL__1 3 /* PC1 */
1732#define PCL__2 4 /* PC2 */
1733#define PCL__3 5 /* PC3 */
1734#define PCL__4 6 /* PC4 */
1735#define PCL__6 7 /* PC6 */
1736#define PCL_6N 8 /* PC6 No Retention */
1737#define PCL_6R 9 /* PC6 Retention */
1738#define PCL__7 10 /* PC7 */
1739#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001740#define PCL__8 12 /* PC8 */
1741#define PCL__9 13 /* PC9 */
1742#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001743
1744int pkg_cstate_limit = PCLUKN;
1745char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001746 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001747
Len Browne9257f52015-04-01 21:02:57 -04001748int 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};
1749int 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};
1750int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brown0539ba112017-02-10 00:27:20 -05001751int 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 -05001752int 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 -04001753int 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 -04001754int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brownd8ebb442016-12-01 20:27:46 -05001755int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brownee7e38e2015-02-09 23:39:45 -05001756
Len Browna2b7b742015-09-26 00:12:38 -04001757
1758static void
1759calculate_tsc_tweak()
1760{
Len Browna2b7b742015-09-26 00:12:38 -04001761 tsc_tweak = base_hz / tsc_hz;
1762}
1763
Len Brownfcd17212015-03-23 20:29:09 -04001764static void
1765dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001766{
1767 unsigned long long msr;
1768 unsigned int ratio;
1769
Len Brownec0adc52015-11-12 02:42:31 -05001770 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001771
Len Brownb7d8c142016-02-13 23:36:17 -05001772 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001773
Len Brown103a8fe2010-10-22 23:53:03 -04001774 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001775 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001776 ratio, bclk, ratio * bclk);
1777
1778 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001779 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001780 ratio, bclk, ratio * bclk);
1781
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001782 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001783 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001784 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001785
Len Brownfcd17212015-03-23 20:29:09 -04001786 return;
1787}
1788
1789static void
1790dump_hsw_turbo_ratio_limits(void)
1791{
1792 unsigned long long msr;
1793 unsigned int ratio;
1794
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001795 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001796
Len Brownb7d8c142016-02-13 23:36:17 -05001797 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001798
1799 ratio = (msr >> 8) & 0xFF;
1800 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001801 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001802 ratio, bclk, ratio * bclk);
1803
1804 ratio = (msr >> 0) & 0xFF;
1805 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001806 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001807 ratio, bclk, ratio * bclk);
1808 return;
1809}
1810
1811static void
1812dump_ivt_turbo_ratio_limits(void)
1813{
1814 unsigned long long msr;
1815 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001816
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001817 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001818
Len Brownb7d8c142016-02-13 23:36:17 -05001819 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001820
1821 ratio = (msr >> 56) & 0xFF;
1822 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001823 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001824 ratio, bclk, ratio * bclk);
1825
1826 ratio = (msr >> 48) & 0xFF;
1827 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001828 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001829 ratio, bclk, ratio * bclk);
1830
1831 ratio = (msr >> 40) & 0xFF;
1832 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001833 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001834 ratio, bclk, ratio * bclk);
1835
1836 ratio = (msr >> 32) & 0xFF;
1837 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001838 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001839 ratio, bclk, ratio * bclk);
1840
1841 ratio = (msr >> 24) & 0xFF;
1842 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001843 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001844 ratio, bclk, ratio * bclk);
1845
1846 ratio = (msr >> 16) & 0xFF;
1847 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001848 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001849 ratio, bclk, ratio * bclk);
1850
1851 ratio = (msr >> 8) & 0xFF;
1852 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001853 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001854 ratio, bclk, ratio * bclk);
1855
1856 ratio = (msr >> 0) & 0xFF;
1857 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001858 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001859 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001860 return;
1861}
Len Brown31e07522017-01-31 23:07:49 -05001862int has_turbo_ratio_group_limits(int family, int model)
1863{
1864
1865 if (!genuine_intel)
1866 return 0;
1867
1868 switch (model) {
1869 case INTEL_FAM6_ATOM_GOLDMONT:
1870 case INTEL_FAM6_SKYLAKE_X:
1871 case INTEL_FAM6_ATOM_DENVERTON:
1872 return 1;
1873 }
1874 return 0;
1875}
Len Brown6574a5d2012-09-21 00:01:31 -04001876
Len Brownfcd17212015-03-23 20:29:09 -04001877static void
Len Brown31e07522017-01-31 23:07:49 -05001878dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001879{
Len Brown31e07522017-01-31 23:07:49 -05001880 unsigned long long msr, core_counts;
1881 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001882
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001883 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001884 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001885
Len Brown31e07522017-01-31 23:07:49 -05001886 if (has_turbo_ratio_group_limits(family, model)) {
1887 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1888 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1889 } else {
1890 core_counts = 0x0807060504030201;
1891 }
1892
Len Brown6574a5d2012-09-21 00:01:31 -04001893 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001894 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001895 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001896 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1897 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001898
1899 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001900 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001901 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001902 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1903 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001904
1905 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001906 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001907 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001908 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1909 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001910
1911 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001912 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001913 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001914 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1915 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001916
Len Brown103a8fe2010-10-22 23:53:03 -04001917 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001918 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001919 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001920 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1921 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001922
1923 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001924 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001925 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001926 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1927 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001928
1929 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001930 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001931 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001932 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1933 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001934
1935 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001936 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001937 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001938 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1939 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04001940 return;
1941}
Len Brown3a9a9412014-08-15 02:39:52 -04001942
Len Brownfcd17212015-03-23 20:29:09 -04001943static void
Len Brown0f7887c2017-01-12 23:49:18 -05001944dump_atom_turbo_ratio_limits(void)
1945{
1946 unsigned long long msr;
1947 unsigned int ratio;
1948
1949 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1950 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1951
1952 ratio = (msr >> 0) & 0x3F;
1953 if (ratio)
1954 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1955 ratio, bclk, ratio * bclk);
1956
1957 ratio = (msr >> 8) & 0x3F;
1958 if (ratio)
1959 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1960 ratio, bclk, ratio * bclk);
1961
1962 ratio = (msr >> 16) & 0x3F;
1963 if (ratio)
1964 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1965 ratio, bclk, ratio * bclk);
1966
1967 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1968 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1969
1970 ratio = (msr >> 24) & 0x3F;
1971 if (ratio)
1972 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1973 ratio, bclk, ratio * bclk);
1974
1975 ratio = (msr >> 16) & 0x3F;
1976 if (ratio)
1977 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1978 ratio, bclk, ratio * bclk);
1979
1980 ratio = (msr >> 8) & 0x3F;
1981 if (ratio)
1982 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1983 ratio, bclk, ratio * bclk);
1984
1985 ratio = (msr >> 0) & 0x3F;
1986 if (ratio)
1987 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1988 ratio, bclk, ratio * bclk);
1989}
1990
1991static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001992dump_knl_turbo_ratio_limits(void)
1993{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001994 const unsigned int buckets_no = 7;
1995
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001996 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001997 int delta_cores, delta_ratio;
1998 int i, b_nr;
1999 unsigned int cores[buckets_no];
2000 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002001
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07002002 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002003
Len Brownb7d8c142016-02-13 23:36:17 -05002004 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04002005 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002006
2007 /**
2008 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002009 * [0] -- Reserved
2010 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002011 * [15:8] -- Base value of freq ratio of bucket 1.
2012 * [20:16] -- +ve delta of number of active cores of bucket 2.
2013 * i.e. active cores of bucket 2 =
2014 * active cores of bucket 1 + delta
2015 * [23:21] -- Negative delta of freq ratio of bucket 2.
2016 * i.e. freq ratio of bucket 2 =
2017 * freq ratio of bucket 1 - delta
2018 * [28:24]-- +ve delta of number of active cores of bucket 3.
2019 * [31:29]-- -ve delta of freq ratio of bucket 3.
2020 * [36:32]-- +ve delta of number of active cores of bucket 4.
2021 * [39:37]-- -ve delta of freq ratio of bucket 4.
2022 * [44:40]-- +ve delta of number of active cores of bucket 5.
2023 * [47:45]-- -ve delta of freq ratio of bucket 5.
2024 * [52:48]-- +ve delta of number of active cores of bucket 6.
2025 * [55:53]-- -ve delta of freq ratio of bucket 6.
2026 * [60:56]-- +ve delta of number of active cores of bucket 7.
2027 * [63:61]-- -ve delta of freq ratio of bucket 7.
2028 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002029
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002030 b_nr = 0;
2031 cores[b_nr] = (msr & 0xFF) >> 1;
2032 ratio[b_nr] = (msr >> 8) & 0xFF;
2033
2034 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002035 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002036 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002037
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002038 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2039 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2040 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002041 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002042
2043 for (i = buckets_no - 1; i >= 0; i--)
2044 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05002045 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05002046 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01002047 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002048}
2049
2050static void
Len Brownfcd17212015-03-23 20:29:09 -04002051dump_nhm_cst_cfg(void)
2052{
2053 unsigned long long msr;
2054
Len Brown1df2e552017-01-07 23:24:57 -05002055 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04002056
2057#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
2058#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
2059
Len Brown1df2e552017-01-07 23:24:57 -05002060 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04002061
Len Brownb7d8c142016-02-13 23:36:17 -05002062 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04002063 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2064 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2065 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2066 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2067 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04002068 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04002069 pkg_cstate_limit_strings[pkg_cstate_limit]);
2070 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002071}
2072
Len Brown6fb31432015-06-17 16:23:45 -04002073static void
2074dump_config_tdp(void)
2075{
2076 unsigned long long msr;
2077
2078 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002079 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002080 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04002081
2082 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002083 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002084 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002085 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2086 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2087 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2088 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002089 }
Len Brownb7d8c142016-02-13 23:36:17 -05002090 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002091
2092 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002093 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002094 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08002095 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2096 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2097 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2098 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04002099 }
Len Brownb7d8c142016-02-13 23:36:17 -05002100 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002101
2102 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002103 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04002104 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05002105 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2106 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2107 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05002108
Len Brown6fb31432015-06-17 16:23:45 -04002109 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002110 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08002111 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05002112 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2113 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04002114}
Len Brown5a634262016-04-06 17:15:55 -04002115
2116unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2117
2118void print_irtl(void)
2119{
2120 unsigned long long msr;
2121
2122 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2123 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2124 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2125 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2126
2127 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2128 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2129 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2130 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2131
2132 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2133 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2134 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2135 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2136
2137 if (!do_irtl_hsw)
2138 return;
2139
2140 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2141 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2142 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2143 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2144
2145 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2146 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2147 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2148 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2149
2150 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2151 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2152 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2153 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2154
2155}
Len Brown36229892016-02-26 20:51:02 -05002156void free_fd_percpu(void)
2157{
2158 int i;
2159
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002160 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002161 if (fd_percpu[i] != 0)
2162 close(fd_percpu[i]);
2163 }
2164
2165 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002166}
2167
Len Brownc98d5d92012-06-04 00:56:40 -04002168void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002169{
Len Brownc98d5d92012-06-04 00:56:40 -04002170 CPU_FREE(cpu_present_set);
2171 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002172 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002173
Len Brownc98d5d92012-06-04 00:56:40 -04002174 CPU_FREE(cpu_affinity_set);
2175 cpu_affinity_set = NULL;
2176 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002177
Len Brownc98d5d92012-06-04 00:56:40 -04002178 free(thread_even);
2179 free(core_even);
2180 free(package_even);
2181
2182 thread_even = NULL;
2183 core_even = NULL;
2184 package_even = NULL;
2185
2186 free(thread_odd);
2187 free(core_odd);
2188 free(package_odd);
2189
2190 thread_odd = NULL;
2191 core_odd = NULL;
2192 package_odd = NULL;
2193
2194 free(output_buffer);
2195 output_buffer = NULL;
2196 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002197
2198 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002199
2200 free(irq_column_2_cpu);
2201 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002202}
2203
Josh Triplett57a42a32013-08-20 17:20:17 -07002204
2205/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002206 * Parse a file containing a single int.
2207 */
2208int parse_int_file(const char *fmt, ...)
2209{
2210 va_list args;
2211 char path[PATH_MAX];
2212 FILE *filep;
2213 int value;
2214
2215 va_start(args, fmt);
2216 vsnprintf(path, sizeof(path), fmt, args);
2217 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002218 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002219 if (fscanf(filep, "%d", &value) != 1)
2220 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002221 fclose(filep);
2222 return value;
2223}
2224
2225/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002226 * get_cpu_position_in_core(cpu)
2227 * return the position of the CPU among its HT siblings in the core
2228 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002229 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002230int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002231{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002232 char path[64];
2233 FILE *filep;
2234 int this_cpu;
2235 char character;
2236 int i;
2237
2238 sprintf(path,
2239 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2240 cpu);
2241 filep = fopen(path, "r");
2242 if (filep == NULL) {
2243 perror(path);
2244 exit(1);
2245 }
2246
2247 for (i = 0; i < topo.num_threads_per_core; i++) {
2248 fscanf(filep, "%d", &this_cpu);
2249 if (this_cpu == cpu) {
2250 fclose(filep);
2251 return i;
2252 }
2253
2254 /* Account for no separator after last thread*/
2255 if (i != (topo.num_threads_per_core - 1))
2256 fscanf(filep, "%c", &character);
2257 }
2258
2259 fclose(filep);
2260 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002261}
2262
Len Brownc98d5d92012-06-04 00:56:40 -04002263/*
2264 * cpu_is_first_core_in_package(cpu)
2265 * return 1 if given CPU is 1st core in package
2266 */
2267int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002268{
Josh Triplett95aebc42013-08-20 17:20:16 -07002269 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002270}
2271
2272int get_physical_package_id(int cpu)
2273{
Josh Triplett95aebc42013-08-20 17:20:16 -07002274 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002275}
2276
2277int get_core_id(int cpu)
2278{
Josh Triplett95aebc42013-08-20 17:20:16 -07002279 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002280}
2281
Len Brownc98d5d92012-06-04 00:56:40 -04002282int get_num_ht_siblings(int cpu)
2283{
2284 char path[80];
2285 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002286 int sib1;
2287 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002288 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002289 char str[100];
2290 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002291
2292 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002293 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002294
Len Brownc98d5d92012-06-04 00:56:40 -04002295 /*
2296 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002297 * A ',' separated or '-' separated set of numbers
2298 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002299 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002300 fscanf(filep, "%d%c\n", &sib1, &character);
2301 fseek(filep, 0, SEEK_SET);
2302 fgets(str, 100, filep);
2303 ch = strchr(str, character);
2304 while (ch != NULL) {
2305 matches++;
2306 ch = strchr(ch+1, character);
2307 }
Len Brownc98d5d92012-06-04 00:56:40 -04002308
2309 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002310 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002311}
2312
Len Brown103a8fe2010-10-22 23:53:03 -04002313/*
Len Brownc98d5d92012-06-04 00:56:40 -04002314 * run func(thread, core, package) in topology order
2315 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002316 */
2317
Len Brownc98d5d92012-06-04 00:56:40 -04002318int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2319 struct pkg_data *, struct thread_data *, struct core_data *,
2320 struct pkg_data *), struct thread_data *thread_base,
2321 struct core_data *core_base, struct pkg_data *pkg_base,
2322 struct thread_data *thread_base2, struct core_data *core_base2,
2323 struct pkg_data *pkg_base2)
2324{
2325 int retval, pkg_no, core_no, thread_no;
2326
2327 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2328 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2329 for (thread_no = 0; thread_no <
2330 topo.num_threads_per_core; ++thread_no) {
2331 struct thread_data *t, *t2;
2332 struct core_data *c, *c2;
2333 struct pkg_data *p, *p2;
2334
2335 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2336
2337 if (cpu_is_not_present(t->cpu_id))
2338 continue;
2339
2340 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2341
2342 c = GET_CORE(core_base, core_no, pkg_no);
2343 c2 = GET_CORE(core_base2, core_no, pkg_no);
2344
2345 p = GET_PKG(pkg_base, pkg_no);
2346 p2 = GET_PKG(pkg_base2, pkg_no);
2347
2348 retval = func(t, c, p, t2, c2, p2);
2349 if (retval)
2350 return retval;
2351 }
2352 }
2353 }
2354 return 0;
2355}
2356
2357/*
2358 * run func(cpu) on every cpu in /proc/stat
2359 * return max_cpu number
2360 */
2361int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002362{
2363 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002364 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002365 int retval;
2366
Josh Triplett57a42a32013-08-20 17:20:17 -07002367 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002368
2369 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002370 if (retval != 0)
2371 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002372
Len Brownc98d5d92012-06-04 00:56:40 -04002373 while (1) {
2374 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 -04002375 if (retval != 1)
2376 break;
2377
Len Brownc98d5d92012-06-04 00:56:40 -04002378 retval = func(cpu_num);
2379 if (retval) {
2380 fclose(fp);
2381 return(retval);
2382 }
Len Brown103a8fe2010-10-22 23:53:03 -04002383 }
2384 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002385 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002386}
2387
2388void re_initialize(void)
2389{
Len Brownc98d5d92012-06-04 00:56:40 -04002390 free_all_buffers();
2391 setup_all_buffers();
2392 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002393}
2394
Len Brownc98d5d92012-06-04 00:56:40 -04002395
Len Brown103a8fe2010-10-22 23:53:03 -04002396/*
Len Brownc98d5d92012-06-04 00:56:40 -04002397 * count_cpus()
2398 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002399 */
Len Brownc98d5d92012-06-04 00:56:40 -04002400int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002401{
Len Brownc98d5d92012-06-04 00:56:40 -04002402 if (topo.max_cpu_num < cpu)
2403 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002404
Len Brownc98d5d92012-06-04 00:56:40 -04002405 topo.num_cpus += 1;
2406 return 0;
2407}
2408int mark_cpu_present(int cpu)
2409{
2410 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002411 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002412}
2413
Len Brown562a2d32016-02-26 23:48:05 -05002414/*
2415 * snapshot_proc_interrupts()
2416 *
2417 * read and record summary of /proc/interrupts
2418 *
2419 * return 1 if config change requires a restart, else return 0
2420 */
2421int snapshot_proc_interrupts(void)
2422{
2423 static FILE *fp;
2424 int column, retval;
2425
2426 if (fp == NULL)
2427 fp = fopen_or_die("/proc/interrupts", "r");
2428 else
2429 rewind(fp);
2430
2431 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2432 for (column = 0; column < topo.num_cpus; ++column) {
2433 int cpu_number;
2434
2435 retval = fscanf(fp, " CPU%d", &cpu_number);
2436 if (retval != 1)
2437 break;
2438
2439 if (cpu_number > topo.max_cpu_num) {
2440 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2441 return 1;
2442 }
2443
2444 irq_column_2_cpu[column] = cpu_number;
2445 irqs_per_cpu[cpu_number] = 0;
2446 }
2447
2448 /* read /proc/interrupt count lines and sum up irqs per cpu */
2449 while (1) {
2450 int column;
2451 char buf[64];
2452
2453 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2454 if (retval != 1)
2455 break;
2456
2457 /* read the count per cpu */
2458 for (column = 0; column < topo.num_cpus; ++column) {
2459
2460 int cpu_number, irq_count;
2461
2462 retval = fscanf(fp, " %d", &irq_count);
2463 if (retval != 1)
2464 break;
2465
2466 cpu_number = irq_column_2_cpu[column];
2467 irqs_per_cpu[cpu_number] += irq_count;
2468
2469 }
2470
2471 while (getc(fp) != '\n')
2472 ; /* flush interrupt description */
2473
2474 }
2475 return 0;
2476}
Len Brown27d47352016-02-27 00:37:54 -05002477/*
Len Brownfdf676e2016-02-27 01:28:12 -05002478 * snapshot_gfx_rc6_ms()
2479 *
2480 * record snapshot of
2481 * /sys/class/drm/card0/power/rc6_residency_ms
2482 *
2483 * return 1 if config change requires a restart, else return 0
2484 */
2485int snapshot_gfx_rc6_ms(void)
2486{
2487 FILE *fp;
2488 int retval;
2489
2490 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2491
2492 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2493 if (retval != 1)
2494 err(1, "GFX rc6");
2495
2496 fclose(fp);
2497
2498 return 0;
2499}
2500/*
Len Brown27d47352016-02-27 00:37:54 -05002501 * snapshot_gfx_mhz()
2502 *
2503 * record snapshot of
2504 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2505 *
2506 * return 1 if config change requires a restart, else return 0
2507 */
2508int snapshot_gfx_mhz(void)
2509{
2510 static FILE *fp;
2511 int retval;
2512
2513 if (fp == NULL)
2514 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
Len Brown22048c52017-03-04 15:42:48 -05002515 else {
Len Brown27d47352016-02-27 00:37:54 -05002516 rewind(fp);
Len Brown22048c52017-03-04 15:42:48 -05002517 fflush(fp);
2518 }
Len Brown27d47352016-02-27 00:37:54 -05002519
2520 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2521 if (retval != 1)
2522 err(1, "GFX MHz");
2523
2524 return 0;
2525}
Len Brown562a2d32016-02-26 23:48:05 -05002526
2527/*
2528 * snapshot /proc and /sys files
2529 *
2530 * return 1 if configuration restart needed, else return 0
2531 */
2532int snapshot_proc_sysfs_files(void)
2533{
Len Brown218f0e82017-02-14 22:07:52 -05002534 if (DO_BIC(BIC_IRQ))
2535 if (snapshot_proc_interrupts())
2536 return 1;
Len Brown562a2d32016-02-26 23:48:05 -05002537
Len Brown812db3f2017-02-10 00:25:41 -05002538 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002539 snapshot_gfx_rc6_ms();
2540
Len Brown812db3f2017-02-10 00:25:41 -05002541 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002542 snapshot_gfx_mhz();
2543
Len Brown562a2d32016-02-26 23:48:05 -05002544 return 0;
2545}
2546
Len Brown103a8fe2010-10-22 23:53:03 -04002547void turbostat_loop()
2548{
Len Brownc98d5d92012-06-04 00:56:40 -04002549 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002550 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002551
Len Brown103a8fe2010-10-22 23:53:03 -04002552restart:
Len Browne52966c2012-11-08 22:38:05 -05002553 restarted++;
2554
Len Brown562a2d32016-02-26 23:48:05 -05002555 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002556 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002557 if (retval < -1) {
2558 exit(retval);
2559 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002560 if (restarted > 1) {
2561 exit(retval);
2562 }
Len Brownc98d5d92012-06-04 00:56:40 -04002563 re_initialize();
2564 goto restart;
2565 }
Len Browne52966c2012-11-08 22:38:05 -05002566 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002567 gettimeofday(&tv_even, (struct timezone *)NULL);
2568
2569 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002570 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002571 re_initialize();
2572 goto restart;
2573 }
Len Brown2a0609c2016-02-12 22:44:48 -05002574 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002575 if (snapshot_proc_sysfs_files())
2576 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002577 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002578 if (retval < -1) {
2579 exit(retval);
2580 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002581 re_initialize();
2582 goto restart;
2583 }
Len Brown103a8fe2010-10-22 23:53:03 -04002584 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002585 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002586 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2587 re_initialize();
2588 goto restart;
2589 }
Len Brownc98d5d92012-06-04 00:56:40 -04002590 compute_average(EVEN_COUNTERS);
2591 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002592 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002593 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002594 if (snapshot_proc_sysfs_files())
2595 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002596 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002597 if (retval < -1) {
2598 exit(retval);
2599 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002600 re_initialize();
2601 goto restart;
2602 }
Len Brown103a8fe2010-10-22 23:53:03 -04002603 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002604 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002605 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2606 re_initialize();
2607 goto restart;
2608 }
Len Brownc98d5d92012-06-04 00:56:40 -04002609 compute_average(ODD_COUNTERS);
2610 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002611 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002612 }
2613}
2614
2615void check_dev_msr()
2616{
2617 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002618 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002619
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002620 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2621 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002622 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2623 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002624}
2625
Len Brown98481e72014-08-15 00:36:50 -04002626void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002627{
Len Brown98481e72014-08-15 00:36:50 -04002628 struct __user_cap_header_struct cap_header_data;
2629 cap_user_header_t cap_header = &cap_header_data;
2630 struct __user_cap_data_struct cap_data_data;
2631 cap_user_data_t cap_data = &cap_data_data;
2632 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2633 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002634 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002635
2636 /* check for CAP_SYS_RAWIO */
2637 cap_header->pid = getpid();
2638 cap_header->version = _LINUX_CAPABILITY_VERSION;
2639 if (capget(cap_header, cap_data) < 0)
2640 err(-6, "capget(2) failed");
2641
2642 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2643 do_exit++;
2644 warnx("capget(CAP_SYS_RAWIO) failed,"
2645 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2646 }
2647
2648 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002649 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2650 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002651 do_exit++;
2652 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2653 }
2654
2655 /* if all else fails, thell them to be root */
2656 if (do_exit)
2657 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002658 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002659
2660 if (do_exit)
2661 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002662}
2663
Len Brownd7899442015-01-23 00:12:33 -05002664/*
2665 * NHM adds support for additional MSRs:
2666 *
2667 * MSR_SMI_COUNT 0x00000034
2668 *
Len Brownec0adc52015-11-12 02:42:31 -05002669 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002670 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002671 *
Len Browncf4cbe52017-01-01 13:08:33 -05002672 * MSR_MISC_PWR_MGMT 0x000001aa
2673 *
Len Brownd7899442015-01-23 00:12:33 -05002674 * MSR_PKG_C3_RESIDENCY 0x000003f8
2675 * MSR_PKG_C6_RESIDENCY 0x000003f9
2676 * MSR_CORE_C3_RESIDENCY 0x000003fc
2677 * MSR_CORE_C6_RESIDENCY 0x000003fd
2678 *
Len Brownee7e38e2015-02-09 23:39:45 -05002679 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002680 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002681 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002682 */
Len Brownee7e38e2015-02-09 23:39:45 -05002683int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002684{
Len Brownee7e38e2015-02-09 23:39:45 -05002685 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002686 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002687 int *pkg_cstate_limits;
2688
Len Brown103a8fe2010-10-22 23:53:03 -04002689 if (!genuine_intel)
2690 return 0;
2691
2692 if (family != 6)
2693 return 0;
2694
Len Brown21ed5572015-10-19 22:37:40 -04002695 bclk = discover_bclk(family, model);
2696
Len Brown103a8fe2010-10-22 23:53:03 -04002697 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002698 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2699 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002700 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce69e2016-06-16 23:22:37 -04002701 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2702 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2703 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2704 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002705 pkg_cstate_limits = nhm_pkg_cstate_limits;
2706 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002707 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2708 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2709 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2710 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002711 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002712 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002713 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002714 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2715 case INTEL_FAM6_HASWELL_X: /* HSX */
2716 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2717 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2718 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2719 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2720 case INTEL_FAM6_BROADWELL_X: /* BDX */
2721 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2722 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2723 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2724 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2725 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002726 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002727 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002728 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002729 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2730 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002731 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002732 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002733 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002734 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04002735 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002736 pkg_cstate_limits = slv_pkg_cstate_limits;
2737 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002738 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002739 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002740 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002741 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002742 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002743 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002744 pkg_cstate_limits = phi_pkg_cstate_limits;
2745 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002746 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002747 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce69e2016-06-16 23:22:37 -04002748 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002749 pkg_cstate_limits = bxt_pkg_cstate_limits;
2750 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002751 default:
2752 return 0;
2753 }
Len Brown1df2e552017-01-07 23:24:57 -05002754 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002755 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002756
Len Brownec0adc52015-11-12 02:42:31 -05002757 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002758 base_ratio = (msr >> 8) & 0xFF;
2759
2760 base_hz = base_ratio * bclk * 1000000;
2761 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002762 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002763}
Len Brown0f7887c2017-01-12 23:49:18 -05002764/*
Len Brown495c76542017-02-08 02:41:51 -05002765 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002766 *
2767 * MSR_CC6_DEMOTION_POLICY_CONFIG
2768 * MSR_MC6_DEMOTION_POLICY_CONFIG
2769 */
2770
2771int has_slv_msrs(unsigned int family, unsigned int model)
2772{
2773 if (!genuine_intel)
2774 return 0;
2775
2776 switch (model) {
2777 case INTEL_FAM6_ATOM_SILVERMONT1:
2778 case INTEL_FAM6_ATOM_MERRIFIELD:
2779 case INTEL_FAM6_ATOM_MOOREFIELD:
2780 return 1;
2781 }
2782 return 0;
2783}
Len Brown7170a372017-01-27 02:13:27 -05002784int is_dnv(unsigned int family, unsigned int model)
2785{
2786
2787 if (!genuine_intel)
2788 return 0;
2789
2790 switch (model) {
2791 case INTEL_FAM6_ATOM_DENVERTON:
2792 return 1;
2793 }
2794 return 0;
2795}
Len Brownade0eba2017-02-10 01:56:47 -05002796int is_bdx(unsigned int family, unsigned int model)
2797{
2798
2799 if (!genuine_intel)
2800 return 0;
2801
2802 switch (model) {
2803 case INTEL_FAM6_BROADWELL_X:
2804 case INTEL_FAM6_BROADWELL_XEON_D:
2805 return 1;
2806 }
2807 return 0;
2808}
Len Brown34c761972017-01-27 02:36:41 -05002809int is_skx(unsigned int family, unsigned int model)
2810{
2811
2812 if (!genuine_intel)
2813 return 0;
2814
2815 switch (model) {
2816 case INTEL_FAM6_SKYLAKE_X:
2817 return 1;
2818 }
2819 return 0;
2820}
Len Brown0f7887c2017-01-12 23:49:18 -05002821
Len Brown31e07522017-01-31 23:07:49 -05002822int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002823{
Len Brown0f7887c2017-01-12 23:49:18 -05002824 if (has_slv_msrs(family, model))
2825 return 0;
2826
Len Brownd7899442015-01-23 00:12:33 -05002827 switch (model) {
2828 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce69e2016-06-16 23:22:37 -04002829 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2830 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2831 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002832 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002833 return 0;
2834 default:
2835 return 1;
2836 }
2837}
Len Brown0f7887c2017-01-12 23:49:18 -05002838int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2839{
2840 if (has_slv_msrs(family, model))
2841 return 1;
2842
2843 return 0;
2844}
Len Brown6574a5d2012-09-21 00:01:31 -04002845int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2846{
2847 if (!genuine_intel)
2848 return 0;
2849
2850 if (family != 6)
2851 return 0;
2852
2853 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002854 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2855 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002856 return 1;
2857 default:
2858 return 0;
2859 }
2860}
Len Brownfcd17212015-03-23 20:29:09 -04002861int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2862{
2863 if (!genuine_intel)
2864 return 0;
2865
2866 if (family != 6)
2867 return 0;
2868
2869 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002870 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002871 return 1;
2872 default:
2873 return 0;
2874 }
2875}
2876
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002877int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2878{
2879 if (!genuine_intel)
2880 return 0;
2881
2882 if (family != 6)
2883 return 0;
2884
2885 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002886 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002887 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002888 return 1;
2889 default:
2890 return 0;
2891 }
2892}
Len Brown31e07522017-01-31 23:07:49 -05002893int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
2894{
2895 if (!genuine_intel)
2896 return 0;
2897
2898 if (family != 6)
2899 return 0;
2900
2901 switch (model) {
2902 case INTEL_FAM6_ATOM_GOLDMONT:
2903 case INTEL_FAM6_SKYLAKE_X:
2904 return 1;
2905 default:
2906 return 0;
2907 }
2908}
Len Brown6fb31432015-06-17 16:23:45 -04002909int has_config_tdp(unsigned int family, unsigned int model)
2910{
2911 if (!genuine_intel)
2912 return 0;
2913
2914 if (family != 6)
2915 return 0;
2916
2917 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002918 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2919 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2920 case INTEL_FAM6_HASWELL_X: /* HSX */
2921 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2922 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2923 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2924 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2925 case INTEL_FAM6_BROADWELL_X: /* BDX */
2926 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2927 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2928 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2929 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2930 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2931 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002932
Len Brown869ce69e2016-06-16 23:22:37 -04002933 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002934 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002935 return 1;
2936 default:
2937 return 0;
2938 }
2939}
2940
Len Brownfcd17212015-03-23 20:29:09 -04002941static void
Colin Ian King1b693172016-03-02 13:50:25 +00002942dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002943{
2944 if (!do_nhm_platform_info)
2945 return;
2946
2947 dump_nhm_platform_info();
2948
2949 if (has_hsw_turbo_ratio_limit(family, model))
2950 dump_hsw_turbo_ratio_limits();
2951
2952 if (has_ivt_turbo_ratio_limit(family, model))
2953 dump_ivt_turbo_ratio_limits();
2954
Len Brown31e07522017-01-31 23:07:49 -05002955 if (has_turbo_ratio_limit(family, model))
2956 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04002957
Len Brown0f7887c2017-01-12 23:49:18 -05002958 if (has_atom_turbo_ratio_limit(family, model))
2959 dump_atom_turbo_ratio_limits();
2960
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002961 if (has_knl_turbo_ratio_limit(family, model))
2962 dump_knl_turbo_ratio_limits();
2963
Len Brown6fb31432015-06-17 16:23:45 -04002964 if (has_config_tdp(family, model))
2965 dump_config_tdp();
2966
Len Brownfcd17212015-03-23 20:29:09 -04002967 dump_nhm_cst_cfg();
2968}
2969
Len Brown41618e62017-02-09 18:25:22 -05002970static void
2971dump_sysfs_cstate_config(void)
2972{
2973 char path[64];
2974 char name_buf[16];
2975 char desc[64];
2976 FILE *input;
2977 int state;
2978 char *sp;
2979
2980 if (!DO_BIC(BIC_sysfs))
2981 return;
2982
2983 for (state = 0; state < 10; ++state) {
2984
2985 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
2986 base_cpu, state);
2987 input = fopen(path, "r");
2988 if (input == NULL)
2989 continue;
2990 fgets(name_buf, sizeof(name_buf), input);
2991
2992 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
2993 sp = strchr(name_buf, '-');
2994 if (!sp)
2995 sp = strchrnul(name_buf, '\n');
2996 *sp = '\0';
2997
2998 fclose(input);
2999
3000 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3001 base_cpu, state);
3002 input = fopen(path, "r");
3003 if (input == NULL)
3004 continue;
3005 fgets(desc, sizeof(desc), input);
3006
3007 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3008 fclose(input);
3009 }
3010}
Len Brown7293fcc2017-02-22 00:11:12 -05003011static void
3012dump_sysfs_pstate_config(void)
3013{
3014 char path[64];
3015 char driver_buf[64];
3016 char governor_buf[64];
3017 FILE *input;
3018 int turbo;
3019
3020 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3021 base_cpu);
3022 input = fopen(path, "r");
3023 if (input == NULL) {
3024 fprintf(stderr, "NSFOD %s\n", path);
3025 return;
3026 }
3027 fgets(driver_buf, sizeof(driver_buf), input);
3028 fclose(input);
3029
3030 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3031 base_cpu);
3032 input = fopen(path, "r");
3033 if (input == NULL) {
3034 fprintf(stderr, "NSFOD %s\n", path);
3035 return;
3036 }
3037 fgets(governor_buf, sizeof(governor_buf), input);
3038 fclose(input);
3039
3040 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3041 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3042
3043 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3044 input = fopen(path, "r");
3045 if (input != NULL) {
3046 fscanf(input, "%d", &turbo);
3047 fprintf(outf, "cpufreq boost: %d\n", turbo);
3048 fclose(input);
3049 }
3050
3051 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3052 input = fopen(path, "r");
3053 if (input != NULL) {
3054 fscanf(input, "%d", &turbo);
3055 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3056 fclose(input);
3057 }
3058}
Len Brown41618e62017-02-09 18:25:22 -05003059
Len Brown6574a5d2012-09-21 00:01:31 -04003060
Len Brown889facb2012-11-08 00:48:57 -05003061/*
3062 * print_epb()
3063 * Decode the ENERGY_PERF_BIAS MSR
3064 */
3065int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3066{
3067 unsigned long long msr;
3068 char *epb_string;
3069 int cpu;
3070
3071 if (!has_epb)
3072 return 0;
3073
3074 cpu = t->cpu_id;
3075
3076 /* EPB is per-package */
3077 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3078 return 0;
3079
3080 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003081 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003082 return -1;
3083 }
3084
3085 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3086 return 0;
3087
Len Browne9be7dd2015-05-26 12:19:37 -04003088 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05003089 case ENERGY_PERF_BIAS_PERFORMANCE:
3090 epb_string = "performance";
3091 break;
3092 case ENERGY_PERF_BIAS_NORMAL:
3093 epb_string = "balanced";
3094 break;
3095 case ENERGY_PERF_BIAS_POWERSAVE:
3096 epb_string = "powersave";
3097 break;
3098 default:
3099 epb_string = "custom";
3100 break;
3101 }
Len Brownb7d8c142016-02-13 23:36:17 -05003102 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05003103
3104 return 0;
3105}
Len Brown7f5c2582015-12-01 01:36:39 -05003106/*
3107 * print_hwp()
3108 * Decode the MSR_HWP_CAPABILITIES
3109 */
3110int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3111{
3112 unsigned long long msr;
3113 int cpu;
3114
3115 if (!has_hwp)
3116 return 0;
3117
3118 cpu = t->cpu_id;
3119
3120 /* MSR_HWP_CAPABILITIES is per-package */
3121 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3122 return 0;
3123
3124 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003125 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05003126 return -1;
3127 }
3128
3129 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3130 return 0;
3131
Len Brownb7d8c142016-02-13 23:36:17 -05003132 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003133 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3134
3135 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3136 if ((msr & (1 << 0)) == 0)
3137 return 0;
3138
3139 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3140 return 0;
3141
Len Brownb7d8c142016-02-13 23:36:17 -05003142 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003143 "(high %d guar %d eff %d low %d)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003144 cpu, msr,
3145 (unsigned int)HWP_HIGHEST_PERF(msr),
3146 (unsigned int)HWP_GUARANTEED_PERF(msr),
3147 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3148 (unsigned int)HWP_LOWEST_PERF(msr));
3149
3150 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3151 return 0;
3152
Len Brownb7d8c142016-02-13 23:36:17 -05003153 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003154 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003155 cpu, msr,
3156 (unsigned int)(((msr) >> 0) & 0xff),
3157 (unsigned int)(((msr) >> 8) & 0xff),
3158 (unsigned int)(((msr) >> 16) & 0xff),
3159 (unsigned int)(((msr) >> 24) & 0xff),
3160 (unsigned int)(((msr) >> 32) & 0xff3),
3161 (unsigned int)(((msr) >> 42) & 0x1));
3162
3163 if (has_hwp_pkg) {
3164 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3165 return 0;
3166
Len Brownb7d8c142016-02-13 23:36:17 -05003167 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown6dbd25a2017-03-04 18:18:28 -05003168 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05003169 cpu, msr,
3170 (unsigned int)(((msr) >> 0) & 0xff),
3171 (unsigned int)(((msr) >> 8) & 0xff),
3172 (unsigned int)(((msr) >> 16) & 0xff),
3173 (unsigned int)(((msr) >> 24) & 0xff),
3174 (unsigned int)(((msr) >> 32) & 0xff3));
3175 }
3176 if (has_hwp_notify) {
3177 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3178 return 0;
3179
Len Brownb7d8c142016-02-13 23:36:17 -05003180 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003181 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3182 cpu, msr,
3183 ((msr) & 0x1) ? "EN" : "Dis",
3184 ((msr) & 0x2) ? "EN" : "Dis");
3185 }
3186 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3187 return 0;
3188
Len Brownb7d8c142016-02-13 23:36:17 -05003189 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003190 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3191 cpu, msr,
3192 ((msr) & 0x1) ? "" : "No-",
3193 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003194
3195 return 0;
3196}
3197
Len Brown3a9a9412014-08-15 02:39:52 -04003198/*
3199 * print_perf_limit()
3200 */
3201int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3202{
3203 unsigned long long msr;
3204 int cpu;
3205
3206 cpu = t->cpu_id;
3207
3208 /* per-package */
3209 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3210 return 0;
3211
3212 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003213 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003214 return -1;
3215 }
3216
3217 if (do_core_perf_limit_reasons) {
3218 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003219 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3220 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003221 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003222 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003223 (msr & 1 << 13) ? "Transitions, " : "",
3224 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3225 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3226 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3227 (msr & 1 << 9) ? "CorePwr, " : "",
3228 (msr & 1 << 8) ? "Amps, " : "",
3229 (msr & 1 << 6) ? "VR-Therm, " : "",
3230 (msr & 1 << 5) ? "Auto-HWP, " : "",
3231 (msr & 1 << 4) ? "Graphics, " : "",
3232 (msr & 1 << 2) ? "bit2, " : "",
3233 (msr & 1 << 1) ? "ThermStatus, " : "",
3234 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003235 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 -04003236 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003237 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003238 (msr & 1 << 29) ? "Transitions, " : "",
3239 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3240 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3241 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3242 (msr & 1 << 25) ? "CorePwr, " : "",
3243 (msr & 1 << 24) ? "Amps, " : "",
3244 (msr & 1 << 22) ? "VR-Therm, " : "",
3245 (msr & 1 << 21) ? "Auto-HWP, " : "",
3246 (msr & 1 << 20) ? "Graphics, " : "",
3247 (msr & 1 << 18) ? "bit18, " : "",
3248 (msr & 1 << 17) ? "ThermStatus, " : "",
3249 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003250
3251 }
3252 if (do_gfx_perf_limit_reasons) {
3253 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003254 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3255 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003256 (msr & 1 << 0) ? "PROCHOT, " : "",
3257 (msr & 1 << 1) ? "ThermStatus, " : "",
3258 (msr & 1 << 4) ? "Graphics, " : "",
3259 (msr & 1 << 6) ? "VR-Therm, " : "",
3260 (msr & 1 << 8) ? "Amps, " : "",
3261 (msr & 1 << 9) ? "GFXPwr, " : "",
3262 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3263 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003264 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003265 (msr & 1 << 16) ? "PROCHOT, " : "",
3266 (msr & 1 << 17) ? "ThermStatus, " : "",
3267 (msr & 1 << 20) ? "Graphics, " : "",
3268 (msr & 1 << 22) ? "VR-Therm, " : "",
3269 (msr & 1 << 24) ? "Amps, " : "",
3270 (msr & 1 << 25) ? "GFXPwr, " : "",
3271 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3272 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3273 }
3274 if (do_ring_perf_limit_reasons) {
3275 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003276 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3277 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003278 (msr & 1 << 0) ? "PROCHOT, " : "",
3279 (msr & 1 << 1) ? "ThermStatus, " : "",
3280 (msr & 1 << 6) ? "VR-Therm, " : "",
3281 (msr & 1 << 8) ? "Amps, " : "",
3282 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3283 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003284 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003285 (msr & 1 << 16) ? "PROCHOT, " : "",
3286 (msr & 1 << 17) ? "ThermStatus, " : "",
3287 (msr & 1 << 22) ? "VR-Therm, " : "",
3288 (msr & 1 << 24) ? "Amps, " : "",
3289 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3290 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3291 }
3292 return 0;
3293}
3294
Len Brown889facb2012-11-08 00:48:57 -05003295#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3296#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3297
Colin Ian King1b693172016-03-02 13:50:25 +00003298double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003299{
3300 unsigned long long msr;
3301
3302 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003303 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003304 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3305
3306 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003307 case INTEL_FAM6_ATOM_SILVERMONT1:
3308 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003309 return 30.0;
3310 default:
3311 return 135.0;
3312 }
3313}
3314
Andrey Semin40ee8e32014-12-05 00:07:00 -05003315/*
3316 * rapl_dram_energy_units_probe()
3317 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3318 */
3319static double
3320rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3321{
3322 /* only called for genuine_intel, family 6 */
3323
3324 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003325 case INTEL_FAM6_HASWELL_X: /* HSX */
3326 case INTEL_FAM6_BROADWELL_X: /* BDX */
3327 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3328 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003329 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003330 return (rapl_dram_energy_units = 15.3 / 1000000);
3331 default:
3332 return (rapl_energy_units);
3333 }
3334}
3335
Len Brown144b44b2013-11-09 00:30:16 -05003336
Len Brown889facb2012-11-08 00:48:57 -05003337/*
3338 * rapl_probe()
3339 *
Len Brown144b44b2013-11-09 00:30:16 -05003340 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003341 */
3342void rapl_probe(unsigned int family, unsigned int model)
3343{
3344 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003345 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003346 double tdp;
3347
3348 if (!genuine_intel)
3349 return;
3350
3351 if (family != 6)
3352 return;
3353
3354 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003355 case INTEL_FAM6_SANDYBRIDGE:
3356 case INTEL_FAM6_IVYBRIDGE:
3357 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3358 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3359 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3360 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3361 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003362 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003363 if (rapl_joules) {
3364 BIC_PRESENT(BIC_Pkg_J);
3365 BIC_PRESENT(BIC_Cor_J);
3366 BIC_PRESENT(BIC_GFX_J);
3367 } else {
3368 BIC_PRESENT(BIC_PkgWatt);
3369 BIC_PRESENT(BIC_CorWatt);
3370 BIC_PRESENT(BIC_GFXWatt);
3371 }
Len Brown889facb2012-11-08 00:48:57 -05003372 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003373 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003374 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003375 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003376 if (rapl_joules)
3377 BIC_PRESENT(BIC_Pkg_J);
3378 else
3379 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003380 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003381 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3382 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3383 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3384 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown81824922017-03-04 17:23:07 -05003385 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 -05003386 BIC_PRESENT(BIC_PKG__);
3387 BIC_PRESENT(BIC_RAM__);
3388 if (rapl_joules) {
3389 BIC_PRESENT(BIC_Pkg_J);
3390 BIC_PRESENT(BIC_Cor_J);
3391 BIC_PRESENT(BIC_RAM_J);
Len Brown81824922017-03-04 17:23:07 -05003392 BIC_PRESENT(BIC_GFX_J);
Len Brown812db3f2017-02-10 00:25:41 -05003393 } else {
3394 BIC_PRESENT(BIC_PkgWatt);
3395 BIC_PRESENT(BIC_CorWatt);
3396 BIC_PRESENT(BIC_RAMWatt);
Len Brown81824922017-03-04 17:23:07 -05003397 BIC_PRESENT(BIC_GFXWatt);
Len Brown812db3f2017-02-10 00:25:41 -05003398 }
Len Brown0b2bb692015-03-26 00:50:30 -04003399 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003400 case INTEL_FAM6_HASWELL_X: /* HSX */
3401 case INTEL_FAM6_BROADWELL_X: /* BDX */
3402 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3403 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3404 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003405 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003406 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 -05003407 BIC_PRESENT(BIC_PKG__);
3408 BIC_PRESENT(BIC_RAM__);
3409 if (rapl_joules) {
3410 BIC_PRESENT(BIC_Pkg_J);
3411 BIC_PRESENT(BIC_RAM_J);
3412 } else {
3413 BIC_PRESENT(BIC_PkgWatt);
3414 BIC_PRESENT(BIC_RAMWatt);
3415 }
Len Browne6f9bb32013-12-03 02:19:19 -05003416 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003417 case INTEL_FAM6_SANDYBRIDGE_X:
3418 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003419 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 -05003420 BIC_PRESENT(BIC_PKG__);
3421 BIC_PRESENT(BIC_RAM__);
3422 if (rapl_joules) {
3423 BIC_PRESENT(BIC_Pkg_J);
3424 BIC_PRESENT(BIC_Cor_J);
3425 BIC_PRESENT(BIC_RAM_J);
3426 } else {
3427 BIC_PRESENT(BIC_PkgWatt);
3428 BIC_PRESENT(BIC_CorWatt);
3429 BIC_PRESENT(BIC_RAMWatt);
3430 }
Len Brown144b44b2013-11-09 00:30:16 -05003431 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003432 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3433 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003434 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003435 if (rapl_joules) {
3436 BIC_PRESENT(BIC_Pkg_J);
3437 BIC_PRESENT(BIC_Cor_J);
3438 } else {
3439 BIC_PRESENT(BIC_PkgWatt);
3440 BIC_PRESENT(BIC_CorWatt);
3441 }
Len Brown889facb2012-11-08 00:48:57 -05003442 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003443 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003444 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 -05003445 BIC_PRESENT(BIC_PKG__);
3446 BIC_PRESENT(BIC_RAM__);
3447 if (rapl_joules) {
3448 BIC_PRESENT(BIC_Pkg_J);
3449 BIC_PRESENT(BIC_Cor_J);
3450 BIC_PRESENT(BIC_RAM_J);
3451 } else {
3452 BIC_PRESENT(BIC_PkgWatt);
3453 BIC_PRESENT(BIC_CorWatt);
3454 BIC_PRESENT(BIC_RAMWatt);
3455 }
Jacob Pan0f644902016-06-16 09:48:22 -07003456 break;
Len Brown889facb2012-11-08 00:48:57 -05003457 default:
3458 return;
3459 }
3460
3461 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003462 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003463 return;
3464
3465 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce69e2016-06-16 23:22:37 -04003466 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003467 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3468 else
3469 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003470
Andrey Semin40ee8e32014-12-05 00:07:00 -05003471 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3472
Len Brown144b44b2013-11-09 00:30:16 -05003473 time_unit = msr >> 16 & 0xF;
3474 if (time_unit == 0)
3475 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003476
Len Brown144b44b2013-11-09 00:30:16 -05003477 rapl_time_units = 1.0 / (1 << (time_unit));
3478
3479 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003480
3481 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003482 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003483 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003484
3485 return;
3486}
3487
Colin Ian King1b693172016-03-02 13:50:25 +00003488void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003489{
3490 if (!genuine_intel)
3491 return;
3492
3493 if (family != 6)
3494 return;
3495
3496 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003497 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3498 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3499 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003500 do_gfx_perf_limit_reasons = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04003501 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003502 do_core_perf_limit_reasons = 1;
3503 do_ring_perf_limit_reasons = 1;
3504 default:
3505 return;
3506 }
3507}
3508
Len Brown889facb2012-11-08 00:48:57 -05003509int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3510{
3511 unsigned long long msr;
Len Brownf4896fa52017-03-04 18:10:45 -05003512 unsigned int dts, dts2;
Len Brown889facb2012-11-08 00:48:57 -05003513 int cpu;
3514
3515 if (!(do_dts || do_ptm))
3516 return 0;
3517
3518 cpu = t->cpu_id;
3519
3520 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003521 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003522 return 0;
3523
3524 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003525 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003526 return -1;
3527 }
3528
3529 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3530 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3531 return 0;
3532
3533 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003534 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003535 cpu, msr, tcc_activation_temp - dts);
3536
Len Brown889facb2012-11-08 00:48:57 -05003537 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3538 return 0;
3539
3540 dts = (msr >> 16) & 0x7F;
3541 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003542 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003543 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003544 }
3545
3546
Len Brownf4896fa52017-03-04 18:10:45 -05003547 if (do_dts && debug) {
Len Brown889facb2012-11-08 00:48:57 -05003548 unsigned int resolution;
3549
3550 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3551 return 0;
3552
3553 dts = (msr >> 16) & 0x7F;
3554 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003555 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003556 cpu, msr, tcc_activation_temp - dts, resolution);
3557
Len Brown889facb2012-11-08 00:48:57 -05003558 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3559 return 0;
3560
3561 dts = (msr >> 16) & 0x7F;
3562 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003563 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003564 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
Len Brown889facb2012-11-08 00:48:57 -05003565 }
3566
3567 return 0;
3568}
Len Brown36229892016-02-26 20:51:02 -05003569
Len Brown889facb2012-11-08 00:48:57 -05003570void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3571{
Len Brownb7d8c142016-02-13 23:36:17 -05003572 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003573 cpu, label,
3574 ((msr >> 15) & 1) ? "EN" : "DIS",
3575 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3576 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3577 (((msr >> 16) & 1) ? "EN" : "DIS"));
3578
3579 return;
3580}
3581
3582int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3583{
3584 unsigned long long msr;
3585 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003586
3587 if (!do_rapl)
3588 return 0;
3589
3590 /* RAPL counters are per package, so print only for 1st thread/package */
3591 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3592 return 0;
3593
3594 cpu = t->cpu_id;
3595 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003596 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003597 return -1;
3598 }
3599
3600 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3601 return -1;
3602
Len Brown96e47152017-01-21 02:26:00 -05003603 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3604 rapl_power_units, rapl_energy_units, rapl_time_units);
3605
Len Brown144b44b2013-11-09 00:30:16 -05003606 if (do_rapl & RAPL_PKG_POWER_INFO) {
3607
Len Brown889facb2012-11-08 00:48:57 -05003608 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3609 return -5;
3610
3611
Len Brownb7d8c142016-02-13 23:36:17 -05003612 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 -05003613 cpu, msr,
3614 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3615 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3616 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3617 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3618
Len Brown144b44b2013-11-09 00:30:16 -05003619 }
3620 if (do_rapl & RAPL_PKG) {
3621
Len Brown889facb2012-11-08 00:48:57 -05003622 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3623 return -9;
3624
Len Brownb7d8c142016-02-13 23:36:17 -05003625 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003626 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003627
3628 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003629 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003630 cpu,
3631 ((msr >> 47) & 1) ? "EN" : "DIS",
3632 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3633 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3634 ((msr >> 48) & 1) ? "EN" : "DIS");
3635 }
3636
Len Brown0b2bb692015-03-26 00:50:30 -04003637 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003638 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3639 return -6;
3640
Len Brownb7d8c142016-02-13 23:36:17 -05003641 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 -05003642 cpu, msr,
3643 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3644 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3645 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3646 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003647 }
3648 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003649 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3650 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003651 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003652 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003653
3654 print_power_limit_msr(cpu, msr, "DRAM Limit");
3655 }
Len Brown144b44b2013-11-09 00:30:16 -05003656 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003657 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3658 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003659
Len Brown96e47152017-01-21 02:26:00 -05003660 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003661 }
Jacob Pan91484942016-06-16 09:48:20 -07003662 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003663 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3664 return -9;
3665 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3666 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3667 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003668 }
3669 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003670 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3671 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003672
Len Brown96e47152017-01-21 02:26:00 -05003673 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003674
Len Brown96e47152017-01-21 02:26:00 -05003675 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3676 return -9;
3677 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3678 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3679 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003680 }
3681 return 0;
3682}
3683
Len Brownd7899442015-01-23 00:12:33 -05003684/*
3685 * SNB adds support for additional MSRs:
3686 *
3687 * MSR_PKG_C7_RESIDENCY 0x000003fa
3688 * MSR_CORE_C7_RESIDENCY 0x000003fe
3689 * MSR_PKG_C2_RESIDENCY 0x0000060d
3690 */
Len Brown103a8fe2010-10-22 23:53:03 -04003691
Len Brownd7899442015-01-23 00:12:33 -05003692int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003693{
3694 if (!genuine_intel)
3695 return 0;
3696
3697 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003698 case INTEL_FAM6_SANDYBRIDGE:
3699 case INTEL_FAM6_SANDYBRIDGE_X:
3700 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3701 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3702 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3703 case INTEL_FAM6_HASWELL_X: /* HSW */
3704 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3705 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3706 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3707 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3708 case INTEL_FAM6_BROADWELL_X: /* BDX */
3709 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3710 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3711 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3712 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3713 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3714 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3715 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003716 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003717 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003718 return 1;
3719 }
3720 return 0;
3721}
3722
Len Brownd7899442015-01-23 00:12:33 -05003723/*
3724 * HSW adds support for additional MSRs:
3725 *
Len Brown5a634262016-04-06 17:15:55 -04003726 * MSR_PKG_C8_RESIDENCY 0x00000630
3727 * MSR_PKG_C9_RESIDENCY 0x00000631
3728 * MSR_PKG_C10_RESIDENCY 0x00000632
3729 *
3730 * MSR_PKGC8_IRTL 0x00000633
3731 * MSR_PKGC9_IRTL 0x00000634
3732 * MSR_PKGC10_IRTL 0x00000635
3733 *
Len Brownd7899442015-01-23 00:12:33 -05003734 */
3735int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003736{
3737 if (!genuine_intel)
3738 return 0;
3739
3740 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003741 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3742 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3743 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3744 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3745 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3746 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3747 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003748 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003749 return 1;
3750 }
3751 return 0;
3752}
3753
Len Brown0b2bb692015-03-26 00:50:30 -04003754/*
3755 * SKL adds support for additional MSRS:
3756 *
3757 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3758 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3759 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3760 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3761 */
3762int has_skl_msrs(unsigned int family, unsigned int model)
3763{
3764 if (!genuine_intel)
3765 return 0;
3766
3767 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003768 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3769 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3770 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3771 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003772 return 1;
3773 }
3774 return 0;
3775}
3776
Len Brown144b44b2013-11-09 00:30:16 -05003777int is_slm(unsigned int family, unsigned int model)
3778{
3779 if (!genuine_intel)
3780 return 0;
3781 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003782 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3783 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003784 return 1;
3785 }
3786 return 0;
3787}
3788
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003789int is_knl(unsigned int family, unsigned int model)
3790{
3791 if (!genuine_intel)
3792 return 0;
3793 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003794 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003795 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003796 return 1;
3797 }
3798 return 0;
3799}
3800
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003801unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3802{
3803 if (is_knl(family, model))
3804 return 1024;
3805 return 1;
3806}
3807
Len Brown144b44b2013-11-09 00:30:16 -05003808#define SLM_BCLK_FREQS 5
3809double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3810
3811double slm_bclk(void)
3812{
3813 unsigned long long msr = 3;
3814 unsigned int i;
3815 double freq;
3816
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003817 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003818 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003819
3820 i = msr & 0xf;
3821 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003822 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003823 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003824 }
3825 freq = slm_freq_table[i];
3826
Len Brown96e47152017-01-21 02:26:00 -05003827 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003828 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003829
3830 return freq;
3831}
3832
Len Brown103a8fe2010-10-22 23:53:03 -04003833double discover_bclk(unsigned int family, unsigned int model)
3834{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003835 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003836 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003837 else if (is_slm(family, model))
3838 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003839 else
3840 return 133.33;
3841}
3842
Len Brown889facb2012-11-08 00:48:57 -05003843/*
3844 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3845 * the Thermal Control Circuit (TCC) activates.
3846 * This is usually equal to tjMax.
3847 *
3848 * Older processors do not have this MSR, so there we guess,
3849 * but also allow cmdline over-ride with -T.
3850 *
3851 * Several MSR temperature values are in units of degrees-C
3852 * below this value, including the Digital Thermal Sensor (DTS),
3853 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3854 */
3855int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3856{
3857 unsigned long long msr;
3858 unsigned int target_c_local;
3859 int cpu;
3860
3861 /* tcc_activation_temp is used only for dts or ptm */
3862 if (!(do_dts || do_ptm))
3863 return 0;
3864
3865 /* this is a per-package concept */
3866 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3867 return 0;
3868
3869 cpu = t->cpu_id;
3870 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003871 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003872 return -1;
3873 }
3874
3875 if (tcc_activation_temp_override != 0) {
3876 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003877 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003878 cpu, tcc_activation_temp);
3879 return 0;
3880 }
3881
3882 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003883 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003884 goto guess;
3885
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003886 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003887 goto guess;
3888
Jean Delvare34821242014-05-01 11:40:19 +02003889 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003890
Len Brown96e47152017-01-21 02:26:00 -05003891 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003892 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003893 cpu, msr, target_c_local);
3894
Jean Delvare34821242014-05-01 11:40:19 +02003895 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003896 goto guess;
3897
3898 tcc_activation_temp = target_c_local;
3899
3900 return 0;
3901
3902guess:
3903 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003904 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003905 cpu, tcc_activation_temp);
3906
3907 return 0;
3908}
Len Brown69807a62015-11-21 12:22:47 -05003909
Len Brownaa8d8cc2016-03-11 13:26:03 -05003910void decode_feature_control_msr(void)
3911{
3912 unsigned long long msr;
3913
3914 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3915 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3916 base_cpu, msr,
3917 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3918 msr & (1 << 18) ? "SGX" : "");
3919}
3920
Len Brown69807a62015-11-21 12:22:47 -05003921void decode_misc_enable_msr(void)
3922{
3923 unsigned long long msr;
3924
3925 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003926 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003927 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003928 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3929 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3930 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3931 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3932 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003933}
3934
Len Brown33148d62017-01-21 01:26:16 -05003935void decode_misc_feature_control(void)
3936{
3937 unsigned long long msr;
3938
3939 if (!has_misc_feature_control)
3940 return;
3941
3942 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
3943 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
3944 base_cpu, msr,
3945 msr & (0 << 0) ? "No-" : "",
3946 msr & (1 << 0) ? "No-" : "",
3947 msr & (2 << 0) ? "No-" : "",
3948 msr & (3 << 0) ? "No-" : "");
3949}
Len Brownf0057312015-12-03 01:35:36 -05003950/*
3951 * Decode MSR_MISC_PWR_MGMT
3952 *
3953 * Decode the bits according to the Nehalem documentation
3954 * bit[0] seems to continue to have same meaning going forward
3955 * bit[1] less so...
3956 */
3957void decode_misc_pwr_mgmt_msr(void)
3958{
3959 unsigned long long msr;
3960
3961 if (!do_nhm_platform_info)
3962 return;
3963
Len Browncf4cbe52017-01-01 13:08:33 -05003964 if (no_MSR_MISC_PWR_MGMT)
3965 return;
3966
Len Brownf0057312015-12-03 01:35:36 -05003967 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003968 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 -05003969 base_cpu, msr,
3970 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003971 msr & (1 << 1) ? "EN" : "DIS",
3972 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003973}
Len Brown71616c82017-01-07 22:37:48 -05003974/*
3975 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3976 *
3977 * This MSRs are present on Silvermont processors,
3978 * Intel Atom processor E3000 series (Baytrail), and friends.
3979 */
3980void decode_c6_demotion_policy_msr(void)
3981{
3982 unsigned long long msr;
3983
3984 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3985 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3986 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3987
3988 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3989 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3990 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3991}
Len Brown7f5c2582015-12-01 01:36:39 -05003992
Len Brownfcd17212015-03-23 20:29:09 -04003993void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003994{
Len Brown61a87ba2015-11-23 02:30:51 -05003995 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003996 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003997 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003998
3999 eax = ebx = ecx = edx = 0;
4000
Len Brown5aea2f72016-03-13 03:14:35 -04004001 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004002
4003 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4004 genuine_intel = 1;
4005
Len Brown96e47152017-01-21 02:26:00 -05004006 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004007 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04004008 (char *)&ebx, (char *)&edx, (char *)&ecx);
4009
Len Brown5aea2f72016-03-13 03:14:35 -04004010 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004011 family = (fms >> 8) & 0xf;
4012 model = (fms >> 4) & 0xf;
4013 stepping = fms & 0xf;
4014 if (family == 6 || family == 0xf)
4015 model += ((fms >> 16) & 0xf) << 4;
4016
Len Brown96e47152017-01-21 02:26:00 -05004017 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05004018 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04004019 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05004020 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05004021 ecx & (1 << 0) ? "SSE3" : "-",
4022 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05004023 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05004024 ecx & (1 << 7) ? "EIST" : "-",
4025 ecx & (1 << 8) ? "TM2" : "-",
4026 edx & (1 << 4) ? "TSC" : "-",
4027 edx & (1 << 5) ? "MSR" : "-",
4028 edx & (1 << 22) ? "ACPI-TM" : "-",
4029 edx & (1 << 29) ? "TM" : "-");
4030 }
Len Brown103a8fe2010-10-22 23:53:03 -04004031
Josh Triplettb2c95d92013-08-20 17:20:18 -07004032 if (!(edx & (1 << 5)))
4033 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04004034
4035 /*
4036 * check max extended function levels of CPUID.
4037 * This is needed to check for invariant TSC.
4038 * This check is valid for both Intel and AMD.
4039 */
4040 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004041 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04004042
Len Brown61a87ba2015-11-23 02:30:51 -05004043 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04004044
Len Brownd7899442015-01-23 00:12:33 -05004045 /*
4046 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4047 * this check is valid for both Intel and AMD
4048 */
Len Brown5aea2f72016-03-13 03:14:35 -04004049 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05004050 has_invariant_tsc = edx & (1 << 8);
4051 }
Len Brown103a8fe2010-10-22 23:53:03 -04004052
4053 /*
4054 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4055 * this check is valid for both Intel and AMD
4056 */
4057
Len Brown5aea2f72016-03-13 03:14:35 -04004058 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01004059 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004060 if (has_aperf) {
4061 BIC_PRESENT(BIC_Avg_MHz);
4062 BIC_PRESENT(BIC_Busy);
4063 BIC_PRESENT(BIC_Bzy_MHz);
4064 }
Len Brown889facb2012-11-08 00:48:57 -05004065 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05004066 if (do_dts)
4067 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05004068 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05004069 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05004070 if (do_ptm)
4071 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05004072 has_hwp = eax & (1 << 7);
4073 has_hwp_notify = eax & (1 << 8);
4074 has_hwp_activity_window = eax & (1 << 9);
4075 has_hwp_epp = eax & (1 << 10);
4076 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05004077 has_epb = ecx & (1 << 3);
4078
Len Brown96e47152017-01-21 02:26:00 -05004079 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05004080 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05004081 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4082 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05004083 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05004084 do_dts ? "" : "No-",
4085 do_ptm ? "" : "No-",
4086 has_hwp ? "" : "No-",
4087 has_hwp_notify ? "" : "No-",
4088 has_hwp_activity_window ? "" : "No-",
4089 has_hwp_epp ? "" : "No-",
4090 has_hwp_pkg ? "" : "No-",
4091 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04004092
Len Brown96e47152017-01-21 02:26:00 -05004093 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05004094 decode_misc_enable_msr();
4095
Len Brown33148d62017-01-21 01:26:16 -05004096
Len Brown96e47152017-01-21 02:26:00 -05004097 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05004098 int has_sgx;
4099
4100 ecx = 0;
4101
4102 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4103
4104 has_sgx = ebx & (1 << 2);
4105 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4106
4107 if (has_sgx)
4108 decode_feature_control_msr();
4109 }
4110
Len Brown61a87ba2015-11-23 02:30:51 -05004111 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04004112 unsigned int eax_crystal;
4113 unsigned int ebx_tsc;
4114
4115 /*
4116 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4117 */
4118 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04004119 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04004120
4121 if (ebx_tsc != 0) {
4122
Len Brown96e47152017-01-21 02:26:00 -05004123 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05004124 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004125 eax_crystal, ebx_tsc, crystal_hz);
4126
4127 if (crystal_hz == 0)
4128 switch(model) {
Len Brown869ce69e2016-06-16 23:22:37 -04004129 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4130 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4131 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4132 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04004133 crystal_hz = 24000000; /* 24.0 MHz */
4134 break;
Len Brown869ce69e2016-06-16 23:22:37 -04004135 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05004136 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04004137 crystal_hz = 25000000; /* 25.0 MHz */
4138 break;
Len Brown869ce69e2016-06-16 23:22:37 -04004139 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05004140 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04004141 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04004142 break;
4143 default:
4144 crystal_hz = 0;
4145 }
4146
4147 if (crystal_hz) {
4148 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05004149 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004150 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04004151 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4152 }
4153 }
4154 }
Len Brown61a87ba2015-11-23 02:30:51 -05004155 if (max_level >= 0x16) {
4156 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4157
4158 /*
4159 * CPUID 16H Base MHz, Max MHz, Bus MHz
4160 */
4161 base_mhz = max_mhz = bus_mhz = edx = 0;
4162
Len Brown5aea2f72016-03-13 03:14:35 -04004163 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05004164 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05004165 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05004166 base_mhz, max_mhz, bus_mhz);
4167 }
Len Brown8a5bdf42015-04-01 21:02:57 -04004168
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02004169 if (has_aperf)
4170 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4171
Len Brown812db3f2017-02-10 00:25:41 -05004172 BIC_PRESENT(BIC_IRQ);
4173 BIC_PRESENT(BIC_TSC_MHz);
4174
4175 if (probe_nhm_msrs(family, model)) {
4176 do_nhm_platform_info = 1;
4177 BIC_PRESENT(BIC_CPU_c1);
4178 BIC_PRESENT(BIC_CPU_c3);
4179 BIC_PRESENT(BIC_CPU_c6);
4180 BIC_PRESENT(BIC_SMI);
4181 }
Len Brownd7899442015-01-23 00:12:33 -05004182 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004183
4184 if (do_snb_cstates)
4185 BIC_PRESENT(BIC_CPU_c7);
4186
Len Brown5a634262016-04-06 17:15:55 -04004187 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004188 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4189 BIC_PRESENT(BIC_Pkgpc2);
4190 if (pkg_cstate_limit >= PCL__3)
4191 BIC_PRESENT(BIC_Pkgpc3);
4192 if (pkg_cstate_limit >= PCL__6)
4193 BIC_PRESENT(BIC_Pkgpc6);
4194 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4195 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba112017-02-10 00:27:20 -05004196 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004197 BIC_NOT_PRESENT(BIC_Pkgpc2);
4198 BIC_NOT_PRESENT(BIC_Pkgpc3);
4199 BIC_PRESENT(BIC_Pkgpc6);
4200 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba112017-02-10 00:27:20 -05004201 BIC_PRESENT(BIC_Mod_c6);
4202 use_c1_residency_msr = 1;
4203 }
Len Brown7170a372017-01-27 02:13:27 -05004204 if (is_dnv(family, model)) {
4205 BIC_PRESENT(BIC_CPU_c1);
4206 BIC_NOT_PRESENT(BIC_CPU_c3);
4207 BIC_NOT_PRESENT(BIC_Pkgpc3);
4208 BIC_NOT_PRESENT(BIC_CPU_c7);
4209 BIC_NOT_PRESENT(BIC_Pkgpc7);
4210 use_c1_residency_msr = 1;
4211 }
Len Brown34c761972017-01-27 02:36:41 -05004212 if (is_skx(family, model)) {
4213 BIC_NOT_PRESENT(BIC_CPU_c3);
4214 BIC_NOT_PRESENT(BIC_Pkgpc3);
4215 BIC_NOT_PRESENT(BIC_CPU_c7);
4216 BIC_NOT_PRESENT(BIC_Pkgpc7);
4217 }
Len Brownade0eba2017-02-10 01:56:47 -05004218 if (is_bdx(family, model)) {
4219 BIC_NOT_PRESENT(BIC_CPU_c7);
4220 BIC_NOT_PRESENT(BIC_Pkgpc7);
4221 }
Len Brown0f47c082017-01-27 00:50:45 -05004222 if (has_hsw_msrs(family, model)) {
4223 BIC_PRESENT(BIC_Pkgpc8);
4224 BIC_PRESENT(BIC_Pkgpc9);
4225 BIC_PRESENT(BIC_Pkgpc10);
4226 }
Len Brown5a634262016-04-06 17:15:55 -04004227 do_irtl_hsw = has_hsw_msrs(family, model);
Len Browna99d8732017-05-20 20:11:55 -04004228 if (has_skl_msrs(family, model)) {
4229 BIC_PRESENT(BIC_Totl_c0);
4230 BIC_PRESENT(BIC_Any_c0);
4231 BIC_PRESENT(BIC_GFX_c0);
4232 BIC_PRESENT(BIC_CPUGFX);
4233 }
Len Brown144b44b2013-11-09 00:30:16 -05004234 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004235 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004236
Len Brown96e47152017-01-21 02:26:00 -05004237 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004238 decode_misc_pwr_mgmt_msr();
4239
Len Brown96e47152017-01-21 02:26:00 -05004240 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004241 decode_c6_demotion_policy_msr();
4242
Len Brown889facb2012-11-08 00:48:57 -05004243 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004244 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004245
Len Brown96e47152017-01-21 02:26:00 -05004246 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004247 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004248
Len Brown41618e62017-02-09 18:25:22 -05004249 if (!quiet)
4250 dump_sysfs_cstate_config();
Len Brown7293fcc2017-02-22 00:11:12 -05004251 if (!quiet)
4252 dump_sysfs_pstate_config();
Len Brown41618e62017-02-09 18:25:22 -05004253
Len Browna2b7b742015-09-26 00:12:38 -04004254 if (has_skl_msrs(family, model))
4255 calculate_tsc_tweak();
4256
Len Brown812db3f2017-02-10 00:25:41 -05004257 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4258 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004259
Len Brown812db3f2017-02-10 00:25:41 -05004260 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4261 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004262
Len Brown96e47152017-01-21 02:26:00 -05004263 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004264 decode_misc_feature_control();
4265
Len Brown889facb2012-11-08 00:48:57 -05004266 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004267}
4268
Len Brown103a8fe2010-10-22 23:53:03 -04004269
4270/*
4271 * in /dev/cpu/ return success for names that are numbers
4272 * ie. filter out ".", "..", "microcode".
4273 */
4274int dir_filter(const struct dirent *dirp)
4275{
4276 if (isdigit(dirp->d_name[0]))
4277 return 1;
4278 else
4279 return 0;
4280}
4281
4282int open_dev_cpu_msr(int dummy1)
4283{
4284 return 0;
4285}
4286
Len Brownc98d5d92012-06-04 00:56:40 -04004287void topology_probe()
4288{
4289 int i;
4290 int max_core_id = 0;
4291 int max_package_id = 0;
4292 int max_siblings = 0;
4293 struct cpu_topology {
4294 int core_id;
4295 int physical_package_id;
4296 } *cpus;
4297
4298 /* Initialize num_cpus, max_cpu_num */
4299 topo.num_cpus = 0;
4300 topo.max_cpu_num = 0;
4301 for_all_proc_cpus(count_cpus);
4302 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004303 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004304
Len Brownd8af6f52015-02-10 01:56:38 -05004305 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004306 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004307
4308 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004309 if (cpus == NULL)
4310 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004311
4312 /*
4313 * Allocate and initialize cpu_present_set
4314 */
4315 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004316 if (cpu_present_set == NULL)
4317 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004318 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4319 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4320 for_all_proc_cpus(mark_cpu_present);
4321
4322 /*
Len Brown1ef7d212017-02-10 23:54:15 -05004323 * Validate that all cpus in cpu_subset are also in cpu_present_set
4324 */
4325 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4326 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4327 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4328 err(1, "cpu%d not present", i);
4329 }
4330
4331 /*
Len Brownc98d5d92012-06-04 00:56:40 -04004332 * Allocate and initialize cpu_affinity_set
4333 */
4334 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004335 if (cpu_affinity_set == NULL)
4336 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004337 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4338 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4339
4340
4341 /*
4342 * For online cpus
4343 * find max_core_id, max_package_id
4344 */
4345 for (i = 0; i <= topo.max_cpu_num; ++i) {
4346 int siblings;
4347
4348 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004349 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004350 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004351 continue;
4352 }
4353 cpus[i].core_id = get_core_id(i);
4354 if (cpus[i].core_id > max_core_id)
4355 max_core_id = cpus[i].core_id;
4356
4357 cpus[i].physical_package_id = get_physical_package_id(i);
4358 if (cpus[i].physical_package_id > max_package_id)
4359 max_package_id = cpus[i].physical_package_id;
4360
4361 siblings = get_num_ht_siblings(i);
4362 if (siblings > max_siblings)
4363 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004364 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004365 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004366 i, cpus[i].physical_package_id, cpus[i].core_id);
4367 }
4368 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004369 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004370 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004371 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004372 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004373 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004374
4375 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004376 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004377 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004378 max_package_id, topo.num_packages);
Len Brown7da6e3e2017-02-21 23:43:41 -05004379 if (!summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004380 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004381
4382 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004383 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004384 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004385
4386 free(cpus);
4387}
4388
4389void
4390allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4391{
4392 int i;
4393
4394 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004395 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004396 if (*t == NULL)
4397 goto error;
4398
4399 for (i = 0; i < topo.num_threads_per_core *
4400 topo.num_cores_per_pkg * topo.num_packages; i++)
4401 (*t)[i].cpu_id = -1;
4402
4403 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004404 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004405 if (*c == NULL)
4406 goto error;
4407
4408 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4409 (*c)[i].core_id = -1;
4410
Len Brown678a3bd2017-02-09 22:22:13 -05004411 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004412 if (*p == NULL)
4413 goto error;
4414
4415 for (i = 0; i < topo.num_packages; i++)
4416 (*p)[i].package_id = i;
4417
4418 return;
4419error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004420 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004421}
4422/*
4423 * init_counter()
4424 *
4425 * set cpu_id, core_num, pkg_num
4426 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4427 *
4428 * increment topo.num_cores when 1st core in pkg seen
4429 */
4430void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4431 struct pkg_data *pkg_base, int thread_num, int core_num,
4432 int pkg_num, int cpu_id)
4433{
4434 struct thread_data *t;
4435 struct core_data *c;
4436 struct pkg_data *p;
4437
4438 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4439 c = GET_CORE(core_base, core_num, pkg_num);
4440 p = GET_PKG(pkg_base, pkg_num);
4441
4442 t->cpu_id = cpu_id;
4443 if (thread_num == 0) {
4444 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4445 if (cpu_is_first_core_in_package(cpu_id))
4446 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4447 }
4448
4449 c->core_id = core_num;
4450 p->package_id = pkg_num;
4451}
4452
4453
4454int initialize_counters(int cpu_id)
4455{
4456 int my_thread_id, my_core_id, my_package_id;
4457
4458 my_package_id = get_physical_package_id(cpu_id);
4459 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004460 my_thread_id = get_cpu_position_in_core(cpu_id);
4461 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004462 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004463
4464 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4465 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4466 return 0;
4467}
4468
4469void allocate_output_buffer()
4470{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004471 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004472 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004473 if (outp == NULL)
4474 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004475}
Len Brown36229892016-02-26 20:51:02 -05004476void allocate_fd_percpu(void)
4477{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004478 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004479 if (fd_percpu == NULL)
4480 err(-1, "calloc fd_percpu");
4481}
Len Brown562a2d32016-02-26 23:48:05 -05004482void allocate_irq_buffers(void)
4483{
4484 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4485 if (irq_column_2_cpu == NULL)
4486 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004487
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004488 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004489 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004490 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004491}
Len Brownc98d5d92012-06-04 00:56:40 -04004492void setup_all_buffers(void)
4493{
4494 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004495 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004496 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004497 allocate_counters(&thread_even, &core_even, &package_even);
4498 allocate_counters(&thread_odd, &core_odd, &package_odd);
4499 allocate_output_buffer();
4500 for_all_proc_cpus(initialize_counters);
4501}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004502
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004503void set_base_cpu(void)
4504{
4505 base_cpu = sched_getcpu();
4506 if (base_cpu < 0)
4507 err(-ENODEV, "No valid cpus found");
4508
4509 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004510 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004511}
4512
Len Brown103a8fe2010-10-22 23:53:03 -04004513void turbostat_init()
4514{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004515 setup_all_buffers();
4516 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004517 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004518 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004519 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004520
Len Brown103a8fe2010-10-22 23:53:03 -04004521
Len Brown96e47152017-01-21 02:26:00 -05004522 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004523 for_all_cpus(print_hwp, ODD_COUNTERS);
4524
Len Brown96e47152017-01-21 02:26:00 -05004525 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004526 for_all_cpus(print_epb, ODD_COUNTERS);
4527
Len Brown96e47152017-01-21 02:26:00 -05004528 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004529 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4530
Len Brown96e47152017-01-21 02:26:00 -05004531 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004532 for_all_cpus(print_rapl, ODD_COUNTERS);
4533
4534 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4535
Len Brown96e47152017-01-21 02:26:00 -05004536 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004537 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004538
Len Brown96e47152017-01-21 02:26:00 -05004539 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004540 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004541}
4542
4543int fork_it(char **argv)
4544{
Len Brown103a8fe2010-10-22 23:53:03 -04004545 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004546 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004547
Len Brown218f0e82017-02-14 22:07:52 -05004548 snapshot_proc_sysfs_files();
Len Brownd91bb172012-11-01 00:08:19 -04004549 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4550 if (status)
4551 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004552 /* clear affinity side-effect of get_counters() */
4553 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004554 gettimeofday(&tv_even, (struct timezone *)NULL);
4555
4556 child_pid = fork();
4557 if (!child_pid) {
4558 /* child */
4559 execvp(argv[0], argv);
Len Brown0815a3d2017-02-23 17:00:51 -05004560 err(errno, "exec %s", argv[0]);
Len Brown103a8fe2010-10-22 23:53:03 -04004561 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004562
4563 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004564 if (child_pid == -1)
4565 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004566
4567 signal(SIGINT, SIG_IGN);
4568 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004569 if (waitpid(child_pid, &status, 0) == -1)
4570 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004571 }
Len Brownc98d5d92012-06-04 00:56:40 -04004572 /*
4573 * n.b. fork_it() does not check for errors from for_all_cpus()
4574 * because re-starting is problematic when forking
4575 */
Len Brown218f0e82017-02-14 22:07:52 -05004576 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04004577 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004578 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004579 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004580 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4581 fprintf(outf, "%s: Counter reset detected\n", progname);
4582 else {
4583 compute_average(EVEN_COUNTERS);
4584 format_all_counters(EVEN_COUNTERS);
4585 }
Len Brown103a8fe2010-10-22 23:53:03 -04004586
Len Brownb7d8c142016-02-13 23:36:17 -05004587 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4588
4589 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004590
Len Brownd91bb172012-11-01 00:08:19 -04004591 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004592}
4593
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004594int get_and_dump_counters(void)
4595{
4596 int status;
4597
Len Brown218f0e82017-02-14 22:07:52 -05004598 snapshot_proc_sysfs_files();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004599 status = for_all_cpus(get_counters, ODD_COUNTERS);
4600 if (status)
4601 return status;
4602
4603 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4604 if (status)
4605 return status;
4606
Len Brownb7d8c142016-02-13 23:36:17 -05004607 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004608
4609 return status;
4610}
4611
Len Brownd8af6f52015-02-10 01:56:38 -05004612void print_version() {
Len Brown5f9bf022017-03-04 17:26:29 -05004613 fprintf(outf, "turbostat version 17.04.12"
Len Brownd8af6f52015-02-10 01:56:38 -05004614 " - Len Brown <lenb@kernel.org>\n");
4615}
4616
Len Brown495c76542017-02-08 02:41:51 -05004617int add_counter(unsigned int msr_num, char *path, char *name,
4618 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004619 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004620{
4621 struct msr_counter *msrp;
4622
4623 msrp = calloc(1, sizeof(struct msr_counter));
4624 if (msrp == NULL) {
4625 perror("calloc");
4626 exit(1);
4627 }
4628
4629 msrp->msr_num = msr_num;
4630 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004631 if (path)
4632 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004633 msrp->width = width;
4634 msrp->type = type;
4635 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004636 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004637
4638 switch (scope) {
4639
4640 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004641 msrp->next = sys.tp;
4642 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004643 sys.added_thread_counters++;
4644 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4645 fprintf(stderr, "exceeded max %d added thread counters\n",
4646 MAX_ADDED_COUNTERS);
4647 exit(-1);
4648 }
Len Brown388e9c82016-12-22 23:57:55 -05004649 break;
4650
4651 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004652 msrp->next = sys.cp;
4653 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004654 sys.added_core_counters++;
4655 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4656 fprintf(stderr, "exceeded max %d added core counters\n",
4657 MAX_ADDED_COUNTERS);
4658 exit(-1);
4659 }
Len Brown388e9c82016-12-22 23:57:55 -05004660 break;
4661
4662 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004663 msrp->next = sys.pp;
4664 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004665 sys.added_package_counters++;
4666 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4667 fprintf(stderr, "exceeded max %d added package counters\n",
4668 MAX_ADDED_COUNTERS);
4669 exit(-1);
4670 }
Len Brown388e9c82016-12-22 23:57:55 -05004671 break;
4672 }
4673
4674 return 0;
4675}
4676
4677void parse_add_command(char *add_command)
4678{
4679 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004680 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004681 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004682 int width = 64;
4683 int fail = 0;
4684 enum counter_scope scope = SCOPE_CPU;
4685 enum counter_type type = COUNTER_CYCLES;
4686 enum counter_format format = FORMAT_DELTA;
4687
4688 while (add_command) {
4689
4690 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4691 goto next;
4692
4693 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4694 goto next;
4695
Len Brown495c76542017-02-08 02:41:51 -05004696 if (*add_command == '/') {
4697 path = add_command;
4698 goto next;
4699 }
4700
Len Brown388e9c82016-12-22 23:57:55 -05004701 if (sscanf(add_command, "u%d", &width) == 1) {
4702 if ((width == 32) || (width == 64))
4703 goto next;
4704 width = 64;
4705 }
4706 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4707 scope = SCOPE_CPU;
4708 goto next;
4709 }
4710 if (!strncmp(add_command, "core", strlen("core"))) {
4711 scope = SCOPE_CORE;
4712 goto next;
4713 }
4714 if (!strncmp(add_command, "package", strlen("package"))) {
4715 scope = SCOPE_PACKAGE;
4716 goto next;
4717 }
4718 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4719 type = COUNTER_CYCLES;
4720 goto next;
4721 }
4722 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4723 type = COUNTER_SECONDS;
4724 goto next;
4725 }
Len Brown41618e62017-02-09 18:25:22 -05004726 if (!strncmp(add_command, "usec", strlen("usec"))) {
4727 type = COUNTER_USEC;
4728 goto next;
4729 }
Len Brown388e9c82016-12-22 23:57:55 -05004730 if (!strncmp(add_command, "raw", strlen("raw"))) {
4731 format = FORMAT_RAW;
4732 goto next;
4733 }
4734 if (!strncmp(add_command, "delta", strlen("delta"))) {
4735 format = FORMAT_DELTA;
4736 goto next;
4737 }
4738 if (!strncmp(add_command, "percent", strlen("percent"))) {
4739 format = FORMAT_PERCENT;
4740 goto next;
4741 }
4742
4743 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4744 char *eos;
4745
4746 eos = strchr(name_buffer, ',');
4747 if (eos)
4748 *eos = '\0';
4749 goto next;
4750 }
4751
4752next:
4753 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004754 if (add_command) {
4755 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004756 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004757 }
Len Brown388e9c82016-12-22 23:57:55 -05004758
4759 }
Len Brown495c76542017-02-08 02:41:51 -05004760 if ((msr_num == 0) && (path == NULL)) {
4761 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004762 fail++;
4763 }
4764
4765 /* generate default column header */
4766 if (*name_buffer == '\0') {
Len Brown5f3aea52017-02-23 18:10:27 -05004767 if (width == 32)
4768 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4769 else
4770 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
Len Brown388e9c82016-12-22 23:57:55 -05004771 }
4772
Len Brown41618e62017-02-09 18:25:22 -05004773 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05004774 fail++;
4775
4776 if (fail) {
4777 help();
4778 exit(1);
4779 }
4780}
Len Brown41618e62017-02-09 18:25:22 -05004781
Len Browndd778a52017-02-21 23:21:13 -05004782int is_deferred_skip(char *name)
4783{
4784 int i;
4785
4786 for (i = 0; i < deferred_skip_index; ++i)
4787 if (!strcmp(name, deferred_skip_names[i]))
4788 return 1;
4789 return 0;
4790}
4791
Len Brown41618e62017-02-09 18:25:22 -05004792void probe_sysfs(void)
4793{
4794 char path[64];
4795 char name_buf[16];
4796 FILE *input;
4797 int state;
4798 char *sp;
4799
4800 if (!DO_BIC(BIC_sysfs))
4801 return;
4802
4803 for (state = 10; state > 0; --state) {
4804
4805 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4806 base_cpu, state);
4807 input = fopen(path, "r");
4808 if (input == NULL)
4809 continue;
4810 fgets(name_buf, sizeof(name_buf), input);
4811
4812 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4813 sp = strchr(name_buf, '-');
4814 if (!sp)
4815 sp = strchrnul(name_buf, '\n');
4816 *sp = '%';
4817 *(sp + 1) = '\0';
4818
4819 fclose(input);
4820
4821 sprintf(path, "cpuidle/state%d/time", state);
4822
Len Browndd778a52017-02-21 23:21:13 -05004823 if (is_deferred_skip(name_buf))
4824 continue;
4825
Len Brown41618e62017-02-09 18:25:22 -05004826 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4827 FORMAT_PERCENT, SYSFS_PERCPU);
4828 }
4829
4830 for (state = 10; state > 0; --state) {
4831
4832 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4833 base_cpu, state);
4834 input = fopen(path, "r");
4835 if (input == NULL)
4836 continue;
4837 fgets(name_buf, sizeof(name_buf), input);
4838 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4839 sp = strchr(name_buf, '-');
4840 if (!sp)
4841 sp = strchrnul(name_buf, '\n');
4842 *sp = '\0';
4843 fclose(input);
4844
4845 sprintf(path, "cpuidle/state%d/usage", state);
4846
Len Browndd778a52017-02-21 23:21:13 -05004847 if (is_deferred_skip(name_buf))
4848 continue;
4849
Len Brown41618e62017-02-09 18:25:22 -05004850 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4851 FORMAT_DELTA, SYSFS_PERCPU);
4852 }
4853
4854}
4855
Len Brown1ef7d212017-02-10 23:54:15 -05004856
4857/*
4858 * parse cpuset with following syntax
4859 * 1,2,4..6,8-10 and set bits in cpu_subset
4860 */
4861void parse_cpu_command(char *optarg)
4862{
4863 unsigned int start, end;
4864 char *next;
4865
Len Brown4e4e1e72017-02-21 22:33:42 -05004866 if (!strcmp(optarg, "core")) {
4867 if (cpu_subset)
4868 goto error;
4869 show_core_only++;
4870 return;
4871 }
4872 if (!strcmp(optarg, "package")) {
4873 if (cpu_subset)
4874 goto error;
4875 show_pkg_only++;
4876 return;
4877 }
4878 if (show_core_only || show_pkg_only)
4879 goto error;
4880
Len Brown1ef7d212017-02-10 23:54:15 -05004881 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
4882 if (cpu_subset == NULL)
4883 err(3, "CPU_ALLOC");
4884 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
4885
4886 CPU_ZERO_S(cpu_subset_size, cpu_subset);
4887
4888 next = optarg;
4889
4890 while (next && *next) {
4891
4892 if (*next == '-') /* no negative cpu numbers */
4893 goto error;
4894
4895 start = strtoul(next, &next, 10);
4896
4897 if (start >= CPU_SUBSET_MAXCPUS)
4898 goto error;
4899 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4900
4901 if (*next == '\0')
4902 break;
4903
4904 if (*next == ',') {
4905 next += 1;
4906 continue;
4907 }
4908
4909 if (*next == '-') {
4910 next += 1; /* start range */
4911 } else if (*next == '.') {
4912 next += 1;
4913 if (*next == '.')
4914 next += 1; /* start range */
4915 else
4916 goto error;
4917 }
4918
4919 end = strtoul(next, &next, 10);
4920 if (end <= start)
4921 goto error;
4922
4923 while (++start <= end) {
4924 if (start >= CPU_SUBSET_MAXCPUS)
4925 goto error;
4926 CPU_SET_S(start, cpu_subset_size, cpu_subset);
4927 }
4928
4929 if (*next == ',')
4930 next += 1;
4931 else if (*next != '\0')
4932 goto error;
4933 }
4934
4935 return;
4936
4937error:
Len Brown4e4e1e72017-02-21 22:33:42 -05004938 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
4939 help();
Len Brown1ef7d212017-02-10 23:54:15 -05004940 exit(-1);
4941}
4942
Len Brown812db3f2017-02-10 00:25:41 -05004943int shown;
4944/*
4945 * parse_show_hide() - process cmdline to set default counter action
4946 */
4947void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4948{
4949 /*
4950 * --show: show only those specified
4951 * The 1st invocation will clear and replace the enabled mask
4952 * subsequent invocations can add to it.
4953 */
4954 if (new_mode == SHOW_LIST) {
4955 if (shown == 0)
Len Browndd778a52017-02-21 23:21:13 -05004956 bic_enabled = bic_lookup(optarg, new_mode);
Len Brown812db3f2017-02-10 00:25:41 -05004957 else
Len Browndd778a52017-02-21 23:21:13 -05004958 bic_enabled |= bic_lookup(optarg, new_mode);
Len Brown812db3f2017-02-10 00:25:41 -05004959 shown = 1;
4960
4961 return;
4962 }
4963
4964 /*
4965 * --hide: do not show those specified
4966 * multiple invocations simply clear more bits in enabled mask
4967 */
Len Browndd778a52017-02-21 23:21:13 -05004968 bic_enabled &= ~bic_lookup(optarg, new_mode);
Len Brown41618e62017-02-09 18:25:22 -05004969
Len Brown812db3f2017-02-10 00:25:41 -05004970}
4971
Len Brown103a8fe2010-10-22 23:53:03 -04004972void cmdline(int argc, char **argv)
4973{
4974 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004975 int option_index = 0;
4976 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004977 {"add", required_argument, 0, 'a'},
Len Brown1ef7d212017-02-10 23:54:15 -05004978 {"cpu", required_argument, 0, 'c'},
Len Brownd8af6f52015-02-10 01:56:38 -05004979 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05004980 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brownd8af6f52015-02-10 01:56:38 -05004981 {"interval", required_argument, 0, 'i'},
4982 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004983 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004984 {"Joules", no_argument, 0, 'J'},
Len Brownc8ade362017-02-15 17:15:11 -05004985 {"list", no_argument, 0, 'l'},
Len Brownb7d8c142016-02-13 23:36:17 -05004986 {"out", required_argument, 0, 'o'},
Len Brown96e47152017-01-21 02:26:00 -05004987 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05004988 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004989 {"Summary", no_argument, 0, 'S'},
4990 {"TCC", required_argument, 0, 'T'},
4991 {"version", no_argument, 0, 'v' },
4992 {0, 0, 0, 0 }
4993 };
Len Brown103a8fe2010-10-22 23:53:03 -04004994
4995 progname = argv[0];
4996
Len Brown4e4e1e72017-02-21 22:33:42 -05004997 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:qST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004998 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004999 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05005000 case 'a':
5001 parse_add_command(optarg);
5002 break;
Len Brown1ef7d212017-02-10 23:54:15 -05005003 case 'c':
5004 parse_cpu_command(optarg);
5005 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005006 case 'D':
5007 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04005008 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005009 case 'd':
5010 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04005011 break;
Len Brown812db3f2017-02-10 00:25:41 -05005012 case 'H':
5013 parse_show_hide(optarg, HIDE_LIST);
5014 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005015 case 'h':
5016 default:
5017 help();
5018 exit(1);
5019 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05005020 {
5021 double interval = strtod(optarg, NULL);
5022
5023 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05005024 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05005025 interval);
5026 exit(2);
5027 }
5028
5029 interval_ts.tv_sec = interval;
5030 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
5031 }
Len Brown889facb2012-11-08 00:48:57 -05005032 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08005033 case 'J':
5034 rapl_joules++;
5035 break;
Len Brownc8ade362017-02-15 17:15:11 -05005036 case 'l':
5037 list_header_only++;
5038 quiet++;
5039 break;
Len Brownb7d8c142016-02-13 23:36:17 -05005040 case 'o':
5041 outf = fopen_or_die(optarg, "w");
5042 break;
Len Brown96e47152017-01-21 02:26:00 -05005043 case 'q':
5044 quiet = 1;
5045 break;
Len Brown812db3f2017-02-10 00:25:41 -05005046 case 's':
5047 parse_show_hide(optarg, SHOW_LIST);
5048 break;
Len Brownd8af6f52015-02-10 01:56:38 -05005049 case 'S':
5050 summary_only++;
5051 break;
5052 case 'T':
5053 tcc_activation_temp_override = atoi(optarg);
5054 break;
5055 case 'v':
5056 print_version();
5057 exit(0);
5058 break;
Len Brown103a8fe2010-10-22 23:53:03 -04005059 }
5060 }
5061}
5062
5063int main(int argc, char **argv)
5064{
Len Brownb7d8c142016-02-13 23:36:17 -05005065 outf = stderr;
5066
Len Brown103a8fe2010-10-22 23:53:03 -04005067 cmdline(argc, argv);
5068
Len Brown96e47152017-01-21 02:26:00 -05005069 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05005070 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04005071
Len Brown41618e62017-02-09 18:25:22 -05005072 probe_sysfs();
5073
Len Brown103a8fe2010-10-22 23:53:03 -04005074 turbostat_init();
5075
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02005076 /* dump counters and exit */
5077 if (dump_only)
5078 return get_and_dump_counters();
5079
Len Brownc8ade362017-02-15 17:15:11 -05005080 /* list header and exit */
5081 if (list_header_only) {
5082 print_header(",");
5083 flush_output_stdout();
5084 return 0;
5085 }
5086
Len Brown103a8fe2010-10-22 23:53:03 -04005087 /*
5088 * if any params left, it must be a command to fork
5089 */
5090 if (argc - optind)
5091 return fork_it(argv + optind);
5092 else
5093 turbostat_loop();
5094
5095 return 0;
5096}