blob: eb6cc8ccef06e1e1c0cc43f5aa9c18e6fdc19be5 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
32#include <sys/resource.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <sys/time.h>
36#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050037#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038#include <dirent.h>
39#include <string.h>
40#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040041#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050042#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070043#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040044#include <linux/capability.h>
45#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040046
Len Brown103a8fe2010-10-22 23:53:03 -040047char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050048FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050049int *fd_percpu;
Len Brown2a0609c2016-02-12 22:44:48 -050050struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050051unsigned int debug;
Len Brown96e47152017-01-21 02:26:00 -050052unsigned int quiet;
Len Brownd8af6f52015-02-10 01:56:38 -050053unsigned int rapl_joules;
54unsigned int summary_only;
55unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040056unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070057unsigned int do_knl_cstates;
Len Brown0b2bb692015-03-26 00:50:30 -040058unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050059unsigned int do_slm_cstates;
60unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040061unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050062unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040063unsigned int do_irtl_snb;
64unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050065unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int genuine_intel;
67unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050068unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050069unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020070unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040071double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040072double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040073unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040074double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040075unsigned int show_pkg_only;
76unsigned int show_core_only;
77char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050078unsigned int do_rapl;
79unsigned int do_dts;
80unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050081unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050082unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050083unsigned int tcc_activation_temp;
84unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050085double rapl_power_units, rapl_time_units;
86double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050087double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040088unsigned int do_core_perf_limit_reasons;
89unsigned int do_gfx_perf_limit_reasons;
90unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040091unsigned int crystal_hz;
92unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040093int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040094double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050095unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
96 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
97unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
98unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
99unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
100unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown33148d62017-01-21 01:26:16 -0500101unsigned int has_misc_feature_control;
Len Brown889facb2012-11-08 00:48:57 -0500102
Len Browne6f9bb32013-12-03 02:19:19 -0500103#define RAPL_PKG (1 << 0)
104 /* 0x610 MSR_PKG_POWER_LIMIT */
105 /* 0x611 MSR_PKG_ENERGY_STATUS */
106#define RAPL_PKG_PERF_STATUS (1 << 1)
107 /* 0x613 MSR_PKG_PERF_STATUS */
108#define RAPL_PKG_POWER_INFO (1 << 2)
109 /* 0x614 MSR_PKG_POWER_INFO */
110
111#define RAPL_DRAM (1 << 3)
112 /* 0x618 MSR_DRAM_POWER_LIMIT */
113 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500114#define RAPL_DRAM_PERF_STATUS (1 << 4)
115 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400116#define RAPL_DRAM_POWER_INFO (1 << 5)
117 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500118
Jacob Pan91484942016-06-16 09:48:20 -0700119#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500120 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400121#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500122 /* 0x63a MSR_PP0_POLICY */
123
Len Brown0b2bb692015-03-26 00:50:30 -0400124#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500125 /* 0x640 MSR_PP1_POWER_LIMIT */
126 /* 0x641 MSR_PP1_ENERGY_STATUS */
127 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700128
129#define RAPL_CORES_ENERGY_STATUS (1 << 9)
130 /* 0x639 MSR_PP0_ENERGY_STATUS */
131#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500132#define TJMAX_DEFAULT 100
133
134#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400135
Len Brown388e9c82016-12-22 23:57:55 -0500136/*
137 * buffer size used by sscanf() for added column names
138 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
139 */
140#define NAME_BYTES 20
Len Brown495c76542017-02-08 02:41:51 -0500141#define PATH_BYTES 128
Len Brown388e9c82016-12-22 23:57:55 -0500142
Len Brown103a8fe2010-10-22 23:53:03 -0400143int backwards_count;
144char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brownc98d5d92012-06-04 00:56:40 -0400146cpu_set_t *cpu_present_set, *cpu_affinity_set;
147size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown678a3bd2017-02-09 22:22:13 -0500148#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400149
Len Brownc98d5d92012-06-04 00:56:40 -0400150struct thread_data {
151 unsigned long long tsc;
152 unsigned long long aperf;
153 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500154 unsigned long long c1;
Len Brown562a2d32016-02-26 23:48:05 -0500155 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500156 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400157 unsigned int cpu_id;
158 unsigned int flags;
159#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
160#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500161 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400162} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400163
Len Brownc98d5d92012-06-04 00:56:40 -0400164struct core_data {
165 unsigned long long c3;
166 unsigned long long c6;
167 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500168 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500169 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400170 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500171 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400172} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400173
Len Brownc98d5d92012-06-04 00:56:40 -0400174struct pkg_data {
175 unsigned long long pc2;
176 unsigned long long pc3;
177 unsigned long long pc6;
178 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800179 unsigned long long pc8;
180 unsigned long long pc9;
181 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400182 unsigned long long pkg_wtd_core_c0;
183 unsigned long long pkg_any_core_c0;
184 unsigned long long pkg_any_gfxe_c0;
185 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400186 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500187 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400188 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500189 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
190 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
191 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
192 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
193 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
194 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
195 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500196 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400197} *package_even, *package_odd;
198
199#define ODD_COUNTERS thread_odd, core_odd, package_odd
200#define EVEN_COUNTERS thread_even, core_even, package_even
201
202#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
203 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
204 topo.num_threads_per_core + \
205 (core_no) * topo.num_threads_per_core + (thread_no))
206#define GET_CORE(core_base, core_no, pkg_no) \
207 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
208#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
209
Len Brown388e9c82016-12-22 23:57:55 -0500210enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
Len Brown41618e62017-02-09 18:25:22 -0500211enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
Len Brown388e9c82016-12-22 23:57:55 -0500212enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
213
214struct msr_counter {
215 unsigned int msr_num;
216 char name[NAME_BYTES];
Len Brown495c76542017-02-08 02:41:51 -0500217 char path[PATH_BYTES];
Len Brown388e9c82016-12-22 23:57:55 -0500218 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 Brown41618e62017-02-09 18:25:22 -0500225#define SYSFS_PERCPU (1 << 1)
Len Brown388e9c82016-12-22 23:57:55 -0500226};
227
228struct sys_counters {
Len Brown678a3bd2017-02-09 22:22:13 -0500229 unsigned int added_thread_counters;
230 unsigned int added_core_counters;
231 unsigned int added_package_counters;
Len Brown388e9c82016-12-22 23:57:55 -0500232 struct msr_counter *tp;
233 struct msr_counter *cp;
234 struct msr_counter *pp;
235} sys;
236
Len Brownc98d5d92012-06-04 00:56:40 -0400237struct system_summary {
238 struct thread_data threads;
239 struct core_data cores;
240 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500241} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400242
243
244struct topo_params {
245 int num_packages;
246 int num_cpus;
247 int num_cores;
248 int max_cpu_num;
249 int num_cores_per_pkg;
250 int num_threads_per_core;
251} topo;
252
253struct timeval tv_even, tv_odd, tv_delta;
254
Len Brown562a2d32016-02-26 23:48:05 -0500255int *irq_column_2_cpu; /* /proc/interrupts column numbers */
256int *irqs_per_cpu; /* indexed by cpu_num */
257
Len Brownc98d5d92012-06-04 00:56:40 -0400258void setup_all_buffers(void);
259
260int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400261{
Len Brownc98d5d92012-06-04 00:56:40 -0400262 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400263}
Len Brown88c32812012-03-29 21:44:40 -0400264/*
Len Brownc98d5d92012-06-04 00:56:40 -0400265 * run func(thread, core, package) in topology order
266 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400267 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400268
Len Brownc98d5d92012-06-04 00:56:40 -0400269int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
270 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400271{
Len Brownc98d5d92012-06-04 00:56:40 -0400272 int retval, pkg_no, core_no, thread_no;
273
274 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
275 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
276 for (thread_no = 0; thread_no <
277 topo.num_threads_per_core; ++thread_no) {
278 struct thread_data *t;
279 struct core_data *c;
280 struct pkg_data *p;
281
282 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
283
284 if (cpu_is_not_present(t->cpu_id))
285 continue;
286
287 c = GET_CORE(core_base, core_no, pkg_no);
288 p = GET_PKG(pkg_base, pkg_no);
289
290 retval = func(t, c, p);
291 if (retval)
292 return retval;
293 }
294 }
295 }
296 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400297}
298
299int cpu_migrate(int cpu)
300{
Len Brownc98d5d92012-06-04 00:56:40 -0400301 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
302 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
303 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400304 return -1;
305 else
306 return 0;
307}
Len Brown36229892016-02-26 20:51:02 -0500308int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400309{
Len Brown103a8fe2010-10-22 23:53:03 -0400310 char pathname[32];
311 int fd;
312
Len Brown36229892016-02-26 20:51:02 -0500313 fd = fd_percpu[cpu];
314
315 if (fd)
316 return fd;
317
Len Brown103a8fe2010-10-22 23:53:03 -0400318 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
319 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400320 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400321 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 -0400322
Len Brown36229892016-02-26 20:51:02 -0500323 fd_percpu[cpu] = fd;
324
325 return fd;
326}
327
328int get_msr(int cpu, off_t offset, unsigned long long *msr)
329{
330 ssize_t retval;
331
332 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400333
Len Brown98481e72014-08-15 00:36:50 -0400334 if (retval != sizeof *msr)
Len Browncf4cbe52017-01-01 13:08:33 -0500335 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400336
337 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400338}
339
Len Brownfc04cc62014-02-06 00:55:19 -0500340/*
Len Brown812db3f2017-02-10 00:25:41 -0500341 * Each string in this array is compared in --show and --hide cmdline.
342 * Thus, strings that are proper sub-sets must follow their more specific peers.
Len Brownfc04cc62014-02-06 00:55:19 -0500343 */
Len Brown812db3f2017-02-10 00:25:41 -0500344struct msr_counter bic[] = {
345 { 0x0, "Package" },
346 { 0x0, "Avg_MHz" },
347 { 0x0, "Bzy_MHz" },
348 { 0x0, "TSC_MHz" },
349 { 0x0, "IRQ" },
Len Brown495c76542017-02-08 02:41:51 -0500350 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
Len Brown812db3f2017-02-10 00:25:41 -0500351 { 0x0, "Busy%" },
352 { 0x0, "CPU%c1" },
353 { 0x0, "CPU%c3" },
354 { 0x0, "CPU%c6" },
355 { 0x0, "CPU%c7" },
356 { 0x0, "ThreadC" },
357 { 0x0, "CoreTmp" },
358 { 0x0, "CoreCnt" },
359 { 0x0, "PkgTmp" },
360 { 0x0, "GFX%rc6" },
361 { 0x0, "GFXMHz" },
362 { 0x0, "Pkg%pc2" },
363 { 0x0, "Pkg%pc3" },
364 { 0x0, "Pkg%pc6" },
365 { 0x0, "Pkg%pc7" },
Len Brown0f47c082017-01-27 00:50:45 -0500366 { 0x0, "Pkg%pc8" },
367 { 0x0, "Pkg%pc9" },
368 { 0x0, "Pkg%pc10" },
Len Brown812db3f2017-02-10 00:25:41 -0500369 { 0x0, "PkgWatt" },
370 { 0x0, "CorWatt" },
371 { 0x0, "GFXWatt" },
372 { 0x0, "PkgCnt" },
373 { 0x0, "RAMWatt" },
374 { 0x0, "PKG_%" },
375 { 0x0, "RAM_%" },
376 { 0x0, "Pkg_J" },
377 { 0x0, "Cor_J" },
378 { 0x0, "GFX_J" },
379 { 0x0, "RAM_J" },
380 { 0x0, "Core" },
381 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500382 { 0x0, "Mod%c6" },
Len Brown41618e62017-02-09 18:25:22 -0500383 { 0x0, "sysfs" },
Len Brown812db3f2017-02-10 00:25:41 -0500384};
385
386#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
387#define BIC_Package (1ULL << 0)
388#define BIC_Avg_MHz (1ULL << 1)
389#define BIC_Bzy_MHz (1ULL << 2)
390#define BIC_TSC_MHz (1ULL << 3)
391#define BIC_IRQ (1ULL << 4)
392#define BIC_SMI (1ULL << 5)
393#define BIC_Busy (1ULL << 6)
394#define BIC_CPU_c1 (1ULL << 7)
395#define BIC_CPU_c3 (1ULL << 8)
396#define BIC_CPU_c6 (1ULL << 9)
397#define BIC_CPU_c7 (1ULL << 10)
398#define BIC_ThreadC (1ULL << 11)
399#define BIC_CoreTmp (1ULL << 12)
400#define BIC_CoreCnt (1ULL << 13)
401#define BIC_PkgTmp (1ULL << 14)
402#define BIC_GFX_rc6 (1ULL << 15)
403#define BIC_GFXMHz (1ULL << 16)
404#define BIC_Pkgpc2 (1ULL << 17)
405#define BIC_Pkgpc3 (1ULL << 18)
406#define BIC_Pkgpc6 (1ULL << 19)
407#define BIC_Pkgpc7 (1ULL << 20)
Len Brown0f47c082017-01-27 00:50:45 -0500408#define BIC_Pkgpc8 (1ULL << 21)
409#define BIC_Pkgpc9 (1ULL << 22)
410#define BIC_Pkgpc10 (1ULL << 23)
411#define BIC_PkgWatt (1ULL << 24)
412#define BIC_CorWatt (1ULL << 25)
413#define BIC_GFXWatt (1ULL << 26)
414#define BIC_PkgCnt (1ULL << 27)
415#define BIC_RAMWatt (1ULL << 28)
416#define BIC_PKG__ (1ULL << 29)
417#define BIC_RAM__ (1ULL << 30)
418#define BIC_Pkg_J (1ULL << 31)
419#define BIC_Cor_J (1ULL << 32)
420#define BIC_GFX_J (1ULL << 33)
421#define BIC_RAM_J (1ULL << 34)
422#define BIC_Core (1ULL << 35)
423#define BIC_CPU (1ULL << 36)
424#define BIC_Mod_c6 (1ULL << 37)
Len Brown41618e62017-02-09 18:25:22 -0500425#define BIC_sysfs (1ULL << 38)
Len Brown812db3f2017-02-10 00:25:41 -0500426
427unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
Len Brown41618e62017-02-09 18:25:22 -0500428unsigned long long bic_present = BIC_sysfs;
Len Brown812db3f2017-02-10 00:25:41 -0500429
430#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
431#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
Len Brown0f47c082017-01-27 00:50:45 -0500432#define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
Len Brown812db3f2017-02-10 00:25:41 -0500433
434/*
435 * bic_lookup
436 * for all the strings in comma separate name_list,
437 * set the approprate bit in return value.
438 */
439unsigned long long bic_lookup(char *name_list)
440{
441 int i;
442 unsigned long long retval = 0;
443
444 while (name_list) {
445 char *comma;
446
447 comma = strchr(name_list, ',');
448
449 if (comma)
450 *comma = '\0';
451
452 for (i = 0; i < MAX_BIC; ++i) {
453 if (!strcmp(name_list, bic[i].name)) {
454 retval |= (1ULL << i);
455 break;
456 }
457 }
458 if (i == MAX_BIC) {
459 fprintf(stderr, "Invalid counter name: %s\n", name_list);
460 exit(-1);
461 }
462
463 name_list = comma;
464 if (name_list)
465 name_list++;
466
467 }
468 return retval;
469}
Len Brownfc04cc62014-02-06 00:55:19 -0500470
Len Browna829eb42011-02-10 23:36:34 -0500471void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400472{
Len Brown388e9c82016-12-22 23:57:55 -0500473 struct msr_counter *mp;
474
Len Brown812db3f2017-02-10 00:25:41 -0500475 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400476 outp += sprintf(outp, "\tPackage");
Len Brown812db3f2017-02-10 00:25:41 -0500477 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400478 outp += sprintf(outp, "\tCore");
Len Brown812db3f2017-02-10 00:25:41 -0500479 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400480 outp += sprintf(outp, "\tCPU");
Len Brown812db3f2017-02-10 00:25:41 -0500481 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400482 outp += sprintf(outp, "\tAvg_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500483 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400484 outp += sprintf(outp, "\tBusy%%");
Len Brown812db3f2017-02-10 00:25:41 -0500485 if (DO_BIC(BIC_Bzy_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400486 outp += sprintf(outp, "\tBzy_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500487 if (DO_BIC(BIC_TSC_MHz))
488 outp += sprintf(outp, "\tTSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500489
Len Brown812db3f2017-02-10 00:25:41 -0500490 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400491 outp += sprintf(outp, "\tIRQ");
Len Brown812db3f2017-02-10 00:25:41 -0500492 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400493 outp += sprintf(outp, "\tSMI");
Len Brown1cc21f72015-02-23 00:34:57 -0500494
Len Brown388e9c82016-12-22 23:57:55 -0500495 for (mp = sys.tp; mp; mp = mp->next) {
496 if (mp->format == FORMAT_RAW) {
497 if (mp->width == 64)
498 outp += sprintf(outp, "\t%18.18s", mp->name);
499 else
500 outp += sprintf(outp, "\t%10.10s", mp->name);
501 } else {
Len Brown41618e62017-02-09 18:25:22 -0500502 outp += sprintf(outp, "\t%s", mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500503 }
504 }
505
Len Brown41618e62017-02-09 18:25:22 -0500506 if (DO_BIC(BIC_CPU_c1))
507 outp += sprintf(outp, "\tCPU%%c1");
Len Brown812db3f2017-02-10 00:25:41 -0500508 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown678a3bd2017-02-09 22:22:13 -0500509 outp += sprintf(outp, "\tCPU%%c3");
Len Brown812db3f2017-02-10 00:25:41 -0500510 if (DO_BIC(BIC_CPU_c6))
Len Brown678a3bd2017-02-09 22:22:13 -0500511 outp += sprintf(outp, "\tCPU%%c6");
Len Brown812db3f2017-02-10 00:25:41 -0500512 if (DO_BIC(BIC_CPU_c7))
Len Brown678a3bd2017-02-09 22:22:13 -0500513 outp += sprintf(outp, "\tCPU%%c7");
514
Len Brown0539ba12017-02-10 00:27:20 -0500515 if (DO_BIC(BIC_Mod_c6))
516 outp += sprintf(outp, "\tMod%%c6");
Len Brown678a3bd2017-02-09 22:22:13 -0500517
Len Brown812db3f2017-02-10 00:25:41 -0500518 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400519 outp += sprintf(outp, "\tCoreTmp");
Len Brown388e9c82016-12-22 23:57:55 -0500520
521 for (mp = sys.cp; mp; mp = mp->next) {
522 if (mp->format == FORMAT_RAW) {
523 if (mp->width == 64)
524 outp += sprintf(outp, "\t%18.18s", mp->name);
525 else
526 outp += sprintf(outp, "\t%10.10s", mp->name);
527 } else {
Len Brown41618e62017-02-09 18:25:22 -0500528 outp += sprintf(outp, "\t%s", mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500529 }
530 }
531
Len Brown812db3f2017-02-10 00:25:41 -0500532 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400533 outp += sprintf(outp, "\tPkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500534
Len Brown812db3f2017-02-10 00:25:41 -0500535 if (DO_BIC(BIC_GFX_rc6))
Len Brown3d109de2016-04-22 23:24:27 -0400536 outp += sprintf(outp, "\tGFX%%rc6");
Len Brownfdf676e2016-02-27 01:28:12 -0500537
Len Brown812db3f2017-02-10 00:25:41 -0500538 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400539 outp += sprintf(outp, "\tGFXMHz");
Len Brown27d47352016-02-27 00:37:54 -0500540
Len Brown0b2bb692015-03-26 00:50:30 -0400541 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400542 outp += sprintf(outp, "\tTotl%%C0");
543 outp += sprintf(outp, "\tAny%%C0");
544 outp += sprintf(outp, "\tGFX%%C0");
545 outp += sprintf(outp, "\tCPUGFX%%");
Len Brown0b2bb692015-03-26 00:50:30 -0400546 }
547
Len Brown0f47c082017-01-27 00:50:45 -0500548 if (DO_BIC(BIC_Pkgpc2))
Len Brown3d109de2016-04-22 23:24:27 -0400549 outp += sprintf(outp, "\tPkg%%pc2");
Len Brown0f47c082017-01-27 00:50:45 -0500550 if (DO_BIC(BIC_Pkgpc3))
Len Brown3d109de2016-04-22 23:24:27 -0400551 outp += sprintf(outp, "\tPkg%%pc3");
Len Brown0f47c082017-01-27 00:50:45 -0500552 if (DO_BIC(BIC_Pkgpc6))
Len Brown3d109de2016-04-22 23:24:27 -0400553 outp += sprintf(outp, "\tPkg%%pc6");
Len Brown0f47c082017-01-27 00:50:45 -0500554 if (DO_BIC(BIC_Pkgpc7))
Len Brown3d109de2016-04-22 23:24:27 -0400555 outp += sprintf(outp, "\tPkg%%pc7");
Len Brown0f47c082017-01-27 00:50:45 -0500556 if (DO_BIC(BIC_Pkgpc8))
Len Brown3d109de2016-04-22 23:24:27 -0400557 outp += sprintf(outp, "\tPkg%%pc8");
Len Brown0f47c082017-01-27 00:50:45 -0500558 if (DO_BIC(BIC_Pkgpc9))
Len Brown3d109de2016-04-22 23:24:27 -0400559 outp += sprintf(outp, "\tPkg%%pc9");
Len Brown0f47c082017-01-27 00:50:45 -0500560 if (DO_BIC(BIC_Pkgpc10))
Len Brown3d109de2016-04-22 23:24:27 -0400561 outp += sprintf(outp, "\tPk%%pc10");
Len Brown103a8fe2010-10-22 23:53:03 -0400562
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800563 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500564 if (DO_BIC(BIC_PkgWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400565 outp += sprintf(outp, "\tPkgWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500566 if (DO_BIC(BIC_CorWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400567 outp += sprintf(outp, "\tCorWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500568 if (DO_BIC(BIC_GFXWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400569 outp += sprintf(outp, "\tGFXWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500570 if (DO_BIC(BIC_RAMWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400571 outp += sprintf(outp, "\tRAMWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500572 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400573 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500574 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400575 outp += sprintf(outp, "\tRAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500576 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500577 if (DO_BIC(BIC_Pkg_J))
Len Brown3d109de2016-04-22 23:24:27 -0400578 outp += sprintf(outp, "\tPkg_J");
Len Brown812db3f2017-02-10 00:25:41 -0500579 if (DO_BIC(BIC_Cor_J))
Len Brown3d109de2016-04-22 23:24:27 -0400580 outp += sprintf(outp, "\tCor_J");
Len Brown812db3f2017-02-10 00:25:41 -0500581 if (DO_BIC(BIC_GFX_J))
Len Brown3d109de2016-04-22 23:24:27 -0400582 outp += sprintf(outp, "\tGFX_J");
Len Brown812db3f2017-02-10 00:25:41 -0500583 if (DO_BIC(BIC_RAM_J))
Len Brown3d109de2016-04-22 23:24:27 -0400584 outp += sprintf(outp, "\tRAM_J");
Len Brown812db3f2017-02-10 00:25:41 -0500585 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400586 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500587 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400588 outp += sprintf(outp, "\tRAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800589 }
Len Brown388e9c82016-12-22 23:57:55 -0500590 for (mp = sys.pp; mp; mp = mp->next) {
591 if (mp->format == FORMAT_RAW) {
592 if (mp->width == 64)
593 outp += sprintf(outp, "\t%18.18s", mp->name);
594 else
595 outp += sprintf(outp, "\t%10.10s", mp->name);
596 } else {
Len Brown41618e62017-02-09 18:25:22 -0500597 outp += sprintf(outp, "\t%s", mp->name);
Len Brown388e9c82016-12-22 23:57:55 -0500598 }
599 }
600
Len Brownc98d5d92012-06-04 00:56:40 -0400601 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400602}
603
Len Brownc98d5d92012-06-04 00:56:40 -0400604int dump_counters(struct thread_data *t, struct core_data *c,
605 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400606{
Len Brown388e9c82016-12-22 23:57:55 -0500607 int i;
608 struct msr_counter *mp;
609
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200610 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400611
Len Brownc98d5d92012-06-04 00:56:40 -0400612 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200613 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
614 t->cpu_id, t->flags);
615 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
616 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
617 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
618 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500619
Len Brown812db3f2017-02-10 00:25:41 -0500620 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -0500621 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500622 if (DO_BIC(BIC_SMI))
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200623 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500624
625 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
626 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
627 i, mp->msr_num, t->counter[i]);
628 }
Len Brownc98d5d92012-06-04 00:56:40 -0400629 }
Len Brown103a8fe2010-10-22 23:53:03 -0400630
Len Brownc98d5d92012-06-04 00:56:40 -0400631 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200632 outp += sprintf(outp, "core: %d\n", c->core_id);
633 outp += sprintf(outp, "c3: %016llX\n", c->c3);
634 outp += sprintf(outp, "c6: %016llX\n", c->c6);
635 outp += sprintf(outp, "c7: %016llX\n", c->c7);
636 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500637
638 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
639 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
640 i, mp->msr_num, c->counter[i]);
641 }
Len Brown0539ba12017-02-10 00:27:20 -0500642 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400643 }
644
645 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200646 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400647
648 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
649 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
650 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
651 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
652
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200653 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brown0f47c082017-01-27 00:50:45 -0500654 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500655 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
Len Brown0f47c082017-01-27 00:50:45 -0500656 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500657 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
Len Brown0f47c082017-01-27 00:50:45 -0500658 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500659 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200660 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
661 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
662 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
663 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
664 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
665 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
666 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
667 outp += sprintf(outp, "Throttle PKG: %0X\n",
668 p->rapl_pkg_perf_status);
669 outp += sprintf(outp, "Throttle RAM: %0X\n",
670 p->rapl_dram_perf_status);
671 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500672
673 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
674 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
675 i, mp->msr_num, p->counter[i]);
676 }
Len Brownc98d5d92012-06-04 00:56:40 -0400677 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200678
679 outp += sprintf(outp, "\n");
680
Len Brownc98d5d92012-06-04 00:56:40 -0400681 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400682}
683
Len Browne23da032012-02-06 18:37:16 -0500684/*
685 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500686 */
Len Brownc98d5d92012-06-04 00:56:40 -0400687int format_counters(struct thread_data *t, struct core_data *c,
688 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400689{
Len Brown008d396e2017-02-10 00:29:51 -0500690 double interval_float, tsc;
Len Brownfc04cc62014-02-06 00:55:19 -0500691 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500692 int i;
693 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400694
Len Brownc98d5d92012-06-04 00:56:40 -0400695 /* if showing only 1st thread in core and this isn't one, bail out */
696 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
697 return 0;
698
699 /* if showing only 1st thread in pkg and this isn't one, bail out */
700 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
701 return 0;
702
Len Brown103a8fe2010-10-22 23:53:03 -0400703 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
704
Len Brown008d396e2017-02-10 00:29:51 -0500705 tsc = t->tsc * tsc_tweak;
706
Len Brownc98d5d92012-06-04 00:56:40 -0400707 /* topo columns, print blanks on 1st (average) line */
708 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500709 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400710 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500711 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400712 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500713 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400714 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400715 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500716 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400717 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400718 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400719 else
Len Brown3d109de2016-04-22 23:24:27 -0400720 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400721 }
Len Brown812db3f2017-02-10 00:25:41 -0500722 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400723 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400724 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400725 else
Len Brown3d109de2016-04-22 23:24:27 -0400726 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400727 }
Len Brown812db3f2017-02-10 00:25:41 -0500728 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400729 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400730 }
Len Brownfc04cc62014-02-06 00:55:19 -0500731
Len Brown812db3f2017-02-10 00:25:41 -0500732 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400733 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500734 1.0 / units * t->aperf / interval_float);
735
Len Brown812db3f2017-02-10 00:25:41 -0500736 if (DO_BIC(BIC_Busy))
Len Brown008d396e2017-02-10 00:29:51 -0500737 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400738
Len Brown812db3f2017-02-10 00:25:41 -0500739 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400740 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400741 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400742 else
Len Brown3d109de2016-04-22 23:24:27 -0400743 outp += sprintf(outp, "\t%.0f",
Len Brown008d396e2017-02-10 00:29:51 -0500744 tsc / units * t->aperf / t->mperf / interval_float);
Len Brown21ed5572015-10-19 22:37:40 -0400745 }
Len Brown103a8fe2010-10-22 23:53:03 -0400746
Len Brown812db3f2017-02-10 00:25:41 -0500747 if (DO_BIC(BIC_TSC_MHz))
748 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400749
Len Brown562a2d32016-02-26 23:48:05 -0500750 /* IRQ */
Len Brown812db3f2017-02-10 00:25:41 -0500751 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400752 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500753
Len Brown1cc21f72015-02-23 00:34:57 -0500754 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500755 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400756 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500757
Len Brown678a3bd2017-02-09 22:22:13 -0500758 /* Added counters */
759 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
760 if (mp->format == FORMAT_RAW) {
761 if (mp->width == 32)
762 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
763 else
764 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
765 } else if (mp->format == FORMAT_DELTA) {
766 outp += sprintf(outp, "\t%lld", t->counter[i]);
767 } else if (mp->format == FORMAT_PERCENT) {
Len Brown41618e62017-02-09 18:25:22 -0500768 if (mp->type == COUNTER_USEC)
769 outp += sprintf(outp, "\t%.2f", t->counter[i]/interval_float/10000);
770 else
771 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/tsc);
Len Brown678a3bd2017-02-09 22:22:13 -0500772 }
773 }
774
Len Brown41618e62017-02-09 18:25:22 -0500775 /* C1 */
776 if (DO_BIC(BIC_CPU_c1))
777 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/tsc);
778
779
Len Brownc98d5d92012-06-04 00:56:40 -0400780 /* print per-core data only for 1st thread in core */
781 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
782 goto done;
783
Len Brown812db3f2017-02-10 00:25:41 -0500784 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown008d396e2017-02-10 00:29:51 -0500785 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500786 if (DO_BIC(BIC_CPU_c6))
Len Brown008d396e2017-02-10 00:29:51 -0500787 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500788 if (DO_BIC(BIC_CPU_c7))
Len Brown008d396e2017-02-10 00:29:51 -0500789 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400790
Len Brown0539ba12017-02-10 00:27:20 -0500791 /* Mod%c6 */
792 if (DO_BIC(BIC_Mod_c6))
Len Brown008d396e2017-02-10 00:29:51 -0500793 outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / tsc);
Len Brown0539ba12017-02-10 00:27:20 -0500794
Len Brown812db3f2017-02-10 00:25:41 -0500795 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400796 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500797
Len Brown388e9c82016-12-22 23:57:55 -0500798 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
799 if (mp->format == FORMAT_RAW) {
800 if (mp->width == 32)
801 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
802 else
803 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
804 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500805 outp += sprintf(outp, "\t%lld", c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500806 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500807 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500808 }
809 }
810
Len Brownc98d5d92012-06-04 00:56:40 -0400811 /* print per-package data only for 1st core in package */
812 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
813 goto done;
814
Len Brown0b2bb692015-03-26 00:50:30 -0400815 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500816 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400817 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500818
Len Brownfdf676e2016-02-27 01:28:12 -0500819 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500820 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400821 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400822 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400823 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400824 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400825 p->gfx_rc6_ms / 10.0 / interval_float);
826 }
827 }
Len Brownfdf676e2016-02-27 01:28:12 -0500828
Len Brown27d47352016-02-27 00:37:54 -0500829 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500830 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400831 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500832
Len Brown0b2bb692015-03-26 00:50:30 -0400833 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
834 if (do_skl_residency) {
Len Brown008d396e2017-02-10 00:29:51 -0500835 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/tsc);
836 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/tsc);
837 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/tsc);
838 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400839 }
840
Len Brown0f47c082017-01-27 00:50:45 -0500841 if (DO_BIC(BIC_Pkgpc2))
Len Brown008d396e2017-02-10 00:29:51 -0500842 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500843 if (DO_BIC(BIC_Pkgpc3))
Len Brown008d396e2017-02-10 00:29:51 -0500844 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500845 if (DO_BIC(BIC_Pkgpc6))
Len Brown008d396e2017-02-10 00:29:51 -0500846 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500847 if (DO_BIC(BIC_Pkgpc7))
Len Brown008d396e2017-02-10 00:29:51 -0500848 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500849 if (DO_BIC(BIC_Pkgpc8))
Len Brown008d396e2017-02-10 00:29:51 -0500850 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500851 if (DO_BIC(BIC_Pkgpc9))
Len Brown008d396e2017-02-10 00:29:51 -0500852 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/tsc);
Len Brown0f47c082017-01-27 00:50:45 -0500853 if (DO_BIC(BIC_Pkgpc10))
Len Brown008d396e2017-02-10 00:29:51 -0500854 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/tsc);
Len Brown889facb2012-11-08 00:48:57 -0500855
856 /*
857 * If measurement interval exceeds minimum RAPL Joule Counter range,
858 * indicate that results are suspect by printing "**" in fraction place.
859 */
Len Brownfc04cc62014-02-06 00:55:19 -0500860 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400861 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500862 else
Len Browne975db52016-04-06 23:56:02 -0400863 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500864
Len Brown812db3f2017-02-10 00:25:41 -0500865 if (DO_BIC(BIC_PkgWatt))
866 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
867 if (DO_BIC(BIC_CorWatt))
868 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
869 if (DO_BIC(BIC_GFXWatt))
870 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
871 if (DO_BIC(BIC_RAMWatt))
872 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
873 if (DO_BIC(BIC_Pkg_J))
874 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
875 if (DO_BIC(BIC_Cor_J))
876 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
877 if (DO_BIC(BIC_GFX_J))
878 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
879 if (DO_BIC(BIC_RAM_J))
880 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
881 if (DO_BIC(BIC_PKG__))
882 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
883 if (DO_BIC(BIC_RAM__))
884 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
885
Len Brown388e9c82016-12-22 23:57:55 -0500886 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
887 if (mp->format == FORMAT_RAW) {
888 if (mp->width == 32)
889 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
890 else
891 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
892 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500893 outp += sprintf(outp, "\t%lld", p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500894 } else if (mp->format == FORMAT_PERCENT) {
Len Brown008d396e2017-02-10 00:29:51 -0500895 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/tsc);
Len Brown388e9c82016-12-22 23:57:55 -0500896 }
897 }
898
Len Brownc98d5d92012-06-04 00:56:40 -0400899done:
Len Brownc98d5d92012-06-04 00:56:40 -0400900 outp += sprintf(outp, "\n");
901
902 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400903}
904
Len Brownb7d8c142016-02-13 23:36:17 -0500905void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400906{
Len Brownb7d8c142016-02-13 23:36:17 -0500907 FILE *filep;
908
909 if (outf == stderr)
910 filep = stdout;
911 else
912 filep = outf;
913
914 fputs(output_buffer, filep);
915 fflush(filep);
916
Len Brownc98d5d92012-06-04 00:56:40 -0400917 outp = output_buffer;
918}
Len Brownb7d8c142016-02-13 23:36:17 -0500919void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400920{
Len Brownb7d8c142016-02-13 23:36:17 -0500921 fputs(output_buffer, outf);
922 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400923 outp = output_buffer;
924}
925void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
926{
Len Browne23da032012-02-06 18:37:16 -0500927 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400928
Len Browne23da032012-02-06 18:37:16 -0500929 if (!printed || !summary_only)
930 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400931
Len Brownc98d5d92012-06-04 00:56:40 -0400932 if (topo.num_cpus > 1)
933 format_counters(&average.threads, &average.cores,
934 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400935
Len Browne23da032012-02-06 18:37:16 -0500936 printed = 1;
937
938 if (summary_only)
939 return;
940
Len Brownc98d5d92012-06-04 00:56:40 -0400941 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400942}
943
Len Brown889facb2012-11-08 00:48:57 -0500944#define DELTA_WRAP32(new, old) \
945 if (new > old) { \
946 old = new - old; \
947 } else { \
948 old = 0x100000000 + new - old; \
949 }
950
Len Brownba3dec92016-04-22 20:31:46 -0400951int
Len Brownc98d5d92012-06-04 00:56:40 -0400952delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400953{
Len Brown388e9c82016-12-22 23:57:55 -0500954 int i;
955 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400956
957 if (do_skl_residency) {
958 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
959 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
960 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
961 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
962 }
Len Brownc98d5d92012-06-04 00:56:40 -0400963 old->pc2 = new->pc2 - old->pc2;
Len Brown0f47c082017-01-27 00:50:45 -0500964 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -0500965 old->pc3 = new->pc3 - old->pc3;
Len Brown0f47c082017-01-27 00:50:45 -0500966 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -0500967 old->pc6 = new->pc6 - old->pc6;
Len Brown0f47c082017-01-27 00:50:45 -0500968 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -0500969 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800970 old->pc8 = new->pc8 - old->pc8;
971 old->pc9 = new->pc9 - old->pc9;
972 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500973 old->pkg_temp_c = new->pkg_temp_c;
974
Len Brown9185e982016-04-06 17:16:00 -0400975 /* flag an error when rc6 counter resets/wraps */
976 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
977 old->gfx_rc6_ms = -1;
978 else
979 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
980
Len Brown27d47352016-02-27 00:37:54 -0500981 old->gfx_mhz = new->gfx_mhz;
982
Len Brown889facb2012-11-08 00:48:57 -0500983 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
984 DELTA_WRAP32(new->energy_cores, old->energy_cores);
985 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
986 DELTA_WRAP32(new->energy_dram, old->energy_dram);
987 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
988 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400989
Len Brown388e9c82016-12-22 23:57:55 -0500990 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
991 if (mp->format == FORMAT_RAW)
992 old->counter[i] = new->counter[i];
993 else
994 old->counter[i] = new->counter[i] - old->counter[i];
995 }
996
Len Brownba3dec92016-04-22 20:31:46 -0400997 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400998}
Len Brown103a8fe2010-10-22 23:53:03 -0400999
Len Brownc98d5d92012-06-04 00:56:40 -04001000void
1001delta_core(struct core_data *new, struct core_data *old)
1002{
Len Brown388e9c82016-12-22 23:57:55 -05001003 int i;
1004 struct msr_counter *mp;
1005
Len Brownc98d5d92012-06-04 00:56:40 -04001006 old->c3 = new->c3 - old->c3;
1007 old->c6 = new->c6 - old->c6;
1008 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001009 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001010 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001011
1012 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1013 if (mp->format == FORMAT_RAW)
1014 old->counter[i] = new->counter[i];
1015 else
1016 old->counter[i] = new->counter[i] - old->counter[i];
1017 }
Len Brownc98d5d92012-06-04 00:56:40 -04001018}
Len Brown103a8fe2010-10-22 23:53:03 -04001019
Len Brownc3ae3312012-06-13 21:31:46 -04001020/*
1021 * old = new - old
1022 */
Len Brownba3dec92016-04-22 20:31:46 -04001023int
Len Brownc98d5d92012-06-04 00:56:40 -04001024delta_thread(struct thread_data *new, struct thread_data *old,
1025 struct core_data *core_delta)
1026{
Len Brown388e9c82016-12-22 23:57:55 -05001027 int i;
1028 struct msr_counter *mp;
1029
Len Brownc98d5d92012-06-04 00:56:40 -04001030 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001031
Len Brownc98d5d92012-06-04 00:56:40 -04001032 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001033 if (old->tsc < (1000 * 1000))
1034 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1035 "You can disable all c-states by booting with \"idle=poll\"\n"
1036 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001037
Len Brownc98d5d92012-06-04 00:56:40 -04001038 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001039
Len Brown812db3f2017-02-10 00:25:41 -05001040 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001041 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1042 old->aperf = new->aperf - old->aperf;
1043 old->mperf = new->mperf - old->mperf;
1044 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001045 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001046 }
Len Brownc98d5d92012-06-04 00:56:40 -04001047 }
Len Brown103a8fe2010-10-22 23:53:03 -04001048
Len Brown103a8fe2010-10-22 23:53:03 -04001049
Len Brown144b44b2013-11-09 00:30:16 -05001050 if (use_c1_residency_msr) {
1051 /*
1052 * Some models have a dedicated C1 residency MSR,
1053 * which should be more accurate than the derivation below.
1054 */
1055 } else {
1056 /*
1057 * As counter collection is not atomic,
1058 * it is possible for mperf's non-halted cycles + idle states
1059 * to exceed TSC's all cycles: show c1 = 0% in that case.
1060 */
1061 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1062 old->c1 = 0;
1063 else {
1064 /* normal case, derive c1 */
Len Brown008d396e2017-02-10 00:29:51 -05001065 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001066 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001067 }
Len Brownc98d5d92012-06-04 00:56:40 -04001068 }
Len Brownc3ae3312012-06-13 21:31:46 -04001069
Len Brownc98d5d92012-06-04 00:56:40 -04001070 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001071 if (debug > 1)
1072 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001073 old->mperf = 1; /* divide by 0 protection */
1074 }
1075
Len Brown812db3f2017-02-10 00:25:41 -05001076 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001077 old->irq_count = new->irq_count - old->irq_count;
1078
Len Brown812db3f2017-02-10 00:25:41 -05001079 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001080 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001081
Len Brown388e9c82016-12-22 23:57:55 -05001082 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1083 if (mp->format == FORMAT_RAW)
1084 old->counter[i] = new->counter[i];
1085 else
1086 old->counter[i] = new->counter[i] - old->counter[i];
1087 }
Len Brownba3dec92016-04-22 20:31:46 -04001088 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001089}
1090
1091int delta_cpu(struct thread_data *t, struct core_data *c,
1092 struct pkg_data *p, struct thread_data *t2,
1093 struct core_data *c2, struct pkg_data *p2)
1094{
Len Brownba3dec92016-04-22 20:31:46 -04001095 int retval = 0;
1096
Len Brownc98d5d92012-06-04 00:56:40 -04001097 /* calculate core delta only for 1st thread in core */
1098 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1099 delta_core(c, c2);
1100
1101 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001102 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1103 if (retval)
1104 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001105
1106 /* calculate package delta only for 1st core in package */
1107 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001108 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001109
Len Brownba3dec92016-04-22 20:31:46 -04001110 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001111}
1112
1113void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1114{
Len Brown388e9c82016-12-22 23:57:55 -05001115 int i;
1116 struct msr_counter *mp;
1117
Len Brownc98d5d92012-06-04 00:56:40 -04001118 t->tsc = 0;
1119 t->aperf = 0;
1120 t->mperf = 0;
1121 t->c1 = 0;
1122
Len Brown562a2d32016-02-26 23:48:05 -05001123 t->irq_count = 0;
1124 t->smi_count = 0;
1125
Len Brownc98d5d92012-06-04 00:56:40 -04001126 /* tells format_counters to dump all fields from this set */
1127 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1128
1129 c->c3 = 0;
1130 c->c6 = 0;
1131 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001132 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001133 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001134
Len Brown0b2bb692015-03-26 00:50:30 -04001135 p->pkg_wtd_core_c0 = 0;
1136 p->pkg_any_core_c0 = 0;
1137 p->pkg_any_gfxe_c0 = 0;
1138 p->pkg_both_core_gfxe_c0 = 0;
1139
Len Brownc98d5d92012-06-04 00:56:40 -04001140 p->pc2 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001141 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001142 p->pc3 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001143 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001144 p->pc6 = 0;
Len Brown0f47c082017-01-27 00:50:45 -05001145 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001146 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001147 p->pc8 = 0;
1148 p->pc9 = 0;
1149 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001150
1151 p->energy_pkg = 0;
1152 p->energy_dram = 0;
1153 p->energy_cores = 0;
1154 p->energy_gfx = 0;
1155 p->rapl_pkg_perf_status = 0;
1156 p->rapl_dram_perf_status = 0;
1157 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001158
Len Brownfdf676e2016-02-27 01:28:12 -05001159 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001160 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001161 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1162 t->counter[i] = 0;
1163
1164 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1165 c->counter[i] = 0;
1166
1167 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1168 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001169}
1170int sum_counters(struct thread_data *t, struct core_data *c,
1171 struct pkg_data *p)
1172{
Len Brown388e9c82016-12-22 23:57:55 -05001173 int i;
1174 struct msr_counter *mp;
1175
Len Brownc98d5d92012-06-04 00:56:40 -04001176 average.threads.tsc += t->tsc;
1177 average.threads.aperf += t->aperf;
1178 average.threads.mperf += t->mperf;
1179 average.threads.c1 += t->c1;
1180
Len Brown562a2d32016-02-26 23:48:05 -05001181 average.threads.irq_count += t->irq_count;
1182 average.threads.smi_count += t->smi_count;
1183
Len Brown388e9c82016-12-22 23:57:55 -05001184 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1185 if (mp->format == FORMAT_RAW)
1186 continue;
1187 average.threads.counter[i] += t->counter[i];
1188 }
1189
Len Brownc98d5d92012-06-04 00:56:40 -04001190 /* sum per-core values only for 1st thread in core */
1191 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1192 return 0;
1193
1194 average.cores.c3 += c->c3;
1195 average.cores.c6 += c->c6;
1196 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001197 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001198
Len Brown889facb2012-11-08 00:48:57 -05001199 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1200
Len Brown388e9c82016-12-22 23:57:55 -05001201 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1202 if (mp->format == FORMAT_RAW)
1203 continue;
1204 average.cores.counter[i] += c->counter[i];
1205 }
1206
Len Brownc98d5d92012-06-04 00:56:40 -04001207 /* sum per-pkg values only for 1st core in pkg */
1208 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1209 return 0;
1210
Len Brown0b2bb692015-03-26 00:50:30 -04001211 if (do_skl_residency) {
1212 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1213 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1214 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1215 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1216 }
1217
Len Brownc98d5d92012-06-04 00:56:40 -04001218 average.packages.pc2 += p->pc2;
Len Brown0f47c082017-01-27 00:50:45 -05001219 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001220 average.packages.pc3 += p->pc3;
Len Brown0f47c082017-01-27 00:50:45 -05001221 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001222 average.packages.pc6 += p->pc6;
Len Brown0f47c082017-01-27 00:50:45 -05001223 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001224 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001225 average.packages.pc8 += p->pc8;
1226 average.packages.pc9 += p->pc9;
1227 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001228
Len Brown889facb2012-11-08 00:48:57 -05001229 average.packages.energy_pkg += p->energy_pkg;
1230 average.packages.energy_dram += p->energy_dram;
1231 average.packages.energy_cores += p->energy_cores;
1232 average.packages.energy_gfx += p->energy_gfx;
1233
Len Brownfdf676e2016-02-27 01:28:12 -05001234 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001235 average.packages.gfx_mhz = p->gfx_mhz;
1236
Len Brown889facb2012-11-08 00:48:57 -05001237 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1238
1239 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1240 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001241
1242 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1243 if (mp->format == FORMAT_RAW)
1244 continue;
1245 average.packages.counter[i] += p->counter[i];
1246 }
Len Brownc98d5d92012-06-04 00:56:40 -04001247 return 0;
1248}
1249/*
1250 * sum the counters for all cpus in the system
1251 * compute the weighted average
1252 */
1253void compute_average(struct thread_data *t, struct core_data *c,
1254 struct pkg_data *p)
1255{
Len Brown388e9c82016-12-22 23:57:55 -05001256 int i;
1257 struct msr_counter *mp;
1258
Len Brownc98d5d92012-06-04 00:56:40 -04001259 clear_counters(&average.threads, &average.cores, &average.packages);
1260
1261 for_all_cpus(sum_counters, t, c, p);
1262
1263 average.threads.tsc /= topo.num_cpus;
1264 average.threads.aperf /= topo.num_cpus;
1265 average.threads.mperf /= topo.num_cpus;
1266 average.threads.c1 /= topo.num_cpus;
1267
1268 average.cores.c3 /= topo.num_cores;
1269 average.cores.c6 /= topo.num_cores;
1270 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001271 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001272
Len Brown0b2bb692015-03-26 00:50:30 -04001273 if (do_skl_residency) {
1274 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1275 average.packages.pkg_any_core_c0 /= topo.num_packages;
1276 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1277 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1278 }
1279
Len Brownc98d5d92012-06-04 00:56:40 -04001280 average.packages.pc2 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001281 if (DO_BIC(BIC_Pkgpc3))
Len Brownee7e38e2015-02-09 23:39:45 -05001282 average.packages.pc3 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001283 if (DO_BIC(BIC_Pkgpc6))
Len Brownee7e38e2015-02-09 23:39:45 -05001284 average.packages.pc6 /= topo.num_packages;
Len Brown0f47c082017-01-27 00:50:45 -05001285 if (DO_BIC(BIC_Pkgpc7))
Len Brownee7e38e2015-02-09 23:39:45 -05001286 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001287
1288 average.packages.pc8 /= topo.num_packages;
1289 average.packages.pc9 /= topo.num_packages;
1290 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001291
1292 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1293 if (mp->format == FORMAT_RAW)
1294 continue;
Len Brown41618e62017-02-09 18:25:22 -05001295 if (mp->flags & SYSFS_PERCPU && mp->type == COUNTER_ITEMS)
1296 continue;
Len Brown388e9c82016-12-22 23:57:55 -05001297 average.threads.counter[i] /= topo.num_cpus;
1298 }
1299 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1300 if (mp->format == FORMAT_RAW)
1301 continue;
1302 average.cores.counter[i] /= topo.num_cores;
1303 }
1304 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1305 if (mp->format == FORMAT_RAW)
1306 continue;
1307 average.packages.counter[i] /= topo.num_packages;
1308 }
Len Brownc98d5d92012-06-04 00:56:40 -04001309}
1310
1311static unsigned long long rdtsc(void)
1312{
1313 unsigned int low, high;
1314
1315 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1316
1317 return low | ((unsigned long long)high) << 32;
1318}
1319
Len Brownc98d5d92012-06-04 00:56:40 -04001320/*
Len Brown495c76542017-02-08 02:41:51 -05001321 * Open a file, and exit on failure
1322 */
1323FILE *fopen_or_die(const char *path, const char *mode)
1324{
1325 FILE *filep = fopen(path, mode);
1326
1327 if (!filep)
1328 err(1, "%s: open failed", path);
1329 return filep;
1330}
1331/*
1332 * snapshot_sysfs_counter()
1333 *
1334 * return snapshot of given counter
1335 */
1336unsigned long long snapshot_sysfs_counter(char *path)
1337{
1338 FILE *fp;
1339 int retval;
1340 unsigned long long counter;
1341
1342 fp = fopen_or_die(path, "r");
1343
1344 retval = fscanf(fp, "%lld", &counter);
1345 if (retval != 1)
1346 err(1, "snapshot_sysfs_counter(%s)", path);
1347
1348 fclose(fp);
1349
1350 return counter;
1351}
1352
1353int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1354{
1355 if (mp->msr_num != 0) {
1356 if (get_msr(cpu, mp->msr_num, counterp))
1357 return -1;
1358 } else {
Len Brown41618e62017-02-09 18:25:22 -05001359 char path[128];
1360
1361 if (mp->flags & SYSFS_PERCPU) {
1362 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1363 cpu, mp->path);
1364
1365 *counterp = snapshot_sysfs_counter(path);
1366 } else {
1367 *counterp = snapshot_sysfs_counter(mp->path);
1368 }
Len Brown495c76542017-02-08 02:41:51 -05001369 }
1370
1371 return 0;
1372}
1373
1374/*
Len Brownc98d5d92012-06-04 00:56:40 -04001375 * get_counters(...)
1376 * migrate to cpu
1377 * acquire and record local counters for that cpu
1378 */
1379int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1380{
1381 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001382 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001383 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001384 struct msr_counter *mp;
1385 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001386
Len Browne52966c2012-11-08 22:38:05 -05001387 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001388 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001389 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001390 }
Len Brownc98d5d92012-06-04 00:56:40 -04001391
Len Brown0102b062016-02-27 03:11:29 -05001392retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001393 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1394
Len Brown812db3f2017-02-10 00:25:41 -05001395 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001396 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1397
1398 /*
1399 * The TSC, APERF and MPERF must be read together for
1400 * APERF/MPERF and MPERF/TSC to give accurate results.
1401 *
1402 * Unfortunately, APERF and MPERF are read by
1403 * individual system call, so delays may occur
1404 * between them. If the time to read them
1405 * varies by a large amount, we re-read them.
1406 */
1407
1408 /*
1409 * This initial dummy APERF read has been seen to
1410 * reduce jitter in the subsequent reads.
1411 */
1412
Len Brown9c63a652012-10-31 01:29:52 -04001413 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001414 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001415
1416 t->tsc = rdtsc(); /* re-read close to APERF */
1417
1418 tsc_before = t->tsc;
1419
1420 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1421 return -3;
1422
1423 tsc_between = rdtsc();
1424
Len Brown9c63a652012-10-31 01:29:52 -04001425 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001426 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001427
1428 tsc_after = rdtsc();
1429
1430 aperf_time = tsc_between - tsc_before;
1431 mperf_time = tsc_after - tsc_between;
1432
1433 /*
1434 * If the system call latency to read APERF and MPERF
1435 * differ by more than 2x, then try again.
1436 */
1437 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1438 aperf_mperf_retry_count++;
1439 if (aperf_mperf_retry_count < 5)
1440 goto retry;
1441 else
1442 warnx("cpu%d jitter %lld %lld",
1443 cpu, aperf_time, mperf_time);
1444 }
1445 aperf_mperf_retry_count = 0;
1446
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001447 t->aperf = t->aperf * aperf_mperf_multiplier;
1448 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001449 }
1450
Len Brown812db3f2017-02-10 00:25:41 -05001451 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001452 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001453 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001454 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1455 return -5;
1456 t->smi_count = msr & 0xFFFFFFFF;
1457 }
Len Brown0539ba12017-02-10 00:27:20 -05001458 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001459 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1460 return -6;
1461 }
1462
Len Brown388e9c82016-12-22 23:57:55 -05001463 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001464 if (get_mp(cpu, mp, &t->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001465 return -10;
1466 }
1467
Len Brownc98d5d92012-06-04 00:56:40 -04001468 /* collect core counters only for 1st thread in core */
1469 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1470 return 0;
1471
Len Brown812db3f2017-02-10 00:25:41 -05001472 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001473 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1474 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001475 }
1476
Len Brown812db3f2017-02-10 00:25:41 -05001477 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001478 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1479 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001480 } else if (do_knl_cstates) {
1481 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1482 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001483 }
1484
Len Brown812db3f2017-02-10 00:25:41 -05001485 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001486 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1487 return -8;
1488
Len Brown0539ba12017-02-10 00:27:20 -05001489 if (DO_BIC(BIC_Mod_c6))
1490 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1491 return -8;
1492
Len Brown812db3f2017-02-10 00:25:41 -05001493 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001494 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1495 return -9;
1496 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1497 }
1498
Len Brown388e9c82016-12-22 23:57:55 -05001499 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001500 if (get_mp(cpu, mp, &c->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001501 return -10;
1502 }
Len Brown889facb2012-11-08 00:48:57 -05001503
Len Brownc98d5d92012-06-04 00:56:40 -04001504 /* collect package counters only for 1st core in package */
1505 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1506 return 0;
1507
Len Brown0b2bb692015-03-26 00:50:30 -04001508 if (do_skl_residency) {
1509 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1510 return -10;
1511 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1512 return -11;
1513 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1514 return -12;
1515 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1516 return -13;
1517 }
Len Brown0f47c082017-01-27 00:50:45 -05001518 if (DO_BIC(BIC_Pkgpc3))
Len Brownc98d5d92012-06-04 00:56:40 -04001519 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1520 return -9;
Len Brown0f47c082017-01-27 00:50:45 -05001521 if (DO_BIC(BIC_Pkgpc6)) {
Len Brown0539ba12017-02-10 00:27:20 -05001522 if (do_slm_cstates) {
1523 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1524 return -10;
1525 } else {
1526 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1527 return -10;
1528 }
1529 }
1530
Len Brown0f47c082017-01-27 00:50:45 -05001531 if (DO_BIC(BIC_Pkgpc2))
Len Brownc98d5d92012-06-04 00:56:40 -04001532 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1533 return -11;
Len Brown0f47c082017-01-27 00:50:45 -05001534 if (DO_BIC(BIC_Pkgpc7))
Len Brownc98d5d92012-06-04 00:56:40 -04001535 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1536 return -12;
Len Brown0f47c082017-01-27 00:50:45 -05001537 if (DO_BIC(BIC_Pkgpc8))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001538 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1539 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001540 if (DO_BIC(BIC_Pkgpc9))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001541 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1542 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001543 if (DO_BIC(BIC_Pkgpc10))
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001544 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1545 return -13;
Len Brown0f47c082017-01-27 00:50:45 -05001546
Len Brown889facb2012-11-08 00:48:57 -05001547 if (do_rapl & RAPL_PKG) {
1548 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1549 return -13;
1550 p->energy_pkg = msr & 0xFFFFFFFF;
1551 }
Jacob Pan91484942016-06-16 09:48:20 -07001552 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001553 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1554 return -14;
1555 p->energy_cores = msr & 0xFFFFFFFF;
1556 }
1557 if (do_rapl & RAPL_DRAM) {
1558 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1559 return -15;
1560 p->energy_dram = msr & 0xFFFFFFFF;
1561 }
1562 if (do_rapl & RAPL_GFX) {
1563 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1564 return -16;
1565 p->energy_gfx = msr & 0xFFFFFFFF;
1566 }
1567 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1568 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1569 return -16;
1570 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1571 }
1572 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1573 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1574 return -16;
1575 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1576 }
Len Brown812db3f2017-02-10 00:25:41 -05001577 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001578 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1579 return -17;
1580 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1581 }
Len Brownfdf676e2016-02-27 01:28:12 -05001582
Len Brown812db3f2017-02-10 00:25:41 -05001583 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001584 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1585
Len Brown812db3f2017-02-10 00:25:41 -05001586 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001587 p->gfx_mhz = gfx_cur_mhz;
1588
Len Brown388e9c82016-12-22 23:57:55 -05001589 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
Len Brown495c76542017-02-08 02:41:51 -05001590 if (get_mp(cpu, mp, &p->counter[i]))
Len Brown388e9c82016-12-22 23:57:55 -05001591 return -10;
1592 }
1593
Len Brown103a8fe2010-10-22 23:53:03 -04001594 return 0;
1595}
1596
Len Brownee7e38e2015-02-09 23:39:45 -05001597/*
1598 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1599 * If you change the values, note they are used both in comparisons
1600 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1601 */
1602
1603#define PCLUKN 0 /* Unknown */
1604#define PCLRSV 1 /* Reserved */
1605#define PCL__0 2 /* PC0 */
1606#define PCL__1 3 /* PC1 */
1607#define PCL__2 4 /* PC2 */
1608#define PCL__3 5 /* PC3 */
1609#define PCL__4 6 /* PC4 */
1610#define PCL__6 7 /* PC6 */
1611#define PCL_6N 8 /* PC6 No Retention */
1612#define PCL_6R 9 /* PC6 Retention */
1613#define PCL__7 10 /* PC7 */
1614#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001615#define PCL__8 12 /* PC8 */
1616#define PCL__9 13 /* PC9 */
1617#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001618
1619int pkg_cstate_limit = PCLUKN;
1620char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001621 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001622
Len Browne9257f52015-04-01 21:02:57 -04001623int 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};
1624int 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};
1625int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Brown0539ba12017-02-10 00:27:20 -05001626int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7};
Len Brownf2642882017-01-07 23:13:15 -05001627int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
Len Browne9257f52015-04-01 21:02:57 -04001628int 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 -04001629int 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 -05001630int 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 -05001631
Len Browna2b7b742015-09-26 00:12:38 -04001632
1633static void
1634calculate_tsc_tweak()
1635{
Len Browna2b7b742015-09-26 00:12:38 -04001636 tsc_tweak = base_hz / tsc_hz;
1637}
1638
Len Brownfcd17212015-03-23 20:29:09 -04001639static void
1640dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001641{
1642 unsigned long long msr;
1643 unsigned int ratio;
1644
Len Brownec0adc52015-11-12 02:42:31 -05001645 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001646
Len Brownb7d8c142016-02-13 23:36:17 -05001647 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001648
Len Brown103a8fe2010-10-22 23:53:03 -04001649 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001650 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001651 ratio, bclk, ratio * bclk);
1652
1653 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001654 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001655 ratio, bclk, ratio * bclk);
1656
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001657 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001658 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001659 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001660
Len Brownfcd17212015-03-23 20:29:09 -04001661 return;
1662}
1663
1664static void
1665dump_hsw_turbo_ratio_limits(void)
1666{
1667 unsigned long long msr;
1668 unsigned int ratio;
1669
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001670 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001671
Len Brownb7d8c142016-02-13 23:36:17 -05001672 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001673
1674 ratio = (msr >> 8) & 0xFF;
1675 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001676 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001677 ratio, bclk, ratio * bclk);
1678
1679 ratio = (msr >> 0) & 0xFF;
1680 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001681 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001682 ratio, bclk, ratio * bclk);
1683 return;
1684}
1685
1686static void
1687dump_ivt_turbo_ratio_limits(void)
1688{
1689 unsigned long long msr;
1690 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001691
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001692 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001693
Len Brownb7d8c142016-02-13 23:36:17 -05001694 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001695
1696 ratio = (msr >> 56) & 0xFF;
1697 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001698 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001699 ratio, bclk, ratio * bclk);
1700
1701 ratio = (msr >> 48) & 0xFF;
1702 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001703 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001704 ratio, bclk, ratio * bclk);
1705
1706 ratio = (msr >> 40) & 0xFF;
1707 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001708 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001709 ratio, bclk, ratio * bclk);
1710
1711 ratio = (msr >> 32) & 0xFF;
1712 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001713 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001714 ratio, bclk, ratio * bclk);
1715
1716 ratio = (msr >> 24) & 0xFF;
1717 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001718 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001719 ratio, bclk, ratio * bclk);
1720
1721 ratio = (msr >> 16) & 0xFF;
1722 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001723 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001724 ratio, bclk, ratio * bclk);
1725
1726 ratio = (msr >> 8) & 0xFF;
1727 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001728 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001729 ratio, bclk, ratio * bclk);
1730
1731 ratio = (msr >> 0) & 0xFF;
1732 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001733 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001734 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001735 return;
1736}
Len Brown31e07522017-01-31 23:07:49 -05001737int has_turbo_ratio_group_limits(int family, int model)
1738{
1739
1740 if (!genuine_intel)
1741 return 0;
1742
1743 switch (model) {
1744 case INTEL_FAM6_ATOM_GOLDMONT:
1745 case INTEL_FAM6_SKYLAKE_X:
1746 case INTEL_FAM6_ATOM_DENVERTON:
1747 return 1;
1748 }
1749 return 0;
1750}
Len Brown6574a5d2012-09-21 00:01:31 -04001751
Len Brownfcd17212015-03-23 20:29:09 -04001752static void
Len Brown31e07522017-01-31 23:07:49 -05001753dump_turbo_ratio_limits(int family, int model)
Len Brownfcd17212015-03-23 20:29:09 -04001754{
Len Brown31e07522017-01-31 23:07:49 -05001755 unsigned long long msr, core_counts;
1756 unsigned int ratio, group_size;
Len Brown103a8fe2010-10-22 23:53:03 -04001757
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001758 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001759 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001760
Len Brown31e07522017-01-31 23:07:49 -05001761 if (has_turbo_ratio_group_limits(family, model)) {
1762 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1763 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1764 } else {
1765 core_counts = 0x0807060504030201;
1766 }
1767
Len Brown6574a5d2012-09-21 00:01:31 -04001768 ratio = (msr >> 56) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001769 group_size = (core_counts >> 56) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001770 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001771 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1772 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001773
1774 ratio = (msr >> 48) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001775 group_size = (core_counts >> 48) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001776 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001777 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1778 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001779
1780 ratio = (msr >> 40) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001781 group_size = (core_counts >> 40) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001782 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001783 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1784 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001785
1786 ratio = (msr >> 32) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001787 group_size = (core_counts >> 32) & 0xFF;
Len Brown6574a5d2012-09-21 00:01:31 -04001788 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001789 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1790 ratio, bclk, ratio * bclk, group_size);
Len Brown6574a5d2012-09-21 00:01:31 -04001791
Len Brown103a8fe2010-10-22 23:53:03 -04001792 ratio = (msr >> 24) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001793 group_size = (core_counts >> 24) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001794 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001795 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1796 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001797
1798 ratio = (msr >> 16) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001799 group_size = (core_counts >> 16) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001800 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001801 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1802 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001803
1804 ratio = (msr >> 8) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001805 group_size = (core_counts >> 8) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001806 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001807 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1808 ratio, bclk, ratio * bclk, group_size);
Len Brown103a8fe2010-10-22 23:53:03 -04001809
1810 ratio = (msr >> 0) & 0xFF;
Len Brown31e07522017-01-31 23:07:49 -05001811 group_size = (core_counts >> 0) & 0xFF;
Len Brown103a8fe2010-10-22 23:53:03 -04001812 if (ratio)
Len Brown31e07522017-01-31 23:07:49 -05001813 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1814 ratio, bclk, ratio * bclk, group_size);
Len Brownfcd17212015-03-23 20:29:09 -04001815 return;
1816}
Len Brown3a9a9412014-08-15 02:39:52 -04001817
Len Brownfcd17212015-03-23 20:29:09 -04001818static void
Len Brown0f7887c2017-01-12 23:49:18 -05001819dump_atom_turbo_ratio_limits(void)
1820{
1821 unsigned long long msr;
1822 unsigned int ratio;
1823
1824 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1825 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1826
1827 ratio = (msr >> 0) & 0x3F;
1828 if (ratio)
1829 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1830 ratio, bclk, ratio * bclk);
1831
1832 ratio = (msr >> 8) & 0x3F;
1833 if (ratio)
1834 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1835 ratio, bclk, ratio * bclk);
1836
1837 ratio = (msr >> 16) & 0x3F;
1838 if (ratio)
1839 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1840 ratio, bclk, ratio * bclk);
1841
1842 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1843 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1844
1845 ratio = (msr >> 24) & 0x3F;
1846 if (ratio)
1847 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1848 ratio, bclk, ratio * bclk);
1849
1850 ratio = (msr >> 16) & 0x3F;
1851 if (ratio)
1852 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1853 ratio, bclk, ratio * bclk);
1854
1855 ratio = (msr >> 8) & 0x3F;
1856 if (ratio)
1857 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1858 ratio, bclk, ratio * bclk);
1859
1860 ratio = (msr >> 0) & 0x3F;
1861 if (ratio)
1862 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1863 ratio, bclk, ratio * bclk);
1864}
1865
1866static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001867dump_knl_turbo_ratio_limits(void)
1868{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001869 const unsigned int buckets_no = 7;
1870
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001871 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001872 int delta_cores, delta_ratio;
1873 int i, b_nr;
1874 unsigned int cores[buckets_no];
1875 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001876
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001877 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001878
Len Brownb7d8c142016-02-13 23:36:17 -05001879 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001880 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001881
1882 /**
1883 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001884 * [0] -- Reserved
1885 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001886 * [15:8] -- Base value of freq ratio of bucket 1.
1887 * [20:16] -- +ve delta of number of active cores of bucket 2.
1888 * i.e. active cores of bucket 2 =
1889 * active cores of bucket 1 + delta
1890 * [23:21] -- Negative delta of freq ratio of bucket 2.
1891 * i.e. freq ratio of bucket 2 =
1892 * freq ratio of bucket 1 - delta
1893 * [28:24]-- +ve delta of number of active cores of bucket 3.
1894 * [31:29]-- -ve delta of freq ratio of bucket 3.
1895 * [36:32]-- +ve delta of number of active cores of bucket 4.
1896 * [39:37]-- -ve delta of freq ratio of bucket 4.
1897 * [44:40]-- +ve delta of number of active cores of bucket 5.
1898 * [47:45]-- -ve delta of freq ratio of bucket 5.
1899 * [52:48]-- +ve delta of number of active cores of bucket 6.
1900 * [55:53]-- -ve delta of freq ratio of bucket 6.
1901 * [60:56]-- +ve delta of number of active cores of bucket 7.
1902 * [63:61]-- -ve delta of freq ratio of bucket 7.
1903 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001904
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001905 b_nr = 0;
1906 cores[b_nr] = (msr & 0xFF) >> 1;
1907 ratio[b_nr] = (msr >> 8) & 0xFF;
1908
1909 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001910 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001911 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001912
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001913 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1914 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1915 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001916 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001917
1918 for (i = buckets_no - 1; i >= 0; i--)
1919 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001920 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05001921 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001922 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001923}
1924
1925static void
Len Brownfcd17212015-03-23 20:29:09 -04001926dump_nhm_cst_cfg(void)
1927{
1928 unsigned long long msr;
1929
Len Brown1df2e552017-01-07 23:24:57 -05001930 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001931
1932#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1933#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1934
Len Brown1df2e552017-01-07 23:24:57 -05001935 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001936
Len Brownb7d8c142016-02-13 23:36:17 -05001937 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001938 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1939 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1940 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1941 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1942 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001943 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001944 pkg_cstate_limit_strings[pkg_cstate_limit]);
1945 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001946}
1947
Len Brown6fb31432015-06-17 16:23:45 -04001948static void
1949dump_config_tdp(void)
1950{
1951 unsigned long long msr;
1952
1953 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001954 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001955 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001956
1957 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001958 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001959 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001960 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1961 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1962 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1963 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001964 }
Len Brownb7d8c142016-02-13 23:36:17 -05001965 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001966
1967 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001968 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001969 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001970 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1971 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1972 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1973 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001974 }
Len Brownb7d8c142016-02-13 23:36:17 -05001975 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001976
1977 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001978 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001979 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001980 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1981 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1982 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001983
Len Brown6fb31432015-06-17 16:23:45 -04001984 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001985 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001986 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001987 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1988 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001989}
Len Brown5a634262016-04-06 17:15:55 -04001990
1991unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1992
1993void print_irtl(void)
1994{
1995 unsigned long long msr;
1996
1997 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1998 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1999 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2000 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2001
2002 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2003 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2004 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2005 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2006
2007 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2008 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2009 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2010 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2011
2012 if (!do_irtl_hsw)
2013 return;
2014
2015 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2016 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2017 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2018 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2019
2020 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2021 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2022 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2023 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2024
2025 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2026 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2027 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2028 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2029
2030}
Len Brown36229892016-02-26 20:51:02 -05002031void free_fd_percpu(void)
2032{
2033 int i;
2034
Mika Westerberg01a67ad2016-04-22 11:13:23 +03002035 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05002036 if (fd_percpu[i] != 0)
2037 close(fd_percpu[i]);
2038 }
2039
2040 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04002041}
2042
Len Brownc98d5d92012-06-04 00:56:40 -04002043void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04002044{
Len Brownc98d5d92012-06-04 00:56:40 -04002045 CPU_FREE(cpu_present_set);
2046 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05002047 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002048
Len Brownc98d5d92012-06-04 00:56:40 -04002049 CPU_FREE(cpu_affinity_set);
2050 cpu_affinity_set = NULL;
2051 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002052
Len Brownc98d5d92012-06-04 00:56:40 -04002053 free(thread_even);
2054 free(core_even);
2055 free(package_even);
2056
2057 thread_even = NULL;
2058 core_even = NULL;
2059 package_even = NULL;
2060
2061 free(thread_odd);
2062 free(core_odd);
2063 free(package_odd);
2064
2065 thread_odd = NULL;
2066 core_odd = NULL;
2067 package_odd = NULL;
2068
2069 free(output_buffer);
2070 output_buffer = NULL;
2071 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05002072
2073 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05002074
2075 free(irq_column_2_cpu);
2076 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002077}
2078
Josh Triplett57a42a32013-08-20 17:20:17 -07002079
2080/*
Josh Triplett95aebc42013-08-20 17:20:16 -07002081 * Parse a file containing a single int.
2082 */
2083int parse_int_file(const char *fmt, ...)
2084{
2085 va_list args;
2086 char path[PATH_MAX];
2087 FILE *filep;
2088 int value;
2089
2090 va_start(args, fmt);
2091 vsnprintf(path, sizeof(path), fmt, args);
2092 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002093 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002094 if (fscanf(filep, "%d", &value) != 1)
2095 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002096 fclose(filep);
2097 return value;
2098}
2099
2100/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002101 * get_cpu_position_in_core(cpu)
2102 * return the position of the CPU among its HT siblings in the core
2103 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002104 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002105int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002106{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002107 char path[64];
2108 FILE *filep;
2109 int this_cpu;
2110 char character;
2111 int i;
2112
2113 sprintf(path,
2114 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2115 cpu);
2116 filep = fopen(path, "r");
2117 if (filep == NULL) {
2118 perror(path);
2119 exit(1);
2120 }
2121
2122 for (i = 0; i < topo.num_threads_per_core; i++) {
2123 fscanf(filep, "%d", &this_cpu);
2124 if (this_cpu == cpu) {
2125 fclose(filep);
2126 return i;
2127 }
2128
2129 /* Account for no separator after last thread*/
2130 if (i != (topo.num_threads_per_core - 1))
2131 fscanf(filep, "%c", &character);
2132 }
2133
2134 fclose(filep);
2135 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002136}
2137
Len Brownc98d5d92012-06-04 00:56:40 -04002138/*
2139 * cpu_is_first_core_in_package(cpu)
2140 * return 1 if given CPU is 1st core in package
2141 */
2142int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002143{
Josh Triplett95aebc42013-08-20 17:20:16 -07002144 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002145}
2146
2147int get_physical_package_id(int cpu)
2148{
Josh Triplett95aebc42013-08-20 17:20:16 -07002149 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002150}
2151
2152int get_core_id(int cpu)
2153{
Josh Triplett95aebc42013-08-20 17:20:16 -07002154 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002155}
2156
Len Brownc98d5d92012-06-04 00:56:40 -04002157int get_num_ht_siblings(int cpu)
2158{
2159 char path[80];
2160 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002161 int sib1;
2162 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002163 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002164 char str[100];
2165 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002166
2167 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002168 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002169
Len Brownc98d5d92012-06-04 00:56:40 -04002170 /*
2171 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002172 * A ',' separated or '-' separated set of numbers
2173 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002174 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002175 fscanf(filep, "%d%c\n", &sib1, &character);
2176 fseek(filep, 0, SEEK_SET);
2177 fgets(str, 100, filep);
2178 ch = strchr(str, character);
2179 while (ch != NULL) {
2180 matches++;
2181 ch = strchr(ch+1, character);
2182 }
Len Brownc98d5d92012-06-04 00:56:40 -04002183
2184 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002185 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002186}
2187
Len Brown103a8fe2010-10-22 23:53:03 -04002188/*
Len Brownc98d5d92012-06-04 00:56:40 -04002189 * run func(thread, core, package) in topology order
2190 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002191 */
2192
Len Brownc98d5d92012-06-04 00:56:40 -04002193int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2194 struct pkg_data *, struct thread_data *, struct core_data *,
2195 struct pkg_data *), struct thread_data *thread_base,
2196 struct core_data *core_base, struct pkg_data *pkg_base,
2197 struct thread_data *thread_base2, struct core_data *core_base2,
2198 struct pkg_data *pkg_base2)
2199{
2200 int retval, pkg_no, core_no, thread_no;
2201
2202 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2203 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2204 for (thread_no = 0; thread_no <
2205 topo.num_threads_per_core; ++thread_no) {
2206 struct thread_data *t, *t2;
2207 struct core_data *c, *c2;
2208 struct pkg_data *p, *p2;
2209
2210 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2211
2212 if (cpu_is_not_present(t->cpu_id))
2213 continue;
2214
2215 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2216
2217 c = GET_CORE(core_base, core_no, pkg_no);
2218 c2 = GET_CORE(core_base2, core_no, pkg_no);
2219
2220 p = GET_PKG(pkg_base, pkg_no);
2221 p2 = GET_PKG(pkg_base2, pkg_no);
2222
2223 retval = func(t, c, p, t2, c2, p2);
2224 if (retval)
2225 return retval;
2226 }
2227 }
2228 }
2229 return 0;
2230}
2231
2232/*
2233 * run func(cpu) on every cpu in /proc/stat
2234 * return max_cpu number
2235 */
2236int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002237{
2238 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002239 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002240 int retval;
2241
Josh Triplett57a42a32013-08-20 17:20:17 -07002242 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002243
2244 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002245 if (retval != 0)
2246 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002247
Len Brownc98d5d92012-06-04 00:56:40 -04002248 while (1) {
2249 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 -04002250 if (retval != 1)
2251 break;
2252
Len Brownc98d5d92012-06-04 00:56:40 -04002253 retval = func(cpu_num);
2254 if (retval) {
2255 fclose(fp);
2256 return(retval);
2257 }
Len Brown103a8fe2010-10-22 23:53:03 -04002258 }
2259 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002260 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002261}
2262
2263void re_initialize(void)
2264{
Len Brownc98d5d92012-06-04 00:56:40 -04002265 free_all_buffers();
2266 setup_all_buffers();
2267 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002268}
2269
Len Brownc98d5d92012-06-04 00:56:40 -04002270
Len Brown103a8fe2010-10-22 23:53:03 -04002271/*
Len Brownc98d5d92012-06-04 00:56:40 -04002272 * count_cpus()
2273 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002274 */
Len Brownc98d5d92012-06-04 00:56:40 -04002275int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002276{
Len Brownc98d5d92012-06-04 00:56:40 -04002277 if (topo.max_cpu_num < cpu)
2278 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002279
Len Brownc98d5d92012-06-04 00:56:40 -04002280 topo.num_cpus += 1;
2281 return 0;
2282}
2283int mark_cpu_present(int cpu)
2284{
2285 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002286 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002287}
2288
Len Brown562a2d32016-02-26 23:48:05 -05002289/*
2290 * snapshot_proc_interrupts()
2291 *
2292 * read and record summary of /proc/interrupts
2293 *
2294 * return 1 if config change requires a restart, else return 0
2295 */
2296int snapshot_proc_interrupts(void)
2297{
2298 static FILE *fp;
2299 int column, retval;
2300
2301 if (fp == NULL)
2302 fp = fopen_or_die("/proc/interrupts", "r");
2303 else
2304 rewind(fp);
2305
2306 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2307 for (column = 0; column < topo.num_cpus; ++column) {
2308 int cpu_number;
2309
2310 retval = fscanf(fp, " CPU%d", &cpu_number);
2311 if (retval != 1)
2312 break;
2313
2314 if (cpu_number > topo.max_cpu_num) {
2315 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2316 return 1;
2317 }
2318
2319 irq_column_2_cpu[column] = cpu_number;
2320 irqs_per_cpu[cpu_number] = 0;
2321 }
2322
2323 /* read /proc/interrupt count lines and sum up irqs per cpu */
2324 while (1) {
2325 int column;
2326 char buf[64];
2327
2328 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2329 if (retval != 1)
2330 break;
2331
2332 /* read the count per cpu */
2333 for (column = 0; column < topo.num_cpus; ++column) {
2334
2335 int cpu_number, irq_count;
2336
2337 retval = fscanf(fp, " %d", &irq_count);
2338 if (retval != 1)
2339 break;
2340
2341 cpu_number = irq_column_2_cpu[column];
2342 irqs_per_cpu[cpu_number] += irq_count;
2343
2344 }
2345
2346 while (getc(fp) != '\n')
2347 ; /* flush interrupt description */
2348
2349 }
2350 return 0;
2351}
Len Brown27d47352016-02-27 00:37:54 -05002352/*
Len Brownfdf676e2016-02-27 01:28:12 -05002353 * snapshot_gfx_rc6_ms()
2354 *
2355 * record snapshot of
2356 * /sys/class/drm/card0/power/rc6_residency_ms
2357 *
2358 * return 1 if config change requires a restart, else return 0
2359 */
2360int snapshot_gfx_rc6_ms(void)
2361{
2362 FILE *fp;
2363 int retval;
2364
2365 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2366
2367 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2368 if (retval != 1)
2369 err(1, "GFX rc6");
2370
2371 fclose(fp);
2372
2373 return 0;
2374}
2375/*
Len Brown27d47352016-02-27 00:37:54 -05002376 * snapshot_gfx_mhz()
2377 *
2378 * record snapshot of
2379 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2380 *
2381 * return 1 if config change requires a restart, else return 0
2382 */
2383int snapshot_gfx_mhz(void)
2384{
2385 static FILE *fp;
2386 int retval;
2387
2388 if (fp == NULL)
2389 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2390 else
2391 rewind(fp);
2392
2393 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2394 if (retval != 1)
2395 err(1, "GFX MHz");
2396
2397 return 0;
2398}
Len Brown562a2d32016-02-26 23:48:05 -05002399
2400/*
2401 * snapshot /proc and /sys files
2402 *
2403 * return 1 if configuration restart needed, else return 0
2404 */
2405int snapshot_proc_sysfs_files(void)
2406{
2407 if (snapshot_proc_interrupts())
2408 return 1;
2409
Len Brown812db3f2017-02-10 00:25:41 -05002410 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002411 snapshot_gfx_rc6_ms();
2412
Len Brown812db3f2017-02-10 00:25:41 -05002413 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002414 snapshot_gfx_mhz();
2415
Len Brown562a2d32016-02-26 23:48:05 -05002416 return 0;
2417}
2418
Len Brown103a8fe2010-10-22 23:53:03 -04002419void turbostat_loop()
2420{
Len Brownc98d5d92012-06-04 00:56:40 -04002421 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002422 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002423
Len Brown103a8fe2010-10-22 23:53:03 -04002424restart:
Len Browne52966c2012-11-08 22:38:05 -05002425 restarted++;
2426
Len Brown562a2d32016-02-26 23:48:05 -05002427 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002428 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002429 if (retval < -1) {
2430 exit(retval);
2431 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002432 if (restarted > 1) {
2433 exit(retval);
2434 }
Len Brownc98d5d92012-06-04 00:56:40 -04002435 re_initialize();
2436 goto restart;
2437 }
Len Browne52966c2012-11-08 22:38:05 -05002438 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002439 gettimeofday(&tv_even, (struct timezone *)NULL);
2440
2441 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002442 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002443 re_initialize();
2444 goto restart;
2445 }
Len Brown2a0609c2016-02-12 22:44:48 -05002446 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002447 if (snapshot_proc_sysfs_files())
2448 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002449 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002450 if (retval < -1) {
2451 exit(retval);
2452 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002453 re_initialize();
2454 goto restart;
2455 }
Len Brown103a8fe2010-10-22 23:53:03 -04002456 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002457 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002458 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2459 re_initialize();
2460 goto restart;
2461 }
Len Brownc98d5d92012-06-04 00:56:40 -04002462 compute_average(EVEN_COUNTERS);
2463 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002464 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002465 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002466 if (snapshot_proc_sysfs_files())
2467 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002468 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002469 if (retval < -1) {
2470 exit(retval);
2471 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002472 re_initialize();
2473 goto restart;
2474 }
Len Brown103a8fe2010-10-22 23:53:03 -04002475 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002476 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002477 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2478 re_initialize();
2479 goto restart;
2480 }
Len Brownc98d5d92012-06-04 00:56:40 -04002481 compute_average(ODD_COUNTERS);
2482 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002483 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002484 }
2485}
2486
2487void check_dev_msr()
2488{
2489 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002490 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002491
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002492 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2493 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002494 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2495 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002496}
2497
Len Brown98481e72014-08-15 00:36:50 -04002498void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002499{
Len Brown98481e72014-08-15 00:36:50 -04002500 struct __user_cap_header_struct cap_header_data;
2501 cap_user_header_t cap_header = &cap_header_data;
2502 struct __user_cap_data_struct cap_data_data;
2503 cap_user_data_t cap_data = &cap_data_data;
2504 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2505 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002506 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002507
2508 /* check for CAP_SYS_RAWIO */
2509 cap_header->pid = getpid();
2510 cap_header->version = _LINUX_CAPABILITY_VERSION;
2511 if (capget(cap_header, cap_data) < 0)
2512 err(-6, "capget(2) failed");
2513
2514 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2515 do_exit++;
2516 warnx("capget(CAP_SYS_RAWIO) failed,"
2517 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2518 }
2519
2520 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002521 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2522 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002523 do_exit++;
2524 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2525 }
2526
2527 /* if all else fails, thell them to be root */
2528 if (do_exit)
2529 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002530 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002531
2532 if (do_exit)
2533 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002534}
2535
Len Brownd7899442015-01-23 00:12:33 -05002536/*
2537 * NHM adds support for additional MSRs:
2538 *
2539 * MSR_SMI_COUNT 0x00000034
2540 *
Len Brownec0adc52015-11-12 02:42:31 -05002541 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002542 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002543 *
Len Browncf4cbe52017-01-01 13:08:33 -05002544 * MSR_MISC_PWR_MGMT 0x000001aa
2545 *
Len Brownd7899442015-01-23 00:12:33 -05002546 * MSR_PKG_C3_RESIDENCY 0x000003f8
2547 * MSR_PKG_C6_RESIDENCY 0x000003f9
2548 * MSR_CORE_C3_RESIDENCY 0x000003fc
2549 * MSR_CORE_C6_RESIDENCY 0x000003fd
2550 *
Len Brownee7e38e2015-02-09 23:39:45 -05002551 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002552 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brown33148d62017-01-21 01:26:16 -05002553 * sets has_misc_feature_control
Len Brownd7899442015-01-23 00:12:33 -05002554 */
Len Brownee7e38e2015-02-09 23:39:45 -05002555int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002556{
Len Brownee7e38e2015-02-09 23:39:45 -05002557 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002558 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002559 int *pkg_cstate_limits;
2560
Len Brown103a8fe2010-10-22 23:53:03 -04002561 if (!genuine_intel)
2562 return 0;
2563
2564 if (family != 6)
2565 return 0;
2566
Len Brown21ed5572015-10-19 22:37:40 -04002567 bclk = discover_bclk(family, model);
2568
Len Brown103a8fe2010-10-22 23:53:03 -04002569 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002570 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2571 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002572 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002573 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2574 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2575 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2576 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002577 pkg_cstate_limits = nhm_pkg_cstate_limits;
2578 break;
Len Brown869ce692016-06-16 23:22:37 -04002579 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2580 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2581 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2582 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002583 pkg_cstate_limits = snb_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002584 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002585 break;
Len Brown869ce692016-06-16 23:22:37 -04002586 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2587 case INTEL_FAM6_HASWELL_X: /* HSX */
2588 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2589 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2590 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2591 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2592 case INTEL_FAM6_BROADWELL_X: /* BDX */
2593 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2594 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2595 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2596 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2597 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002598 pkg_cstate_limits = hsw_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002599 has_misc_feature_control = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002600 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002601 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2602 pkg_cstate_limits = skx_pkg_cstate_limits;
Len Brown33148d62017-01-21 01:26:16 -05002603 has_misc_feature_control = 1;
Len Brownd8ebb442016-12-01 20:27:46 -05002604 break;
Len Brown869ce692016-06-16 23:22:37 -04002605 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002606 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002607 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002608 pkg_cstate_limits = slv_pkg_cstate_limits;
2609 break;
Len Brown869ce692016-06-16 23:22:37 -04002610 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002611 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002612 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002613 break;
Len Brown869ce692016-06-16 23:22:37 -04002614 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002615 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002616 pkg_cstate_limits = phi_pkg_cstate_limits;
2617 break;
Len Brown869ce692016-06-16 23:22:37 -04002618 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05002619 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Brown869ce692016-06-16 23:22:37 -04002620 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002621 pkg_cstate_limits = bxt_pkg_cstate_limits;
2622 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002623 default:
2624 return 0;
2625 }
Len Brown1df2e552017-01-07 23:24:57 -05002626 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002627 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002628
Len Brownec0adc52015-11-12 02:42:31 -05002629 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002630 base_ratio = (msr >> 8) & 0xFF;
2631
2632 base_hz = base_ratio * bclk * 1000000;
2633 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002634 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002635}
Len Brown0f7887c2017-01-12 23:49:18 -05002636/*
Len Brown495c76542017-02-08 02:41:51 -05002637 * SLV client has support for unique MSRs:
Len Brown0f7887c2017-01-12 23:49:18 -05002638 *
2639 * MSR_CC6_DEMOTION_POLICY_CONFIG
2640 * MSR_MC6_DEMOTION_POLICY_CONFIG
2641 */
2642
2643int has_slv_msrs(unsigned int family, unsigned int model)
2644{
2645 if (!genuine_intel)
2646 return 0;
2647
2648 switch (model) {
2649 case INTEL_FAM6_ATOM_SILVERMONT1:
2650 case INTEL_FAM6_ATOM_MERRIFIELD:
2651 case INTEL_FAM6_ATOM_MOOREFIELD:
2652 return 1;
2653 }
2654 return 0;
2655}
Len Brown7170a372017-01-27 02:13:27 -05002656int is_dnv(unsigned int family, unsigned int model)
2657{
2658
2659 if (!genuine_intel)
2660 return 0;
2661
2662 switch (model) {
2663 case INTEL_FAM6_ATOM_DENVERTON:
2664 return 1;
2665 }
2666 return 0;
2667}
Len Brownade0eba2017-02-10 01:56:47 -05002668int is_bdx(unsigned int family, unsigned int model)
2669{
2670
2671 if (!genuine_intel)
2672 return 0;
2673
2674 switch (model) {
2675 case INTEL_FAM6_BROADWELL_X:
2676 case INTEL_FAM6_BROADWELL_XEON_D:
2677 return 1;
2678 }
2679 return 0;
2680}
Len Brown34c761972017-01-27 02:36:41 -05002681int is_skx(unsigned int family, unsigned int model)
2682{
2683
2684 if (!genuine_intel)
2685 return 0;
2686
2687 switch (model) {
2688 case INTEL_FAM6_SKYLAKE_X:
2689 return 1;
2690 }
2691 return 0;
2692}
Len Brown0f7887c2017-01-12 23:49:18 -05002693
Len Brown31e07522017-01-31 23:07:49 -05002694int has_turbo_ratio_limit(unsigned int family, unsigned int model)
Len Brownd7899442015-01-23 00:12:33 -05002695{
Len Brown0f7887c2017-01-12 23:49:18 -05002696 if (has_slv_msrs(family, model))
2697 return 0;
2698
Len Brownd7899442015-01-23 00:12:33 -05002699 switch (model) {
2700 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002701 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2702 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2703 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002704 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002705 return 0;
2706 default:
2707 return 1;
2708 }
2709}
Len Brown0f7887c2017-01-12 23:49:18 -05002710int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2711{
2712 if (has_slv_msrs(family, model))
2713 return 1;
2714
2715 return 0;
2716}
Len Brown6574a5d2012-09-21 00:01:31 -04002717int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2718{
2719 if (!genuine_intel)
2720 return 0;
2721
2722 if (family != 6)
2723 return 0;
2724
2725 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002726 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2727 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002728 return 1;
2729 default:
2730 return 0;
2731 }
2732}
Len Brownfcd17212015-03-23 20:29:09 -04002733int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2734{
2735 if (!genuine_intel)
2736 return 0;
2737
2738 if (family != 6)
2739 return 0;
2740
2741 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002742 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002743 return 1;
2744 default:
2745 return 0;
2746 }
2747}
2748
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002749int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2750{
2751 if (!genuine_intel)
2752 return 0;
2753
2754 if (family != 6)
2755 return 0;
2756
2757 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002758 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002759 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002760 return 1;
2761 default:
2762 return 0;
2763 }
2764}
Len Brown31e07522017-01-31 23:07:49 -05002765int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
2766{
2767 if (!genuine_intel)
2768 return 0;
2769
2770 if (family != 6)
2771 return 0;
2772
2773 switch (model) {
2774 case INTEL_FAM6_ATOM_GOLDMONT:
2775 case INTEL_FAM6_SKYLAKE_X:
2776 return 1;
2777 default:
2778 return 0;
2779 }
2780}
Len Brown6fb31432015-06-17 16:23:45 -04002781int has_config_tdp(unsigned int family, unsigned int model)
2782{
2783 if (!genuine_intel)
2784 return 0;
2785
2786 if (family != 6)
2787 return 0;
2788
2789 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002790 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2791 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2792 case INTEL_FAM6_HASWELL_X: /* HSX */
2793 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2794 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2795 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2796 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2797 case INTEL_FAM6_BROADWELL_X: /* BDX */
2798 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2799 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2800 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2801 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2802 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2803 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002804
Len Brown869ce692016-06-16 23:22:37 -04002805 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002806 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002807 return 1;
2808 default:
2809 return 0;
2810 }
2811}
2812
Len Brownfcd17212015-03-23 20:29:09 -04002813static void
Colin Ian King1b693172016-03-02 13:50:25 +00002814dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002815{
2816 if (!do_nhm_platform_info)
2817 return;
2818
2819 dump_nhm_platform_info();
2820
2821 if (has_hsw_turbo_ratio_limit(family, model))
2822 dump_hsw_turbo_ratio_limits();
2823
2824 if (has_ivt_turbo_ratio_limit(family, model))
2825 dump_ivt_turbo_ratio_limits();
2826
Len Brown31e07522017-01-31 23:07:49 -05002827 if (has_turbo_ratio_limit(family, model))
2828 dump_turbo_ratio_limits(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04002829
Len Brown0f7887c2017-01-12 23:49:18 -05002830 if (has_atom_turbo_ratio_limit(family, model))
2831 dump_atom_turbo_ratio_limits();
2832
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002833 if (has_knl_turbo_ratio_limit(family, model))
2834 dump_knl_turbo_ratio_limits();
2835
Len Brown6fb31432015-06-17 16:23:45 -04002836 if (has_config_tdp(family, model))
2837 dump_config_tdp();
2838
Len Brownfcd17212015-03-23 20:29:09 -04002839 dump_nhm_cst_cfg();
2840}
2841
Len Brown41618e62017-02-09 18:25:22 -05002842static void
2843dump_sysfs_cstate_config(void)
2844{
2845 char path[64];
2846 char name_buf[16];
2847 char desc[64];
2848 FILE *input;
2849 int state;
2850 char *sp;
2851
2852 if (!DO_BIC(BIC_sysfs))
2853 return;
2854
2855 for (state = 0; state < 10; ++state) {
2856
2857 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
2858 base_cpu, state);
2859 input = fopen(path, "r");
2860 if (input == NULL)
2861 continue;
2862 fgets(name_buf, sizeof(name_buf), input);
2863
2864 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
2865 sp = strchr(name_buf, '-');
2866 if (!sp)
2867 sp = strchrnul(name_buf, '\n');
2868 *sp = '\0';
2869
2870 fclose(input);
2871
2872 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
2873 base_cpu, state);
2874 input = fopen(path, "r");
2875 if (input == NULL)
2876 continue;
2877 fgets(desc, sizeof(desc), input);
2878
2879 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
2880 fclose(input);
2881 }
2882}
2883
Len Brown6574a5d2012-09-21 00:01:31 -04002884
Len Brown889facb2012-11-08 00:48:57 -05002885/*
2886 * print_epb()
2887 * Decode the ENERGY_PERF_BIAS MSR
2888 */
2889int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2890{
2891 unsigned long long msr;
2892 char *epb_string;
2893 int cpu;
2894
2895 if (!has_epb)
2896 return 0;
2897
2898 cpu = t->cpu_id;
2899
2900 /* EPB is per-package */
2901 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2902 return 0;
2903
2904 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002905 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002906 return -1;
2907 }
2908
2909 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2910 return 0;
2911
Len Browne9be7dd2015-05-26 12:19:37 -04002912 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002913 case ENERGY_PERF_BIAS_PERFORMANCE:
2914 epb_string = "performance";
2915 break;
2916 case ENERGY_PERF_BIAS_NORMAL:
2917 epb_string = "balanced";
2918 break;
2919 case ENERGY_PERF_BIAS_POWERSAVE:
2920 epb_string = "powersave";
2921 break;
2922 default:
2923 epb_string = "custom";
2924 break;
2925 }
Len Brownb7d8c142016-02-13 23:36:17 -05002926 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002927
2928 return 0;
2929}
Len Brown7f5c2582015-12-01 01:36:39 -05002930/*
2931 * print_hwp()
2932 * Decode the MSR_HWP_CAPABILITIES
2933 */
2934int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2935{
2936 unsigned long long msr;
2937 int cpu;
2938
2939 if (!has_hwp)
2940 return 0;
2941
2942 cpu = t->cpu_id;
2943
2944 /* MSR_HWP_CAPABILITIES is per-package */
2945 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2946 return 0;
2947
2948 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002949 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002950 return -1;
2951 }
2952
2953 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2954 return 0;
2955
Len Brownb7d8c142016-02-13 23:36:17 -05002956 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002957 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2958
2959 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2960 if ((msr & (1 << 0)) == 0)
2961 return 0;
2962
2963 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2964 return 0;
2965
Len Brownb7d8c142016-02-13 23:36:17 -05002966 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002967 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2968 cpu, msr,
2969 (unsigned int)HWP_HIGHEST_PERF(msr),
2970 (unsigned int)HWP_GUARANTEED_PERF(msr),
2971 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2972 (unsigned int)HWP_LOWEST_PERF(msr));
2973
2974 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2975 return 0;
2976
Len Brownb7d8c142016-02-13 23:36:17 -05002977 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002978 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2979 cpu, msr,
2980 (unsigned int)(((msr) >> 0) & 0xff),
2981 (unsigned int)(((msr) >> 8) & 0xff),
2982 (unsigned int)(((msr) >> 16) & 0xff),
2983 (unsigned int)(((msr) >> 24) & 0xff),
2984 (unsigned int)(((msr) >> 32) & 0xff3),
2985 (unsigned int)(((msr) >> 42) & 0x1));
2986
2987 if (has_hwp_pkg) {
2988 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2989 return 0;
2990
Len Brownb7d8c142016-02-13 23:36:17 -05002991 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002992 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2993 cpu, msr,
2994 (unsigned int)(((msr) >> 0) & 0xff),
2995 (unsigned int)(((msr) >> 8) & 0xff),
2996 (unsigned int)(((msr) >> 16) & 0xff),
2997 (unsigned int)(((msr) >> 24) & 0xff),
2998 (unsigned int)(((msr) >> 32) & 0xff3));
2999 }
3000 if (has_hwp_notify) {
3001 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3002 return 0;
3003
Len Brownb7d8c142016-02-13 23:36:17 -05003004 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003005 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3006 cpu, msr,
3007 ((msr) & 0x1) ? "EN" : "Dis",
3008 ((msr) & 0x2) ? "EN" : "Dis");
3009 }
3010 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3011 return 0;
3012
Len Brownb7d8c142016-02-13 23:36:17 -05003013 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05003014 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3015 cpu, msr,
3016 ((msr) & 0x1) ? "" : "No-",
3017 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05003018
3019 return 0;
3020}
3021
Len Brown3a9a9412014-08-15 02:39:52 -04003022/*
3023 * print_perf_limit()
3024 */
3025int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3026{
3027 unsigned long long msr;
3028 int cpu;
3029
3030 cpu = t->cpu_id;
3031
3032 /* per-package */
3033 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3034 return 0;
3035
3036 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003037 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04003038 return -1;
3039 }
3040
3041 if (do_core_perf_limit_reasons) {
3042 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003043 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3044 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04003045 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003046 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003047 (msr & 1 << 13) ? "Transitions, " : "",
3048 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3049 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3050 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3051 (msr & 1 << 9) ? "CorePwr, " : "",
3052 (msr & 1 << 8) ? "Amps, " : "",
3053 (msr & 1 << 6) ? "VR-Therm, " : "",
3054 (msr & 1 << 5) ? "Auto-HWP, " : "",
3055 (msr & 1 << 4) ? "Graphics, " : "",
3056 (msr & 1 << 2) ? "bit2, " : "",
3057 (msr & 1 << 1) ? "ThermStatus, " : "",
3058 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003059 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 -04003060 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04003061 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04003062 (msr & 1 << 29) ? "Transitions, " : "",
3063 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3064 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3065 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3066 (msr & 1 << 25) ? "CorePwr, " : "",
3067 (msr & 1 << 24) ? "Amps, " : "",
3068 (msr & 1 << 22) ? "VR-Therm, " : "",
3069 (msr & 1 << 21) ? "Auto-HWP, " : "",
3070 (msr & 1 << 20) ? "Graphics, " : "",
3071 (msr & 1 << 18) ? "bit18, " : "",
3072 (msr & 1 << 17) ? "ThermStatus, " : "",
3073 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04003074
3075 }
3076 if (do_gfx_perf_limit_reasons) {
3077 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003078 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3079 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003080 (msr & 1 << 0) ? "PROCHOT, " : "",
3081 (msr & 1 << 1) ? "ThermStatus, " : "",
3082 (msr & 1 << 4) ? "Graphics, " : "",
3083 (msr & 1 << 6) ? "VR-Therm, " : "",
3084 (msr & 1 << 8) ? "Amps, " : "",
3085 (msr & 1 << 9) ? "GFXPwr, " : "",
3086 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3087 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003088 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003089 (msr & 1 << 16) ? "PROCHOT, " : "",
3090 (msr & 1 << 17) ? "ThermStatus, " : "",
3091 (msr & 1 << 20) ? "Graphics, " : "",
3092 (msr & 1 << 22) ? "VR-Therm, " : "",
3093 (msr & 1 << 24) ? "Amps, " : "",
3094 (msr & 1 << 25) ? "GFXPwr, " : "",
3095 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3096 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3097 }
3098 if (do_ring_perf_limit_reasons) {
3099 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05003100 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3101 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04003102 (msr & 1 << 0) ? "PROCHOT, " : "",
3103 (msr & 1 << 1) ? "ThermStatus, " : "",
3104 (msr & 1 << 6) ? "VR-Therm, " : "",
3105 (msr & 1 << 8) ? "Amps, " : "",
3106 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3107 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05003108 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04003109 (msr & 1 << 16) ? "PROCHOT, " : "",
3110 (msr & 1 << 17) ? "ThermStatus, " : "",
3111 (msr & 1 << 22) ? "VR-Therm, " : "",
3112 (msr & 1 << 24) ? "Amps, " : "",
3113 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3114 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3115 }
3116 return 0;
3117}
3118
Len Brown889facb2012-11-08 00:48:57 -05003119#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3120#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3121
Colin Ian King1b693172016-03-02 13:50:25 +00003122double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05003123{
3124 unsigned long long msr;
3125
3126 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003127 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05003128 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3129
3130 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003131 case INTEL_FAM6_ATOM_SILVERMONT1:
3132 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05003133 return 30.0;
3134 default:
3135 return 135.0;
3136 }
3137}
3138
Andrey Semin40ee8e32014-12-05 00:07:00 -05003139/*
3140 * rapl_dram_energy_units_probe()
3141 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3142 */
3143static double
3144rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3145{
3146 /* only called for genuine_intel, family 6 */
3147
3148 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003149 case INTEL_FAM6_HASWELL_X: /* HSX */
3150 case INTEL_FAM6_BROADWELL_X: /* BDX */
3151 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3152 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003153 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05003154 return (rapl_dram_energy_units = 15.3 / 1000000);
3155 default:
3156 return (rapl_energy_units);
3157 }
3158}
3159
Len Brown144b44b2013-11-09 00:30:16 -05003160
Len Brown889facb2012-11-08 00:48:57 -05003161/*
3162 * rapl_probe()
3163 *
Len Brown144b44b2013-11-09 00:30:16 -05003164 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05003165 */
3166void rapl_probe(unsigned int family, unsigned int model)
3167{
3168 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05003169 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05003170 double tdp;
3171
3172 if (!genuine_intel)
3173 return;
3174
3175 if (family != 6)
3176 return;
3177
3178 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003179 case INTEL_FAM6_SANDYBRIDGE:
3180 case INTEL_FAM6_IVYBRIDGE:
3181 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3182 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3183 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3184 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3185 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003186 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003187 if (rapl_joules) {
3188 BIC_PRESENT(BIC_Pkg_J);
3189 BIC_PRESENT(BIC_Cor_J);
3190 BIC_PRESENT(BIC_GFX_J);
3191 } else {
3192 BIC_PRESENT(BIC_PkgWatt);
3193 BIC_PRESENT(BIC_CorWatt);
3194 BIC_PRESENT(BIC_GFXWatt);
3195 }
Len Brown889facb2012-11-08 00:48:57 -05003196 break;
Len Brown869ce692016-06-16 23:22:37 -04003197 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003198 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne4085d52016-04-06 17:15:56 -04003199 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003200 if (rapl_joules)
3201 BIC_PRESENT(BIC_Pkg_J);
3202 else
3203 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003204 break;
Len Brown869ce692016-06-16 23:22:37 -04003205 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3206 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3207 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3208 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003209 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 -05003210 BIC_PRESENT(BIC_PKG__);
3211 BIC_PRESENT(BIC_RAM__);
3212 if (rapl_joules) {
3213 BIC_PRESENT(BIC_Pkg_J);
3214 BIC_PRESENT(BIC_Cor_J);
3215 BIC_PRESENT(BIC_RAM_J);
3216 } else {
3217 BIC_PRESENT(BIC_PkgWatt);
3218 BIC_PRESENT(BIC_CorWatt);
3219 BIC_PRESENT(BIC_RAMWatt);
3220 }
Len Brown0b2bb692015-03-26 00:50:30 -04003221 break;
Len Brown869ce692016-06-16 23:22:37 -04003222 case INTEL_FAM6_HASWELL_X: /* HSX */
3223 case INTEL_FAM6_BROADWELL_X: /* BDX */
3224 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3225 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3226 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003227 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003228 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 -05003229 BIC_PRESENT(BIC_PKG__);
3230 BIC_PRESENT(BIC_RAM__);
3231 if (rapl_joules) {
3232 BIC_PRESENT(BIC_Pkg_J);
3233 BIC_PRESENT(BIC_RAM_J);
3234 } else {
3235 BIC_PRESENT(BIC_PkgWatt);
3236 BIC_PRESENT(BIC_RAMWatt);
3237 }
Len Browne6f9bb32013-12-03 02:19:19 -05003238 break;
Len Brown869ce692016-06-16 23:22:37 -04003239 case INTEL_FAM6_SANDYBRIDGE_X:
3240 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003241 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 -05003242 BIC_PRESENT(BIC_PKG__);
3243 BIC_PRESENT(BIC_RAM__);
3244 if (rapl_joules) {
3245 BIC_PRESENT(BIC_Pkg_J);
3246 BIC_PRESENT(BIC_Cor_J);
3247 BIC_PRESENT(BIC_RAM_J);
3248 } else {
3249 BIC_PRESENT(BIC_PkgWatt);
3250 BIC_PRESENT(BIC_CorWatt);
3251 BIC_PRESENT(BIC_RAMWatt);
3252 }
Len Brown144b44b2013-11-09 00:30:16 -05003253 break;
Len Brown869ce692016-06-16 23:22:37 -04003254 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3255 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003256 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003257 if (rapl_joules) {
3258 BIC_PRESENT(BIC_Pkg_J);
3259 BIC_PRESENT(BIC_Cor_J);
3260 } else {
3261 BIC_PRESENT(BIC_PkgWatt);
3262 BIC_PRESENT(BIC_CorWatt);
3263 }
Len Brown889facb2012-11-08 00:48:57 -05003264 break;
Len Brown869ce692016-06-16 23:22:37 -04003265 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003266 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 -05003267 BIC_PRESENT(BIC_PKG__);
3268 BIC_PRESENT(BIC_RAM__);
3269 if (rapl_joules) {
3270 BIC_PRESENT(BIC_Pkg_J);
3271 BIC_PRESENT(BIC_Cor_J);
3272 BIC_PRESENT(BIC_RAM_J);
3273 } else {
3274 BIC_PRESENT(BIC_PkgWatt);
3275 BIC_PRESENT(BIC_CorWatt);
3276 BIC_PRESENT(BIC_RAMWatt);
3277 }
Jacob Pan0f644902016-06-16 09:48:22 -07003278 break;
Len Brown889facb2012-11-08 00:48:57 -05003279 default:
3280 return;
3281 }
3282
3283 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003284 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003285 return;
3286
3287 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003288 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003289 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3290 else
3291 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003292
Andrey Semin40ee8e32014-12-05 00:07:00 -05003293 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3294
Len Brown144b44b2013-11-09 00:30:16 -05003295 time_unit = msr >> 16 & 0xF;
3296 if (time_unit == 0)
3297 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003298
Len Brown144b44b2013-11-09 00:30:16 -05003299 rapl_time_units = 1.0 / (1 << (time_unit));
3300
3301 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003302
3303 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown96e47152017-01-21 02:26:00 -05003304 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003305 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003306
3307 return;
3308}
3309
Colin Ian King1b693172016-03-02 13:50:25 +00003310void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003311{
3312 if (!genuine_intel)
3313 return;
3314
3315 if (family != 6)
3316 return;
3317
3318 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003319 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3320 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3321 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003322 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003323 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003324 do_core_perf_limit_reasons = 1;
3325 do_ring_perf_limit_reasons = 1;
3326 default:
3327 return;
3328 }
3329}
3330
Len Brown889facb2012-11-08 00:48:57 -05003331int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3332{
3333 unsigned long long msr;
3334 unsigned int dts;
3335 int cpu;
3336
3337 if (!(do_dts || do_ptm))
3338 return 0;
3339
3340 cpu = t->cpu_id;
3341
3342 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003343 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003344 return 0;
3345
3346 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003347 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003348 return -1;
3349 }
3350
3351 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3352 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3353 return 0;
3354
3355 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003356 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003357 cpu, msr, tcc_activation_temp - dts);
3358
3359#ifdef THERM_DEBUG
3360 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3361 return 0;
3362
3363 dts = (msr >> 16) & 0x7F;
3364 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003365 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003366 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3367#endif
3368 }
3369
3370
3371 if (do_dts) {
3372 unsigned int resolution;
3373
3374 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3375 return 0;
3376
3377 dts = (msr >> 16) & 0x7F;
3378 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003379 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003380 cpu, msr, tcc_activation_temp - dts, resolution);
3381
3382#ifdef THERM_DEBUG
3383 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3384 return 0;
3385
3386 dts = (msr >> 16) & 0x7F;
3387 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003388 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003389 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3390#endif
3391 }
3392
3393 return 0;
3394}
Len Brown36229892016-02-26 20:51:02 -05003395
Len Brown889facb2012-11-08 00:48:57 -05003396void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3397{
Len Brownb7d8c142016-02-13 23:36:17 -05003398 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003399 cpu, label,
3400 ((msr >> 15) & 1) ? "EN" : "DIS",
3401 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3402 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3403 (((msr >> 16) & 1) ? "EN" : "DIS"));
3404
3405 return;
3406}
3407
3408int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3409{
3410 unsigned long long msr;
3411 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003412
3413 if (!do_rapl)
3414 return 0;
3415
3416 /* RAPL counters are per package, so print only for 1st thread/package */
3417 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3418 return 0;
3419
3420 cpu = t->cpu_id;
3421 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003422 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003423 return -1;
3424 }
3425
3426 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3427 return -1;
3428
Len Brown96e47152017-01-21 02:26:00 -05003429 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3430 rapl_power_units, rapl_energy_units, rapl_time_units);
3431
Len Brown144b44b2013-11-09 00:30:16 -05003432 if (do_rapl & RAPL_PKG_POWER_INFO) {
3433
Len Brown889facb2012-11-08 00:48:57 -05003434 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3435 return -5;
3436
3437
Len Brownb7d8c142016-02-13 23:36:17 -05003438 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 -05003439 cpu, msr,
3440 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3441 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3442 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3443 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3444
Len Brown144b44b2013-11-09 00:30:16 -05003445 }
3446 if (do_rapl & RAPL_PKG) {
3447
Len Brown889facb2012-11-08 00:48:57 -05003448 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3449 return -9;
3450
Len Brownb7d8c142016-02-13 23:36:17 -05003451 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003452 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003453
3454 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003455 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003456 cpu,
3457 ((msr >> 47) & 1) ? "EN" : "DIS",
3458 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3459 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3460 ((msr >> 48) & 1) ? "EN" : "DIS");
3461 }
3462
Len Brown0b2bb692015-03-26 00:50:30 -04003463 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003464 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3465 return -6;
3466
Len Brownb7d8c142016-02-13 23:36:17 -05003467 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 -05003468 cpu, msr,
3469 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3470 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3471 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3472 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003473 }
3474 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003475 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3476 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003477 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown96e47152017-01-21 02:26:00 -05003478 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
Len Brown889facb2012-11-08 00:48:57 -05003479
3480 print_power_limit_msr(cpu, msr, "DRAM Limit");
3481 }
Len Brown144b44b2013-11-09 00:30:16 -05003482 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown96e47152017-01-21 02:26:00 -05003483 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3484 return -7;
Len Brown889facb2012-11-08 00:48:57 -05003485
Len Brown96e47152017-01-21 02:26:00 -05003486 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003487 }
Jacob Pan91484942016-06-16 09:48:20 -07003488 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brown96e47152017-01-21 02:26:00 -05003489 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3490 return -9;
3491 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3492 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3493 print_power_limit_msr(cpu, msr, "Cores Limit");
Len Brown889facb2012-11-08 00:48:57 -05003494 }
3495 if (do_rapl & RAPL_GFX) {
Len Brown96e47152017-01-21 02:26:00 -05003496 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3497 return -8;
Len Brown889facb2012-11-08 00:48:57 -05003498
Len Brown96e47152017-01-21 02:26:00 -05003499 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003500
Len Brown96e47152017-01-21 02:26:00 -05003501 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3502 return -9;
3503 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3504 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3505 print_power_limit_msr(cpu, msr, "GFX Limit");
Len Brown889facb2012-11-08 00:48:57 -05003506 }
3507 return 0;
3508}
3509
Len Brownd7899442015-01-23 00:12:33 -05003510/*
3511 * SNB adds support for additional MSRs:
3512 *
3513 * MSR_PKG_C7_RESIDENCY 0x000003fa
3514 * MSR_CORE_C7_RESIDENCY 0x000003fe
3515 * MSR_PKG_C2_RESIDENCY 0x0000060d
3516 */
Len Brown103a8fe2010-10-22 23:53:03 -04003517
Len Brownd7899442015-01-23 00:12:33 -05003518int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003519{
3520 if (!genuine_intel)
3521 return 0;
3522
3523 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003524 case INTEL_FAM6_SANDYBRIDGE:
3525 case INTEL_FAM6_SANDYBRIDGE_X:
3526 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3527 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3528 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3529 case INTEL_FAM6_HASWELL_X: /* HSW */
3530 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3531 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3532 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3533 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3534 case INTEL_FAM6_BROADWELL_X: /* BDX */
3535 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3536 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3537 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3538 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3539 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3540 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3541 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003542 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003543 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003544 return 1;
3545 }
3546 return 0;
3547}
3548
Len Brownd7899442015-01-23 00:12:33 -05003549/*
3550 * HSW adds support for additional MSRs:
3551 *
Len Brown5a634262016-04-06 17:15:55 -04003552 * MSR_PKG_C8_RESIDENCY 0x00000630
3553 * MSR_PKG_C9_RESIDENCY 0x00000631
3554 * MSR_PKG_C10_RESIDENCY 0x00000632
3555 *
3556 * MSR_PKGC8_IRTL 0x00000633
3557 * MSR_PKGC9_IRTL 0x00000634
3558 * MSR_PKGC10_IRTL 0x00000635
3559 *
Len Brownd7899442015-01-23 00:12:33 -05003560 */
3561int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003562{
3563 if (!genuine_intel)
3564 return 0;
3565
3566 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003567 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3568 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3569 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3570 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3571 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3572 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3573 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003574 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003575 return 1;
3576 }
3577 return 0;
3578}
3579
Len Brown0b2bb692015-03-26 00:50:30 -04003580/*
3581 * SKL adds support for additional MSRS:
3582 *
3583 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3584 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3585 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3586 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3587 */
3588int has_skl_msrs(unsigned int family, unsigned int model)
3589{
3590 if (!genuine_intel)
3591 return 0;
3592
3593 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003594 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3595 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3596 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3597 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003598 return 1;
3599 }
3600 return 0;
3601}
3602
Len Brown144b44b2013-11-09 00:30:16 -05003603int is_slm(unsigned int family, unsigned int model)
3604{
3605 if (!genuine_intel)
3606 return 0;
3607 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003608 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3609 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003610 return 1;
3611 }
3612 return 0;
3613}
3614
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003615int is_knl(unsigned int family, unsigned int model)
3616{
3617 if (!genuine_intel)
3618 return 0;
3619 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003620 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003621 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003622 return 1;
3623 }
3624 return 0;
3625}
3626
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003627unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3628{
3629 if (is_knl(family, model))
3630 return 1024;
3631 return 1;
3632}
3633
Len Brown144b44b2013-11-09 00:30:16 -05003634#define SLM_BCLK_FREQS 5
3635double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3636
3637double slm_bclk(void)
3638{
3639 unsigned long long msr = 3;
3640 unsigned int i;
3641 double freq;
3642
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003643 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003644 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003645
3646 i = msr & 0xf;
3647 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003648 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003649 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003650 }
3651 freq = slm_freq_table[i];
3652
Len Brown96e47152017-01-21 02:26:00 -05003653 if (!quiet)
Len Brown8f6196c2017-01-07 22:40:23 -05003654 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003655
3656 return freq;
3657}
3658
Len Brown103a8fe2010-10-22 23:53:03 -04003659double discover_bclk(unsigned int family, unsigned int model)
3660{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003661 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003662 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003663 else if (is_slm(family, model))
3664 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003665 else
3666 return 133.33;
3667}
3668
Len Brown889facb2012-11-08 00:48:57 -05003669/*
3670 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3671 * the Thermal Control Circuit (TCC) activates.
3672 * This is usually equal to tjMax.
3673 *
3674 * Older processors do not have this MSR, so there we guess,
3675 * but also allow cmdline over-ride with -T.
3676 *
3677 * Several MSR temperature values are in units of degrees-C
3678 * below this value, including the Digital Thermal Sensor (DTS),
3679 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3680 */
3681int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3682{
3683 unsigned long long msr;
3684 unsigned int target_c_local;
3685 int cpu;
3686
3687 /* tcc_activation_temp is used only for dts or ptm */
3688 if (!(do_dts || do_ptm))
3689 return 0;
3690
3691 /* this is a per-package concept */
3692 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3693 return 0;
3694
3695 cpu = t->cpu_id;
3696 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003697 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003698 return -1;
3699 }
3700
3701 if (tcc_activation_temp_override != 0) {
3702 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003703 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003704 cpu, tcc_activation_temp);
3705 return 0;
3706 }
3707
3708 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003709 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003710 goto guess;
3711
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003712 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003713 goto guess;
3714
Jean Delvare34821242014-05-01 11:40:19 +02003715 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003716
Len Brown96e47152017-01-21 02:26:00 -05003717 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003718 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003719 cpu, msr, target_c_local);
3720
Jean Delvare34821242014-05-01 11:40:19 +02003721 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003722 goto guess;
3723
3724 tcc_activation_temp = target_c_local;
3725
3726 return 0;
3727
3728guess:
3729 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003730 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003731 cpu, tcc_activation_temp);
3732
3733 return 0;
3734}
Len Brown69807a62015-11-21 12:22:47 -05003735
Len Brownaa8d8cc2016-03-11 13:26:03 -05003736void decode_feature_control_msr(void)
3737{
3738 unsigned long long msr;
3739
3740 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3741 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3742 base_cpu, msr,
3743 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3744 msr & (1 << 18) ? "SGX" : "");
3745}
3746
Len Brown69807a62015-11-21 12:22:47 -05003747void decode_misc_enable_msr(void)
3748{
3749 unsigned long long msr;
3750
3751 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003752 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003753 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003754 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3755 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3756 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3757 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3758 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003759}
3760
Len Brown33148d62017-01-21 01:26:16 -05003761void decode_misc_feature_control(void)
3762{
3763 unsigned long long msr;
3764
3765 if (!has_misc_feature_control)
3766 return;
3767
3768 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
3769 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
3770 base_cpu, msr,
3771 msr & (0 << 0) ? "No-" : "",
3772 msr & (1 << 0) ? "No-" : "",
3773 msr & (2 << 0) ? "No-" : "",
3774 msr & (3 << 0) ? "No-" : "");
3775}
Len Brownf0057312015-12-03 01:35:36 -05003776/*
3777 * Decode MSR_MISC_PWR_MGMT
3778 *
3779 * Decode the bits according to the Nehalem documentation
3780 * bit[0] seems to continue to have same meaning going forward
3781 * bit[1] less so...
3782 */
3783void decode_misc_pwr_mgmt_msr(void)
3784{
3785 unsigned long long msr;
3786
3787 if (!do_nhm_platform_info)
3788 return;
3789
Len Browncf4cbe52017-01-01 13:08:33 -05003790 if (no_MSR_MISC_PWR_MGMT)
3791 return;
3792
Len Brownf0057312015-12-03 01:35:36 -05003793 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003794 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 -05003795 base_cpu, msr,
3796 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003797 msr & (1 << 1) ? "EN" : "DIS",
3798 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003799}
Len Brown71616c82017-01-07 22:37:48 -05003800/*
3801 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3802 *
3803 * This MSRs are present on Silvermont processors,
3804 * Intel Atom processor E3000 series (Baytrail), and friends.
3805 */
3806void decode_c6_demotion_policy_msr(void)
3807{
3808 unsigned long long msr;
3809
3810 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3811 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3812 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3813
3814 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3815 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3816 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3817}
Len Brown7f5c2582015-12-01 01:36:39 -05003818
Len Brownfcd17212015-03-23 20:29:09 -04003819void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003820{
Len Brown61a87ba2015-11-23 02:30:51 -05003821 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003822 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003823 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003824
3825 eax = ebx = ecx = edx = 0;
3826
Len Brown5aea2f72016-03-13 03:14:35 -04003827 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003828
3829 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3830 genuine_intel = 1;
3831
Len Brown96e47152017-01-21 02:26:00 -05003832 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003833 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003834 (char *)&ebx, (char *)&edx, (char *)&ecx);
3835
Len Brown5aea2f72016-03-13 03:14:35 -04003836 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003837 family = (fms >> 8) & 0xf;
3838 model = (fms >> 4) & 0xf;
3839 stepping = fms & 0xf;
3840 if (family == 6 || family == 0xf)
3841 model += ((fms >> 16) & 0xf) << 4;
3842
Len Brown96e47152017-01-21 02:26:00 -05003843 if (!quiet) {
Len Brownb7d8c142016-02-13 23:36:17 -05003844 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003845 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003846 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003847 ecx & (1 << 0) ? "SSE3" : "-",
3848 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003849 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003850 ecx & (1 << 7) ? "EIST" : "-",
3851 ecx & (1 << 8) ? "TM2" : "-",
3852 edx & (1 << 4) ? "TSC" : "-",
3853 edx & (1 << 5) ? "MSR" : "-",
3854 edx & (1 << 22) ? "ACPI-TM" : "-",
3855 edx & (1 << 29) ? "TM" : "-");
3856 }
Len Brown103a8fe2010-10-22 23:53:03 -04003857
Josh Triplettb2c95d92013-08-20 17:20:18 -07003858 if (!(edx & (1 << 5)))
3859 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003860
3861 /*
3862 * check max extended function levels of CPUID.
3863 * This is needed to check for invariant TSC.
3864 * This check is valid for both Intel and AMD.
3865 */
3866 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003867 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003868
Len Brown61a87ba2015-11-23 02:30:51 -05003869 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003870
Len Brownd7899442015-01-23 00:12:33 -05003871 /*
3872 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3873 * this check is valid for both Intel and AMD
3874 */
Len Brown5aea2f72016-03-13 03:14:35 -04003875 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003876 has_invariant_tsc = edx & (1 << 8);
3877 }
Len Brown103a8fe2010-10-22 23:53:03 -04003878
3879 /*
3880 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3881 * this check is valid for both Intel and AMD
3882 */
3883
Len Brown5aea2f72016-03-13 03:14:35 -04003884 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003885 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003886 if (has_aperf) {
3887 BIC_PRESENT(BIC_Avg_MHz);
3888 BIC_PRESENT(BIC_Busy);
3889 BIC_PRESENT(BIC_Bzy_MHz);
3890 }
Len Brown889facb2012-11-08 00:48:57 -05003891 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003892 if (do_dts)
3893 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05003894 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05003895 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003896 if (do_ptm)
3897 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003898 has_hwp = eax & (1 << 7);
3899 has_hwp_notify = eax & (1 << 8);
3900 has_hwp_activity_window = eax & (1 << 9);
3901 has_hwp_epp = eax & (1 << 10);
3902 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003903 has_epb = ecx & (1 << 3);
3904
Len Brown96e47152017-01-21 02:26:00 -05003905 if (!quiet)
Len Brownb3a34e92017-01-21 00:50:08 -05003906 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003907 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3908 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05003909 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05003910 do_dts ? "" : "No-",
3911 do_ptm ? "" : "No-",
3912 has_hwp ? "" : "No-",
3913 has_hwp_notify ? "" : "No-",
3914 has_hwp_activity_window ? "" : "No-",
3915 has_hwp_epp ? "" : "No-",
3916 has_hwp_pkg ? "" : "No-",
3917 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003918
Len Brown96e47152017-01-21 02:26:00 -05003919 if (!quiet)
Len Brown69807a62015-11-21 12:22:47 -05003920 decode_misc_enable_msr();
3921
Len Brown33148d62017-01-21 01:26:16 -05003922
Len Brown96e47152017-01-21 02:26:00 -05003923 if (max_level >= 0x7 && !quiet) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003924 int has_sgx;
3925
3926 ecx = 0;
3927
3928 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3929
3930 has_sgx = ebx & (1 << 2);
3931 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3932
3933 if (has_sgx)
3934 decode_feature_control_msr();
3935 }
3936
Len Brown61a87ba2015-11-23 02:30:51 -05003937 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003938 unsigned int eax_crystal;
3939 unsigned int ebx_tsc;
3940
3941 /*
3942 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3943 */
3944 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003945 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003946
3947 if (ebx_tsc != 0) {
3948
Len Brown96e47152017-01-21 02:26:00 -05003949 if (!quiet && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003950 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003951 eax_crystal, ebx_tsc, crystal_hz);
3952
3953 if (crystal_hz == 0)
3954 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04003955 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3956 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3957 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3958 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003959 crystal_hz = 24000000; /* 24.0 MHz */
3960 break;
Len Brown869ce692016-06-16 23:22:37 -04003961 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003962 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003963 crystal_hz = 25000000; /* 25.0 MHz */
3964 break;
Len Brown869ce692016-06-16 23:22:37 -04003965 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Brownac01ac12017-01-27 01:45:35 -05003966 case INTEL_FAM6_ATOM_GEMINI_LAKE:
Len Browne8efbc82016-04-06 17:15:57 -04003967 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003968 break;
3969 default:
3970 crystal_hz = 0;
3971 }
3972
3973 if (crystal_hz) {
3974 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
Len Brown96e47152017-01-21 02:26:00 -05003975 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003976 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003977 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3978 }
3979 }
3980 }
Len Brown61a87ba2015-11-23 02:30:51 -05003981 if (max_level >= 0x16) {
3982 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3983
3984 /*
3985 * CPUID 16H Base MHz, Max MHz, Bus MHz
3986 */
3987 base_mhz = max_mhz = bus_mhz = edx = 0;
3988
Len Brown5aea2f72016-03-13 03:14:35 -04003989 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown96e47152017-01-21 02:26:00 -05003990 if (!quiet)
Len Brownb7d8c142016-02-13 23:36:17 -05003991 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003992 base_mhz, max_mhz, bus_mhz);
3993 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003994
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003995 if (has_aperf)
3996 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3997
Len Brown812db3f2017-02-10 00:25:41 -05003998 BIC_PRESENT(BIC_IRQ);
3999 BIC_PRESENT(BIC_TSC_MHz);
4000
4001 if (probe_nhm_msrs(family, model)) {
4002 do_nhm_platform_info = 1;
4003 BIC_PRESENT(BIC_CPU_c1);
4004 BIC_PRESENT(BIC_CPU_c3);
4005 BIC_PRESENT(BIC_CPU_c6);
4006 BIC_PRESENT(BIC_SMI);
4007 }
Len Brownd7899442015-01-23 00:12:33 -05004008 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05004009
4010 if (do_snb_cstates)
4011 BIC_PRESENT(BIC_CPU_c7);
4012
Len Brown5a634262016-04-06 17:15:55 -04004013 do_irtl_snb = has_snb_msrs(family, model);
Len Brown0f47c082017-01-27 00:50:45 -05004014 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4015 BIC_PRESENT(BIC_Pkgpc2);
4016 if (pkg_cstate_limit >= PCL__3)
4017 BIC_PRESENT(BIC_Pkgpc3);
4018 if (pkg_cstate_limit >= PCL__6)
4019 BIC_PRESENT(BIC_Pkgpc6);
4020 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4021 BIC_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004022 if (has_slv_msrs(family, model)) {
Len Brown0f47c082017-01-27 00:50:45 -05004023 BIC_NOT_PRESENT(BIC_Pkgpc2);
4024 BIC_NOT_PRESENT(BIC_Pkgpc3);
4025 BIC_PRESENT(BIC_Pkgpc6);
4026 BIC_NOT_PRESENT(BIC_Pkgpc7);
Len Brown0539ba12017-02-10 00:27:20 -05004027 BIC_PRESENT(BIC_Mod_c6);
4028 use_c1_residency_msr = 1;
4029 }
Len Brown7170a372017-01-27 02:13:27 -05004030 if (is_dnv(family, model)) {
4031 BIC_PRESENT(BIC_CPU_c1);
4032 BIC_NOT_PRESENT(BIC_CPU_c3);
4033 BIC_NOT_PRESENT(BIC_Pkgpc3);
4034 BIC_NOT_PRESENT(BIC_CPU_c7);
4035 BIC_NOT_PRESENT(BIC_Pkgpc7);
4036 use_c1_residency_msr = 1;
4037 }
Len Brown34c761972017-01-27 02:36:41 -05004038 if (is_skx(family, model)) {
4039 BIC_NOT_PRESENT(BIC_CPU_c3);
4040 BIC_NOT_PRESENT(BIC_Pkgpc3);
4041 BIC_NOT_PRESENT(BIC_CPU_c7);
4042 BIC_NOT_PRESENT(BIC_Pkgpc7);
4043 }
Len Brownade0eba2017-02-10 01:56:47 -05004044 if (is_bdx(family, model)) {
4045 BIC_NOT_PRESENT(BIC_CPU_c7);
4046 BIC_NOT_PRESENT(BIC_Pkgpc7);
4047 }
Len Brown0f47c082017-01-27 00:50:45 -05004048 if (has_hsw_msrs(family, model)) {
4049 BIC_PRESENT(BIC_Pkgpc8);
4050 BIC_PRESENT(BIC_Pkgpc9);
4051 BIC_PRESENT(BIC_Pkgpc10);
4052 }
Len Brown5a634262016-04-06 17:15:55 -04004053 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04004054 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05004055 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07004056 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04004057
Len Brown96e47152017-01-21 02:26:00 -05004058 if (!quiet)
Len Brownf0057312015-12-03 01:35:36 -05004059 decode_misc_pwr_mgmt_msr();
4060
Len Brown96e47152017-01-21 02:26:00 -05004061 if (!quiet && has_slv_msrs(family, model))
Len Brown71616c82017-01-07 22:37:48 -05004062 decode_c6_demotion_policy_msr();
4063
Len Brown889facb2012-11-08 00:48:57 -05004064 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04004065 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05004066
Len Brown96e47152017-01-21 02:26:00 -05004067 if (!quiet)
Colin Ian King1b693172016-03-02 13:50:25 +00004068 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04004069
Len Brown41618e62017-02-09 18:25:22 -05004070 if (!quiet)
4071 dump_sysfs_cstate_config();
4072
Len Browna2b7b742015-09-26 00:12:38 -04004073 if (has_skl_msrs(family, model))
4074 calculate_tsc_tweak();
4075
Len Brown812db3f2017-02-10 00:25:41 -05004076 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4077 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05004078
Len Brown812db3f2017-02-10 00:25:41 -05004079 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4080 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05004081
Len Brown96e47152017-01-21 02:26:00 -05004082 if (!quiet)
Len Brown33148d62017-01-21 01:26:16 -05004083 decode_misc_feature_control();
4084
Len Brown889facb2012-11-08 00:48:57 -05004085 return;
Len Brown103a8fe2010-10-22 23:53:03 -04004086}
4087
Len Brownd8af6f52015-02-10 01:56:38 -05004088void help()
Len Brown103a8fe2010-10-22 23:53:03 -04004089{
Len Brownb7d8c142016-02-13 23:36:17 -05004090 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05004091 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
4092 "\n"
4093 "Turbostat forks the specified COMMAND and prints statistics\n"
4094 "when COMMAND completes.\n"
4095 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
4096 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05004097 "--add add a counter\n"
4098 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brown96e47152017-01-21 02:26:00 -05004099 "--quiet skip decoding system configuration header\n"
Len Brownd8af6f52015-02-10 01:56:38 -05004100 "--interval sec Override default 5-second measurement interval\n"
4101 "--help print this help message\n"
Len Brownb7d8c142016-02-13 23:36:17 -05004102 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05004103 "--version print version information\n"
4104 "\n"
4105 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04004106}
4107
4108
4109/*
4110 * in /dev/cpu/ return success for names that are numbers
4111 * ie. filter out ".", "..", "microcode".
4112 */
4113int dir_filter(const struct dirent *dirp)
4114{
4115 if (isdigit(dirp->d_name[0]))
4116 return 1;
4117 else
4118 return 0;
4119}
4120
4121int open_dev_cpu_msr(int dummy1)
4122{
4123 return 0;
4124}
4125
Len Brownc98d5d92012-06-04 00:56:40 -04004126void topology_probe()
4127{
4128 int i;
4129 int max_core_id = 0;
4130 int max_package_id = 0;
4131 int max_siblings = 0;
4132 struct cpu_topology {
4133 int core_id;
4134 int physical_package_id;
4135 } *cpus;
4136
4137 /* Initialize num_cpus, max_cpu_num */
4138 topo.num_cpus = 0;
4139 topo.max_cpu_num = 0;
4140 for_all_proc_cpus(count_cpus);
4141 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004142 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04004143
Len Brownd8af6f52015-02-10 01:56:38 -05004144 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004145 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04004146
4147 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004148 if (cpus == NULL)
4149 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04004150
4151 /*
4152 * Allocate and initialize cpu_present_set
4153 */
4154 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004155 if (cpu_present_set == NULL)
4156 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004157 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4158 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4159 for_all_proc_cpus(mark_cpu_present);
4160
4161 /*
4162 * Allocate and initialize cpu_affinity_set
4163 */
4164 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07004165 if (cpu_affinity_set == NULL)
4166 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04004167 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4168 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4169
4170
4171 /*
4172 * For online cpus
4173 * find max_core_id, max_package_id
4174 */
4175 for (i = 0; i <= topo.max_cpu_num; ++i) {
4176 int siblings;
4177
4178 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05004179 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004180 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04004181 continue;
4182 }
4183 cpus[i].core_id = get_core_id(i);
4184 if (cpus[i].core_id > max_core_id)
4185 max_core_id = cpus[i].core_id;
4186
4187 cpus[i].physical_package_id = get_physical_package_id(i);
4188 if (cpus[i].physical_package_id > max_package_id)
4189 max_package_id = cpus[i].physical_package_id;
4190
4191 siblings = get_num_ht_siblings(i);
4192 if (siblings > max_siblings)
4193 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004194 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004195 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004196 i, cpus[i].physical_package_id, cpus[i].core_id);
4197 }
4198 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004199 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004200 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004201 max_core_id, topo.num_cores_per_pkg);
Len Brown0f47c082017-01-27 00:50:45 -05004202 if (!summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004203 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04004204
4205 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05004206 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004207 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04004208 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05004209 if (debug && !summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05004210 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04004211
4212 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05004213 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004214 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04004215
4216 free(cpus);
4217}
4218
4219void
4220allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4221{
4222 int i;
4223
4224 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05004225 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004226 if (*t == NULL)
4227 goto error;
4228
4229 for (i = 0; i < topo.num_threads_per_core *
4230 topo.num_cores_per_pkg * topo.num_packages; i++)
4231 (*t)[i].cpu_id = -1;
4232
4233 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004234 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004235 if (*c == NULL)
4236 goto error;
4237
4238 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4239 (*c)[i].core_id = -1;
4240
Len Brown678a3bd2017-02-09 22:22:13 -05004241 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004242 if (*p == NULL)
4243 goto error;
4244
4245 for (i = 0; i < topo.num_packages; i++)
4246 (*p)[i].package_id = i;
4247
4248 return;
4249error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004250 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004251}
4252/*
4253 * init_counter()
4254 *
4255 * set cpu_id, core_num, pkg_num
4256 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4257 *
4258 * increment topo.num_cores when 1st core in pkg seen
4259 */
4260void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4261 struct pkg_data *pkg_base, int thread_num, int core_num,
4262 int pkg_num, int cpu_id)
4263{
4264 struct thread_data *t;
4265 struct core_data *c;
4266 struct pkg_data *p;
4267
4268 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4269 c = GET_CORE(core_base, core_num, pkg_num);
4270 p = GET_PKG(pkg_base, pkg_num);
4271
4272 t->cpu_id = cpu_id;
4273 if (thread_num == 0) {
4274 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4275 if (cpu_is_first_core_in_package(cpu_id))
4276 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4277 }
4278
4279 c->core_id = core_num;
4280 p->package_id = pkg_num;
4281}
4282
4283
4284int initialize_counters(int cpu_id)
4285{
4286 int my_thread_id, my_core_id, my_package_id;
4287
4288 my_package_id = get_physical_package_id(cpu_id);
4289 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004290 my_thread_id = get_cpu_position_in_core(cpu_id);
4291 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004292 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004293
4294 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4295 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4296 return 0;
4297}
4298
4299void allocate_output_buffer()
4300{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004301 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004302 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004303 if (outp == NULL)
4304 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004305}
Len Brown36229892016-02-26 20:51:02 -05004306void allocate_fd_percpu(void)
4307{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004308 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004309 if (fd_percpu == NULL)
4310 err(-1, "calloc fd_percpu");
4311}
Len Brown562a2d32016-02-26 23:48:05 -05004312void allocate_irq_buffers(void)
4313{
4314 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4315 if (irq_column_2_cpu == NULL)
4316 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004317
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004318 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004319 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004320 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004321}
Len Brownc98d5d92012-06-04 00:56:40 -04004322void setup_all_buffers(void)
4323{
4324 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004325 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004326 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004327 allocate_counters(&thread_even, &core_even, &package_even);
4328 allocate_counters(&thread_odd, &core_odd, &package_odd);
4329 allocate_output_buffer();
4330 for_all_proc_cpus(initialize_counters);
4331}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004332
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004333void set_base_cpu(void)
4334{
4335 base_cpu = sched_getcpu();
4336 if (base_cpu < 0)
4337 err(-ENODEV, "No valid cpus found");
4338
4339 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004340 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004341}
4342
Len Brown103a8fe2010-10-22 23:53:03 -04004343void turbostat_init()
4344{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004345 setup_all_buffers();
4346 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004347 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004348 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004349 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004350
Len Brown103a8fe2010-10-22 23:53:03 -04004351
Len Brown96e47152017-01-21 02:26:00 -05004352 if (!quiet)
Len Brown7f5c2582015-12-01 01:36:39 -05004353 for_all_cpus(print_hwp, ODD_COUNTERS);
4354
Len Brown96e47152017-01-21 02:26:00 -05004355 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004356 for_all_cpus(print_epb, ODD_COUNTERS);
4357
Len Brown96e47152017-01-21 02:26:00 -05004358 if (!quiet)
Len Brown3a9a9412014-08-15 02:39:52 -04004359 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4360
Len Brown96e47152017-01-21 02:26:00 -05004361 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004362 for_all_cpus(print_rapl, ODD_COUNTERS);
4363
4364 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4365
Len Brown96e47152017-01-21 02:26:00 -05004366 if (!quiet)
Len Brown889facb2012-11-08 00:48:57 -05004367 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004368
Len Brown96e47152017-01-21 02:26:00 -05004369 if (!quiet && do_irtl_snb)
Len Brown5a634262016-04-06 17:15:55 -04004370 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004371}
4372
4373int fork_it(char **argv)
4374{
Len Brown103a8fe2010-10-22 23:53:03 -04004375 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004376 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004377
Len Brownd91bb172012-11-01 00:08:19 -04004378 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4379 if (status)
4380 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004381 /* clear affinity side-effect of get_counters() */
4382 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004383 gettimeofday(&tv_even, (struct timezone *)NULL);
4384
4385 child_pid = fork();
4386 if (!child_pid) {
4387 /* child */
4388 execvp(argv[0], argv);
4389 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004390
4391 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004392 if (child_pid == -1)
4393 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004394
4395 signal(SIGINT, SIG_IGN);
4396 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004397 if (waitpid(child_pid, &status, 0) == -1)
4398 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004399 }
Len Brownc98d5d92012-06-04 00:56:40 -04004400 /*
4401 * n.b. fork_it() does not check for errors from for_all_cpus()
4402 * because re-starting is problematic when forking
4403 */
4404 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004405 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004406 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004407 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4408 fprintf(outf, "%s: Counter reset detected\n", progname);
4409 else {
4410 compute_average(EVEN_COUNTERS);
4411 format_all_counters(EVEN_COUNTERS);
4412 }
Len Brown103a8fe2010-10-22 23:53:03 -04004413
Len Brownb7d8c142016-02-13 23:36:17 -05004414 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4415
4416 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004417
Len Brownd91bb172012-11-01 00:08:19 -04004418 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004419}
4420
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004421int get_and_dump_counters(void)
4422{
4423 int status;
4424
4425 status = for_all_cpus(get_counters, ODD_COUNTERS);
4426 if (status)
4427 return status;
4428
4429 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4430 if (status)
4431 return status;
4432
Len Brownb7d8c142016-02-13 23:36:17 -05004433 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004434
4435 return status;
4436}
4437
Len Brownd8af6f52015-02-10 01:56:38 -05004438void print_version() {
Len Brown0539ba12017-02-10 00:27:20 -05004439 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004440 " - Len Brown <lenb@kernel.org>\n");
4441}
4442
Len Brown495c76542017-02-08 02:41:51 -05004443int add_counter(unsigned int msr_num, char *path, char *name,
4444 unsigned int width, enum counter_scope scope,
Len Brown41618e62017-02-09 18:25:22 -05004445 enum counter_type type, enum counter_format format, int flags)
Len Brown388e9c82016-12-22 23:57:55 -05004446{
4447 struct msr_counter *msrp;
4448
4449 msrp = calloc(1, sizeof(struct msr_counter));
4450 if (msrp == NULL) {
4451 perror("calloc");
4452 exit(1);
4453 }
4454
4455 msrp->msr_num = msr_num;
4456 strncpy(msrp->name, name, NAME_BYTES);
Len Brown495c76542017-02-08 02:41:51 -05004457 if (path)
4458 strncpy(msrp->path, path, PATH_BYTES);
Len Brown388e9c82016-12-22 23:57:55 -05004459 msrp->width = width;
4460 msrp->type = type;
4461 msrp->format = format;
Len Brown41618e62017-02-09 18:25:22 -05004462 msrp->flags = flags;
Len Brown388e9c82016-12-22 23:57:55 -05004463
4464 switch (scope) {
4465
4466 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004467 msrp->next = sys.tp;
4468 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004469 sys.added_thread_counters++;
4470 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4471 fprintf(stderr, "exceeded max %d added thread counters\n",
4472 MAX_ADDED_COUNTERS);
4473 exit(-1);
4474 }
Len Brown388e9c82016-12-22 23:57:55 -05004475 break;
4476
4477 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004478 msrp->next = sys.cp;
4479 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004480 sys.added_core_counters++;
4481 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4482 fprintf(stderr, "exceeded max %d added core counters\n",
4483 MAX_ADDED_COUNTERS);
4484 exit(-1);
4485 }
Len Brown388e9c82016-12-22 23:57:55 -05004486 break;
4487
4488 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004489 msrp->next = sys.pp;
4490 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004491 sys.added_package_counters++;
4492 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4493 fprintf(stderr, "exceeded max %d added package counters\n",
4494 MAX_ADDED_COUNTERS);
4495 exit(-1);
4496 }
Len Brown388e9c82016-12-22 23:57:55 -05004497 break;
4498 }
4499
4500 return 0;
4501}
4502
4503void parse_add_command(char *add_command)
4504{
4505 int msr_num = 0;
Len Brown495c76542017-02-08 02:41:51 -05004506 char *path = NULL;
Len Brown0f47c082017-01-27 00:50:45 -05004507 char name_buffer[NAME_BYTES] = "";
Len Brown388e9c82016-12-22 23:57:55 -05004508 int width = 64;
4509 int fail = 0;
4510 enum counter_scope scope = SCOPE_CPU;
4511 enum counter_type type = COUNTER_CYCLES;
4512 enum counter_format format = FORMAT_DELTA;
4513
4514 while (add_command) {
4515
4516 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4517 goto next;
4518
4519 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4520 goto next;
4521
Len Brown495c76542017-02-08 02:41:51 -05004522 if (*add_command == '/') {
4523 path = add_command;
4524 goto next;
4525 }
4526
Len Brown388e9c82016-12-22 23:57:55 -05004527 if (sscanf(add_command, "u%d", &width) == 1) {
4528 if ((width == 32) || (width == 64))
4529 goto next;
4530 width = 64;
4531 }
4532 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4533 scope = SCOPE_CPU;
4534 goto next;
4535 }
4536 if (!strncmp(add_command, "core", strlen("core"))) {
4537 scope = SCOPE_CORE;
4538 goto next;
4539 }
4540 if (!strncmp(add_command, "package", strlen("package"))) {
4541 scope = SCOPE_PACKAGE;
4542 goto next;
4543 }
4544 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4545 type = COUNTER_CYCLES;
4546 goto next;
4547 }
4548 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4549 type = COUNTER_SECONDS;
4550 goto next;
4551 }
Len Brown41618e62017-02-09 18:25:22 -05004552 if (!strncmp(add_command, "usec", strlen("usec"))) {
4553 type = COUNTER_USEC;
4554 goto next;
4555 }
Len Brown388e9c82016-12-22 23:57:55 -05004556 if (!strncmp(add_command, "raw", strlen("raw"))) {
4557 format = FORMAT_RAW;
4558 goto next;
4559 }
4560 if (!strncmp(add_command, "delta", strlen("delta"))) {
4561 format = FORMAT_DELTA;
4562 goto next;
4563 }
4564 if (!strncmp(add_command, "percent", strlen("percent"))) {
4565 format = FORMAT_PERCENT;
4566 goto next;
4567 }
4568
4569 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4570 char *eos;
4571
4572 eos = strchr(name_buffer, ',');
4573 if (eos)
4574 *eos = '\0';
4575 goto next;
4576 }
4577
4578next:
4579 add_command = strchr(add_command, ',');
Len Brown495c76542017-02-08 02:41:51 -05004580 if (add_command) {
4581 *add_command = '\0';
Len Brown388e9c82016-12-22 23:57:55 -05004582 add_command++;
Len Brown495c76542017-02-08 02:41:51 -05004583 }
Len Brown388e9c82016-12-22 23:57:55 -05004584
4585 }
Len Brown495c76542017-02-08 02:41:51 -05004586 if ((msr_num == 0) && (path == NULL)) {
4587 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
Len Brown388e9c82016-12-22 23:57:55 -05004588 fail++;
4589 }
4590
4591 /* generate default column header */
4592 if (*name_buffer == '\0') {
4593 if (format == FORMAT_RAW) {
4594 if (width == 32)
4595 sprintf(name_buffer, "msr%d", msr_num);
4596 else
4597 sprintf(name_buffer, "MSR%d", msr_num);
4598 } else if (format == FORMAT_DELTA) {
4599 if (width == 32)
4600 sprintf(name_buffer, "cnt%d", msr_num);
4601 else
4602 sprintf(name_buffer, "CNT%d", msr_num);
4603 } else if (format == FORMAT_PERCENT) {
4604 if (width == 32)
4605 sprintf(name_buffer, "msr%d%%", msr_num);
4606 else
4607 sprintf(name_buffer, "MSR%d%%", msr_num);
4608 }
4609 }
4610
Len Brown41618e62017-02-09 18:25:22 -05004611 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
Len Brown388e9c82016-12-22 23:57:55 -05004612 fail++;
4613
4614 if (fail) {
4615 help();
4616 exit(1);
4617 }
4618}
Len Brown41618e62017-02-09 18:25:22 -05004619
4620void probe_sysfs(void)
4621{
4622 char path[64];
4623 char name_buf[16];
4624 FILE *input;
4625 int state;
4626 char *sp;
4627
4628 if (!DO_BIC(BIC_sysfs))
4629 return;
4630
4631 for (state = 10; state > 0; --state) {
4632
4633 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4634 base_cpu, state);
4635 input = fopen(path, "r");
4636 if (input == NULL)
4637 continue;
4638 fgets(name_buf, sizeof(name_buf), input);
4639
4640 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4641 sp = strchr(name_buf, '-');
4642 if (!sp)
4643 sp = strchrnul(name_buf, '\n');
4644 *sp = '%';
4645 *(sp + 1) = '\0';
4646
4647 fclose(input);
4648
4649 sprintf(path, "cpuidle/state%d/time", state);
4650
4651 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4652 FORMAT_PERCENT, SYSFS_PERCPU);
4653 }
4654
4655 for (state = 10; state > 0; --state) {
4656
4657 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4658 base_cpu, state);
4659 input = fopen(path, "r");
4660 if (input == NULL)
4661 continue;
4662 fgets(name_buf, sizeof(name_buf), input);
4663 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4664 sp = strchr(name_buf, '-');
4665 if (!sp)
4666 sp = strchrnul(name_buf, '\n');
4667 *sp = '\0';
4668 fclose(input);
4669
4670 sprintf(path, "cpuidle/state%d/usage", state);
4671
4672 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4673 FORMAT_DELTA, SYSFS_PERCPU);
4674 }
4675
4676}
4677
Len Brown812db3f2017-02-10 00:25:41 -05004678/*
4679 * HIDE_LIST - hide this list of counters, show the rest [default]
4680 * SHOW_LIST - show this list of counters, hide the rest
4681 */
4682enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4683
4684int shown;
4685/*
4686 * parse_show_hide() - process cmdline to set default counter action
4687 */
4688void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4689{
4690 /*
4691 * --show: show only those specified
4692 * The 1st invocation will clear and replace the enabled mask
4693 * subsequent invocations can add to it.
4694 */
4695 if (new_mode == SHOW_LIST) {
4696 if (shown == 0)
4697 bic_enabled = bic_lookup(optarg);
4698 else
4699 bic_enabled |= bic_lookup(optarg);
4700 shown = 1;
4701
4702 return;
4703 }
4704
4705 /*
4706 * --hide: do not show those specified
4707 * multiple invocations simply clear more bits in enabled mask
4708 */
4709 bic_enabled &= ~bic_lookup(optarg);
Len Brown41618e62017-02-09 18:25:22 -05004710
Len Brown812db3f2017-02-10 00:25:41 -05004711}
4712
Len Brown103a8fe2010-10-22 23:53:03 -04004713void cmdline(int argc, char **argv)
4714{
4715 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004716 int option_index = 0;
4717 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004718 {"add", required_argument, 0, 'a'},
Len Brownd8af6f52015-02-10 01:56:38 -05004719 {"Dump", no_argument, 0, 'D'},
Len Brown96e47152017-01-21 02:26:00 -05004720 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
Len Brownd8af6f52015-02-10 01:56:38 -05004721 {"interval", required_argument, 0, 'i'},
4722 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004723 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004724 {"Joules", no_argument, 0, 'J'},
Len Brownb7d8c142016-02-13 23:36:17 -05004725 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004726 {"Package", no_argument, 0, 'p'},
4727 {"processor", no_argument, 0, 'p'},
Len Brown96e47152017-01-21 02:26:00 -05004728 {"quiet", no_argument, 0, 'q'},
Len Brown812db3f2017-02-10 00:25:41 -05004729 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004730 {"Summary", no_argument, 0, 'S'},
4731 {"TCC", required_argument, 0, 'T'},
4732 {"version", no_argument, 0, 'v' },
4733 {0, 0, 0, 0 }
4734 };
Len Brown103a8fe2010-10-22 23:53:03 -04004735
4736 progname = argv[0];
4737
Len Brown96e47152017-01-21 02:26:00 -05004738 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpqST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004739 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004740 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004741 case 'a':
4742 parse_add_command(optarg);
4743 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004744 case 'D':
4745 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004746 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004747 case 'd':
4748 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004749 break;
Len Brown812db3f2017-02-10 00:25:41 -05004750 case 'H':
4751 parse_show_hide(optarg, HIDE_LIST);
4752 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004753 case 'h':
4754 default:
4755 help();
4756 exit(1);
4757 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004758 {
4759 double interval = strtod(optarg, NULL);
4760
4761 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004762 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004763 interval);
4764 exit(2);
4765 }
4766
4767 interval_ts.tv_sec = interval;
4768 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4769 }
Len Brown889facb2012-11-08 00:48:57 -05004770 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004771 case 'J':
4772 rapl_joules++;
4773 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004774 case 'o':
4775 outf = fopen_or_die(optarg, "w");
4776 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004777 case 'P':
4778 show_pkg_only++;
4779 break;
4780 case 'p':
4781 show_core_only++;
4782 break;
Len Brown96e47152017-01-21 02:26:00 -05004783 case 'q':
4784 quiet = 1;
4785 break;
Len Brown812db3f2017-02-10 00:25:41 -05004786 case 's':
4787 parse_show_hide(optarg, SHOW_LIST);
4788 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004789 case 'S':
4790 summary_only++;
4791 break;
4792 case 'T':
4793 tcc_activation_temp_override = atoi(optarg);
4794 break;
4795 case 'v':
4796 print_version();
4797 exit(0);
4798 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004799 }
4800 }
4801}
4802
4803int main(int argc, char **argv)
4804{
Len Brownb7d8c142016-02-13 23:36:17 -05004805 outf = stderr;
4806
Len Brown103a8fe2010-10-22 23:53:03 -04004807 cmdline(argc, argv);
4808
Len Brown96e47152017-01-21 02:26:00 -05004809 if (!quiet)
Len Brownd8af6f52015-02-10 01:56:38 -05004810 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004811
Len Brown41618e62017-02-09 18:25:22 -05004812 probe_sysfs();
4813
Len Brown103a8fe2010-10-22 23:53:03 -04004814 turbostat_init();
4815
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004816 /* dump counters and exit */
4817 if (dump_only)
4818 return get_and_dump_counters();
4819
Len Brown103a8fe2010-10-22 23:53:03 -04004820 /*
4821 * if any params left, it must be a command to fork
4822 */
4823 if (argc - optind)
4824 return fork_it(argv + optind);
4825 else
4826 turbostat_loop();
4827
4828 return 0;
4829}