blob: 8c437115d41b9342427c8fb757d5a4893201057c [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
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 Brown0b2bb692015-03-26 00:50:30 -040058unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050059unsigned int do_slm_cstates;
60unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040061unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050062unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040063unsigned int do_irtl_snb;
64unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050065unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int genuine_intel;
67unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050068unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050069unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020070unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040071double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040072double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040073unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040074double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040075unsigned int show_pkg_only;
76unsigned int show_core_only;
77char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050078unsigned int do_rapl;
79unsigned int do_dts;
80unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050081unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050082unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050083unsigned int tcc_activation_temp;
84unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050085double rapl_power_units, rapl_time_units;
86double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050087double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040088unsigned int do_core_perf_limit_reasons;
89unsigned int do_gfx_perf_limit_reasons;
90unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040091unsigned int crystal_hz;
92unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040093int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040094double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050095unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
96 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
97unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
98unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
99unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
100unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500101unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500102
Len Browne6f9bb32013-12-03 02:19:19 -0500103#define RAPL_PKG (1 << 0)
104 /* 0x610 MSR_PKG_POWER_LIMIT */
105 /* 0x611 MSR_PKG_ENERGY_STATUS */
106#define RAPL_PKG_PERF_STATUS (1 << 1)
107 /* 0x613 MSR_PKG_PERF_STATUS */
108#define RAPL_PKG_POWER_INFO (1 << 2)
109 /* 0x614 MSR_PKG_POWER_INFO */
110
111#define RAPL_DRAM (1 << 3)
112 /* 0x618 MSR_DRAM_POWER_LIMIT */
113 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500114#define RAPL_DRAM_PERF_STATUS (1 << 4)
115 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400116#define RAPL_DRAM_POWER_INFO (1 << 5)
117 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500118
Jacob Pan91484942016-06-16 09:48:20 -0700119#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500120 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400121#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500122 /* 0x63a MSR_PP0_POLICY */
123
Len Brown0b2bb692015-03-26 00:50:30 -0400124#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500125 /* 0x640 MSR_PP1_POWER_LIMIT */
126 /* 0x641 MSR_PP1_ENERGY_STATUS */
127 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700128
129#define RAPL_CORES_ENERGY_STATUS (1 << 9)
130 /* 0x639 MSR_PP0_ENERGY_STATUS */
131#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500132#define TJMAX_DEFAULT 100
133
134#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400135
Len Brown388e9c82016-12-22 23:57:55 -0500136/*
137 * buffer size used by sscanf() for added column names
138 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
139 */
140#define NAME_BYTES 20
141
Len Brown103a8fe2010-10-22 23:53:03 -0400142int backwards_count;
143char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400144
Len Brownc98d5d92012-06-04 00:56:40 -0400145cpu_set_t *cpu_present_set, *cpu_affinity_set;
146size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown678a3bd2017-02-09 22:22:13 -0500147#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400148
Len Brownc98d5d92012-06-04 00:56:40 -0400149struct thread_data {
150 unsigned long long tsc;
151 unsigned long long aperf;
152 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500153 unsigned long long c1;
Len Brown562a2d32016-02-26 23:48:05 -0500154 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500155 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400156 unsigned int cpu_id;
157 unsigned int flags;
158#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
159#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500160 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400161} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400162
Len Brownc98d5d92012-06-04 00:56:40 -0400163struct core_data {
164 unsigned long long c3;
165 unsigned long long c6;
166 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500167 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500168 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400169 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500170 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400171} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400172
Len Brownc98d5d92012-06-04 00:56:40 -0400173struct pkg_data {
174 unsigned long long pc2;
175 unsigned long long pc3;
176 unsigned long long pc6;
177 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800178 unsigned long long pc8;
179 unsigned long long pc9;
180 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400181 unsigned long long pkg_wtd_core_c0;
182 unsigned long long pkg_any_core_c0;
183 unsigned long long pkg_any_gfxe_c0;
184 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400185 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500186 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400187 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500188 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
189 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
190 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
191 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
192 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
193 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
194 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500195 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400196} *package_even, *package_odd;
197
198#define ODD_COUNTERS thread_odd, core_odd, package_odd
199#define EVEN_COUNTERS thread_even, core_even, package_even
200
201#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
202 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
203 topo.num_threads_per_core + \
204 (core_no) * topo.num_threads_per_core + (thread_no))
205#define GET_CORE(core_base, core_no, pkg_no) \
206 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
207#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
208
Len Brown388e9c82016-12-22 23:57:55 -0500209enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
210enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
211enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
212
213struct msr_counter {
214 unsigned int msr_num;
215 char name[NAME_BYTES];
216 unsigned int width;
217 enum counter_type type;
218 enum counter_format format;
219 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500220 unsigned int flags;
221#define FLAGS_HIDE (1 << 0)
222#define FLAGS_SHOW (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500223};
224
225struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500226 unsigned int added_thread_counters;
227 unsigned int added_core_counters;
228 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500229 struct msr_counter *tp;
230 struct msr_counter *cp;
231 struct msr_counter *pp;
232} sys;
233
Len Brownc98d5d92012-06-04 00:56:40 -0400234struct system_summary {
235 struct thread_data threads;
236 struct core_data cores;
237 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500238} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400239
240
241struct topo_params {
242 int num_packages;
243 int num_cpus;
244 int num_cores;
245 int max_cpu_num;
246 int num_cores_per_pkg;
247 int num_threads_per_core;
248} topo;
249
250struct timeval tv_even, tv_odd, tv_delta;
251
Len Brown562a2d32016-02-26 23:48:05 -0500252int *irq_column_2_cpu; /* /proc/interrupts column numbers */
253int *irqs_per_cpu; /* indexed by cpu_num */
254
Len Brownc98d5d92012-06-04 00:56:40 -0400255void setup_all_buffers(void);
256
257int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400258{
Len Brownc98d5d92012-06-04 00:56:40 -0400259 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400260}
Len Brown88c32812012-03-29 21:44:40 -0400261/*
Len Brownc98d5d92012-06-04 00:56:40 -0400262 * run func(thread, core, package) in topology order
263 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400264 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400265
Len Brownc98d5d92012-06-04 00:56:40 -0400266int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
267 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400268{
Len Brownc98d5d92012-06-04 00:56:40 -0400269 int retval, pkg_no, core_no, thread_no;
270
271 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
272 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
273 for (thread_no = 0; thread_no <
274 topo.num_threads_per_core; ++thread_no) {
275 struct thread_data *t;
276 struct core_data *c;
277 struct pkg_data *p;
278
279 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
280
281 if (cpu_is_not_present(t->cpu_id))
282 continue;
283
284 c = GET_CORE(core_base, core_no, pkg_no);
285 p = GET_PKG(pkg_base, pkg_no);
286
287 retval = func(t, c, p);
288 if (retval)
289 return retval;
290 }
291 }
292 }
293 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400294}
295
296int cpu_migrate(int cpu)
297{
Len Brownc98d5d92012-06-04 00:56:40 -0400298 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
299 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
300 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400301 return -1;
302 else
303 return 0;
304}
Len Brown36229892016-02-26 20:51:02 -0500305int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400306{
Len Brown103a8fe2010-10-22 23:53:03 -0400307 char pathname[32];
308 int fd;
309
Len Brown36229892016-02-26 20:51:02 -0500310 fd = fd_percpu[cpu];
311
312 if (fd)
313 return fd;
314
Len Brown103a8fe2010-10-22 23:53:03 -0400315 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
316 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400317 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400318 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 -0400319
Len Brown36229892016-02-26 20:51:02 -0500320 fd_percpu[cpu] = fd;
321
322 return fd;
323}
324
325int get_msr(int cpu, off_t offset, unsigned long long *msr)
326{
327 ssize_t retval;
328
329 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400330
Len Brown98481e72014-08-15 00:36:50 -0400331 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500332 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400333
334 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400335}
336
Len Brownfc04cc62014-02-06 00:55:19 -0500337/*
Len Brown812db3f2017-02-10 00:25:41 -0500338 * Each string in this array is compared in --show and --hide cmdline.
339 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500340 */
Len Brown812db3f2017-02-10 00:25:41 -0500341struct msr_counter bic[] = {
342 { 0x0, "Package" },
343 { 0x0, "Avg_MHz" },
344 { 0x0, "Bzy_MHz" },
345 { 0x0, "TSC_MHz" },
346 { 0x0, "IRQ" },
347 { 0x0, "SMI", 32, 0, FORMAT_DELTA, NULL},
348 { 0x0, "Busy%" },
349 { 0x0, "CPU%c1" },
350 { 0x0, "CPU%c3" },
351 { 0x0, "CPU%c6" },
352 { 0x0, "CPU%c7" },
353 { 0x0, "ThreadC" },
354 { 0x0, "CoreTmp" },
355 { 0x0, "CoreCnt" },
356 { 0x0, "PkgTmp" },
357 { 0x0, "GFX%rc6" },
358 { 0x0, "GFXMHz" },
359 { 0x0, "Pkg%pc2" },
360 { 0x0, "Pkg%pc3" },
361 { 0x0, "Pkg%pc6" },
362 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500363 { 0x0, "Pkg%pc8" },
364 { 0x0, "Pkg%pc9" },
365 { 0x0, "Pkg%pc10" },
Len Brown812db3f2017-02-10 00:25:41 -0500366 { 0x0, "PkgWatt" },
367 { 0x0, "CorWatt" },
368 { 0x0, "GFXWatt" },
369 { 0x0, "PkgCnt" },
370 { 0x0, "RAMWatt" },
371 { 0x0, "PKG_%" },
372 { 0x0, "RAM_%" },
373 { 0x0, "Pkg_J" },
374 { 0x0, "Cor_J" },
375 { 0x0, "GFX_J" },
376 { 0x0, "RAM_J" },
377 { 0x0, "Core" },
378 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500379 { 0x0, "Mod%c6" },
Len Brown812db3f2017-02-10 00:25:41 -0500380};
381
382#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
383#define BIC_Package (1ULL << 0)
384#define BIC_Avg_MHz (1ULL << 1)
385#define BIC_Bzy_MHz (1ULL << 2)
386#define BIC_TSC_MHz (1ULL << 3)
387#define BIC_IRQ (1ULL << 4)
388#define BIC_SMI (1ULL << 5)
389#define BIC_Busy (1ULL << 6)
390#define BIC_CPU_c1 (1ULL << 7)
391#define BIC_CPU_c3 (1ULL << 8)
392#define BIC_CPU_c6 (1ULL << 9)
393#define BIC_CPU_c7 (1ULL << 10)
394#define BIC_ThreadC (1ULL << 11)
395#define BIC_CoreTmp (1ULL << 12)
396#define BIC_CoreCnt (1ULL << 13)
397#define BIC_PkgTmp (1ULL << 14)
398#define BIC_GFX_rc6 (1ULL << 15)
399#define BIC_GFXMHz (1ULL << 16)
400#define BIC_Pkgpc2 (1ULL << 17)
401#define BIC_Pkgpc3 (1ULL << 18)
402#define BIC_Pkgpc6 (1ULL << 19)
403#define BIC_Pkgpc7 (1ULL << 20)
Len Brown0f47c082017-01-27 00:50:45 -0500404#define BIC_Pkgpc8 (1ULL << 21)
405#define BIC_Pkgpc9 (1ULL << 22)
406#define BIC_Pkgpc10 (1ULL << 23)
407#define BIC_PkgWatt (1ULL << 24)
408#define BIC_CorWatt (1ULL << 25)
409#define BIC_GFXWatt (1ULL << 26)
410#define BIC_PkgCnt (1ULL << 27)
411#define BIC_RAMWatt (1ULL << 28)
412#define BIC_PKG__ (1ULL << 29)
413#define BIC_RAM__ (1ULL << 30)
414#define BIC_Pkg_J (1ULL << 31)
415#define BIC_Cor_J (1ULL << 32)
416#define BIC_GFX_J (1ULL << 33)
417#define BIC_RAM_J (1ULL << 34)
418#define BIC_Core (1ULL << 35)
419#define BIC_CPU (1ULL << 36)
420#define BIC_Mod_c6 (1ULL << 37)
Len Brown812db3f2017-02-10 00:25:41 -0500421
422unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
423unsigned long long bic_present;
424
425#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
426#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500427#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500428
429/*
430 * bic_lookup
431 * for all the strings in comma separate name_list,
432 * set the approprate bit in return value.
433 */
434unsigned long long bic_lookup(char *name_list)
435{
436 int i;
437 unsigned long long retval = 0;
438
439 while (name_list) {
440 char *comma;
441
442 comma = strchr(name_list, ',');
443
444 if (comma)
445 *comma = '\0';
446
447 for (i = 0; i < MAX_BIC; ++i) {
448 if (!strcmp(name_list, bic[i].name)) {
449 retval |= (1ULL << i);
450 break;
451 }
452 }
453 if (i == MAX_BIC) {
454 fprintf(stderr, "Invalid counter name: %s\n", name_list);
455 exit(-1);
456 }
457
458 name_list = comma;
459 if (name_list)
460 name_list++;
461
462 }
463 return retval;
464}
Len Brownfc04cc62014-02-06 00:55:19 -0500465
Len Browna829eb42011-02-10 23:36:34 -0500466void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400467{
Len Brown388e9c82016-12-22 23:57:55 -0500468 struct msr_counter *mp;
469
Len Brown812db3f2017-02-10 00:25:41 -0500470 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400471 outp += sprintf(outp, "\tPackage");
Len Brown812db3f2017-02-10 00:25:41 -0500472 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400473 outp += sprintf(outp, "\tCore");
Len Brown812db3f2017-02-10 00:25:41 -0500474 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400475 outp += sprintf(outp, "\tCPU");
Len Brown812db3f2017-02-10 00:25:41 -0500476 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400477 outp += sprintf(outp, "\tAvg_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500478 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400479 outp += sprintf(outp, "\tBusy%%");
Len Brown812db3f2017-02-10 00:25:41 -0500480 if (DO_BIC(BIC_Bzy_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400481 outp += sprintf(outp, "\tBzy_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500482 if (DO_BIC(BIC_TSC_MHz))
483 outp += sprintf(outp, "\tTSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500484
Len Brown812db3f2017-02-10 00:25:41 -0500485 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400486 outp += sprintf(outp, "\tIRQ");
Len Brown812db3f2017-02-10 00:25:41 -0500487 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400488 outp += sprintf(outp, "\tSMI");
Len Brown1cc21f72015-02-23 00:34:57 -0500489
Len Brown812db3f2017-02-10 00:25:41 -0500490 if (DO_BIC(BIC_CPU_c1))
Len Brown3d109de2016-04-22 23:24:27 -0400491 outp += sprintf(outp, "\tCPU%%c1");
Len Brown889facb2012-11-08 00:48:57 -0500492
Len Brown388e9c82016-12-22 23:57:55 -0500493 for (mp = sys.tp; mp; mp = mp->next) {
494 if (mp->format == FORMAT_RAW) {
495 if (mp->width == 64)
496 outp += sprintf(outp, "\t%18.18s", mp->name);
497 else
498 outp += sprintf(outp, "\t%10.10s", mp->name);
499 } else {
500 outp += sprintf(outp, "\t%-7.7s", mp->name);
501 }
502 }
503
Len Brown812db3f2017-02-10 00:25:41 -0500504 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown678a3bd2017-02-09 22:22:13 -0500505 outp += sprintf(outp, "\tCPU%%c3");
Len Brown812db3f2017-02-10 00:25:41 -0500506 if (DO_BIC(BIC_CPU_c6))
Len Brown678a3bd2017-02-09 22:22:13 -0500507 outp += sprintf(outp, "\tCPU%%c6");
Len Brown812db3f2017-02-10 00:25:41 -0500508 if (DO_BIC(BIC_CPU_c7))
Len Brown678a3bd2017-02-09 22:22:13 -0500509 outp += sprintf(outp, "\tCPU%%c7");
510
Len Brown0539ba12017-02-10 00:27:20 -0500511 if (DO_BIC(BIC_Mod_c6))
512 outp += sprintf(outp, "\tMod%%c6");
Len Brown678a3bd2017-02-09 22:22:13 -0500513
Len Brown812db3f2017-02-10 00:25:41 -0500514 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400515 outp += sprintf(outp, "\tCoreTmp");
Len Brown388e9c82016-12-22 23:57:55 -0500516
517 for (mp = sys.cp; mp; mp = mp->next) {
518 if (mp->format == FORMAT_RAW) {
519 if (mp->width == 64)
520 outp += sprintf(outp, "\t%18.18s", mp->name);
521 else
522 outp += sprintf(outp, "\t%10.10s", mp->name);
523 } else {
524 outp += sprintf(outp, "\t%-7.7s", mp->name);
525 }
526 }
527
Len Brown812db3f2017-02-10 00:25:41 -0500528 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400529 outp += sprintf(outp, "\tPkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500530
Len Brown812db3f2017-02-10 00:25:41 -0500531 if (DO_BIC(BIC_GFX_rc6))
Len Brown3d109de2016-04-22 23:24:27 -0400532 outp += sprintf(outp, "\tGFX%%rc6");
Len Brownfdf676e2016-02-27 01:28:12 -0500533
Len Brown812db3f2017-02-10 00:25:41 -0500534 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400535 outp += sprintf(outp, "\tGFXMHz");
Len Brown27d47352016-02-27 00:37:54 -0500536
Len Brown0b2bb692015-03-26 00:50:30 -0400537 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400538 outp += sprintf(outp, "\tTotl%%C0");
539 outp += sprintf(outp, "\tAny%%C0");
540 outp += sprintf(outp, "\tGFX%%C0");
541 outp += sprintf(outp, "\tCPUGFX%%");
Len Brown0b2bb692015-03-26 00:50:30 -0400542 }
543
Len Brown0f47c082017-01-27 00:50:45 -0500544 if (DO_BIC(BIC_Pkgpc2))
Len Brown3d109de2016-04-22 23:24:27 -0400545 outp += sprintf(outp, "\tPkg%%pc2");
Len Brown0f47c082017-01-27 00:50:45 -0500546 if (DO_BIC(BIC_Pkgpc3))
Len Brown3d109de2016-04-22 23:24:27 -0400547 outp += sprintf(outp, "\tPkg%%pc3");
Len Brown0f47c082017-01-27 00:50:45 -0500548 if (DO_BIC(BIC_Pkgpc6))
Len Brown3d109de2016-04-22 23:24:27 -0400549 outp += sprintf(outp, "\tPkg%%pc6");
Len Brown0f47c082017-01-27 00:50:45 -0500550 if (DO_BIC(BIC_Pkgpc7))
Len Brown3d109de2016-04-22 23:24:27 -0400551 outp += sprintf(outp, "\tPkg%%pc7");
Len Brown0f47c082017-01-27 00:50:45 -0500552 if (DO_BIC(BIC_Pkgpc8))
Len Brown3d109de2016-04-22 23:24:27 -0400553 outp += sprintf(outp, "\tPkg%%pc8");
Len Brown0f47c082017-01-27 00:50:45 -0500554 if (DO_BIC(BIC_Pkgpc9))
Len Brown3d109de2016-04-22 23:24:27 -0400555 outp += sprintf(outp, "\tPkg%%pc9");
Len Brown0f47c082017-01-27 00:50:45 -0500556 if (DO_BIC(BIC_Pkgpc10))
Len Brown3d109de2016-04-22 23:24:27 -0400557 outp += sprintf(outp, "\tPk%%pc10");
Len Brown103a8fe2010-10-22 23:53:03 -0400558
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800559 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500560 if (DO_BIC(BIC_PkgWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400561 outp += sprintf(outp, "\tPkgWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500562 if (DO_BIC(BIC_CorWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400563 outp += sprintf(outp, "\tCorWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500564 if (DO_BIC(BIC_GFXWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400565 outp += sprintf(outp, "\tGFXWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500566 if (DO_BIC(BIC_RAMWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400567 outp += sprintf(outp, "\tRAMWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500568 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400569 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500570 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400571 outp += sprintf(outp, "\tRAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500572 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500573 if (DO_BIC(BIC_Pkg_J))
Len Brown3d109de2016-04-22 23:24:27 -0400574 outp += sprintf(outp, "\tPkg_J");
Len Brown812db3f2017-02-10 00:25:41 -0500575 if (DO_BIC(BIC_Cor_J))
Len Brown3d109de2016-04-22 23:24:27 -0400576 outp += sprintf(outp, "\tCor_J");
Len Brown812db3f2017-02-10 00:25:41 -0500577 if (DO_BIC(BIC_GFX_J))
Len Brown3d109de2016-04-22 23:24:27 -0400578 outp += sprintf(outp, "\tGFX_J");
Len Brown812db3f2017-02-10 00:25:41 -0500579 if (DO_BIC(BIC_RAM_J))
Len Brown3d109de2016-04-22 23:24:27 -0400580 outp += sprintf(outp, "\tRAM_J");
Len Brown812db3f2017-02-10 00:25:41 -0500581 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400582 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500583 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400584 outp += sprintf(outp, "\tRAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800585 }
Len Brown388e9c82016-12-22 23:57:55 -0500586 for (mp = sys.pp; mp; mp = mp->next) {
587 if (mp->format == FORMAT_RAW) {
588 if (mp->width == 64)
589 outp += sprintf(outp, "\t%18.18s", mp->name);
590 else
591 outp += sprintf(outp, "\t%10.10s", mp->name);
592 } else {
593 outp += sprintf(outp, "\t%-7.7s", mp->name);
594 }
595 }
596
Len Brownc98d5d92012-06-04 00:56:40 -0400597 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400598}
599
Len Brownc98d5d92012-06-04 00:56:40 -0400600int dump_counters(struct thread_data *t, struct core_data *c,
601 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400602{
Len Brown388e9c82016-12-22 23:57:55 -0500603 int i;
604 struct msr_counter *mp;
605
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200606 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400607
Len Brownc98d5d92012-06-04 00:56:40 -0400608 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200609 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
610 t->cpu_id, t->flags);
611 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
612 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
613 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
614 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500615
Len Brown812db3f2017-02-10 00:25:41 -0500616 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -0500617 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500618 if (DO_BIC(BIC_SMI))
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200619 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500620
621 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
622 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
623 i, mp->msr_num, t->counter[i]);
624 }
Len Brownc98d5d92012-06-04 00:56:40 -0400625 }
Len Brown103a8fe2010-10-22 23:53:03 -0400626
Len Brownc98d5d92012-06-04 00:56:40 -0400627 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200628 outp += sprintf(outp, "core: %d\n", c->core_id);
629 outp += sprintf(outp, "c3: %016llX\n", c->c3);
630 outp += sprintf(outp, "c6: %016llX\n", c->c6);
631 outp += sprintf(outp, "c7: %016llX\n", c->c7);
632 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500633
634 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
635 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
636 i, mp->msr_num, c->counter[i]);
637 }
Len Brown0539ba12017-02-10 00:27:20 -0500638 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400639 }
640
641 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200642 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400643
644 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
645 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
646 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
647 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
648
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200649 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500650 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500651 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500652 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500653 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500654 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500655 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200656 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
657 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
658 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
659 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
660 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
661 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
662 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
663 outp += sprintf(outp, "Throttle PKG: %0X\n",
664 p->rapl_pkg_perf_status);
665 outp += sprintf(outp, "Throttle RAM: %0X\n",
666 p->rapl_dram_perf_status);
667 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500668
669 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
670 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
671 i, mp->msr_num, p->counter[i]);
672 }
Len Brownc98d5d92012-06-04 00:56:40 -0400673 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200674
675 outp += sprintf(outp, "\n");
676
Len Brownc98d5d92012-06-04 00:56:40 -0400677 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400678}
679
Len Browne23da032012-02-06 18:37:16 -0500680/*
681 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500682 */
Len Brownc98d5d92012-06-04 00:56:40 -0400683int format_counters(struct thread_data *t, struct core_data *c,
684 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400685{
Len Brown008d396e2017-02-10 00:29:51 -0500686 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500687 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500688 int i;
689 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400690
Len Brownc98d5d92012-06-04 00:56:40 -0400691 /* if showing only 1st thread in core and this isn't one, bail out */
692 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
693 return 0;
694
695 /* if showing only 1st thread in pkg and this isn't one, bail out */
696 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
697 return 0;
698
Len Brown103a8fe2010-10-22 23:53:03 -0400699 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
700
Len Brown008d396e2017-02-10 00:29:51 -0500701 tsc = t->tsc * tsc_tweak;
702
Len Brownc98d5d92012-06-04 00:56:40 -0400703 /* topo columns, print blanks on 1st (average) line */
704 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500705 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400706 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500707 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400708 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500709 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400710 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400711 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500712 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400713 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400714 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400715 else
Len Brown3d109de2016-04-22 23:24:27 -0400716 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400717 }
Len Brown812db3f2017-02-10 00:25:41 -0500718 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400719 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400720 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400721 else
Len Brown3d109de2016-04-22 23:24:27 -0400722 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400723 }
Len Brown812db3f2017-02-10 00:25:41 -0500724 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400725 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400726 }
Len Brownfc04cc62014-02-06 00:55:19 -0500727
Len Brown812db3f2017-02-10 00:25:41 -0500728 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400729 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500730 1.0 / units * t->aperf / interval_float);
731
Len Brown812db3f2017-02-10 00:25:41 -0500732 if (DO_BIC(BIC_Busy))
Len Brown008d396e2017-02-10 00:29:51 -0500733 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400734
Len Brown812db3f2017-02-10 00:25:41 -0500735 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400736 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400737 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400738 else
Len Brown3d109de2016-04-22 23:24:27 -0400739 outp += sprintf(outp, "\t%.0f",
Len Brown008d396e2017-02-10 00:29:51 -0500740 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400741 }
Len Brown103a8fe2010-10-22 23:53:03 -0400742
Len Brown812db3f2017-02-10 00:25:41 -0500743 if (DO_BIC(BIC_TSC_MHz))
744 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400745
Len Brown562a2d32016-02-26 23:48:05 -0500746 /* IRQ */
Len Brown812db3f2017-02-10 00:25:41 -0500747 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400748 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500749
Len Brown1cc21f72015-02-23 00:34:57 -0500750 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500751 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400752 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500753
Len Brown678a3bd2017-02-09 22:22:13 -0500754 /* C1 */
Len Brown812db3f2017-02-10 00:25:41 -0500755 if (DO_BIC(BIC_CPU_c1))
Len Brown008d396e2017-02-10 00:29:51 -0500756 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400757
Len Brown678a3bd2017-02-09 22:22:13 -0500758 /* Added counters */
759 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
760 if (mp->format == FORMAT_RAW) {
761 if (mp->width == 32)
762 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
763 else
764 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
765 } else if (mp->format == FORMAT_DELTA) {
766 outp += sprintf(outp, "\t%lld", t->counter[i]);
767 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500768 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500769 }
770 }
771
Len Brownc98d5d92012-06-04 00:56:40 -0400772 /* print per-core data only for 1st thread in core */
773 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
774 goto done;
775
Len Brown812db3f2017-02-10 00:25:41 -0500776 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown008d396e2017-02-10 00:29:51 -0500777 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500778 if (DO_BIC(BIC_CPU_c6))
Len Brown008d396e2017-02-10 00:29:51 -0500779 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500780 if (DO_BIC(BIC_CPU_c7))
Len Brown008d396e2017-02-10 00:29:51 -0500781 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400782
Len Brown0539ba12017-02-10 00:27:20 -0500783 /* Mod%c6 */
784 if (DO_BIC(BIC_Mod_c6))
Len Brown008d396e2017-02-10 00:29:51 -0500785 outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500786
Len Brown812db3f2017-02-10 00:25:41 -0500787 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400788 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500789
Len Brown388e9c82016-12-22 23:57:55 -0500790 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
791 if (mp->format == FORMAT_RAW) {
792 if (mp->width == 32)
793 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
794 else
795 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
796 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500797 outp += sprintf(outp, "\t%lld", c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500798 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500799 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500800 }
801 }
802
Len Brownc98d5d92012-06-04 00:56:40 -0400803 /* print per-package data only for 1st core in package */
804 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
805 goto done;
806
Len Brown0b2bb692015-03-26 00:50:30 -0400807 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500808 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400809 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500810
Len Brownfdf676e2016-02-27 01:28:12 -0500811 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500812 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400813 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400814 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400815 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400816 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400817 p->gfx_rc6_ms / 10.0 / interval_float);
818 }
819 }
Len Brownfdf676e2016-02-27 01:28:12 -0500820
Len Brown27d47352016-02-27 00:37:54 -0500821 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500822 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400823 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500824
Len Brown0b2bb692015-03-26 00:50:30 -0400825 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
826 if (do_skl_residency) {
Len Brown008d396e2017-02-10 00:29:51 -0500827 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/tsc);
828 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/tsc);
829 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/tsc);
830 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400831 }
832
Len Brown0f47c082017-01-27 00:50:45 -0500833 if (DO_BIC(BIC_Pkgpc2))
Len Brown008d396e2017-02-10 00:29:51 -0500834 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500835 if (DO_BIC(BIC_Pkgpc3))
Len Brown008d396e2017-02-10 00:29:51 -0500836 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500837 if (DO_BIC(BIC_Pkgpc6))
Len Brown008d396e2017-02-10 00:29:51 -0500838 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500839 if (DO_BIC(BIC_Pkgpc7))
Len Brown008d396e2017-02-10 00:29:51 -0500840 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500841 if (DO_BIC(BIC_Pkgpc8))
Len Brown008d396e2017-02-10 00:29:51 -0500842 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500843 if (DO_BIC(BIC_Pkgpc9))
Len Brown008d396e2017-02-10 00:29:51 -0500844 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500845 if (DO_BIC(BIC_Pkgpc10))
Len Brown008d396e2017-02-10 00:29:51 -0500846 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500847
848 /*
849 * If measurement interval exceeds minimum RAPL Joule Counter range,
850 * indicate that results are suspect by printing "**" in fraction place.
851 */
Len Brownfc04cc62014-02-06 00:55:19 -0500852 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400853 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500854 else
Len Browne975db52016-04-06 23:56:02 -0400855 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500856
Len Brown812db3f2017-02-10 00:25:41 -0500857 if (DO_BIC(BIC_PkgWatt))
858 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
859 if (DO_BIC(BIC_CorWatt))
860 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
861 if (DO_BIC(BIC_GFXWatt))
862 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
863 if (DO_BIC(BIC_RAMWatt))
864 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
865 if (DO_BIC(BIC_Pkg_J))
866 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
867 if (DO_BIC(BIC_Cor_J))
868 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
869 if (DO_BIC(BIC_GFX_J))
870 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
871 if (DO_BIC(BIC_RAM_J))
872 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
873 if (DO_BIC(BIC_PKG__))
874 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
875 if (DO_BIC(BIC_RAM__))
876 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
877
Len Brown388e9c82016-12-22 23:57:55 -0500878 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
879 if (mp->format == FORMAT_RAW) {
880 if (mp->width == 32)
881 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
882 else
883 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
884 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500885 outp += sprintf(outp, "\t%lld", p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500886 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500887 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500888 }
889 }
890
Len Brownc98d5d92012-06-04 00:56:40 -0400891done:
Len Brownc98d5d92012-06-04 00:56:40 -0400892 outp += sprintf(outp, "\n");
893
894 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400895}
896
Len Brownb7d8c142016-02-13 23:36:17 -0500897void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400898{
Len Brownb7d8c142016-02-13 23:36:17 -0500899 FILE *filep;
900
901 if (outf == stderr)
902 filep = stdout;
903 else
904 filep = outf;
905
906 fputs(output_buffer, filep);
907 fflush(filep);
908
Len Brownc98d5d92012-06-04 00:56:40 -0400909 outp = output_buffer;
910}
Len Brownb7d8c142016-02-13 23:36:17 -0500911void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400912{
Len Brownb7d8c142016-02-13 23:36:17 -0500913 fputs(output_buffer, outf);
914 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400915 outp = output_buffer;
916}
917void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
918{
Len Browne23da032012-02-06 18:37:16 -0500919 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400920
Len Browne23da032012-02-06 18:37:16 -0500921 if (!printed || !summary_only)
922 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400923
Len Brownc98d5d92012-06-04 00:56:40 -0400924 if (topo.num_cpus > 1)
925 format_counters(&average.threads, &average.cores,
926 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400927
Len Browne23da032012-02-06 18:37:16 -0500928 printed = 1;
929
930 if (summary_only)
931 return;
932
Len Brownc98d5d92012-06-04 00:56:40 -0400933 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400934}
935
Len Brown889facb2012-11-08 00:48:57 -0500936#define DELTA_WRAP32(new, old) \
937 if (new > old) { \
938 old = new - old; \
939 } else { \
940 old = 0x100000000 + new - old; \
941 }
942
Len Brownba3dec92016-04-22 20:31:46 -0400943int
Len Brownc98d5d92012-06-04 00:56:40 -0400944delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400945{
Len Brown388e9c82016-12-22 23:57:55 -0500946 int i;
947 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400948
949 if (do_skl_residency) {
950 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
951 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
952 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
953 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
954 }
Len Brownc98d5d92012-06-04 00:56:40 -0400955 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -0500956 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500957 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -0500958 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500959 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -0500960 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500961 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800962 old->pc8 = new->pc8 - old->pc8;
963 old->pc9 = new->pc9 - old->pc9;
964 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500965 old->pkg_temp_c = new->pkg_temp_c;
966
Len Brown9185e982016-04-06 17:16:00 -0400967 /* flag an error when rc6 counter resets/wraps */
968 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
969 old->gfx_rc6_ms = -1;
970 else
971 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
972
Len Brown27d47352016-02-27 00:37:54 -0500973 old->gfx_mhz = new->gfx_mhz;
974
Len Brown889facb2012-11-08 00:48:57 -0500975 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
976 DELTA_WRAP32(new->energy_cores, old->energy_cores);
977 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
978 DELTA_WRAP32(new->energy_dram, old->energy_dram);
979 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
980 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400981
Len Brown388e9c82016-12-22 23:57:55 -0500982 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
983 if (mp->format == FORMAT_RAW)
984 old->counter[i] = new->counter[i];
985 else
986 old->counter[i] = new->counter[i] - old->counter[i];
987 }
988
Len Brownba3dec92016-04-22 20:31:46 -0400989 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400990}
Len Brown103a8fe2010-10-22 23:53:03 -0400991
Len Brownc98d5d92012-06-04 00:56:40 -0400992void
993delta_core(struct core_data *new, struct core_data *old)
994{
Len Brown388e9c82016-12-22 23:57:55 -0500995 int i;
996 struct msr_counter *mp;
997
Len Brownc98d5d92012-06-04 00:56:40 -0400998 old->c3 = new->c3 - old->c3;
999 old->c6 = new->c6 - old->c6;
1000 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001001 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001002 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001003
1004 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1005 if (mp->format == FORMAT_RAW)
1006 old->counter[i] = new->counter[i];
1007 else
1008 old->counter[i] = new->counter[i] - old->counter[i];
1009 }
Len Brownc98d5d92012-06-04 00:56:40 -04001010}
Len Brown103a8fe2010-10-22 23:53:03 -04001011
Len Brownc3ae3312012-06-13 21:31:46 -04001012/*
1013 * old = new - old
1014 */
Len Brownba3dec92016-04-22 20:31:46 -04001015int
Len Brownc98d5d92012-06-04 00:56:40 -04001016delta_thread(struct thread_data *new, struct thread_data *old,
1017 struct core_data *core_delta)
1018{
Len Brown388e9c82016-12-22 23:57:55 -05001019 int i;
1020 struct msr_counter *mp;
1021
Len Brownc98d5d92012-06-04 00:56:40 -04001022 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001023
Len Brownc98d5d92012-06-04 00:56:40 -04001024 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001025 if (old->tsc < (1000 * 1000))
1026 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1027 "You can disable all c-states by booting with \"idle=poll\"\n"
1028 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001029
Len Brownc98d5d92012-06-04 00:56:40 -04001030 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001031
Len Brown812db3f2017-02-10 00:25:41 -05001032 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001033 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1034 old->aperf = new->aperf - old->aperf;
1035 old->mperf = new->mperf - old->mperf;
1036 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001037 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001038 }
Len Brownc98d5d92012-06-04 00:56:40 -04001039 }
Len Brown103a8fe2010-10-22 23:53:03 -04001040
Len Brown103a8fe2010-10-22 23:53:03 -04001041
Len Brown144b44b2013-11-09 00:30:16 -05001042 if (use_c1_residency_msr) {
1043 /*
1044 * Some models have a dedicated C1 residency MSR,
1045 * which should be more accurate than the derivation below.
1046 */
1047 } else {
1048 /*
1049 * As counter collection is not atomic,
1050 * it is possible for mperf's non-halted cycles + idle states
1051 * to exceed TSC's all cycles: show c1 = 0% in that case.
1052 */
1053 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1054 old->c1 = 0;
1055 else {
1056 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001057 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001058 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001059 }
Len Brownc98d5d92012-06-04 00:56:40 -04001060 }
Len Brownc3ae3312012-06-13 21:31:46 -04001061
Len Brownc98d5d92012-06-04 00:56:40 -04001062 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001063 if (debug > 1)
1064 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001065 old->mperf = 1; /* divide by 0 protection */
1066 }
1067
Len Brown812db3f2017-02-10 00:25:41 -05001068 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001069 old->irq_count = new->irq_count - old->irq_count;
1070
Len Brown812db3f2017-02-10 00:25:41 -05001071 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001072 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001073
Len Brown388e9c82016-12-22 23:57:55 -05001074 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1075 if (mp->format == FORMAT_RAW)
1076 old->counter[i] = new->counter[i];
1077 else
1078 old->counter[i] = new->counter[i] - old->counter[i];
1079 }
Len Brownba3dec92016-04-22 20:31:46 -04001080 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001081}
1082
1083int delta_cpu(struct thread_data *t, struct core_data *c,
1084 struct pkg_data *p, struct thread_data *t2,
1085 struct core_data *c2, struct pkg_data *p2)
1086{
Len Brownba3dec92016-04-22 20:31:46 -04001087 int retval = 0;
1088
Len Brownc98d5d92012-06-04 00:56:40 -04001089 /* calculate core delta only for 1st thread in core */
1090 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1091 delta_core(c, c2);
1092
1093 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001094 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1095 if (retval)
1096 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001097
1098 /* calculate package delta only for 1st core in package */
1099 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001100 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001101
Len Brownba3dec92016-04-22 20:31:46 -04001102 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001103}
1104
1105void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1106{
Len Brown388e9c82016-12-22 23:57:55 -05001107 int i;
1108 struct msr_counter *mp;
1109
Len Brownc98d5d92012-06-04 00:56:40 -04001110 t->tsc = 0;
1111 t->aperf = 0;
1112 t->mperf = 0;
1113 t->c1 = 0;
1114
Len Brown562a2d32016-02-26 23:48:05 -05001115 t->irq_count = 0;
1116 t->smi_count = 0;
1117
Len Brownc98d5d92012-06-04 00:56:40 -04001118 /* tells format_counters to dump all fields from this set */
1119 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1120
1121 c->c3 = 0;
1122 c->c6 = 0;
1123 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001124 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001125 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001126
Len Brown0b2bb692015-03-26 00:50:30 -04001127 p->pkg_wtd_core_c0 = 0;
1128 p->pkg_any_core_c0 = 0;
1129 p->pkg_any_gfxe_c0 = 0;
1130 p->pkg_both_core_gfxe_c0 = 0;
1131
Len Brownc98d5d92012-06-04 00:56:40 -04001132 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001133 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001134 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001135 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001136 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001137 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001138 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001139 p->pc8 = 0;
1140 p->pc9 = 0;
1141 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001142
1143 p->energy_pkg = 0;
1144 p->energy_dram = 0;
1145 p->energy_cores = 0;
1146 p->energy_gfx = 0;
1147 p->rapl_pkg_perf_status = 0;
1148 p->rapl_dram_perf_status = 0;
1149 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001150
Len Brownfdf676e2016-02-27 01:28:12 -05001151 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001152 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001153 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1154 t->counter[i] = 0;
1155
1156 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1157 c->counter[i] = 0;
1158
1159 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1160 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001161}
1162int sum_counters(struct thread_data *t, struct core_data *c,
1163 struct pkg_data *p)
1164{
Len Brown388e9c82016-12-22 23:57:55 -05001165 int i;
1166 struct msr_counter *mp;
1167
Len Brownc98d5d92012-06-04 00:56:40 -04001168 average.threads.tsc += t->tsc;
1169 average.threads.aperf += t->aperf;
1170 average.threads.mperf += t->mperf;
1171 average.threads.c1 += t->c1;
1172
Len Brown562a2d32016-02-26 23:48:05 -05001173 average.threads.irq_count += t->irq_count;
1174 average.threads.smi_count += t->smi_count;
1175
Len Brown388e9c82016-12-22 23:57:55 -05001176 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1177 if (mp->format == FORMAT_RAW)
1178 continue;
1179 average.threads.counter[i] += t->counter[i];
1180 }
1181
Len Brownc98d5d92012-06-04 00:56:40 -04001182 /* sum per-core values only for 1st thread in core */
1183 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1184 return 0;
1185
1186 average.cores.c3 += c->c3;
1187 average.cores.c6 += c->c6;
1188 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001189 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001190
Len Brown889facb2012-11-08 00:48:57 -05001191 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1192
Len Brown388e9c82016-12-22 23:57:55 -05001193 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1194 if (mp->format == FORMAT_RAW)
1195 continue;
1196 average.cores.counter[i] += c->counter[i];
1197 }
1198
Len Brownc98d5d92012-06-04 00:56:40 -04001199 /* sum per-pkg values only for 1st core in pkg */
1200 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1201 return 0;
1202
Len Brown0b2bb692015-03-26 00:50:30 -04001203 if (do_skl_residency) {
1204 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1205 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1206 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1207 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1208 }
1209
Len Brownc98d5d92012-06-04 00:56:40 -04001210 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001211 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001212 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001213 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001214 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001215 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001216 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001217 average.packages.pc8 += p->pc8;
1218 average.packages.pc9 += p->pc9;
1219 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001220
Len Brown889facb2012-11-08 00:48:57 -05001221 average.packages.energy_pkg += p->energy_pkg;
1222 average.packages.energy_dram += p->energy_dram;
1223 average.packages.energy_cores += p->energy_cores;
1224 average.packages.energy_gfx += p->energy_gfx;
1225
Len Brownfdf676e2016-02-27 01:28:12 -05001226 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001227 average.packages.gfx_mhz = p->gfx_mhz;
1228
Len Brown889facb2012-11-08 00:48:57 -05001229 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1230
1231 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1232 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001233
1234 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1235 if (mp->format == FORMAT_RAW)
1236 continue;
1237 average.packages.counter[i] += p->counter[i];
1238 }
Len Brownc98d5d92012-06-04 00:56:40 -04001239 return 0;
1240}
1241/*
1242 * sum the counters for all cpus in the system
1243 * compute the weighted average
1244 */
1245void compute_average(struct thread_data *t, struct core_data *c,
1246 struct pkg_data *p)
1247{
Len Brown388e9c82016-12-22 23:57:55 -05001248 int i;
1249 struct msr_counter *mp;
1250
Len Brownc98d5d92012-06-04 00:56:40 -04001251 clear_counters(&average.threads, &average.cores, &average.packages);
1252
1253 for_all_cpus(sum_counters, t, c, p);
1254
1255 average.threads.tsc /= topo.num_cpus;
1256 average.threads.aperf /= topo.num_cpus;
1257 average.threads.mperf /= topo.num_cpus;
1258 average.threads.c1 /= topo.num_cpus;
1259
1260 average.cores.c3 /= topo.num_cores;
1261 average.cores.c6 /= topo.num_cores;
1262 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001263 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001264
Len Brown0b2bb692015-03-26 00:50:30 -04001265 if (do_skl_residency) {
1266 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1267 average.packages.pkg_any_core_c0 /= topo.num_packages;
1268 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1269 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1270 }
1271
Len Brownc98d5d92012-06-04 00:56:40 -04001272 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001273 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001274 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001275 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001276 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001277 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001278 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001279
1280 average.packages.pc8 /= topo.num_packages;
1281 average.packages.pc9 /= topo.num_packages;
1282 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001283
1284 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1285 if (mp->format == FORMAT_RAW)
1286 continue;
1287 average.threads.counter[i] /= topo.num_cpus;
1288 }
1289 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1290 if (mp->format == FORMAT_RAW)
1291 continue;
1292 average.cores.counter[i] /= topo.num_cores;
1293 }
1294 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1295 if (mp->format == FORMAT_RAW)
1296 continue;
1297 average.packages.counter[i] /= topo.num_packages;
1298 }
Len Brownc98d5d92012-06-04 00:56:40 -04001299}
1300
1301static unsigned long long rdtsc(void)
1302{
1303 unsigned int low, high;
1304
1305 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1306
1307 return low | ((unsigned long long)high) << 32;
1308}
1309
Len Brownc98d5d92012-06-04 00:56:40 -04001310/*
1311 * get_counters(...)
1312 * migrate to cpu
1313 * acquire and record local counters for that cpu
1314 */
1315int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1316{
1317 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001318 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001319 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001320 struct msr_counter *mp;
1321 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001322
Len Browne52966c2012-11-08 22:38:05 -05001323 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001324 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001325 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001326 }
Len Brownc98d5d92012-06-04 00:56:40 -04001327
Len Brown0102b062016-02-27 03:11:29 -05001328retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001329 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1330
Len Brown812db3f2017-02-10 00:25:41 -05001331 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001332 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1333
1334 /*
1335 * The TSC, APERF and MPERF must be read together for
1336 * APERF/MPERF and MPERF/TSC to give accurate results.
1337 *
1338 * Unfortunately, APERF and MPERF are read by
1339 * individual system call, so delays may occur
1340 * between them. If the time to read them
1341 * varies by a large amount, we re-read them.
1342 */
1343
1344 /*
1345 * This initial dummy APERF read has been seen to
1346 * reduce jitter in the subsequent reads.
1347 */
1348
Len Brown9c63a652012-10-31 01:29:52 -04001349 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001350 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001351
1352 t->tsc = rdtsc(); /* re-read close to APERF */
1353
1354 tsc_before = t->tsc;
1355
1356 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1357 return -3;
1358
1359 tsc_between = rdtsc();
1360
Len Brown9c63a652012-10-31 01:29:52 -04001361 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001362 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001363
1364 tsc_after = rdtsc();
1365
1366 aperf_time = tsc_between - tsc_before;
1367 mperf_time = tsc_after - tsc_between;
1368
1369 /*
1370 * If the system call latency to read APERF and MPERF
1371 * differ by more than 2x, then try again.
1372 */
1373 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1374 aperf_mperf_retry_count++;
1375 if (aperf_mperf_retry_count < 5)
1376 goto retry;
1377 else
1378 warnx("cpu%d jitter %lld %lld",
1379 cpu, aperf_time, mperf_time);
1380 }
1381 aperf_mperf_retry_count = 0;
1382
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001383 t->aperf = t->aperf * aperf_mperf_multiplier;
1384 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001385 }
1386
Len Brown812db3f2017-02-10 00:25:41 -05001387 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001388 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001389 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001390 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1391 return -5;
1392 t->smi_count = msr & 0xFFFFFFFF;
1393 }
Len Brown0539ba12017-02-10 00:27:20 -05001394 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001395 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1396 return -6;
1397 }
1398
Len Brown388e9c82016-12-22 23:57:55 -05001399 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1400 if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1401 return -10;
1402 }
1403
1404
Len Brownc98d5d92012-06-04 00:56:40 -04001405 /* collect core counters only for 1st thread in core */
1406 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1407 return 0;
1408
Len Brown812db3f2017-02-10 00:25:41 -05001409 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001410 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1411 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001412 }
1413
Len Brown812db3f2017-02-10 00:25:41 -05001414 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001415 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1416 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001417 } else if (do_knl_cstates) {
1418 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1419 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001420 }
1421
Len Brown812db3f2017-02-10 00:25:41 -05001422 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001423 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1424 return -8;
1425
Len Brown0539ba12017-02-10 00:27:20 -05001426 if (DO_BIC(BIC_Mod_c6))
1427 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1428 return -8;
1429
Len Brown812db3f2017-02-10 00:25:41 -05001430 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001431 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1432 return -9;
1433 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1434 }
1435
Len Brown388e9c82016-12-22 23:57:55 -05001436 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1437 if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1438 return -10;
1439 }
Len Brown889facb2012-11-08 00:48:57 -05001440
Len Brownc98d5d92012-06-04 00:56:40 -04001441 /* collect package counters only for 1st core in package */
1442 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1443 return 0;
1444
Len Brown0b2bb692015-03-26 00:50:30 -04001445 if (do_skl_residency) {
1446 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1447 return -10;
1448 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1449 return -11;
1450 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1451 return -12;
1452 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1453 return -13;
1454 }
Len Brown0f47c082017-01-27 00:50:45 -05001455 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001456 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1457 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001458 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001459 if (do_slm_cstates) {
1460 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1461 return -10;
1462 } else {
1463 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1464 return -10;
1465 }
1466 }
1467
Len Brown0f47c082017-01-27 00:50:45 -05001468 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001469 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1470 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001471 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001472 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1473 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001474 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001475 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1476 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001477 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001478 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1479 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001480 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001481 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1482 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001483
Len Brown889facb2012-11-08 00:48:57 -05001484 if (do_rapl & RAPL_PKG) {
1485 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1486 return -13;
1487 p->energy_pkg = msr & 0xFFFFFFFF;
1488 }
Jacob Pan91484942016-06-16 09:48:20 -07001489 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001490 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1491 return -14;
1492 p->energy_cores = msr & 0xFFFFFFFF;
1493 }
1494 if (do_rapl & RAPL_DRAM) {
1495 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1496 return -15;
1497 p->energy_dram = msr & 0xFFFFFFFF;
1498 }
1499 if (do_rapl & RAPL_GFX) {
1500 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1501 return -16;
1502 p->energy_gfx = msr & 0xFFFFFFFF;
1503 }
1504 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1505 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1506 return -16;
1507 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1508 }
1509 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1510 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1511 return -16;
1512 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1513 }
Len Brown812db3f2017-02-10 00:25:41 -05001514 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001515 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1516 return -17;
1517 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1518 }
Len Brownfdf676e2016-02-27 01:28:12 -05001519
Len Brown812db3f2017-02-10 00:25:41 -05001520 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001521 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1522
Len Brown812db3f2017-02-10 00:25:41 -05001523 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001524 p->gfx_mhz = gfx_cur_mhz;
1525
Len Brown388e9c82016-12-22 23:57:55 -05001526 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1527 if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1528 return -10;
1529 }
1530
Len Brown103a8fe2010-10-22 23:53:03 -04001531 return 0;
1532}
1533
Len Brownee7e38e2015-02-09 23:39:45 -05001534/*
1535 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1536 * If you change the values, note they are used both in comparisons
1537 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1538 */
1539
1540#define PCLUKN 0 /* Unknown */
1541#define PCLRSV 1 /* Reserved */
1542#define PCL__0 2 /* PC0 */
1543#define PCL__1 3 /* PC1 */
1544#define PCL__2 4 /* PC2 */
1545#define PCL__3 5 /* PC3 */
1546#define PCL__4 6 /* PC4 */
1547#define PCL__6 7 /* PC6 */
1548#define PCL_6N 8 /* PC6 No Retention */
1549#define PCL_6R 9 /* PC6 Retention */
1550#define PCL__7 10 /* PC7 */
1551#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001552#define PCL__8 12 /* PC8 */
1553#define PCL__9 13 /* PC9 */
1554#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001555
1556int pkg_cstate_limit = PCLUKN;
1557char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001558 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001559
Len Browne9257f52015-04-01 21:02:57 -04001560int 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};
1561int 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};
1562int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brown0539ba12017-02-10 00:27:20 -05001563int 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 -05001564int 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 -04001565int 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 -04001566int 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 -05001567int 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 -05001568
Len Browna2b7b742015-09-26 00:12:38 -04001569
1570static void
1571calculate_tsc_tweak()
1572{
Len Browna2b7b742015-09-26 00:12:38 -04001573 tsc_tweak = base_hz / tsc_hz;
1574}
1575
Len Brownfcd17212015-03-23 20:29:09 -04001576static void
1577dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001578{
1579 unsigned long long msr;
1580 unsigned int ratio;
1581
Len Brownec0adc52015-11-12 02:42:31 -05001582 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001583
Len Brownb7d8c142016-02-13 23:36:17 -05001584 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001585
Len Brown103a8fe2010-10-22 23:53:03 -04001586 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001587 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001588 ratio, bclk, ratio * bclk);
1589
1590 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001591 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001592 ratio, bclk, ratio * bclk);
1593
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001594 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001595 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001596 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001597
Len Brownfcd17212015-03-23 20:29:09 -04001598 return;
1599}
1600
1601static void
1602dump_hsw_turbo_ratio_limits(void)
1603{
1604 unsigned long long msr;
1605 unsigned int ratio;
1606
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001607 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001608
Len Brownb7d8c142016-02-13 23:36:17 -05001609 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001610
1611 ratio = (msr >> 8) & 0xFF;
1612 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001613 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001614 ratio, bclk, ratio * bclk);
1615
1616 ratio = (msr >> 0) & 0xFF;
1617 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001618 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001619 ratio, bclk, ratio * bclk);
1620 return;
1621}
1622
1623static void
1624dump_ivt_turbo_ratio_limits(void)
1625{
1626 unsigned long long msr;
1627 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001628
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001629 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001630
Len Brownb7d8c142016-02-13 23:36:17 -05001631 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001632
1633 ratio = (msr >> 56) & 0xFF;
1634 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001635 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001636 ratio, bclk, ratio * bclk);
1637
1638 ratio = (msr >> 48) & 0xFF;
1639 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001640 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001641 ratio, bclk, ratio * bclk);
1642
1643 ratio = (msr >> 40) & 0xFF;
1644 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001645 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001646 ratio, bclk, ratio * bclk);
1647
1648 ratio = (msr >> 32) & 0xFF;
1649 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001650 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001651 ratio, bclk, ratio * bclk);
1652
1653 ratio = (msr >> 24) & 0xFF;
1654 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001655 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001656 ratio, bclk, ratio * bclk);
1657
1658 ratio = (msr >> 16) & 0xFF;
1659 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001660 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001661 ratio, bclk, ratio * bclk);
1662
1663 ratio = (msr >> 8) & 0xFF;
1664 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001665 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001666 ratio, bclk, ratio * bclk);
1667
1668 ratio = (msr >> 0) & 0xFF;
1669 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001670 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001671 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001672 return;
1673}
Len Brown6574a5d2012-09-21 00:01:31 -04001674
Len Brownfcd17212015-03-23 20:29:09 -04001675static void
1676dump_nhm_turbo_ratio_limits(void)
1677{
1678 unsigned long long msr;
1679 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001680
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001681 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001682
Len Brownb7d8c142016-02-13 23:36:17 -05001683 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001684
1685 ratio = (msr >> 56) & 0xFF;
1686 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001687 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 8 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001688 ratio, bclk, ratio * bclk);
1689
1690 ratio = (msr >> 48) & 0xFF;
1691 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001692 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 7 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001693 ratio, bclk, ratio * bclk);
1694
1695 ratio = (msr >> 40) & 0xFF;
1696 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001697 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 6 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001698 ratio, bclk, ratio * bclk);
1699
1700 ratio = (msr >> 32) & 0xFF;
1701 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001702 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 5 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001703 ratio, bclk, ratio * bclk);
1704
Len Brown103a8fe2010-10-22 23:53:03 -04001705 ratio = (msr >> 24) & 0xFF;
1706 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001707 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001708 ratio, bclk, ratio * bclk);
1709
1710 ratio = (msr >> 16) & 0xFF;
1711 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001712 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001713 ratio, bclk, ratio * bclk);
1714
1715 ratio = (msr >> 8) & 0xFF;
1716 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001717 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001718 ratio, bclk, ratio * bclk);
1719
1720 ratio = (msr >> 0) & 0xFF;
1721 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001722 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001723 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001724 return;
1725}
Len Brown3a9a9412014-08-15 02:39:52 -04001726
Len Brownfcd17212015-03-23 20:29:09 -04001727static void
Len Brown0f7887c2017-01-12 23:49:18 -05001728dump_atom_turbo_ratio_limits(void)
1729{
1730 unsigned long long msr;
1731 unsigned int ratio;
1732
1733 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1734 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1735
1736 ratio = (msr >> 0) & 0x3F;
1737 if (ratio)
1738 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1739 ratio, bclk, ratio * bclk);
1740
1741 ratio = (msr >> 8) & 0x3F;
1742 if (ratio)
1743 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1744 ratio, bclk, ratio * bclk);
1745
1746 ratio = (msr >> 16) & 0x3F;
1747 if (ratio)
1748 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1749 ratio, bclk, ratio * bclk);
1750
1751 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1752 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1753
1754 ratio = (msr >> 24) & 0x3F;
1755 if (ratio)
1756 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1757 ratio, bclk, ratio * bclk);
1758
1759 ratio = (msr >> 16) & 0x3F;
1760 if (ratio)
1761 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1762 ratio, bclk, ratio * bclk);
1763
1764 ratio = (msr >> 8) & 0x3F;
1765 if (ratio)
1766 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1767 ratio, bclk, ratio * bclk);
1768
1769 ratio = (msr >> 0) & 0x3F;
1770 if (ratio)
1771 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1772 ratio, bclk, ratio * bclk);
1773}
1774
1775static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001776dump_knl_turbo_ratio_limits(void)
1777{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001778 const unsigned int buckets_no = 7;
1779
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001780 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001781 int delta_cores, delta_ratio;
1782 int i, b_nr;
1783 unsigned int cores[buckets_no];
1784 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001785
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001786 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001787
Len Brownb7d8c142016-02-13 23:36:17 -05001788 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001789 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001790
1791 /**
1792 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001793 * [0] -- Reserved
1794 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001795 * [15:8] -- Base value of freq ratio of bucket 1.
1796 * [20:16] -- +ve delta of number of active cores of bucket 2.
1797 * i.e. active cores of bucket 2 =
1798 * active cores of bucket 1 + delta
1799 * [23:21] -- Negative delta of freq ratio of bucket 2.
1800 * i.e. freq ratio of bucket 2 =
1801 * freq ratio of bucket 1 - delta
1802 * [28:24]-- +ve delta of number of active cores of bucket 3.
1803 * [31:29]-- -ve delta of freq ratio of bucket 3.
1804 * [36:32]-- +ve delta of number of active cores of bucket 4.
1805 * [39:37]-- -ve delta of freq ratio of bucket 4.
1806 * [44:40]-- +ve delta of number of active cores of bucket 5.
1807 * [47:45]-- -ve delta of freq ratio of bucket 5.
1808 * [52:48]-- +ve delta of number of active cores of bucket 6.
1809 * [55:53]-- -ve delta of freq ratio of bucket 6.
1810 * [60:56]-- +ve delta of number of active cores of bucket 7.
1811 * [63:61]-- -ve delta of freq ratio of bucket 7.
1812 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001813
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001814 b_nr = 0;
1815 cores[b_nr] = (msr & 0xFF) >> 1;
1816 ratio[b_nr] = (msr >> 8) & 0xFF;
1817
1818 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001819 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001820 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001821
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001822 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1823 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1824 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001825 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001826
1827 for (i = buckets_no - 1; i >= 0; i--)
1828 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001829 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05001830 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001831 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001832}
1833
1834static void
Len Brownfcd17212015-03-23 20:29:09 -04001835dump_nhm_cst_cfg(void)
1836{
1837 unsigned long long msr;
1838
Len Brown1df2e552017-01-07 23:24:57 -05001839 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001840
1841#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1842#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1843
Len Brown1df2e552017-01-07 23:24:57 -05001844 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001845
Len Brownb7d8c142016-02-13 23:36:17 -05001846 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001847 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1848 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1849 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1850 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1851 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001852 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001853 pkg_cstate_limit_strings[pkg_cstate_limit]);
1854 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001855}
1856
Len Brown6fb31432015-06-17 16:23:45 -04001857static void
1858dump_config_tdp(void)
1859{
1860 unsigned long long msr;
1861
1862 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001863 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001864 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001865
1866 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001867 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001868 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001869 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1870 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1871 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1872 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001873 }
Len Brownb7d8c142016-02-13 23:36:17 -05001874 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001875
1876 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001877 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001878 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001879 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1880 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1881 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1882 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001883 }
Len Brownb7d8c142016-02-13 23:36:17 -05001884 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001885
1886 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001887 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001888 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001889 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1890 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1891 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001892
Len Brown6fb31432015-06-17 16:23:45 -04001893 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001894 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001895 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001896 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1897 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001898}
Len Brown5a634262016-04-06 17:15:55 -04001899
1900unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1901
1902void print_irtl(void)
1903{
1904 unsigned long long msr;
1905
1906 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1907 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1908 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1909 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1910
1911 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
1912 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
1913 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1914 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1915
1916 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
1917 fprintf(outf, "cpu%d: MSR_PKGC7_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 if (!do_irtl_hsw)
1922 return;
1923
1924 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
1925 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
1926 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1927 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1928
1929 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
1930 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
1931 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1932 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1933
1934 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
1935 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
1936 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1937 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1938
1939}
Len Brown36229892016-02-26 20:51:02 -05001940void free_fd_percpu(void)
1941{
1942 int i;
1943
Mika Westerberg01a67ad2016-04-22 11:13:23 +03001944 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05001945 if (fd_percpu[i] != 0)
1946 close(fd_percpu[i]);
1947 }
1948
1949 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04001950}
1951
Len Brownc98d5d92012-06-04 00:56:40 -04001952void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001953{
Len Brownc98d5d92012-06-04 00:56:40 -04001954 CPU_FREE(cpu_present_set);
1955 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05001956 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001957
Len Brownc98d5d92012-06-04 00:56:40 -04001958 CPU_FREE(cpu_affinity_set);
1959 cpu_affinity_set = NULL;
1960 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001961
Len Brownc98d5d92012-06-04 00:56:40 -04001962 free(thread_even);
1963 free(core_even);
1964 free(package_even);
1965
1966 thread_even = NULL;
1967 core_even = NULL;
1968 package_even = NULL;
1969
1970 free(thread_odd);
1971 free(core_odd);
1972 free(package_odd);
1973
1974 thread_odd = NULL;
1975 core_odd = NULL;
1976 package_odd = NULL;
1977
1978 free(output_buffer);
1979 output_buffer = NULL;
1980 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05001981
1982 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05001983
1984 free(irq_column_2_cpu);
1985 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001986}
1987
Len Brownc98d5d92012-06-04 00:56:40 -04001988/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001989 * Open a file, and exit on failure
1990 */
1991FILE *fopen_or_die(const char *path, const char *mode)
1992{
Len Brownb7d8c142016-02-13 23:36:17 -05001993 FILE *filep = fopen(path, mode);
Josh Triplettb2c95d92013-08-20 17:20:18 -07001994 if (!filep)
1995 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001996 return filep;
1997}
1998
1999/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002000 * Parse a file containing a single int.
2001 */
2002int parse_int_file(const char *fmt, ...)
2003{
2004 va_list args;
2005 char path[PATH_MAX];
2006 FILE *filep;
2007 int value;
2008
2009 va_start(args, fmt);
2010 vsnprintf(path, sizeof(path), fmt, args);
2011 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002012 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002013 if (fscanf(filep, "%d", &value) != 1)
2014 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002015 fclose(filep);
2016 return value;
2017}
2018
2019/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002020 * get_cpu_position_in_core(cpu)
2021 * return the position of the CPU among its HT siblings in the core
2022 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002023 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002024int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002025{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002026 char path[64];
2027 FILE *filep;
2028 int this_cpu;
2029 char character;
2030 int i;
2031
2032 sprintf(path,
2033 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2034 cpu);
2035 filep = fopen(path, "r");
2036 if (filep == NULL) {
2037 perror(path);
2038 exit(1);
2039 }
2040
2041 for (i = 0; i < topo.num_threads_per_core; i++) {
2042 fscanf(filep, "%d", &this_cpu);
2043 if (this_cpu == cpu) {
2044 fclose(filep);
2045 return i;
2046 }
2047
2048 /* Account for no separator after last thread*/
2049 if (i != (topo.num_threads_per_core - 1))
2050 fscanf(filep, "%c", &character);
2051 }
2052
2053 fclose(filep);
2054 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002055}
2056
Len Brownc98d5d92012-06-04 00:56:40 -04002057/*
2058 * cpu_is_first_core_in_package(cpu)
2059 * return 1 if given CPU is 1st core in package
2060 */
2061int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002062{
Josh Triplett95aebc42013-08-20 17:20:16 -07002063 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002064}
2065
2066int get_physical_package_id(int cpu)
2067{
Josh Triplett95aebc42013-08-20 17:20:16 -07002068 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002069}
2070
2071int get_core_id(int cpu)
2072{
Josh Triplett95aebc42013-08-20 17:20:16 -07002073 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002074}
2075
Len Brownc98d5d92012-06-04 00:56:40 -04002076int get_num_ht_siblings(int cpu)
2077{
2078 char path[80];
2079 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002080 int sib1;
2081 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002082 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002083 char str[100];
2084 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002085
2086 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002087 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002088
Len Brownc98d5d92012-06-04 00:56:40 -04002089 /*
2090 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002091 * A ',' separated or '-' separated set of numbers
2092 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002093 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002094 fscanf(filep, "%d%c\n", &sib1, &character);
2095 fseek(filep, 0, SEEK_SET);
2096 fgets(str, 100, filep);
2097 ch = strchr(str, character);
2098 while (ch != NULL) {
2099 matches++;
2100 ch = strchr(ch+1, character);
2101 }
Len Brownc98d5d92012-06-04 00:56:40 -04002102
2103 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002104 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002105}
2106
Len Brown103a8fe2010-10-22 23:53:03 -04002107/*
Len Brownc98d5d92012-06-04 00:56:40 -04002108 * run func(thread, core, package) in topology order
2109 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002110 */
2111
Len Brownc98d5d92012-06-04 00:56:40 -04002112int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2113 struct pkg_data *, struct thread_data *, struct core_data *,
2114 struct pkg_data *), struct thread_data *thread_base,
2115 struct core_data *core_base, struct pkg_data *pkg_base,
2116 struct thread_data *thread_base2, struct core_data *core_base2,
2117 struct pkg_data *pkg_base2)
2118{
2119 int retval, pkg_no, core_no, thread_no;
2120
2121 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2122 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2123 for (thread_no = 0; thread_no <
2124 topo.num_threads_per_core; ++thread_no) {
2125 struct thread_data *t, *t2;
2126 struct core_data *c, *c2;
2127 struct pkg_data *p, *p2;
2128
2129 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2130
2131 if (cpu_is_not_present(t->cpu_id))
2132 continue;
2133
2134 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2135
2136 c = GET_CORE(core_base, core_no, pkg_no);
2137 c2 = GET_CORE(core_base2, core_no, pkg_no);
2138
2139 p = GET_PKG(pkg_base, pkg_no);
2140 p2 = GET_PKG(pkg_base2, pkg_no);
2141
2142 retval = func(t, c, p, t2, c2, p2);
2143 if (retval)
2144 return retval;
2145 }
2146 }
2147 }
2148 return 0;
2149}
2150
2151/*
2152 * run func(cpu) on every cpu in /proc/stat
2153 * return max_cpu number
2154 */
2155int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002156{
2157 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002158 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002159 int retval;
2160
Josh Triplett57a42a32013-08-20 17:20:17 -07002161 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002162
2163 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002164 if (retval != 0)
2165 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002166
Len Brownc98d5d92012-06-04 00:56:40 -04002167 while (1) {
2168 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 -04002169 if (retval != 1)
2170 break;
2171
Len Brownc98d5d92012-06-04 00:56:40 -04002172 retval = func(cpu_num);
2173 if (retval) {
2174 fclose(fp);
2175 return(retval);
2176 }
Len Brown103a8fe2010-10-22 23:53:03 -04002177 }
2178 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002179 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002180}
2181
2182void re_initialize(void)
2183{
Len Brownc98d5d92012-06-04 00:56:40 -04002184 free_all_buffers();
2185 setup_all_buffers();
2186 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002187}
2188
Len Brownc98d5d92012-06-04 00:56:40 -04002189
Len Brown103a8fe2010-10-22 23:53:03 -04002190/*
Len Brownc98d5d92012-06-04 00:56:40 -04002191 * count_cpus()
2192 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002193 */
Len Brownc98d5d92012-06-04 00:56:40 -04002194int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002195{
Len Brownc98d5d92012-06-04 00:56:40 -04002196 if (topo.max_cpu_num < cpu)
2197 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002198
Len Brownc98d5d92012-06-04 00:56:40 -04002199 topo.num_cpus += 1;
2200 return 0;
2201}
2202int mark_cpu_present(int cpu)
2203{
2204 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002205 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002206}
2207
Len Brown562a2d32016-02-26 23:48:05 -05002208/*
2209 * snapshot_proc_interrupts()
2210 *
2211 * read and record summary of /proc/interrupts
2212 *
2213 * return 1 if config change requires a restart, else return 0
2214 */
2215int snapshot_proc_interrupts(void)
2216{
2217 static FILE *fp;
2218 int column, retval;
2219
2220 if (fp == NULL)
2221 fp = fopen_or_die("/proc/interrupts", "r");
2222 else
2223 rewind(fp);
2224
2225 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2226 for (column = 0; column < topo.num_cpus; ++column) {
2227 int cpu_number;
2228
2229 retval = fscanf(fp, " CPU%d", &cpu_number);
2230 if (retval != 1)
2231 break;
2232
2233 if (cpu_number > topo.max_cpu_num) {
2234 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2235 return 1;
2236 }
2237
2238 irq_column_2_cpu[column] = cpu_number;
2239 irqs_per_cpu[cpu_number] = 0;
2240 }
2241
2242 /* read /proc/interrupt count lines and sum up irqs per cpu */
2243 while (1) {
2244 int column;
2245 char buf[64];
2246
2247 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2248 if (retval != 1)
2249 break;
2250
2251 /* read the count per cpu */
2252 for (column = 0; column < topo.num_cpus; ++column) {
2253
2254 int cpu_number, irq_count;
2255
2256 retval = fscanf(fp, " %d", &irq_count);
2257 if (retval != 1)
2258 break;
2259
2260 cpu_number = irq_column_2_cpu[column];
2261 irqs_per_cpu[cpu_number] += irq_count;
2262
2263 }
2264
2265 while (getc(fp) != '\n')
2266 ; /* flush interrupt description */
2267
2268 }
2269 return 0;
2270}
Len Brown27d47352016-02-27 00:37:54 -05002271/*
Len Brownfdf676e2016-02-27 01:28:12 -05002272 * snapshot_gfx_rc6_ms()
2273 *
2274 * record snapshot of
2275 * /sys/class/drm/card0/power/rc6_residency_ms
2276 *
2277 * return 1 if config change requires a restart, else return 0
2278 */
2279int snapshot_gfx_rc6_ms(void)
2280{
2281 FILE *fp;
2282 int retval;
2283
2284 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2285
2286 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2287 if (retval != 1)
2288 err(1, "GFX rc6");
2289
2290 fclose(fp);
2291
2292 return 0;
2293}
2294/*
Len Brown27d47352016-02-27 00:37:54 -05002295 * snapshot_gfx_mhz()
2296 *
2297 * record snapshot of
2298 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2299 *
2300 * return 1 if config change requires a restart, else return 0
2301 */
2302int snapshot_gfx_mhz(void)
2303{
2304 static FILE *fp;
2305 int retval;
2306
2307 if (fp == NULL)
2308 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2309 else
2310 rewind(fp);
2311
2312 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2313 if (retval != 1)
2314 err(1, "GFX MHz");
2315
2316 return 0;
2317}
Len Brown562a2d32016-02-26 23:48:05 -05002318
2319/*
2320 * snapshot /proc and /sys files
2321 *
2322 * return 1 if configuration restart needed, else return 0
2323 */
2324int snapshot_proc_sysfs_files(void)
2325{
2326 if (snapshot_proc_interrupts())
2327 return 1;
2328
Len Brown812db3f2017-02-10 00:25:41 -05002329 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002330 snapshot_gfx_rc6_ms();
2331
Len Brown812db3f2017-02-10 00:25:41 -05002332 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002333 snapshot_gfx_mhz();
2334
Len Brown562a2d32016-02-26 23:48:05 -05002335 return 0;
2336}
2337
Len Brown103a8fe2010-10-22 23:53:03 -04002338void turbostat_loop()
2339{
Len Brownc98d5d92012-06-04 00:56:40 -04002340 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002341 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002342
Len Brown103a8fe2010-10-22 23:53:03 -04002343restart:
Len Browne52966c2012-11-08 22:38:05 -05002344 restarted++;
2345
Len Brown562a2d32016-02-26 23:48:05 -05002346 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002347 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002348 if (retval < -1) {
2349 exit(retval);
2350 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002351 if (restarted > 1) {
2352 exit(retval);
2353 }
Len Brownc98d5d92012-06-04 00:56:40 -04002354 re_initialize();
2355 goto restart;
2356 }
Len Browne52966c2012-11-08 22:38:05 -05002357 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002358 gettimeofday(&tv_even, (struct timezone *)NULL);
2359
2360 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002361 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002362 re_initialize();
2363 goto restart;
2364 }
Len Brown2a0609c2016-02-12 22:44:48 -05002365 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002366 if (snapshot_proc_sysfs_files())
2367 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002368 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002369 if (retval < -1) {
2370 exit(retval);
2371 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002372 re_initialize();
2373 goto restart;
2374 }
Len Brown103a8fe2010-10-22 23:53:03 -04002375 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002376 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002377 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2378 re_initialize();
2379 goto restart;
2380 }
Len Brownc98d5d92012-06-04 00:56:40 -04002381 compute_average(EVEN_COUNTERS);
2382 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002383 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002384 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002385 if (snapshot_proc_sysfs_files())
2386 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002387 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002388 if (retval < -1) {
2389 exit(retval);
2390 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002391 re_initialize();
2392 goto restart;
2393 }
Len Brown103a8fe2010-10-22 23:53:03 -04002394 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002395 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002396 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2397 re_initialize();
2398 goto restart;
2399 }
Len Brownc98d5d92012-06-04 00:56:40 -04002400 compute_average(ODD_COUNTERS);
2401 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002402 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002403 }
2404}
2405
2406void check_dev_msr()
2407{
2408 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002409 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002410
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002411 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2412 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002413 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2414 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002415}
2416
Len Brown98481e72014-08-15 00:36:50 -04002417void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002418{
Len Brown98481e72014-08-15 00:36:50 -04002419 struct __user_cap_header_struct cap_header_data;
2420 cap_user_header_t cap_header = &cap_header_data;
2421 struct __user_cap_data_struct cap_data_data;
2422 cap_user_data_t cap_data = &cap_data_data;
2423 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2424 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002425 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002426
2427 /* check for CAP_SYS_RAWIO */
2428 cap_header->pid = getpid();
2429 cap_header->version = _LINUX_CAPABILITY_VERSION;
2430 if (capget(cap_header, cap_data) < 0)
2431 err(-6, "capget(2) failed");
2432
2433 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2434 do_exit++;
2435 warnx("capget(CAP_SYS_RAWIO) failed,"
2436 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2437 }
2438
2439 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002440 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2441 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002442 do_exit++;
2443 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2444 }
2445
2446 /* if all else fails, thell them to be root */
2447 if (do_exit)
2448 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002449 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002450
2451 if (do_exit)
2452 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002453}
2454
Len Brownd7899442015-01-23 00:12:33 -05002455/*
2456 * NHM adds support for additional MSRs:
2457 *
2458 * MSR_SMI_COUNT 0x00000034
2459 *
Len Brownec0adc52015-11-12 02:42:31 -05002460 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002461 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002462 *
Len Browncf4cbe52017-01-01 13:08:33 -05002463 * MSR_MISC_PWR_MGMT 0x000001aa
2464 *
Len Brownd7899442015-01-23 00:12:33 -05002465 * MSR_PKG_C3_RESIDENCY 0x000003f8
2466 * MSR_PKG_C6_RESIDENCY 0x000003f9
2467 * MSR_CORE_C3_RESIDENCY 0x000003fc
2468 * MSR_CORE_C6_RESIDENCY 0x000003fd
2469 *
Len Brownee7e38e2015-02-09 23:39:45 -05002470 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002471 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002472 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002473 */
Len Brownee7e38e2015-02-09 23:39:45 -05002474int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002475{
Len Brownee7e38e2015-02-09 23:39:45 -05002476 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002477 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002478 int *pkg_cstate_limits;
2479
Len Brown103a8fe2010-10-22 23:53:03 -04002480 if (!genuine_intel)
2481 return 0;
2482
2483 if (family != 6)
2484 return 0;
2485
Len Brown21ed5572015-10-19 22:37:40 -04002486 bclk = discover_bclk(family, model);
2487
Len Brown103a8fe2010-10-22 23:53:03 -04002488 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002489 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2490 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002491 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002492 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2493 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2494 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2495 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002496 pkg_cstate_limits = nhm_pkg_cstate_limits;
2497 break;
Len Brown869ce692016-06-16 23:22:37 -04002498 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2499 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2500 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2501 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002502 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002503 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002504 break;
Len Brown869ce692016-06-16 23:22:37 -04002505 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2506 case INTEL_FAM6_HASWELL_X: /* HSX */
2507 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2508 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2509 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2510 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2511 case INTEL_FAM6_BROADWELL_X: /* BDX */
2512 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2513 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2514 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2515 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2516 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002517 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002518 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002519 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002520 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2521 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002522 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002523 break;
Len Brown869ce692016-06-16 23:22:37 -04002524 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002525 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002526 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002527 pkg_cstate_limits = slv_pkg_cstate_limits;
2528 break;
Len Brown869ce692016-06-16 23:22:37 -04002529 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002530 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002531 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002532 break;
Len Brown869ce692016-06-16 23:22:37 -04002533 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002534 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002535 pkg_cstate_limits = phi_pkg_cstate_limits;
2536 break;
Len Brown869ce692016-06-16 23:22:37 -04002537 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002538 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002539 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002540 pkg_cstate_limits = bxt_pkg_cstate_limits;
2541 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002542 default:
2543 return 0;
2544 }
Len Brown1df2e552017-01-07 23:24:57 -05002545 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002546 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002547
Len Brownec0adc52015-11-12 02:42:31 -05002548 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002549 base_ratio = (msr >> 8) & 0xFF;
2550
2551 base_hz = base_ratio * bclk * 1000000;
2552 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002553 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002554}
Len Brown0f7887c2017-01-12 23:49:18 -05002555/*
2556 * SLV client has supporet for unique MSRs:
2557 *
2558 * MSR_CC6_DEMOTION_POLICY_CONFIG
2559 * MSR_MC6_DEMOTION_POLICY_CONFIG
2560 */
2561
2562int has_slv_msrs(unsigned int family, unsigned int model)
2563{
2564 if (!genuine_intel)
2565 return 0;
2566
2567 switch (model) {
2568 case INTEL_FAM6_ATOM_SILVERMONT1:
2569 case INTEL_FAM6_ATOM_MERRIFIELD:
2570 case INTEL_FAM6_ATOM_MOOREFIELD:
2571 return 1;
2572 }
2573 return 0;
2574}
Len Brown7170a372017-01-27 02:13:27 -05002575int is_dnv(unsigned int family, unsigned int model)
2576{
2577
2578 if (!genuine_intel)
2579 return 0;
2580
2581 switch (model) {
2582 case INTEL_FAM6_ATOM_DENVERTON:
2583 return 1;
2584 }
2585 return 0;
2586}
Len Brown34c761972017-01-27 02:36:41 -05002587int is_skx(unsigned int family, unsigned int model)
2588{
2589
2590 if (!genuine_intel)
2591 return 0;
2592
2593 switch (model) {
2594 case INTEL_FAM6_SKYLAKE_X:
2595 return 1;
2596 }
2597 return 0;
2598}
Len Brown0f7887c2017-01-12 23:49:18 -05002599
Len Brownd7899442015-01-23 00:12:33 -05002600int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2601{
Len Brown0f7887c2017-01-12 23:49:18 -05002602 if (has_slv_msrs(family, model))
2603 return 0;
2604
Len Brownd7899442015-01-23 00:12:33 -05002605 switch (model) {
2606 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002607 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2608 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2609 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002610 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002611 return 0;
2612 default:
2613 return 1;
2614 }
2615}
Len Brown0f7887c2017-01-12 23:49:18 -05002616int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2617{
2618 if (has_slv_msrs(family, model))
2619 return 1;
2620
2621 return 0;
2622}
Len Brown6574a5d2012-09-21 00:01:31 -04002623int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2624{
2625 if (!genuine_intel)
2626 return 0;
2627
2628 if (family != 6)
2629 return 0;
2630
2631 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002632 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2633 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002634 return 1;
2635 default:
2636 return 0;
2637 }
2638}
Len Brownfcd17212015-03-23 20:29:09 -04002639int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2640{
2641 if (!genuine_intel)
2642 return 0;
2643
2644 if (family != 6)
2645 return 0;
2646
2647 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002648 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002649 return 1;
2650 default:
2651 return 0;
2652 }
2653}
2654
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002655int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2656{
2657 if (!genuine_intel)
2658 return 0;
2659
2660 if (family != 6)
2661 return 0;
2662
2663 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002664 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002665 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002666 return 1;
2667 default:
2668 return 0;
2669 }
2670}
Len Brown6fb31432015-06-17 16:23:45 -04002671int has_config_tdp(unsigned int family, unsigned int model)
2672{
2673 if (!genuine_intel)
2674 return 0;
2675
2676 if (family != 6)
2677 return 0;
2678
2679 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002680 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2681 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2682 case INTEL_FAM6_HASWELL_X: /* HSX */
2683 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2684 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2685 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2686 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2687 case INTEL_FAM6_BROADWELL_X: /* BDX */
2688 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2689 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2690 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2691 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2692 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2693 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002694
Len Brown869ce692016-06-16 23:22:37 -04002695 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002696 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002697 return 1;
2698 default:
2699 return 0;
2700 }
2701}
2702
Len Brownfcd17212015-03-23 20:29:09 -04002703static void
Colin Ian King1b693172016-03-02 13:50:25 +00002704dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002705{
2706 if (!do_nhm_platform_info)
2707 return;
2708
2709 dump_nhm_platform_info();
2710
2711 if (has_hsw_turbo_ratio_limit(family, model))
2712 dump_hsw_turbo_ratio_limits();
2713
2714 if (has_ivt_turbo_ratio_limit(family, model))
2715 dump_ivt_turbo_ratio_limits();
2716
2717 if (has_nhm_turbo_ratio_limit(family, model))
2718 dump_nhm_turbo_ratio_limits();
2719
Len Brown0f7887c2017-01-12 23:49:18 -05002720 if (has_atom_turbo_ratio_limit(family, model))
2721 dump_atom_turbo_ratio_limits();
2722
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002723 if (has_knl_turbo_ratio_limit(family, model))
2724 dump_knl_turbo_ratio_limits();
2725
Len Brown6fb31432015-06-17 16:23:45 -04002726 if (has_config_tdp(family, model))
2727 dump_config_tdp();
2728
Len Brownfcd17212015-03-23 20:29:09 -04002729 dump_nhm_cst_cfg();
2730}
2731
Len Brown6574a5d2012-09-21 00:01:31 -04002732
Len Brown889facb2012-11-08 00:48:57 -05002733/*
2734 * print_epb()
2735 * Decode the ENERGY_PERF_BIAS MSR
2736 */
2737int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2738{
2739 unsigned long long msr;
2740 char *epb_string;
2741 int cpu;
2742
2743 if (!has_epb)
2744 return 0;
2745
2746 cpu = t->cpu_id;
2747
2748 /* EPB is per-package */
2749 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2750 return 0;
2751
2752 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002753 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002754 return -1;
2755 }
2756
2757 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2758 return 0;
2759
Len Browne9be7dd2015-05-26 12:19:37 -04002760 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002761 case ENERGY_PERF_BIAS_PERFORMANCE:
2762 epb_string = "performance";
2763 break;
2764 case ENERGY_PERF_BIAS_NORMAL:
2765 epb_string = "balanced";
2766 break;
2767 case ENERGY_PERF_BIAS_POWERSAVE:
2768 epb_string = "powersave";
2769 break;
2770 default:
2771 epb_string = "custom";
2772 break;
2773 }
Len Brownb7d8c142016-02-13 23:36:17 -05002774 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002775
2776 return 0;
2777}
Len Brown7f5c2582015-12-01 01:36:39 -05002778/*
2779 * print_hwp()
2780 * Decode the MSR_HWP_CAPABILITIES
2781 */
2782int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2783{
2784 unsigned long long msr;
2785 int cpu;
2786
2787 if (!has_hwp)
2788 return 0;
2789
2790 cpu = t->cpu_id;
2791
2792 /* MSR_HWP_CAPABILITIES is per-package */
2793 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2794 return 0;
2795
2796 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002797 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002798 return -1;
2799 }
2800
2801 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2802 return 0;
2803
Len Brownb7d8c142016-02-13 23:36:17 -05002804 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002805 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2806
2807 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2808 if ((msr & (1 << 0)) == 0)
2809 return 0;
2810
2811 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2812 return 0;
2813
Len Brownb7d8c142016-02-13 23:36:17 -05002814 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002815 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2816 cpu, msr,
2817 (unsigned int)HWP_HIGHEST_PERF(msr),
2818 (unsigned int)HWP_GUARANTEED_PERF(msr),
2819 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2820 (unsigned int)HWP_LOWEST_PERF(msr));
2821
2822 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2823 return 0;
2824
Len Brownb7d8c142016-02-13 23:36:17 -05002825 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002826 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2827 cpu, msr,
2828 (unsigned int)(((msr) >> 0) & 0xff),
2829 (unsigned int)(((msr) >> 8) & 0xff),
2830 (unsigned int)(((msr) >> 16) & 0xff),
2831 (unsigned int)(((msr) >> 24) & 0xff),
2832 (unsigned int)(((msr) >> 32) & 0xff3),
2833 (unsigned int)(((msr) >> 42) & 0x1));
2834
2835 if (has_hwp_pkg) {
2836 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2837 return 0;
2838
Len Brownb7d8c142016-02-13 23:36:17 -05002839 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002840 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2841 cpu, msr,
2842 (unsigned int)(((msr) >> 0) & 0xff),
2843 (unsigned int)(((msr) >> 8) & 0xff),
2844 (unsigned int)(((msr) >> 16) & 0xff),
2845 (unsigned int)(((msr) >> 24) & 0xff),
2846 (unsigned int)(((msr) >> 32) & 0xff3));
2847 }
2848 if (has_hwp_notify) {
2849 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2850 return 0;
2851
Len Brownb7d8c142016-02-13 23:36:17 -05002852 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002853 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2854 cpu, msr,
2855 ((msr) & 0x1) ? "EN" : "Dis",
2856 ((msr) & 0x2) ? "EN" : "Dis");
2857 }
2858 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2859 return 0;
2860
Len Brownb7d8c142016-02-13 23:36:17 -05002861 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002862 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2863 cpu, msr,
2864 ((msr) & 0x1) ? "" : "No-",
2865 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05002866
2867 return 0;
2868}
2869
Len Brown3a9a9412014-08-15 02:39:52 -04002870/*
2871 * print_perf_limit()
2872 */
2873int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2874{
2875 unsigned long long msr;
2876 int cpu;
2877
2878 cpu = t->cpu_id;
2879
2880 /* per-package */
2881 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2882 return 0;
2883
2884 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002885 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002886 return -1;
2887 }
2888
2889 if (do_core_perf_limit_reasons) {
2890 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002891 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2892 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002893 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002894 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002895 (msr & 1 << 13) ? "Transitions, " : "",
2896 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2897 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2898 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2899 (msr & 1 << 9) ? "CorePwr, " : "",
2900 (msr & 1 << 8) ? "Amps, " : "",
2901 (msr & 1 << 6) ? "VR-Therm, " : "",
2902 (msr & 1 << 5) ? "Auto-HWP, " : "",
2903 (msr & 1 << 4) ? "Graphics, " : "",
2904 (msr & 1 << 2) ? "bit2, " : "",
2905 (msr & 1 << 1) ? "ThermStatus, " : "",
2906 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002907 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 -04002908 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002909 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002910 (msr & 1 << 29) ? "Transitions, " : "",
2911 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2912 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2913 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2914 (msr & 1 << 25) ? "CorePwr, " : "",
2915 (msr & 1 << 24) ? "Amps, " : "",
2916 (msr & 1 << 22) ? "VR-Therm, " : "",
2917 (msr & 1 << 21) ? "Auto-HWP, " : "",
2918 (msr & 1 << 20) ? "Graphics, " : "",
2919 (msr & 1 << 18) ? "bit18, " : "",
2920 (msr & 1 << 17) ? "ThermStatus, " : "",
2921 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002922
2923 }
2924 if (do_gfx_perf_limit_reasons) {
2925 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002926 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2927 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002928 (msr & 1 << 0) ? "PROCHOT, " : "",
2929 (msr & 1 << 1) ? "ThermStatus, " : "",
2930 (msr & 1 << 4) ? "Graphics, " : "",
2931 (msr & 1 << 6) ? "VR-Therm, " : "",
2932 (msr & 1 << 8) ? "Amps, " : "",
2933 (msr & 1 << 9) ? "GFXPwr, " : "",
2934 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2935 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002936 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002937 (msr & 1 << 16) ? "PROCHOT, " : "",
2938 (msr & 1 << 17) ? "ThermStatus, " : "",
2939 (msr & 1 << 20) ? "Graphics, " : "",
2940 (msr & 1 << 22) ? "VR-Therm, " : "",
2941 (msr & 1 << 24) ? "Amps, " : "",
2942 (msr & 1 << 25) ? "GFXPwr, " : "",
2943 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2944 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2945 }
2946 if (do_ring_perf_limit_reasons) {
2947 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002948 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2949 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002950 (msr & 1 << 0) ? "PROCHOT, " : "",
2951 (msr & 1 << 1) ? "ThermStatus, " : "",
2952 (msr & 1 << 6) ? "VR-Therm, " : "",
2953 (msr & 1 << 8) ? "Amps, " : "",
2954 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2955 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002956 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002957 (msr & 1 << 16) ? "PROCHOT, " : "",
2958 (msr & 1 << 17) ? "ThermStatus, " : "",
2959 (msr & 1 << 22) ? "VR-Therm, " : "",
2960 (msr & 1 << 24) ? "Amps, " : "",
2961 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2962 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2963 }
2964 return 0;
2965}
2966
Len Brown889facb2012-11-08 00:48:57 -05002967#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2968#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2969
Colin Ian King1b693172016-03-02 13:50:25 +00002970double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05002971{
2972 unsigned long long msr;
2973
2974 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002975 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002976 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2977
2978 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002979 case INTEL_FAM6_ATOM_SILVERMONT1:
2980 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05002981 return 30.0;
2982 default:
2983 return 135.0;
2984 }
2985}
2986
Andrey Semin40ee8e32014-12-05 00:07:00 -05002987/*
2988 * rapl_dram_energy_units_probe()
2989 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2990 */
2991static double
2992rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2993{
2994 /* only called for genuine_intel, family 6 */
2995
2996 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002997 case INTEL_FAM6_HASWELL_X: /* HSX */
2998 case INTEL_FAM6_BROADWELL_X: /* BDX */
2999 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3000 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003001 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003002 return (rapl_dram_energy_units = 15.3 / 1000000);
3003 default:
3004 return (rapl_energy_units);
3005 }
3006}
3007
Len Brown144b44b2013-11-09 00:30:16 -05003008
Len Brown889facb2012-11-08 00:48:57 -05003009/*
3010 * rapl_probe()
3011 *
Len Brown144b44b2013-11-09 00:30:16 -05003012 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003013 */
3014void rapl_probe(unsigned int family, unsigned int model)
3015{
3016 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003017 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003018 double tdp;
3019
3020 if (!genuine_intel)
3021 return;
3022
3023 if (family != 6)
3024 return;
3025
3026 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003027 case INTEL_FAM6_SANDYBRIDGE:
3028 case INTEL_FAM6_IVYBRIDGE:
3029 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3030 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3031 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3032 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3033 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003034 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003035 if (rapl_joules) {
3036 BIC_PRESENT(BIC_Pkg_J);
3037 BIC_PRESENT(BIC_Cor_J);
3038 BIC_PRESENT(BIC_GFX_J);
3039 } else {
3040 BIC_PRESENT(BIC_PkgWatt);
3041 BIC_PRESENT(BIC_CorWatt);
3042 BIC_PRESENT(BIC_GFXWatt);
3043 }
Len Brown889facb2012-11-08 00:48:57 -05003044 break;
Len Brown869ce692016-06-16 23:22:37 -04003045 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003046 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003047 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003048 if (rapl_joules)
3049 BIC_PRESENT(BIC_Pkg_J);
3050 else
3051 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003052 break;
Len Brown869ce692016-06-16 23:22:37 -04003053 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3054 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3055 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3056 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003057 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 -05003058 BIC_PRESENT(BIC_PKG__);
3059 BIC_PRESENT(BIC_RAM__);
3060 if (rapl_joules) {
3061 BIC_PRESENT(BIC_Pkg_J);
3062 BIC_PRESENT(BIC_Cor_J);
3063 BIC_PRESENT(BIC_RAM_J);
3064 } else {
3065 BIC_PRESENT(BIC_PkgWatt);
3066 BIC_PRESENT(BIC_CorWatt);
3067 BIC_PRESENT(BIC_RAMWatt);
3068 }
Len Brown0b2bb692015-03-26 00:50:30 -04003069 break;
Len Brown869ce692016-06-16 23:22:37 -04003070 case INTEL_FAM6_HASWELL_X: /* HSX */
3071 case INTEL_FAM6_BROADWELL_X: /* BDX */
3072 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3073 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3074 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003075 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003076 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 -05003077 BIC_PRESENT(BIC_PKG__);
3078 BIC_PRESENT(BIC_RAM__);
3079 if (rapl_joules) {
3080 BIC_PRESENT(BIC_Pkg_J);
3081 BIC_PRESENT(BIC_RAM_J);
3082 } else {
3083 BIC_PRESENT(BIC_PkgWatt);
3084 BIC_PRESENT(BIC_RAMWatt);
3085 }
Len Browne6f9bb32013-12-03 02:19:19 -05003086 break;
Len Brown869ce692016-06-16 23:22:37 -04003087 case INTEL_FAM6_SANDYBRIDGE_X:
3088 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003089 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 -05003090 BIC_PRESENT(BIC_PKG__);
3091 BIC_PRESENT(BIC_RAM__);
3092 if (rapl_joules) {
3093 BIC_PRESENT(BIC_Pkg_J);
3094 BIC_PRESENT(BIC_Cor_J);
3095 BIC_PRESENT(BIC_RAM_J);
3096 } else {
3097 BIC_PRESENT(BIC_PkgWatt);
3098 BIC_PRESENT(BIC_CorWatt);
3099 BIC_PRESENT(BIC_RAMWatt);
3100 }
Len Brown144b44b2013-11-09 00:30:16 -05003101 break;
Len Brown869ce692016-06-16 23:22:37 -04003102 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3103 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003104 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003105 if (rapl_joules) {
3106 BIC_PRESENT(BIC_Pkg_J);
3107 BIC_PRESENT(BIC_Cor_J);
3108 } else {
3109 BIC_PRESENT(BIC_PkgWatt);
3110 BIC_PRESENT(BIC_CorWatt);
3111 }
Len Brown889facb2012-11-08 00:48:57 -05003112 break;
Len Brown869ce692016-06-16 23:22:37 -04003113 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003114 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 -05003115 BIC_PRESENT(BIC_PKG__);
3116 BIC_PRESENT(BIC_RAM__);
3117 if (rapl_joules) {
3118 BIC_PRESENT(BIC_Pkg_J);
3119 BIC_PRESENT(BIC_Cor_J);
3120 BIC_PRESENT(BIC_RAM_J);
3121 } else {
3122 BIC_PRESENT(BIC_PkgWatt);
3123 BIC_PRESENT(BIC_CorWatt);
3124 BIC_PRESENT(BIC_RAMWatt);
3125 }
Jacob Pan0f644902016-06-16 09:48:22 -07003126 break;
Len Brown889facb2012-11-08 00:48:57 -05003127 default:
3128 return;
3129 }
3130
3131 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003132 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003133 return;
3134
3135 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003136 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003137 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3138 else
3139 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003140
Andrey Semin40ee8e32014-12-05 00:07:00 -05003141 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3142
Len Brown144b44b2013-11-09 00:30:16 -05003143 time_unit = msr >> 16 & 0xF;
3144 if (time_unit == 0)
3145 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003146
Len Brown144b44b2013-11-09 00:30:16 -05003147 rapl_time_units = 1.0 / (1 << (time_unit));
3148
3149 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003150
3151 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003152 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003153 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003154
3155 return;
3156}
3157
Colin Ian King1b693172016-03-02 13:50:25 +00003158void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003159{
3160 if (!genuine_intel)
3161 return;
3162
3163 if (family != 6)
3164 return;
3165
3166 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003167 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3168 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3169 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003170 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003171 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003172 do_core_perf_limit_reasons = 1;
3173 do_ring_perf_limit_reasons = 1;
3174 default:
3175 return;
3176 }
3177}
3178
Len Brown889facb2012-11-08 00:48:57 -05003179int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3180{
3181 unsigned long long msr;
3182 unsigned int dts;
3183 int cpu;
3184
3185 if (!(do_dts || do_ptm))
3186 return 0;
3187
3188 cpu = t->cpu_id;
3189
3190 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003191 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003192 return 0;
3193
3194 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003195 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003196 return -1;
3197 }
3198
3199 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3200 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3201 return 0;
3202
3203 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003204 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003205 cpu, msr, tcc_activation_temp - dts);
3206
3207#ifdef THERM_DEBUG
3208 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3209 return 0;
3210
3211 dts = (msr >> 16) & 0x7F;
3212 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003213 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003214 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3215#endif
3216 }
3217
3218
3219 if (do_dts) {
3220 unsigned int resolution;
3221
3222 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3223 return 0;
3224
3225 dts = (msr >> 16) & 0x7F;
3226 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003227 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003228 cpu, msr, tcc_activation_temp - dts, resolution);
3229
3230#ifdef THERM_DEBUG
3231 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3232 return 0;
3233
3234 dts = (msr >> 16) & 0x7F;
3235 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003236 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003237 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3238#endif
3239 }
3240
3241 return 0;
3242}
Len Brown36229892016-02-26 20:51:02 -05003243
Len Brown889facb2012-11-08 00:48:57 -05003244void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3245{
Len Brownb7d8c142016-02-13 23:36:17 -05003246 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003247 cpu, label,
3248 ((msr >> 15) & 1) ? "EN" : "DIS",
3249 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3250 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3251 (((msr >> 16) & 1) ? "EN" : "DIS"));
3252
3253 return;
3254}
3255
3256int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3257{
3258 unsigned long long msr;
3259 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003260
3261 if (!do_rapl)
3262 return 0;
3263
3264 /* RAPL counters are per package, so print only for 1st thread/package */
3265 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3266 return 0;
3267
3268 cpu = t->cpu_id;
3269 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003270 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003271 return -1;
3272 }
3273
3274 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3275 return -1;
3276
Len Brown96e47152017-01-21 02:26:00 -05003277 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3278 rapl_power_units, rapl_energy_units, rapl_time_units);
3279
Len Brown144b44b2013-11-09 00:30:16 -05003280 if (do_rapl & RAPL_PKG_POWER_INFO) {
3281
Len Brown889facb2012-11-08 00:48:57 -05003282 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3283 return -5;
3284
3285
Len Brownb7d8c142016-02-13 23:36:17 -05003286 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 -05003287 cpu, msr,
3288 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3289 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3290 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3291 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3292
Len Brown144b44b2013-11-09 00:30:16 -05003293 }
3294 if (do_rapl & RAPL_PKG) {
3295
Len Brown889facb2012-11-08 00:48:57 -05003296 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3297 return -9;
3298
Len Brownb7d8c142016-02-13 23:36:17 -05003299 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003300 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003301
3302 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003303 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003304 cpu,
3305 ((msr >> 47) & 1) ? "EN" : "DIS",
3306 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3307 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3308 ((msr >> 48) & 1) ? "EN" : "DIS");
3309 }
3310
Len Brown0b2bb692015-03-26 00:50:30 -04003311 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003312 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3313 return -6;
3314
Len Brownb7d8c142016-02-13 23:36:17 -05003315 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 -05003316 cpu, msr,
3317 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3318 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3319 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3320 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003321 }
3322 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003323 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3324 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003325 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003326 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003327
3328 print_power_limit_msr(cpu, msr, "DRAM Limit");
3329 }
Len Brown144b44b2013-11-09 00:30:16 -05003330 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003331 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3332 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003333
Len Brown96e47152017-01-21 02:26:00 -05003334 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003335 }
Jacob Pan91484942016-06-16 09:48:20 -07003336 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003337 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3338 return -9;
3339 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3340 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3341 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003342 }
3343 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003344 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3345 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003346
Len Brown96e47152017-01-21 02:26:00 -05003347 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003348
Len Brown96e47152017-01-21 02:26:00 -05003349 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3350 return -9;
3351 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3352 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3353 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003354 }
3355 return 0;
3356}
3357
Len Brownd7899442015-01-23 00:12:33 -05003358/*
3359 * SNB adds support for additional MSRs:
3360 *
3361 * MSR_PKG_C7_RESIDENCY 0x000003fa
3362 * MSR_CORE_C7_RESIDENCY 0x000003fe
3363 * MSR_PKG_C2_RESIDENCY 0x0000060d
3364 */
Len Brown103a8fe2010-10-22 23:53:03 -04003365
Len Brownd7899442015-01-23 00:12:33 -05003366int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003367{
3368 if (!genuine_intel)
3369 return 0;
3370
3371 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003372 case INTEL_FAM6_SANDYBRIDGE:
3373 case INTEL_FAM6_SANDYBRIDGE_X:
3374 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3375 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3376 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3377 case INTEL_FAM6_HASWELL_X: /* HSW */
3378 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3379 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3380 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3381 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3382 case INTEL_FAM6_BROADWELL_X: /* BDX */
3383 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3384 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3385 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3386 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3387 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3388 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3389 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003390 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003391 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003392 return 1;
3393 }
3394 return 0;
3395}
3396
Len Brownd7899442015-01-23 00:12:33 -05003397/*
3398 * HSW adds support for additional MSRs:
3399 *
Len Brown5a634262016-04-06 17:15:55 -04003400 * MSR_PKG_C8_RESIDENCY 0x00000630
3401 * MSR_PKG_C9_RESIDENCY 0x00000631
3402 * MSR_PKG_C10_RESIDENCY 0x00000632
3403 *
3404 * MSR_PKGC8_IRTL 0x00000633
3405 * MSR_PKGC9_IRTL 0x00000634
3406 * MSR_PKGC10_IRTL 0x00000635
3407 *
Len Brownd7899442015-01-23 00:12:33 -05003408 */
3409int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003410{
3411 if (!genuine_intel)
3412 return 0;
3413
3414 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003415 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3416 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3417 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3418 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3419 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3420 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3421 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003422 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003423 return 1;
3424 }
3425 return 0;
3426}
3427
Len Brown0b2bb692015-03-26 00:50:30 -04003428/*
3429 * SKL adds support for additional MSRS:
3430 *
3431 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3432 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3433 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3434 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3435 */
3436int has_skl_msrs(unsigned int family, unsigned int model)
3437{
3438 if (!genuine_intel)
3439 return 0;
3440
3441 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003442 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3443 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3444 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3445 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003446 return 1;
3447 }
3448 return 0;
3449}
3450
Len Brown144b44b2013-11-09 00:30:16 -05003451int is_slm(unsigned int family, unsigned int model)
3452{
3453 if (!genuine_intel)
3454 return 0;
3455 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003456 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3457 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003458 return 1;
3459 }
3460 return 0;
3461}
3462
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003463int is_knl(unsigned int family, unsigned int model)
3464{
3465 if (!genuine_intel)
3466 return 0;
3467 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003468 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003469 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003470 return 1;
3471 }
3472 return 0;
3473}
3474
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003475unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3476{
3477 if (is_knl(family, model))
3478 return 1024;
3479 return 1;
3480}
3481
Len Brown144b44b2013-11-09 00:30:16 -05003482#define SLM_BCLK_FREQS 5
3483double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3484
3485double slm_bclk(void)
3486{
3487 unsigned long long msr = 3;
3488 unsigned int i;
3489 double freq;
3490
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003491 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003492 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003493
3494 i = msr & 0xf;
3495 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003496 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003497 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003498 }
3499 freq = slm_freq_table[i];
3500
Len Brown96e47152017-01-21 02:26:00 -05003501 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003502 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003503
3504 return freq;
3505}
3506
Len Brown103a8fe2010-10-22 23:53:03 -04003507double discover_bclk(unsigned int family, unsigned int model)
3508{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003509 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003510 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003511 else if (is_slm(family, model))
3512 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003513 else
3514 return 133.33;
3515}
3516
Len Brown889facb2012-11-08 00:48:57 -05003517/*
3518 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3519 * the Thermal Control Circuit (TCC) activates.
3520 * This is usually equal to tjMax.
3521 *
3522 * Older processors do not have this MSR, so there we guess,
3523 * but also allow cmdline over-ride with -T.
3524 *
3525 * Several MSR temperature values are in units of degrees-C
3526 * below this value, including the Digital Thermal Sensor (DTS),
3527 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3528 */
3529int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3530{
3531 unsigned long long msr;
3532 unsigned int target_c_local;
3533 int cpu;
3534
3535 /* tcc_activation_temp is used only for dts or ptm */
3536 if (!(do_dts || do_ptm))
3537 return 0;
3538
3539 /* this is a per-package concept */
3540 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3541 return 0;
3542
3543 cpu = t->cpu_id;
3544 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003545 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003546 return -1;
3547 }
3548
3549 if (tcc_activation_temp_override != 0) {
3550 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003551 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003552 cpu, tcc_activation_temp);
3553 return 0;
3554 }
3555
3556 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003557 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003558 goto guess;
3559
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003560 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003561 goto guess;
3562
Jean Delvare34821242014-05-01 11:40:19 +02003563 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003564
Len Brown96e47152017-01-21 02:26:00 -05003565 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003566 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003567 cpu, msr, target_c_local);
3568
Jean Delvare34821242014-05-01 11:40:19 +02003569 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003570 goto guess;
3571
3572 tcc_activation_temp = target_c_local;
3573
3574 return 0;
3575
3576guess:
3577 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003578 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003579 cpu, tcc_activation_temp);
3580
3581 return 0;
3582}
Len Brown69807a62015-11-21 12:22:47 -05003583
Len Brownaa8d8cc2016-03-11 13:26:03 -05003584void decode_feature_control_msr(void)
3585{
3586 unsigned long long msr;
3587
3588 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3589 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3590 base_cpu, msr,
3591 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3592 msr & (1 << 18) ? "SGX" : "");
3593}
3594
Len Brown69807a62015-11-21 12:22:47 -05003595void decode_misc_enable_msr(void)
3596{
3597 unsigned long long msr;
3598
3599 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003600 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003601 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003602 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3603 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3604 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3605 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3606 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003607}
3608
Len Brown33148d62017-01-21 01:26:16 -05003609void decode_misc_feature_control(void)
3610{
3611 unsigned long long msr;
3612
3613 if (!has_misc_feature_control)
3614 return;
3615
3616 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
3617 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
3618 base_cpu, msr,
3619 msr & (0 << 0) ? "No-" : "",
3620 msr & (1 << 0) ? "No-" : "",
3621 msr & (2 << 0) ? "No-" : "",
3622 msr & (3 << 0) ? "No-" : "");
3623}
Len Brownf0057312015-12-03 01:35:36 -05003624/*
3625 * Decode MSR_MISC_PWR_MGMT
3626 *
3627 * Decode the bits according to the Nehalem documentation
3628 * bit[0] seems to continue to have same meaning going forward
3629 * bit[1] less so...
3630 */
3631void decode_misc_pwr_mgmt_msr(void)
3632{
3633 unsigned long long msr;
3634
3635 if (!do_nhm_platform_info)
3636 return;
3637
Len Browncf4cbe52017-01-01 13:08:33 -05003638 if (no_MSR_MISC_PWR_MGMT)
3639 return;
3640
Len Brownf0057312015-12-03 01:35:36 -05003641 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003642 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 -05003643 base_cpu, msr,
3644 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003645 msr & (1 << 1) ? "EN" : "DIS",
3646 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003647}
Len Brown71616c82017-01-07 22:37:48 -05003648/*
3649 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3650 *
3651 * This MSRs are present on Silvermont processors,
3652 * Intel Atom processor E3000 series (Baytrail), and friends.
3653 */
3654void decode_c6_demotion_policy_msr(void)
3655{
3656 unsigned long long msr;
3657
3658 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3659 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3660 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3661
3662 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3663 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3664 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3665}
Len Brown7f5c2582015-12-01 01:36:39 -05003666
Len Brownfcd17212015-03-23 20:29:09 -04003667void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003668{
Len Brown61a87ba2015-11-23 02:30:51 -05003669 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003670 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003671 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003672
3673 eax = ebx = ecx = edx = 0;
3674
Len Brown5aea2f72016-03-13 03:14:35 -04003675 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003676
3677 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3678 genuine_intel = 1;
3679
Len Brown96e47152017-01-21 02:26:00 -05003680 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003681 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003682 (char *)&ebx, (char *)&edx, (char *)&ecx);
3683
Len Brown5aea2f72016-03-13 03:14:35 -04003684 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003685 family = (fms >> 8) & 0xf;
3686 model = (fms >> 4) & 0xf;
3687 stepping = fms & 0xf;
3688 if (family == 6 || family == 0xf)
3689 model += ((fms >> 16) & 0xf) << 4;
3690
Len Brown96e47152017-01-21 02:26:00 -05003691 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05003692 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003693 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003694 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003695 ecx & (1 << 0) ? "SSE3" : "-",
3696 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003697 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003698 ecx & (1 << 7) ? "EIST" : "-",
3699 ecx & (1 << 8) ? "TM2" : "-",
3700 edx & (1 << 4) ? "TSC" : "-",
3701 edx & (1 << 5) ? "MSR" : "-",
3702 edx & (1 << 22) ? "ACPI-TM" : "-",
3703 edx & (1 << 29) ? "TM" : "-");
3704 }
Len Brown103a8fe2010-10-22 23:53:03 -04003705
Josh Triplettb2c95d92013-08-20 17:20:18 -07003706 if (!(edx & (1 << 5)))
3707 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003708
3709 /*
3710 * check max extended function levels of CPUID.
3711 * This is needed to check for invariant TSC.
3712 * This check is valid for both Intel and AMD.
3713 */
3714 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003715 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003716
Len Brown61a87ba2015-11-23 02:30:51 -05003717 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003718
Len Brownd7899442015-01-23 00:12:33 -05003719 /*
3720 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3721 * this check is valid for both Intel and AMD
3722 */
Len Brown5aea2f72016-03-13 03:14:35 -04003723 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003724 has_invariant_tsc = edx & (1 << 8);
3725 }
Len Brown103a8fe2010-10-22 23:53:03 -04003726
3727 /*
3728 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3729 * this check is valid for both Intel and AMD
3730 */
3731
Len Brown5aea2f72016-03-13 03:14:35 -04003732 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003733 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003734 if (has_aperf) {
3735 BIC_PRESENT(BIC_Avg_MHz);
3736 BIC_PRESENT(BIC_Busy);
3737 BIC_PRESENT(BIC_Bzy_MHz);
3738 }
Len Brown889facb2012-11-08 00:48:57 -05003739 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003740 if (do_dts)
3741 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05003742 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05003743 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003744 if (do_ptm)
3745 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003746 has_hwp = eax & (1 << 7);
3747 has_hwp_notify = eax & (1 << 8);
3748 has_hwp_activity_window = eax & (1 << 9);
3749 has_hwp_epp = eax & (1 << 10);
3750 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003751 has_epb = ecx & (1 << 3);
3752
Len Brown96e47152017-01-21 02:26:00 -05003753 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05003754 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003755 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3756 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05003757 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05003758 do_dts ? "" : "No-",
3759 do_ptm ? "" : "No-",
3760 has_hwp ? "" : "No-",
3761 has_hwp_notify ? "" : "No-",
3762 has_hwp_activity_window ? "" : "No-",
3763 has_hwp_epp ? "" : "No-",
3764 has_hwp_pkg ? "" : "No-",
3765 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003766
Len Brown96e47152017-01-21 02:26:00 -05003767 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05003768 decode_misc_enable_msr();
3769
Len Brown33148d62017-01-21 01:26:16 -05003770
Len Brown96e47152017-01-21 02:26:00 -05003771 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003772 int has_sgx;
3773
3774 ecx = 0;
3775
3776 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3777
3778 has_sgx = ebx & (1 << 2);
3779 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3780
3781 if (has_sgx)
3782 decode_feature_control_msr();
3783 }
3784
Len Brown61a87ba2015-11-23 02:30:51 -05003785 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003786 unsigned int eax_crystal;
3787 unsigned int ebx_tsc;
3788
3789 /*
3790 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3791 */
3792 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003793 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003794
3795 if (ebx_tsc != 0) {
3796
Len Brown96e47152017-01-21 02:26:00 -05003797 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003798 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003799 eax_crystal, ebx_tsc, crystal_hz);
3800
3801 if (crystal_hz == 0)
3802 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04003803 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3804 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3805 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3806 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003807 crystal_hz = 24000000; /* 24.0 MHz */
3808 break;
Len Brown869ce692016-06-16 23:22:37 -04003809 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003810 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003811 crystal_hz = 25000000; /* 25.0 MHz */
3812 break;
Len Brown869ce692016-06-16 23:22:37 -04003813 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003814 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04003815 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003816 break;
3817 default:
3818 crystal_hz = 0;
3819 }
3820
3821 if (crystal_hz) {
3822 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05003823 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003824 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003825 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3826 }
3827 }
3828 }
Len Brown61a87ba2015-11-23 02:30:51 -05003829 if (max_level >= 0x16) {
3830 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3831
3832 /*
3833 * CPUID 16H Base MHz, Max MHz, Bus MHz
3834 */
3835 base_mhz = max_mhz = bus_mhz = edx = 0;
3836
Len Brown5aea2f72016-03-13 03:14:35 -04003837 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05003838 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003839 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003840 base_mhz, max_mhz, bus_mhz);
3841 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003842
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003843 if (has_aperf)
3844 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3845
Len Brown812db3f2017-02-10 00:25:41 -05003846 BIC_PRESENT(BIC_IRQ);
3847 BIC_PRESENT(BIC_TSC_MHz);
3848
3849 if (probe_nhm_msrs(family, model)) {
3850 do_nhm_platform_info = 1;
3851 BIC_PRESENT(BIC_CPU_c1);
3852 BIC_PRESENT(BIC_CPU_c3);
3853 BIC_PRESENT(BIC_CPU_c6);
3854 BIC_PRESENT(BIC_SMI);
3855 }
Len Brownd7899442015-01-23 00:12:33 -05003856 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05003857
3858 if (do_snb_cstates)
3859 BIC_PRESENT(BIC_CPU_c7);
3860
Len Brown5a634262016-04-06 17:15:55 -04003861 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05003862 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
3863 BIC_PRESENT(BIC_Pkgpc2);
3864 if (pkg_cstate_limit >= PCL__3)
3865 BIC_PRESENT(BIC_Pkgpc3);
3866 if (pkg_cstate_limit >= PCL__6)
3867 BIC_PRESENT(BIC_Pkgpc6);
3868 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
3869 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05003870 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05003871 BIC_NOT_PRESENT(BIC_Pkgpc2);
3872 BIC_NOT_PRESENT(BIC_Pkgpc3);
3873 BIC_PRESENT(BIC_Pkgpc6);
3874 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05003875 BIC_PRESENT(BIC_Mod_c6);
3876 use_c1_residency_msr = 1;
3877 }
Len Brown7170a372017-01-27 02:13:27 -05003878 if (is_dnv(family, model)) {
3879 BIC_PRESENT(BIC_CPU_c1);
3880 BIC_NOT_PRESENT(BIC_CPU_c3);
3881 BIC_NOT_PRESENT(BIC_Pkgpc3);
3882 BIC_NOT_PRESENT(BIC_CPU_c7);
3883 BIC_NOT_PRESENT(BIC_Pkgpc7);
3884 use_c1_residency_msr = 1;
3885 }
Len Brown34c761972017-01-27 02:36:41 -05003886 if (is_skx(family, model)) {
3887 BIC_NOT_PRESENT(BIC_CPU_c3);
3888 BIC_NOT_PRESENT(BIC_Pkgpc3);
3889 BIC_NOT_PRESENT(BIC_CPU_c7);
3890 BIC_NOT_PRESENT(BIC_Pkgpc7);
3891 }
Len Brown0f47c082017-01-27 00:50:45 -05003892 if (has_hsw_msrs(family, model)) {
3893 BIC_PRESENT(BIC_Pkgpc8);
3894 BIC_PRESENT(BIC_Pkgpc9);
3895 BIC_PRESENT(BIC_Pkgpc10);
3896 }
Len Brown5a634262016-04-06 17:15:55 -04003897 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04003898 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05003899 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003900 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04003901
Len Brown96e47152017-01-21 02:26:00 -05003902 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05003903 decode_misc_pwr_mgmt_msr();
3904
Len Brown96e47152017-01-21 02:26:00 -05003905 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05003906 decode_c6_demotion_policy_msr();
3907
Len Brown889facb2012-11-08 00:48:57 -05003908 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04003909 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05003910
Len Brown96e47152017-01-21 02:26:00 -05003911 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00003912 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003913
Len Browna2b7b742015-09-26 00:12:38 -04003914 if (has_skl_msrs(family, model))
3915 calculate_tsc_tweak();
3916
Len Brown812db3f2017-02-10 00:25:41 -05003917 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
3918 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05003919
Len Brown812db3f2017-02-10 00:25:41 -05003920 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
3921 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05003922
Len Brown96e47152017-01-21 02:26:00 -05003923 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05003924 decode_misc_feature_control();
3925
Len Brown889facb2012-11-08 00:48:57 -05003926 return;
Len Brown103a8fe2010-10-22 23:53:03 -04003927}
3928
Len Brownd8af6f52015-02-10 01:56:38 -05003929void help()
Len Brown103a8fe2010-10-22 23:53:03 -04003930{
Len Brownb7d8c142016-02-13 23:36:17 -05003931 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05003932 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3933 "\n"
3934 "Turbostat forks the specified COMMAND and prints statistics\n"
3935 "when COMMAND completes.\n"
3936 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3937 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05003938 "--add add a counter\n"
3939 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brown96e47152017-01-21 02:26:00 -05003940 "--quiet skip decoding system configuration header\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003941 "--interval sec Override default 5-second measurement interval\n"
3942 "--help print this help message\n"
Len Brownb7d8c142016-02-13 23:36:17 -05003943 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003944 "--version print version information\n"
3945 "\n"
3946 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04003947}
3948
3949
3950/*
3951 * in /dev/cpu/ return success for names that are numbers
3952 * ie. filter out ".", "..", "microcode".
3953 */
3954int dir_filter(const struct dirent *dirp)
3955{
3956 if (isdigit(dirp->d_name[0]))
3957 return 1;
3958 else
3959 return 0;
3960}
3961
3962int open_dev_cpu_msr(int dummy1)
3963{
3964 return 0;
3965}
3966
Len Brownc98d5d92012-06-04 00:56:40 -04003967void topology_probe()
3968{
3969 int i;
3970 int max_core_id = 0;
3971 int max_package_id = 0;
3972 int max_siblings = 0;
3973 struct cpu_topology {
3974 int core_id;
3975 int physical_package_id;
3976 } *cpus;
3977
3978 /* Initialize num_cpus, max_cpu_num */
3979 topo.num_cpus = 0;
3980 topo.max_cpu_num = 0;
3981 for_all_proc_cpus(count_cpus);
3982 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003983 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04003984
Len Brownd8af6f52015-02-10 01:56:38 -05003985 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003986 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04003987
3988 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003989 if (cpus == NULL)
3990 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003991
3992 /*
3993 * Allocate and initialize cpu_present_set
3994 */
3995 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003996 if (cpu_present_set == NULL)
3997 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003998 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3999 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4000 for_all_proc_cpus(mark_cpu_present);
4001
4002 /*
4003 * Allocate and initialize cpu_affinity_set
4004 */
4005 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004006 if (cpu_affinity_set == NULL)
4007 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004008 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4009 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4010
4011
4012 /*
4013 * For online cpus
4014 * find max_core_id, max_package_id
4015 */
4016 for (i = 0; i <= topo.max_cpu_num; ++i) {
4017 int siblings;
4018
4019 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004020 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004021 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004022 continue;
4023 }
4024 cpus[i].core_id = get_core_id(i);
4025 if (cpus[i].core_id > max_core_id)
4026 max_core_id = cpus[i].core_id;
4027
4028 cpus[i].physical_package_id = get_physical_package_id(i);
4029 if (cpus[i].physical_package_id > max_package_id)
4030 max_package_id = cpus[i].physical_package_id;
4031
4032 siblings = get_num_ht_siblings(i);
4033 if (siblings > max_siblings)
4034 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004035 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004036 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004037 i, cpus[i].physical_package_id, cpus[i].core_id);
4038 }
4039 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004040 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004041 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004042 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004043 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004044 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004045
4046 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004047 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004048 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004049 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05004050 if (debug && !summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004051 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004052
4053 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004054 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004055 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004056
4057 free(cpus);
4058}
4059
4060void
4061allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4062{
4063 int i;
4064
4065 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004066 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004067 if (*t == NULL)
4068 goto error;
4069
4070 for (i = 0; i < topo.num_threads_per_core *
4071 topo.num_cores_per_pkg * topo.num_packages; i++)
4072 (*t)[i].cpu_id = -1;
4073
4074 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004075 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004076 if (*c == NULL)
4077 goto error;
4078
4079 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4080 (*c)[i].core_id = -1;
4081
Len Brown678a3bd2017-02-09 22:22:13 -05004082 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004083 if (*p == NULL)
4084 goto error;
4085
4086 for (i = 0; i < topo.num_packages; i++)
4087 (*p)[i].package_id = i;
4088
4089 return;
4090error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004091 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004092}
4093/*
4094 * init_counter()
4095 *
4096 * set cpu_id, core_num, pkg_num
4097 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4098 *
4099 * increment topo.num_cores when 1st core in pkg seen
4100 */
4101void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4102 struct pkg_data *pkg_base, int thread_num, int core_num,
4103 int pkg_num, int cpu_id)
4104{
4105 struct thread_data *t;
4106 struct core_data *c;
4107 struct pkg_data *p;
4108
4109 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4110 c = GET_CORE(core_base, core_num, pkg_num);
4111 p = GET_PKG(pkg_base, pkg_num);
4112
4113 t->cpu_id = cpu_id;
4114 if (thread_num == 0) {
4115 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4116 if (cpu_is_first_core_in_package(cpu_id))
4117 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4118 }
4119
4120 c->core_id = core_num;
4121 p->package_id = pkg_num;
4122}
4123
4124
4125int initialize_counters(int cpu_id)
4126{
4127 int my_thread_id, my_core_id, my_package_id;
4128
4129 my_package_id = get_physical_package_id(cpu_id);
4130 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004131 my_thread_id = get_cpu_position_in_core(cpu_id);
4132 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004133 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004134
4135 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4136 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4137 return 0;
4138}
4139
4140void allocate_output_buffer()
4141{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004142 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004143 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004144 if (outp == NULL)
4145 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004146}
Len Brown36229892016-02-26 20:51:02 -05004147void allocate_fd_percpu(void)
4148{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004149 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004150 if (fd_percpu == NULL)
4151 err(-1, "calloc fd_percpu");
4152}
Len Brown562a2d32016-02-26 23:48:05 -05004153void allocate_irq_buffers(void)
4154{
4155 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4156 if (irq_column_2_cpu == NULL)
4157 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004158
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004159 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004160 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004161 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004162}
Len Brownc98d5d92012-06-04 00:56:40 -04004163void setup_all_buffers(void)
4164{
4165 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004166 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004167 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004168 allocate_counters(&thread_even, &core_even, &package_even);
4169 allocate_counters(&thread_odd, &core_odd, &package_odd);
4170 allocate_output_buffer();
4171 for_all_proc_cpus(initialize_counters);
4172}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004173
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004174void set_base_cpu(void)
4175{
4176 base_cpu = sched_getcpu();
4177 if (base_cpu < 0)
4178 err(-ENODEV, "No valid cpus found");
4179
4180 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004181 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004182}
4183
Len Brown103a8fe2010-10-22 23:53:03 -04004184void turbostat_init()
4185{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004186 setup_all_buffers();
4187 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004188 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004189 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004190 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004191
Len Brown103a8fe2010-10-22 23:53:03 -04004192
Len Brown96e47152017-01-21 02:26:00 -05004193 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004194 for_all_cpus(print_hwp, ODD_COUNTERS);
4195
Len Brown96e47152017-01-21 02:26:00 -05004196 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004197 for_all_cpus(print_epb, ODD_COUNTERS);
4198
Len Brown96e47152017-01-21 02:26:00 -05004199 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004200 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4201
Len Brown96e47152017-01-21 02:26:00 -05004202 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004203 for_all_cpus(print_rapl, ODD_COUNTERS);
4204
4205 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4206
Len Brown96e47152017-01-21 02:26:00 -05004207 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004208 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004209
Len Brown96e47152017-01-21 02:26:00 -05004210 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004211 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004212}
4213
4214int fork_it(char **argv)
4215{
Len Brown103a8fe2010-10-22 23:53:03 -04004216 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004217 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004218
Len Brownd91bb172012-11-01 00:08:19 -04004219 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4220 if (status)
4221 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004222 /* clear affinity side-effect of get_counters() */
4223 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004224 gettimeofday(&tv_even, (struct timezone *)NULL);
4225
4226 child_pid = fork();
4227 if (!child_pid) {
4228 /* child */
4229 execvp(argv[0], argv);
4230 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004231
4232 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004233 if (child_pid == -1)
4234 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004235
4236 signal(SIGINT, SIG_IGN);
4237 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004238 if (waitpid(child_pid, &status, 0) == -1)
4239 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004240 }
Len Brownc98d5d92012-06-04 00:56:40 -04004241 /*
4242 * n.b. fork_it() does not check for errors from for_all_cpus()
4243 * because re-starting is problematic when forking
4244 */
4245 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004246 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004247 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004248 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4249 fprintf(outf, "%s: Counter reset detected\n", progname);
4250 else {
4251 compute_average(EVEN_COUNTERS);
4252 format_all_counters(EVEN_COUNTERS);
4253 }
Len Brown103a8fe2010-10-22 23:53:03 -04004254
Len Brownb7d8c142016-02-13 23:36:17 -05004255 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4256
4257 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004258
Len Brownd91bb172012-11-01 00:08:19 -04004259 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004260}
4261
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004262int get_and_dump_counters(void)
4263{
4264 int status;
4265
4266 status = for_all_cpus(get_counters, ODD_COUNTERS);
4267 if (status)
4268 return status;
4269
4270 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4271 if (status)
4272 return status;
4273
Len Brownb7d8c142016-02-13 23:36:17 -05004274 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004275
4276 return status;
4277}
4278
Len Brownd8af6f52015-02-10 01:56:38 -05004279void print_version() {
Len Brown0539ba12017-02-10 00:27:20 -05004280 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004281 " - Len Brown <lenb@kernel.org>\n");
4282}
4283
Len Brown388e9c82016-12-22 23:57:55 -05004284int add_counter(unsigned int msr_num, char *name, unsigned int width,
4285 enum counter_scope scope, enum counter_type type,
4286 enum counter_format format)
4287{
4288 struct msr_counter *msrp;
4289
4290 msrp = calloc(1, sizeof(struct msr_counter));
4291 if (msrp == NULL) {
4292 perror("calloc");
4293 exit(1);
4294 }
4295
4296 msrp->msr_num = msr_num;
4297 strncpy(msrp->name, name, NAME_BYTES);
4298 msrp->width = width;
4299 msrp->type = type;
4300 msrp->format = format;
4301
4302 switch (scope) {
4303
4304 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004305 msrp->next = sys.tp;
4306 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004307 sys.added_thread_counters++;
4308 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4309 fprintf(stderr, "exceeded max %d added thread counters\n",
4310 MAX_ADDED_COUNTERS);
4311 exit(-1);
4312 }
Len Brown388e9c82016-12-22 23:57:55 -05004313 break;
4314
4315 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004316 msrp->next = sys.cp;
4317 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004318 sys.added_core_counters++;
4319 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4320 fprintf(stderr, "exceeded max %d added core counters\n",
4321 MAX_ADDED_COUNTERS);
4322 exit(-1);
4323 }
Len Brown388e9c82016-12-22 23:57:55 -05004324 break;
4325
4326 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004327 msrp->next = sys.pp;
4328 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004329 sys.added_package_counters++;
4330 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4331 fprintf(stderr, "exceeded max %d added package counters\n",
4332 MAX_ADDED_COUNTERS);
4333 exit(-1);
4334 }
Len Brown388e9c82016-12-22 23:57:55 -05004335 break;
4336 }
4337
4338 return 0;
4339}
4340
4341void parse_add_command(char *add_command)
4342{
4343 int msr_num = 0;
Len Brown0f47c082017-01-27 00:50:45 -05004344 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004345 int width = 64;
4346 int fail = 0;
4347 enum counter_scope scope = SCOPE_CPU;
4348 enum counter_type type = COUNTER_CYCLES;
4349 enum counter_format format = FORMAT_DELTA;
4350
4351 while (add_command) {
4352
4353 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4354 goto next;
4355
4356 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4357 goto next;
4358
4359 if (sscanf(add_command, "u%d", &width) == 1) {
4360 if ((width == 32) || (width == 64))
4361 goto next;
4362 width = 64;
4363 }
4364 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4365 scope = SCOPE_CPU;
4366 goto next;
4367 }
4368 if (!strncmp(add_command, "core", strlen("core"))) {
4369 scope = SCOPE_CORE;
4370 goto next;
4371 }
4372 if (!strncmp(add_command, "package", strlen("package"))) {
4373 scope = SCOPE_PACKAGE;
4374 goto next;
4375 }
4376 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4377 type = COUNTER_CYCLES;
4378 goto next;
4379 }
4380 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4381 type = COUNTER_SECONDS;
4382 goto next;
4383 }
4384 if (!strncmp(add_command, "raw", strlen("raw"))) {
4385 format = FORMAT_RAW;
4386 goto next;
4387 }
4388 if (!strncmp(add_command, "delta", strlen("delta"))) {
4389 format = FORMAT_DELTA;
4390 goto next;
4391 }
4392 if (!strncmp(add_command, "percent", strlen("percent"))) {
4393 format = FORMAT_PERCENT;
4394 goto next;
4395 }
4396
4397 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4398 char *eos;
4399
4400 eos = strchr(name_buffer, ',');
4401 if (eos)
4402 *eos = '\0';
4403 goto next;
4404 }
4405
4406next:
4407 add_command = strchr(add_command, ',');
4408 if (add_command)
4409 add_command++;
4410
4411 }
4412 if (msr_num == 0) {
4413 fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4414 fail++;
4415 }
4416
4417 /* generate default column header */
4418 if (*name_buffer == '\0') {
4419 if (format == FORMAT_RAW) {
4420 if (width == 32)
4421 sprintf(name_buffer, "msr%d", msr_num);
4422 else
4423 sprintf(name_buffer, "MSR%d", msr_num);
4424 } else if (format == FORMAT_DELTA) {
4425 if (width == 32)
4426 sprintf(name_buffer, "cnt%d", msr_num);
4427 else
4428 sprintf(name_buffer, "CNT%d", msr_num);
4429 } else if (format == FORMAT_PERCENT) {
4430 if (width == 32)
4431 sprintf(name_buffer, "msr%d%%", msr_num);
4432 else
4433 sprintf(name_buffer, "MSR%d%%", msr_num);
4434 }
4435 }
4436
4437 if (add_counter(msr_num, name_buffer, width, scope, type, format))
4438 fail++;
4439
4440 if (fail) {
4441 help();
4442 exit(1);
4443 }
4444}
Len Brown812db3f2017-02-10 00:25:41 -05004445/*
4446 * HIDE_LIST - hide this list of counters, show the rest [default]
4447 * SHOW_LIST - show this list of counters, hide the rest
4448 */
4449enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4450
4451int shown;
4452/*
4453 * parse_show_hide() - process cmdline to set default counter action
4454 */
4455void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4456{
4457 /*
4458 * --show: show only those specified
4459 * The 1st invocation will clear and replace the enabled mask
4460 * subsequent invocations can add to it.
4461 */
4462 if (new_mode == SHOW_LIST) {
4463 if (shown == 0)
4464 bic_enabled = bic_lookup(optarg);
4465 else
4466 bic_enabled |= bic_lookup(optarg);
4467 shown = 1;
4468
4469 return;
4470 }
4471
4472 /*
4473 * --hide: do not show those specified
4474 * multiple invocations simply clear more bits in enabled mask
4475 */
4476 bic_enabled &= ~bic_lookup(optarg);
4477}
4478
Len Brown103a8fe2010-10-22 23:53:03 -04004479void cmdline(int argc, char **argv)
4480{
4481 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004482 int option_index = 0;
4483 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004484 {"add", required_argument, 0, 'a'},
Len Brownd8af6f52015-02-10 01:56:38 -05004485 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05004486 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brownd8af6f52015-02-10 01:56:38 -05004487 {"interval", required_argument, 0, 'i'},
4488 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004489 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004490 {"Joules", no_argument, 0, 'J'},
Len Brownb7d8c142016-02-13 23:36:17 -05004491 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004492 {"Package", no_argument, 0, 'p'},
4493 {"processor", no_argument, 0, 'p'},
Len Brown96e47152017-01-21 02:26:00 -05004494 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05004495 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004496 {"Summary", no_argument, 0, 'S'},
4497 {"TCC", required_argument, 0, 'T'},
4498 {"version", no_argument, 0, 'v' },
4499 {0, 0, 0, 0 }
4500 };
Len Brown103a8fe2010-10-22 23:53:03 -04004501
4502 progname = argv[0];
4503
Len Brown96e47152017-01-21 02:26:00 -05004504 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpqST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004505 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004506 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004507 case 'a':
4508 parse_add_command(optarg);
4509 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004510 case 'D':
4511 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004512 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004513 case 'd':
4514 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004515 break;
Len Brown812db3f2017-02-10 00:25:41 -05004516 case 'H':
4517 parse_show_hide(optarg, HIDE_LIST);
4518 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004519 case 'h':
4520 default:
4521 help();
4522 exit(1);
4523 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004524 {
4525 double interval = strtod(optarg, NULL);
4526
4527 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004528 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004529 interval);
4530 exit(2);
4531 }
4532
4533 interval_ts.tv_sec = interval;
4534 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4535 }
Len Brown889facb2012-11-08 00:48:57 -05004536 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004537 case 'J':
4538 rapl_joules++;
4539 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004540 case 'o':
4541 outf = fopen_or_die(optarg, "w");
4542 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004543 case 'P':
4544 show_pkg_only++;
4545 break;
4546 case 'p':
4547 show_core_only++;
4548 break;
Len Brown96e47152017-01-21 02:26:00 -05004549 case 'q':
4550 quiet = 1;
4551 break;
Len Brown812db3f2017-02-10 00:25:41 -05004552 case 's':
4553 parse_show_hide(optarg, SHOW_LIST);
4554 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004555 case 'S':
4556 summary_only++;
4557 break;
4558 case 'T':
4559 tcc_activation_temp_override = atoi(optarg);
4560 break;
4561 case 'v':
4562 print_version();
4563 exit(0);
4564 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004565 }
4566 }
4567}
4568
4569int main(int argc, char **argv)
4570{
Len Brownb7d8c142016-02-13 23:36:17 -05004571 outf = stderr;
4572
Len Brown103a8fe2010-10-22 23:53:03 -04004573 cmdline(argc, argv);
4574
Len Brown96e47152017-01-21 02:26:00 -05004575 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05004576 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004577
4578 turbostat_init();
4579
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004580 /* dump counters and exit */
4581 if (dump_only)
4582 return get_and_dump_counters();
4583
Len Brown103a8fe2010-10-22 23:53:03 -04004584 /*
4585 * if any params left, it must be a command to fork
4586 */
4587 if (argc - optind)
4588 return fork_it(argv + optind);
4589 else
4590 turbostat_loop();
4591
4592 return 0;
4593}