blob: 6867557596afcc5f408498faa86e770a6540d712 [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 Brownd8af6f52015-02-10 01:56:38 -050053unsigned int rapl_joules;
54unsigned int summary_only;
55unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040056unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070057unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050058unsigned int do_pc2;
59unsigned int do_pc3;
60unsigned int do_pc6;
61unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080062unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040063unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050064unsigned int do_slm_cstates;
65unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050067unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040068unsigned int do_irtl_snb;
69unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050070unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040071unsigned int genuine_intel;
72unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050073unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050074unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020075unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040076double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040077double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040078unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040079double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040080unsigned int show_pkg_only;
81unsigned int show_core_only;
82char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050083unsigned int do_rapl;
84unsigned int do_dts;
85unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050086unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050087unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050088unsigned int tcc_activation_temp;
89unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050090double rapl_power_units, rapl_time_units;
91double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050092double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040093unsigned int do_core_perf_limit_reasons;
94unsigned int do_gfx_perf_limit_reasons;
95unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040096unsigned int crystal_hz;
97unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040098int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040099double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500100unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
101 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
102unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
103unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
104unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
105unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500106unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500107
Len Browne6f9bb32013-12-03 02:19:19 -0500108#define RAPL_PKG (1 << 0)
109 /* 0x610 MSR_PKG_POWER_LIMIT */
110 /* 0x611 MSR_PKG_ENERGY_STATUS */
111#define RAPL_PKG_PERF_STATUS (1 << 1)
112 /* 0x613 MSR_PKG_PERF_STATUS */
113#define RAPL_PKG_POWER_INFO (1 << 2)
114 /* 0x614 MSR_PKG_POWER_INFO */
115
116#define RAPL_DRAM (1 << 3)
117 /* 0x618 MSR_DRAM_POWER_LIMIT */
118 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500119#define RAPL_DRAM_PERF_STATUS (1 << 4)
120 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400121#define RAPL_DRAM_POWER_INFO (1 << 5)
122 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500123
Jacob Pan91484942016-06-16 09:48:20 -0700124#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500125 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400126#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500127 /* 0x63a MSR_PP0_POLICY */
128
Len Brown0b2bb692015-03-26 00:50:30 -0400129#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500130 /* 0x640 MSR_PP1_POWER_LIMIT */
131 /* 0x641 MSR_PP1_ENERGY_STATUS */
132 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700133
134#define RAPL_CORES_ENERGY_STATUS (1 << 9)
135 /* 0x639 MSR_PP0_ENERGY_STATUS */
136#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500137#define TJMAX_DEFAULT 100
138
139#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400140
Len Brown388e9c82016-12-22 23:57:55 -0500141/*
142 * buffer size used by sscanf() for added column names
143 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
144 */
145#define NAME_BYTES 20
146
Len Brown103a8fe2010-10-22 23:53:03 -0400147int backwards_count;
148char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400149
Len Brownc98d5d92012-06-04 00:56:40 -0400150cpu_set_t *cpu_present_set, *cpu_affinity_set;
151size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown678a3bd2017-02-09 22:22:13 -0500152#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400153
Len Brownc98d5d92012-06-04 00:56:40 -0400154struct thread_data {
155 unsigned long long tsc;
156 unsigned long long aperf;
157 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500158 unsigned long long c1;
Len Brown562a2d32016-02-26 23:48:05 -0500159 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500160 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400161 unsigned int cpu_id;
162 unsigned int flags;
163#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
164#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500165 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400166} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400167
Len Brownc98d5d92012-06-04 00:56:40 -0400168struct core_data {
169 unsigned long long c3;
170 unsigned long long c6;
171 unsigned long long c7;
Len Brown0539ba112017-02-10 00:27:20 -0500172 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500173 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400174 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500175 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400176} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400177
Len Brownc98d5d92012-06-04 00:56:40 -0400178struct pkg_data {
179 unsigned long long pc2;
180 unsigned long long pc3;
181 unsigned long long pc6;
182 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800183 unsigned long long pc8;
184 unsigned long long pc9;
185 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400186 unsigned long long pkg_wtd_core_c0;
187 unsigned long long pkg_any_core_c0;
188 unsigned long long pkg_any_gfxe_c0;
189 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400190 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500191 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400192 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500193 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
194 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
195 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
196 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
197 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
198 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
199 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500200 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400201} *package_even, *package_odd;
202
203#define ODD_COUNTERS thread_odd, core_odd, package_odd
204#define EVEN_COUNTERS thread_even, core_even, package_even
205
206#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
207 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
208 topo.num_threads_per_core + \
209 (core_no) * topo.num_threads_per_core + (thread_no))
210#define GET_CORE(core_base, core_no, pkg_no) \
211 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
212#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
213
Len Brown388e9c82016-12-22 23:57:55 -0500214enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
215enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
216enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
217
218struct msr_counter {
219 unsigned int msr_num;
220 char name[NAME_BYTES];
221 unsigned int width;
222 enum counter_type type;
223 enum counter_format format;
224 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500225 unsigned int flags;
226#define FLAGS_HIDE (1 << 0)
227#define FLAGS_SHOW (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" },
352 { 0x0, "SMI", 32, 0, FORMAT_DELTA, NULL},
353 { 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" },
368 { 0x0, "PkgWatt" },
369 { 0x0, "CorWatt" },
370 { 0x0, "GFXWatt" },
371 { 0x0, "PkgCnt" },
372 { 0x0, "RAMWatt" },
373 { 0x0, "PKG_%" },
374 { 0x0, "RAM_%" },
375 { 0x0, "Pkg_J" },
376 { 0x0, "Cor_J" },
377 { 0x0, "GFX_J" },
378 { 0x0, "RAM_J" },
379 { 0x0, "Core" },
380 { 0x0, "CPU" },
Len Brown0539ba112017-02-10 00:27:20 -0500381 { 0x0, "Mod%c6" },
Len Brown812db3f2017-02-10 00:25:41 -0500382};
383
384#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
385#define BIC_Package (1ULL << 0)
386#define BIC_Avg_MHz (1ULL << 1)
387#define BIC_Bzy_MHz (1ULL << 2)
388#define BIC_TSC_MHz (1ULL << 3)
389#define BIC_IRQ (1ULL << 4)
390#define BIC_SMI (1ULL << 5)
391#define BIC_Busy (1ULL << 6)
392#define BIC_CPU_c1 (1ULL << 7)
393#define BIC_CPU_c3 (1ULL << 8)
394#define BIC_CPU_c6 (1ULL << 9)
395#define BIC_CPU_c7 (1ULL << 10)
396#define BIC_ThreadC (1ULL << 11)
397#define BIC_CoreTmp (1ULL << 12)
398#define BIC_CoreCnt (1ULL << 13)
399#define BIC_PkgTmp (1ULL << 14)
400#define BIC_GFX_rc6 (1ULL << 15)
401#define BIC_GFXMHz (1ULL << 16)
402#define BIC_Pkgpc2 (1ULL << 17)
403#define BIC_Pkgpc3 (1ULL << 18)
404#define BIC_Pkgpc6 (1ULL << 19)
405#define BIC_Pkgpc7 (1ULL << 20)
406#define BIC_PkgWatt (1ULL << 21)
407#define BIC_CorWatt (1ULL << 22)
408#define BIC_GFXWatt (1ULL << 23)
409#define BIC_PkgCnt (1ULL << 24)
410#define BIC_RAMWatt (1ULL << 27)
411#define BIC_PKG__ (1ULL << 28)
412#define BIC_RAM__ (1ULL << 29)
413#define BIC_Pkg_J (1ULL << 30)
414#define BIC_Cor_J (1ULL << 31)
415#define BIC_GFX_J (1ULL << 30)
416#define BIC_RAM_J (1ULL << 31)
417#define BIC_Core (1ULL << 32)
418#define BIC_CPU (1ULL << 33)
Len Brown0539ba112017-02-10 00:27:20 -0500419#define BIC_Mod_c6 (1ULL << 34)
Len Brown812db3f2017-02-10 00:25:41 -0500420
421unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
422unsigned long long bic_present;
423
424#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
425#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
426
427/*
428 * bic_lookup
429 * for all the strings in comma separate name_list,
430 * set the approprate bit in return value.
431 */
432unsigned long long bic_lookup(char *name_list)
433{
434 int i;
435 unsigned long long retval = 0;
436
437 while (name_list) {
438 char *comma;
439
440 comma = strchr(name_list, ',');
441
442 if (comma)
443 *comma = '\0';
444
445 for (i = 0; i < MAX_BIC; ++i) {
446 if (!strcmp(name_list, bic[i].name)) {
447 retval |= (1ULL << i);
448 break;
449 }
450 }
451 if (i == MAX_BIC) {
452 fprintf(stderr, "Invalid counter name: %s\n", name_list);
453 exit(-1);
454 }
455
456 name_list = comma;
457 if (name_list)
458 name_list++;
459
460 }
461 return retval;
462}
Len Brownfc04cc62014-02-06 00:55:19 -0500463
Len Browna829eb42011-02-10 23:36:34 -0500464void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400465{
Len Brown388e9c82016-12-22 23:57:55 -0500466 struct msr_counter *mp;
467
Len Brown812db3f2017-02-10 00:25:41 -0500468 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400469 outp += sprintf(outp, "\tPackage");
Len Brown812db3f2017-02-10 00:25:41 -0500470 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400471 outp += sprintf(outp, "\tCore");
Len Brown812db3f2017-02-10 00:25:41 -0500472 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400473 outp += sprintf(outp, "\tCPU");
Len Brown812db3f2017-02-10 00:25:41 -0500474 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400475 outp += sprintf(outp, "\tAvg_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500476 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400477 outp += sprintf(outp, "\tBusy%%");
Len Brown812db3f2017-02-10 00:25:41 -0500478 if (DO_BIC(BIC_Bzy_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400479 outp += sprintf(outp, "\tBzy_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500480 if (DO_BIC(BIC_TSC_MHz))
481 outp += sprintf(outp, "\tTSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500482
Len Brown812db3f2017-02-10 00:25:41 -0500483 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400484 outp += sprintf(outp, "\tIRQ");
Len Brown812db3f2017-02-10 00:25:41 -0500485 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400486 outp += sprintf(outp, "\tSMI");
Len Brown1cc21f72015-02-23 00:34:57 -0500487
Len Brown812db3f2017-02-10 00:25:41 -0500488 if (DO_BIC(BIC_CPU_c1))
Len Brown3d109de2016-04-22 23:24:27 -0400489 outp += sprintf(outp, "\tCPU%%c1");
Len Brown889facb2012-11-08 00:48:57 -0500490
Len Brown388e9c82016-12-22 23:57:55 -0500491 for (mp = sys.tp; mp; mp = mp->next) {
492 if (mp->format == FORMAT_RAW) {
493 if (mp->width == 64)
494 outp += sprintf(outp, "\t%18.18s", mp->name);
495 else
496 outp += sprintf(outp, "\t%10.10s", mp->name);
497 } else {
498 outp += sprintf(outp, "\t%-7.7s", mp->name);
499 }
500 }
501
Len Brown812db3f2017-02-10 00:25:41 -0500502 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown678a3bd2017-02-09 22:22:13 -0500503 outp += sprintf(outp, "\tCPU%%c3");
Len Brown812db3f2017-02-10 00:25:41 -0500504 if (DO_BIC(BIC_CPU_c6))
Len Brown678a3bd2017-02-09 22:22:13 -0500505 outp += sprintf(outp, "\tCPU%%c6");
Len Brown812db3f2017-02-10 00:25:41 -0500506 if (DO_BIC(BIC_CPU_c7))
Len Brown678a3bd2017-02-09 22:22:13 -0500507 outp += sprintf(outp, "\tCPU%%c7");
508
Len Brown0539ba112017-02-10 00:27:20 -0500509 if (DO_BIC(BIC_Mod_c6))
510 outp += sprintf(outp, "\tMod%%c6");
Len Brown678a3bd2017-02-09 22:22:13 -0500511
Len Brown812db3f2017-02-10 00:25:41 -0500512 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400513 outp += sprintf(outp, "\tCoreTmp");
Len Brown388e9c82016-12-22 23:57:55 -0500514
515 for (mp = sys.cp; mp; mp = mp->next) {
516 if (mp->format == FORMAT_RAW) {
517 if (mp->width == 64)
518 outp += sprintf(outp, "\t%18.18s", mp->name);
519 else
520 outp += sprintf(outp, "\t%10.10s", mp->name);
521 } else {
522 outp += sprintf(outp, "\t%-7.7s", mp->name);
523 }
524 }
525
Len Brown812db3f2017-02-10 00:25:41 -0500526 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400527 outp += sprintf(outp, "\tPkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500528
Len Brown812db3f2017-02-10 00:25:41 -0500529 if (DO_BIC(BIC_GFX_rc6))
Len Brown3d109de2016-04-22 23:24:27 -0400530 outp += sprintf(outp, "\tGFX%%rc6");
Len Brownfdf676e2016-02-27 01:28:12 -0500531
Len Brown812db3f2017-02-10 00:25:41 -0500532 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400533 outp += sprintf(outp, "\tGFXMHz");
Len Brown27d47352016-02-27 00:37:54 -0500534
Len Brown0b2bb692015-03-26 00:50:30 -0400535 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400536 outp += sprintf(outp, "\tTotl%%C0");
537 outp += sprintf(outp, "\tAny%%C0");
538 outp += sprintf(outp, "\tGFX%%C0");
539 outp += sprintf(outp, "\tCPUGFX%%");
Len Brown0b2bb692015-03-26 00:50:30 -0400540 }
541
Len Brownee7e38e2015-02-09 23:39:45 -0500542 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400543 outp += sprintf(outp, "\tPkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500544 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400545 outp += sprintf(outp, "\tPkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500546 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400547 outp += sprintf(outp, "\tPkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500548 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400549 outp += sprintf(outp, "\tPkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800550 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400551 outp += sprintf(outp, "\tPkg%%pc8");
552 outp += sprintf(outp, "\tPkg%%pc9");
553 outp += sprintf(outp, "\tPk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800554 }
Len Brown103a8fe2010-10-22 23:53:03 -0400555
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800556 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500557 if (DO_BIC(BIC_PkgWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400558 outp += sprintf(outp, "\tPkgWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500559 if (DO_BIC(BIC_CorWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400560 outp += sprintf(outp, "\tCorWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500561 if (DO_BIC(BIC_GFXWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400562 outp += sprintf(outp, "\tGFXWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500563 if (DO_BIC(BIC_RAMWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400564 outp += sprintf(outp, "\tRAMWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500565 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400566 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500567 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400568 outp += sprintf(outp, "\tRAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500569 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500570 if (DO_BIC(BIC_Pkg_J))
Len Brown3d109de2016-04-22 23:24:27 -0400571 outp += sprintf(outp, "\tPkg_J");
Len Brown812db3f2017-02-10 00:25:41 -0500572 if (DO_BIC(BIC_Cor_J))
Len Brown3d109de2016-04-22 23:24:27 -0400573 outp += sprintf(outp, "\tCor_J");
Len Brown812db3f2017-02-10 00:25:41 -0500574 if (DO_BIC(BIC_GFX_J))
Len Brown3d109de2016-04-22 23:24:27 -0400575 outp += sprintf(outp, "\tGFX_J");
Len Brown812db3f2017-02-10 00:25:41 -0500576 if (DO_BIC(BIC_RAM_J))
Len Brown3d109de2016-04-22 23:24:27 -0400577 outp += sprintf(outp, "\tRAM_J");
Len Brown812db3f2017-02-10 00:25:41 -0500578 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400579 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500580 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400581 outp += sprintf(outp, "\tRAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800582 }
Len Brown388e9c82016-12-22 23:57:55 -0500583 for (mp = sys.pp; mp; mp = mp->next) {
584 if (mp->format == FORMAT_RAW) {
585 if (mp->width == 64)
586 outp += sprintf(outp, "\t%18.18s", mp->name);
587 else
588 outp += sprintf(outp, "\t%10.10s", mp->name);
589 } else {
590 outp += sprintf(outp, "\t%-7.7s", mp->name);
591 }
592 }
593
Len Brownc98d5d92012-06-04 00:56:40 -0400594 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400595}
596
Len Brownc98d5d92012-06-04 00:56:40 -0400597int dump_counters(struct thread_data *t, struct core_data *c,
598 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400599{
Len Brown388e9c82016-12-22 23:57:55 -0500600 int i;
601 struct msr_counter *mp;
602
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200603 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400604
Len Brownc98d5d92012-06-04 00:56:40 -0400605 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200606 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
607 t->cpu_id, t->flags);
608 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
609 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
610 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
611 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500612
Len Brown812db3f2017-02-10 00:25:41 -0500613 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -0500614 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500615 if (DO_BIC(BIC_SMI))
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200616 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500617
618 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
619 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
620 i, mp->msr_num, t->counter[i]);
621 }
Len Brownc98d5d92012-06-04 00:56:40 -0400622 }
Len Brown103a8fe2010-10-22 23:53:03 -0400623
Len Brownc98d5d92012-06-04 00:56:40 -0400624 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200625 outp += sprintf(outp, "core: %d\n", c->core_id);
626 outp += sprintf(outp, "c3: %016llX\n", c->c3);
627 outp += sprintf(outp, "c6: %016llX\n", c->c6);
628 outp += sprintf(outp, "c7: %016llX\n", c->c7);
629 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500630
631 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
632 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
633 i, mp->msr_num, c->counter[i]);
634 }
Len Brown0539ba112017-02-10 00:27:20 -0500635 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400636 }
637
638 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200639 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400640
641 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
642 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
643 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
644 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
645
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200646 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500647 if (do_pc3)
648 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
649 if (do_pc6)
650 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
651 if (do_pc7)
652 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200653 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
654 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
655 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
656 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
657 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
658 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
659 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
660 outp += sprintf(outp, "Throttle PKG: %0X\n",
661 p->rapl_pkg_perf_status);
662 outp += sprintf(outp, "Throttle RAM: %0X\n",
663 p->rapl_dram_perf_status);
664 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500665
666 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
667 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
668 i, mp->msr_num, p->counter[i]);
669 }
Len Brownc98d5d92012-06-04 00:56:40 -0400670 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200671
672 outp += sprintf(outp, "\n");
673
Len Brownc98d5d92012-06-04 00:56:40 -0400674 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400675}
676
Len Browne23da032012-02-06 18:37:16 -0500677/*
678 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500679 */
Len Brownc98d5d92012-06-04 00:56:40 -0400680int format_counters(struct thread_data *t, struct core_data *c,
681 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400682{
683 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500684 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500685 int i;
686 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400687
Len Brownc98d5d92012-06-04 00:56:40 -0400688 /* if showing only 1st thread in core and this isn't one, bail out */
689 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
690 return 0;
691
692 /* if showing only 1st thread in pkg and this isn't one, bail out */
693 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
694 return 0;
695
Len Brown103a8fe2010-10-22 23:53:03 -0400696 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
697
Len Brownc98d5d92012-06-04 00:56:40 -0400698 /* topo columns, print blanks on 1st (average) line */
699 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500700 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400701 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500702 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400703 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500704 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400705 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400706 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500707 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400708 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400709 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400710 else
Len Brown3d109de2016-04-22 23:24:27 -0400711 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400712 }
Len Brown812db3f2017-02-10 00:25:41 -0500713 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400714 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400715 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400716 else
Len Brown3d109de2016-04-22 23:24:27 -0400717 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400718 }
Len Brown812db3f2017-02-10 00:25:41 -0500719 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400720 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400721 }
Len Brownfc04cc62014-02-06 00:55:19 -0500722
Len Brown812db3f2017-02-10 00:25:41 -0500723 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400724 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500725 1.0 / units * t->aperf / interval_float);
726
Len Brown812db3f2017-02-10 00:25:41 -0500727 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400728 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400729
Len Brown812db3f2017-02-10 00:25:41 -0500730 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400731 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400732 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400733 else
Len Brown3d109de2016-04-22 23:24:27 -0400734 outp += sprintf(outp, "\t%.0f",
Len Brown21ed5572015-10-19 22:37:40 -0400735 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
736 }
Len Brown103a8fe2010-10-22 23:53:03 -0400737
Len Brown812db3f2017-02-10 00:25:41 -0500738 if (DO_BIC(BIC_TSC_MHz))
739 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400740
Len Brown562a2d32016-02-26 23:48:05 -0500741 /* IRQ */
Len Brown812db3f2017-02-10 00:25:41 -0500742 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400743 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500744
Len Brown1cc21f72015-02-23 00:34:57 -0500745 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500746 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400747 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500748
Len Brown678a3bd2017-02-09 22:22:13 -0500749 /* C1 */
Len Brown812db3f2017-02-10 00:25:41 -0500750 if (DO_BIC(BIC_CPU_c1))
Len Brown3d109de2016-04-22 23:24:27 -0400751 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400752
Len Brown678a3bd2017-02-09 22:22:13 -0500753 /* Added counters */
754 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
755 if (mp->format == FORMAT_RAW) {
756 if (mp->width == 32)
757 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
758 else
759 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
760 } else if (mp->format == FORMAT_DELTA) {
761 outp += sprintf(outp, "\t%lld", t->counter[i]);
762 } else if (mp->format == FORMAT_PERCENT) {
763 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/t->tsc);
764 }
765 }
766
Len Brownc98d5d92012-06-04 00:56:40 -0400767 /* print per-core data only for 1st thread in core */
768 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
769 goto done;
770
Len Brown812db3f2017-02-10 00:25:41 -0500771 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400772 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500773 if (DO_BIC(BIC_CPU_c6))
Len Brown3d109de2016-04-22 23:24:27 -0400774 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500775 if (DO_BIC(BIC_CPU_c7))
Len Brown3d109de2016-04-22 23:24:27 -0400776 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400777
Len Brown0539ba112017-02-10 00:27:20 -0500778 /* Mod%c6 */
779 if (DO_BIC(BIC_Mod_c6))
780 outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / t->tsc);
781
Len Brown812db3f2017-02-10 00:25:41 -0500782 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400783 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500784
Len Brown388e9c82016-12-22 23:57:55 -0500785 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
786 if (mp->format == FORMAT_RAW) {
787 if (mp->width == 32)
788 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
789 else
790 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
791 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500792 outp += sprintf(outp, "\t%lld", c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500793 } else if (mp->format == FORMAT_PERCENT) {
794 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/t->tsc);
795 }
796 }
797
Len Brownc98d5d92012-06-04 00:56:40 -0400798 /* print per-package data only for 1st core in package */
799 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
800 goto done;
801
Len Brown0b2bb692015-03-26 00:50:30 -0400802 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500803 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400804 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500805
Len Brownfdf676e2016-02-27 01:28:12 -0500806 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500807 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400808 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400809 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400810 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400811 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400812 p->gfx_rc6_ms / 10.0 / interval_float);
813 }
814 }
Len Brownfdf676e2016-02-27 01:28:12 -0500815
Len Brown27d47352016-02-27 00:37:54 -0500816 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500817 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400818 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500819
Len Brown0b2bb692015-03-26 00:50:30 -0400820 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
821 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400822 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
823 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
824 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
825 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400826 }
827
Len Brownee7e38e2015-02-09 23:39:45 -0500828 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400829 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500830 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400831 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500832 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400833 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500834 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400835 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800836 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400837 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
838 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
839 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800840 }
Len Brown889facb2012-11-08 00:48:57 -0500841
842 /*
843 * If measurement interval exceeds minimum RAPL Joule Counter range,
844 * indicate that results are suspect by printing "**" in fraction place.
845 */
Len Brownfc04cc62014-02-06 00:55:19 -0500846 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400847 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500848 else
Len Browne975db52016-04-06 23:56:02 -0400849 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500850
Len Brown812db3f2017-02-10 00:25:41 -0500851 if (DO_BIC(BIC_PkgWatt))
852 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
853 if (DO_BIC(BIC_CorWatt))
854 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
855 if (DO_BIC(BIC_GFXWatt))
856 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
857 if (DO_BIC(BIC_RAMWatt))
858 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
859 if (DO_BIC(BIC_Pkg_J))
860 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
861 if (DO_BIC(BIC_Cor_J))
862 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
863 if (DO_BIC(BIC_GFX_J))
864 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
865 if (DO_BIC(BIC_RAM_J))
866 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
867 if (DO_BIC(BIC_PKG__))
868 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
869 if (DO_BIC(BIC_RAM__))
870 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
871
Len Brown388e9c82016-12-22 23:57:55 -0500872 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
873 if (mp->format == FORMAT_RAW) {
874 if (mp->width == 32)
875 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
876 else
877 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
878 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500879 outp += sprintf(outp, "\t%lld", p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500880 } else if (mp->format == FORMAT_PERCENT) {
881 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/t->tsc);
882 }
883 }
884
Len Brownc98d5d92012-06-04 00:56:40 -0400885done:
Len Brownc98d5d92012-06-04 00:56:40 -0400886 outp += sprintf(outp, "\n");
887
888 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400889}
890
Len Brownb7d8c142016-02-13 23:36:17 -0500891void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400892{
Len Brownb7d8c142016-02-13 23:36:17 -0500893 FILE *filep;
894
895 if (outf == stderr)
896 filep = stdout;
897 else
898 filep = outf;
899
900 fputs(output_buffer, filep);
901 fflush(filep);
902
Len Brownc98d5d92012-06-04 00:56:40 -0400903 outp = output_buffer;
904}
Len Brownb7d8c142016-02-13 23:36:17 -0500905void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400906{
Len Brownb7d8c142016-02-13 23:36:17 -0500907 fputs(output_buffer, outf);
908 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400909 outp = output_buffer;
910}
911void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
912{
Len Browne23da032012-02-06 18:37:16 -0500913 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400914
Len Browne23da032012-02-06 18:37:16 -0500915 if (!printed || !summary_only)
916 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400917
Len Brownc98d5d92012-06-04 00:56:40 -0400918 if (topo.num_cpus > 1)
919 format_counters(&average.threads, &average.cores,
920 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400921
Len Browne23da032012-02-06 18:37:16 -0500922 printed = 1;
923
924 if (summary_only)
925 return;
926
Len Brownc98d5d92012-06-04 00:56:40 -0400927 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400928}
929
Len Brown889facb2012-11-08 00:48:57 -0500930#define DELTA_WRAP32(new, old) \
931 if (new > old) { \
932 old = new - old; \
933 } else { \
934 old = 0x100000000 + new - old; \
935 }
936
Len Brownba3dec92016-04-22 20:31:46 -0400937int
Len Brownc98d5d92012-06-04 00:56:40 -0400938delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400939{
Len Brown388e9c82016-12-22 23:57:55 -0500940 int i;
941 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400942
943 if (do_skl_residency) {
944 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
945 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
946 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
947 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
948 }
Len Brownc98d5d92012-06-04 00:56:40 -0400949 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500950 if (do_pc3)
951 old->pc3 = new->pc3 - old->pc3;
952 if (do_pc6)
953 old->pc6 = new->pc6 - old->pc6;
954 if (do_pc7)
955 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800956 old->pc8 = new->pc8 - old->pc8;
957 old->pc9 = new->pc9 - old->pc9;
958 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500959 old->pkg_temp_c = new->pkg_temp_c;
960
Len Brown9185e982016-04-06 17:16:00 -0400961 /* flag an error when rc6 counter resets/wraps */
962 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
963 old->gfx_rc6_ms = -1;
964 else
965 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
966
Len Brown27d47352016-02-27 00:37:54 -0500967 old->gfx_mhz = new->gfx_mhz;
968
Len Brown889facb2012-11-08 00:48:57 -0500969 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
970 DELTA_WRAP32(new->energy_cores, old->energy_cores);
971 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
972 DELTA_WRAP32(new->energy_dram, old->energy_dram);
973 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
974 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400975
Len Brown388e9c82016-12-22 23:57:55 -0500976 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
977 if (mp->format == FORMAT_RAW)
978 old->counter[i] = new->counter[i];
979 else
980 old->counter[i] = new->counter[i] - old->counter[i];
981 }
982
Len Brownba3dec92016-04-22 20:31:46 -0400983 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400984}
Len Brown103a8fe2010-10-22 23:53:03 -0400985
Len Brownc98d5d92012-06-04 00:56:40 -0400986void
987delta_core(struct core_data *new, struct core_data *old)
988{
Len Brown388e9c82016-12-22 23:57:55 -0500989 int i;
990 struct msr_counter *mp;
991
Len Brownc98d5d92012-06-04 00:56:40 -0400992 old->c3 = new->c3 - old->c3;
993 old->c6 = new->c6 - old->c6;
994 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500995 old->core_temp_c = new->core_temp_c;
Len Brown0539ba112017-02-10 00:27:20 -0500996 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -0500997
998 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
999 if (mp->format == FORMAT_RAW)
1000 old->counter[i] = new->counter[i];
1001 else
1002 old->counter[i] = new->counter[i] - old->counter[i];
1003 }
Len Brownc98d5d92012-06-04 00:56:40 -04001004}
Len Brown103a8fe2010-10-22 23:53:03 -04001005
Len Brownc3ae3312012-06-13 21:31:46 -04001006/*
1007 * old = new - old
1008 */
Len Brownba3dec92016-04-22 20:31:46 -04001009int
Len Brownc98d5d92012-06-04 00:56:40 -04001010delta_thread(struct thread_data *new, struct thread_data *old,
1011 struct core_data *core_delta)
1012{
Len Brown388e9c82016-12-22 23:57:55 -05001013 int i;
1014 struct msr_counter *mp;
1015
Len Brownc98d5d92012-06-04 00:56:40 -04001016 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001017
Len Brownc98d5d92012-06-04 00:56:40 -04001018 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001019 if (old->tsc < (1000 * 1000))
1020 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1021 "You can disable all c-states by booting with \"idle=poll\"\n"
1022 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001023
Len Brownc98d5d92012-06-04 00:56:40 -04001024 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001025
Len Brown812db3f2017-02-10 00:25:41 -05001026 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001027 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1028 old->aperf = new->aperf - old->aperf;
1029 old->mperf = new->mperf - old->mperf;
1030 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001031 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001032 }
Len Brownc98d5d92012-06-04 00:56:40 -04001033 }
Len Brown103a8fe2010-10-22 23:53:03 -04001034
Len Brown103a8fe2010-10-22 23:53:03 -04001035
Len Brown144b44b2013-11-09 00:30:16 -05001036 if (use_c1_residency_msr) {
1037 /*
1038 * Some models have a dedicated C1 residency MSR,
1039 * which should be more accurate than the derivation below.
1040 */
1041 } else {
1042 /*
1043 * As counter collection is not atomic,
1044 * it is possible for mperf's non-halted cycles + idle states
1045 * to exceed TSC's all cycles: show c1 = 0% in that case.
1046 */
1047 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1048 old->c1 = 0;
1049 else {
1050 /* normal case, derive c1 */
1051 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001052 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001053 }
Len Brownc98d5d92012-06-04 00:56:40 -04001054 }
Len Brownc3ae3312012-06-13 21:31:46 -04001055
Len Brownc98d5d92012-06-04 00:56:40 -04001056 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001057 if (debug > 1)
1058 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001059 old->mperf = 1; /* divide by 0 protection */
1060 }
1061
Len Brown812db3f2017-02-10 00:25:41 -05001062 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001063 old->irq_count = new->irq_count - old->irq_count;
1064
Len Brown812db3f2017-02-10 00:25:41 -05001065 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001066 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001067
Len Brown388e9c82016-12-22 23:57:55 -05001068 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1069 if (mp->format == FORMAT_RAW)
1070 old->counter[i] = new->counter[i];
1071 else
1072 old->counter[i] = new->counter[i] - old->counter[i];
1073 }
Len Brownba3dec92016-04-22 20:31:46 -04001074 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001075}
1076
1077int delta_cpu(struct thread_data *t, struct core_data *c,
1078 struct pkg_data *p, struct thread_data *t2,
1079 struct core_data *c2, struct pkg_data *p2)
1080{
Len Brownba3dec92016-04-22 20:31:46 -04001081 int retval = 0;
1082
Len Brownc98d5d92012-06-04 00:56:40 -04001083 /* calculate core delta only for 1st thread in core */
1084 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1085 delta_core(c, c2);
1086
1087 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001088 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1089 if (retval)
1090 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001091
1092 /* calculate package delta only for 1st core in package */
1093 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001094 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001095
Len Brownba3dec92016-04-22 20:31:46 -04001096 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001097}
1098
1099void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1100{
Len Brown388e9c82016-12-22 23:57:55 -05001101 int i;
1102 struct msr_counter *mp;
1103
Len Brownc98d5d92012-06-04 00:56:40 -04001104 t->tsc = 0;
1105 t->aperf = 0;
1106 t->mperf = 0;
1107 t->c1 = 0;
1108
Len Brown562a2d32016-02-26 23:48:05 -05001109 t->irq_count = 0;
1110 t->smi_count = 0;
1111
Len Brownc98d5d92012-06-04 00:56:40 -04001112 /* tells format_counters to dump all fields from this set */
1113 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1114
1115 c->c3 = 0;
1116 c->c6 = 0;
1117 c->c7 = 0;
Len Brown0539ba112017-02-10 00:27:20 -05001118 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001119 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001120
Len Brown0b2bb692015-03-26 00:50:30 -04001121 p->pkg_wtd_core_c0 = 0;
1122 p->pkg_any_core_c0 = 0;
1123 p->pkg_any_gfxe_c0 = 0;
1124 p->pkg_both_core_gfxe_c0 = 0;
1125
Len Brownc98d5d92012-06-04 00:56:40 -04001126 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -05001127 if (do_pc3)
1128 p->pc3 = 0;
1129 if (do_pc6)
1130 p->pc6 = 0;
1131 if (do_pc7)
1132 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001133 p->pc8 = 0;
1134 p->pc9 = 0;
1135 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001136
1137 p->energy_pkg = 0;
1138 p->energy_dram = 0;
1139 p->energy_cores = 0;
1140 p->energy_gfx = 0;
1141 p->rapl_pkg_perf_status = 0;
1142 p->rapl_dram_perf_status = 0;
1143 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001144
Len Brownfdf676e2016-02-27 01:28:12 -05001145 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001146 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001147 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1148 t->counter[i] = 0;
1149
1150 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1151 c->counter[i] = 0;
1152
1153 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1154 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001155}
1156int sum_counters(struct thread_data *t, struct core_data *c,
1157 struct pkg_data *p)
1158{
Len Brown388e9c82016-12-22 23:57:55 -05001159 int i;
1160 struct msr_counter *mp;
1161
Len Brownc98d5d92012-06-04 00:56:40 -04001162 average.threads.tsc += t->tsc;
1163 average.threads.aperf += t->aperf;
1164 average.threads.mperf += t->mperf;
1165 average.threads.c1 += t->c1;
1166
Len Brown562a2d32016-02-26 23:48:05 -05001167 average.threads.irq_count += t->irq_count;
1168 average.threads.smi_count += t->smi_count;
1169
Len Brown388e9c82016-12-22 23:57:55 -05001170 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1171 if (mp->format == FORMAT_RAW)
1172 continue;
1173 average.threads.counter[i] += t->counter[i];
1174 }
1175
Len Brownc98d5d92012-06-04 00:56:40 -04001176 /* sum per-core values only for 1st thread in core */
1177 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1178 return 0;
1179
1180 average.cores.c3 += c->c3;
1181 average.cores.c6 += c->c6;
1182 average.cores.c7 += c->c7;
Len Brown0539ba112017-02-10 00:27:20 -05001183 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001184
Len Brown889facb2012-11-08 00:48:57 -05001185 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1186
Len Brown388e9c82016-12-22 23:57:55 -05001187 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1188 if (mp->format == FORMAT_RAW)
1189 continue;
1190 average.cores.counter[i] += c->counter[i];
1191 }
1192
Len Brownc98d5d92012-06-04 00:56:40 -04001193 /* sum per-pkg values only for 1st core in pkg */
1194 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1195 return 0;
1196
Len Brown0b2bb692015-03-26 00:50:30 -04001197 if (do_skl_residency) {
1198 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1199 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1200 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1201 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1202 }
1203
Len Brownc98d5d92012-06-04 00:56:40 -04001204 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -05001205 if (do_pc3)
1206 average.packages.pc3 += p->pc3;
1207 if (do_pc6)
1208 average.packages.pc6 += p->pc6;
1209 if (do_pc7)
1210 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001211 average.packages.pc8 += p->pc8;
1212 average.packages.pc9 += p->pc9;
1213 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001214
Len Brown889facb2012-11-08 00:48:57 -05001215 average.packages.energy_pkg += p->energy_pkg;
1216 average.packages.energy_dram += p->energy_dram;
1217 average.packages.energy_cores += p->energy_cores;
1218 average.packages.energy_gfx += p->energy_gfx;
1219
Len Brownfdf676e2016-02-27 01:28:12 -05001220 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001221 average.packages.gfx_mhz = p->gfx_mhz;
1222
Len Brown889facb2012-11-08 00:48:57 -05001223 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1224
1225 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1226 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001227
1228 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1229 if (mp->format == FORMAT_RAW)
1230 continue;
1231 average.packages.counter[i] += p->counter[i];
1232 }
Len Brownc98d5d92012-06-04 00:56:40 -04001233 return 0;
1234}
1235/*
1236 * sum the counters for all cpus in the system
1237 * compute the weighted average
1238 */
1239void compute_average(struct thread_data *t, struct core_data *c,
1240 struct pkg_data *p)
1241{
Len Brown388e9c82016-12-22 23:57:55 -05001242 int i;
1243 struct msr_counter *mp;
1244
Len Brownc98d5d92012-06-04 00:56:40 -04001245 clear_counters(&average.threads, &average.cores, &average.packages);
1246
1247 for_all_cpus(sum_counters, t, c, p);
1248
1249 average.threads.tsc /= topo.num_cpus;
1250 average.threads.aperf /= topo.num_cpus;
1251 average.threads.mperf /= topo.num_cpus;
1252 average.threads.c1 /= topo.num_cpus;
1253
1254 average.cores.c3 /= topo.num_cores;
1255 average.cores.c6 /= topo.num_cores;
1256 average.cores.c7 /= topo.num_cores;
Len Brown0539ba112017-02-10 00:27:20 -05001257 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001258
Len Brown0b2bb692015-03-26 00:50:30 -04001259 if (do_skl_residency) {
1260 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1261 average.packages.pkg_any_core_c0 /= topo.num_packages;
1262 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1263 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1264 }
1265
Len Brownc98d5d92012-06-04 00:56:40 -04001266 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -05001267 if (do_pc3)
1268 average.packages.pc3 /= topo.num_packages;
1269 if (do_pc6)
1270 average.packages.pc6 /= topo.num_packages;
1271 if (do_pc7)
1272 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001273
1274 average.packages.pc8 /= topo.num_packages;
1275 average.packages.pc9 /= topo.num_packages;
1276 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001277
1278 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1279 if (mp->format == FORMAT_RAW)
1280 continue;
1281 average.threads.counter[i] /= topo.num_cpus;
1282 }
1283 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1284 if (mp->format == FORMAT_RAW)
1285 continue;
1286 average.cores.counter[i] /= topo.num_cores;
1287 }
1288 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1289 if (mp->format == FORMAT_RAW)
1290 continue;
1291 average.packages.counter[i] /= topo.num_packages;
1292 }
Len Brownc98d5d92012-06-04 00:56:40 -04001293}
1294
1295static unsigned long long rdtsc(void)
1296{
1297 unsigned int low, high;
1298
1299 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1300
1301 return low | ((unsigned long long)high) << 32;
1302}
1303
Len Brownc98d5d92012-06-04 00:56:40 -04001304/*
1305 * get_counters(...)
1306 * migrate to cpu
1307 * acquire and record local counters for that cpu
1308 */
1309int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1310{
1311 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001312 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001313 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001314 struct msr_counter *mp;
1315 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001316
Len Browne52966c2012-11-08 22:38:05 -05001317 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001318 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001319 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001320 }
Len Brownc98d5d92012-06-04 00:56:40 -04001321
Len Brown0102b062016-02-27 03:11:29 -05001322retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001323 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1324
Len Brown812db3f2017-02-10 00:25:41 -05001325 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001326 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1327
1328 /*
1329 * The TSC, APERF and MPERF must be read together for
1330 * APERF/MPERF and MPERF/TSC to give accurate results.
1331 *
1332 * Unfortunately, APERF and MPERF are read by
1333 * individual system call, so delays may occur
1334 * between them. If the time to read them
1335 * varies by a large amount, we re-read them.
1336 */
1337
1338 /*
1339 * This initial dummy APERF read has been seen to
1340 * reduce jitter in the subsequent reads.
1341 */
1342
Len Brown9c63a652012-10-31 01:29:52 -04001343 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001344 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001345
1346 t->tsc = rdtsc(); /* re-read close to APERF */
1347
1348 tsc_before = t->tsc;
1349
1350 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1351 return -3;
1352
1353 tsc_between = rdtsc();
1354
Len Brown9c63a652012-10-31 01:29:52 -04001355 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001356 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001357
1358 tsc_after = rdtsc();
1359
1360 aperf_time = tsc_between - tsc_before;
1361 mperf_time = tsc_after - tsc_between;
1362
1363 /*
1364 * If the system call latency to read APERF and MPERF
1365 * differ by more than 2x, then try again.
1366 */
1367 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1368 aperf_mperf_retry_count++;
1369 if (aperf_mperf_retry_count < 5)
1370 goto retry;
1371 else
1372 warnx("cpu%d jitter %lld %lld",
1373 cpu, aperf_time, mperf_time);
1374 }
1375 aperf_mperf_retry_count = 0;
1376
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001377 t->aperf = t->aperf * aperf_mperf_multiplier;
1378 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001379 }
1380
Len Brown812db3f2017-02-10 00:25:41 -05001381 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001382 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001383 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001384 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1385 return -5;
1386 t->smi_count = msr & 0xFFFFFFFF;
1387 }
Len Brown0539ba112017-02-10 00:27:20 -05001388 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001389 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1390 return -6;
1391 }
1392
Len Brown388e9c82016-12-22 23:57:55 -05001393 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1394 if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1395 return -10;
1396 }
1397
1398
Len Brownc98d5d92012-06-04 00:56:40 -04001399 /* collect core counters only for 1st thread in core */
1400 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1401 return 0;
1402
Len Brown812db3f2017-02-10 00:25:41 -05001403 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001404 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1405 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001406 }
1407
Len Brown812db3f2017-02-10 00:25:41 -05001408 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001409 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1410 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001411 } else if (do_knl_cstates) {
1412 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1413 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001414 }
1415
Len Brown812db3f2017-02-10 00:25:41 -05001416 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001417 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1418 return -8;
1419
Len Brown0539ba112017-02-10 00:27:20 -05001420 if (DO_BIC(BIC_Mod_c6))
1421 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1422 return -8;
1423
Len Brown812db3f2017-02-10 00:25:41 -05001424 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001425 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1426 return -9;
1427 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1428 }
1429
Len Brown388e9c82016-12-22 23:57:55 -05001430 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1431 if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1432 return -10;
1433 }
Len Brown889facb2012-11-08 00:48:57 -05001434
Len Brownc98d5d92012-06-04 00:56:40 -04001435 /* collect package counters only for 1st core in package */
1436 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1437 return 0;
1438
Len Brown0b2bb692015-03-26 00:50:30 -04001439 if (do_skl_residency) {
1440 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1441 return -10;
1442 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1443 return -11;
1444 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1445 return -12;
1446 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1447 return -13;
1448 }
Len Brownee7e38e2015-02-09 23:39:45 -05001449 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001450 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1451 return -9;
Len Brown0539ba112017-02-10 00:27:20 -05001452 if (do_pc6) {
1453 if (do_slm_cstates) {
1454 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1455 return -10;
1456 } else {
1457 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1458 return -10;
1459 }
1460 }
1461
Len Brownee7e38e2015-02-09 23:39:45 -05001462 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001463 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1464 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001465 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001466 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1467 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001468 if (do_c8_c9_c10) {
1469 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1470 return -13;
1471 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1472 return -13;
1473 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1474 return -13;
1475 }
Len Brown889facb2012-11-08 00:48:57 -05001476 if (do_rapl & RAPL_PKG) {
1477 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1478 return -13;
1479 p->energy_pkg = msr & 0xFFFFFFFF;
1480 }
Jacob Pan91484942016-06-16 09:48:20 -07001481 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001482 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1483 return -14;
1484 p->energy_cores = msr & 0xFFFFFFFF;
1485 }
1486 if (do_rapl & RAPL_DRAM) {
1487 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1488 return -15;
1489 p->energy_dram = msr & 0xFFFFFFFF;
1490 }
1491 if (do_rapl & RAPL_GFX) {
1492 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1493 return -16;
1494 p->energy_gfx = msr & 0xFFFFFFFF;
1495 }
1496 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1497 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1498 return -16;
1499 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1500 }
1501 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1502 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1503 return -16;
1504 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1505 }
Len Brown812db3f2017-02-10 00:25:41 -05001506 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001507 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1508 return -17;
1509 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1510 }
Len Brownfdf676e2016-02-27 01:28:12 -05001511
Len Brown812db3f2017-02-10 00:25:41 -05001512 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001513 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1514
Len Brown812db3f2017-02-10 00:25:41 -05001515 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001516 p->gfx_mhz = gfx_cur_mhz;
1517
Len Brown388e9c82016-12-22 23:57:55 -05001518 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1519 if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1520 return -10;
1521 }
1522
Len Brown103a8fe2010-10-22 23:53:03 -04001523 return 0;
1524}
1525
Len Brownee7e38e2015-02-09 23:39:45 -05001526/*
1527 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1528 * If you change the values, note they are used both in comparisons
1529 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1530 */
1531
1532#define PCLUKN 0 /* Unknown */
1533#define PCLRSV 1 /* Reserved */
1534#define PCL__0 2 /* PC0 */
1535#define PCL__1 3 /* PC1 */
1536#define PCL__2 4 /* PC2 */
1537#define PCL__3 5 /* PC3 */
1538#define PCL__4 6 /* PC4 */
1539#define PCL__6 7 /* PC6 */
1540#define PCL_6N 8 /* PC6 No Retention */
1541#define PCL_6R 9 /* PC6 Retention */
1542#define PCL__7 10 /* PC7 */
1543#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001544#define PCL__8 12 /* PC8 */
1545#define PCL__9 13 /* PC9 */
1546#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001547
1548int pkg_cstate_limit = PCLUKN;
1549char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001550 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001551
Len Browne9257f52015-04-01 21:02:57 -04001552int 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};
1553int 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};
1554int 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 -05001555int 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 -05001556int 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 -04001557int 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 -04001558int 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 -05001559int 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 -05001560
Len Browna2b7b742015-09-26 00:12:38 -04001561
1562static void
1563calculate_tsc_tweak()
1564{
Len Browna2b7b742015-09-26 00:12:38 -04001565 tsc_tweak = base_hz / tsc_hz;
1566}
1567
Len Brownfcd17212015-03-23 20:29:09 -04001568static void
1569dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001570{
1571 unsigned long long msr;
1572 unsigned int ratio;
1573
Len Brownec0adc52015-11-12 02:42:31 -05001574 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001575
Len Brownb7d8c142016-02-13 23:36:17 -05001576 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001577
Len Brown103a8fe2010-10-22 23:53:03 -04001578 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001579 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001580 ratio, bclk, ratio * bclk);
1581
1582 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001583 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001584 ratio, bclk, ratio * bclk);
1585
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001586 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001587 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001588 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001589
Len Brownfcd17212015-03-23 20:29:09 -04001590 return;
1591}
1592
1593static void
1594dump_hsw_turbo_ratio_limits(void)
1595{
1596 unsigned long long msr;
1597 unsigned int ratio;
1598
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001599 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001600
Len Brownb7d8c142016-02-13 23:36:17 -05001601 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001602
1603 ratio = (msr >> 8) & 0xFF;
1604 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001605 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001606 ratio, bclk, ratio * bclk);
1607
1608 ratio = (msr >> 0) & 0xFF;
1609 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001610 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001611 ratio, bclk, ratio * bclk);
1612 return;
1613}
1614
1615static void
1616dump_ivt_turbo_ratio_limits(void)
1617{
1618 unsigned long long msr;
1619 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001620
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001621 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001622
Len Brownb7d8c142016-02-13 23:36:17 -05001623 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001624
1625 ratio = (msr >> 56) & 0xFF;
1626 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001627 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001628 ratio, bclk, ratio * bclk);
1629
1630 ratio = (msr >> 48) & 0xFF;
1631 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001632 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001633 ratio, bclk, ratio * bclk);
1634
1635 ratio = (msr >> 40) & 0xFF;
1636 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001637 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001638 ratio, bclk, ratio * bclk);
1639
1640 ratio = (msr >> 32) & 0xFF;
1641 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001642 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001643 ratio, bclk, ratio * bclk);
1644
1645 ratio = (msr >> 24) & 0xFF;
1646 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001647 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001648 ratio, bclk, ratio * bclk);
1649
1650 ratio = (msr >> 16) & 0xFF;
1651 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001652 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001653 ratio, bclk, ratio * bclk);
1654
1655 ratio = (msr >> 8) & 0xFF;
1656 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001657 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001658 ratio, bclk, ratio * bclk);
1659
1660 ratio = (msr >> 0) & 0xFF;
1661 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001662 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001663 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001664 return;
1665}
Len Brown6574a5d2012-09-21 00:01:31 -04001666
Len Brownfcd17212015-03-23 20:29:09 -04001667static void
1668dump_nhm_turbo_ratio_limits(void)
1669{
1670 unsigned long long msr;
1671 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001672
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001673 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001674
Len Brownb7d8c142016-02-13 23:36:17 -05001675 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001676
1677 ratio = (msr >> 56) & 0xFF;
1678 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001679 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 8 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001680 ratio, bclk, ratio * bclk);
1681
1682 ratio = (msr >> 48) & 0xFF;
1683 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001684 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 7 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001685 ratio, bclk, ratio * bclk);
1686
1687 ratio = (msr >> 40) & 0xFF;
1688 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001689 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 6 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001690 ratio, bclk, ratio * bclk);
1691
1692 ratio = (msr >> 32) & 0xFF;
1693 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001694 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 5 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001695 ratio, bclk, ratio * bclk);
1696
Len Brown103a8fe2010-10-22 23:53:03 -04001697 ratio = (msr >> 24) & 0xFF;
1698 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001699 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001700 ratio, bclk, ratio * bclk);
1701
1702 ratio = (msr >> 16) & 0xFF;
1703 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001704 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001705 ratio, bclk, ratio * bclk);
1706
1707 ratio = (msr >> 8) & 0xFF;
1708 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001709 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001710 ratio, bclk, ratio * bclk);
1711
1712 ratio = (msr >> 0) & 0xFF;
1713 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001714 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001715 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001716 return;
1717}
Len Brown3a9a9412014-08-15 02:39:52 -04001718
Len Brownfcd17212015-03-23 20:29:09 -04001719static void
Len Brown0f7887c2017-01-12 23:49:18 -05001720dump_atom_turbo_ratio_limits(void)
1721{
1722 unsigned long long msr;
1723 unsigned int ratio;
1724
1725 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1726 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1727
1728 ratio = (msr >> 0) & 0x3F;
1729 if (ratio)
1730 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1731 ratio, bclk, ratio * bclk);
1732
1733 ratio = (msr >> 8) & 0x3F;
1734 if (ratio)
1735 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1736 ratio, bclk, ratio * bclk);
1737
1738 ratio = (msr >> 16) & 0x3F;
1739 if (ratio)
1740 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1741 ratio, bclk, ratio * bclk);
1742
1743 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1744 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1745
1746 ratio = (msr >> 24) & 0x3F;
1747 if (ratio)
1748 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1749 ratio, bclk, ratio * bclk);
1750
1751 ratio = (msr >> 16) & 0x3F;
1752 if (ratio)
1753 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1754 ratio, bclk, ratio * bclk);
1755
1756 ratio = (msr >> 8) & 0x3F;
1757 if (ratio)
1758 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1759 ratio, bclk, ratio * bclk);
1760
1761 ratio = (msr >> 0) & 0x3F;
1762 if (ratio)
1763 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1764 ratio, bclk, ratio * bclk);
1765}
1766
1767static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001768dump_knl_turbo_ratio_limits(void)
1769{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001770 const unsigned int buckets_no = 7;
1771
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001772 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001773 int delta_cores, delta_ratio;
1774 int i, b_nr;
1775 unsigned int cores[buckets_no];
1776 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001777
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001778 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001779
Len Brownb7d8c142016-02-13 23:36:17 -05001780 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001781 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001782
1783 /**
1784 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001785 * [0] -- Reserved
1786 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001787 * [15:8] -- Base value of freq ratio of bucket 1.
1788 * [20:16] -- +ve delta of number of active cores of bucket 2.
1789 * i.e. active cores of bucket 2 =
1790 * active cores of bucket 1 + delta
1791 * [23:21] -- Negative delta of freq ratio of bucket 2.
1792 * i.e. freq ratio of bucket 2 =
1793 * freq ratio of bucket 1 - delta
1794 * [28:24]-- +ve delta of number of active cores of bucket 3.
1795 * [31:29]-- -ve delta of freq ratio of bucket 3.
1796 * [36:32]-- +ve delta of number of active cores of bucket 4.
1797 * [39:37]-- -ve delta of freq ratio of bucket 4.
1798 * [44:40]-- +ve delta of number of active cores of bucket 5.
1799 * [47:45]-- -ve delta of freq ratio of bucket 5.
1800 * [52:48]-- +ve delta of number of active cores of bucket 6.
1801 * [55:53]-- -ve delta of freq ratio of bucket 6.
1802 * [60:56]-- +ve delta of number of active cores of bucket 7.
1803 * [63:61]-- -ve delta of freq ratio of bucket 7.
1804 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001805
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001806 b_nr = 0;
1807 cores[b_nr] = (msr & 0xFF) >> 1;
1808 ratio[b_nr] = (msr >> 8) & 0xFF;
1809
1810 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001811 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001812 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001813
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001814 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1815 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1816 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001817 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001818
1819 for (i = buckets_no - 1; i >= 0; i--)
1820 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001821 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05001822 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001823 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001824}
1825
1826static void
Len Brownfcd17212015-03-23 20:29:09 -04001827dump_nhm_cst_cfg(void)
1828{
1829 unsigned long long msr;
1830
Len Brown1df2e552017-01-07 23:24:57 -05001831 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001832
1833#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1834#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1835
Len Brown1df2e552017-01-07 23:24:57 -05001836 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001837
Len Brownb7d8c142016-02-13 23:36:17 -05001838 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001839 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1840 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1841 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1842 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1843 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001844 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001845 pkg_cstate_limit_strings[pkg_cstate_limit]);
1846 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001847}
1848
Len Brown6fb31432015-06-17 16:23:45 -04001849static void
1850dump_config_tdp(void)
1851{
1852 unsigned long long msr;
1853
1854 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001855 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001856 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001857
1858 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001859 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001860 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001861 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1862 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1863 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1864 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001865 }
Len Brownb7d8c142016-02-13 23:36:17 -05001866 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001867
1868 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001869 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001870 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001871 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1872 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1873 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1874 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001875 }
Len Brownb7d8c142016-02-13 23:36:17 -05001876 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001877
1878 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001879 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001880 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001881 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1882 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1883 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001884
Len Brown6fb31432015-06-17 16:23:45 -04001885 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001886 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001887 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001888 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1889 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001890}
Len Brown5a634262016-04-06 17:15:55 -04001891
1892unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1893
1894void print_irtl(void)
1895{
1896 unsigned long long msr;
1897
1898 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1899 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1900 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1901 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1902
1903 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
1904 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
1905 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1906 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1907
1908 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
1909 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
1910 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1911 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1912
1913 if (!do_irtl_hsw)
1914 return;
1915
1916 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
1917 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
1918 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1919 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1920
1921 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
1922 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
1923 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1924 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1925
1926 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
1927 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
1928 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1929 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1930
1931}
Len Brown36229892016-02-26 20:51:02 -05001932void free_fd_percpu(void)
1933{
1934 int i;
1935
Mika Westerberg01a67ad2016-04-22 11:13:23 +03001936 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05001937 if (fd_percpu[i] != 0)
1938 close(fd_percpu[i]);
1939 }
1940
1941 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04001942}
1943
Len Brownc98d5d92012-06-04 00:56:40 -04001944void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001945{
Len Brownc98d5d92012-06-04 00:56:40 -04001946 CPU_FREE(cpu_present_set);
1947 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05001948 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001949
Len Brownc98d5d92012-06-04 00:56:40 -04001950 CPU_FREE(cpu_affinity_set);
1951 cpu_affinity_set = NULL;
1952 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001953
Len Brownc98d5d92012-06-04 00:56:40 -04001954 free(thread_even);
1955 free(core_even);
1956 free(package_even);
1957
1958 thread_even = NULL;
1959 core_even = NULL;
1960 package_even = NULL;
1961
1962 free(thread_odd);
1963 free(core_odd);
1964 free(package_odd);
1965
1966 thread_odd = NULL;
1967 core_odd = NULL;
1968 package_odd = NULL;
1969
1970 free(output_buffer);
1971 output_buffer = NULL;
1972 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05001973
1974 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05001975
1976 free(irq_column_2_cpu);
1977 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001978}
1979
Len Brownc98d5d92012-06-04 00:56:40 -04001980/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001981 * Open a file, and exit on failure
1982 */
1983FILE *fopen_or_die(const char *path, const char *mode)
1984{
Len Brownb7d8c142016-02-13 23:36:17 -05001985 FILE *filep = fopen(path, mode);
Josh Triplettb2c95d92013-08-20 17:20:18 -07001986 if (!filep)
1987 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001988 return filep;
1989}
1990
1991/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001992 * Parse a file containing a single int.
1993 */
1994int parse_int_file(const char *fmt, ...)
1995{
1996 va_list args;
1997 char path[PATH_MAX];
1998 FILE *filep;
1999 int value;
2000
2001 va_start(args, fmt);
2002 vsnprintf(path, sizeof(path), fmt, args);
2003 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002004 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002005 if (fscanf(filep, "%d", &value) != 1)
2006 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002007 fclose(filep);
2008 return value;
2009}
2010
2011/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002012 * get_cpu_position_in_core(cpu)
2013 * return the position of the CPU among its HT siblings in the core
2014 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002015 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002016int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002017{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002018 char path[64];
2019 FILE *filep;
2020 int this_cpu;
2021 char character;
2022 int i;
2023
2024 sprintf(path,
2025 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2026 cpu);
2027 filep = fopen(path, "r");
2028 if (filep == NULL) {
2029 perror(path);
2030 exit(1);
2031 }
2032
2033 for (i = 0; i < topo.num_threads_per_core; i++) {
2034 fscanf(filep, "%d", &this_cpu);
2035 if (this_cpu == cpu) {
2036 fclose(filep);
2037 return i;
2038 }
2039
2040 /* Account for no separator after last thread*/
2041 if (i != (topo.num_threads_per_core - 1))
2042 fscanf(filep, "%c", &character);
2043 }
2044
2045 fclose(filep);
2046 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002047}
2048
Len Brownc98d5d92012-06-04 00:56:40 -04002049/*
2050 * cpu_is_first_core_in_package(cpu)
2051 * return 1 if given CPU is 1st core in package
2052 */
2053int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002054{
Josh Triplett95aebc42013-08-20 17:20:16 -07002055 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002056}
2057
2058int get_physical_package_id(int cpu)
2059{
Josh Triplett95aebc42013-08-20 17:20:16 -07002060 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002061}
2062
2063int get_core_id(int cpu)
2064{
Josh Triplett95aebc42013-08-20 17:20:16 -07002065 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002066}
2067
Len Brownc98d5d92012-06-04 00:56:40 -04002068int get_num_ht_siblings(int cpu)
2069{
2070 char path[80];
2071 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002072 int sib1;
2073 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002074 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002075 char str[100];
2076 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002077
2078 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002079 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002080
Len Brownc98d5d92012-06-04 00:56:40 -04002081 /*
2082 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002083 * A ',' separated or '-' separated set of numbers
2084 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002085 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002086 fscanf(filep, "%d%c\n", &sib1, &character);
2087 fseek(filep, 0, SEEK_SET);
2088 fgets(str, 100, filep);
2089 ch = strchr(str, character);
2090 while (ch != NULL) {
2091 matches++;
2092 ch = strchr(ch+1, character);
2093 }
Len Brownc98d5d92012-06-04 00:56:40 -04002094
2095 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002096 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002097}
2098
Len Brown103a8fe2010-10-22 23:53:03 -04002099/*
Len Brownc98d5d92012-06-04 00:56:40 -04002100 * run func(thread, core, package) in topology order
2101 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002102 */
2103
Len Brownc98d5d92012-06-04 00:56:40 -04002104int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2105 struct pkg_data *, struct thread_data *, struct core_data *,
2106 struct pkg_data *), struct thread_data *thread_base,
2107 struct core_data *core_base, struct pkg_data *pkg_base,
2108 struct thread_data *thread_base2, struct core_data *core_base2,
2109 struct pkg_data *pkg_base2)
2110{
2111 int retval, pkg_no, core_no, thread_no;
2112
2113 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2114 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2115 for (thread_no = 0; thread_no <
2116 topo.num_threads_per_core; ++thread_no) {
2117 struct thread_data *t, *t2;
2118 struct core_data *c, *c2;
2119 struct pkg_data *p, *p2;
2120
2121 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2122
2123 if (cpu_is_not_present(t->cpu_id))
2124 continue;
2125
2126 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2127
2128 c = GET_CORE(core_base, core_no, pkg_no);
2129 c2 = GET_CORE(core_base2, core_no, pkg_no);
2130
2131 p = GET_PKG(pkg_base, pkg_no);
2132 p2 = GET_PKG(pkg_base2, pkg_no);
2133
2134 retval = func(t, c, p, t2, c2, p2);
2135 if (retval)
2136 return retval;
2137 }
2138 }
2139 }
2140 return 0;
2141}
2142
2143/*
2144 * run func(cpu) on every cpu in /proc/stat
2145 * return max_cpu number
2146 */
2147int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002148{
2149 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002150 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002151 int retval;
2152
Josh Triplett57a42a32013-08-20 17:20:17 -07002153 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002154
2155 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002156 if (retval != 0)
2157 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002158
Len Brownc98d5d92012-06-04 00:56:40 -04002159 while (1) {
2160 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 -04002161 if (retval != 1)
2162 break;
2163
Len Brownc98d5d92012-06-04 00:56:40 -04002164 retval = func(cpu_num);
2165 if (retval) {
2166 fclose(fp);
2167 return(retval);
2168 }
Len Brown103a8fe2010-10-22 23:53:03 -04002169 }
2170 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002171 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002172}
2173
2174void re_initialize(void)
2175{
Len Brownc98d5d92012-06-04 00:56:40 -04002176 free_all_buffers();
2177 setup_all_buffers();
2178 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002179}
2180
Len Brownc98d5d92012-06-04 00:56:40 -04002181
Len Brown103a8fe2010-10-22 23:53:03 -04002182/*
Len Brownc98d5d92012-06-04 00:56:40 -04002183 * count_cpus()
2184 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002185 */
Len Brownc98d5d92012-06-04 00:56:40 -04002186int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002187{
Len Brownc98d5d92012-06-04 00:56:40 -04002188 if (topo.max_cpu_num < cpu)
2189 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002190
Len Brownc98d5d92012-06-04 00:56:40 -04002191 topo.num_cpus += 1;
2192 return 0;
2193}
2194int mark_cpu_present(int cpu)
2195{
2196 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002197 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002198}
2199
Len Brown562a2d32016-02-26 23:48:05 -05002200/*
2201 * snapshot_proc_interrupts()
2202 *
2203 * read and record summary of /proc/interrupts
2204 *
2205 * return 1 if config change requires a restart, else return 0
2206 */
2207int snapshot_proc_interrupts(void)
2208{
2209 static FILE *fp;
2210 int column, retval;
2211
2212 if (fp == NULL)
2213 fp = fopen_or_die("/proc/interrupts", "r");
2214 else
2215 rewind(fp);
2216
2217 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2218 for (column = 0; column < topo.num_cpus; ++column) {
2219 int cpu_number;
2220
2221 retval = fscanf(fp, " CPU%d", &cpu_number);
2222 if (retval != 1)
2223 break;
2224
2225 if (cpu_number > topo.max_cpu_num) {
2226 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2227 return 1;
2228 }
2229
2230 irq_column_2_cpu[column] = cpu_number;
2231 irqs_per_cpu[cpu_number] = 0;
2232 }
2233
2234 /* read /proc/interrupt count lines and sum up irqs per cpu */
2235 while (1) {
2236 int column;
2237 char buf[64];
2238
2239 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2240 if (retval != 1)
2241 break;
2242
2243 /* read the count per cpu */
2244 for (column = 0; column < topo.num_cpus; ++column) {
2245
2246 int cpu_number, irq_count;
2247
2248 retval = fscanf(fp, " %d", &irq_count);
2249 if (retval != 1)
2250 break;
2251
2252 cpu_number = irq_column_2_cpu[column];
2253 irqs_per_cpu[cpu_number] += irq_count;
2254
2255 }
2256
2257 while (getc(fp) != '\n')
2258 ; /* flush interrupt description */
2259
2260 }
2261 return 0;
2262}
Len Brown27d47352016-02-27 00:37:54 -05002263/*
Len Brownfdf676e2016-02-27 01:28:12 -05002264 * snapshot_gfx_rc6_ms()
2265 *
2266 * record snapshot of
2267 * /sys/class/drm/card0/power/rc6_residency_ms
2268 *
2269 * return 1 if config change requires a restart, else return 0
2270 */
2271int snapshot_gfx_rc6_ms(void)
2272{
2273 FILE *fp;
2274 int retval;
2275
2276 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2277
2278 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2279 if (retval != 1)
2280 err(1, "GFX rc6");
2281
2282 fclose(fp);
2283
2284 return 0;
2285}
2286/*
Len Brown27d47352016-02-27 00:37:54 -05002287 * snapshot_gfx_mhz()
2288 *
2289 * record snapshot of
2290 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2291 *
2292 * return 1 if config change requires a restart, else return 0
2293 */
2294int snapshot_gfx_mhz(void)
2295{
2296 static FILE *fp;
2297 int retval;
2298
2299 if (fp == NULL)
2300 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2301 else
2302 rewind(fp);
2303
2304 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2305 if (retval != 1)
2306 err(1, "GFX MHz");
2307
2308 return 0;
2309}
Len Brown562a2d32016-02-26 23:48:05 -05002310
2311/*
2312 * snapshot /proc and /sys files
2313 *
2314 * return 1 if configuration restart needed, else return 0
2315 */
2316int snapshot_proc_sysfs_files(void)
2317{
2318 if (snapshot_proc_interrupts())
2319 return 1;
2320
Len Brown812db3f2017-02-10 00:25:41 -05002321 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002322 snapshot_gfx_rc6_ms();
2323
Len Brown812db3f2017-02-10 00:25:41 -05002324 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002325 snapshot_gfx_mhz();
2326
Len Brown562a2d32016-02-26 23:48:05 -05002327 return 0;
2328}
2329
Len Brown103a8fe2010-10-22 23:53:03 -04002330void turbostat_loop()
2331{
Len Brownc98d5d92012-06-04 00:56:40 -04002332 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002333 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002334
Len Brown103a8fe2010-10-22 23:53:03 -04002335restart:
Len Browne52966c2012-11-08 22:38:05 -05002336 restarted++;
2337
Len Brown562a2d32016-02-26 23:48:05 -05002338 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002339 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002340 if (retval < -1) {
2341 exit(retval);
2342 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002343 if (restarted > 1) {
2344 exit(retval);
2345 }
Len Brownc98d5d92012-06-04 00:56:40 -04002346 re_initialize();
2347 goto restart;
2348 }
Len Browne52966c2012-11-08 22:38:05 -05002349 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002350 gettimeofday(&tv_even, (struct timezone *)NULL);
2351
2352 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002353 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002354 re_initialize();
2355 goto restart;
2356 }
Len Brown2a0609c2016-02-12 22:44:48 -05002357 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002358 if (snapshot_proc_sysfs_files())
2359 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002360 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002361 if (retval < -1) {
2362 exit(retval);
2363 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002364 re_initialize();
2365 goto restart;
2366 }
Len Brown103a8fe2010-10-22 23:53:03 -04002367 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002368 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002369 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2370 re_initialize();
2371 goto restart;
2372 }
Len Brownc98d5d92012-06-04 00:56:40 -04002373 compute_average(EVEN_COUNTERS);
2374 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002375 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002376 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002377 if (snapshot_proc_sysfs_files())
2378 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002379 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002380 if (retval < -1) {
2381 exit(retval);
2382 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002383 re_initialize();
2384 goto restart;
2385 }
Len Brown103a8fe2010-10-22 23:53:03 -04002386 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002387 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002388 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2389 re_initialize();
2390 goto restart;
2391 }
Len Brownc98d5d92012-06-04 00:56:40 -04002392 compute_average(ODD_COUNTERS);
2393 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002394 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002395 }
2396}
2397
2398void check_dev_msr()
2399{
2400 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002401 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002402
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002403 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2404 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002405 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2406 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002407}
2408
Len Brown98481e72014-08-15 00:36:50 -04002409void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002410{
Len Brown98481e72014-08-15 00:36:50 -04002411 struct __user_cap_header_struct cap_header_data;
2412 cap_user_header_t cap_header = &cap_header_data;
2413 struct __user_cap_data_struct cap_data_data;
2414 cap_user_data_t cap_data = &cap_data_data;
2415 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2416 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002417 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002418
2419 /* check for CAP_SYS_RAWIO */
2420 cap_header->pid = getpid();
2421 cap_header->version = _LINUX_CAPABILITY_VERSION;
2422 if (capget(cap_header, cap_data) < 0)
2423 err(-6, "capget(2) failed");
2424
2425 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2426 do_exit++;
2427 warnx("capget(CAP_SYS_RAWIO) failed,"
2428 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2429 }
2430
2431 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002432 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2433 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002434 do_exit++;
2435 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2436 }
2437
2438 /* if all else fails, thell them to be root */
2439 if (do_exit)
2440 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002441 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002442
2443 if (do_exit)
2444 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002445}
2446
Len Brownd7899442015-01-23 00:12:33 -05002447/*
2448 * NHM adds support for additional MSRs:
2449 *
2450 * MSR_SMI_COUNT 0x00000034
2451 *
Len Brownec0adc52015-11-12 02:42:31 -05002452 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002453 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002454 *
Len Browncf4cbe52017-01-01 13:08:33 -05002455 * MSR_MISC_PWR_MGMT 0x000001aa
2456 *
Len Brownd7899442015-01-23 00:12:33 -05002457 * MSR_PKG_C3_RESIDENCY 0x000003f8
2458 * MSR_PKG_C6_RESIDENCY 0x000003f9
2459 * MSR_CORE_C3_RESIDENCY 0x000003fc
2460 * MSR_CORE_C6_RESIDENCY 0x000003fd
2461 *
Len Brownee7e38e2015-02-09 23:39:45 -05002462 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002463 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002464 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002465 */
Len Brownee7e38e2015-02-09 23:39:45 -05002466int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002467{
Len Brownee7e38e2015-02-09 23:39:45 -05002468 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002469 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002470 int *pkg_cstate_limits;
2471
Len Brown103a8fe2010-10-22 23:53:03 -04002472 if (!genuine_intel)
2473 return 0;
2474
2475 if (family != 6)
2476 return 0;
2477
Len Brown21ed5572015-10-19 22:37:40 -04002478 bclk = discover_bclk(family, model);
2479
Len Brown103a8fe2010-10-22 23:53:03 -04002480 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002481 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2482 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002483 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce69e2016-06-16 23:22:37 -04002484 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2485 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2486 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2487 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002488 pkg_cstate_limits = nhm_pkg_cstate_limits;
2489 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002490 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2491 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2492 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2493 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002494 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002495 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002496 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002497 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2498 case INTEL_FAM6_HASWELL_X: /* HSX */
2499 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2500 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2501 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2502 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2503 case INTEL_FAM6_BROADWELL_X: /* BDX */
2504 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2505 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2506 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2507 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2508 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002509 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002510 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002511 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002512 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2513 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002514 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002515 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002516 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002517 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04002518 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002519 pkg_cstate_limits = slv_pkg_cstate_limits;
2520 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002521 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002522 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002523 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002524 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002525 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002526 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002527 pkg_cstate_limits = phi_pkg_cstate_limits;
2528 break;
Len Brown869ce69e2016-06-16 23:22:37 -04002529 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
2530 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002531 pkg_cstate_limits = bxt_pkg_cstate_limits;
2532 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002533 default:
2534 return 0;
2535 }
Len Brown1df2e552017-01-07 23:24:57 -05002536 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002537 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002538
Len Brownec0adc52015-11-12 02:42:31 -05002539 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002540 base_ratio = (msr >> 8) & 0xFF;
2541
2542 base_hz = base_ratio * bclk * 1000000;
2543 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002544 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002545}
Len Brown0f7887c2017-01-12 23:49:18 -05002546/*
2547 * SLV client has supporet for unique MSRs:
2548 *
2549 * MSR_CC6_DEMOTION_POLICY_CONFIG
2550 * MSR_MC6_DEMOTION_POLICY_CONFIG
2551 */
2552
2553int has_slv_msrs(unsigned int family, unsigned int model)
2554{
2555 if (!genuine_intel)
2556 return 0;
2557
2558 switch (model) {
2559 case INTEL_FAM6_ATOM_SILVERMONT1:
2560 case INTEL_FAM6_ATOM_MERRIFIELD:
2561 case INTEL_FAM6_ATOM_MOOREFIELD:
2562 return 1;
2563 }
2564 return 0;
2565}
2566
Len Brownd7899442015-01-23 00:12:33 -05002567int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2568{
Len Brown0f7887c2017-01-12 23:49:18 -05002569 if (has_slv_msrs(family, model))
2570 return 0;
2571
Len Brownd7899442015-01-23 00:12:33 -05002572 switch (model) {
2573 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce69e2016-06-16 23:22:37 -04002574 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2575 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2576 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002577 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002578 return 0;
2579 default:
2580 return 1;
2581 }
2582}
Len Brown0f7887c2017-01-12 23:49:18 -05002583int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2584{
2585 if (has_slv_msrs(family, model))
2586 return 1;
2587
2588 return 0;
2589}
Len Brown6574a5d2012-09-21 00:01:31 -04002590int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2591{
2592 if (!genuine_intel)
2593 return 0;
2594
2595 if (family != 6)
2596 return 0;
2597
2598 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002599 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2600 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002601 return 1;
2602 default:
2603 return 0;
2604 }
2605}
Len Brownfcd17212015-03-23 20:29:09 -04002606int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2607{
2608 if (!genuine_intel)
2609 return 0;
2610
2611 if (family != 6)
2612 return 0;
2613
2614 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002615 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002616 return 1;
2617 default:
2618 return 0;
2619 }
2620}
2621
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002622int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2623{
2624 if (!genuine_intel)
2625 return 0;
2626
2627 if (family != 6)
2628 return 0;
2629
2630 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002631 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002632 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002633 return 1;
2634 default:
2635 return 0;
2636 }
2637}
Len Brown6fb31432015-06-17 16:23:45 -04002638int has_config_tdp(unsigned int family, unsigned int model)
2639{
2640 if (!genuine_intel)
2641 return 0;
2642
2643 if (family != 6)
2644 return 0;
2645
2646 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002647 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2648 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2649 case INTEL_FAM6_HASWELL_X: /* HSX */
2650 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2651 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2652 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2653 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2654 case INTEL_FAM6_BROADWELL_X: /* BDX */
2655 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2656 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2657 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2658 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2659 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2660 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002661
Len Brown869ce69e2016-06-16 23:22:37 -04002662 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002663 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002664 return 1;
2665 default:
2666 return 0;
2667 }
2668}
2669
Len Brownfcd17212015-03-23 20:29:09 -04002670static void
Colin Ian King1b693172016-03-02 13:50:25 +00002671dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002672{
2673 if (!do_nhm_platform_info)
2674 return;
2675
2676 dump_nhm_platform_info();
2677
2678 if (has_hsw_turbo_ratio_limit(family, model))
2679 dump_hsw_turbo_ratio_limits();
2680
2681 if (has_ivt_turbo_ratio_limit(family, model))
2682 dump_ivt_turbo_ratio_limits();
2683
2684 if (has_nhm_turbo_ratio_limit(family, model))
2685 dump_nhm_turbo_ratio_limits();
2686
Len Brown0f7887c2017-01-12 23:49:18 -05002687 if (has_atom_turbo_ratio_limit(family, model))
2688 dump_atom_turbo_ratio_limits();
2689
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002690 if (has_knl_turbo_ratio_limit(family, model))
2691 dump_knl_turbo_ratio_limits();
2692
Len Brown6fb31432015-06-17 16:23:45 -04002693 if (has_config_tdp(family, model))
2694 dump_config_tdp();
2695
Len Brownfcd17212015-03-23 20:29:09 -04002696 dump_nhm_cst_cfg();
2697}
2698
Len Brown6574a5d2012-09-21 00:01:31 -04002699
Len Brown889facb2012-11-08 00:48:57 -05002700/*
2701 * print_epb()
2702 * Decode the ENERGY_PERF_BIAS MSR
2703 */
2704int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2705{
2706 unsigned long long msr;
2707 char *epb_string;
2708 int cpu;
2709
2710 if (!has_epb)
2711 return 0;
2712
2713 cpu = t->cpu_id;
2714
2715 /* EPB is per-package */
2716 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2717 return 0;
2718
2719 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002720 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002721 return -1;
2722 }
2723
2724 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2725 return 0;
2726
Len Browne9be7dd2015-05-26 12:19:37 -04002727 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002728 case ENERGY_PERF_BIAS_PERFORMANCE:
2729 epb_string = "performance";
2730 break;
2731 case ENERGY_PERF_BIAS_NORMAL:
2732 epb_string = "balanced";
2733 break;
2734 case ENERGY_PERF_BIAS_POWERSAVE:
2735 epb_string = "powersave";
2736 break;
2737 default:
2738 epb_string = "custom";
2739 break;
2740 }
Len Brownb7d8c142016-02-13 23:36:17 -05002741 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002742
2743 return 0;
2744}
Len Brown7f5c2582015-12-01 01:36:39 -05002745/*
2746 * print_hwp()
2747 * Decode the MSR_HWP_CAPABILITIES
2748 */
2749int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2750{
2751 unsigned long long msr;
2752 int cpu;
2753
2754 if (!has_hwp)
2755 return 0;
2756
2757 cpu = t->cpu_id;
2758
2759 /* MSR_HWP_CAPABILITIES is per-package */
2760 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2761 return 0;
2762
2763 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002764 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002765 return -1;
2766 }
2767
2768 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2769 return 0;
2770
Len Brownb7d8c142016-02-13 23:36:17 -05002771 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002772 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2773
2774 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2775 if ((msr & (1 << 0)) == 0)
2776 return 0;
2777
2778 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2779 return 0;
2780
Len Brownb7d8c142016-02-13 23:36:17 -05002781 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002782 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2783 cpu, msr,
2784 (unsigned int)HWP_HIGHEST_PERF(msr),
2785 (unsigned int)HWP_GUARANTEED_PERF(msr),
2786 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2787 (unsigned int)HWP_LOWEST_PERF(msr));
2788
2789 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2790 return 0;
2791
Len Brownb7d8c142016-02-13 23:36:17 -05002792 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002793 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2794 cpu, msr,
2795 (unsigned int)(((msr) >> 0) & 0xff),
2796 (unsigned int)(((msr) >> 8) & 0xff),
2797 (unsigned int)(((msr) >> 16) & 0xff),
2798 (unsigned int)(((msr) >> 24) & 0xff),
2799 (unsigned int)(((msr) >> 32) & 0xff3),
2800 (unsigned int)(((msr) >> 42) & 0x1));
2801
2802 if (has_hwp_pkg) {
2803 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2804 return 0;
2805
Len Brownb7d8c142016-02-13 23:36:17 -05002806 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002807 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2808 cpu, msr,
2809 (unsigned int)(((msr) >> 0) & 0xff),
2810 (unsigned int)(((msr) >> 8) & 0xff),
2811 (unsigned int)(((msr) >> 16) & 0xff),
2812 (unsigned int)(((msr) >> 24) & 0xff),
2813 (unsigned int)(((msr) >> 32) & 0xff3));
2814 }
2815 if (has_hwp_notify) {
2816 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2817 return 0;
2818
Len Brownb7d8c142016-02-13 23:36:17 -05002819 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002820 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2821 cpu, msr,
2822 ((msr) & 0x1) ? "EN" : "Dis",
2823 ((msr) & 0x2) ? "EN" : "Dis");
2824 }
2825 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2826 return 0;
2827
Len Brownb7d8c142016-02-13 23:36:17 -05002828 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002829 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2830 cpu, msr,
2831 ((msr) & 0x1) ? "" : "No-",
2832 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05002833
2834 return 0;
2835}
2836
Len Brown3a9a9412014-08-15 02:39:52 -04002837/*
2838 * print_perf_limit()
2839 */
2840int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2841{
2842 unsigned long long msr;
2843 int cpu;
2844
2845 cpu = t->cpu_id;
2846
2847 /* per-package */
2848 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2849 return 0;
2850
2851 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002852 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002853 return -1;
2854 }
2855
2856 if (do_core_perf_limit_reasons) {
2857 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002858 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2859 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002860 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002861 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002862 (msr & 1 << 13) ? "Transitions, " : "",
2863 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2864 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2865 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2866 (msr & 1 << 9) ? "CorePwr, " : "",
2867 (msr & 1 << 8) ? "Amps, " : "",
2868 (msr & 1 << 6) ? "VR-Therm, " : "",
2869 (msr & 1 << 5) ? "Auto-HWP, " : "",
2870 (msr & 1 << 4) ? "Graphics, " : "",
2871 (msr & 1 << 2) ? "bit2, " : "",
2872 (msr & 1 << 1) ? "ThermStatus, " : "",
2873 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002874 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 -04002875 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002876 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002877 (msr & 1 << 29) ? "Transitions, " : "",
2878 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2879 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2880 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2881 (msr & 1 << 25) ? "CorePwr, " : "",
2882 (msr & 1 << 24) ? "Amps, " : "",
2883 (msr & 1 << 22) ? "VR-Therm, " : "",
2884 (msr & 1 << 21) ? "Auto-HWP, " : "",
2885 (msr & 1 << 20) ? "Graphics, " : "",
2886 (msr & 1 << 18) ? "bit18, " : "",
2887 (msr & 1 << 17) ? "ThermStatus, " : "",
2888 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002889
2890 }
2891 if (do_gfx_perf_limit_reasons) {
2892 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002893 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2894 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002895 (msr & 1 << 0) ? "PROCHOT, " : "",
2896 (msr & 1 << 1) ? "ThermStatus, " : "",
2897 (msr & 1 << 4) ? "Graphics, " : "",
2898 (msr & 1 << 6) ? "VR-Therm, " : "",
2899 (msr & 1 << 8) ? "Amps, " : "",
2900 (msr & 1 << 9) ? "GFXPwr, " : "",
2901 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2902 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002903 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002904 (msr & 1 << 16) ? "PROCHOT, " : "",
2905 (msr & 1 << 17) ? "ThermStatus, " : "",
2906 (msr & 1 << 20) ? "Graphics, " : "",
2907 (msr & 1 << 22) ? "VR-Therm, " : "",
2908 (msr & 1 << 24) ? "Amps, " : "",
2909 (msr & 1 << 25) ? "GFXPwr, " : "",
2910 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2911 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2912 }
2913 if (do_ring_perf_limit_reasons) {
2914 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002915 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2916 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002917 (msr & 1 << 0) ? "PROCHOT, " : "",
2918 (msr & 1 << 1) ? "ThermStatus, " : "",
2919 (msr & 1 << 6) ? "VR-Therm, " : "",
2920 (msr & 1 << 8) ? "Amps, " : "",
2921 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2922 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002923 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002924 (msr & 1 << 16) ? "PROCHOT, " : "",
2925 (msr & 1 << 17) ? "ThermStatus, " : "",
2926 (msr & 1 << 22) ? "VR-Therm, " : "",
2927 (msr & 1 << 24) ? "Amps, " : "",
2928 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2929 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2930 }
2931 return 0;
2932}
2933
Len Brown889facb2012-11-08 00:48:57 -05002934#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2935#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2936
Colin Ian King1b693172016-03-02 13:50:25 +00002937double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05002938{
2939 unsigned long long msr;
2940
2941 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002942 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002943 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2944
2945 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002946 case INTEL_FAM6_ATOM_SILVERMONT1:
2947 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05002948 return 30.0;
2949 default:
2950 return 135.0;
2951 }
2952}
2953
Andrey Semin40ee8e32014-12-05 00:07:00 -05002954/*
2955 * rapl_dram_energy_units_probe()
2956 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2957 */
2958static double
2959rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2960{
2961 /* only called for genuine_intel, family 6 */
2962
2963 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002964 case INTEL_FAM6_HASWELL_X: /* HSX */
2965 case INTEL_FAM6_BROADWELL_X: /* BDX */
2966 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2967 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05002968 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05002969 return (rapl_dram_energy_units = 15.3 / 1000000);
2970 default:
2971 return (rapl_energy_units);
2972 }
2973}
2974
Len Brown144b44b2013-11-09 00:30:16 -05002975
Len Brown889facb2012-11-08 00:48:57 -05002976/*
2977 * rapl_probe()
2978 *
Len Brown144b44b2013-11-09 00:30:16 -05002979 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002980 */
2981void rapl_probe(unsigned int family, unsigned int model)
2982{
2983 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002984 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002985 double tdp;
2986
2987 if (!genuine_intel)
2988 return;
2989
2990 if (family != 6)
2991 return;
2992
2993 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04002994 case INTEL_FAM6_SANDYBRIDGE:
2995 case INTEL_FAM6_IVYBRIDGE:
2996 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2997 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2998 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2999 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3000 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003001 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003002 if (rapl_joules) {
3003 BIC_PRESENT(BIC_Pkg_J);
3004 BIC_PRESENT(BIC_Cor_J);
3005 BIC_PRESENT(BIC_GFX_J);
3006 } else {
3007 BIC_PRESENT(BIC_PkgWatt);
3008 BIC_PRESENT(BIC_CorWatt);
3009 BIC_PRESENT(BIC_GFXWatt);
3010 }
Len Brown889facb2012-11-08 00:48:57 -05003011 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003012 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne4085d52016-04-06 17:15:56 -04003013 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003014 if (rapl_joules)
3015 BIC_PRESENT(BIC_Pkg_J);
3016 else
3017 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003018 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003019 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3020 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3021 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3022 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003023 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003024 BIC_PRESENT(BIC_PKG__);
3025 BIC_PRESENT(BIC_RAM__);
3026 if (rapl_joules) {
3027 BIC_PRESENT(BIC_Pkg_J);
3028 BIC_PRESENT(BIC_Cor_J);
3029 BIC_PRESENT(BIC_RAM_J);
3030 } else {
3031 BIC_PRESENT(BIC_PkgWatt);
3032 BIC_PRESENT(BIC_CorWatt);
3033 BIC_PRESENT(BIC_RAMWatt);
3034 }
Len Brown0b2bb692015-03-26 00:50:30 -04003035 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003036 case INTEL_FAM6_HASWELL_X: /* HSX */
3037 case INTEL_FAM6_BROADWELL_X: /* BDX */
3038 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3039 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3040 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003041 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003042 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 -05003043 BIC_PRESENT(BIC_PKG__);
3044 BIC_PRESENT(BIC_RAM__);
3045 if (rapl_joules) {
3046 BIC_PRESENT(BIC_Pkg_J);
3047 BIC_PRESENT(BIC_RAM_J);
3048 } else {
3049 BIC_PRESENT(BIC_PkgWatt);
3050 BIC_PRESENT(BIC_RAMWatt);
3051 }
Len Browne6f9bb32013-12-03 02:19:19 -05003052 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003053 case INTEL_FAM6_SANDYBRIDGE_X:
3054 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003055 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 -05003056 BIC_PRESENT(BIC_PKG__);
3057 BIC_PRESENT(BIC_RAM__);
3058 if (rapl_joules) {
3059 BIC_PRESENT(BIC_Pkg_J);
3060 BIC_PRESENT(BIC_Cor_J);
3061 BIC_PRESENT(BIC_RAM_J);
3062 } else {
3063 BIC_PRESENT(BIC_PkgWatt);
3064 BIC_PRESENT(BIC_CorWatt);
3065 BIC_PRESENT(BIC_RAMWatt);
3066 }
Len Brown144b44b2013-11-09 00:30:16 -05003067 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003068 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3069 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003070 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003071 if (rapl_joules) {
3072 BIC_PRESENT(BIC_Pkg_J);
3073 BIC_PRESENT(BIC_Cor_J);
3074 } else {
3075 BIC_PRESENT(BIC_PkgWatt);
3076 BIC_PRESENT(BIC_CorWatt);
3077 }
Len Brown889facb2012-11-08 00:48:57 -05003078 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003079 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003080 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 -05003081 BIC_PRESENT(BIC_PKG__);
3082 BIC_PRESENT(BIC_RAM__);
3083 if (rapl_joules) {
3084 BIC_PRESENT(BIC_Pkg_J);
3085 BIC_PRESENT(BIC_Cor_J);
3086 BIC_PRESENT(BIC_RAM_J);
3087 } else {
3088 BIC_PRESENT(BIC_PkgWatt);
3089 BIC_PRESENT(BIC_CorWatt);
3090 BIC_PRESENT(BIC_RAMWatt);
3091 }
Jacob Pan0f644902016-06-16 09:48:22 -07003092 break;
Len Brown889facb2012-11-08 00:48:57 -05003093 default:
3094 return;
3095 }
3096
3097 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003098 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003099 return;
3100
3101 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce69e2016-06-16 23:22:37 -04003102 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003103 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3104 else
3105 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003106
Andrey Semin40ee8e32014-12-05 00:07:00 -05003107 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3108
Len Brown144b44b2013-11-09 00:30:16 -05003109 time_unit = msr >> 16 & 0xF;
3110 if (time_unit == 0)
3111 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003112
Len Brown144b44b2013-11-09 00:30:16 -05003113 rapl_time_units = 1.0 / (1 << (time_unit));
3114
3115 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003116
3117 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003118 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003119 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003120
3121 return;
3122}
3123
Colin Ian King1b693172016-03-02 13:50:25 +00003124void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003125{
3126 if (!genuine_intel)
3127 return;
3128
3129 if (family != 6)
3130 return;
3131
3132 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003133 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3134 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3135 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003136 do_gfx_perf_limit_reasons = 1;
Len Brown869ce69e2016-06-16 23:22:37 -04003137 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003138 do_core_perf_limit_reasons = 1;
3139 do_ring_perf_limit_reasons = 1;
3140 default:
3141 return;
3142 }
3143}
3144
Len Brown889facb2012-11-08 00:48:57 -05003145int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3146{
3147 unsigned long long msr;
3148 unsigned int dts;
3149 int cpu;
3150
3151 if (!(do_dts || do_ptm))
3152 return 0;
3153
3154 cpu = t->cpu_id;
3155
3156 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003157 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003158 return 0;
3159
3160 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003161 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003162 return -1;
3163 }
3164
3165 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3166 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3167 return 0;
3168
3169 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003170 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003171 cpu, msr, tcc_activation_temp - dts);
3172
3173#ifdef THERM_DEBUG
3174 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3175 return 0;
3176
3177 dts = (msr >> 16) & 0x7F;
3178 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003179 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003180 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3181#endif
3182 }
3183
3184
3185 if (do_dts) {
3186 unsigned int resolution;
3187
3188 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3189 return 0;
3190
3191 dts = (msr >> 16) & 0x7F;
3192 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003193 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003194 cpu, msr, tcc_activation_temp - dts, resolution);
3195
3196#ifdef THERM_DEBUG
3197 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3198 return 0;
3199
3200 dts = (msr >> 16) & 0x7F;
3201 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003202 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003203 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3204#endif
3205 }
3206
3207 return 0;
3208}
Len Brown36229892016-02-26 20:51:02 -05003209
Len Brown889facb2012-11-08 00:48:57 -05003210void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3211{
Len Brownb7d8c142016-02-13 23:36:17 -05003212 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003213 cpu, label,
3214 ((msr >> 15) & 1) ? "EN" : "DIS",
3215 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3216 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3217 (((msr >> 16) & 1) ? "EN" : "DIS"));
3218
3219 return;
3220}
3221
3222int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3223{
3224 unsigned long long msr;
3225 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003226
3227 if (!do_rapl)
3228 return 0;
3229
3230 /* RAPL counters are per package, so print only for 1st thread/package */
3231 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3232 return 0;
3233
3234 cpu = t->cpu_id;
3235 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003236 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003237 return -1;
3238 }
3239
3240 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3241 return -1;
3242
Len Brown96e47152017-01-21 02:26:00 -05003243 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3244 rapl_power_units, rapl_energy_units, rapl_time_units);
3245
Len Brown144b44b2013-11-09 00:30:16 -05003246 if (do_rapl & RAPL_PKG_POWER_INFO) {
3247
Len Brown889facb2012-11-08 00:48:57 -05003248 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3249 return -5;
3250
3251
Len Brownb7d8c142016-02-13 23:36:17 -05003252 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 -05003253 cpu, msr,
3254 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3255 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3256 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3257 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3258
Len Brown144b44b2013-11-09 00:30:16 -05003259 }
3260 if (do_rapl & RAPL_PKG) {
3261
Len Brown889facb2012-11-08 00:48:57 -05003262 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3263 return -9;
3264
Len Brownb7d8c142016-02-13 23:36:17 -05003265 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003266 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003267
3268 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003269 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003270 cpu,
3271 ((msr >> 47) & 1) ? "EN" : "DIS",
3272 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3273 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3274 ((msr >> 48) & 1) ? "EN" : "DIS");
3275 }
3276
Len Brown0b2bb692015-03-26 00:50:30 -04003277 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003278 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3279 return -6;
3280
Len Brownb7d8c142016-02-13 23:36:17 -05003281 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 -05003282 cpu, msr,
3283 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3284 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3285 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3286 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003287 }
3288 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003289 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3290 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003291 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003292 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003293
3294 print_power_limit_msr(cpu, msr, "DRAM Limit");
3295 }
Len Brown144b44b2013-11-09 00:30:16 -05003296 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003297 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3298 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003299
Len Brown96e47152017-01-21 02:26:00 -05003300 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003301 }
Jacob Pan91484942016-06-16 09:48:20 -07003302 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003303 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3304 return -9;
3305 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3306 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3307 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003308 }
3309 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003310 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3311 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003312
Len Brown96e47152017-01-21 02:26:00 -05003313 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003314
Len Brown96e47152017-01-21 02:26:00 -05003315 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3316 return -9;
3317 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3318 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3319 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003320 }
3321 return 0;
3322}
3323
Len Brownd7899442015-01-23 00:12:33 -05003324/*
3325 * SNB adds support for additional MSRs:
3326 *
3327 * MSR_PKG_C7_RESIDENCY 0x000003fa
3328 * MSR_CORE_C7_RESIDENCY 0x000003fe
3329 * MSR_PKG_C2_RESIDENCY 0x0000060d
3330 */
Len Brown103a8fe2010-10-22 23:53:03 -04003331
Len Brownd7899442015-01-23 00:12:33 -05003332int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003333{
3334 if (!genuine_intel)
3335 return 0;
3336
3337 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003338 case INTEL_FAM6_SANDYBRIDGE:
3339 case INTEL_FAM6_SANDYBRIDGE_X:
3340 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3341 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3342 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3343 case INTEL_FAM6_HASWELL_X: /* HSW */
3344 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3345 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3346 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3347 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3348 case INTEL_FAM6_BROADWELL_X: /* BDX */
3349 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3350 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3351 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3352 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3353 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3354 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3355 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003356 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003357 return 1;
3358 }
3359 return 0;
3360}
3361
Len Brownd7899442015-01-23 00:12:33 -05003362/*
3363 * HSW adds support for additional MSRs:
3364 *
Len Brown5a634262016-04-06 17:15:55 -04003365 * MSR_PKG_C8_RESIDENCY 0x00000630
3366 * MSR_PKG_C9_RESIDENCY 0x00000631
3367 * MSR_PKG_C10_RESIDENCY 0x00000632
3368 *
3369 * MSR_PKGC8_IRTL 0x00000633
3370 * MSR_PKGC9_IRTL 0x00000634
3371 * MSR_PKGC10_IRTL 0x00000635
3372 *
Len Brownd7899442015-01-23 00:12:33 -05003373 */
3374int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003375{
3376 if (!genuine_intel)
3377 return 0;
3378
3379 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003380 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3381 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3382 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3383 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3384 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3385 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3386 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003387 return 1;
3388 }
3389 return 0;
3390}
3391
Len Brown0b2bb692015-03-26 00:50:30 -04003392/*
3393 * SKL adds support for additional MSRS:
3394 *
3395 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3396 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3397 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3398 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3399 */
3400int has_skl_msrs(unsigned int family, unsigned int model)
3401{
3402 if (!genuine_intel)
3403 return 0;
3404
3405 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003406 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3407 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3408 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3409 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003410 return 1;
3411 }
3412 return 0;
3413}
3414
Len Brown144b44b2013-11-09 00:30:16 -05003415int is_slm(unsigned int family, unsigned int model)
3416{
3417 if (!genuine_intel)
3418 return 0;
3419 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003420 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3421 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003422 return 1;
3423 }
3424 return 0;
3425}
3426
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003427int is_knl(unsigned int family, unsigned int model)
3428{
3429 if (!genuine_intel)
3430 return 0;
3431 switch (model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003432 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003433 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003434 return 1;
3435 }
3436 return 0;
3437}
3438
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003439unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3440{
3441 if (is_knl(family, model))
3442 return 1024;
3443 return 1;
3444}
3445
Len Brown144b44b2013-11-09 00:30:16 -05003446#define SLM_BCLK_FREQS 5
3447double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3448
3449double slm_bclk(void)
3450{
3451 unsigned long long msr = 3;
3452 unsigned int i;
3453 double freq;
3454
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003455 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003456 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003457
3458 i = msr & 0xf;
3459 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003460 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003461 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003462 }
3463 freq = slm_freq_table[i];
3464
Len Brown96e47152017-01-21 02:26:00 -05003465 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003466 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003467
3468 return freq;
3469}
3470
Len Brown103a8fe2010-10-22 23:53:03 -04003471double discover_bclk(unsigned int family, unsigned int model)
3472{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003473 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003474 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003475 else if (is_slm(family, model))
3476 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003477 else
3478 return 133.33;
3479}
3480
Len Brown889facb2012-11-08 00:48:57 -05003481/*
3482 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3483 * the Thermal Control Circuit (TCC) activates.
3484 * This is usually equal to tjMax.
3485 *
3486 * Older processors do not have this MSR, so there we guess,
3487 * but also allow cmdline over-ride with -T.
3488 *
3489 * Several MSR temperature values are in units of degrees-C
3490 * below this value, including the Digital Thermal Sensor (DTS),
3491 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3492 */
3493int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3494{
3495 unsigned long long msr;
3496 unsigned int target_c_local;
3497 int cpu;
3498
3499 /* tcc_activation_temp is used only for dts or ptm */
3500 if (!(do_dts || do_ptm))
3501 return 0;
3502
3503 /* this is a per-package concept */
3504 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3505 return 0;
3506
3507 cpu = t->cpu_id;
3508 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003509 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003510 return -1;
3511 }
3512
3513 if (tcc_activation_temp_override != 0) {
3514 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003515 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003516 cpu, tcc_activation_temp);
3517 return 0;
3518 }
3519
3520 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003521 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003522 goto guess;
3523
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003524 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003525 goto guess;
3526
Jean Delvare34821242014-05-01 11:40:19 +02003527 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003528
Len Brown96e47152017-01-21 02:26:00 -05003529 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003530 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003531 cpu, msr, target_c_local);
3532
Jean Delvare34821242014-05-01 11:40:19 +02003533 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003534 goto guess;
3535
3536 tcc_activation_temp = target_c_local;
3537
3538 return 0;
3539
3540guess:
3541 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003542 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003543 cpu, tcc_activation_temp);
3544
3545 return 0;
3546}
Len Brown69807a62015-11-21 12:22:47 -05003547
Len Brownaa8d8cc2016-03-11 13:26:03 -05003548void decode_feature_control_msr(void)
3549{
3550 unsigned long long msr;
3551
3552 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3553 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3554 base_cpu, msr,
3555 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3556 msr & (1 << 18) ? "SGX" : "");
3557}
3558
Len Brown69807a62015-11-21 12:22:47 -05003559void decode_misc_enable_msr(void)
3560{
3561 unsigned long long msr;
3562
3563 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003564 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003565 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003566 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3567 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3568 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3569 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3570 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003571}
3572
Len Brown33148d62017-01-21 01:26:16 -05003573void decode_misc_feature_control(void)
3574{
3575 unsigned long long msr;
3576
3577 if (!has_misc_feature_control)
3578 return;
3579
3580 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
3581 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
3582 base_cpu, msr,
3583 msr & (0 << 0) ? "No-" : "",
3584 msr & (1 << 0) ? "No-" : "",
3585 msr & (2 << 0) ? "No-" : "",
3586 msr & (3 << 0) ? "No-" : "");
3587}
Len Brownf0057312015-12-03 01:35:36 -05003588/*
3589 * Decode MSR_MISC_PWR_MGMT
3590 *
3591 * Decode the bits according to the Nehalem documentation
3592 * bit[0] seems to continue to have same meaning going forward
3593 * bit[1] less so...
3594 */
3595void decode_misc_pwr_mgmt_msr(void)
3596{
3597 unsigned long long msr;
3598
3599 if (!do_nhm_platform_info)
3600 return;
3601
Len Browncf4cbe52017-01-01 13:08:33 -05003602 if (no_MSR_MISC_PWR_MGMT)
3603 return;
3604
Len Brownf0057312015-12-03 01:35:36 -05003605 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003606 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 -05003607 base_cpu, msr,
3608 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003609 msr & (1 << 1) ? "EN" : "DIS",
3610 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003611}
Len Brown71616c82017-01-07 22:37:48 -05003612/*
3613 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3614 *
3615 * This MSRs are present on Silvermont processors,
3616 * Intel Atom processor E3000 series (Baytrail), and friends.
3617 */
3618void decode_c6_demotion_policy_msr(void)
3619{
3620 unsigned long long msr;
3621
3622 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3623 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3624 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3625
3626 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3627 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3628 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3629}
Len Brown7f5c2582015-12-01 01:36:39 -05003630
Len Brownfcd17212015-03-23 20:29:09 -04003631void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003632{
Len Brown61a87ba2015-11-23 02:30:51 -05003633 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003634 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003635 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003636
3637 eax = ebx = ecx = edx = 0;
3638
Len Brown5aea2f72016-03-13 03:14:35 -04003639 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003640
3641 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3642 genuine_intel = 1;
3643
Len Brown96e47152017-01-21 02:26:00 -05003644 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003645 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003646 (char *)&ebx, (char *)&edx, (char *)&ecx);
3647
Len Brown5aea2f72016-03-13 03:14:35 -04003648 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003649 family = (fms >> 8) & 0xf;
3650 model = (fms >> 4) & 0xf;
3651 stepping = fms & 0xf;
3652 if (family == 6 || family == 0xf)
3653 model += ((fms >> 16) & 0xf) << 4;
3654
Len Brown96e47152017-01-21 02:26:00 -05003655 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05003656 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003657 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003658 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003659 ecx & (1 << 0) ? "SSE3" : "-",
3660 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003661 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003662 ecx & (1 << 7) ? "EIST" : "-",
3663 ecx & (1 << 8) ? "TM2" : "-",
3664 edx & (1 << 4) ? "TSC" : "-",
3665 edx & (1 << 5) ? "MSR" : "-",
3666 edx & (1 << 22) ? "ACPI-TM" : "-",
3667 edx & (1 << 29) ? "TM" : "-");
3668 }
Len Brown103a8fe2010-10-22 23:53:03 -04003669
Josh Triplettb2c95d92013-08-20 17:20:18 -07003670 if (!(edx & (1 << 5)))
3671 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003672
3673 /*
3674 * check max extended function levels of CPUID.
3675 * This is needed to check for invariant TSC.
3676 * This check is valid for both Intel and AMD.
3677 */
3678 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003679 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003680
Len Brown61a87ba2015-11-23 02:30:51 -05003681 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003682
Len Brownd7899442015-01-23 00:12:33 -05003683 /*
3684 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3685 * this check is valid for both Intel and AMD
3686 */
Len Brown5aea2f72016-03-13 03:14:35 -04003687 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003688 has_invariant_tsc = edx & (1 << 8);
3689 }
Len Brown103a8fe2010-10-22 23:53:03 -04003690
3691 /*
3692 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3693 * this check is valid for both Intel and AMD
3694 */
3695
Len Brown5aea2f72016-03-13 03:14:35 -04003696 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003697 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003698 if (has_aperf) {
3699 BIC_PRESENT(BIC_Avg_MHz);
3700 BIC_PRESENT(BIC_Busy);
3701 BIC_PRESENT(BIC_Bzy_MHz);
3702 }
Len Brown889facb2012-11-08 00:48:57 -05003703 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003704 if (do_dts)
3705 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05003706 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05003707 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003708 if (do_ptm)
3709 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003710 has_hwp = eax & (1 << 7);
3711 has_hwp_notify = eax & (1 << 8);
3712 has_hwp_activity_window = eax & (1 << 9);
3713 has_hwp_epp = eax & (1 << 10);
3714 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003715 has_epb = ecx & (1 << 3);
3716
Len Brown96e47152017-01-21 02:26:00 -05003717 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05003718 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003719 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3720 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05003721 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05003722 do_dts ? "" : "No-",
3723 do_ptm ? "" : "No-",
3724 has_hwp ? "" : "No-",
3725 has_hwp_notify ? "" : "No-",
3726 has_hwp_activity_window ? "" : "No-",
3727 has_hwp_epp ? "" : "No-",
3728 has_hwp_pkg ? "" : "No-",
3729 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003730
Len Brown96e47152017-01-21 02:26:00 -05003731 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05003732 decode_misc_enable_msr();
3733
Len Brown33148d62017-01-21 01:26:16 -05003734
Len Brown96e47152017-01-21 02:26:00 -05003735 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003736 int has_sgx;
3737
3738 ecx = 0;
3739
3740 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3741
3742 has_sgx = ebx & (1 << 2);
3743 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3744
3745 if (has_sgx)
3746 decode_feature_control_msr();
3747 }
3748
Len Brown61a87ba2015-11-23 02:30:51 -05003749 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003750 unsigned int eax_crystal;
3751 unsigned int ebx_tsc;
3752
3753 /*
3754 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3755 */
3756 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003757 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003758
3759 if (ebx_tsc != 0) {
3760
Len Brown96e47152017-01-21 02:26:00 -05003761 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003762 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003763 eax_crystal, ebx_tsc, crystal_hz);
3764
3765 if (crystal_hz == 0)
3766 switch(model) {
Len Brown869ce69e2016-06-16 23:22:37 -04003767 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3768 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3769 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3770 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003771 crystal_hz = 24000000; /* 24.0 MHz */
3772 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003773 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003774 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003775 crystal_hz = 25000000; /* 25.0 MHz */
3776 break;
Len Brown869ce69e2016-06-16 23:22:37 -04003777 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne8efbc82016-04-06 17:15:57 -04003778 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003779 break;
3780 default:
3781 crystal_hz = 0;
3782 }
3783
3784 if (crystal_hz) {
3785 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05003786 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003787 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003788 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3789 }
3790 }
3791 }
Len Brown61a87ba2015-11-23 02:30:51 -05003792 if (max_level >= 0x16) {
3793 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3794
3795 /*
3796 * CPUID 16H Base MHz, Max MHz, Bus MHz
3797 */
3798 base_mhz = max_mhz = bus_mhz = edx = 0;
3799
Len Brown5aea2f72016-03-13 03:14:35 -04003800 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05003801 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003802 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003803 base_mhz, max_mhz, bus_mhz);
3804 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003805
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003806 if (has_aperf)
3807 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3808
Len Brown812db3f2017-02-10 00:25:41 -05003809 BIC_PRESENT(BIC_IRQ);
3810 BIC_PRESENT(BIC_TSC_MHz);
3811
3812 if (probe_nhm_msrs(family, model)) {
3813 do_nhm_platform_info = 1;
3814 BIC_PRESENT(BIC_CPU_c1);
3815 BIC_PRESENT(BIC_CPU_c3);
3816 BIC_PRESENT(BIC_CPU_c6);
3817 BIC_PRESENT(BIC_SMI);
3818 }
Len Brownd7899442015-01-23 00:12:33 -05003819 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05003820
3821 if (do_snb_cstates)
3822 BIC_PRESENT(BIC_CPU_c7);
3823
Len Brown5a634262016-04-06 17:15:55 -04003824 do_irtl_snb = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05003825 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3826 do_pc3 = (pkg_cstate_limit >= PCL__3);
3827 do_pc6 = (pkg_cstate_limit >= PCL__6);
3828 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brown0539ba112017-02-10 00:27:20 -05003829 if (has_slv_msrs(family, model)) {
3830 do_pc2 = do_pc3 = do_pc7 = 0;
3831 do_pc6 = 1;
3832 BIC_PRESENT(BIC_Mod_c6);
3833 use_c1_residency_msr = 1;
3834 }
Len Brownd7899442015-01-23 00:12:33 -05003835 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown5a634262016-04-06 17:15:55 -04003836 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04003837 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05003838 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003839 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04003840
Len Brown96e47152017-01-21 02:26:00 -05003841 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05003842 decode_misc_pwr_mgmt_msr();
3843
Len Brown96e47152017-01-21 02:26:00 -05003844 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05003845 decode_c6_demotion_policy_msr();
3846
Len Brown889facb2012-11-08 00:48:57 -05003847 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04003848 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05003849
Len Brown96e47152017-01-21 02:26:00 -05003850 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00003851 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003852
Len Browna2b7b742015-09-26 00:12:38 -04003853 if (has_skl_msrs(family, model))
3854 calculate_tsc_tweak();
3855
Len Brown812db3f2017-02-10 00:25:41 -05003856 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
3857 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05003858
Len Brown812db3f2017-02-10 00:25:41 -05003859 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
3860 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05003861
Len Brown96e47152017-01-21 02:26:00 -05003862 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05003863 decode_misc_feature_control();
3864
Len Brown889facb2012-11-08 00:48:57 -05003865 return;
Len Brown103a8fe2010-10-22 23:53:03 -04003866}
3867
Len Brownd8af6f52015-02-10 01:56:38 -05003868void help()
Len Brown103a8fe2010-10-22 23:53:03 -04003869{
Len Brownb7d8c142016-02-13 23:36:17 -05003870 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05003871 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3872 "\n"
3873 "Turbostat forks the specified COMMAND and prints statistics\n"
3874 "when COMMAND completes.\n"
3875 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3876 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05003877 "--add add a counter\n"
3878 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brown96e47152017-01-21 02:26:00 -05003879 "--quiet skip decoding system configuration header\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003880 "--interval sec Override default 5-second measurement interval\n"
3881 "--help print this help message\n"
Len Brownb7d8c142016-02-13 23:36:17 -05003882 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003883 "--version print version information\n"
3884 "\n"
3885 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04003886}
3887
3888
3889/*
3890 * in /dev/cpu/ return success for names that are numbers
3891 * ie. filter out ".", "..", "microcode".
3892 */
3893int dir_filter(const struct dirent *dirp)
3894{
3895 if (isdigit(dirp->d_name[0]))
3896 return 1;
3897 else
3898 return 0;
3899}
3900
3901int open_dev_cpu_msr(int dummy1)
3902{
3903 return 0;
3904}
3905
Len Brownc98d5d92012-06-04 00:56:40 -04003906void topology_probe()
3907{
3908 int i;
3909 int max_core_id = 0;
3910 int max_package_id = 0;
3911 int max_siblings = 0;
3912 struct cpu_topology {
3913 int core_id;
3914 int physical_package_id;
3915 } *cpus;
3916
3917 /* Initialize num_cpus, max_cpu_num */
3918 topo.num_cpus = 0;
3919 topo.max_cpu_num = 0;
3920 for_all_proc_cpus(count_cpus);
3921 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003922 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04003923
Len Brownd8af6f52015-02-10 01:56:38 -05003924 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003925 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04003926
3927 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003928 if (cpus == NULL)
3929 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003930
3931 /*
3932 * Allocate and initialize cpu_present_set
3933 */
3934 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003935 if (cpu_present_set == NULL)
3936 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003937 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3938 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3939 for_all_proc_cpus(mark_cpu_present);
3940
3941 /*
3942 * Allocate and initialize cpu_affinity_set
3943 */
3944 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003945 if (cpu_affinity_set == NULL)
3946 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003947 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3948 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3949
3950
3951 /*
3952 * For online cpus
3953 * find max_core_id, max_package_id
3954 */
3955 for (i = 0; i <= topo.max_cpu_num; ++i) {
3956 int siblings;
3957
3958 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003959 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003960 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04003961 continue;
3962 }
3963 cpus[i].core_id = get_core_id(i);
3964 if (cpus[i].core_id > max_core_id)
3965 max_core_id = cpus[i].core_id;
3966
3967 cpus[i].physical_package_id = get_physical_package_id(i);
3968 if (cpus[i].physical_package_id > max_package_id)
3969 max_package_id = cpus[i].physical_package_id;
3970
3971 siblings = get_num_ht_siblings(i);
3972 if (siblings > max_siblings)
3973 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003974 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003975 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003976 i, cpus[i].physical_package_id, cpus[i].core_id);
3977 }
3978 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003979 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003980 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003981 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003982 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003983 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04003984
3985 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003986 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003987 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003988 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003989 if (debug && !summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003990 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04003991
3992 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003993 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003994 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04003995
3996 free(cpus);
3997}
3998
3999void
4000allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4001{
4002 int i;
4003
4004 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004005 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004006 if (*t == NULL)
4007 goto error;
4008
4009 for (i = 0; i < topo.num_threads_per_core *
4010 topo.num_cores_per_pkg * topo.num_packages; i++)
4011 (*t)[i].cpu_id = -1;
4012
4013 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004014 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004015 if (*c == NULL)
4016 goto error;
4017
4018 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4019 (*c)[i].core_id = -1;
4020
Len Brown678a3bd2017-02-09 22:22:13 -05004021 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004022 if (*p == NULL)
4023 goto error;
4024
4025 for (i = 0; i < topo.num_packages; i++)
4026 (*p)[i].package_id = i;
4027
4028 return;
4029error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004030 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004031}
4032/*
4033 * init_counter()
4034 *
4035 * set cpu_id, core_num, pkg_num
4036 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4037 *
4038 * increment topo.num_cores when 1st core in pkg seen
4039 */
4040void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4041 struct pkg_data *pkg_base, int thread_num, int core_num,
4042 int pkg_num, int cpu_id)
4043{
4044 struct thread_data *t;
4045 struct core_data *c;
4046 struct pkg_data *p;
4047
4048 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4049 c = GET_CORE(core_base, core_num, pkg_num);
4050 p = GET_PKG(pkg_base, pkg_num);
4051
4052 t->cpu_id = cpu_id;
4053 if (thread_num == 0) {
4054 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4055 if (cpu_is_first_core_in_package(cpu_id))
4056 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4057 }
4058
4059 c->core_id = core_num;
4060 p->package_id = pkg_num;
4061}
4062
4063
4064int initialize_counters(int cpu_id)
4065{
4066 int my_thread_id, my_core_id, my_package_id;
4067
4068 my_package_id = get_physical_package_id(cpu_id);
4069 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004070 my_thread_id = get_cpu_position_in_core(cpu_id);
4071 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004072 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004073
4074 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4075 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4076 return 0;
4077}
4078
4079void allocate_output_buffer()
4080{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004081 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004082 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004083 if (outp == NULL)
4084 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004085}
Len Brown36229892016-02-26 20:51:02 -05004086void allocate_fd_percpu(void)
4087{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004088 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004089 if (fd_percpu == NULL)
4090 err(-1, "calloc fd_percpu");
4091}
Len Brown562a2d32016-02-26 23:48:05 -05004092void allocate_irq_buffers(void)
4093{
4094 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4095 if (irq_column_2_cpu == NULL)
4096 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004097
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004098 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004099 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004100 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004101}
Len Brownc98d5d92012-06-04 00:56:40 -04004102void setup_all_buffers(void)
4103{
4104 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004105 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004106 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004107 allocate_counters(&thread_even, &core_even, &package_even);
4108 allocate_counters(&thread_odd, &core_odd, &package_odd);
4109 allocate_output_buffer();
4110 for_all_proc_cpus(initialize_counters);
4111}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004112
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004113void set_base_cpu(void)
4114{
4115 base_cpu = sched_getcpu();
4116 if (base_cpu < 0)
4117 err(-ENODEV, "No valid cpus found");
4118
4119 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004120 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004121}
4122
Len Brown103a8fe2010-10-22 23:53:03 -04004123void turbostat_init()
4124{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004125 setup_all_buffers();
4126 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004127 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004128 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004129 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004130
Len Brown103a8fe2010-10-22 23:53:03 -04004131
Len Brown96e47152017-01-21 02:26:00 -05004132 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004133 for_all_cpus(print_hwp, ODD_COUNTERS);
4134
Len Brown96e47152017-01-21 02:26:00 -05004135 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004136 for_all_cpus(print_epb, ODD_COUNTERS);
4137
Len Brown96e47152017-01-21 02:26:00 -05004138 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004139 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4140
Len Brown96e47152017-01-21 02:26:00 -05004141 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004142 for_all_cpus(print_rapl, ODD_COUNTERS);
4143
4144 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4145
Len Brown96e47152017-01-21 02:26:00 -05004146 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004147 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004148
Len Brown96e47152017-01-21 02:26:00 -05004149 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004150 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004151}
4152
4153int fork_it(char **argv)
4154{
Len Brown103a8fe2010-10-22 23:53:03 -04004155 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004156 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004157
Len Brownd91bb172012-11-01 00:08:19 -04004158 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4159 if (status)
4160 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004161 /* clear affinity side-effect of get_counters() */
4162 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004163 gettimeofday(&tv_even, (struct timezone *)NULL);
4164
4165 child_pid = fork();
4166 if (!child_pid) {
4167 /* child */
4168 execvp(argv[0], argv);
4169 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004170
4171 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004172 if (child_pid == -1)
4173 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004174
4175 signal(SIGINT, SIG_IGN);
4176 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004177 if (waitpid(child_pid, &status, 0) == -1)
4178 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004179 }
Len Brownc98d5d92012-06-04 00:56:40 -04004180 /*
4181 * n.b. fork_it() does not check for errors from for_all_cpus()
4182 * because re-starting is problematic when forking
4183 */
4184 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004185 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004186 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004187 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4188 fprintf(outf, "%s: Counter reset detected\n", progname);
4189 else {
4190 compute_average(EVEN_COUNTERS);
4191 format_all_counters(EVEN_COUNTERS);
4192 }
Len Brown103a8fe2010-10-22 23:53:03 -04004193
Len Brownb7d8c142016-02-13 23:36:17 -05004194 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4195
4196 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004197
Len Brownd91bb172012-11-01 00:08:19 -04004198 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004199}
4200
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004201int get_and_dump_counters(void)
4202{
4203 int status;
4204
4205 status = for_all_cpus(get_counters, ODD_COUNTERS);
4206 if (status)
4207 return status;
4208
4209 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4210 if (status)
4211 return status;
4212
Len Brownb7d8c142016-02-13 23:36:17 -05004213 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004214
4215 return status;
4216}
4217
Len Brownd8af6f52015-02-10 01:56:38 -05004218void print_version() {
Len Brown0539ba112017-02-10 00:27:20 -05004219 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004220 " - Len Brown <lenb@kernel.org>\n");
4221}
4222
Len Brown388e9c82016-12-22 23:57:55 -05004223int add_counter(unsigned int msr_num, char *name, unsigned int width,
4224 enum counter_scope scope, enum counter_type type,
4225 enum counter_format format)
4226{
4227 struct msr_counter *msrp;
4228
4229 msrp = calloc(1, sizeof(struct msr_counter));
4230 if (msrp == NULL) {
4231 perror("calloc");
4232 exit(1);
4233 }
4234
4235 msrp->msr_num = msr_num;
4236 strncpy(msrp->name, name, NAME_BYTES);
4237 msrp->width = width;
4238 msrp->type = type;
4239 msrp->format = format;
4240
4241 switch (scope) {
4242
4243 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004244 msrp->next = sys.tp;
4245 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004246 sys.added_thread_counters++;
4247 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4248 fprintf(stderr, "exceeded max %d added thread counters\n",
4249 MAX_ADDED_COUNTERS);
4250 exit(-1);
4251 }
Len Brown388e9c82016-12-22 23:57:55 -05004252 break;
4253
4254 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004255 msrp->next = sys.cp;
4256 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004257 sys.added_core_counters++;
4258 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4259 fprintf(stderr, "exceeded max %d added core counters\n",
4260 MAX_ADDED_COUNTERS);
4261 exit(-1);
4262 }
Len Brown388e9c82016-12-22 23:57:55 -05004263 break;
4264
4265 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004266 msrp->next = sys.pp;
4267 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004268 sys.added_package_counters++;
4269 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4270 fprintf(stderr, "exceeded max %d added package counters\n",
4271 MAX_ADDED_COUNTERS);
4272 exit(-1);
4273 }
Len Brown388e9c82016-12-22 23:57:55 -05004274 break;
4275 }
4276
4277 return 0;
4278}
4279
4280void parse_add_command(char *add_command)
4281{
4282 int msr_num = 0;
4283 char name_buffer[NAME_BYTES];
4284 int width = 64;
4285 int fail = 0;
4286 enum counter_scope scope = SCOPE_CPU;
4287 enum counter_type type = COUNTER_CYCLES;
4288 enum counter_format format = FORMAT_DELTA;
4289
4290 while (add_command) {
4291
4292 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4293 goto next;
4294
4295 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4296 goto next;
4297
4298 if (sscanf(add_command, "u%d", &width) == 1) {
4299 if ((width == 32) || (width == 64))
4300 goto next;
4301 width = 64;
4302 }
4303 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4304 scope = SCOPE_CPU;
4305 goto next;
4306 }
4307 if (!strncmp(add_command, "core", strlen("core"))) {
4308 scope = SCOPE_CORE;
4309 goto next;
4310 }
4311 if (!strncmp(add_command, "package", strlen("package"))) {
4312 scope = SCOPE_PACKAGE;
4313 goto next;
4314 }
4315 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4316 type = COUNTER_CYCLES;
4317 goto next;
4318 }
4319 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4320 type = COUNTER_SECONDS;
4321 goto next;
4322 }
4323 if (!strncmp(add_command, "raw", strlen("raw"))) {
4324 format = FORMAT_RAW;
4325 goto next;
4326 }
4327 if (!strncmp(add_command, "delta", strlen("delta"))) {
4328 format = FORMAT_DELTA;
4329 goto next;
4330 }
4331 if (!strncmp(add_command, "percent", strlen("percent"))) {
4332 format = FORMAT_PERCENT;
4333 goto next;
4334 }
4335
4336 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4337 char *eos;
4338
4339 eos = strchr(name_buffer, ',');
4340 if (eos)
4341 *eos = '\0';
4342 goto next;
4343 }
4344
4345next:
4346 add_command = strchr(add_command, ',');
4347 if (add_command)
4348 add_command++;
4349
4350 }
4351 if (msr_num == 0) {
4352 fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4353 fail++;
4354 }
4355
4356 /* generate default column header */
4357 if (*name_buffer == '\0') {
4358 if (format == FORMAT_RAW) {
4359 if (width == 32)
4360 sprintf(name_buffer, "msr%d", msr_num);
4361 else
4362 sprintf(name_buffer, "MSR%d", msr_num);
4363 } else if (format == FORMAT_DELTA) {
4364 if (width == 32)
4365 sprintf(name_buffer, "cnt%d", msr_num);
4366 else
4367 sprintf(name_buffer, "CNT%d", msr_num);
4368 } else if (format == FORMAT_PERCENT) {
4369 if (width == 32)
4370 sprintf(name_buffer, "msr%d%%", msr_num);
4371 else
4372 sprintf(name_buffer, "MSR%d%%", msr_num);
4373 }
4374 }
4375
4376 if (add_counter(msr_num, name_buffer, width, scope, type, format))
4377 fail++;
4378
4379 if (fail) {
4380 help();
4381 exit(1);
4382 }
4383}
Len Brown812db3f2017-02-10 00:25:41 -05004384/*
4385 * HIDE_LIST - hide this list of counters, show the rest [default]
4386 * SHOW_LIST - show this list of counters, hide the rest
4387 */
4388enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4389
4390int shown;
4391/*
4392 * parse_show_hide() - process cmdline to set default counter action
4393 */
4394void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4395{
4396 /*
4397 * --show: show only those specified
4398 * The 1st invocation will clear and replace the enabled mask
4399 * subsequent invocations can add to it.
4400 */
4401 if (new_mode == SHOW_LIST) {
4402 if (shown == 0)
4403 bic_enabled = bic_lookup(optarg);
4404 else
4405 bic_enabled |= bic_lookup(optarg);
4406 shown = 1;
4407
4408 return;
4409 }
4410
4411 /*
4412 * --hide: do not show those specified
4413 * multiple invocations simply clear more bits in enabled mask
4414 */
4415 bic_enabled &= ~bic_lookup(optarg);
4416}
4417
Len Brown103a8fe2010-10-22 23:53:03 -04004418void cmdline(int argc, char **argv)
4419{
4420 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004421 int option_index = 0;
4422 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004423 {"add", required_argument, 0, 'a'},
Len Brownd8af6f52015-02-10 01:56:38 -05004424 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05004425 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brownd8af6f52015-02-10 01:56:38 -05004426 {"interval", required_argument, 0, 'i'},
4427 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004428 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004429 {"Joules", no_argument, 0, 'J'},
Len Brownb7d8c142016-02-13 23:36:17 -05004430 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004431 {"Package", no_argument, 0, 'p'},
4432 {"processor", no_argument, 0, 'p'},
Len Brown96e47152017-01-21 02:26:00 -05004433 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05004434 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004435 {"Summary", no_argument, 0, 'S'},
4436 {"TCC", required_argument, 0, 'T'},
4437 {"version", no_argument, 0, 'v' },
4438 {0, 0, 0, 0 }
4439 };
Len Brown103a8fe2010-10-22 23:53:03 -04004440
4441 progname = argv[0];
4442
Len Brown96e47152017-01-21 02:26:00 -05004443 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpqST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004444 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004445 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004446 case 'a':
4447 parse_add_command(optarg);
4448 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004449 case 'D':
4450 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004451 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004452 case 'd':
4453 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004454 break;
Len Brown812db3f2017-02-10 00:25:41 -05004455 case 'H':
4456 parse_show_hide(optarg, HIDE_LIST);
4457 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004458 case 'h':
4459 default:
4460 help();
4461 exit(1);
4462 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004463 {
4464 double interval = strtod(optarg, NULL);
4465
4466 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004467 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004468 interval);
4469 exit(2);
4470 }
4471
4472 interval_ts.tv_sec = interval;
4473 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4474 }
Len Brown889facb2012-11-08 00:48:57 -05004475 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004476 case 'J':
4477 rapl_joules++;
4478 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004479 case 'o':
4480 outf = fopen_or_die(optarg, "w");
4481 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004482 case 'P':
4483 show_pkg_only++;
4484 break;
4485 case 'p':
4486 show_core_only++;
4487 break;
Len Brown96e47152017-01-21 02:26:00 -05004488 case 'q':
4489 quiet = 1;
4490 break;
Len Brown812db3f2017-02-10 00:25:41 -05004491 case 's':
4492 parse_show_hide(optarg, SHOW_LIST);
4493 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004494 case 'S':
4495 summary_only++;
4496 break;
4497 case 'T':
4498 tcc_activation_temp_override = atoi(optarg);
4499 break;
4500 case 'v':
4501 print_version();
4502 exit(0);
4503 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004504 }
4505 }
4506}
4507
4508int main(int argc, char **argv)
4509{
Len Brownb7d8c142016-02-13 23:36:17 -05004510 outf = stderr;
4511
Len Brown103a8fe2010-10-22 23:53:03 -04004512 cmdline(argc, argv);
4513
Len Brown96e47152017-01-21 02:26:00 -05004514 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05004515 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004516
4517 turbostat_init();
4518
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004519 /* dump counters and exit */
4520 if (dump_only)
4521 return get_and_dump_counters();
4522
Len Brown103a8fe2010-10-22 23:53:03 -04004523 /*
4524 * if any params left, it must be a command to fork
4525 */
4526 if (argc - optind)
4527 return fork_it(argv + optind);
4528 else
4529 turbostat_loop();
4530
4531 return 0;
4532}