blob: fdf0273465fab88ace89f31249fd829e27b3e295 [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;
52unsigned int rapl_joules;
53unsigned int summary_only;
54unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040055unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070056unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050057unsigned int do_pc2;
58unsigned int do_pc3;
59unsigned int do_pc6;
60unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080061unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040062unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050063unsigned int do_slm_cstates;
64unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040065unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050066unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040067unsigned int do_irtl_snb;
68unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050069unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040070unsigned int genuine_intel;
71unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050072unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050073unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020074unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040075double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040076double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040077unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040078double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040079unsigned int show_pkg_only;
80unsigned int show_core_only;
81char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050082unsigned int do_rapl;
83unsigned int do_dts;
84unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050085unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050086unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050087unsigned int tcc_activation_temp;
88unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050089double rapl_power_units, rapl_time_units;
90double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050091double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040092unsigned int do_core_perf_limit_reasons;
93unsigned int do_gfx_perf_limit_reasons;
94unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040095unsigned int crystal_hz;
96unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040097int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040098double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050099unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
100 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
101unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
102unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
103unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
104unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown889facb2012-11-08 00:48:57 -0500105
Len Browne6f9bb32013-12-03 02:19:19 -0500106#define RAPL_PKG (1 << 0)
107 /* 0x610 MSR_PKG_POWER_LIMIT */
108 /* 0x611 MSR_PKG_ENERGY_STATUS */
109#define RAPL_PKG_PERF_STATUS (1 << 1)
110 /* 0x613 MSR_PKG_PERF_STATUS */
111#define RAPL_PKG_POWER_INFO (1 << 2)
112 /* 0x614 MSR_PKG_POWER_INFO */
113
114#define RAPL_DRAM (1 << 3)
115 /* 0x618 MSR_DRAM_POWER_LIMIT */
116 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500117#define RAPL_DRAM_PERF_STATUS (1 << 4)
118 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400119#define RAPL_DRAM_POWER_INFO (1 << 5)
120 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500121
Jacob Pan91484942016-06-16 09:48:20 -0700122#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500123 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400124#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500125 /* 0x63a MSR_PP0_POLICY */
126
Len Brown0b2bb692015-03-26 00:50:30 -0400127#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500128 /* 0x640 MSR_PP1_POWER_LIMIT */
129 /* 0x641 MSR_PP1_ENERGY_STATUS */
130 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700131
132#define RAPL_CORES_ENERGY_STATUS (1 << 9)
133 /* 0x639 MSR_PP0_ENERGY_STATUS */
134#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500135#define TJMAX_DEFAULT 100
136
137#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400138
Len Brown388e9c82016-12-22 23:57:55 -0500139/*
140 * buffer size used by sscanf() for added column names
141 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
142 */
143#define NAME_BYTES 20
144
Len Brown103a8fe2010-10-22 23:53:03 -0400145int backwards_count;
146char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400147
Len Brownc98d5d92012-06-04 00:56:40 -0400148cpu_set_t *cpu_present_set, *cpu_affinity_set;
149size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown678a3bd2017-02-09 22:22:13 -0500150#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400151
Len Brownc98d5d92012-06-04 00:56:40 -0400152struct thread_data {
153 unsigned long long tsc;
154 unsigned long long aperf;
155 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500156 unsigned long long c1;
Len Brown562a2d32016-02-26 23:48:05 -0500157 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500158 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400159 unsigned int cpu_id;
160 unsigned int flags;
161#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
162#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500163 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400164} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400165
Len Brownc98d5d92012-06-04 00:56:40 -0400166struct core_data {
167 unsigned long long c3;
168 unsigned long long c6;
169 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500170 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400171 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500172 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400173} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400174
Len Brownc98d5d92012-06-04 00:56:40 -0400175struct pkg_data {
176 unsigned long long pc2;
177 unsigned long long pc3;
178 unsigned long long pc6;
179 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800180 unsigned long long pc8;
181 unsigned long long pc9;
182 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400183 unsigned long long pkg_wtd_core_c0;
184 unsigned long long pkg_any_core_c0;
185 unsigned long long pkg_any_gfxe_c0;
186 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400187 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500188 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400189 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500190 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
191 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
192 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
193 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
194 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
195 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
196 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500197 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400198} *package_even, *package_odd;
199
200#define ODD_COUNTERS thread_odd, core_odd, package_odd
201#define EVEN_COUNTERS thread_even, core_even, package_even
202
203#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
204 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
205 topo.num_threads_per_core + \
206 (core_no) * topo.num_threads_per_core + (thread_no))
207#define GET_CORE(core_base, core_no, pkg_no) \
208 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
209#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
210
Len Brown388e9c82016-12-22 23:57:55 -0500211enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
212enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
213enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
214
215struct msr_counter {
216 unsigned int msr_num;
217 char name[NAME_BYTES];
218 unsigned int width;
219 enum counter_type type;
220 enum counter_format format;
221 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500222 unsigned int flags;
223#define FLAGS_HIDE (1 << 0)
224#define FLAGS_SHOW (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500225};
226
227struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500228 unsigned int added_thread_counters;
229 unsigned int added_core_counters;
230 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500231 struct msr_counter *tp;
232 struct msr_counter *cp;
233 struct msr_counter *pp;
234} sys;
235
Len Brownc98d5d92012-06-04 00:56:40 -0400236struct system_summary {
237 struct thread_data threads;
238 struct core_data cores;
239 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500240} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400241
242
243struct topo_params {
244 int num_packages;
245 int num_cpus;
246 int num_cores;
247 int max_cpu_num;
248 int num_cores_per_pkg;
249 int num_threads_per_core;
250} topo;
251
252struct timeval tv_even, tv_odd, tv_delta;
253
Len Brown562a2d32016-02-26 23:48:05 -0500254int *irq_column_2_cpu; /* /proc/interrupts column numbers */
255int *irqs_per_cpu; /* indexed by cpu_num */
256
Len Brownc98d5d92012-06-04 00:56:40 -0400257void setup_all_buffers(void);
258
259int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400260{
Len Brownc98d5d92012-06-04 00:56:40 -0400261 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400262}
Len Brown88c32812012-03-29 21:44:40 -0400263/*
Len Brownc98d5d92012-06-04 00:56:40 -0400264 * run func(thread, core, package) in topology order
265 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400266 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400267
Len Brownc98d5d92012-06-04 00:56:40 -0400268int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
269 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400270{
Len Brownc98d5d92012-06-04 00:56:40 -0400271 int retval, pkg_no, core_no, thread_no;
272
273 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
274 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
275 for (thread_no = 0; thread_no <
276 topo.num_threads_per_core; ++thread_no) {
277 struct thread_data *t;
278 struct core_data *c;
279 struct pkg_data *p;
280
281 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
282
283 if (cpu_is_not_present(t->cpu_id))
284 continue;
285
286 c = GET_CORE(core_base, core_no, pkg_no);
287 p = GET_PKG(pkg_base, pkg_no);
288
289 retval = func(t, c, p);
290 if (retval)
291 return retval;
292 }
293 }
294 }
295 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400296}
297
298int cpu_migrate(int cpu)
299{
Len Brownc98d5d92012-06-04 00:56:40 -0400300 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
301 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
302 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400303 return -1;
304 else
305 return 0;
306}
Len Brown36229892016-02-26 20:51:02 -0500307int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400308{
Len Brown103a8fe2010-10-22 23:53:03 -0400309 char pathname[32];
310 int fd;
311
Len Brown36229892016-02-26 20:51:02 -0500312 fd = fd_percpu[cpu];
313
314 if (fd)
315 return fd;
316
Len Brown103a8fe2010-10-22 23:53:03 -0400317 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
318 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400319 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400320 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 -0400321
Len Brown36229892016-02-26 20:51:02 -0500322 fd_percpu[cpu] = fd;
323
324 return fd;
325}
326
327int get_msr(int cpu, off_t offset, unsigned long long *msr)
328{
329 ssize_t retval;
330
331 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400332
Len Brown98481e72014-08-15 00:36:50 -0400333 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500334 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400335
336 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400337}
338
Len Brownfc04cc62014-02-06 00:55:19 -0500339/*
Len Brown812db3f2017-02-10 00:25:41 -0500340 * Each string in this array is compared in --show and --hide cmdline.
341 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500342 */
Len Brown812db3f2017-02-10 00:25:41 -0500343struct msr_counter bic[] = {
344 { 0x0, "Package" },
345 { 0x0, "Avg_MHz" },
346 { 0x0, "Bzy_MHz" },
347 { 0x0, "TSC_MHz" },
348 { 0x0, "IRQ" },
349 { 0x0, "SMI", 32, 0, FORMAT_DELTA, NULL},
350 { 0x0, "Busy%" },
351 { 0x0, "CPU%c1" },
352 { 0x0, "CPU%c3" },
353 { 0x0, "CPU%c6" },
354 { 0x0, "CPU%c7" },
355 { 0x0, "ThreadC" },
356 { 0x0, "CoreTmp" },
357 { 0x0, "CoreCnt" },
358 { 0x0, "PkgTmp" },
359 { 0x0, "GFX%rc6" },
360 { 0x0, "GFXMHz" },
361 { 0x0, "Pkg%pc2" },
362 { 0x0, "Pkg%pc3" },
363 { 0x0, "Pkg%pc6" },
364 { 0x0, "Pkg%pc7" },
365 { 0x0, "PkgWatt" },
366 { 0x0, "CorWatt" },
367 { 0x0, "GFXWatt" },
368 { 0x0, "PkgCnt" },
369 { 0x0, "RAMWatt" },
370 { 0x0, "PKG_%" },
371 { 0x0, "RAM_%" },
372 { 0x0, "Pkg_J" },
373 { 0x0, "Cor_J" },
374 { 0x0, "GFX_J" },
375 { 0x0, "RAM_J" },
376 { 0x0, "Core" },
377 { 0x0, "CPU" },
378};
379
380#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
381#define BIC_Package (1ULL << 0)
382#define BIC_Avg_MHz (1ULL << 1)
383#define BIC_Bzy_MHz (1ULL << 2)
384#define BIC_TSC_MHz (1ULL << 3)
385#define BIC_IRQ (1ULL << 4)
386#define BIC_SMI (1ULL << 5)
387#define BIC_Busy (1ULL << 6)
388#define BIC_CPU_c1 (1ULL << 7)
389#define BIC_CPU_c3 (1ULL << 8)
390#define BIC_CPU_c6 (1ULL << 9)
391#define BIC_CPU_c7 (1ULL << 10)
392#define BIC_ThreadC (1ULL << 11)
393#define BIC_CoreTmp (1ULL << 12)
394#define BIC_CoreCnt (1ULL << 13)
395#define BIC_PkgTmp (1ULL << 14)
396#define BIC_GFX_rc6 (1ULL << 15)
397#define BIC_GFXMHz (1ULL << 16)
398#define BIC_Pkgpc2 (1ULL << 17)
399#define BIC_Pkgpc3 (1ULL << 18)
400#define BIC_Pkgpc6 (1ULL << 19)
401#define BIC_Pkgpc7 (1ULL << 20)
402#define BIC_PkgWatt (1ULL << 21)
403#define BIC_CorWatt (1ULL << 22)
404#define BIC_GFXWatt (1ULL << 23)
405#define BIC_PkgCnt (1ULL << 24)
406#define BIC_RAMWatt (1ULL << 27)
407#define BIC_PKG__ (1ULL << 28)
408#define BIC_RAM__ (1ULL << 29)
409#define BIC_Pkg_J (1ULL << 30)
410#define BIC_Cor_J (1ULL << 31)
411#define BIC_GFX_J (1ULL << 30)
412#define BIC_RAM_J (1ULL << 31)
413#define BIC_Core (1ULL << 32)
414#define BIC_CPU (1ULL << 33)
415
416unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
417unsigned long long bic_present;
418
419#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
420#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
421
422/*
423 * bic_lookup
424 * for all the strings in comma separate name_list,
425 * set the approprate bit in return value.
426 */
427unsigned long long bic_lookup(char *name_list)
428{
429 int i;
430 unsigned long long retval = 0;
431
432 while (name_list) {
433 char *comma;
434
435 comma = strchr(name_list, ',');
436
437 if (comma)
438 *comma = '\0';
439
440 for (i = 0; i < MAX_BIC; ++i) {
441 if (!strcmp(name_list, bic[i].name)) {
442 retval |= (1ULL << i);
443 break;
444 }
445 }
446 if (i == MAX_BIC) {
447 fprintf(stderr, "Invalid counter name: %s\n", name_list);
448 exit(-1);
449 }
450
451 name_list = comma;
452 if (name_list)
453 name_list++;
454
455 }
456 return retval;
457}
Len Brownfc04cc62014-02-06 00:55:19 -0500458
Len Browna829eb42011-02-10 23:36:34 -0500459void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400460{
Len Brown388e9c82016-12-22 23:57:55 -0500461 struct msr_counter *mp;
462
Len Brown812db3f2017-02-10 00:25:41 -0500463 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400464 outp += sprintf(outp, "\tPackage");
Len Brown812db3f2017-02-10 00:25:41 -0500465 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400466 outp += sprintf(outp, "\tCore");
Len Brown812db3f2017-02-10 00:25:41 -0500467 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400468 outp += sprintf(outp, "\tCPU");
Len Brown812db3f2017-02-10 00:25:41 -0500469 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400470 outp += sprintf(outp, "\tAvg_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500471 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400472 outp += sprintf(outp, "\tBusy%%");
Len Brown812db3f2017-02-10 00:25:41 -0500473 if (DO_BIC(BIC_Bzy_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400474 outp += sprintf(outp, "\tBzy_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500475 if (DO_BIC(BIC_TSC_MHz))
476 outp += sprintf(outp, "\tTSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500477
Len Brown1cc21f72015-02-23 00:34:57 -0500478 if (!debug)
479 goto done;
480
Len Brown812db3f2017-02-10 00:25:41 -0500481 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400482 outp += sprintf(outp, "\tIRQ");
Len Brown812db3f2017-02-10 00:25:41 -0500483 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400484 outp += sprintf(outp, "\tSMI");
Len Brown1cc21f72015-02-23 00:34:57 -0500485
Len Brown812db3f2017-02-10 00:25:41 -0500486 if (DO_BIC(BIC_CPU_c1))
Len Brown3d109de2016-04-22 23:24:27 -0400487 outp += sprintf(outp, "\tCPU%%c1");
Len Brown889facb2012-11-08 00:48:57 -0500488
Len Brown388e9c82016-12-22 23:57:55 -0500489 for (mp = sys.tp; mp; mp = mp->next) {
490 if (mp->format == FORMAT_RAW) {
491 if (mp->width == 64)
492 outp += sprintf(outp, "\t%18.18s", mp->name);
493 else
494 outp += sprintf(outp, "\t%10.10s", mp->name);
495 } else {
496 outp += sprintf(outp, "\t%-7.7s", mp->name);
497 }
498 }
499
Len Brown812db3f2017-02-10 00:25:41 -0500500 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown678a3bd2017-02-09 22:22:13 -0500501 outp += sprintf(outp, "\tCPU%%c3");
Len Brown812db3f2017-02-10 00:25:41 -0500502 if (DO_BIC(BIC_CPU_c6))
Len Brown678a3bd2017-02-09 22:22:13 -0500503 outp += sprintf(outp, "\tCPU%%c6");
Len Brown812db3f2017-02-10 00:25:41 -0500504 if (DO_BIC(BIC_CPU_c7))
Len Brown678a3bd2017-02-09 22:22:13 -0500505 outp += sprintf(outp, "\tCPU%%c7");
506
507
Len Brown812db3f2017-02-10 00:25:41 -0500508 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400509 outp += sprintf(outp, "\tCoreTmp");
Len Brown388e9c82016-12-22 23:57:55 -0500510
511 for (mp = sys.cp; mp; mp = mp->next) {
512 if (mp->format == FORMAT_RAW) {
513 if (mp->width == 64)
514 outp += sprintf(outp, "\t%18.18s", mp->name);
515 else
516 outp += sprintf(outp, "\t%10.10s", mp->name);
517 } else {
518 outp += sprintf(outp, "\t%-7.7s", mp->name);
519 }
520 }
521
Len Brown812db3f2017-02-10 00:25:41 -0500522 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400523 outp += sprintf(outp, "\tPkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500524
Len Brown812db3f2017-02-10 00:25:41 -0500525 if (DO_BIC(BIC_GFX_rc6))
Len Brown3d109de2016-04-22 23:24:27 -0400526 outp += sprintf(outp, "\tGFX%%rc6");
Len Brownfdf676e2016-02-27 01:28:12 -0500527
Len Brown812db3f2017-02-10 00:25:41 -0500528 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400529 outp += sprintf(outp, "\tGFXMHz");
Len Brown27d47352016-02-27 00:37:54 -0500530
Len Brown0b2bb692015-03-26 00:50:30 -0400531 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400532 outp += sprintf(outp, "\tTotl%%C0");
533 outp += sprintf(outp, "\tAny%%C0");
534 outp += sprintf(outp, "\tGFX%%C0");
535 outp += sprintf(outp, "\tCPUGFX%%");
Len Brown0b2bb692015-03-26 00:50:30 -0400536 }
537
Len Brownee7e38e2015-02-09 23:39:45 -0500538 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400539 outp += sprintf(outp, "\tPkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500540 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400541 outp += sprintf(outp, "\tPkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500542 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400543 outp += sprintf(outp, "\tPkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500544 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400545 outp += sprintf(outp, "\tPkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800546 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400547 outp += sprintf(outp, "\tPkg%%pc8");
548 outp += sprintf(outp, "\tPkg%%pc9");
549 outp += sprintf(outp, "\tPk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800550 }
Len Brown103a8fe2010-10-22 23:53:03 -0400551
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800552 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500553 if (DO_BIC(BIC_PkgWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400554 outp += sprintf(outp, "\tPkgWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500555 if (DO_BIC(BIC_CorWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400556 outp += sprintf(outp, "\tCorWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500557 if (DO_BIC(BIC_GFXWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400558 outp += sprintf(outp, "\tGFXWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500559 if (DO_BIC(BIC_RAMWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400560 outp += sprintf(outp, "\tRAMWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500561 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400562 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500563 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400564 outp += sprintf(outp, "\tRAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500565 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500566 if (DO_BIC(BIC_Pkg_J))
Len Brown3d109de2016-04-22 23:24:27 -0400567 outp += sprintf(outp, "\tPkg_J");
Len Brown812db3f2017-02-10 00:25:41 -0500568 if (DO_BIC(BIC_Cor_J))
Len Brown3d109de2016-04-22 23:24:27 -0400569 outp += sprintf(outp, "\tCor_J");
Len Brown812db3f2017-02-10 00:25:41 -0500570 if (DO_BIC(BIC_GFX_J))
Len Brown3d109de2016-04-22 23:24:27 -0400571 outp += sprintf(outp, "\tGFX_J");
Len Brown812db3f2017-02-10 00:25:41 -0500572 if (DO_BIC(BIC_RAM_J))
Len Brown3d109de2016-04-22 23:24:27 -0400573 outp += sprintf(outp, "\tRAM_J");
Len Brown812db3f2017-02-10 00:25:41 -0500574 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400575 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500576 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400577 outp += sprintf(outp, "\tRAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800578 }
Len Brown388e9c82016-12-22 23:57:55 -0500579 for (mp = sys.pp; mp; mp = mp->next) {
580 if (mp->format == FORMAT_RAW) {
581 if (mp->width == 64)
582 outp += sprintf(outp, "\t%18.18s", mp->name);
583 else
584 outp += sprintf(outp, "\t%10.10s", mp->name);
585 } else {
586 outp += sprintf(outp, "\t%-7.7s", mp->name);
587 }
588 }
589
590done:
Len Brownc98d5d92012-06-04 00:56:40 -0400591 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400592}
593
Len Brownc98d5d92012-06-04 00:56:40 -0400594int dump_counters(struct thread_data *t, struct core_data *c,
595 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400596{
Len Brown388e9c82016-12-22 23:57:55 -0500597 int i;
598 struct msr_counter *mp;
599
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200600 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400601
Len Brownc98d5d92012-06-04 00:56:40 -0400602 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200603 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
604 t->cpu_id, t->flags);
605 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
606 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
607 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
608 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500609
Len Brown812db3f2017-02-10 00:25:41 -0500610 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -0500611 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500612 if (DO_BIC(BIC_SMI))
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200613 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500614
615 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
616 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
617 i, mp->msr_num, t->counter[i]);
618 }
Len Brownc98d5d92012-06-04 00:56:40 -0400619 }
Len Brown103a8fe2010-10-22 23:53:03 -0400620
Len Brownc98d5d92012-06-04 00:56:40 -0400621 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200622 outp += sprintf(outp, "core: %d\n", c->core_id);
623 outp += sprintf(outp, "c3: %016llX\n", c->c3);
624 outp += sprintf(outp, "c6: %016llX\n", c->c6);
625 outp += sprintf(outp, "c7: %016llX\n", c->c7);
626 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500627
628 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
629 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
630 i, mp->msr_num, c->counter[i]);
631 }
Len Brownc98d5d92012-06-04 00:56:40 -0400632 }
633
634 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200635 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400636
637 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
638 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
639 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
640 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
641
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200642 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500643 if (do_pc3)
644 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
645 if (do_pc6)
646 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
647 if (do_pc7)
648 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200649 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
650 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
651 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
652 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
653 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
654 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
655 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
656 outp += sprintf(outp, "Throttle PKG: %0X\n",
657 p->rapl_pkg_perf_status);
658 outp += sprintf(outp, "Throttle RAM: %0X\n",
659 p->rapl_dram_perf_status);
660 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500661
662 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
663 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
664 i, mp->msr_num, p->counter[i]);
665 }
Len Brownc98d5d92012-06-04 00:56:40 -0400666 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200667
668 outp += sprintf(outp, "\n");
669
Len Brownc98d5d92012-06-04 00:56:40 -0400670 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400671}
672
Len Browne23da032012-02-06 18:37:16 -0500673/*
674 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500675 */
Len Brownc98d5d92012-06-04 00:56:40 -0400676int format_counters(struct thread_data *t, struct core_data *c,
677 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400678{
679 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500680 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500681 int i;
682 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400683
Len Brownc98d5d92012-06-04 00:56:40 -0400684 /* if showing only 1st thread in core and this isn't one, bail out */
685 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
686 return 0;
687
688 /* if showing only 1st thread in pkg and this isn't one, bail out */
689 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
690 return 0;
691
Len Brown103a8fe2010-10-22 23:53:03 -0400692 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
693
Len Brownc98d5d92012-06-04 00:56:40 -0400694 /* topo columns, print blanks on 1st (average) line */
695 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500696 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400697 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500698 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400699 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500700 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400701 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400702 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500703 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400704 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400705 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400706 else
Len Brown3d109de2016-04-22 23:24:27 -0400707 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400708 }
Len Brown812db3f2017-02-10 00:25:41 -0500709 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400710 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400711 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400712 else
Len Brown3d109de2016-04-22 23:24:27 -0400713 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400714 }
Len Brown812db3f2017-02-10 00:25:41 -0500715 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400716 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400717 }
Len Brownfc04cc62014-02-06 00:55:19 -0500718
Len Brown812db3f2017-02-10 00:25:41 -0500719 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400720 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500721 1.0 / units * t->aperf / interval_float);
722
Len Brown812db3f2017-02-10 00:25:41 -0500723 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400724 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400725
Len Brown812db3f2017-02-10 00:25:41 -0500726 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400727 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400728 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400729 else
Len Brown3d109de2016-04-22 23:24:27 -0400730 outp += sprintf(outp, "\t%.0f",
Len Brown21ed5572015-10-19 22:37:40 -0400731 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
732 }
Len Brown103a8fe2010-10-22 23:53:03 -0400733
Len Brown812db3f2017-02-10 00:25:41 -0500734 if (DO_BIC(BIC_TSC_MHz))
735 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400736
Len Brown1cc21f72015-02-23 00:34:57 -0500737 if (!debug)
738 goto done;
739
Len Brown562a2d32016-02-26 23:48:05 -0500740 /* IRQ */
Len Brown812db3f2017-02-10 00:25:41 -0500741 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400742 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500743
Len Brown1cc21f72015-02-23 00:34:57 -0500744 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500745 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400746 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500747
Len Brown678a3bd2017-02-09 22:22:13 -0500748 /* C1 */
Len Brown812db3f2017-02-10 00:25:41 -0500749 if (DO_BIC(BIC_CPU_c1))
Len Brown3d109de2016-04-22 23:24:27 -0400750 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400751
Len Brown678a3bd2017-02-09 22:22:13 -0500752 /* Added counters */
753 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
754 if (mp->format == FORMAT_RAW) {
755 if (mp->width == 32)
756 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
757 else
758 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
759 } else if (mp->format == FORMAT_DELTA) {
760 outp += sprintf(outp, "\t%lld", t->counter[i]);
761 } else if (mp->format == FORMAT_PERCENT) {
762 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/t->tsc);
763 }
764 }
765
Len Brownc98d5d92012-06-04 00:56:40 -0400766 /* print per-core data only for 1st thread in core */
767 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
768 goto done;
769
Len Brown812db3f2017-02-10 00:25:41 -0500770 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400771 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500772 if (DO_BIC(BIC_CPU_c6))
Len Brown3d109de2016-04-22 23:24:27 -0400773 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500774 if (DO_BIC(BIC_CPU_c7))
Len Brown3d109de2016-04-22 23:24:27 -0400775 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400776
Len Brown812db3f2017-02-10 00:25:41 -0500777 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400778 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500779
Len Brown388e9c82016-12-22 23:57:55 -0500780 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
781 if (mp->format == FORMAT_RAW) {
782 if (mp->width == 32)
783 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
784 else
785 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
786 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500787 outp += sprintf(outp, "\t%lld", c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500788 } else if (mp->format == FORMAT_PERCENT) {
789 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/t->tsc);
790 }
791 }
792
Len Brownc98d5d92012-06-04 00:56:40 -0400793 /* print per-package data only for 1st core in package */
794 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
795 goto done;
796
Len Brown0b2bb692015-03-26 00:50:30 -0400797 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500798 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400799 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500800
Len Brownfdf676e2016-02-27 01:28:12 -0500801 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500802 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400803 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400804 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400805 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400806 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400807 p->gfx_rc6_ms / 10.0 / interval_float);
808 }
809 }
Len Brownfdf676e2016-02-27 01:28:12 -0500810
Len Brown27d47352016-02-27 00:37:54 -0500811 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500812 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400813 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500814
Len Brown0b2bb692015-03-26 00:50:30 -0400815 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
816 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400817 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
818 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
819 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
820 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400821 }
822
Len Brownee7e38e2015-02-09 23:39:45 -0500823 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400824 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500825 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400826 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500827 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400828 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500829 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400830 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800831 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400832 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
833 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
834 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800835 }
Len Brown889facb2012-11-08 00:48:57 -0500836
837 /*
838 * If measurement interval exceeds minimum RAPL Joule Counter range,
839 * indicate that results are suspect by printing "**" in fraction place.
840 */
Len Brownfc04cc62014-02-06 00:55:19 -0500841 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400842 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500843 else
Len Browne975db52016-04-06 23:56:02 -0400844 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500845
Len Brown812db3f2017-02-10 00:25:41 -0500846 if (DO_BIC(BIC_PkgWatt))
847 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
848 if (DO_BIC(BIC_CorWatt))
849 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
850 if (DO_BIC(BIC_GFXWatt))
851 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
852 if (DO_BIC(BIC_RAMWatt))
853 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
854 if (DO_BIC(BIC_Pkg_J))
855 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
856 if (DO_BIC(BIC_Cor_J))
857 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
858 if (DO_BIC(BIC_GFX_J))
859 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
860 if (DO_BIC(BIC_RAM_J))
861 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
862 if (DO_BIC(BIC_PKG__))
863 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
864 if (DO_BIC(BIC_RAM__))
865 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
866
Len Brown388e9c82016-12-22 23:57:55 -0500867 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
868 if (mp->format == FORMAT_RAW) {
869 if (mp->width == 32)
870 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
871 else
872 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
873 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500874 outp += sprintf(outp, "\t%lld", p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500875 } else if (mp->format == FORMAT_PERCENT) {
876 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/t->tsc);
877 }
878 }
879
Len Brownc98d5d92012-06-04 00:56:40 -0400880done:
Len Brownc98d5d92012-06-04 00:56:40 -0400881 outp += sprintf(outp, "\n");
882
883 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400884}
885
Len Brownb7d8c142016-02-13 23:36:17 -0500886void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400887{
Len Brownb7d8c142016-02-13 23:36:17 -0500888 FILE *filep;
889
890 if (outf == stderr)
891 filep = stdout;
892 else
893 filep = outf;
894
895 fputs(output_buffer, filep);
896 fflush(filep);
897
Len Brownc98d5d92012-06-04 00:56:40 -0400898 outp = output_buffer;
899}
Len Brownb7d8c142016-02-13 23:36:17 -0500900void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400901{
Len Brownb7d8c142016-02-13 23:36:17 -0500902 fputs(output_buffer, outf);
903 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400904 outp = output_buffer;
905}
906void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
907{
Len Browne23da032012-02-06 18:37:16 -0500908 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400909
Len Browne23da032012-02-06 18:37:16 -0500910 if (!printed || !summary_only)
911 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400912
Len Brownc98d5d92012-06-04 00:56:40 -0400913 if (topo.num_cpus > 1)
914 format_counters(&average.threads, &average.cores,
915 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400916
Len Browne23da032012-02-06 18:37:16 -0500917 printed = 1;
918
919 if (summary_only)
920 return;
921
Len Brownc98d5d92012-06-04 00:56:40 -0400922 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400923}
924
Len Brown889facb2012-11-08 00:48:57 -0500925#define DELTA_WRAP32(new, old) \
926 if (new > old) { \
927 old = new - old; \
928 } else { \
929 old = 0x100000000 + new - old; \
930 }
931
Len Brownba3dec92016-04-22 20:31:46 -0400932int
Len Brownc98d5d92012-06-04 00:56:40 -0400933delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400934{
Len Brown388e9c82016-12-22 23:57:55 -0500935 int i;
936 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400937
938 if (do_skl_residency) {
939 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
940 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
941 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
942 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
943 }
Len Brownc98d5d92012-06-04 00:56:40 -0400944 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500945 if (do_pc3)
946 old->pc3 = new->pc3 - old->pc3;
947 if (do_pc6)
948 old->pc6 = new->pc6 - old->pc6;
949 if (do_pc7)
950 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800951 old->pc8 = new->pc8 - old->pc8;
952 old->pc9 = new->pc9 - old->pc9;
953 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500954 old->pkg_temp_c = new->pkg_temp_c;
955
Len Brown9185e982016-04-06 17:16:00 -0400956 /* flag an error when rc6 counter resets/wraps */
957 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
958 old->gfx_rc6_ms = -1;
959 else
960 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
961
Len Brown27d47352016-02-27 00:37:54 -0500962 old->gfx_mhz = new->gfx_mhz;
963
Len Brown889facb2012-11-08 00:48:57 -0500964 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
965 DELTA_WRAP32(new->energy_cores, old->energy_cores);
966 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
967 DELTA_WRAP32(new->energy_dram, old->energy_dram);
968 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
969 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400970
Len Brown388e9c82016-12-22 23:57:55 -0500971 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
972 if (mp->format == FORMAT_RAW)
973 old->counter[i] = new->counter[i];
974 else
975 old->counter[i] = new->counter[i] - old->counter[i];
976 }
977
Len Brownba3dec92016-04-22 20:31:46 -0400978 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400979}
Len Brown103a8fe2010-10-22 23:53:03 -0400980
Len Brownc98d5d92012-06-04 00:56:40 -0400981void
982delta_core(struct core_data *new, struct core_data *old)
983{
Len Brown388e9c82016-12-22 23:57:55 -0500984 int i;
985 struct msr_counter *mp;
986
Len Brownc98d5d92012-06-04 00:56:40 -0400987 old->c3 = new->c3 - old->c3;
988 old->c6 = new->c6 - old->c6;
989 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500990 old->core_temp_c = new->core_temp_c;
Len Brown388e9c82016-12-22 23:57:55 -0500991
992 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
993 if (mp->format == FORMAT_RAW)
994 old->counter[i] = new->counter[i];
995 else
996 old->counter[i] = new->counter[i] - old->counter[i];
997 }
Len Brownc98d5d92012-06-04 00:56:40 -0400998}
Len Brown103a8fe2010-10-22 23:53:03 -0400999
Len Brownc3ae3312012-06-13 21:31:46 -04001000/*
1001 * old = new - old
1002 */
Len Brownba3dec92016-04-22 20:31:46 -04001003int
Len Brownc98d5d92012-06-04 00:56:40 -04001004delta_thread(struct thread_data *new, struct thread_data *old,
1005 struct core_data *core_delta)
1006{
Len Brown388e9c82016-12-22 23:57:55 -05001007 int i;
1008 struct msr_counter *mp;
1009
Len Brownc98d5d92012-06-04 00:56:40 -04001010 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001011
Len Brownc98d5d92012-06-04 00:56:40 -04001012 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001013 if (old->tsc < (1000 * 1000))
1014 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1015 "You can disable all c-states by booting with \"idle=poll\"\n"
1016 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001017
Len Brownc98d5d92012-06-04 00:56:40 -04001018 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001019
Len Brown812db3f2017-02-10 00:25:41 -05001020 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001021 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1022 old->aperf = new->aperf - old->aperf;
1023 old->mperf = new->mperf - old->mperf;
1024 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001025 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001026 }
Len Brownc98d5d92012-06-04 00:56:40 -04001027 }
Len Brown103a8fe2010-10-22 23:53:03 -04001028
Len Brown103a8fe2010-10-22 23:53:03 -04001029
Len Brown144b44b2013-11-09 00:30:16 -05001030 if (use_c1_residency_msr) {
1031 /*
1032 * Some models have a dedicated C1 residency MSR,
1033 * which should be more accurate than the derivation below.
1034 */
1035 } else {
1036 /*
1037 * As counter collection is not atomic,
1038 * it is possible for mperf's non-halted cycles + idle states
1039 * to exceed TSC's all cycles: show c1 = 0% in that case.
1040 */
1041 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1042 old->c1 = 0;
1043 else {
1044 /* normal case, derive c1 */
1045 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001046 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001047 }
Len Brownc98d5d92012-06-04 00:56:40 -04001048 }
Len Brownc3ae3312012-06-13 21:31:46 -04001049
Len Brownc98d5d92012-06-04 00:56:40 -04001050 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001051 if (debug > 1)
1052 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001053 old->mperf = 1; /* divide by 0 protection */
1054 }
1055
Len Brown812db3f2017-02-10 00:25:41 -05001056 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001057 old->irq_count = new->irq_count - old->irq_count;
1058
Len Brown812db3f2017-02-10 00:25:41 -05001059 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001060 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001061
Len Brown388e9c82016-12-22 23:57:55 -05001062 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1063 if (mp->format == FORMAT_RAW)
1064 old->counter[i] = new->counter[i];
1065 else
1066 old->counter[i] = new->counter[i] - old->counter[i];
1067 }
Len Brownba3dec92016-04-22 20:31:46 -04001068 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001069}
1070
1071int delta_cpu(struct thread_data *t, struct core_data *c,
1072 struct pkg_data *p, struct thread_data *t2,
1073 struct core_data *c2, struct pkg_data *p2)
1074{
Len Brownba3dec92016-04-22 20:31:46 -04001075 int retval = 0;
1076
Len Brownc98d5d92012-06-04 00:56:40 -04001077 /* calculate core delta only for 1st thread in core */
1078 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1079 delta_core(c, c2);
1080
1081 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001082 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1083 if (retval)
1084 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001085
1086 /* calculate package delta only for 1st core in package */
1087 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001088 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001089
Len Brownba3dec92016-04-22 20:31:46 -04001090 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001091}
1092
1093void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1094{
Len Brown388e9c82016-12-22 23:57:55 -05001095 int i;
1096 struct msr_counter *mp;
1097
Len Brownc98d5d92012-06-04 00:56:40 -04001098 t->tsc = 0;
1099 t->aperf = 0;
1100 t->mperf = 0;
1101 t->c1 = 0;
1102
Len Brown562a2d32016-02-26 23:48:05 -05001103 t->irq_count = 0;
1104 t->smi_count = 0;
1105
Len Brownc98d5d92012-06-04 00:56:40 -04001106 /* tells format_counters to dump all fields from this set */
1107 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1108
1109 c->c3 = 0;
1110 c->c6 = 0;
1111 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001112 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001113
Len Brown0b2bb692015-03-26 00:50:30 -04001114 p->pkg_wtd_core_c0 = 0;
1115 p->pkg_any_core_c0 = 0;
1116 p->pkg_any_gfxe_c0 = 0;
1117 p->pkg_both_core_gfxe_c0 = 0;
1118
Len Brownc98d5d92012-06-04 00:56:40 -04001119 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -05001120 if (do_pc3)
1121 p->pc3 = 0;
1122 if (do_pc6)
1123 p->pc6 = 0;
1124 if (do_pc7)
1125 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001126 p->pc8 = 0;
1127 p->pc9 = 0;
1128 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001129
1130 p->energy_pkg = 0;
1131 p->energy_dram = 0;
1132 p->energy_cores = 0;
1133 p->energy_gfx = 0;
1134 p->rapl_pkg_perf_status = 0;
1135 p->rapl_dram_perf_status = 0;
1136 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001137
Len Brownfdf676e2016-02-27 01:28:12 -05001138 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001139 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001140 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1141 t->counter[i] = 0;
1142
1143 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1144 c->counter[i] = 0;
1145
1146 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1147 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001148}
1149int sum_counters(struct thread_data *t, struct core_data *c,
1150 struct pkg_data *p)
1151{
Len Brown388e9c82016-12-22 23:57:55 -05001152 int i;
1153 struct msr_counter *mp;
1154
Len Brownc98d5d92012-06-04 00:56:40 -04001155 average.threads.tsc += t->tsc;
1156 average.threads.aperf += t->aperf;
1157 average.threads.mperf += t->mperf;
1158 average.threads.c1 += t->c1;
1159
Len Brown562a2d32016-02-26 23:48:05 -05001160 average.threads.irq_count += t->irq_count;
1161 average.threads.smi_count += t->smi_count;
1162
Len Brown388e9c82016-12-22 23:57:55 -05001163 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1164 if (mp->format == FORMAT_RAW)
1165 continue;
1166 average.threads.counter[i] += t->counter[i];
1167 }
1168
Len Brownc98d5d92012-06-04 00:56:40 -04001169 /* sum per-core values only for 1st thread in core */
1170 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1171 return 0;
1172
1173 average.cores.c3 += c->c3;
1174 average.cores.c6 += c->c6;
1175 average.cores.c7 += c->c7;
1176
Len Brown889facb2012-11-08 00:48:57 -05001177 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1178
Len Brown388e9c82016-12-22 23:57:55 -05001179 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1180 if (mp->format == FORMAT_RAW)
1181 continue;
1182 average.cores.counter[i] += c->counter[i];
1183 }
1184
Len Brownc98d5d92012-06-04 00:56:40 -04001185 /* sum per-pkg values only for 1st core in pkg */
1186 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1187 return 0;
1188
Len Brown0b2bb692015-03-26 00:50:30 -04001189 if (do_skl_residency) {
1190 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1191 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1192 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1193 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1194 }
1195
Len Brownc98d5d92012-06-04 00:56:40 -04001196 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -05001197 if (do_pc3)
1198 average.packages.pc3 += p->pc3;
1199 if (do_pc6)
1200 average.packages.pc6 += p->pc6;
1201 if (do_pc7)
1202 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001203 average.packages.pc8 += p->pc8;
1204 average.packages.pc9 += p->pc9;
1205 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001206
Len Brown889facb2012-11-08 00:48:57 -05001207 average.packages.energy_pkg += p->energy_pkg;
1208 average.packages.energy_dram += p->energy_dram;
1209 average.packages.energy_cores += p->energy_cores;
1210 average.packages.energy_gfx += p->energy_gfx;
1211
Len Brownfdf676e2016-02-27 01:28:12 -05001212 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001213 average.packages.gfx_mhz = p->gfx_mhz;
1214
Len Brown889facb2012-11-08 00:48:57 -05001215 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1216
1217 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1218 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001219
1220 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1221 if (mp->format == FORMAT_RAW)
1222 continue;
1223 average.packages.counter[i] += p->counter[i];
1224 }
Len Brownc98d5d92012-06-04 00:56:40 -04001225 return 0;
1226}
1227/*
1228 * sum the counters for all cpus in the system
1229 * compute the weighted average
1230 */
1231void compute_average(struct thread_data *t, struct core_data *c,
1232 struct pkg_data *p)
1233{
Len Brown388e9c82016-12-22 23:57:55 -05001234 int i;
1235 struct msr_counter *mp;
1236
Len Brownc98d5d92012-06-04 00:56:40 -04001237 clear_counters(&average.threads, &average.cores, &average.packages);
1238
1239 for_all_cpus(sum_counters, t, c, p);
1240
1241 average.threads.tsc /= topo.num_cpus;
1242 average.threads.aperf /= topo.num_cpus;
1243 average.threads.mperf /= topo.num_cpus;
1244 average.threads.c1 /= topo.num_cpus;
1245
1246 average.cores.c3 /= topo.num_cores;
1247 average.cores.c6 /= topo.num_cores;
1248 average.cores.c7 /= topo.num_cores;
1249
Len Brown0b2bb692015-03-26 00:50:30 -04001250 if (do_skl_residency) {
1251 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1252 average.packages.pkg_any_core_c0 /= topo.num_packages;
1253 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1254 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1255 }
1256
Len Brownc98d5d92012-06-04 00:56:40 -04001257 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -05001258 if (do_pc3)
1259 average.packages.pc3 /= topo.num_packages;
1260 if (do_pc6)
1261 average.packages.pc6 /= topo.num_packages;
1262 if (do_pc7)
1263 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001264
1265 average.packages.pc8 /= topo.num_packages;
1266 average.packages.pc9 /= topo.num_packages;
1267 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001268
1269 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1270 if (mp->format == FORMAT_RAW)
1271 continue;
1272 average.threads.counter[i] /= topo.num_cpus;
1273 }
1274 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1275 if (mp->format == FORMAT_RAW)
1276 continue;
1277 average.cores.counter[i] /= topo.num_cores;
1278 }
1279 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1280 if (mp->format == FORMAT_RAW)
1281 continue;
1282 average.packages.counter[i] /= topo.num_packages;
1283 }
Len Brownc98d5d92012-06-04 00:56:40 -04001284}
1285
1286static unsigned long long rdtsc(void)
1287{
1288 unsigned int low, high;
1289
1290 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1291
1292 return low | ((unsigned long long)high) << 32;
1293}
1294
Len Brownc98d5d92012-06-04 00:56:40 -04001295/*
1296 * get_counters(...)
1297 * migrate to cpu
1298 * acquire and record local counters for that cpu
1299 */
1300int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1301{
1302 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001303 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001304 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001305 struct msr_counter *mp;
1306 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001307
Len Browne52966c2012-11-08 22:38:05 -05001308 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001309 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001310 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001311 }
Len Brownc98d5d92012-06-04 00:56:40 -04001312
Len Brown0102b062016-02-27 03:11:29 -05001313retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001314 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1315
Len Brown812db3f2017-02-10 00:25:41 -05001316 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001317 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1318
1319 /*
1320 * The TSC, APERF and MPERF must be read together for
1321 * APERF/MPERF and MPERF/TSC to give accurate results.
1322 *
1323 * Unfortunately, APERF and MPERF are read by
1324 * individual system call, so delays may occur
1325 * between them. If the time to read them
1326 * varies by a large amount, we re-read them.
1327 */
1328
1329 /*
1330 * This initial dummy APERF read has been seen to
1331 * reduce jitter in the subsequent reads.
1332 */
1333
Len Brown9c63a652012-10-31 01:29:52 -04001334 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001335 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001336
1337 t->tsc = rdtsc(); /* re-read close to APERF */
1338
1339 tsc_before = t->tsc;
1340
1341 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1342 return -3;
1343
1344 tsc_between = rdtsc();
1345
Len Brown9c63a652012-10-31 01:29:52 -04001346 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001347 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001348
1349 tsc_after = rdtsc();
1350
1351 aperf_time = tsc_between - tsc_before;
1352 mperf_time = tsc_after - tsc_between;
1353
1354 /*
1355 * If the system call latency to read APERF and MPERF
1356 * differ by more than 2x, then try again.
1357 */
1358 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1359 aperf_mperf_retry_count++;
1360 if (aperf_mperf_retry_count < 5)
1361 goto retry;
1362 else
1363 warnx("cpu%d jitter %lld %lld",
1364 cpu, aperf_time, mperf_time);
1365 }
1366 aperf_mperf_retry_count = 0;
1367
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001368 t->aperf = t->aperf * aperf_mperf_multiplier;
1369 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001370 }
1371
Len Brown812db3f2017-02-10 00:25:41 -05001372 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001373 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001374 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001375 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1376 return -5;
1377 t->smi_count = msr & 0xFFFFFFFF;
1378 }
Len Brownc98d5d92012-06-04 00:56:40 -04001379
Len Brown144b44b2013-11-09 00:30:16 -05001380 if (use_c1_residency_msr) {
1381 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1382 return -6;
1383 }
1384
Len Brown388e9c82016-12-22 23:57:55 -05001385 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1386 if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1387 return -10;
1388 }
1389
1390
Len Brownc98d5d92012-06-04 00:56:40 -04001391 /* collect core counters only for 1st thread in core */
1392 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1393 return 0;
1394
Len Brown812db3f2017-02-10 00:25:41 -05001395 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001396 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1397 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001398 }
1399
Len Brown812db3f2017-02-10 00:25:41 -05001400 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001401 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1402 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001403 } else if (do_knl_cstates) {
1404 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1405 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001406 }
1407
Len Brown812db3f2017-02-10 00:25:41 -05001408 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001409 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1410 return -8;
1411
Len Brown812db3f2017-02-10 00:25:41 -05001412 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001413 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1414 return -9;
1415 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1416 }
1417
Len Brown388e9c82016-12-22 23:57:55 -05001418 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1419 if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1420 return -10;
1421 }
Len Brown889facb2012-11-08 00:48:57 -05001422
Len Brownc98d5d92012-06-04 00:56:40 -04001423 /* collect package counters only for 1st core in package */
1424 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1425 return 0;
1426
Len Brown0b2bb692015-03-26 00:50:30 -04001427 if (do_skl_residency) {
1428 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1429 return -10;
1430 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1431 return -11;
1432 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1433 return -12;
1434 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1435 return -13;
1436 }
Len Brownee7e38e2015-02-09 23:39:45 -05001437 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001438 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1439 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -05001440 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -04001441 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1442 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -05001443 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001444 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1445 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001446 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001447 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1448 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001449 if (do_c8_c9_c10) {
1450 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1451 return -13;
1452 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1453 return -13;
1454 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1455 return -13;
1456 }
Len Brown889facb2012-11-08 00:48:57 -05001457 if (do_rapl & RAPL_PKG) {
1458 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1459 return -13;
1460 p->energy_pkg = msr & 0xFFFFFFFF;
1461 }
Jacob Pan91484942016-06-16 09:48:20 -07001462 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001463 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1464 return -14;
1465 p->energy_cores = msr & 0xFFFFFFFF;
1466 }
1467 if (do_rapl & RAPL_DRAM) {
1468 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1469 return -15;
1470 p->energy_dram = msr & 0xFFFFFFFF;
1471 }
1472 if (do_rapl & RAPL_GFX) {
1473 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1474 return -16;
1475 p->energy_gfx = msr & 0xFFFFFFFF;
1476 }
1477 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1478 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1479 return -16;
1480 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1481 }
1482 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1483 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1484 return -16;
1485 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1486 }
Len Brown812db3f2017-02-10 00:25:41 -05001487 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001488 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1489 return -17;
1490 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1491 }
Len Brownfdf676e2016-02-27 01:28:12 -05001492
Len Brown812db3f2017-02-10 00:25:41 -05001493 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001494 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1495
Len Brown812db3f2017-02-10 00:25:41 -05001496 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001497 p->gfx_mhz = gfx_cur_mhz;
1498
Len Brown388e9c82016-12-22 23:57:55 -05001499 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1500 if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1501 return -10;
1502 }
1503
Len Brown103a8fe2010-10-22 23:53:03 -04001504 return 0;
1505}
1506
Len Brownee7e38e2015-02-09 23:39:45 -05001507/*
1508 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1509 * If you change the values, note they are used both in comparisons
1510 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1511 */
1512
1513#define PCLUKN 0 /* Unknown */
1514#define PCLRSV 1 /* Reserved */
1515#define PCL__0 2 /* PC0 */
1516#define PCL__1 3 /* PC1 */
1517#define PCL__2 4 /* PC2 */
1518#define PCL__3 5 /* PC3 */
1519#define PCL__4 6 /* PC4 */
1520#define PCL__6 7 /* PC6 */
1521#define PCL_6N 8 /* PC6 No Retention */
1522#define PCL_6R 9 /* PC6 Retention */
1523#define PCL__7 10 /* PC7 */
1524#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001525#define PCL__8 12 /* PC8 */
1526#define PCL__9 13 /* PC9 */
1527#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001528
1529int pkg_cstate_limit = PCLUKN;
1530char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001531 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001532
Len Browne9257f52015-04-01 21:02:57 -04001533int 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};
1534int 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};
1535int 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};
1536int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1537int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1538int 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 -04001539int 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 -05001540int 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 -05001541
Len Browna2b7b742015-09-26 00:12:38 -04001542
1543static void
1544calculate_tsc_tweak()
1545{
Len Browna2b7b742015-09-26 00:12:38 -04001546 tsc_tweak = base_hz / tsc_hz;
1547}
1548
Len Brownfcd17212015-03-23 20:29:09 -04001549static void
1550dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001551{
1552 unsigned long long msr;
1553 unsigned int ratio;
1554
Len Brownec0adc52015-11-12 02:42:31 -05001555 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001556
Len Brownb7d8c142016-02-13 23:36:17 -05001557 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001558
Len Brown103a8fe2010-10-22 23:53:03 -04001559 ratio = (msr >> 40) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001560 fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001561 ratio, bclk, ratio * bclk);
1562
1563 ratio = (msr >> 8) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001564 fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001565 ratio, bclk, ratio * bclk);
1566
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001567 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001568 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001569 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001570
Len Brownfcd17212015-03-23 20:29:09 -04001571 return;
1572}
1573
1574static void
1575dump_hsw_turbo_ratio_limits(void)
1576{
1577 unsigned long long msr;
1578 unsigned int ratio;
1579
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001580 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001581
Len Brownb7d8c142016-02-13 23:36:17 -05001582 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001583
1584 ratio = (msr >> 8) & 0xFF;
1585 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001586 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001587 ratio, bclk, ratio * bclk);
1588
1589 ratio = (msr >> 0) & 0xFF;
1590 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001591 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001592 ratio, bclk, ratio * bclk);
1593 return;
1594}
1595
1596static void
1597dump_ivt_turbo_ratio_limits(void)
1598{
1599 unsigned long long msr;
1600 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001601
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001602 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001603
Len Brownb7d8c142016-02-13 23:36:17 -05001604 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001605
1606 ratio = (msr >> 56) & 0xFF;
1607 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001608 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001609 ratio, bclk, ratio * bclk);
1610
1611 ratio = (msr >> 48) & 0xFF;
1612 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001613 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001614 ratio, bclk, ratio * bclk);
1615
1616 ratio = (msr >> 40) & 0xFF;
1617 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001618 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001619 ratio, bclk, ratio * bclk);
1620
1621 ratio = (msr >> 32) & 0xFF;
1622 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001623 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001624 ratio, bclk, ratio * bclk);
1625
1626 ratio = (msr >> 24) & 0xFF;
1627 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001628 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001629 ratio, bclk, ratio * bclk);
1630
1631 ratio = (msr >> 16) & 0xFF;
1632 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001633 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001634 ratio, bclk, ratio * bclk);
1635
1636 ratio = (msr >> 8) & 0xFF;
1637 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001638 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001639 ratio, bclk, ratio * bclk);
1640
1641 ratio = (msr >> 0) & 0xFF;
1642 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001643 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001644 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001645 return;
1646}
Len Brown6574a5d2012-09-21 00:01:31 -04001647
Len Brownfcd17212015-03-23 20:29:09 -04001648static void
1649dump_nhm_turbo_ratio_limits(void)
1650{
1651 unsigned long long msr;
1652 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001653
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001654 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001655
Len Brownb7d8c142016-02-13 23:36:17 -05001656 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001657
1658 ratio = (msr >> 56) & 0xFF;
1659 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001660 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001661 ratio, bclk, ratio * bclk);
1662
1663 ratio = (msr >> 48) & 0xFF;
1664 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001665 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001666 ratio, bclk, ratio * bclk);
1667
1668 ratio = (msr >> 40) & 0xFF;
1669 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001670 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001671 ratio, bclk, ratio * bclk);
1672
1673 ratio = (msr >> 32) & 0xFF;
1674 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001675 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001676 ratio, bclk, ratio * bclk);
1677
Len Brown103a8fe2010-10-22 23:53:03 -04001678 ratio = (msr >> 24) & 0xFF;
1679 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001680 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001681 ratio, bclk, ratio * bclk);
1682
1683 ratio = (msr >> 16) & 0xFF;
1684 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001685 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001686 ratio, bclk, ratio * bclk);
1687
1688 ratio = (msr >> 8) & 0xFF;
1689 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001690 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001691 ratio, bclk, ratio * bclk);
1692
1693 ratio = (msr >> 0) & 0xFF;
1694 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001695 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001696 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001697 return;
1698}
Len Brown3a9a9412014-08-15 02:39:52 -04001699
Len Brownfcd17212015-03-23 20:29:09 -04001700static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001701dump_knl_turbo_ratio_limits(void)
1702{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001703 const unsigned int buckets_no = 7;
1704
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001705 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001706 int delta_cores, delta_ratio;
1707 int i, b_nr;
1708 unsigned int cores[buckets_no];
1709 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001710
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001711 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001712
Len Brownb7d8c142016-02-13 23:36:17 -05001713 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001714 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001715
1716 /**
1717 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001718 * [0] -- Reserved
1719 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001720 * [15:8] -- Base value of freq ratio of bucket 1.
1721 * [20:16] -- +ve delta of number of active cores of bucket 2.
1722 * i.e. active cores of bucket 2 =
1723 * active cores of bucket 1 + delta
1724 * [23:21] -- Negative delta of freq ratio of bucket 2.
1725 * i.e. freq ratio of bucket 2 =
1726 * freq ratio of bucket 1 - delta
1727 * [28:24]-- +ve delta of number of active cores of bucket 3.
1728 * [31:29]-- -ve delta of freq ratio of bucket 3.
1729 * [36:32]-- +ve delta of number of active cores of bucket 4.
1730 * [39:37]-- -ve delta of freq ratio of bucket 4.
1731 * [44:40]-- +ve delta of number of active cores of bucket 5.
1732 * [47:45]-- -ve delta of freq ratio of bucket 5.
1733 * [52:48]-- +ve delta of number of active cores of bucket 6.
1734 * [55:53]-- -ve delta of freq ratio of bucket 6.
1735 * [60:56]-- +ve delta of number of active cores of bucket 7.
1736 * [63:61]-- -ve delta of freq ratio of bucket 7.
1737 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001738
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001739 b_nr = 0;
1740 cores[b_nr] = (msr & 0xFF) >> 1;
1741 ratio[b_nr] = (msr >> 8) & 0xFF;
1742
1743 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001744 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001745 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001746
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001747 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1748 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1749 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001750 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001751
1752 for (i = buckets_no - 1; i >= 0; i--)
1753 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001754 fprintf(outf,
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001755 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001756 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001757}
1758
1759static void
Len Brownfcd17212015-03-23 20:29:09 -04001760dump_nhm_cst_cfg(void)
1761{
1762 unsigned long long msr;
1763
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001764 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001765
1766#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1767#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1768
Len Brownb7d8c142016-02-13 23:36:17 -05001769 fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001770
Len Brownb7d8c142016-02-13 23:36:17 -05001771 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001772 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1773 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1774 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1775 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1776 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001777 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001778 pkg_cstate_limit_strings[pkg_cstate_limit]);
1779 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001780}
1781
Len Brown6fb31432015-06-17 16:23:45 -04001782static void
1783dump_config_tdp(void)
1784{
1785 unsigned long long msr;
1786
1787 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001788 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001789 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001790
1791 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001792 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001793 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001794 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1795 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1796 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1797 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001798 }
Len Brownb7d8c142016-02-13 23:36:17 -05001799 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001800
1801 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001802 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001803 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001804 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1805 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1806 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1807 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001808 }
Len Brownb7d8c142016-02-13 23:36:17 -05001809 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001810
1811 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001812 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001813 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001814 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1815 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1816 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001817
Len Brown6fb31432015-06-17 16:23:45 -04001818 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001819 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001820 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001821 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1822 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001823}
Len Brown5a634262016-04-06 17:15:55 -04001824
1825unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1826
1827void print_irtl(void)
1828{
1829 unsigned long long msr;
1830
1831 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1832 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1833 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1834 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1835
1836 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
1837 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
1838 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1839 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1840
1841 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
1842 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
1843 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1844 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1845
1846 if (!do_irtl_hsw)
1847 return;
1848
1849 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
1850 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
1851 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1852 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1853
1854 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
1855 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
1856 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1857 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1858
1859 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
1860 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
1861 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1862 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1863
1864}
Len Brown36229892016-02-26 20:51:02 -05001865void free_fd_percpu(void)
1866{
1867 int i;
1868
Mika Westerberg01a67ad2016-04-22 11:13:23 +03001869 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05001870 if (fd_percpu[i] != 0)
1871 close(fd_percpu[i]);
1872 }
1873
1874 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04001875}
1876
Len Brownc98d5d92012-06-04 00:56:40 -04001877void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001878{
Len Brownc98d5d92012-06-04 00:56:40 -04001879 CPU_FREE(cpu_present_set);
1880 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05001881 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001882
Len Brownc98d5d92012-06-04 00:56:40 -04001883 CPU_FREE(cpu_affinity_set);
1884 cpu_affinity_set = NULL;
1885 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001886
Len Brownc98d5d92012-06-04 00:56:40 -04001887 free(thread_even);
1888 free(core_even);
1889 free(package_even);
1890
1891 thread_even = NULL;
1892 core_even = NULL;
1893 package_even = NULL;
1894
1895 free(thread_odd);
1896 free(core_odd);
1897 free(package_odd);
1898
1899 thread_odd = NULL;
1900 core_odd = NULL;
1901 package_odd = NULL;
1902
1903 free(output_buffer);
1904 output_buffer = NULL;
1905 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05001906
1907 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05001908
1909 free(irq_column_2_cpu);
1910 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001911}
1912
Len Brownc98d5d92012-06-04 00:56:40 -04001913/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001914 * Open a file, and exit on failure
1915 */
1916FILE *fopen_or_die(const char *path, const char *mode)
1917{
Len Brownb7d8c142016-02-13 23:36:17 -05001918 FILE *filep = fopen(path, mode);
Josh Triplettb2c95d92013-08-20 17:20:18 -07001919 if (!filep)
1920 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001921 return filep;
1922}
1923
1924/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001925 * Parse a file containing a single int.
1926 */
1927int parse_int_file(const char *fmt, ...)
1928{
1929 va_list args;
1930 char path[PATH_MAX];
1931 FILE *filep;
1932 int value;
1933
1934 va_start(args, fmt);
1935 vsnprintf(path, sizeof(path), fmt, args);
1936 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001937 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001938 if (fscanf(filep, "%d", &value) != 1)
1939 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001940 fclose(filep);
1941 return value;
1942}
1943
1944/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001945 * get_cpu_position_in_core(cpu)
1946 * return the position of the CPU among its HT siblings in the core
1947 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04001948 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001949int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001950{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001951 char path[64];
1952 FILE *filep;
1953 int this_cpu;
1954 char character;
1955 int i;
1956
1957 sprintf(path,
1958 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1959 cpu);
1960 filep = fopen(path, "r");
1961 if (filep == NULL) {
1962 perror(path);
1963 exit(1);
1964 }
1965
1966 for (i = 0; i < topo.num_threads_per_core; i++) {
1967 fscanf(filep, "%d", &this_cpu);
1968 if (this_cpu == cpu) {
1969 fclose(filep);
1970 return i;
1971 }
1972
1973 /* Account for no separator after last thread*/
1974 if (i != (topo.num_threads_per_core - 1))
1975 fscanf(filep, "%c", &character);
1976 }
1977
1978 fclose(filep);
1979 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04001980}
1981
Len Brownc98d5d92012-06-04 00:56:40 -04001982/*
1983 * cpu_is_first_core_in_package(cpu)
1984 * return 1 if given CPU is 1st core in package
1985 */
1986int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001987{
Josh Triplett95aebc42013-08-20 17:20:16 -07001988 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001989}
1990
1991int get_physical_package_id(int cpu)
1992{
Josh Triplett95aebc42013-08-20 17:20:16 -07001993 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001994}
1995
1996int get_core_id(int cpu)
1997{
Josh Triplett95aebc42013-08-20 17:20:16 -07001998 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001999}
2000
Len Brownc98d5d92012-06-04 00:56:40 -04002001int get_num_ht_siblings(int cpu)
2002{
2003 char path[80];
2004 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002005 int sib1;
2006 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002007 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002008 char str[100];
2009 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002010
2011 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002012 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002013
Len Brownc98d5d92012-06-04 00:56:40 -04002014 /*
2015 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002016 * A ',' separated or '-' separated set of numbers
2017 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002018 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002019 fscanf(filep, "%d%c\n", &sib1, &character);
2020 fseek(filep, 0, SEEK_SET);
2021 fgets(str, 100, filep);
2022 ch = strchr(str, character);
2023 while (ch != NULL) {
2024 matches++;
2025 ch = strchr(ch+1, character);
2026 }
Len Brownc98d5d92012-06-04 00:56:40 -04002027
2028 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002029 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002030}
2031
Len Brown103a8fe2010-10-22 23:53:03 -04002032/*
Len Brownc98d5d92012-06-04 00:56:40 -04002033 * run func(thread, core, package) in topology order
2034 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002035 */
2036
Len Brownc98d5d92012-06-04 00:56:40 -04002037int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2038 struct pkg_data *, struct thread_data *, struct core_data *,
2039 struct pkg_data *), struct thread_data *thread_base,
2040 struct core_data *core_base, struct pkg_data *pkg_base,
2041 struct thread_data *thread_base2, struct core_data *core_base2,
2042 struct pkg_data *pkg_base2)
2043{
2044 int retval, pkg_no, core_no, thread_no;
2045
2046 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2047 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2048 for (thread_no = 0; thread_no <
2049 topo.num_threads_per_core; ++thread_no) {
2050 struct thread_data *t, *t2;
2051 struct core_data *c, *c2;
2052 struct pkg_data *p, *p2;
2053
2054 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2055
2056 if (cpu_is_not_present(t->cpu_id))
2057 continue;
2058
2059 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2060
2061 c = GET_CORE(core_base, core_no, pkg_no);
2062 c2 = GET_CORE(core_base2, core_no, pkg_no);
2063
2064 p = GET_PKG(pkg_base, pkg_no);
2065 p2 = GET_PKG(pkg_base2, pkg_no);
2066
2067 retval = func(t, c, p, t2, c2, p2);
2068 if (retval)
2069 return retval;
2070 }
2071 }
2072 }
2073 return 0;
2074}
2075
2076/*
2077 * run func(cpu) on every cpu in /proc/stat
2078 * return max_cpu number
2079 */
2080int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002081{
2082 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002083 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002084 int retval;
2085
Josh Triplett57a42a32013-08-20 17:20:17 -07002086 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002087
2088 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002089 if (retval != 0)
2090 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002091
Len Brownc98d5d92012-06-04 00:56:40 -04002092 while (1) {
2093 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 -04002094 if (retval != 1)
2095 break;
2096
Len Brownc98d5d92012-06-04 00:56:40 -04002097 retval = func(cpu_num);
2098 if (retval) {
2099 fclose(fp);
2100 return(retval);
2101 }
Len Brown103a8fe2010-10-22 23:53:03 -04002102 }
2103 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002104 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002105}
2106
2107void re_initialize(void)
2108{
Len Brownc98d5d92012-06-04 00:56:40 -04002109 free_all_buffers();
2110 setup_all_buffers();
2111 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002112}
2113
Len Brownc98d5d92012-06-04 00:56:40 -04002114
Len Brown103a8fe2010-10-22 23:53:03 -04002115/*
Len Brownc98d5d92012-06-04 00:56:40 -04002116 * count_cpus()
2117 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002118 */
Len Brownc98d5d92012-06-04 00:56:40 -04002119int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002120{
Len Brownc98d5d92012-06-04 00:56:40 -04002121 if (topo.max_cpu_num < cpu)
2122 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002123
Len Brownc98d5d92012-06-04 00:56:40 -04002124 topo.num_cpus += 1;
2125 return 0;
2126}
2127int mark_cpu_present(int cpu)
2128{
2129 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002130 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002131}
2132
Len Brown562a2d32016-02-26 23:48:05 -05002133/*
2134 * snapshot_proc_interrupts()
2135 *
2136 * read and record summary of /proc/interrupts
2137 *
2138 * return 1 if config change requires a restart, else return 0
2139 */
2140int snapshot_proc_interrupts(void)
2141{
2142 static FILE *fp;
2143 int column, retval;
2144
2145 if (fp == NULL)
2146 fp = fopen_or_die("/proc/interrupts", "r");
2147 else
2148 rewind(fp);
2149
2150 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2151 for (column = 0; column < topo.num_cpus; ++column) {
2152 int cpu_number;
2153
2154 retval = fscanf(fp, " CPU%d", &cpu_number);
2155 if (retval != 1)
2156 break;
2157
2158 if (cpu_number > topo.max_cpu_num) {
2159 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2160 return 1;
2161 }
2162
2163 irq_column_2_cpu[column] = cpu_number;
2164 irqs_per_cpu[cpu_number] = 0;
2165 }
2166
2167 /* read /proc/interrupt count lines and sum up irqs per cpu */
2168 while (1) {
2169 int column;
2170 char buf[64];
2171
2172 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2173 if (retval != 1)
2174 break;
2175
2176 /* read the count per cpu */
2177 for (column = 0; column < topo.num_cpus; ++column) {
2178
2179 int cpu_number, irq_count;
2180
2181 retval = fscanf(fp, " %d", &irq_count);
2182 if (retval != 1)
2183 break;
2184
2185 cpu_number = irq_column_2_cpu[column];
2186 irqs_per_cpu[cpu_number] += irq_count;
2187
2188 }
2189
2190 while (getc(fp) != '\n')
2191 ; /* flush interrupt description */
2192
2193 }
2194 return 0;
2195}
Len Brown27d47352016-02-27 00:37:54 -05002196/*
Len Brownfdf676e2016-02-27 01:28:12 -05002197 * snapshot_gfx_rc6_ms()
2198 *
2199 * record snapshot of
2200 * /sys/class/drm/card0/power/rc6_residency_ms
2201 *
2202 * return 1 if config change requires a restart, else return 0
2203 */
2204int snapshot_gfx_rc6_ms(void)
2205{
2206 FILE *fp;
2207 int retval;
2208
2209 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2210
2211 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2212 if (retval != 1)
2213 err(1, "GFX rc6");
2214
2215 fclose(fp);
2216
2217 return 0;
2218}
2219/*
Len Brown27d47352016-02-27 00:37:54 -05002220 * snapshot_gfx_mhz()
2221 *
2222 * record snapshot of
2223 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2224 *
2225 * return 1 if config change requires a restart, else return 0
2226 */
2227int snapshot_gfx_mhz(void)
2228{
2229 static FILE *fp;
2230 int retval;
2231
2232 if (fp == NULL)
2233 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2234 else
2235 rewind(fp);
2236
2237 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2238 if (retval != 1)
2239 err(1, "GFX MHz");
2240
2241 return 0;
2242}
Len Brown562a2d32016-02-26 23:48:05 -05002243
2244/*
2245 * snapshot /proc and /sys files
2246 *
2247 * return 1 if configuration restart needed, else return 0
2248 */
2249int snapshot_proc_sysfs_files(void)
2250{
2251 if (snapshot_proc_interrupts())
2252 return 1;
2253
Len Brown812db3f2017-02-10 00:25:41 -05002254 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002255 snapshot_gfx_rc6_ms();
2256
Len Brown812db3f2017-02-10 00:25:41 -05002257 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002258 snapshot_gfx_mhz();
2259
Len Brown562a2d32016-02-26 23:48:05 -05002260 return 0;
2261}
2262
Len Brown103a8fe2010-10-22 23:53:03 -04002263void turbostat_loop()
2264{
Len Brownc98d5d92012-06-04 00:56:40 -04002265 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002266 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002267
Len Brown103a8fe2010-10-22 23:53:03 -04002268restart:
Len Browne52966c2012-11-08 22:38:05 -05002269 restarted++;
2270
Len Brown562a2d32016-02-26 23:48:05 -05002271 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002272 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002273 if (retval < -1) {
2274 exit(retval);
2275 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002276 if (restarted > 1) {
2277 exit(retval);
2278 }
Len Brownc98d5d92012-06-04 00:56:40 -04002279 re_initialize();
2280 goto restart;
2281 }
Len Browne52966c2012-11-08 22:38:05 -05002282 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002283 gettimeofday(&tv_even, (struct timezone *)NULL);
2284
2285 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002286 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002287 re_initialize();
2288 goto restart;
2289 }
Len Brown2a0609c2016-02-12 22:44:48 -05002290 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002291 if (snapshot_proc_sysfs_files())
2292 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002293 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002294 if (retval < -1) {
2295 exit(retval);
2296 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002297 re_initialize();
2298 goto restart;
2299 }
Len Brown103a8fe2010-10-22 23:53:03 -04002300 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002301 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002302 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2303 re_initialize();
2304 goto restart;
2305 }
Len Brownc98d5d92012-06-04 00:56:40 -04002306 compute_average(EVEN_COUNTERS);
2307 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002308 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002309 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002310 if (snapshot_proc_sysfs_files())
2311 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002312 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002313 if (retval < -1) {
2314 exit(retval);
2315 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002316 re_initialize();
2317 goto restart;
2318 }
Len Brown103a8fe2010-10-22 23:53:03 -04002319 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002320 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002321 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2322 re_initialize();
2323 goto restart;
2324 }
Len Brownc98d5d92012-06-04 00:56:40 -04002325 compute_average(ODD_COUNTERS);
2326 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002327 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002328 }
2329}
2330
2331void check_dev_msr()
2332{
2333 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002334 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002335
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002336 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2337 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002338 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2339 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002340}
2341
Len Brown98481e72014-08-15 00:36:50 -04002342void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002343{
Len Brown98481e72014-08-15 00:36:50 -04002344 struct __user_cap_header_struct cap_header_data;
2345 cap_user_header_t cap_header = &cap_header_data;
2346 struct __user_cap_data_struct cap_data_data;
2347 cap_user_data_t cap_data = &cap_data_data;
2348 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2349 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002350 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002351
2352 /* check for CAP_SYS_RAWIO */
2353 cap_header->pid = getpid();
2354 cap_header->version = _LINUX_CAPABILITY_VERSION;
2355 if (capget(cap_header, cap_data) < 0)
2356 err(-6, "capget(2) failed");
2357
2358 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2359 do_exit++;
2360 warnx("capget(CAP_SYS_RAWIO) failed,"
2361 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2362 }
2363
2364 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002365 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2366 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002367 do_exit++;
2368 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2369 }
2370
2371 /* if all else fails, thell them to be root */
2372 if (do_exit)
2373 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002374 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002375
2376 if (do_exit)
2377 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002378}
2379
Len Brownd7899442015-01-23 00:12:33 -05002380/*
2381 * NHM adds support for additional MSRs:
2382 *
2383 * MSR_SMI_COUNT 0x00000034
2384 *
Len Brownec0adc52015-11-12 02:42:31 -05002385 * MSR_PLATFORM_INFO 0x000000ce
Len Brownd7899442015-01-23 00:12:33 -05002386 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
2387 *
Len Browncf4cbe52017-01-01 13:08:33 -05002388 * MSR_MISC_PWR_MGMT 0x000001aa
2389 *
Len Brownd7899442015-01-23 00:12:33 -05002390 * MSR_PKG_C3_RESIDENCY 0x000003f8
2391 * MSR_PKG_C6_RESIDENCY 0x000003f9
2392 * MSR_CORE_C3_RESIDENCY 0x000003fc
2393 * MSR_CORE_C6_RESIDENCY 0x000003fd
2394 *
Len Brownee7e38e2015-02-09 23:39:45 -05002395 * Side effect:
2396 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05002397 */
Len Brownee7e38e2015-02-09 23:39:45 -05002398int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002399{
Len Brownee7e38e2015-02-09 23:39:45 -05002400 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002401 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002402 int *pkg_cstate_limits;
2403
Len Brown103a8fe2010-10-22 23:53:03 -04002404 if (!genuine_intel)
2405 return 0;
2406
2407 if (family != 6)
2408 return 0;
2409
Len Brown21ed5572015-10-19 22:37:40 -04002410 bclk = discover_bclk(family, model);
2411
Len Brown103a8fe2010-10-22 23:53:03 -04002412 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002413 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2414 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002415 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002416 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2417 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2418 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2419 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002420 pkg_cstate_limits = nhm_pkg_cstate_limits;
2421 break;
Len Brown869ce692016-06-16 23:22:37 -04002422 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2423 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2424 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2425 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002426 pkg_cstate_limits = snb_pkg_cstate_limits;
2427 break;
Len Brown869ce692016-06-16 23:22:37 -04002428 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2429 case INTEL_FAM6_HASWELL_X: /* HSX */
2430 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2431 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2432 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2433 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2434 case INTEL_FAM6_BROADWELL_X: /* BDX */
2435 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2436 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2437 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2438 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2439 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002440 pkg_cstate_limits = hsw_pkg_cstate_limits;
2441 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002442 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2443 pkg_cstate_limits = skx_pkg_cstate_limits;
2444 break;
Len Brown869ce692016-06-16 23:22:37 -04002445 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002446 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002447 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002448 pkg_cstate_limits = slv_pkg_cstate_limits;
2449 break;
Len Brown869ce692016-06-16 23:22:37 -04002450 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002451 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002452 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002453 break;
Len Brown869ce692016-06-16 23:22:37 -04002454 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002455 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002456 pkg_cstate_limits = phi_pkg_cstate_limits;
2457 break;
Len Brown869ce692016-06-16 23:22:37 -04002458 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
2459 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002460 pkg_cstate_limits = bxt_pkg_cstate_limits;
2461 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002462 default:
2463 return 0;
2464 }
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002465 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002466 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002467
Len Brownec0adc52015-11-12 02:42:31 -05002468 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002469 base_ratio = (msr >> 8) & 0xFF;
2470
2471 base_hz = base_ratio * bclk * 1000000;
2472 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002473 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002474}
Len Brownd7899442015-01-23 00:12:33 -05002475int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2476{
Len Brownd7899442015-01-23 00:12:33 -05002477 switch (model) {
2478 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002479 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2480 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2481 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002482 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002483 return 0;
2484 default:
2485 return 1;
2486 }
2487}
Len Brown6574a5d2012-09-21 00:01:31 -04002488int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2489{
2490 if (!genuine_intel)
2491 return 0;
2492
2493 if (family != 6)
2494 return 0;
2495
2496 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002497 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2498 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002499 return 1;
2500 default:
2501 return 0;
2502 }
2503}
Len Brownfcd17212015-03-23 20:29:09 -04002504int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2505{
2506 if (!genuine_intel)
2507 return 0;
2508
2509 if (family != 6)
2510 return 0;
2511
2512 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002513 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002514 return 1;
2515 default:
2516 return 0;
2517 }
2518}
2519
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002520int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2521{
2522 if (!genuine_intel)
2523 return 0;
2524
2525 if (family != 6)
2526 return 0;
2527
2528 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002529 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002530 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002531 return 1;
2532 default:
2533 return 0;
2534 }
2535}
Len Brown6fb31432015-06-17 16:23:45 -04002536int has_config_tdp(unsigned int family, unsigned int model)
2537{
2538 if (!genuine_intel)
2539 return 0;
2540
2541 if (family != 6)
2542 return 0;
2543
2544 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002545 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2546 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2547 case INTEL_FAM6_HASWELL_X: /* HSX */
2548 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2549 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2550 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2551 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2552 case INTEL_FAM6_BROADWELL_X: /* BDX */
2553 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2554 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2555 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2556 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2557 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2558 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002559
Len Brown869ce692016-06-16 23:22:37 -04002560 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002561 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002562 return 1;
2563 default:
2564 return 0;
2565 }
2566}
2567
Len Brownfcd17212015-03-23 20:29:09 -04002568static void
Colin Ian King1b693172016-03-02 13:50:25 +00002569dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002570{
2571 if (!do_nhm_platform_info)
2572 return;
2573
2574 dump_nhm_platform_info();
2575
2576 if (has_hsw_turbo_ratio_limit(family, model))
2577 dump_hsw_turbo_ratio_limits();
2578
2579 if (has_ivt_turbo_ratio_limit(family, model))
2580 dump_ivt_turbo_ratio_limits();
2581
2582 if (has_nhm_turbo_ratio_limit(family, model))
2583 dump_nhm_turbo_ratio_limits();
2584
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002585 if (has_knl_turbo_ratio_limit(family, model))
2586 dump_knl_turbo_ratio_limits();
2587
Len Brown6fb31432015-06-17 16:23:45 -04002588 if (has_config_tdp(family, model))
2589 dump_config_tdp();
2590
Len Brownfcd17212015-03-23 20:29:09 -04002591 dump_nhm_cst_cfg();
2592}
2593
Len Brown6574a5d2012-09-21 00:01:31 -04002594
Len Brown889facb2012-11-08 00:48:57 -05002595/*
2596 * print_epb()
2597 * Decode the ENERGY_PERF_BIAS MSR
2598 */
2599int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2600{
2601 unsigned long long msr;
2602 char *epb_string;
2603 int cpu;
2604
2605 if (!has_epb)
2606 return 0;
2607
2608 cpu = t->cpu_id;
2609
2610 /* EPB is per-package */
2611 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2612 return 0;
2613
2614 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002615 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002616 return -1;
2617 }
2618
2619 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2620 return 0;
2621
Len Browne9be7dd2015-05-26 12:19:37 -04002622 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002623 case ENERGY_PERF_BIAS_PERFORMANCE:
2624 epb_string = "performance";
2625 break;
2626 case ENERGY_PERF_BIAS_NORMAL:
2627 epb_string = "balanced";
2628 break;
2629 case ENERGY_PERF_BIAS_POWERSAVE:
2630 epb_string = "powersave";
2631 break;
2632 default:
2633 epb_string = "custom";
2634 break;
2635 }
Len Brownb7d8c142016-02-13 23:36:17 -05002636 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002637
2638 return 0;
2639}
Len Brown7f5c2582015-12-01 01:36:39 -05002640/*
2641 * print_hwp()
2642 * Decode the MSR_HWP_CAPABILITIES
2643 */
2644int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2645{
2646 unsigned long long msr;
2647 int cpu;
2648
2649 if (!has_hwp)
2650 return 0;
2651
2652 cpu = t->cpu_id;
2653
2654 /* MSR_HWP_CAPABILITIES is per-package */
2655 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2656 return 0;
2657
2658 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002659 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002660 return -1;
2661 }
2662
2663 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2664 return 0;
2665
Len Brownb7d8c142016-02-13 23:36:17 -05002666 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002667 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2668
2669 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2670 if ((msr & (1 << 0)) == 0)
2671 return 0;
2672
2673 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2674 return 0;
2675
Len Brownb7d8c142016-02-13 23:36:17 -05002676 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002677 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2678 cpu, msr,
2679 (unsigned int)HWP_HIGHEST_PERF(msr),
2680 (unsigned int)HWP_GUARANTEED_PERF(msr),
2681 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2682 (unsigned int)HWP_LOWEST_PERF(msr));
2683
2684 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2685 return 0;
2686
Len Brownb7d8c142016-02-13 23:36:17 -05002687 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002688 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2689 cpu, msr,
2690 (unsigned int)(((msr) >> 0) & 0xff),
2691 (unsigned int)(((msr) >> 8) & 0xff),
2692 (unsigned int)(((msr) >> 16) & 0xff),
2693 (unsigned int)(((msr) >> 24) & 0xff),
2694 (unsigned int)(((msr) >> 32) & 0xff3),
2695 (unsigned int)(((msr) >> 42) & 0x1));
2696
2697 if (has_hwp_pkg) {
2698 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2699 return 0;
2700
Len Brownb7d8c142016-02-13 23:36:17 -05002701 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002702 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2703 cpu, msr,
2704 (unsigned int)(((msr) >> 0) & 0xff),
2705 (unsigned int)(((msr) >> 8) & 0xff),
2706 (unsigned int)(((msr) >> 16) & 0xff),
2707 (unsigned int)(((msr) >> 24) & 0xff),
2708 (unsigned int)(((msr) >> 32) & 0xff3));
2709 }
2710 if (has_hwp_notify) {
2711 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2712 return 0;
2713
Len Brownb7d8c142016-02-13 23:36:17 -05002714 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002715 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2716 cpu, msr,
2717 ((msr) & 0x1) ? "EN" : "Dis",
2718 ((msr) & 0x2) ? "EN" : "Dis");
2719 }
2720 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2721 return 0;
2722
Len Brownb7d8c142016-02-13 23:36:17 -05002723 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002724 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2725 cpu, msr,
2726 ((msr) & 0x1) ? "" : "No-",
2727 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05002728
2729 return 0;
2730}
2731
Len Brown3a9a9412014-08-15 02:39:52 -04002732/*
2733 * print_perf_limit()
2734 */
2735int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2736{
2737 unsigned long long msr;
2738 int cpu;
2739
2740 cpu = t->cpu_id;
2741
2742 /* per-package */
2743 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2744 return 0;
2745
2746 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002747 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002748 return -1;
2749 }
2750
2751 if (do_core_perf_limit_reasons) {
2752 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002753 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2754 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002755 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002756 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002757 (msr & 1 << 13) ? "Transitions, " : "",
2758 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2759 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2760 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2761 (msr & 1 << 9) ? "CorePwr, " : "",
2762 (msr & 1 << 8) ? "Amps, " : "",
2763 (msr & 1 << 6) ? "VR-Therm, " : "",
2764 (msr & 1 << 5) ? "Auto-HWP, " : "",
2765 (msr & 1 << 4) ? "Graphics, " : "",
2766 (msr & 1 << 2) ? "bit2, " : "",
2767 (msr & 1 << 1) ? "ThermStatus, " : "",
2768 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002769 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 -04002770 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002771 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002772 (msr & 1 << 29) ? "Transitions, " : "",
2773 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2774 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2775 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2776 (msr & 1 << 25) ? "CorePwr, " : "",
2777 (msr & 1 << 24) ? "Amps, " : "",
2778 (msr & 1 << 22) ? "VR-Therm, " : "",
2779 (msr & 1 << 21) ? "Auto-HWP, " : "",
2780 (msr & 1 << 20) ? "Graphics, " : "",
2781 (msr & 1 << 18) ? "bit18, " : "",
2782 (msr & 1 << 17) ? "ThermStatus, " : "",
2783 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002784
2785 }
2786 if (do_gfx_perf_limit_reasons) {
2787 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002788 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2789 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002790 (msr & 1 << 0) ? "PROCHOT, " : "",
2791 (msr & 1 << 1) ? "ThermStatus, " : "",
2792 (msr & 1 << 4) ? "Graphics, " : "",
2793 (msr & 1 << 6) ? "VR-Therm, " : "",
2794 (msr & 1 << 8) ? "Amps, " : "",
2795 (msr & 1 << 9) ? "GFXPwr, " : "",
2796 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2797 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002798 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002799 (msr & 1 << 16) ? "PROCHOT, " : "",
2800 (msr & 1 << 17) ? "ThermStatus, " : "",
2801 (msr & 1 << 20) ? "Graphics, " : "",
2802 (msr & 1 << 22) ? "VR-Therm, " : "",
2803 (msr & 1 << 24) ? "Amps, " : "",
2804 (msr & 1 << 25) ? "GFXPwr, " : "",
2805 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2806 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2807 }
2808 if (do_ring_perf_limit_reasons) {
2809 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002810 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2811 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002812 (msr & 1 << 0) ? "PROCHOT, " : "",
2813 (msr & 1 << 1) ? "ThermStatus, " : "",
2814 (msr & 1 << 6) ? "VR-Therm, " : "",
2815 (msr & 1 << 8) ? "Amps, " : "",
2816 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2817 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002818 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002819 (msr & 1 << 16) ? "PROCHOT, " : "",
2820 (msr & 1 << 17) ? "ThermStatus, " : "",
2821 (msr & 1 << 22) ? "VR-Therm, " : "",
2822 (msr & 1 << 24) ? "Amps, " : "",
2823 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2824 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2825 }
2826 return 0;
2827}
2828
Len Brown889facb2012-11-08 00:48:57 -05002829#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2830#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2831
Colin Ian King1b693172016-03-02 13:50:25 +00002832double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05002833{
2834 unsigned long long msr;
2835
2836 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002837 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002838 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2839
2840 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002841 case INTEL_FAM6_ATOM_SILVERMONT1:
2842 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05002843 return 30.0;
2844 default:
2845 return 135.0;
2846 }
2847}
2848
Andrey Semin40ee8e32014-12-05 00:07:00 -05002849/*
2850 * rapl_dram_energy_units_probe()
2851 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2852 */
2853static double
2854rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2855{
2856 /* only called for genuine_intel, family 6 */
2857
2858 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002859 case INTEL_FAM6_HASWELL_X: /* HSX */
2860 case INTEL_FAM6_BROADWELL_X: /* BDX */
2861 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2862 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05002863 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05002864 return (rapl_dram_energy_units = 15.3 / 1000000);
2865 default:
2866 return (rapl_energy_units);
2867 }
2868}
2869
Len Brown144b44b2013-11-09 00:30:16 -05002870
Len Brown889facb2012-11-08 00:48:57 -05002871/*
2872 * rapl_probe()
2873 *
Len Brown144b44b2013-11-09 00:30:16 -05002874 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002875 */
2876void rapl_probe(unsigned int family, unsigned int model)
2877{
2878 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002879 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002880 double tdp;
2881
2882 if (!genuine_intel)
2883 return;
2884
2885 if (family != 6)
2886 return;
2887
2888 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002889 case INTEL_FAM6_SANDYBRIDGE:
2890 case INTEL_FAM6_IVYBRIDGE:
2891 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2892 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2893 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2894 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2895 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05002896 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05002897 if (rapl_joules) {
2898 BIC_PRESENT(BIC_Pkg_J);
2899 BIC_PRESENT(BIC_Cor_J);
2900 BIC_PRESENT(BIC_GFX_J);
2901 } else {
2902 BIC_PRESENT(BIC_PkgWatt);
2903 BIC_PRESENT(BIC_CorWatt);
2904 BIC_PRESENT(BIC_GFXWatt);
2905 }
Len Brown889facb2012-11-08 00:48:57 -05002906 break;
Len Brown869ce692016-06-16 23:22:37 -04002907 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne4085d52016-04-06 17:15:56 -04002908 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05002909 if (rapl_joules)
2910 BIC_PRESENT(BIC_Pkg_J);
2911 else
2912 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04002913 break;
Len Brown869ce692016-06-16 23:22:37 -04002914 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2915 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2916 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2917 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04002918 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 -05002919 BIC_PRESENT(BIC_PKG__);
2920 BIC_PRESENT(BIC_RAM__);
2921 if (rapl_joules) {
2922 BIC_PRESENT(BIC_Pkg_J);
2923 BIC_PRESENT(BIC_Cor_J);
2924 BIC_PRESENT(BIC_RAM_J);
2925 } else {
2926 BIC_PRESENT(BIC_PkgWatt);
2927 BIC_PRESENT(BIC_CorWatt);
2928 BIC_PRESENT(BIC_RAMWatt);
2929 }
Len Brown0b2bb692015-03-26 00:50:30 -04002930 break;
Len Brown869ce692016-06-16 23:22:37 -04002931 case INTEL_FAM6_HASWELL_X: /* HSX */
2932 case INTEL_FAM6_BROADWELL_X: /* BDX */
2933 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2934 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2935 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05002936 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04002937 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 -05002938 BIC_PRESENT(BIC_PKG__);
2939 BIC_PRESENT(BIC_RAM__);
2940 if (rapl_joules) {
2941 BIC_PRESENT(BIC_Pkg_J);
2942 BIC_PRESENT(BIC_RAM_J);
2943 } else {
2944 BIC_PRESENT(BIC_PkgWatt);
2945 BIC_PRESENT(BIC_RAMWatt);
2946 }
Len Browne6f9bb32013-12-03 02:19:19 -05002947 break;
Len Brown869ce692016-06-16 23:22:37 -04002948 case INTEL_FAM6_SANDYBRIDGE_X:
2949 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04002950 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 -05002951 BIC_PRESENT(BIC_PKG__);
2952 BIC_PRESENT(BIC_RAM__);
2953 if (rapl_joules) {
2954 BIC_PRESENT(BIC_Pkg_J);
2955 BIC_PRESENT(BIC_Cor_J);
2956 BIC_PRESENT(BIC_RAM_J);
2957 } else {
2958 BIC_PRESENT(BIC_PkgWatt);
2959 BIC_PRESENT(BIC_CorWatt);
2960 BIC_PRESENT(BIC_RAMWatt);
2961 }
Len Brown144b44b2013-11-09 00:30:16 -05002962 break;
Len Brown869ce692016-06-16 23:22:37 -04002963 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
2964 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07002965 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05002966 if (rapl_joules) {
2967 BIC_PRESENT(BIC_Pkg_J);
2968 BIC_PRESENT(BIC_Cor_J);
2969 } else {
2970 BIC_PRESENT(BIC_PkgWatt);
2971 BIC_PRESENT(BIC_CorWatt);
2972 }
Len Brown889facb2012-11-08 00:48:57 -05002973 break;
Len Brown869ce692016-06-16 23:22:37 -04002974 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07002975 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 -05002976 BIC_PRESENT(BIC_PKG__);
2977 BIC_PRESENT(BIC_RAM__);
2978 if (rapl_joules) {
2979 BIC_PRESENT(BIC_Pkg_J);
2980 BIC_PRESENT(BIC_Cor_J);
2981 BIC_PRESENT(BIC_RAM_J);
2982 } else {
2983 BIC_PRESENT(BIC_PkgWatt);
2984 BIC_PRESENT(BIC_CorWatt);
2985 BIC_PRESENT(BIC_RAMWatt);
2986 }
Jacob Pan0f644902016-06-16 09:48:22 -07002987 break;
Len Brown889facb2012-11-08 00:48:57 -05002988 default:
2989 return;
2990 }
2991
2992 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002993 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002994 return;
2995
2996 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04002997 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05002998 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2999 else
3000 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003001
Andrey Semin40ee8e32014-12-05 00:07:00 -05003002 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3003
Len Brown144b44b2013-11-09 00:30:16 -05003004 time_unit = msr >> 16 & 0xF;
3005 if (time_unit == 0)
3006 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003007
Len Brown144b44b2013-11-09 00:30:16 -05003008 rapl_time_units = 1.0 / (1 << (time_unit));
3009
3010 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003011
3012 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05003013 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003014 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003015
3016 return;
3017}
3018
Colin Ian King1b693172016-03-02 13:50:25 +00003019void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003020{
3021 if (!genuine_intel)
3022 return;
3023
3024 if (family != 6)
3025 return;
3026
3027 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003028 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3029 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3030 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003031 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003032 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003033 do_core_perf_limit_reasons = 1;
3034 do_ring_perf_limit_reasons = 1;
3035 default:
3036 return;
3037 }
3038}
3039
Len Brown889facb2012-11-08 00:48:57 -05003040int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3041{
3042 unsigned long long msr;
3043 unsigned int dts;
3044 int cpu;
3045
3046 if (!(do_dts || do_ptm))
3047 return 0;
3048
3049 cpu = t->cpu_id;
3050
3051 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003052 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003053 return 0;
3054
3055 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003056 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003057 return -1;
3058 }
3059
3060 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3061 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3062 return 0;
3063
3064 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003065 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003066 cpu, msr, tcc_activation_temp - dts);
3067
3068#ifdef THERM_DEBUG
3069 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3070 return 0;
3071
3072 dts = (msr >> 16) & 0x7F;
3073 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003074 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003075 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3076#endif
3077 }
3078
3079
3080 if (do_dts) {
3081 unsigned int resolution;
3082
3083 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3084 return 0;
3085
3086 dts = (msr >> 16) & 0x7F;
3087 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003088 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003089 cpu, msr, tcc_activation_temp - dts, resolution);
3090
3091#ifdef THERM_DEBUG
3092 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3093 return 0;
3094
3095 dts = (msr >> 16) & 0x7F;
3096 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003097 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003098 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3099#endif
3100 }
3101
3102 return 0;
3103}
Len Brown36229892016-02-26 20:51:02 -05003104
Len Brown889facb2012-11-08 00:48:57 -05003105void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3106{
Len Brownb7d8c142016-02-13 23:36:17 -05003107 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003108 cpu, label,
3109 ((msr >> 15) & 1) ? "EN" : "DIS",
3110 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3111 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3112 (((msr >> 16) & 1) ? "EN" : "DIS"));
3113
3114 return;
3115}
3116
3117int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3118{
3119 unsigned long long msr;
3120 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003121
3122 if (!do_rapl)
3123 return 0;
3124
3125 /* RAPL counters are per package, so print only for 1st thread/package */
3126 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3127 return 0;
3128
3129 cpu = t->cpu_id;
3130 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003131 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003132 return -1;
3133 }
3134
3135 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3136 return -1;
3137
Len Brownd8af6f52015-02-10 01:56:38 -05003138 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05003139 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
Len Brown889facb2012-11-08 00:48:57 -05003140 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05003141 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05003142 }
Len Brown144b44b2013-11-09 00:30:16 -05003143 if (do_rapl & RAPL_PKG_POWER_INFO) {
3144
Len Brown889facb2012-11-08 00:48:57 -05003145 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3146 return -5;
3147
3148
Len Brownb7d8c142016-02-13 23:36:17 -05003149 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 -05003150 cpu, msr,
3151 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3152 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3153 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3154 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3155
Len Brown144b44b2013-11-09 00:30:16 -05003156 }
3157 if (do_rapl & RAPL_PKG) {
3158
Len Brown889facb2012-11-08 00:48:57 -05003159 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3160 return -9;
3161
Len Brownb7d8c142016-02-13 23:36:17 -05003162 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003163 cpu, msr, (msr >> 63) & 1 ? "": "UN");
3164
3165 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003166 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003167 cpu,
3168 ((msr >> 47) & 1) ? "EN" : "DIS",
3169 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3170 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3171 ((msr >> 48) & 1) ? "EN" : "DIS");
3172 }
3173
Len Brown0b2bb692015-03-26 00:50:30 -04003174 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003175 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3176 return -6;
3177
Len Brownb7d8c142016-02-13 23:36:17 -05003178 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 -05003179 cpu, msr,
3180 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3181 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3182 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3183 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003184 }
3185 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003186 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3187 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003188 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003189 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3190
3191 print_power_limit_msr(cpu, msr, "DRAM Limit");
3192 }
Len Brown144b44b2013-11-09 00:30:16 -05003193 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05003194 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003195 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3196 return -7;
3197
Len Brownb7d8c142016-02-13 23:36:17 -05003198 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003199 }
3200 }
Jacob Pan91484942016-06-16 09:48:20 -07003201 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brownd8af6f52015-02-10 01:56:38 -05003202 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003203 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3204 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003205 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003206 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3207 print_power_limit_msr(cpu, msr, "Cores Limit");
3208 }
3209 }
3210 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05003211 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003212 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3213 return -8;
3214
Len Brownb7d8c142016-02-13 23:36:17 -05003215 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003216
3217 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3218 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003219 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003220 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3221 print_power_limit_msr(cpu, msr, "GFX Limit");
3222 }
3223 }
3224 return 0;
3225}
3226
Len Brownd7899442015-01-23 00:12:33 -05003227/*
3228 * SNB adds support for additional MSRs:
3229 *
3230 * MSR_PKG_C7_RESIDENCY 0x000003fa
3231 * MSR_CORE_C7_RESIDENCY 0x000003fe
3232 * MSR_PKG_C2_RESIDENCY 0x0000060d
3233 */
Len Brown103a8fe2010-10-22 23:53:03 -04003234
Len Brownd7899442015-01-23 00:12:33 -05003235int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003236{
3237 if (!genuine_intel)
3238 return 0;
3239
3240 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003241 case INTEL_FAM6_SANDYBRIDGE:
3242 case INTEL_FAM6_SANDYBRIDGE_X:
3243 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3244 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3245 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3246 case INTEL_FAM6_HASWELL_X: /* HSW */
3247 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3248 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3249 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3250 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3251 case INTEL_FAM6_BROADWELL_X: /* BDX */
3252 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3253 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3254 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3255 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3256 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3257 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3258 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003259 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003260 return 1;
3261 }
3262 return 0;
3263}
3264
Len Brownd7899442015-01-23 00:12:33 -05003265/*
3266 * HSW adds support for additional MSRs:
3267 *
Len Brown5a634262016-04-06 17:15:55 -04003268 * MSR_PKG_C8_RESIDENCY 0x00000630
3269 * MSR_PKG_C9_RESIDENCY 0x00000631
3270 * MSR_PKG_C10_RESIDENCY 0x00000632
3271 *
3272 * MSR_PKGC8_IRTL 0x00000633
3273 * MSR_PKGC9_IRTL 0x00000634
3274 * MSR_PKGC10_IRTL 0x00000635
3275 *
Len Brownd7899442015-01-23 00:12:33 -05003276 */
3277int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003278{
3279 if (!genuine_intel)
3280 return 0;
3281
3282 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003283 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3284 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3285 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3286 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3287 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3288 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3289 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003290 return 1;
3291 }
3292 return 0;
3293}
3294
Len Brown0b2bb692015-03-26 00:50:30 -04003295/*
3296 * SKL adds support for additional MSRS:
3297 *
3298 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3299 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3300 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3301 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3302 */
3303int has_skl_msrs(unsigned int family, unsigned int model)
3304{
3305 if (!genuine_intel)
3306 return 0;
3307
3308 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003309 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3310 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3311 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3312 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003313 return 1;
3314 }
3315 return 0;
3316}
3317
3318
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003319
Len Brown144b44b2013-11-09 00:30:16 -05003320int is_slm(unsigned int family, unsigned int model)
3321{
3322 if (!genuine_intel)
3323 return 0;
3324 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003325 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3326 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003327 return 1;
3328 }
3329 return 0;
3330}
3331
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003332int is_knl(unsigned int family, unsigned int model)
3333{
3334 if (!genuine_intel)
3335 return 0;
3336 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003337 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003338 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003339 return 1;
3340 }
3341 return 0;
3342}
3343
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003344unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3345{
3346 if (is_knl(family, model))
3347 return 1024;
3348 return 1;
3349}
3350
Len Brown144b44b2013-11-09 00:30:16 -05003351#define SLM_BCLK_FREQS 5
3352double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3353
3354double slm_bclk(void)
3355{
3356 unsigned long long msr = 3;
3357 unsigned int i;
3358 double freq;
3359
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003360 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003361 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003362
3363 i = msr & 0xf;
3364 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003365 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003366 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003367 }
3368 freq = slm_freq_table[i];
3369
Len Brownb7d8c142016-02-13 23:36:17 -05003370 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003371
3372 return freq;
3373}
3374
Len Brown103a8fe2010-10-22 23:53:03 -04003375double discover_bclk(unsigned int family, unsigned int model)
3376{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003377 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003378 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003379 else if (is_slm(family, model))
3380 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003381 else
3382 return 133.33;
3383}
3384
Len Brown889facb2012-11-08 00:48:57 -05003385/*
3386 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3387 * the Thermal Control Circuit (TCC) activates.
3388 * This is usually equal to tjMax.
3389 *
3390 * Older processors do not have this MSR, so there we guess,
3391 * but also allow cmdline over-ride with -T.
3392 *
3393 * Several MSR temperature values are in units of degrees-C
3394 * below this value, including the Digital Thermal Sensor (DTS),
3395 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3396 */
3397int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3398{
3399 unsigned long long msr;
3400 unsigned int target_c_local;
3401 int cpu;
3402
3403 /* tcc_activation_temp is used only for dts or ptm */
3404 if (!(do_dts || do_ptm))
3405 return 0;
3406
3407 /* this is a per-package concept */
3408 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3409 return 0;
3410
3411 cpu = t->cpu_id;
3412 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003413 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003414 return -1;
3415 }
3416
3417 if (tcc_activation_temp_override != 0) {
3418 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003419 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003420 cpu, tcc_activation_temp);
3421 return 0;
3422 }
3423
3424 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003425 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003426 goto guess;
3427
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003428 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003429 goto guess;
3430
Jean Delvare34821242014-05-01 11:40:19 +02003431 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003432
Len Brownd8af6f52015-02-10 01:56:38 -05003433 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003434 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003435 cpu, msr, target_c_local);
3436
Jean Delvare34821242014-05-01 11:40:19 +02003437 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003438 goto guess;
3439
3440 tcc_activation_temp = target_c_local;
3441
3442 return 0;
3443
3444guess:
3445 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003446 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003447 cpu, tcc_activation_temp);
3448
3449 return 0;
3450}
Len Brown69807a62015-11-21 12:22:47 -05003451
Len Brownaa8d8cc2016-03-11 13:26:03 -05003452void decode_feature_control_msr(void)
3453{
3454 unsigned long long msr;
3455
3456 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3457 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3458 base_cpu, msr,
3459 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3460 msr & (1 << 18) ? "SGX" : "");
3461}
3462
Len Brown69807a62015-11-21 12:22:47 -05003463void decode_misc_enable_msr(void)
3464{
3465 unsigned long long msr;
3466
3467 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003468 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
Len Brown69807a62015-11-21 12:22:47 -05003469 base_cpu, msr,
3470 msr & (1 << 3) ? "TCC" : "",
3471 msr & (1 << 16) ? "EIST" : "",
3472 msr & (1 << 18) ? "MONITOR" : "");
3473}
3474
Len Brownf0057312015-12-03 01:35:36 -05003475/*
3476 * Decode MSR_MISC_PWR_MGMT
3477 *
3478 * Decode the bits according to the Nehalem documentation
3479 * bit[0] seems to continue to have same meaning going forward
3480 * bit[1] less so...
3481 */
3482void decode_misc_pwr_mgmt_msr(void)
3483{
3484 unsigned long long msr;
3485
3486 if (!do_nhm_platform_info)
3487 return;
3488
Len Browncf4cbe52017-01-01 13:08:33 -05003489 if (no_MSR_MISC_PWR_MGMT)
3490 return;
3491
Len Brownf0057312015-12-03 01:35:36 -05003492 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003493 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 -05003494 base_cpu, msr,
3495 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003496 msr & (1 << 1) ? "EN" : "DIS",
3497 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003498}
Len Brown7f5c2582015-12-01 01:36:39 -05003499
Len Brownfcd17212015-03-23 20:29:09 -04003500void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003501{
Len Brown61a87ba2015-11-23 02:30:51 -05003502 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003503 unsigned int fms, family, model, stepping;
3504
3505 eax = ebx = ecx = edx = 0;
3506
Len Brown5aea2f72016-03-13 03:14:35 -04003507 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003508
3509 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3510 genuine_intel = 1;
3511
Len Brownd8af6f52015-02-10 01:56:38 -05003512 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003513 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003514 (char *)&ebx, (char *)&edx, (char *)&ecx);
3515
Len Brown5aea2f72016-03-13 03:14:35 -04003516 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003517 family = (fms >> 8) & 0xf;
3518 model = (fms >> 4) & 0xf;
3519 stepping = fms & 0xf;
3520 if (family == 6 || family == 0xf)
3521 model += ((fms >> 16) & 0xf) << 4;
3522
Len Brown69807a62015-11-21 12:22:47 -05003523 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05003524 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003525 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003526 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003527 ecx & (1 << 0) ? "SSE3" : "-",
3528 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003529 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003530 ecx & (1 << 7) ? "EIST" : "-",
3531 ecx & (1 << 8) ? "TM2" : "-",
3532 edx & (1 << 4) ? "TSC" : "-",
3533 edx & (1 << 5) ? "MSR" : "-",
3534 edx & (1 << 22) ? "ACPI-TM" : "-",
3535 edx & (1 << 29) ? "TM" : "-");
3536 }
Len Brown103a8fe2010-10-22 23:53:03 -04003537
Josh Triplettb2c95d92013-08-20 17:20:18 -07003538 if (!(edx & (1 << 5)))
3539 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003540
3541 /*
3542 * check max extended function levels of CPUID.
3543 * This is needed to check for invariant TSC.
3544 * This check is valid for both Intel and AMD.
3545 */
3546 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003547 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003548
Len Brown61a87ba2015-11-23 02:30:51 -05003549 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003550
Len Brownd7899442015-01-23 00:12:33 -05003551 /*
3552 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3553 * this check is valid for both Intel and AMD
3554 */
Len Brown5aea2f72016-03-13 03:14:35 -04003555 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003556 has_invariant_tsc = edx & (1 << 8);
3557 }
Len Brown103a8fe2010-10-22 23:53:03 -04003558
3559 /*
3560 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3561 * this check is valid for both Intel and AMD
3562 */
3563
Len Brown5aea2f72016-03-13 03:14:35 -04003564 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003565 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003566 if (has_aperf) {
3567 BIC_PRESENT(BIC_Avg_MHz);
3568 BIC_PRESENT(BIC_Busy);
3569 BIC_PRESENT(BIC_Bzy_MHz);
3570 }
Len Brown889facb2012-11-08 00:48:57 -05003571 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003572 if (do_dts)
3573 BIC_PRESENT(BIC_CoreTmp);
Len Brown889facb2012-11-08 00:48:57 -05003574 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003575 if (do_ptm)
3576 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003577 has_hwp = eax & (1 << 7);
3578 has_hwp_notify = eax & (1 << 8);
3579 has_hwp_activity_window = eax & (1 << 9);
3580 has_hwp_epp = eax & (1 << 10);
3581 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003582 has_epb = ecx & (1 << 3);
3583
Len Brownd8af6f52015-02-10 01:56:38 -05003584 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003585 fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003586 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3587 has_aperf ? "" : "No-",
3588 do_dts ? "" : "No-",
3589 do_ptm ? "" : "No-",
3590 has_hwp ? "" : "No-",
3591 has_hwp_notify ? "" : "No-",
3592 has_hwp_activity_window ? "" : "No-",
3593 has_hwp_epp ? "" : "No-",
3594 has_hwp_pkg ? "" : "No-",
3595 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003596
Len Brown69807a62015-11-21 12:22:47 -05003597 if (debug)
3598 decode_misc_enable_msr();
3599
Len Brown8ae72252016-04-06 17:15:54 -04003600 if (max_level >= 0x7 && debug) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003601 int has_sgx;
3602
3603 ecx = 0;
3604
3605 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3606
3607 has_sgx = ebx & (1 << 2);
3608 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3609
3610 if (has_sgx)
3611 decode_feature_control_msr();
3612 }
3613
Len Brown61a87ba2015-11-23 02:30:51 -05003614 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003615 unsigned int eax_crystal;
3616 unsigned int ebx_tsc;
3617
3618 /*
3619 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3620 */
3621 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003622 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003623
3624 if (ebx_tsc != 0) {
3625
3626 if (debug && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003627 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003628 eax_crystal, ebx_tsc, crystal_hz);
3629
3630 if (crystal_hz == 0)
3631 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04003632 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3633 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3634 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3635 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003636 crystal_hz = 24000000; /* 24.0 MHz */
3637 break;
Len Brown869ce692016-06-16 23:22:37 -04003638 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003639 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003640 crystal_hz = 25000000; /* 25.0 MHz */
3641 break;
Len Brown869ce692016-06-16 23:22:37 -04003642 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne8efbc82016-04-06 17:15:57 -04003643 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003644 break;
3645 default:
3646 crystal_hz = 0;
3647 }
3648
3649 if (crystal_hz) {
3650 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
3651 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003652 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003653 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3654 }
3655 }
3656 }
Len Brown61a87ba2015-11-23 02:30:51 -05003657 if (max_level >= 0x16) {
3658 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3659
3660 /*
3661 * CPUID 16H Base MHz, Max MHz, Bus MHz
3662 */
3663 base_mhz = max_mhz = bus_mhz = edx = 0;
3664
Len Brown5aea2f72016-03-13 03:14:35 -04003665 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown61a87ba2015-11-23 02:30:51 -05003666 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003667 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003668 base_mhz, max_mhz, bus_mhz);
3669 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003670
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003671 if (has_aperf)
3672 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3673
Len Brown812db3f2017-02-10 00:25:41 -05003674 BIC_PRESENT(BIC_IRQ);
3675 BIC_PRESENT(BIC_TSC_MHz);
3676
3677 if (probe_nhm_msrs(family, model)) {
3678 do_nhm_platform_info = 1;
3679 BIC_PRESENT(BIC_CPU_c1);
3680 BIC_PRESENT(BIC_CPU_c3);
3681 BIC_PRESENT(BIC_CPU_c6);
3682 BIC_PRESENT(BIC_SMI);
3683 }
Len Brownd7899442015-01-23 00:12:33 -05003684 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05003685
3686 if (do_snb_cstates)
3687 BIC_PRESENT(BIC_CPU_c7);
3688
Len Brown5a634262016-04-06 17:15:55 -04003689 do_irtl_snb = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05003690 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3691 do_pc3 = (pkg_cstate_limit >= PCL__3);
3692 do_pc6 = (pkg_cstate_limit >= PCL__6);
3693 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05003694 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown5a634262016-04-06 17:15:55 -04003695 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04003696 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05003697 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003698 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04003699
Len Brownf0057312015-12-03 01:35:36 -05003700 if (debug)
3701 decode_misc_pwr_mgmt_msr();
3702
Len Brown889facb2012-11-08 00:48:57 -05003703 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04003704 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05003705
Len Brownfcd17212015-03-23 20:29:09 -04003706 if (debug)
Colin Ian King1b693172016-03-02 13:50:25 +00003707 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003708
Len Browna2b7b742015-09-26 00:12:38 -04003709 if (has_skl_msrs(family, model))
3710 calculate_tsc_tweak();
3711
Len Brown812db3f2017-02-10 00:25:41 -05003712 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
3713 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05003714
Len Brown812db3f2017-02-10 00:25:41 -05003715 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
3716 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05003717
Len Brown889facb2012-11-08 00:48:57 -05003718 return;
Len Brown103a8fe2010-10-22 23:53:03 -04003719}
3720
Len Brownd8af6f52015-02-10 01:56:38 -05003721void help()
Len Brown103a8fe2010-10-22 23:53:03 -04003722{
Len Brownb7d8c142016-02-13 23:36:17 -05003723 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05003724 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3725 "\n"
3726 "Turbostat forks the specified COMMAND and prints statistics\n"
3727 "when COMMAND completes.\n"
3728 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3729 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05003730 "--add add a counter\n"
3731 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003732 "--debug run in \"debug\" mode\n"
3733 "--interval sec Override default 5-second measurement interval\n"
3734 "--help print this help message\n"
Len Brownb7d8c142016-02-13 23:36:17 -05003735 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003736 "--version print version information\n"
3737 "\n"
3738 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04003739}
3740
3741
3742/*
3743 * in /dev/cpu/ return success for names that are numbers
3744 * ie. filter out ".", "..", "microcode".
3745 */
3746int dir_filter(const struct dirent *dirp)
3747{
3748 if (isdigit(dirp->d_name[0]))
3749 return 1;
3750 else
3751 return 0;
3752}
3753
3754int open_dev_cpu_msr(int dummy1)
3755{
3756 return 0;
3757}
3758
Len Brownc98d5d92012-06-04 00:56:40 -04003759void topology_probe()
3760{
3761 int i;
3762 int max_core_id = 0;
3763 int max_package_id = 0;
3764 int max_siblings = 0;
3765 struct cpu_topology {
3766 int core_id;
3767 int physical_package_id;
3768 } *cpus;
3769
3770 /* Initialize num_cpus, max_cpu_num */
3771 topo.num_cpus = 0;
3772 topo.max_cpu_num = 0;
3773 for_all_proc_cpus(count_cpus);
3774 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003775 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04003776
Len Brownd8af6f52015-02-10 01:56:38 -05003777 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003778 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04003779
3780 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003781 if (cpus == NULL)
3782 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003783
3784 /*
3785 * Allocate and initialize cpu_present_set
3786 */
3787 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003788 if (cpu_present_set == NULL)
3789 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003790 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3791 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3792 for_all_proc_cpus(mark_cpu_present);
3793
3794 /*
3795 * Allocate and initialize cpu_affinity_set
3796 */
3797 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003798 if (cpu_affinity_set == NULL)
3799 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003800 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3801 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3802
3803
3804 /*
3805 * For online cpus
3806 * find max_core_id, max_package_id
3807 */
3808 for (i = 0; i <= topo.max_cpu_num; ++i) {
3809 int siblings;
3810
3811 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003812 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003813 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04003814 continue;
3815 }
3816 cpus[i].core_id = get_core_id(i);
3817 if (cpus[i].core_id > max_core_id)
3818 max_core_id = cpus[i].core_id;
3819
3820 cpus[i].physical_package_id = get_physical_package_id(i);
3821 if (cpus[i].physical_package_id > max_package_id)
3822 max_package_id = cpus[i].physical_package_id;
3823
3824 siblings = get_num_ht_siblings(i);
3825 if (siblings > max_siblings)
3826 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003827 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003828 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003829 i, cpus[i].physical_package_id, cpus[i].core_id);
3830 }
3831 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003832 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003833 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003834 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003835 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003836 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04003837
3838 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003839 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003840 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003841 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003842 if (debug && !summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003843 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04003844
3845 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003846 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003847 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04003848
3849 free(cpus);
3850}
3851
3852void
3853allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3854{
3855 int i;
3856
3857 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05003858 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04003859 if (*t == NULL)
3860 goto error;
3861
3862 for (i = 0; i < topo.num_threads_per_core *
3863 topo.num_cores_per_pkg * topo.num_packages; i++)
3864 (*t)[i].cpu_id = -1;
3865
3866 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05003867 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04003868 if (*c == NULL)
3869 goto error;
3870
3871 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3872 (*c)[i].core_id = -1;
3873
Len Brown678a3bd2017-02-09 22:22:13 -05003874 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04003875 if (*p == NULL)
3876 goto error;
3877
3878 for (i = 0; i < topo.num_packages; i++)
3879 (*p)[i].package_id = i;
3880
3881 return;
3882error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07003883 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04003884}
3885/*
3886 * init_counter()
3887 *
3888 * set cpu_id, core_num, pkg_num
3889 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3890 *
3891 * increment topo.num_cores when 1st core in pkg seen
3892 */
3893void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3894 struct pkg_data *pkg_base, int thread_num, int core_num,
3895 int pkg_num, int cpu_id)
3896{
3897 struct thread_data *t;
3898 struct core_data *c;
3899 struct pkg_data *p;
3900
3901 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3902 c = GET_CORE(core_base, core_num, pkg_num);
3903 p = GET_PKG(pkg_base, pkg_num);
3904
3905 t->cpu_id = cpu_id;
3906 if (thread_num == 0) {
3907 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3908 if (cpu_is_first_core_in_package(cpu_id))
3909 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3910 }
3911
3912 c->core_id = core_num;
3913 p->package_id = pkg_num;
3914}
3915
3916
3917int initialize_counters(int cpu_id)
3918{
3919 int my_thread_id, my_core_id, my_package_id;
3920
3921 my_package_id = get_physical_package_id(cpu_id);
3922 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07003923 my_thread_id = get_cpu_position_in_core(cpu_id);
3924 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04003925 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04003926
3927 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3928 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3929 return 0;
3930}
3931
3932void allocate_output_buffer()
3933{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003934 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04003935 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07003936 if (outp == NULL)
3937 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04003938}
Len Brown36229892016-02-26 20:51:02 -05003939void allocate_fd_percpu(void)
3940{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003941 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05003942 if (fd_percpu == NULL)
3943 err(-1, "calloc fd_percpu");
3944}
Len Brown562a2d32016-02-26 23:48:05 -05003945void allocate_irq_buffers(void)
3946{
3947 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
3948 if (irq_column_2_cpu == NULL)
3949 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04003950
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003951 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05003952 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003953 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05003954}
Len Brownc98d5d92012-06-04 00:56:40 -04003955void setup_all_buffers(void)
3956{
3957 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05003958 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05003959 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04003960 allocate_counters(&thread_even, &core_even, &package_even);
3961 allocate_counters(&thread_odd, &core_odd, &package_odd);
3962 allocate_output_buffer();
3963 for_all_proc_cpus(initialize_counters);
3964}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003965
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003966void set_base_cpu(void)
3967{
3968 base_cpu = sched_getcpu();
3969 if (base_cpu < 0)
3970 err(-ENODEV, "No valid cpus found");
3971
3972 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003973 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003974}
3975
Len Brown103a8fe2010-10-22 23:53:03 -04003976void turbostat_init()
3977{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003978 setup_all_buffers();
3979 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04003980 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04003981 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04003982 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04003983
Len Brown103a8fe2010-10-22 23:53:03 -04003984
Len Brownd8af6f52015-02-10 01:56:38 -05003985 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05003986 for_all_cpus(print_hwp, ODD_COUNTERS);
3987
3988 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003989 for_all_cpus(print_epb, ODD_COUNTERS);
3990
Len Brownd8af6f52015-02-10 01:56:38 -05003991 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04003992 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3993
Len Brownd8af6f52015-02-10 01:56:38 -05003994 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003995 for_all_cpus(print_rapl, ODD_COUNTERS);
3996
3997 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3998
Len Brownd8af6f52015-02-10 01:56:38 -05003999 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05004000 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004001
4002 if (debug && do_irtl_snb)
4003 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004004}
4005
4006int fork_it(char **argv)
4007{
Len Brown103a8fe2010-10-22 23:53:03 -04004008 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004009 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004010
Len Brownd91bb172012-11-01 00:08:19 -04004011 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4012 if (status)
4013 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004014 /* clear affinity side-effect of get_counters() */
4015 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004016 gettimeofday(&tv_even, (struct timezone *)NULL);
4017
4018 child_pid = fork();
4019 if (!child_pid) {
4020 /* child */
4021 execvp(argv[0], argv);
4022 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004023
4024 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004025 if (child_pid == -1)
4026 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004027
4028 signal(SIGINT, SIG_IGN);
4029 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004030 if (waitpid(child_pid, &status, 0) == -1)
4031 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004032 }
Len Brownc98d5d92012-06-04 00:56:40 -04004033 /*
4034 * n.b. fork_it() does not check for errors from for_all_cpus()
4035 * because re-starting is problematic when forking
4036 */
4037 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004038 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004039 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004040 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4041 fprintf(outf, "%s: Counter reset detected\n", progname);
4042 else {
4043 compute_average(EVEN_COUNTERS);
4044 format_all_counters(EVEN_COUNTERS);
4045 }
Len Brown103a8fe2010-10-22 23:53:03 -04004046
Len Brownb7d8c142016-02-13 23:36:17 -05004047 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4048
4049 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004050
Len Brownd91bb172012-11-01 00:08:19 -04004051 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004052}
4053
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004054int get_and_dump_counters(void)
4055{
4056 int status;
4057
4058 status = for_all_cpus(get_counters, ODD_COUNTERS);
4059 if (status)
4060 return status;
4061
4062 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4063 if (status)
4064 return status;
4065
Len Brownb7d8c142016-02-13 23:36:17 -05004066 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004067
4068 return status;
4069}
4070
Len Brownd8af6f52015-02-10 01:56:38 -05004071void print_version() {
Len Browncf4cbe52017-01-01 13:08:33 -05004072 fprintf(outf, "turbostat version 4.17 1 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004073 " - Len Brown <lenb@kernel.org>\n");
4074}
4075
Len Brown388e9c82016-12-22 23:57:55 -05004076int add_counter(unsigned int msr_num, char *name, unsigned int width,
4077 enum counter_scope scope, enum counter_type type,
4078 enum counter_format format)
4079{
4080 struct msr_counter *msrp;
4081
4082 msrp = calloc(1, sizeof(struct msr_counter));
4083 if (msrp == NULL) {
4084 perror("calloc");
4085 exit(1);
4086 }
4087
4088 msrp->msr_num = msr_num;
4089 strncpy(msrp->name, name, NAME_BYTES);
4090 msrp->width = width;
4091 msrp->type = type;
4092 msrp->format = format;
4093
4094 switch (scope) {
4095
4096 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004097 msrp->next = sys.tp;
4098 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004099 sys.added_thread_counters++;
4100 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4101 fprintf(stderr, "exceeded max %d added thread counters\n",
4102 MAX_ADDED_COUNTERS);
4103 exit(-1);
4104 }
Len Brown388e9c82016-12-22 23:57:55 -05004105 break;
4106
4107 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004108 msrp->next = sys.cp;
4109 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004110 sys.added_core_counters++;
4111 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4112 fprintf(stderr, "exceeded max %d added core counters\n",
4113 MAX_ADDED_COUNTERS);
4114 exit(-1);
4115 }
Len Brown388e9c82016-12-22 23:57:55 -05004116 break;
4117
4118 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004119 msrp->next = sys.pp;
4120 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004121 sys.added_package_counters++;
4122 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4123 fprintf(stderr, "exceeded max %d added package counters\n",
4124 MAX_ADDED_COUNTERS);
4125 exit(-1);
4126 }
Len Brown388e9c82016-12-22 23:57:55 -05004127 break;
4128 }
4129
4130 return 0;
4131}
4132
4133void parse_add_command(char *add_command)
4134{
4135 int msr_num = 0;
4136 char name_buffer[NAME_BYTES];
4137 int width = 64;
4138 int fail = 0;
4139 enum counter_scope scope = SCOPE_CPU;
4140 enum counter_type type = COUNTER_CYCLES;
4141 enum counter_format format = FORMAT_DELTA;
4142
4143 while (add_command) {
4144
4145 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4146 goto next;
4147
4148 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4149 goto next;
4150
4151 if (sscanf(add_command, "u%d", &width) == 1) {
4152 if ((width == 32) || (width == 64))
4153 goto next;
4154 width = 64;
4155 }
4156 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4157 scope = SCOPE_CPU;
4158 goto next;
4159 }
4160 if (!strncmp(add_command, "core", strlen("core"))) {
4161 scope = SCOPE_CORE;
4162 goto next;
4163 }
4164 if (!strncmp(add_command, "package", strlen("package"))) {
4165 scope = SCOPE_PACKAGE;
4166 goto next;
4167 }
4168 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4169 type = COUNTER_CYCLES;
4170 goto next;
4171 }
4172 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4173 type = COUNTER_SECONDS;
4174 goto next;
4175 }
4176 if (!strncmp(add_command, "raw", strlen("raw"))) {
4177 format = FORMAT_RAW;
4178 goto next;
4179 }
4180 if (!strncmp(add_command, "delta", strlen("delta"))) {
4181 format = FORMAT_DELTA;
4182 goto next;
4183 }
4184 if (!strncmp(add_command, "percent", strlen("percent"))) {
4185 format = FORMAT_PERCENT;
4186 goto next;
4187 }
4188
4189 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4190 char *eos;
4191
4192 eos = strchr(name_buffer, ',');
4193 if (eos)
4194 *eos = '\0';
4195 goto next;
4196 }
4197
4198next:
4199 add_command = strchr(add_command, ',');
4200 if (add_command)
4201 add_command++;
4202
4203 }
4204 if (msr_num == 0) {
4205 fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4206 fail++;
4207 }
4208
4209 /* generate default column header */
4210 if (*name_buffer == '\0') {
4211 if (format == FORMAT_RAW) {
4212 if (width == 32)
4213 sprintf(name_buffer, "msr%d", msr_num);
4214 else
4215 sprintf(name_buffer, "MSR%d", msr_num);
4216 } else if (format == FORMAT_DELTA) {
4217 if (width == 32)
4218 sprintf(name_buffer, "cnt%d", msr_num);
4219 else
4220 sprintf(name_buffer, "CNT%d", msr_num);
4221 } else if (format == FORMAT_PERCENT) {
4222 if (width == 32)
4223 sprintf(name_buffer, "msr%d%%", msr_num);
4224 else
4225 sprintf(name_buffer, "MSR%d%%", msr_num);
4226 }
4227 }
4228
4229 if (add_counter(msr_num, name_buffer, width, scope, type, format))
4230 fail++;
4231
4232 if (fail) {
4233 help();
4234 exit(1);
4235 }
4236}
Len Brown812db3f2017-02-10 00:25:41 -05004237/*
4238 * HIDE_LIST - hide this list of counters, show the rest [default]
4239 * SHOW_LIST - show this list of counters, hide the rest
4240 */
4241enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4242
4243int shown;
4244/*
4245 * parse_show_hide() - process cmdline to set default counter action
4246 */
4247void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4248{
4249 /*
4250 * --show: show only those specified
4251 * The 1st invocation will clear and replace the enabled mask
4252 * subsequent invocations can add to it.
4253 */
4254 if (new_mode == SHOW_LIST) {
4255 if (shown == 0)
4256 bic_enabled = bic_lookup(optarg);
4257 else
4258 bic_enabled |= bic_lookup(optarg);
4259 shown = 1;
4260
4261 return;
4262 }
4263
4264 /*
4265 * --hide: do not show those specified
4266 * multiple invocations simply clear more bits in enabled mask
4267 */
4268 bic_enabled &= ~bic_lookup(optarg);
4269}
4270
Len Brown103a8fe2010-10-22 23:53:03 -04004271void cmdline(int argc, char **argv)
4272{
4273 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004274 int option_index = 0;
4275 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004276 {"add", required_argument, 0, 'a'},
Len Brownd8af6f52015-02-10 01:56:38 -05004277 {"Dump", no_argument, 0, 'D'},
4278 {"debug", no_argument, 0, 'd'},
4279 {"interval", required_argument, 0, 'i'},
4280 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004281 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004282 {"Joules", no_argument, 0, 'J'},
Len Brownb7d8c142016-02-13 23:36:17 -05004283 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004284 {"Package", no_argument, 0, 'p'},
4285 {"processor", no_argument, 0, 'p'},
Len Brown812db3f2017-02-10 00:25:41 -05004286 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004287 {"Summary", no_argument, 0, 'S'},
4288 {"TCC", required_argument, 0, 'T'},
4289 {"version", no_argument, 0, 'v' },
4290 {0, 0, 0, 0 }
4291 };
Len Brown103a8fe2010-10-22 23:53:03 -04004292
4293 progname = argv[0];
4294
Len Brownb7d8c142016-02-13 23:36:17 -05004295 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004296 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004297 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004298 case 'a':
4299 parse_add_command(optarg);
4300 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004301 case 'D':
4302 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004303 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004304 case 'd':
4305 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004306 break;
Len Brown812db3f2017-02-10 00:25:41 -05004307 case 'H':
4308 parse_show_hide(optarg, HIDE_LIST);
4309 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004310 case 'h':
4311 default:
4312 help();
4313 exit(1);
4314 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004315 {
4316 double interval = strtod(optarg, NULL);
4317
4318 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004319 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004320 interval);
4321 exit(2);
4322 }
4323
4324 interval_ts.tv_sec = interval;
4325 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4326 }
Len Brown889facb2012-11-08 00:48:57 -05004327 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004328 case 'J':
4329 rapl_joules++;
4330 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004331 case 'o':
4332 outf = fopen_or_die(optarg, "w");
4333 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004334 case 'P':
4335 show_pkg_only++;
4336 break;
4337 case 'p':
4338 show_core_only++;
4339 break;
Len Brown812db3f2017-02-10 00:25:41 -05004340 case 's':
4341 parse_show_hide(optarg, SHOW_LIST);
4342 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004343 case 'S':
4344 summary_only++;
4345 break;
4346 case 'T':
4347 tcc_activation_temp_override = atoi(optarg);
4348 break;
4349 case 'v':
4350 print_version();
4351 exit(0);
4352 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004353 }
4354 }
4355}
4356
4357int main(int argc, char **argv)
4358{
Len Brownb7d8c142016-02-13 23:36:17 -05004359 outf = stderr;
4360
Len Brown103a8fe2010-10-22 23:53:03 -04004361 cmdline(argc, argv);
4362
Len Brownd8af6f52015-02-10 01:56:38 -05004363 if (debug)
4364 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004365
4366 turbostat_init();
4367
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004368 /* dump counters and exit */
4369 if (dump_only)
4370 return get_and_dump_counters();
4371
Len Brown103a8fe2010-10-22 23:53:03 -04004372 /*
4373 * if any params left, it must be a command to fork
4374 */
4375 if (argc - optind)
4376 return fork_it(argv + optind);
4377 else
4378 turbostat_loop();
4379
4380 return 0;
4381}